Fix access violation when disassembling a corrupt VMS binary.
[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
TT
3062 const gdb_byte *iter, *end;
3063 struct obstack temp_obstack;
3064 struct addrmap *mutable_map;
3065 struct cleanup *cleanup;
3066 CORE_ADDR baseaddr;
3067
3068 obstack_init (&temp_obstack);
3069 cleanup = make_cleanup_obstack_free (&temp_obstack);
3070 mutable_map = addrmap_create_mutable (&temp_obstack);
3071
3072 iter = index->address_table;
3073 end = iter + index->address_table_size;
3074
3075 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3076
3077 while (iter < end)
3078 {
3079 ULONGEST hi, lo, cu_index;
3080 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3081 iter += 8;
3082 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3083 iter += 8;
3084 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3085 iter += 4;
f652bce2 3086
24a55014 3087 if (lo > hi)
f652bce2 3088 {
24a55014
DE
3089 complaint (&symfile_complaints,
3090 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3091 hex_string (lo), hex_string (hi));
24a55014 3092 continue;
f652bce2 3093 }
24a55014
DE
3094
3095 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3096 {
3097 complaint (&symfile_complaints,
3098 _(".gdb_index address table has invalid CU number %u"),
3099 (unsigned) cu_index);
24a55014 3100 continue;
f652bce2 3101 }
24a55014 3102
3e29f34a
MR
3103 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3104 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3105 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3106 }
3107
3108 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3109 &objfile->objfile_obstack);
3110 do_cleanups (cleanup);
3111}
3112
59d7bcaf
JK
3113/* The hash function for strings in the mapped index. This is the same as
3114 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3115 implementation. This is necessary because the hash function is tied to the
3116 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3117 SYMBOL_HASH_NEXT.
3118
3119 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3120
9291a0cd 3121static hashval_t
559a7a62 3122mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3123{
3124 const unsigned char *str = (const unsigned char *) p;
3125 hashval_t r = 0;
3126 unsigned char c;
3127
3128 while ((c = *str++) != 0)
559a7a62
JK
3129 {
3130 if (index_version >= 5)
3131 c = tolower (c);
3132 r = r * 67 + c - 113;
3133 }
9291a0cd
TT
3134
3135 return r;
3136}
3137
3138/* Find a slot in the mapped index INDEX for the object named NAME.
3139 If NAME is found, set *VEC_OUT to point to the CU vector in the
3140 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3141
9291a0cd
TT
3142static int
3143find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3144 offset_type **vec_out)
3145{
0cf03b49
JK
3146 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3147 offset_type hash;
9291a0cd 3148 offset_type slot, step;
559a7a62 3149 int (*cmp) (const char *, const char *);
9291a0cd 3150
0cf03b49 3151 if (current_language->la_language == language_cplus
45280282
IB
3152 || current_language->la_language == language_fortran
3153 || current_language->la_language == language_d)
0cf03b49
JK
3154 {
3155 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3156 not contain any. */
a8719064 3157
72998fb3 3158 if (strchr (name, '(') != NULL)
0cf03b49 3159 {
72998fb3 3160 char *without_params = cp_remove_params (name);
0cf03b49 3161
72998fb3
DE
3162 if (without_params != NULL)
3163 {
3164 make_cleanup (xfree, without_params);
3165 name = without_params;
3166 }
0cf03b49
JK
3167 }
3168 }
3169
559a7a62 3170 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3171 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3172 simulate our NAME being searched is also lowercased. */
3173 hash = mapped_index_string_hash ((index->version == 4
3174 && case_sensitivity == case_sensitive_off
3175 ? 5 : index->version),
3176 name);
3177
3876f04e
DE
3178 slot = hash & (index->symbol_table_slots - 1);
3179 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3180 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3181
3182 for (;;)
3183 {
3184 /* Convert a slot number to an offset into the table. */
3185 offset_type i = 2 * slot;
3186 const char *str;
3876f04e 3187 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3188 {
3189 do_cleanups (back_to);
3190 return 0;
3191 }
9291a0cd 3192
3876f04e 3193 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3194 if (!cmp (name, str))
9291a0cd
TT
3195 {
3196 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3197 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3198 do_cleanups (back_to);
9291a0cd
TT
3199 return 1;
3200 }
3201
3876f04e 3202 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3203 }
3204}
3205
2ec9a5e0
TT
3206/* A helper function that reads the .gdb_index from SECTION and fills
3207 in MAP. FILENAME is the name of the file containing the section;
3208 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3209 ok to use deprecated sections.
3210
3211 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3212 out parameters that are filled in with information about the CU and
3213 TU lists in the section.
3214
3215 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3216
9291a0cd 3217static int
2ec9a5e0
TT
3218read_index_from_section (struct objfile *objfile,
3219 const char *filename,
3220 int deprecated_ok,
3221 struct dwarf2_section_info *section,
3222 struct mapped_index *map,
3223 const gdb_byte **cu_list,
3224 offset_type *cu_list_elements,
3225 const gdb_byte **types_list,
3226 offset_type *types_list_elements)
9291a0cd 3227{
948f8e3d 3228 const gdb_byte *addr;
2ec9a5e0 3229 offset_type version;
b3b272e1 3230 offset_type *metadata;
1fd400ff 3231 int i;
9291a0cd 3232
2ec9a5e0 3233 if (dwarf2_section_empty_p (section))
9291a0cd 3234 return 0;
82430852
JK
3235
3236 /* Older elfutils strip versions could keep the section in the main
3237 executable while splitting it for the separate debug info file. */
a32a8923 3238 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3239 return 0;
3240
2ec9a5e0 3241 dwarf2_read_section (objfile, section);
9291a0cd 3242
2ec9a5e0 3243 addr = section->buffer;
9291a0cd 3244 /* Version check. */
1fd400ff 3245 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3246 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3247 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3248 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3249 indices. */
831adc1f 3250 if (version < 4)
481860b3
GB
3251 {
3252 static int warning_printed = 0;
3253 if (!warning_printed)
3254 {
3255 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3256 filename);
481860b3
GB
3257 warning_printed = 1;
3258 }
3259 return 0;
3260 }
3261 /* Index version 4 uses a different hash function than index version
3262 5 and later.
3263
3264 Versions earlier than 6 did not emit psymbols for inlined
3265 functions. Using these files will cause GDB not to be able to
3266 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3267 indices unless the user has done
3268 "set use-deprecated-index-sections on". */
2ec9a5e0 3269 if (version < 6 && !deprecated_ok)
481860b3
GB
3270 {
3271 static int warning_printed = 0;
3272 if (!warning_printed)
3273 {
e615022a
DE
3274 warning (_("\
3275Skipping deprecated .gdb_index section in %s.\n\
3276Do \"set use-deprecated-index-sections on\" before the file is read\n\
3277to use the section anyway."),
2ec9a5e0 3278 filename);
481860b3
GB
3279 warning_printed = 1;
3280 }
3281 return 0;
3282 }
796a7ff8 3283 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3284 of the TU (for symbols coming from TUs),
3285 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3286 Plus gold-generated indices can have duplicate entries for global symbols,
3287 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3288 These are just performance bugs, and we can't distinguish gdb-generated
3289 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3290
481860b3 3291 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3292 longer backward compatible. */
796a7ff8 3293 if (version > 8)
594e8718 3294 return 0;
9291a0cd 3295
559a7a62 3296 map->version = version;
2ec9a5e0 3297 map->total_size = section->size;
9291a0cd
TT
3298
3299 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3300
3301 i = 0;
2ec9a5e0
TT
3302 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3303 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3304 / 8);
1fd400ff
TT
3305 ++i;
3306
2ec9a5e0
TT
3307 *types_list = addr + MAYBE_SWAP (metadata[i]);
3308 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3309 - MAYBE_SWAP (metadata[i]))
3310 / 8);
987d643c 3311 ++i;
1fd400ff
TT
3312
3313 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3314 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3315 - MAYBE_SWAP (metadata[i]));
3316 ++i;
3317
3876f04e
DE
3318 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3319 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3320 - MAYBE_SWAP (metadata[i]))
3321 / (2 * sizeof (offset_type)));
1fd400ff 3322 ++i;
9291a0cd 3323
f9d83a0b 3324 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3325
2ec9a5e0
TT
3326 return 1;
3327}
3328
3329
3330/* Read the index file. If everything went ok, initialize the "quick"
3331 elements of all the CUs and return 1. Otherwise, return 0. */
3332
3333static int
3334dwarf2_read_index (struct objfile *objfile)
3335{
3336 struct mapped_index local_map, *map;
3337 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3338 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3339 struct dwz_file *dwz;
2ec9a5e0 3340
4262abfb 3341 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3342 use_deprecated_index_sections,
3343 &dwarf2_per_objfile->gdb_index, &local_map,
3344 &cu_list, &cu_list_elements,
3345 &types_list, &types_list_elements))
3346 return 0;
3347
0fefef59 3348 /* Don't use the index if it's empty. */
2ec9a5e0 3349 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3350 return 0;
3351
2ec9a5e0
TT
3352 /* If there is a .dwz file, read it so we can get its CU list as
3353 well. */
4db1a1dc
TT
3354 dwz = dwarf2_get_dwz_file ();
3355 if (dwz != NULL)
2ec9a5e0 3356 {
2ec9a5e0
TT
3357 struct mapped_index dwz_map;
3358 const gdb_byte *dwz_types_ignore;
3359 offset_type dwz_types_elements_ignore;
3360
3361 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3362 1,
3363 &dwz->gdb_index, &dwz_map,
3364 &dwz_list, &dwz_list_elements,
3365 &dwz_types_ignore,
3366 &dwz_types_elements_ignore))
3367 {
3368 warning (_("could not read '.gdb_index' section from %s; skipping"),
3369 bfd_get_filename (dwz->dwz_bfd));
3370 return 0;
3371 }
3372 }
3373
74a0d9f6
JK
3374 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3375 dwz_list_elements);
1fd400ff 3376
8b70b953
TT
3377 if (types_list_elements)
3378 {
3379 struct dwarf2_section_info *section;
3380
3381 /* We can only handle a single .debug_types when we have an
3382 index. */
3383 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3384 return 0;
3385
3386 section = VEC_index (dwarf2_section_info_def,
3387 dwarf2_per_objfile->types, 0);
3388
74a0d9f6
JK
3389 create_signatured_type_table_from_index (objfile, section, types_list,
3390 types_list_elements);
8b70b953 3391 }
9291a0cd 3392
2ec9a5e0
TT
3393 create_addrmap_from_index (objfile, &local_map);
3394
8d749320 3395 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3396 *map = local_map;
9291a0cd
TT
3397
3398 dwarf2_per_objfile->index_table = map;
3399 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3400 dwarf2_per_objfile->quick_file_names_table =
3401 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3402
3403 return 1;
3404}
3405
3406/* A helper for the "quick" functions which sets the global
3407 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3408
9291a0cd
TT
3409static void
3410dw2_setup (struct objfile *objfile)
3411{
9a3c8263
SM
3412 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3413 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3414 gdb_assert (dwarf2_per_objfile);
3415}
3416
dee91e82 3417/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3418
dee91e82
DE
3419static void
3420dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3421 const gdb_byte *info_ptr,
dee91e82
DE
3422 struct die_info *comp_unit_die,
3423 int has_children,
3424 void *data)
9291a0cd 3425{
dee91e82
DE
3426 struct dwarf2_cu *cu = reader->cu;
3427 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3428 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3429 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3430 struct attribute *attr;
dee91e82 3431 int i;
7b9f3c50
DE
3432 void **slot;
3433 struct quick_file_names *qfn;
9291a0cd 3434
0186c6a7
DE
3435 gdb_assert (! this_cu->is_debug_types);
3436
07261596
TT
3437 /* Our callers never want to match partial units -- instead they
3438 will match the enclosing full CU. */
3439 if (comp_unit_die->tag == DW_TAG_partial_unit)
3440 {
3441 this_cu->v.quick->no_file_data = 1;
3442 return;
3443 }
3444
0186c6a7 3445 lh_cu = this_cu;
7b9f3c50 3446 slot = NULL;
dee91e82 3447
fff8551c 3448 line_header_up lh;
9c541725 3449 sect_offset line_offset {};
fff8551c 3450
dee91e82 3451 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3452 if (attr)
3453 {
7b9f3c50
DE
3454 struct quick_file_names find_entry;
3455
9c541725 3456 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3457
3458 /* We may have already read in this line header (TU line header sharing).
3459 If we have we're done. */
094b34ac 3460 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3461 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3462 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3463 &find_entry, INSERT);
3464 if (*slot != NULL)
3465 {
9a3c8263 3466 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3467 return;
7b9f3c50
DE
3468 }
3469
3019eac3 3470 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3471 }
3472 if (lh == NULL)
3473 {
094b34ac 3474 lh_cu->v.quick->no_file_data = 1;
dee91e82 3475 return;
9291a0cd
TT
3476 }
3477
8d749320 3478 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3479 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3480 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3481 gdb_assert (slot != NULL);
3482 *slot = qfn;
9291a0cd 3483
d721ba37 3484 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3485
fff8551c 3486 qfn->num_file_names = lh->file_names.size ();
8d749320 3487 qfn->file_names =
fff8551c
PA
3488 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3489 for (i = 0; i < lh->file_names.size (); ++i)
3490 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3491 qfn->real_names = NULL;
9291a0cd 3492
094b34ac 3493 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3494}
3495
3496/* A helper for the "quick" functions which attempts to read the line
3497 table for THIS_CU. */
3498
3499static struct quick_file_names *
e4a48d9d 3500dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3501{
0186c6a7
DE
3502 /* This should never be called for TUs. */
3503 gdb_assert (! this_cu->is_debug_types);
3504 /* Nor type unit groups. */
3505 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3506
dee91e82
DE
3507 if (this_cu->v.quick->file_names != NULL)
3508 return this_cu->v.quick->file_names;
3509 /* If we know there is no line data, no point in looking again. */
3510 if (this_cu->v.quick->no_file_data)
3511 return NULL;
3512
0186c6a7 3513 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3514
3515 if (this_cu->v.quick->no_file_data)
3516 return NULL;
3517 return this_cu->v.quick->file_names;
9291a0cd
TT
3518}
3519
3520/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3521 real path for a given file name from the line table. */
2fdf6df6 3522
9291a0cd 3523static const char *
7b9f3c50
DE
3524dw2_get_real_path (struct objfile *objfile,
3525 struct quick_file_names *qfn, int index)
9291a0cd 3526{
7b9f3c50
DE
3527 if (qfn->real_names == NULL)
3528 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3529 qfn->num_file_names, const char *);
9291a0cd 3530
7b9f3c50
DE
3531 if (qfn->real_names[index] == NULL)
3532 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3533
7b9f3c50 3534 return qfn->real_names[index];
9291a0cd
TT
3535}
3536
3537static struct symtab *
3538dw2_find_last_source_symtab (struct objfile *objfile)
3539{
43f3e411 3540 struct compunit_symtab *cust;
9291a0cd 3541 int index;
ae2de4f8 3542
9291a0cd
TT
3543 dw2_setup (objfile);
3544 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3545 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3546 if (cust == NULL)
3547 return NULL;
3548 return compunit_primary_filetab (cust);
9291a0cd
TT
3549}
3550
7b9f3c50
DE
3551/* Traversal function for dw2_forget_cached_source_info. */
3552
3553static int
3554dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3555{
7b9f3c50 3556 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3557
7b9f3c50 3558 if (file_data->real_names)
9291a0cd 3559 {
7b9f3c50 3560 int i;
9291a0cd 3561
7b9f3c50 3562 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3563 {
7b9f3c50
DE
3564 xfree ((void*) file_data->real_names[i]);
3565 file_data->real_names[i] = NULL;
9291a0cd
TT
3566 }
3567 }
7b9f3c50
DE
3568
3569 return 1;
3570}
3571
3572static void
3573dw2_forget_cached_source_info (struct objfile *objfile)
3574{
3575 dw2_setup (objfile);
3576
3577 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3578 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3579}
3580
f8eba3c6
TT
3581/* Helper function for dw2_map_symtabs_matching_filename that expands
3582 the symtabs and calls the iterator. */
3583
3584static int
3585dw2_map_expand_apply (struct objfile *objfile,
3586 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3587 const char *name, const char *real_path,
14bc53a8 3588 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3589{
43f3e411 3590 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3591
3592 /* Don't visit already-expanded CUs. */
43f3e411 3593 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3594 return 0;
3595
3596 /* This may expand more than one symtab, and we want to iterate over
3597 all of them. */
a0f42c21 3598 dw2_instantiate_symtab (per_cu);
f8eba3c6 3599
14bc53a8
PA
3600 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3601 last_made, callback);
f8eba3c6
TT
3602}
3603
3604/* Implementation of the map_symtabs_matching_filename method. */
3605
14bc53a8
PA
3606static bool
3607dw2_map_symtabs_matching_filename
3608 (struct objfile *objfile, const char *name, const char *real_path,
3609 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3610{
3611 int i;
c011a4f4 3612 const char *name_basename = lbasename (name);
9291a0cd
TT
3613
3614 dw2_setup (objfile);
ae2de4f8 3615
848e3e78
DE
3616 /* The rule is CUs specify all the files, including those used by
3617 any TU, so there's no need to scan TUs here. */
f4dc4d17 3618
848e3e78 3619 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3620 {
3621 int j;
8832e7e3 3622 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3623 struct quick_file_names *file_data;
9291a0cd 3624
3d7bb9d9 3625 /* We only need to look at symtabs not already expanded. */
43f3e411 3626 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3627 continue;
3628
e4a48d9d 3629 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3630 if (file_data == NULL)
9291a0cd
TT
3631 continue;
3632
7b9f3c50 3633 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3634 {
7b9f3c50 3635 const char *this_name = file_data->file_names[j];
da235a7c 3636 const char *this_real_name;
9291a0cd 3637
af529f8f 3638 if (compare_filenames_for_search (this_name, name))
9291a0cd 3639 {
f5b95b50 3640 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3641 callback))
3642 return true;
288e77a7 3643 continue;
4aac40c8 3644 }
9291a0cd 3645
c011a4f4
DE
3646 /* Before we invoke realpath, which can get expensive when many
3647 files are involved, do a quick comparison of the basenames. */
3648 if (! basenames_may_differ
3649 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3650 continue;
3651
da235a7c
JK
3652 this_real_name = dw2_get_real_path (objfile, file_data, j);
3653 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3654 {
da235a7c 3655 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3656 callback))
3657 return true;
288e77a7 3658 continue;
da235a7c 3659 }
9291a0cd 3660
da235a7c
JK
3661 if (real_path != NULL)
3662 {
af529f8f
JK
3663 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3664 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3665 if (this_real_name != NULL
af529f8f 3666 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3667 {
f5b95b50 3668 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3669 callback))
3670 return true;
288e77a7 3671 continue;
9291a0cd
TT
3672 }
3673 }
3674 }
3675 }
3676
14bc53a8 3677 return false;
9291a0cd
TT
3678}
3679
da51c347
DE
3680/* Struct used to manage iterating over all CUs looking for a symbol. */
3681
3682struct dw2_symtab_iterator
9291a0cd 3683{
da51c347
DE
3684 /* The internalized form of .gdb_index. */
3685 struct mapped_index *index;
3686 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3687 int want_specific_block;
3688 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3689 Unused if !WANT_SPECIFIC_BLOCK. */
3690 int block_index;
3691 /* The kind of symbol we're looking for. */
3692 domain_enum domain;
3693 /* The list of CUs from the index entry of the symbol,
3694 or NULL if not found. */
3695 offset_type *vec;
3696 /* The next element in VEC to look at. */
3697 int next;
3698 /* The number of elements in VEC, or zero if there is no match. */
3699 int length;
8943b874
DE
3700 /* Have we seen a global version of the symbol?
3701 If so we can ignore all further global instances.
3702 This is to work around gold/15646, inefficient gold-generated
3703 indices. */
3704 int global_seen;
da51c347 3705};
9291a0cd 3706
da51c347
DE
3707/* Initialize the index symtab iterator ITER.
3708 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3709 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3710
9291a0cd 3711static void
da51c347
DE
3712dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3713 struct mapped_index *index,
3714 int want_specific_block,
3715 int block_index,
3716 domain_enum domain,
3717 const char *name)
3718{
3719 iter->index = index;
3720 iter->want_specific_block = want_specific_block;
3721 iter->block_index = block_index;
3722 iter->domain = domain;
3723 iter->next = 0;
8943b874 3724 iter->global_seen = 0;
da51c347
DE
3725
3726 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3727 iter->length = MAYBE_SWAP (*iter->vec);
3728 else
3729 {
3730 iter->vec = NULL;
3731 iter->length = 0;
3732 }
3733}
3734
3735/* Return the next matching CU or NULL if there are no more. */
3736
3737static struct dwarf2_per_cu_data *
3738dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3739{
3740 for ( ; iter->next < iter->length; ++iter->next)
3741 {
3742 offset_type cu_index_and_attrs =
3743 MAYBE_SWAP (iter->vec[iter->next + 1]);
3744 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3745 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3746 int want_static = iter->block_index != GLOBAL_BLOCK;
3747 /* This value is only valid for index versions >= 7. */
3748 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3749 gdb_index_symbol_kind symbol_kind =
3750 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3751 /* Only check the symbol attributes if they're present.
3752 Indices prior to version 7 don't record them,
3753 and indices >= 7 may elide them for certain symbols
3754 (gold does this). */
3755 int attrs_valid =
3756 (iter->index->version >= 7
3757 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3758
3190f0c6
DE
3759 /* Don't crash on bad data. */
3760 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3761 + dwarf2_per_objfile->n_type_units))
3762 {
3763 complaint (&symfile_complaints,
3764 _(".gdb_index entry has bad CU index"
4262abfb
JK
3765 " [in module %s]"),
3766 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3767 continue;
3768 }
3769
8832e7e3 3770 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3771
da51c347 3772 /* Skip if already read in. */
43f3e411 3773 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3774 continue;
3775
8943b874
DE
3776 /* Check static vs global. */
3777 if (attrs_valid)
3778 {
3779 if (iter->want_specific_block
3780 && want_static != is_static)
3781 continue;
3782 /* Work around gold/15646. */
3783 if (!is_static && iter->global_seen)
3784 continue;
3785 if (!is_static)
3786 iter->global_seen = 1;
3787 }
da51c347
DE
3788
3789 /* Only check the symbol's kind if it has one. */
3790 if (attrs_valid)
3791 {
3792 switch (iter->domain)
3793 {
3794 case VAR_DOMAIN:
3795 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3796 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3797 /* Some types are also in VAR_DOMAIN. */
3798 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3799 continue;
3800 break;
3801 case STRUCT_DOMAIN:
3802 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3803 continue;
3804 break;
3805 case LABEL_DOMAIN:
3806 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3807 continue;
3808 break;
3809 default:
3810 break;
3811 }
3812 }
3813
3814 ++iter->next;
3815 return per_cu;
3816 }
3817
3818 return NULL;
3819}
3820
43f3e411 3821static struct compunit_symtab *
da51c347
DE
3822dw2_lookup_symbol (struct objfile *objfile, int block_index,
3823 const char *name, domain_enum domain)
9291a0cd 3824{
43f3e411 3825 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3826 struct mapped_index *index;
3827
9291a0cd
TT
3828 dw2_setup (objfile);
3829
156942c7
DE
3830 index = dwarf2_per_objfile->index_table;
3831
da51c347 3832 /* index is NULL if OBJF_READNOW. */
156942c7 3833 if (index)
9291a0cd 3834 {
da51c347
DE
3835 struct dw2_symtab_iterator iter;
3836 struct dwarf2_per_cu_data *per_cu;
3837
3838 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3839
da51c347 3840 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3841 {
b2e2f908 3842 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3843 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3844 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3845 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3846
b2e2f908
DE
3847 sym = block_find_symbol (block, name, domain,
3848 block_find_non_opaque_type_preferred,
3849 &with_opaque);
3850
da51c347
DE
3851 /* Some caution must be observed with overloaded functions
3852 and methods, since the index will not contain any overload
3853 information (but NAME might contain it). */
da51c347 3854
b2e2f908
DE
3855 if (sym != NULL
3856 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3857 return stab;
3858 if (with_opaque != NULL
3859 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3860 stab_best = stab;
da51c347
DE
3861
3862 /* Keep looking through other CUs. */
9291a0cd
TT
3863 }
3864 }
9291a0cd 3865
da51c347 3866 return stab_best;
9291a0cd
TT
3867}
3868
3869static void
3870dw2_print_stats (struct objfile *objfile)
3871{
e4a48d9d 3872 int i, total, count;
9291a0cd
TT
3873
3874 dw2_setup (objfile);
e4a48d9d 3875 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3876 count = 0;
e4a48d9d 3877 for (i = 0; i < total; ++i)
9291a0cd 3878 {
8832e7e3 3879 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3880
43f3e411 3881 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3882 ++count;
3883 }
e4a48d9d 3884 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3885 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3886}
3887
779bd270
DE
3888/* This dumps minimal information about the index.
3889 It is called via "mt print objfiles".
3890 One use is to verify .gdb_index has been loaded by the
3891 gdb.dwarf2/gdb-index.exp testcase. */
3892
9291a0cd
TT
3893static void
3894dw2_dump (struct objfile *objfile)
3895{
779bd270
DE
3896 dw2_setup (objfile);
3897 gdb_assert (dwarf2_per_objfile->using_index);
3898 printf_filtered (".gdb_index:");
3899 if (dwarf2_per_objfile->index_table != NULL)
3900 {
3901 printf_filtered (" version %d\n",
3902 dwarf2_per_objfile->index_table->version);
3903 }
3904 else
3905 printf_filtered (" faked for \"readnow\"\n");
3906 printf_filtered ("\n");
9291a0cd
TT
3907}
3908
3909static void
3189cb12
DE
3910dw2_relocate (struct objfile *objfile,
3911 const struct section_offsets *new_offsets,
3912 const struct section_offsets *delta)
9291a0cd
TT
3913{
3914 /* There's nothing to relocate here. */
3915}
3916
3917static void
3918dw2_expand_symtabs_for_function (struct objfile *objfile,
3919 const char *func_name)
3920{
da51c347
DE
3921 struct mapped_index *index;
3922
3923 dw2_setup (objfile);
3924
3925 index = dwarf2_per_objfile->index_table;
3926
3927 /* index is NULL if OBJF_READNOW. */
3928 if (index)
3929 {
3930 struct dw2_symtab_iterator iter;
3931 struct dwarf2_per_cu_data *per_cu;
3932
3933 /* Note: It doesn't matter what we pass for block_index here. */
3934 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3935 func_name);
3936
3937 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3938 dw2_instantiate_symtab (per_cu);
3939 }
9291a0cd
TT
3940}
3941
3942static void
3943dw2_expand_all_symtabs (struct objfile *objfile)
3944{
3945 int i;
3946
3947 dw2_setup (objfile);
1fd400ff
TT
3948
3949 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3950 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3951 {
8832e7e3 3952 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3953
a0f42c21 3954 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3955 }
3956}
3957
3958static void
652a8996
JK
3959dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3960 const char *fullname)
9291a0cd
TT
3961{
3962 int i;
3963
3964 dw2_setup (objfile);
d4637a04
DE
3965
3966 /* We don't need to consider type units here.
3967 This is only called for examining code, e.g. expand_line_sal.
3968 There can be an order of magnitude (or more) more type units
3969 than comp units, and we avoid them if we can. */
3970
3971 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3972 {
3973 int j;
8832e7e3 3974 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3975 struct quick_file_names *file_data;
9291a0cd 3976
3d7bb9d9 3977 /* We only need to look at symtabs not already expanded. */
43f3e411 3978 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3979 continue;
3980
e4a48d9d 3981 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3982 if (file_data == NULL)
9291a0cd
TT
3983 continue;
3984
7b9f3c50 3985 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3986 {
652a8996
JK
3987 const char *this_fullname = file_data->file_names[j];
3988
3989 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3990 {
a0f42c21 3991 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3992 break;
3993 }
3994 }
3995 }
3996}
3997
9291a0cd 3998static void
ade7ed9e 3999dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4000 const char * name, domain_enum domain,
ade7ed9e 4001 int global,
40658b94
PH
4002 int (*callback) (struct block *,
4003 struct symbol *, void *),
2edb89d3
JK
4004 void *data, symbol_compare_ftype *match,
4005 symbol_compare_ftype *ordered_compare)
9291a0cd 4006{
40658b94 4007 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4008 current language is Ada for a non-Ada objfile using GNU index. As Ada
4009 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4010}
4011
4012static void
f8eba3c6
TT
4013dw2_expand_symtabs_matching
4014 (struct objfile *objfile,
14bc53a8
PA
4015 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4016 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4017 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4018 enum search_domain kind)
9291a0cd
TT
4019{
4020 int i;
4021 offset_type iter;
4b5246aa 4022 struct mapped_index *index;
9291a0cd
TT
4023
4024 dw2_setup (objfile);
ae2de4f8
DE
4025
4026 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
4027 if (!dwarf2_per_objfile->index_table)
4028 return;
4b5246aa 4029 index = dwarf2_per_objfile->index_table;
9291a0cd 4030
7b08b9eb 4031 if (file_matcher != NULL)
24c79950 4032 {
fc4007c9
TT
4033 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4034 htab_eq_pointer,
4035 NULL, xcalloc, xfree));
4036 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4037 htab_eq_pointer,
4038 NULL, xcalloc, xfree));
24c79950 4039
848e3e78
DE
4040 /* The rule is CUs specify all the files, including those used by
4041 any TU, so there's no need to scan TUs here. */
4042
4043 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
4044 {
4045 int j;
8832e7e3 4046 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
4047 struct quick_file_names *file_data;
4048 void **slot;
7b08b9eb 4049
61d96d7e
DE
4050 QUIT;
4051
24c79950 4052 per_cu->v.quick->mark = 0;
3d7bb9d9 4053
24c79950 4054 /* We only need to look at symtabs not already expanded. */
43f3e411 4055 if (per_cu->v.quick->compunit_symtab)
24c79950 4056 continue;
7b08b9eb 4057
e4a48d9d 4058 file_data = dw2_get_file_names (per_cu);
24c79950
TT
4059 if (file_data == NULL)
4060 continue;
7b08b9eb 4061
fc4007c9 4062 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 4063 continue;
fc4007c9 4064 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
4065 {
4066 per_cu->v.quick->mark = 1;
4067 continue;
4068 }
4069
4070 for (j = 0; j < file_data->num_file_names; ++j)
4071 {
da235a7c
JK
4072 const char *this_real_name;
4073
14bc53a8 4074 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
4075 {
4076 per_cu->v.quick->mark = 1;
4077 break;
4078 }
da235a7c
JK
4079
4080 /* Before we invoke realpath, which can get expensive when many
4081 files are involved, do a quick comparison of the basenames. */
4082 if (!basenames_may_differ
4083 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4084 true))
da235a7c
JK
4085 continue;
4086
4087 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4088 if (file_matcher (this_real_name, false))
da235a7c
JK
4089 {
4090 per_cu->v.quick->mark = 1;
4091 break;
4092 }
24c79950
TT
4093 }
4094
4095 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4096 ? visited_found.get ()
4097 : visited_not_found.get (),
24c79950
TT
4098 file_data, INSERT);
4099 *slot = file_data;
4100 }
24c79950 4101 }
9291a0cd 4102
3876f04e 4103 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4104 {
4105 offset_type idx = 2 * iter;
4106 const char *name;
4107 offset_type *vec, vec_len, vec_idx;
8943b874 4108 int global_seen = 0;
9291a0cd 4109
61d96d7e
DE
4110 QUIT;
4111
3876f04e 4112 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4113 continue;
4114
3876f04e 4115 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4116
14bc53a8 4117 if (!symbol_matcher (name))
9291a0cd
TT
4118 continue;
4119
4120 /* The name was matched, now expand corresponding CUs that were
4121 marked. */
4b5246aa 4122 vec = (offset_type *) (index->constant_pool
3876f04e 4123 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4124 vec_len = MAYBE_SWAP (vec[0]);
4125 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4126 {
e254ef6a 4127 struct dwarf2_per_cu_data *per_cu;
156942c7 4128 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4129 /* This value is only valid for index versions >= 7. */
4130 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4131 gdb_index_symbol_kind symbol_kind =
4132 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4133 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4134 /* Only check the symbol attributes if they're present.
4135 Indices prior to version 7 don't record them,
4136 and indices >= 7 may elide them for certain symbols
4137 (gold does this). */
4138 int attrs_valid =
4139 (index->version >= 7
4140 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4141
8943b874
DE
4142 /* Work around gold/15646. */
4143 if (attrs_valid)
4144 {
4145 if (!is_static && global_seen)
4146 continue;
4147 if (!is_static)
4148 global_seen = 1;
4149 }
4150
3190f0c6
DE
4151 /* Only check the symbol's kind if it has one. */
4152 if (attrs_valid)
156942c7
DE
4153 {
4154 switch (kind)
4155 {
4156 case VARIABLES_DOMAIN:
4157 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4158 continue;
4159 break;
4160 case FUNCTIONS_DOMAIN:
4161 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4162 continue;
4163 break;
4164 case TYPES_DOMAIN:
4165 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4166 continue;
4167 break;
4168 default:
4169 break;
4170 }
4171 }
4172
3190f0c6
DE
4173 /* Don't crash on bad data. */
4174 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4175 + dwarf2_per_objfile->n_type_units))
4176 {
4177 complaint (&symfile_complaints,
4178 _(".gdb_index entry has bad CU index"
4262abfb 4179 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4180 continue;
4181 }
4182
8832e7e3 4183 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4184 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4185 {
4186 int symtab_was_null =
4187 (per_cu->v.quick->compunit_symtab == NULL);
4188
4189 dw2_instantiate_symtab (per_cu);
4190
4191 if (expansion_notify != NULL
4192 && symtab_was_null
4193 && per_cu->v.quick->compunit_symtab != NULL)
4194 {
14bc53a8 4195 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4196 }
4197 }
9291a0cd
TT
4198 }
4199 }
4200}
4201
43f3e411 4202/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4203 symtab. */
4204
43f3e411
DE
4205static struct compunit_symtab *
4206recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4207 CORE_ADDR pc)
9703b513
TT
4208{
4209 int i;
4210
43f3e411
DE
4211 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4212 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4213 return cust;
9703b513 4214
43f3e411 4215 if (cust->includes == NULL)
a3ec0bb1
DE
4216 return NULL;
4217
43f3e411 4218 for (i = 0; cust->includes[i]; ++i)
9703b513 4219 {
43f3e411 4220 struct compunit_symtab *s = cust->includes[i];
9703b513 4221
43f3e411 4222 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4223 if (s != NULL)
4224 return s;
4225 }
4226
4227 return NULL;
4228}
4229
43f3e411
DE
4230static struct compunit_symtab *
4231dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4232 struct bound_minimal_symbol msymbol,
4233 CORE_ADDR pc,
4234 struct obj_section *section,
4235 int warn_if_readin)
9291a0cd
TT
4236{
4237 struct dwarf2_per_cu_data *data;
43f3e411 4238 struct compunit_symtab *result;
9291a0cd
TT
4239
4240 dw2_setup (objfile);
4241
4242 if (!objfile->psymtabs_addrmap)
4243 return NULL;
4244
9a3c8263
SM
4245 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4246 pc);
9291a0cd
TT
4247 if (!data)
4248 return NULL;
4249
43f3e411 4250 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4251 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4252 paddress (get_objfile_arch (objfile), pc));
4253
43f3e411
DE
4254 result
4255 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4256 pc);
9703b513
TT
4257 gdb_assert (result != NULL);
4258 return result;
9291a0cd
TT
4259}
4260
9291a0cd 4261static void
44b13c5a 4262dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4263 void *data, int need_fullname)
9291a0cd
TT
4264{
4265 int i;
fc4007c9
TT
4266 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4267 NULL, xcalloc, xfree));
9291a0cd
TT
4268
4269 dw2_setup (objfile);
ae2de4f8 4270
848e3e78
DE
4271 /* The rule is CUs specify all the files, including those used by
4272 any TU, so there's no need to scan TUs here.
4273 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 4274
848e3e78 4275 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4276 {
8832e7e3 4277 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4278
43f3e411 4279 if (per_cu->v.quick->compunit_symtab)
24c79950 4280 {
fc4007c9
TT
4281 void **slot = htab_find_slot (visited.get (),
4282 per_cu->v.quick->file_names,
24c79950
TT
4283 INSERT);
4284
4285 *slot = per_cu->v.quick->file_names;
4286 }
4287 }
4288
848e3e78 4289 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4290 {
4291 int j;
8832e7e3 4292 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4293 struct quick_file_names *file_data;
24c79950 4294 void **slot;
9291a0cd 4295
3d7bb9d9 4296 /* We only need to look at symtabs not already expanded. */
43f3e411 4297 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4298 continue;
4299
e4a48d9d 4300 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4301 if (file_data == NULL)
9291a0cd
TT
4302 continue;
4303
fc4007c9 4304 slot = htab_find_slot (visited.get (), file_data, INSERT);
24c79950
TT
4305 if (*slot)
4306 {
4307 /* Already visited. */
4308 continue;
4309 }
4310 *slot = file_data;
4311
7b9f3c50 4312 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4313 {
74e2f255
DE
4314 const char *this_real_name;
4315
4316 if (need_fullname)
4317 this_real_name = dw2_get_real_path (objfile, file_data, j);
4318 else
4319 this_real_name = NULL;
7b9f3c50 4320 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4321 }
4322 }
4323}
4324
4325static int
4326dw2_has_symbols (struct objfile *objfile)
4327{
4328 return 1;
4329}
4330
4331const struct quick_symbol_functions dwarf2_gdb_index_functions =
4332{
4333 dw2_has_symbols,
4334 dw2_find_last_source_symtab,
4335 dw2_forget_cached_source_info,
f8eba3c6 4336 dw2_map_symtabs_matching_filename,
9291a0cd 4337 dw2_lookup_symbol,
9291a0cd
TT
4338 dw2_print_stats,
4339 dw2_dump,
4340 dw2_relocate,
4341 dw2_expand_symtabs_for_function,
4342 dw2_expand_all_symtabs,
652a8996 4343 dw2_expand_symtabs_with_fullname,
40658b94 4344 dw2_map_matching_symbols,
9291a0cd 4345 dw2_expand_symtabs_matching,
43f3e411 4346 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4347 dw2_map_symbol_filenames
4348};
4349
4350/* Initialize for reading DWARF for this objfile. Return 0 if this
4351 file will use psymtabs, or 1 if using the GNU index. */
4352
4353int
4354dwarf2_initialize_objfile (struct objfile *objfile)
4355{
4356 /* If we're about to read full symbols, don't bother with the
4357 indices. In this case we also don't care if some other debug
4358 format is making psymtabs, because they are all about to be
4359 expanded anyway. */
4360 if ((objfile->flags & OBJF_READNOW))
4361 {
4362 int i;
4363
4364 dwarf2_per_objfile->using_index = 1;
4365 create_all_comp_units (objfile);
0e50663e 4366 create_all_type_units (objfile);
7b9f3c50
DE
4367 dwarf2_per_objfile->quick_file_names_table =
4368 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4369
1fd400ff 4370 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4371 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4372 {
8832e7e3 4373 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4374
e254ef6a
DE
4375 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4376 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4377 }
4378
4379 /* Return 1 so that gdb sees the "quick" functions. However,
4380 these functions will be no-ops because we will have expanded
4381 all symtabs. */
4382 return 1;
4383 }
4384
4385 if (dwarf2_read_index (objfile))
4386 return 1;
4387
9291a0cd
TT
4388 return 0;
4389}
4390
4391\f
4392
dce234bc
PP
4393/* Build a partial symbol table. */
4394
4395void
f29dff0a 4396dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4397{
c9bf0622 4398
f29dff0a 4399 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4400 {
4401 init_psymbol_list (objfile, 1024);
4402 }
4403
492d29ea 4404 TRY
c9bf0622
TT
4405 {
4406 /* This isn't really ideal: all the data we allocate on the
4407 objfile's obstack is still uselessly kept around. However,
4408 freeing it seems unsafe. */
906768f9 4409 psymtab_discarder psymtabs (objfile);
c9bf0622 4410 dwarf2_build_psymtabs_hard (objfile);
906768f9 4411 psymtabs.keep ();
c9bf0622 4412 }
492d29ea
PA
4413 CATCH (except, RETURN_MASK_ERROR)
4414 {
4415 exception_print (gdb_stderr, except);
4416 }
4417 END_CATCH
c906108c 4418}
c906108c 4419
1ce1cefd
DE
4420/* Return the total length of the CU described by HEADER. */
4421
4422static unsigned int
4423get_cu_length (const struct comp_unit_head *header)
4424{
4425 return header->initial_length_size + header->length;
4426}
4427
9c541725 4428/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 4429
9c541725
PA
4430static inline bool
4431offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 4432{
9c541725
PA
4433 sect_offset bottom = cu_header->sect_off;
4434 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 4435
9c541725 4436 return sect_off >= bottom && sect_off < top;
45452591
DE
4437}
4438
3b80fe9b
DE
4439/* Find the base address of the compilation unit for range lists and
4440 location lists. It will normally be specified by DW_AT_low_pc.
4441 In DWARF-3 draft 4, the base address could be overridden by
4442 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4443 compilation units with discontinuous ranges. */
4444
4445static void
4446dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4447{
4448 struct attribute *attr;
4449
4450 cu->base_known = 0;
4451 cu->base_address = 0;
4452
4453 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4454 if (attr)
4455 {
31aa7e4e 4456 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4457 cu->base_known = 1;
4458 }
4459 else
4460 {
4461 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4462 if (attr)
4463 {
31aa7e4e 4464 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4465 cu->base_known = 1;
4466 }
4467 }
4468}
4469
93311388 4470/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4471 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4472 NOTE: This leaves members offset, first_die_offset to be filled in
4473 by the caller. */
107d2387 4474
d521ce57 4475static const gdb_byte *
107d2387 4476read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4477 const gdb_byte *info_ptr,
4478 struct dwarf2_section_info *section,
4479 rcuh_kind section_kind)
107d2387
AC
4480{
4481 int signed_addr;
891d2f0b 4482 unsigned int bytes_read;
43988095
JK
4483 const char *filename = get_section_file_name (section);
4484 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4485
4486 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4487 cu_header->initial_length_size = bytes_read;
4488 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4489 info_ptr += bytes_read;
107d2387
AC
4490 cu_header->version = read_2_bytes (abfd, info_ptr);
4491 info_ptr += 2;
43988095
JK
4492 if (cu_header->version < 5)
4493 switch (section_kind)
4494 {
4495 case rcuh_kind::COMPILE:
4496 cu_header->unit_type = DW_UT_compile;
4497 break;
4498 case rcuh_kind::TYPE:
4499 cu_header->unit_type = DW_UT_type;
4500 break;
4501 default:
4502 internal_error (__FILE__, __LINE__,
4503 _("read_comp_unit_head: invalid section_kind"));
4504 }
4505 else
4506 {
4507 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4508 (read_1_byte (abfd, info_ptr));
4509 info_ptr += 1;
4510 switch (cu_header->unit_type)
4511 {
4512 case DW_UT_compile:
4513 if (section_kind != rcuh_kind::COMPILE)
4514 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4515 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4516 filename);
4517 break;
4518 case DW_UT_type:
4519 section_kind = rcuh_kind::TYPE;
4520 break;
4521 default:
4522 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4523 "(is %d, should be %d or %d) [in module %s]"),
4524 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4525 }
4526
4527 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4528 info_ptr += 1;
4529 }
9c541725
PA
4530 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
4531 cu_header,
4532 &bytes_read);
613e1657 4533 info_ptr += bytes_read;
43988095
JK
4534 if (cu_header->version < 5)
4535 {
4536 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4537 info_ptr += 1;
4538 }
107d2387
AC
4539 signed_addr = bfd_get_sign_extend_vma (abfd);
4540 if (signed_addr < 0)
8e65ff28 4541 internal_error (__FILE__, __LINE__,
e2e0b3e5 4542 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4543 cu_header->signed_addr_p = signed_addr;
c764a876 4544
43988095
JK
4545 if (section_kind == rcuh_kind::TYPE)
4546 {
4547 LONGEST type_offset;
4548
4549 cu_header->signature = read_8_bytes (abfd, info_ptr);
4550 info_ptr += 8;
4551
4552 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4553 info_ptr += bytes_read;
9c541725
PA
4554 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
4555 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
4556 error (_("Dwarf Error: Too big type_offset in compilation unit "
4557 "header (is %s) [in module %s]"), plongest (type_offset),
4558 filename);
4559 }
4560
107d2387
AC
4561 return info_ptr;
4562}
4563
36586728
TT
4564/* Helper function that returns the proper abbrev section for
4565 THIS_CU. */
4566
4567static struct dwarf2_section_info *
4568get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4569{
4570 struct dwarf2_section_info *abbrev;
4571
4572 if (this_cu->is_dwz)
4573 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4574 else
4575 abbrev = &dwarf2_per_objfile->abbrev;
4576
4577 return abbrev;
4578}
4579
9ff913ba
DE
4580/* Subroutine of read_and_check_comp_unit_head and
4581 read_and_check_type_unit_head to simplify them.
4582 Perform various error checking on the header. */
4583
4584static void
4585error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4586 struct dwarf2_section_info *section,
4587 struct dwarf2_section_info *abbrev_section)
9ff913ba 4588{
a32a8923 4589 const char *filename = get_section_file_name (section);
9ff913ba 4590
43988095 4591 if (header->version < 2 || header->version > 5)
9ff913ba 4592 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4593 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4594 filename);
4595
9c541725 4596 if (to_underlying (header->abbrev_sect_off)
36586728 4597 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9c541725
PA
4598 error (_("Dwarf Error: bad offset (0x%x) in compilation unit header "
4599 "(offset 0x%x + 6) [in module %s]"),
4600 to_underlying (header->abbrev_sect_off),
4601 to_underlying (header->sect_off),
9ff913ba
DE
4602 filename);
4603
9c541725 4604 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 4605 avoid potential 32-bit overflow. */
9c541725 4606 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 4607 > section->size)
9c541725
PA
4608 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
4609 "(offset 0x%x + 0) [in module %s]"),
4610 header->length, to_underlying (header->sect_off),
9ff913ba
DE
4611 filename);
4612}
4613
4614/* Read in a CU/TU header and perform some basic error checking.
4615 The contents of the header are stored in HEADER.
4616 The result is a pointer to the start of the first DIE. */
adabb602 4617
d521ce57 4618static const gdb_byte *
9ff913ba
DE
4619read_and_check_comp_unit_head (struct comp_unit_head *header,
4620 struct dwarf2_section_info *section,
4bdcc0c1 4621 struct dwarf2_section_info *abbrev_section,
d521ce57 4622 const gdb_byte *info_ptr,
43988095 4623 rcuh_kind section_kind)
72bf9492 4624{
d521ce57 4625 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4626 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4627
9c541725 4628 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 4629
43988095 4630 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4631
9c541725 4632 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 4633
4bdcc0c1 4634 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4635
4636 return info_ptr;
348e048f
DE
4637}
4638
f4dc4d17
DE
4639/* Fetch the abbreviation table offset from a comp or type unit header. */
4640
4641static sect_offset
4642read_abbrev_offset (struct dwarf2_section_info *section,
9c541725 4643 sect_offset sect_off)
f4dc4d17 4644{
a32a8923 4645 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4646 const gdb_byte *info_ptr;
ac298888 4647 unsigned int initial_length_size, offset_size;
43988095 4648 uint16_t version;
f4dc4d17
DE
4649
4650 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 4651 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 4652 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4653 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4654 info_ptr += initial_length_size;
4655
4656 version = read_2_bytes (abfd, info_ptr);
4657 info_ptr += 2;
4658 if (version >= 5)
4659 {
4660 /* Skip unit type and address size. */
4661 info_ptr += 2;
4662 }
4663
9c541725 4664 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
4665}
4666
aaa75496
JB
4667/* Allocate a new partial symtab for file named NAME and mark this new
4668 partial symtab as being an include of PST. */
4669
4670static void
d521ce57 4671dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4672 struct objfile *objfile)
4673{
4674 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4675
fbd9ab74
JK
4676 if (!IS_ABSOLUTE_PATH (subpst->filename))
4677 {
4678 /* It shares objfile->objfile_obstack. */
4679 subpst->dirname = pst->dirname;
4680 }
4681
aaa75496
JB
4682 subpst->textlow = 0;
4683 subpst->texthigh = 0;
4684
8d749320
SM
4685 subpst->dependencies
4686 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4687 subpst->dependencies[0] = pst;
4688 subpst->number_of_dependencies = 1;
4689
4690 subpst->globals_offset = 0;
4691 subpst->n_global_syms = 0;
4692 subpst->statics_offset = 0;
4693 subpst->n_static_syms = 0;
43f3e411 4694 subpst->compunit_symtab = NULL;
aaa75496
JB
4695 subpst->read_symtab = pst->read_symtab;
4696 subpst->readin = 0;
4697
4698 /* No private part is necessary for include psymtabs. This property
4699 can be used to differentiate between such include psymtabs and
10b3939b 4700 the regular ones. */
58a9656e 4701 subpst->read_symtab_private = NULL;
aaa75496
JB
4702}
4703
4704/* Read the Line Number Program data and extract the list of files
4705 included by the source file represented by PST. Build an include
d85a05f0 4706 partial symtab for each of these included files. */
aaa75496
JB
4707
4708static void
4709dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4710 struct die_info *die,
4711 struct partial_symtab *pst)
aaa75496 4712{
fff8551c 4713 line_header_up lh;
d85a05f0 4714 struct attribute *attr;
aaa75496 4715
d85a05f0
DJ
4716 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4717 if (attr)
9c541725 4718 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
4719 if (lh == NULL)
4720 return; /* No linetable, so no includes. */
4721
c6da4cef 4722 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 4723 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4724}
4725
348e048f 4726static hashval_t
52dc124a 4727hash_signatured_type (const void *item)
348e048f 4728{
9a3c8263
SM
4729 const struct signatured_type *sig_type
4730 = (const struct signatured_type *) item;
9a619af0 4731
348e048f 4732 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4733 return sig_type->signature;
348e048f
DE
4734}
4735
4736static int
52dc124a 4737eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4738{
9a3c8263
SM
4739 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4740 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4741
348e048f
DE
4742 return lhs->signature == rhs->signature;
4743}
4744
1fd400ff
TT
4745/* Allocate a hash table for signatured types. */
4746
4747static htab_t
673bfd45 4748allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4749{
4750 return htab_create_alloc_ex (41,
52dc124a
DE
4751 hash_signatured_type,
4752 eq_signatured_type,
1fd400ff
TT
4753 NULL,
4754 &objfile->objfile_obstack,
4755 hashtab_obstack_allocate,
4756 dummy_obstack_deallocate);
4757}
4758
d467dd73 4759/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4760
4761static int
d467dd73 4762add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4763{
9a3c8263
SM
4764 struct signatured_type *sigt = (struct signatured_type *) *slot;
4765 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4766
b4dd5633 4767 **datap = sigt;
1fd400ff
TT
4768 ++*datap;
4769
4770 return 1;
4771}
4772
78d4d2c5 4773/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4774 and fill them into TYPES_HTAB. It will process only type units,
4775 therefore DW_UT_type. */
c88ee1f0 4776
78d4d2c5
JK
4777static void
4778create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4779 dwarf2_section_info *section, htab_t &types_htab,
4780 rcuh_kind section_kind)
348e048f 4781{
3019eac3 4782 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4783 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4784 bfd *abfd;
4785 const gdb_byte *info_ptr, *end_ptr;
348e048f 4786
4bdcc0c1
DE
4787 abbrev_section = (dwo_file != NULL
4788 ? &dwo_file->sections.abbrev
4789 : &dwarf2_per_objfile->abbrev);
4790
b4f54984 4791 if (dwarf_read_debug)
43988095
JK
4792 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4793 get_section_name (section),
a32a8923 4794 get_section_file_name (abbrev_section));
09406207 4795
78d4d2c5
JK
4796 dwarf2_read_section (objfile, section);
4797 info_ptr = section->buffer;
348e048f 4798
78d4d2c5
JK
4799 if (info_ptr == NULL)
4800 return;
348e048f 4801
78d4d2c5
JK
4802 /* We can't set abfd until now because the section may be empty or
4803 not present, in which case the bfd is unknown. */
4804 abfd = get_section_bfd_owner (section);
348e048f 4805
78d4d2c5
JK
4806 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4807 because we don't need to read any dies: the signature is in the
4808 header. */
3019eac3 4809
78d4d2c5
JK
4810 end_ptr = info_ptr + section->size;
4811 while (info_ptr < end_ptr)
4812 {
78d4d2c5
JK
4813 struct signatured_type *sig_type;
4814 struct dwo_unit *dwo_tu;
4815 void **slot;
4816 const gdb_byte *ptr = info_ptr;
4817 struct comp_unit_head header;
4818 unsigned int length;
8b70b953 4819
9c541725 4820 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 4821
a49dd8dd
JK
4822 /* Initialize it due to a false compiler warning. */
4823 header.signature = -1;
9c541725 4824 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 4825
78d4d2c5
JK
4826 /* We need to read the type's signature in order to build the hash
4827 table, but we don't need anything else just yet. */
348e048f 4828
43988095
JK
4829 ptr = read_and_check_comp_unit_head (&header, section,
4830 abbrev_section, ptr, section_kind);
348e048f 4831
78d4d2c5 4832 length = get_cu_length (&header);
6caca83c 4833
78d4d2c5
JK
4834 /* Skip dummy type units. */
4835 if (ptr >= info_ptr + length
43988095
JK
4836 || peek_abbrev_code (abfd, ptr) == 0
4837 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4838 {
4839 info_ptr += length;
4840 continue;
4841 }
dee91e82 4842
78d4d2c5
JK
4843 if (types_htab == NULL)
4844 {
4845 if (dwo_file)
4846 types_htab = allocate_dwo_unit_table (objfile);
4847 else
4848 types_htab = allocate_signatured_type_table (objfile);
4849 }
8b70b953 4850
78d4d2c5
JK
4851 if (dwo_file)
4852 {
4853 sig_type = NULL;
4854 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4855 struct dwo_unit);
4856 dwo_tu->dwo_file = dwo_file;
43988095 4857 dwo_tu->signature = header.signature;
9c541725 4858 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 4859 dwo_tu->section = section;
9c541725 4860 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
4861 dwo_tu->length = length;
4862 }
4863 else
4864 {
4865 /* N.B.: type_offset is not usable if this type uses a DWO file.
4866 The real type_offset is in the DWO file. */
4867 dwo_tu = NULL;
4868 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4869 struct signatured_type);
43988095 4870 sig_type->signature = header.signature;
9c541725 4871 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
4872 sig_type->per_cu.objfile = objfile;
4873 sig_type->per_cu.is_debug_types = 1;
4874 sig_type->per_cu.section = section;
9c541725 4875 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
4876 sig_type->per_cu.length = length;
4877 }
4878
4879 slot = htab_find_slot (types_htab,
4880 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4881 INSERT);
4882 gdb_assert (slot != NULL);
4883 if (*slot != NULL)
4884 {
9c541725 4885 sect_offset dup_sect_off;
0349ea22 4886
3019eac3
DE
4887 if (dwo_file)
4888 {
78d4d2c5
JK
4889 const struct dwo_unit *dup_tu
4890 = (const struct dwo_unit *) *slot;
4891
9c541725 4892 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
4893 }
4894 else
4895 {
78d4d2c5
JK
4896 const struct signatured_type *dup_tu
4897 = (const struct signatured_type *) *slot;
4898
9c541725 4899 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 4900 }
8b70b953 4901
78d4d2c5
JK
4902 complaint (&symfile_complaints,
4903 _("debug type entry at offset 0x%x is duplicate to"
4904 " the entry at offset 0x%x, signature %s"),
9c541725 4905 to_underlying (sect_off), to_underlying (dup_sect_off),
43988095 4906 hex_string (header.signature));
78d4d2c5
JK
4907 }
4908 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4909
78d4d2c5
JK
4910 if (dwarf_read_debug > 1)
4911 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
9c541725 4912 to_underlying (sect_off),
43988095 4913 hex_string (header.signature));
3019eac3 4914
78d4d2c5
JK
4915 info_ptr += length;
4916 }
4917}
3019eac3 4918
78d4d2c5
JK
4919/* Create the hash table of all entries in the .debug_types
4920 (or .debug_types.dwo) section(s).
4921 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4922 otherwise it is NULL.
b3c8eb43 4923
78d4d2c5 4924 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4925
78d4d2c5 4926 Note: This function processes DWO files only, not DWP files. */
348e048f 4927
78d4d2c5
JK
4928static void
4929create_debug_types_hash_table (struct dwo_file *dwo_file,
4930 VEC (dwarf2_section_info_def) *types,
4931 htab_t &types_htab)
4932{
4933 int ix;
4934 struct dwarf2_section_info *section;
4935
4936 if (VEC_empty (dwarf2_section_info_def, types))
4937 return;
348e048f 4938
78d4d2c5
JK
4939 for (ix = 0;
4940 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4941 ++ix)
43988095
JK
4942 create_debug_type_hash_table (dwo_file, section, types_htab,
4943 rcuh_kind::TYPE);
3019eac3
DE
4944}
4945
4946/* Create the hash table of all entries in the .debug_types section,
4947 and initialize all_type_units.
4948 The result is zero if there is an error (e.g. missing .debug_types section),
4949 otherwise non-zero. */
4950
4951static int
4952create_all_type_units (struct objfile *objfile)
4953{
78d4d2c5 4954 htab_t types_htab = NULL;
b4dd5633 4955 struct signatured_type **iter;
3019eac3 4956
43988095
JK
4957 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4958 rcuh_kind::COMPILE);
78d4d2c5 4959 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
4960 if (types_htab == NULL)
4961 {
4962 dwarf2_per_objfile->signatured_types = NULL;
4963 return 0;
4964 }
4965
348e048f
DE
4966 dwarf2_per_objfile->signatured_types = types_htab;
4967
6aa5f3a6
DE
4968 dwarf2_per_objfile->n_type_units
4969 = dwarf2_per_objfile->n_allocated_type_units
4970 = htab_elements (types_htab);
8d749320
SM
4971 dwarf2_per_objfile->all_type_units =
4972 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
4973 iter = &dwarf2_per_objfile->all_type_units[0];
4974 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4975 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4976 == dwarf2_per_objfile->n_type_units);
1fd400ff 4977
348e048f
DE
4978 return 1;
4979}
4980
6aa5f3a6
DE
4981/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4982 If SLOT is non-NULL, it is the entry to use in the hash table.
4983 Otherwise we find one. */
4984
4985static struct signatured_type *
4986add_type_unit (ULONGEST sig, void **slot)
4987{
4988 struct objfile *objfile = dwarf2_per_objfile->objfile;
4989 int n_type_units = dwarf2_per_objfile->n_type_units;
4990 struct signatured_type *sig_type;
4991
4992 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4993 ++n_type_units;
4994 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4995 {
4996 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4997 dwarf2_per_objfile->n_allocated_type_units = 1;
4998 dwarf2_per_objfile->n_allocated_type_units *= 2;
4999 dwarf2_per_objfile->all_type_units
224c3ddb
SM
5000 = XRESIZEVEC (struct signatured_type *,
5001 dwarf2_per_objfile->all_type_units,
5002 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
5003 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5004 }
5005 dwarf2_per_objfile->n_type_units = n_type_units;
5006
5007 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5008 struct signatured_type);
5009 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5010 sig_type->signature = sig;
5011 sig_type->per_cu.is_debug_types = 1;
5012 if (dwarf2_per_objfile->using_index)
5013 {
5014 sig_type->per_cu.v.quick =
5015 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5016 struct dwarf2_per_cu_quick_data);
5017 }
5018
5019 if (slot == NULL)
5020 {
5021 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5022 sig_type, INSERT);
5023 }
5024 gdb_assert (*slot == NULL);
5025 *slot = sig_type;
5026 /* The rest of sig_type must be filled in by the caller. */
5027 return sig_type;
5028}
5029
a2ce51a0
DE
5030/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5031 Fill in SIG_ENTRY with DWO_ENTRY. */
5032
5033static void
5034fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5035 struct signatured_type *sig_entry,
5036 struct dwo_unit *dwo_entry)
5037{
7ee85ab1 5038 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
5039 gdb_assert (! sig_entry->per_cu.queued);
5040 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
5041 if (dwarf2_per_objfile->using_index)
5042 {
5043 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 5044 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
5045 }
5046 else
5047 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 5048 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5049 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5050 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5051 gdb_assert (sig_entry->dwo_unit == NULL);
5052
5053 sig_entry->per_cu.section = dwo_entry->section;
9c541725 5054 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
5055 sig_entry->per_cu.length = dwo_entry->length;
5056 sig_entry->per_cu.reading_dwo_directly = 1;
5057 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
5058 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5059 sig_entry->dwo_unit = dwo_entry;
5060}
5061
5062/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5063 If we haven't read the TU yet, create the signatured_type data structure
5064 for a TU to be read in directly from a DWO file, bypassing the stub.
5065 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5066 using .gdb_index, then when reading a CU we want to stay in the DWO file
5067 containing that CU. Otherwise we could end up reading several other DWO
5068 files (due to comdat folding) to process the transitive closure of all the
5069 mentioned TUs, and that can be slow. The current DWO file will have every
5070 type signature that it needs.
a2ce51a0
DE
5071 We only do this for .gdb_index because in the psymtab case we already have
5072 to read all the DWOs to build the type unit groups. */
5073
5074static struct signatured_type *
5075lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5076{
5077 struct objfile *objfile = dwarf2_per_objfile->objfile;
5078 struct dwo_file *dwo_file;
5079 struct dwo_unit find_dwo_entry, *dwo_entry;
5080 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5081 void **slot;
a2ce51a0
DE
5082
5083 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5084
6aa5f3a6
DE
5085 /* If TU skeletons have been removed then we may not have read in any
5086 TUs yet. */
5087 if (dwarf2_per_objfile->signatured_types == NULL)
5088 {
5089 dwarf2_per_objfile->signatured_types
5090 = allocate_signatured_type_table (objfile);
5091 }
a2ce51a0
DE
5092
5093 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5094 Use the global signatured_types array to do our own comdat-folding
5095 of types. If this is the first time we're reading this TU, and
5096 the TU has an entry in .gdb_index, replace the recorded data from
5097 .gdb_index with this TU. */
a2ce51a0 5098
a2ce51a0 5099 find_sig_entry.signature = sig;
6aa5f3a6
DE
5100 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5101 &find_sig_entry, INSERT);
9a3c8263 5102 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5103
5104 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5105 read. Don't reassign the global entry to point to this DWO if that's
5106 the case. Also note that if the TU is already being read, it may not
5107 have come from a DWO, the program may be a mix of Fission-compiled
5108 code and non-Fission-compiled code. */
5109
5110 /* Have we already tried to read this TU?
5111 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5112 needn't exist in the global table yet). */
5113 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5114 return sig_entry;
5115
6aa5f3a6
DE
5116 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5117 dwo_unit of the TU itself. */
5118 dwo_file = cu->dwo_unit->dwo_file;
5119
a2ce51a0
DE
5120 /* Ok, this is the first time we're reading this TU. */
5121 if (dwo_file->tus == NULL)
5122 return NULL;
5123 find_dwo_entry.signature = sig;
9a3c8263 5124 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5125 if (dwo_entry == NULL)
5126 return NULL;
5127
6aa5f3a6
DE
5128 /* If the global table doesn't have an entry for this TU, add one. */
5129 if (sig_entry == NULL)
5130 sig_entry = add_type_unit (sig, slot);
5131
a2ce51a0 5132 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5133 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5134 return sig_entry;
5135}
5136
a2ce51a0
DE
5137/* Subroutine of lookup_signatured_type.
5138 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5139 then try the DWP file. If the TU stub (skeleton) has been removed then
5140 it won't be in .gdb_index. */
a2ce51a0
DE
5141
5142static struct signatured_type *
5143lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5144{
5145 struct objfile *objfile = dwarf2_per_objfile->objfile;
5146 struct dwp_file *dwp_file = get_dwp_file ();
5147 struct dwo_unit *dwo_entry;
5148 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5149 void **slot;
a2ce51a0
DE
5150
5151 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5152 gdb_assert (dwp_file != NULL);
5153
6aa5f3a6
DE
5154 /* If TU skeletons have been removed then we may not have read in any
5155 TUs yet. */
5156 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5157 {
6aa5f3a6
DE
5158 dwarf2_per_objfile->signatured_types
5159 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5160 }
5161
6aa5f3a6
DE
5162 find_sig_entry.signature = sig;
5163 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5164 &find_sig_entry, INSERT);
9a3c8263 5165 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5166
5167 /* Have we already tried to read this TU?
5168 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5169 needn't exist in the global table yet). */
5170 if (sig_entry != NULL)
5171 return sig_entry;
5172
a2ce51a0
DE
5173 if (dwp_file->tus == NULL)
5174 return NULL;
57d63ce2
DE
5175 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5176 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5177 if (dwo_entry == NULL)
5178 return NULL;
5179
6aa5f3a6 5180 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5181 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5182
a2ce51a0
DE
5183 return sig_entry;
5184}
5185
380bca97 5186/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5187 Returns NULL if signature SIG is not present in the table.
5188 It is up to the caller to complain about this. */
348e048f
DE
5189
5190static struct signatured_type *
a2ce51a0 5191lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5192{
a2ce51a0
DE
5193 if (cu->dwo_unit
5194 && dwarf2_per_objfile->using_index)
5195 {
5196 /* We're in a DWO/DWP file, and we're using .gdb_index.
5197 These cases require special processing. */
5198 if (get_dwp_file () == NULL)
5199 return lookup_dwo_signatured_type (cu, sig);
5200 else
5201 return lookup_dwp_signatured_type (cu, sig);
5202 }
5203 else
5204 {
5205 struct signatured_type find_entry, *entry;
348e048f 5206
a2ce51a0
DE
5207 if (dwarf2_per_objfile->signatured_types == NULL)
5208 return NULL;
5209 find_entry.signature = sig;
9a3c8263
SM
5210 entry = ((struct signatured_type *)
5211 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5212 return entry;
5213 }
348e048f 5214}
42e7ad6c
DE
5215\f
5216/* Low level DIE reading support. */
348e048f 5217
d85a05f0
DJ
5218/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5219
5220static void
5221init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5222 struct dwarf2_cu *cu,
3019eac3
DE
5223 struct dwarf2_section_info *section,
5224 struct dwo_file *dwo_file)
d85a05f0 5225{
fceca515 5226 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5227 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5228 reader->cu = cu;
3019eac3 5229 reader->dwo_file = dwo_file;
dee91e82
DE
5230 reader->die_section = section;
5231 reader->buffer = section->buffer;
f664829e 5232 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5233 reader->comp_dir = NULL;
d85a05f0
DJ
5234}
5235
b0c7bfa9
DE
5236/* Subroutine of init_cutu_and_read_dies to simplify it.
5237 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5238 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5239 already.
5240
5241 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5242 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5243 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5244 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5245 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5246 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5247 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5248 are filled in with the info of the DIE from the DWO file.
5249 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5250 provided an abbrev table to use.
5251 The result is non-zero if a valid (non-dummy) DIE was found. */
5252
5253static int
5254read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5255 struct dwo_unit *dwo_unit,
5256 int abbrev_table_provided,
5257 struct die_info *stub_comp_unit_die,
a2ce51a0 5258 const char *stub_comp_dir,
b0c7bfa9 5259 struct die_reader_specs *result_reader,
d521ce57 5260 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5261 struct die_info **result_comp_unit_die,
5262 int *result_has_children)
5263{
5264 struct objfile *objfile = dwarf2_per_objfile->objfile;
5265 struct dwarf2_cu *cu = this_cu->cu;
5266 struct dwarf2_section_info *section;
5267 bfd *abfd;
d521ce57 5268 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5269 ULONGEST signature; /* Or dwo_id. */
5270 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5271 int i,num_extra_attrs;
5272 struct dwarf2_section_info *dwo_abbrev_section;
5273 struct attribute *attr;
5274 struct die_info *comp_unit_die;
5275
b0aeadb3
DE
5276 /* At most one of these may be provided. */
5277 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5278
b0c7bfa9
DE
5279 /* These attributes aren't processed until later:
5280 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5281 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5282 referenced later. However, these attributes are found in the stub
5283 which we won't have later. In order to not impose this complication
5284 on the rest of the code, we read them here and copy them to the
5285 DWO CU/TU die. */
b0c7bfa9
DE
5286
5287 stmt_list = NULL;
5288 low_pc = NULL;
5289 high_pc = NULL;
5290 ranges = NULL;
5291 comp_dir = NULL;
5292
5293 if (stub_comp_unit_die != NULL)
5294 {
5295 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5296 DWO file. */
5297 if (! this_cu->is_debug_types)
5298 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5299 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5300 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5301 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5302 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5303
5304 /* There should be a DW_AT_addr_base attribute here (if needed).
5305 We need the value before we can process DW_FORM_GNU_addr_index. */
5306 cu->addr_base = 0;
5307 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5308 if (attr)
5309 cu->addr_base = DW_UNSND (attr);
5310
5311 /* There should be a DW_AT_ranges_base attribute here (if needed).
5312 We need the value before we can process DW_AT_ranges. */
5313 cu->ranges_base = 0;
5314 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5315 if (attr)
5316 cu->ranges_base = DW_UNSND (attr);
5317 }
a2ce51a0
DE
5318 else if (stub_comp_dir != NULL)
5319 {
5320 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5321 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5322 comp_dir->name = DW_AT_comp_dir;
5323 comp_dir->form = DW_FORM_string;
5324 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5325 DW_STRING (comp_dir) = stub_comp_dir;
5326 }
b0c7bfa9
DE
5327
5328 /* Set up for reading the DWO CU/TU. */
5329 cu->dwo_unit = dwo_unit;
5330 section = dwo_unit->section;
5331 dwarf2_read_section (objfile, section);
a32a8923 5332 abfd = get_section_bfd_owner (section);
9c541725
PA
5333 begin_info_ptr = info_ptr = (section->buffer
5334 + to_underlying (dwo_unit->sect_off));
b0c7bfa9
DE
5335 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5336 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5337
5338 if (this_cu->is_debug_types)
5339 {
b0c7bfa9
DE
5340 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5341
43988095 5342 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5343 dwo_abbrev_section,
43988095 5344 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5345 /* This is not an assert because it can be caused by bad debug info. */
43988095 5346 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5347 {
5348 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5349 " TU at offset 0x%x [in module %s]"),
5350 hex_string (sig_type->signature),
43988095 5351 hex_string (cu->header.signature),
9c541725 5352 to_underlying (dwo_unit->sect_off),
a2ce51a0
DE
5353 bfd_get_filename (abfd));
5354 }
9c541725 5355 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5356 /* For DWOs coming from DWP files, we don't know the CU length
5357 nor the type's offset in the TU until now. */
5358 dwo_unit->length = get_cu_length (&cu->header);
9c541725 5359 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
5360
5361 /* Establish the type offset that can be used to lookup the type.
5362 For DWO files, we don't know it until now. */
9c541725
PA
5363 sig_type->type_offset_in_section
5364 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
5365 }
5366 else
5367 {
5368 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5369 dwo_abbrev_section,
43988095 5370 info_ptr, rcuh_kind::COMPILE);
9c541725 5371 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
5372 /* For DWOs coming from DWP files, we don't know the CU length
5373 until now. */
5374 dwo_unit->length = get_cu_length (&cu->header);
5375 }
5376
02142a6c
DE
5377 /* Replace the CU's original abbrev table with the DWO's.
5378 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5379 if (abbrev_table_provided)
5380 {
5381 /* Don't free the provided abbrev table, the caller of
5382 init_cutu_and_read_dies owns it. */
5383 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5384 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5385 make_cleanup (dwarf2_free_abbrev_table, cu);
5386 }
5387 else
5388 {
5389 dwarf2_free_abbrev_table (cu);
5390 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5391 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5392 }
5393
5394 /* Read in the die, but leave space to copy over the attributes
5395 from the stub. This has the benefit of simplifying the rest of
5396 the code - all the work to maintain the illusion of a single
5397 DW_TAG_{compile,type}_unit DIE is done here. */
5398 num_extra_attrs = ((stmt_list != NULL)
5399 + (low_pc != NULL)
5400 + (high_pc != NULL)
5401 + (ranges != NULL)
5402 + (comp_dir != NULL));
5403 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5404 result_has_children, num_extra_attrs);
5405
5406 /* Copy over the attributes from the stub to the DIE we just read in. */
5407 comp_unit_die = *result_comp_unit_die;
5408 i = comp_unit_die->num_attrs;
5409 if (stmt_list != NULL)
5410 comp_unit_die->attrs[i++] = *stmt_list;
5411 if (low_pc != NULL)
5412 comp_unit_die->attrs[i++] = *low_pc;
5413 if (high_pc != NULL)
5414 comp_unit_die->attrs[i++] = *high_pc;
5415 if (ranges != NULL)
5416 comp_unit_die->attrs[i++] = *ranges;
5417 if (comp_dir != NULL)
5418 comp_unit_die->attrs[i++] = *comp_dir;
5419 comp_unit_die->num_attrs += num_extra_attrs;
5420
b4f54984 5421 if (dwarf_die_debug)
bf6af496
DE
5422 {
5423 fprintf_unfiltered (gdb_stdlog,
5424 "Read die from %s@0x%x of %s:\n",
a32a8923 5425 get_section_name (section),
bf6af496
DE
5426 (unsigned) (begin_info_ptr - section->buffer),
5427 bfd_get_filename (abfd));
b4f54984 5428 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5429 }
5430
a2ce51a0
DE
5431 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5432 TUs by skipping the stub and going directly to the entry in the DWO file.
5433 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5434 to get it via circuitous means. Blech. */
5435 if (comp_dir != NULL)
5436 result_reader->comp_dir = DW_STRING (comp_dir);
5437
b0c7bfa9
DE
5438 /* Skip dummy compilation units. */
5439 if (info_ptr >= begin_info_ptr + dwo_unit->length
5440 || peek_abbrev_code (abfd, info_ptr) == 0)
5441 return 0;
5442
5443 *result_info_ptr = info_ptr;
5444 return 1;
5445}
5446
5447/* Subroutine of init_cutu_and_read_dies to simplify it.
5448 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5449 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5450
5451static struct dwo_unit *
5452lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5453 struct die_info *comp_unit_die)
5454{
5455 struct dwarf2_cu *cu = this_cu->cu;
5456 struct attribute *attr;
5457 ULONGEST signature;
5458 struct dwo_unit *dwo_unit;
5459 const char *comp_dir, *dwo_name;
5460
a2ce51a0
DE
5461 gdb_assert (cu != NULL);
5462
b0c7bfa9 5463 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5464 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5465 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5466
5467 if (this_cu->is_debug_types)
5468 {
5469 struct signatured_type *sig_type;
5470
5471 /* Since this_cu is the first member of struct signatured_type,
5472 we can go from a pointer to one to a pointer to the other. */
5473 sig_type = (struct signatured_type *) this_cu;
5474 signature = sig_type->signature;
5475 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5476 }
5477 else
5478 {
5479 struct attribute *attr;
5480
5481 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5482 if (! attr)
5483 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5484 " [in module %s]"),
4262abfb 5485 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5486 signature = DW_UNSND (attr);
5487 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5488 signature);
5489 }
5490
b0c7bfa9
DE
5491 return dwo_unit;
5492}
5493
a2ce51a0 5494/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5495 See it for a description of the parameters.
5496 Read a TU directly from a DWO file, bypassing the stub.
5497
5498 Note: This function could be a little bit simpler if we shared cleanups
5499 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5500 to do, so we keep this function self-contained. Or we could move this
5501 into our caller, but it's complex enough already. */
a2ce51a0
DE
5502
5503static void
6aa5f3a6
DE
5504init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5505 int use_existing_cu, int keep,
a2ce51a0
DE
5506 die_reader_func_ftype *die_reader_func,
5507 void *data)
5508{
5509 struct dwarf2_cu *cu;
5510 struct signatured_type *sig_type;
6aa5f3a6 5511 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5512 struct die_reader_specs reader;
5513 const gdb_byte *info_ptr;
5514 struct die_info *comp_unit_die;
5515 int has_children;
5516
5517 /* Verify we can do the following downcast, and that we have the
5518 data we need. */
5519 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5520 sig_type = (struct signatured_type *) this_cu;
5521 gdb_assert (sig_type->dwo_unit != NULL);
5522
5523 cleanups = make_cleanup (null_cleanup, NULL);
5524
6aa5f3a6
DE
5525 if (use_existing_cu && this_cu->cu != NULL)
5526 {
5527 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5528 cu = this_cu->cu;
5529 /* There's no need to do the rereading_dwo_cu handling that
5530 init_cutu_and_read_dies does since we don't read the stub. */
5531 }
5532 else
5533 {
5534 /* If !use_existing_cu, this_cu->cu must be NULL. */
5535 gdb_assert (this_cu->cu == NULL);
8d749320 5536 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5537 init_one_comp_unit (cu, this_cu);
5538 /* If an error occurs while loading, release our storage. */
5539 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5540 }
5541
5542 /* A future optimization, if needed, would be to use an existing
5543 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5544 could share abbrev tables. */
a2ce51a0
DE
5545
5546 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5547 0 /* abbrev_table_provided */,
5548 NULL /* stub_comp_unit_die */,
5549 sig_type->dwo_unit->dwo_file->comp_dir,
5550 &reader, &info_ptr,
5551 &comp_unit_die, &has_children) == 0)
5552 {
5553 /* Dummy die. */
5554 do_cleanups (cleanups);
5555 return;
5556 }
5557
5558 /* All the "real" work is done here. */
5559 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5560
6aa5f3a6 5561 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5562 but the alternative is making the latter more complex.
5563 This function is only for the special case of using DWO files directly:
5564 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5565 if (free_cu_cleanup != NULL)
a2ce51a0 5566 {
6aa5f3a6
DE
5567 if (keep)
5568 {
5569 /* We've successfully allocated this compilation unit. Let our
5570 caller clean it up when finished with it. */
5571 discard_cleanups (free_cu_cleanup);
a2ce51a0 5572
6aa5f3a6
DE
5573 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5574 So we have to manually free the abbrev table. */
5575 dwarf2_free_abbrev_table (cu);
a2ce51a0 5576
6aa5f3a6
DE
5577 /* Link this CU into read_in_chain. */
5578 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5579 dwarf2_per_objfile->read_in_chain = this_cu;
5580 }
5581 else
5582 do_cleanups (free_cu_cleanup);
a2ce51a0 5583 }
a2ce51a0
DE
5584
5585 do_cleanups (cleanups);
5586}
5587
fd820528 5588/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5589 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5590
f4dc4d17
DE
5591 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5592 Otherwise the table specified in the comp unit header is read in and used.
5593 This is an optimization for when we already have the abbrev table.
5594
dee91e82
DE
5595 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5596 Otherwise, a new CU is allocated with xmalloc.
5597
5598 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5599 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5600
5601 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5602 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5603
70221824 5604static void
fd820528 5605init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5606 struct abbrev_table *abbrev_table,
fd820528
DE
5607 int use_existing_cu, int keep,
5608 die_reader_func_ftype *die_reader_func,
5609 void *data)
c906108c 5610{
dee91e82 5611 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5612 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5613 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5614 struct dwarf2_cu *cu;
d521ce57 5615 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5616 struct die_reader_specs reader;
d85a05f0 5617 struct die_info *comp_unit_die;
dee91e82 5618 int has_children;
d85a05f0 5619 struct attribute *attr;
365156ad 5620 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5621 struct signatured_type *sig_type = NULL;
4bdcc0c1 5622 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5623 /* Non-zero if CU currently points to a DWO file and we need to
5624 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5625 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5626 int rereading_dwo_cu = 0;
c906108c 5627
b4f54984 5628 if (dwarf_die_debug)
09406207
DE
5629 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5630 this_cu->is_debug_types ? "type" : "comp",
9c541725 5631 to_underlying (this_cu->sect_off));
09406207 5632
dee91e82
DE
5633 if (use_existing_cu)
5634 gdb_assert (keep);
23745b47 5635
a2ce51a0
DE
5636 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5637 file (instead of going through the stub), short-circuit all of this. */
5638 if (this_cu->reading_dwo_directly)
5639 {
5640 /* Narrow down the scope of possibilities to have to understand. */
5641 gdb_assert (this_cu->is_debug_types);
5642 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5643 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5644 die_reader_func, data);
a2ce51a0
DE
5645 return;
5646 }
5647
dee91e82
DE
5648 cleanups = make_cleanup (null_cleanup, NULL);
5649
5650 /* This is cheap if the section is already read in. */
5651 dwarf2_read_section (objfile, section);
5652
9c541725 5653 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
5654
5655 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5656
5657 if (use_existing_cu && this_cu->cu != NULL)
5658 {
5659 cu = this_cu->cu;
42e7ad6c
DE
5660 /* If this CU is from a DWO file we need to start over, we need to
5661 refetch the attributes from the skeleton CU.
5662 This could be optimized by retrieving those attributes from when we
5663 were here the first time: the previous comp_unit_die was stored in
5664 comp_unit_obstack. But there's no data yet that we need this
5665 optimization. */
5666 if (cu->dwo_unit != NULL)
5667 rereading_dwo_cu = 1;
dee91e82
DE
5668 }
5669 else
5670 {
5671 /* If !use_existing_cu, this_cu->cu must be NULL. */
5672 gdb_assert (this_cu->cu == NULL);
8d749320 5673 cu = XNEW (struct dwarf2_cu);
dee91e82 5674 init_one_comp_unit (cu, this_cu);
dee91e82 5675 /* If an error occurs while loading, release our storage. */
365156ad 5676 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5677 }
dee91e82 5678
b0c7bfa9 5679 /* Get the header. */
9c541725 5680 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
5681 {
5682 /* We already have the header, there's no need to read it in again. */
9c541725 5683 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
5684 }
5685 else
5686 {
3019eac3 5687 if (this_cu->is_debug_types)
dee91e82 5688 {
43988095 5689 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5690 abbrev_section, info_ptr,
43988095 5691 rcuh_kind::TYPE);
dee91e82 5692
42e7ad6c
DE
5693 /* Since per_cu is the first member of struct signatured_type,
5694 we can go from a pointer to one to a pointer to the other. */
5695 sig_type = (struct signatured_type *) this_cu;
43988095 5696 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
5697 gdb_assert (sig_type->type_offset_in_tu
5698 == cu->header.type_cu_offset_in_tu);
5699 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 5700
42e7ad6c
DE
5701 /* LENGTH has not been set yet for type units if we're
5702 using .gdb_index. */
1ce1cefd 5703 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5704
5705 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
5706 sig_type->type_offset_in_section =
5707 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
5708
5709 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5710 }
5711 else
5712 {
4bdcc0c1
DE
5713 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5714 abbrev_section,
43988095
JK
5715 info_ptr,
5716 rcuh_kind::COMPILE);
dee91e82 5717
9c541725 5718 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 5719 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5720 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5721 }
5722 }
10b3939b 5723
6caca83c 5724 /* Skip dummy compilation units. */
dee91e82 5725 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5726 || peek_abbrev_code (abfd, info_ptr) == 0)
5727 {
dee91e82 5728 do_cleanups (cleanups);
21b2bd31 5729 return;
6caca83c
CC
5730 }
5731
433df2d4
DE
5732 /* If we don't have them yet, read the abbrevs for this compilation unit.
5733 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5734 done. Note that it's important that if the CU had an abbrev table
5735 on entry we don't free it when we're done: Somewhere up the call stack
5736 it may be in use. */
f4dc4d17
DE
5737 if (abbrev_table != NULL)
5738 {
5739 gdb_assert (cu->abbrev_table == NULL);
9c541725 5740 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
f4dc4d17
DE
5741 cu->abbrev_table = abbrev_table;
5742 }
5743 else if (cu->abbrev_table == NULL)
dee91e82 5744 {
4bdcc0c1 5745 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5746 make_cleanup (dwarf2_free_abbrev_table, cu);
5747 }
42e7ad6c
DE
5748 else if (rereading_dwo_cu)
5749 {
5750 dwarf2_free_abbrev_table (cu);
5751 dwarf2_read_abbrevs (cu, abbrev_section);
5752 }
af703f96 5753
dee91e82 5754 /* Read the top level CU/TU die. */
3019eac3 5755 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5756 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5757
b0c7bfa9
DE
5758 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5759 from the DWO file.
5760 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5761 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5762 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5763 if (attr)
5764 {
3019eac3 5765 struct dwo_unit *dwo_unit;
b0c7bfa9 5766 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5767
5768 if (has_children)
6a506a2d
DE
5769 {
5770 complaint (&symfile_complaints,
5771 _("compilation unit with DW_AT_GNU_dwo_name"
5772 " has children (offset 0x%x) [in module %s]"),
9c541725 5773 to_underlying (this_cu->sect_off), bfd_get_filename (abfd));
6a506a2d 5774 }
b0c7bfa9 5775 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5776 if (dwo_unit != NULL)
3019eac3 5777 {
6a506a2d
DE
5778 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5779 abbrev_table != NULL,
a2ce51a0 5780 comp_unit_die, NULL,
6a506a2d
DE
5781 &reader, &info_ptr,
5782 &dwo_comp_unit_die, &has_children) == 0)
5783 {
5784 /* Dummy die. */
5785 do_cleanups (cleanups);
5786 return;
5787 }
5788 comp_unit_die = dwo_comp_unit_die;
5789 }
5790 else
5791 {
5792 /* Yikes, we couldn't find the rest of the DIE, we only have
5793 the stub. A complaint has already been logged. There's
5794 not much more we can do except pass on the stub DIE to
5795 die_reader_func. We don't want to throw an error on bad
5796 debug info. */
3019eac3
DE
5797 }
5798 }
5799
b0c7bfa9 5800 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5801 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5802
b0c7bfa9 5803 /* Done, clean up. */
365156ad 5804 if (free_cu_cleanup != NULL)
348e048f 5805 {
365156ad
TT
5806 if (keep)
5807 {
5808 /* We've successfully allocated this compilation unit. Let our
5809 caller clean it up when finished with it. */
5810 discard_cleanups (free_cu_cleanup);
dee91e82 5811
365156ad
TT
5812 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5813 So we have to manually free the abbrev table. */
5814 dwarf2_free_abbrev_table (cu);
dee91e82 5815
365156ad
TT
5816 /* Link this CU into read_in_chain. */
5817 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5818 dwarf2_per_objfile->read_in_chain = this_cu;
5819 }
5820 else
5821 do_cleanups (free_cu_cleanup);
348e048f 5822 }
365156ad
TT
5823
5824 do_cleanups (cleanups);
dee91e82
DE
5825}
5826
33e80786
DE
5827/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5828 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5829 to have already done the lookup to find the DWO file).
dee91e82
DE
5830
5831 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5832 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5833
5834 We fill in THIS_CU->length.
5835
5836 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5837 linker) then DIE_READER_FUNC will not get called.
5838
5839 THIS_CU->cu is always freed when done.
3019eac3
DE
5840 This is done in order to not leave THIS_CU->cu in a state where we have
5841 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5842
5843static void
5844init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5845 struct dwo_file *dwo_file,
dee91e82
DE
5846 die_reader_func_ftype *die_reader_func,
5847 void *data)
5848{
5849 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5850 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5851 bfd *abfd = get_section_bfd_owner (section);
33e80786 5852 struct dwarf2_section_info *abbrev_section;
dee91e82 5853 struct dwarf2_cu cu;
d521ce57 5854 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5855 struct die_reader_specs reader;
5856 struct cleanup *cleanups;
5857 struct die_info *comp_unit_die;
5858 int has_children;
5859
b4f54984 5860 if (dwarf_die_debug)
09406207
DE
5861 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5862 this_cu->is_debug_types ? "type" : "comp",
9c541725 5863 to_underlying (this_cu->sect_off));
09406207 5864
dee91e82
DE
5865 gdb_assert (this_cu->cu == NULL);
5866
33e80786
DE
5867 abbrev_section = (dwo_file != NULL
5868 ? &dwo_file->sections.abbrev
5869 : get_abbrev_section_for_cu (this_cu));
5870
dee91e82
DE
5871 /* This is cheap if the section is already read in. */
5872 dwarf2_read_section (objfile, section);
5873
5874 init_one_comp_unit (&cu, this_cu);
5875
5876 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5877
9c541725 5878 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4bdcc0c1
DE
5879 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5880 abbrev_section, info_ptr,
43988095
JK
5881 (this_cu->is_debug_types
5882 ? rcuh_kind::TYPE
5883 : rcuh_kind::COMPILE));
dee91e82 5884
1ce1cefd 5885 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5886
5887 /* Skip dummy compilation units. */
5888 if (info_ptr >= begin_info_ptr + this_cu->length
5889 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5890 {
dee91e82 5891 do_cleanups (cleanups);
21b2bd31 5892 return;
93311388 5893 }
72bf9492 5894
dee91e82
DE
5895 dwarf2_read_abbrevs (&cu, abbrev_section);
5896 make_cleanup (dwarf2_free_abbrev_table, &cu);
5897
3019eac3 5898 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5899 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5900
5901 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5902
5903 do_cleanups (cleanups);
5904}
5905
3019eac3
DE
5906/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5907 does not lookup the specified DWO file.
5908 This cannot be used to read DWO files.
dee91e82
DE
5909
5910 THIS_CU->cu is always freed when done.
3019eac3
DE
5911 This is done in order to not leave THIS_CU->cu in a state where we have
5912 to care whether it refers to the "main" CU or the DWO CU.
5913 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5914
5915static void
5916init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5917 die_reader_func_ftype *die_reader_func,
5918 void *data)
5919{
33e80786 5920 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5921}
0018ea6f
DE
5922\f
5923/* Type Unit Groups.
dee91e82 5924
0018ea6f
DE
5925 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5926 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5927 so that all types coming from the same compilation (.o file) are grouped
5928 together. A future step could be to put the types in the same symtab as
5929 the CU the types ultimately came from. */
ff013f42 5930
f4dc4d17
DE
5931static hashval_t
5932hash_type_unit_group (const void *item)
5933{
9a3c8263
SM
5934 const struct type_unit_group *tu_group
5935 = (const struct type_unit_group *) item;
f4dc4d17 5936
094b34ac 5937 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5938}
348e048f
DE
5939
5940static int
f4dc4d17 5941eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5942{
9a3c8263
SM
5943 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5944 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 5945
094b34ac 5946 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5947}
348e048f 5948
f4dc4d17
DE
5949/* Allocate a hash table for type unit groups. */
5950
5951static htab_t
5952allocate_type_unit_groups_table (void)
5953{
5954 return htab_create_alloc_ex (3,
5955 hash_type_unit_group,
5956 eq_type_unit_group,
5957 NULL,
5958 &dwarf2_per_objfile->objfile->objfile_obstack,
5959 hashtab_obstack_allocate,
5960 dummy_obstack_deallocate);
5961}
dee91e82 5962
f4dc4d17
DE
5963/* Type units that don't have DW_AT_stmt_list are grouped into their own
5964 partial symtabs. We combine several TUs per psymtab to not let the size
5965 of any one psymtab grow too big. */
5966#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5967#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5968
094b34ac 5969/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5970 Create the type_unit_group object used to hold one or more TUs. */
5971
5972static struct type_unit_group *
094b34ac 5973create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5974{
5975 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5976 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5977 struct type_unit_group *tu_group;
f4dc4d17
DE
5978
5979 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5980 struct type_unit_group);
094b34ac 5981 per_cu = &tu_group->per_cu;
f4dc4d17 5982 per_cu->objfile = objfile;
f4dc4d17 5983
094b34ac
DE
5984 if (dwarf2_per_objfile->using_index)
5985 {
5986 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5987 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5988 }
5989 else
5990 {
9c541725 5991 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
5992 struct partial_symtab *pst;
5993 char *name;
5994
5995 /* Give the symtab a useful name for debug purposes. */
5996 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5997 name = xstrprintf ("<type_units_%d>",
5998 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5999 else
6000 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
6001
6002 pst = create_partial_symtab (per_cu, name);
6003 pst->anonymous = 1;
f4dc4d17 6004
094b34ac
DE
6005 xfree (name);
6006 }
f4dc4d17 6007
094b34ac 6008 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6009 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6010
6011 return tu_group;
6012}
6013
094b34ac
DE
6014/* Look up the type_unit_group for type unit CU, and create it if necessary.
6015 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6016
6017static struct type_unit_group *
ff39bb5e 6018get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
6019{
6020 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6021 struct type_unit_group *tu_group;
6022 void **slot;
6023 unsigned int line_offset;
6024 struct type_unit_group type_unit_group_for_lookup;
6025
6026 if (dwarf2_per_objfile->type_unit_groups == NULL)
6027 {
6028 dwarf2_per_objfile->type_unit_groups =
6029 allocate_type_unit_groups_table ();
6030 }
6031
6032 /* Do we need to create a new group, or can we use an existing one? */
6033
6034 if (stmt_list)
6035 {
6036 line_offset = DW_UNSND (stmt_list);
6037 ++tu_stats->nr_symtab_sharers;
6038 }
6039 else
6040 {
6041 /* Ugh, no stmt_list. Rare, but we have to handle it.
6042 We can do various things here like create one group per TU or
6043 spread them over multiple groups to split up the expansion work.
6044 To avoid worst case scenarios (too many groups or too large groups)
6045 we, umm, group them in bunches. */
6046 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6047 | (tu_stats->nr_stmt_less_type_units
6048 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6049 ++tu_stats->nr_stmt_less_type_units;
6050 }
6051
094b34ac 6052 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6053 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
6054 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6055 &type_unit_group_for_lookup, INSERT);
6056 if (*slot != NULL)
6057 {
9a3c8263 6058 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
6059 gdb_assert (tu_group != NULL);
6060 }
6061 else
6062 {
9c541725 6063 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 6064 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
6065 *slot = tu_group;
6066 ++tu_stats->nr_symtabs;
6067 }
6068
6069 return tu_group;
6070}
0018ea6f
DE
6071\f
6072/* Partial symbol tables. */
6073
6074/* Create a psymtab named NAME and assign it to PER_CU.
6075
6076 The caller must fill in the following details:
6077 dirname, textlow, texthigh. */
6078
6079static struct partial_symtab *
6080create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6081{
6082 struct objfile *objfile = per_cu->objfile;
6083 struct partial_symtab *pst;
6084
18a94d75 6085 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
6086 objfile->global_psymbols.next,
6087 objfile->static_psymbols.next);
6088
6089 pst->psymtabs_addrmap_supported = 1;
6090
6091 /* This is the glue that links PST into GDB's symbol API. */
6092 pst->read_symtab_private = per_cu;
6093 pst->read_symtab = dwarf2_read_symtab;
6094 per_cu->v.psymtab = pst;
6095
6096 return pst;
6097}
6098
b93601f3
TT
6099/* The DATA object passed to process_psymtab_comp_unit_reader has this
6100 type. */
6101
6102struct process_psymtab_comp_unit_data
6103{
6104 /* True if we are reading a DW_TAG_partial_unit. */
6105
6106 int want_partial_unit;
6107
6108 /* The "pretend" language that is used if the CU doesn't declare a
6109 language. */
6110
6111 enum language pretend_language;
6112};
6113
0018ea6f
DE
6114/* die_reader_func for process_psymtab_comp_unit. */
6115
6116static void
6117process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6118 const gdb_byte *info_ptr,
0018ea6f
DE
6119 struct die_info *comp_unit_die,
6120 int has_children,
6121 void *data)
6122{
6123 struct dwarf2_cu *cu = reader->cu;
6124 struct objfile *objfile = cu->objfile;
3e29f34a 6125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6126 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6127 CORE_ADDR baseaddr;
6128 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6129 struct partial_symtab *pst;
3a2b436a 6130 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6131 const char *filename;
9a3c8263
SM
6132 struct process_psymtab_comp_unit_data *info
6133 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6134
b93601f3 6135 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6136 return;
6137
6138 gdb_assert (! per_cu->is_debug_types);
6139
b93601f3 6140 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6141
6142 cu->list_in_scope = &file_symbols;
6143
6144 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6145 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6146 if (filename == NULL)
0018ea6f 6147 filename = "";
0018ea6f
DE
6148
6149 pst = create_partial_symtab (per_cu, filename);
6150
6151 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6152 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6153
6154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6155
6156 dwarf2_find_base_address (comp_unit_die, cu);
6157
6158 /* Possibly set the default values of LOWPC and HIGHPC from
6159 `DW_AT_ranges'. */
3a2b436a
JK
6160 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6161 &best_highpc, cu, pst);
6162 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6163 /* Store the contiguous range if it is not empty; it can be empty for
6164 CUs with no code. */
6165 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6166 gdbarch_adjust_dwarf2_addr (gdbarch,
6167 best_lowpc + baseaddr),
6168 gdbarch_adjust_dwarf2_addr (gdbarch,
6169 best_highpc + baseaddr) - 1,
6170 pst);
0018ea6f
DE
6171
6172 /* Check if comp unit has_children.
6173 If so, read the rest of the partial symbols from this comp unit.
6174 If not, there's no more debug_info for this comp unit. */
6175 if (has_children)
6176 {
6177 struct partial_die_info *first_die;
6178 CORE_ADDR lowpc, highpc;
6179
6180 lowpc = ((CORE_ADDR) -1);
6181 highpc = ((CORE_ADDR) 0);
6182
6183 first_die = load_partial_dies (reader, info_ptr, 1);
6184
6185 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6186 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6187
6188 /* If we didn't find a lowpc, set it to highpc to avoid
6189 complaints from `maint check'. */
6190 if (lowpc == ((CORE_ADDR) -1))
6191 lowpc = highpc;
6192
6193 /* If the compilation unit didn't have an explicit address range,
6194 then use the information extracted from its child dies. */
e385593e 6195 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6196 {
6197 best_lowpc = lowpc;
6198 best_highpc = highpc;
6199 }
6200 }
3e29f34a
MR
6201 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6202 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6203
8763cede 6204 end_psymtab_common (objfile, pst);
0018ea6f
DE
6205
6206 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6207 {
6208 int i;
6209 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6210 struct dwarf2_per_cu_data *iter;
6211
6212 /* Fill in 'dependencies' here; we fill in 'users' in a
6213 post-pass. */
6214 pst->number_of_dependencies = len;
8d749320
SM
6215 pst->dependencies =
6216 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6217 for (i = 0;
6218 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6219 i, iter);
6220 ++i)
6221 pst->dependencies[i] = iter->v.psymtab;
6222
6223 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6224 }
6225
6226 /* Get the list of files included in the current compilation unit,
6227 and build a psymtab for each of them. */
6228 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6229
b4f54984 6230 if (dwarf_read_debug)
0018ea6f
DE
6231 {
6232 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6233
6234 fprintf_unfiltered (gdb_stdlog,
6235 "Psymtab for %s unit @0x%x: %s - %s"
6236 ", %d global, %d static syms\n",
6237 per_cu->is_debug_types ? "type" : "comp",
9c541725 6238 to_underlying (per_cu->sect_off),
0018ea6f
DE
6239 paddress (gdbarch, pst->textlow),
6240 paddress (gdbarch, pst->texthigh),
6241 pst->n_global_syms, pst->n_static_syms);
6242 }
6243}
6244
6245/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6246 Process compilation unit THIS_CU for a psymtab. */
6247
6248static void
6249process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6250 int want_partial_unit,
6251 enum language pretend_language)
0018ea6f 6252{
b93601f3
TT
6253 struct process_psymtab_comp_unit_data info;
6254
0018ea6f
DE
6255 /* If this compilation unit was already read in, free the
6256 cached copy in order to read it in again. This is
6257 necessary because we skipped some symbols when we first
6258 read in the compilation unit (see load_partial_dies).
6259 This problem could be avoided, but the benefit is unclear. */
6260 if (this_cu->cu != NULL)
6261 free_one_cached_comp_unit (this_cu);
6262
6263 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6264 info.want_partial_unit = want_partial_unit;
6265 info.pretend_language = pretend_language;
0018ea6f
DE
6266 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6267 process_psymtab_comp_unit_reader,
b93601f3 6268 &info);
0018ea6f
DE
6269
6270 /* Age out any secondary CUs. */
6271 age_cached_comp_units ();
6272}
f4dc4d17
DE
6273
6274/* Reader function for build_type_psymtabs. */
6275
6276static void
6277build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6278 const gdb_byte *info_ptr,
f4dc4d17
DE
6279 struct die_info *type_unit_die,
6280 int has_children,
6281 void *data)
6282{
6283 struct objfile *objfile = dwarf2_per_objfile->objfile;
6284 struct dwarf2_cu *cu = reader->cu;
6285 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6286 struct signatured_type *sig_type;
f4dc4d17
DE
6287 struct type_unit_group *tu_group;
6288 struct attribute *attr;
6289 struct partial_die_info *first_die;
6290 CORE_ADDR lowpc, highpc;
6291 struct partial_symtab *pst;
6292
6293 gdb_assert (data == NULL);
0186c6a7
DE
6294 gdb_assert (per_cu->is_debug_types);
6295 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6296
6297 if (! has_children)
6298 return;
6299
6300 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6301 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6302
0186c6a7 6303 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6304
6305 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6306 cu->list_in_scope = &file_symbols;
6307 pst = create_partial_symtab (per_cu, "");
6308 pst->anonymous = 1;
6309
6310 first_die = load_partial_dies (reader, info_ptr, 1);
6311
6312 lowpc = (CORE_ADDR) -1;
6313 highpc = (CORE_ADDR) 0;
6314 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6315
8763cede 6316 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6317}
6318
73051182
DE
6319/* Struct used to sort TUs by their abbreviation table offset. */
6320
6321struct tu_abbrev_offset
6322{
6323 struct signatured_type *sig_type;
6324 sect_offset abbrev_offset;
6325};
6326
6327/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6328
6329static int
6330sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6331{
9a3c8263
SM
6332 const struct tu_abbrev_offset * const *a
6333 = (const struct tu_abbrev_offset * const*) ap;
6334 const struct tu_abbrev_offset * const *b
6335 = (const struct tu_abbrev_offset * const*) bp;
9c541725
PA
6336 sect_offset aoff = (*a)->abbrev_offset;
6337 sect_offset boff = (*b)->abbrev_offset;
73051182
DE
6338
6339 return (aoff > boff) - (aoff < boff);
6340}
6341
6342/* Efficiently read all the type units.
6343 This does the bulk of the work for build_type_psymtabs.
6344
6345 The efficiency is because we sort TUs by the abbrev table they use and
6346 only read each abbrev table once. In one program there are 200K TUs
6347 sharing 8K abbrev tables.
6348
6349 The main purpose of this function is to support building the
6350 dwarf2_per_objfile->type_unit_groups table.
6351 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6352 can collapse the search space by grouping them by stmt_list.
6353 The savings can be significant, in the same program from above the 200K TUs
6354 share 8K stmt_list tables.
6355
6356 FUNC is expected to call get_type_unit_group, which will create the
6357 struct type_unit_group if necessary and add it to
6358 dwarf2_per_objfile->type_unit_groups. */
6359
6360static void
6361build_type_psymtabs_1 (void)
6362{
73051182
DE
6363 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6364 struct cleanup *cleanups;
6365 struct abbrev_table *abbrev_table;
6366 sect_offset abbrev_offset;
6367 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6368 int i;
6369
6370 /* It's up to the caller to not call us multiple times. */
6371 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6372
6373 if (dwarf2_per_objfile->n_type_units == 0)
6374 return;
6375
6376 /* TUs typically share abbrev tables, and there can be way more TUs than
6377 abbrev tables. Sort by abbrev table to reduce the number of times we
6378 read each abbrev table in.
6379 Alternatives are to punt or to maintain a cache of abbrev tables.
6380 This is simpler and efficient enough for now.
6381
6382 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6383 symtab to use). Typically TUs with the same abbrev offset have the same
6384 stmt_list value too so in practice this should work well.
6385
6386 The basic algorithm here is:
6387
6388 sort TUs by abbrev table
6389 for each TU with same abbrev table:
6390 read abbrev table if first user
6391 read TU top level DIE
6392 [IWBN if DWO skeletons had DW_AT_stmt_list]
6393 call FUNC */
6394
b4f54984 6395 if (dwarf_read_debug)
73051182
DE
6396 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6397
6398 /* Sort in a separate table to maintain the order of all_type_units
6399 for .gdb_index: TU indices directly index all_type_units. */
6400 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6401 dwarf2_per_objfile->n_type_units);
6402 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6403 {
6404 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6405
6406 sorted_by_abbrev[i].sig_type = sig_type;
6407 sorted_by_abbrev[i].abbrev_offset =
6408 read_abbrev_offset (sig_type->per_cu.section,
9c541725 6409 sig_type->per_cu.sect_off);
73051182
DE
6410 }
6411 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6412 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6413 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6414
9c541725 6415 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182
DE
6416 abbrev_table = NULL;
6417 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6418
6419 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6420 {
6421 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6422
6423 /* Switch to the next abbrev table if necessary. */
6424 if (abbrev_table == NULL
9c541725 6425 || tu->abbrev_offset != abbrev_offset)
73051182
DE
6426 {
6427 if (abbrev_table != NULL)
6428 {
6429 abbrev_table_free (abbrev_table);
6430 /* Reset to NULL in case abbrev_table_read_table throws
6431 an error: abbrev_table_free_cleanup will get called. */
6432 abbrev_table = NULL;
6433 }
6434 abbrev_offset = tu->abbrev_offset;
6435 abbrev_table =
6436 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6437 abbrev_offset);
6438 ++tu_stats->nr_uniq_abbrev_tables;
6439 }
6440
6441 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6442 build_type_psymtabs_reader, NULL);
6443 }
6444
73051182 6445 do_cleanups (cleanups);
6aa5f3a6 6446}
73051182 6447
6aa5f3a6
DE
6448/* Print collected type unit statistics. */
6449
6450static void
6451print_tu_stats (void)
6452{
6453 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6454
6455 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6456 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6457 dwarf2_per_objfile->n_type_units);
6458 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6459 tu_stats->nr_uniq_abbrev_tables);
6460 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6461 tu_stats->nr_symtabs);
6462 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6463 tu_stats->nr_symtab_sharers);
6464 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6465 tu_stats->nr_stmt_less_type_units);
6466 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6467 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6468}
6469
f4dc4d17
DE
6470/* Traversal function for build_type_psymtabs. */
6471
6472static int
6473build_type_psymtab_dependencies (void **slot, void *info)
6474{
6475 struct objfile *objfile = dwarf2_per_objfile->objfile;
6476 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6477 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6478 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6479 int len = VEC_length (sig_type_ptr, tu_group->tus);
6480 struct signatured_type *iter;
f4dc4d17
DE
6481 int i;
6482
6483 gdb_assert (len > 0);
0186c6a7 6484 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6485
6486 pst->number_of_dependencies = len;
8d749320
SM
6487 pst->dependencies =
6488 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6489 for (i = 0;
0186c6a7 6490 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6491 ++i)
6492 {
0186c6a7
DE
6493 gdb_assert (iter->per_cu.is_debug_types);
6494 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6495 iter->type_unit_group = tu_group;
f4dc4d17
DE
6496 }
6497
0186c6a7 6498 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6499
6500 return 1;
6501}
6502
6503/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6504 Build partial symbol tables for the .debug_types comp-units. */
6505
6506static void
6507build_type_psymtabs (struct objfile *objfile)
6508{
0e50663e 6509 if (! create_all_type_units (objfile))
348e048f
DE
6510 return;
6511
73051182 6512 build_type_psymtabs_1 ();
6aa5f3a6 6513}
f4dc4d17 6514
6aa5f3a6
DE
6515/* Traversal function for process_skeletonless_type_unit.
6516 Read a TU in a DWO file and build partial symbols for it. */
6517
6518static int
6519process_skeletonless_type_unit (void **slot, void *info)
6520{
6521 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6522 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6523 struct signatured_type find_entry, *entry;
6524
6525 /* If this TU doesn't exist in the global table, add it and read it in. */
6526
6527 if (dwarf2_per_objfile->signatured_types == NULL)
6528 {
6529 dwarf2_per_objfile->signatured_types
6530 = allocate_signatured_type_table (objfile);
6531 }
6532
6533 find_entry.signature = dwo_unit->signature;
6534 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6535 INSERT);
6536 /* If we've already seen this type there's nothing to do. What's happening
6537 is we're doing our own version of comdat-folding here. */
6538 if (*slot != NULL)
6539 return 1;
6540
6541 /* This does the job that create_all_type_units would have done for
6542 this TU. */
6543 entry = add_type_unit (dwo_unit->signature, slot);
6544 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6545 *slot = entry;
6546
6547 /* This does the job that build_type_psymtabs_1 would have done. */
6548 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6549 build_type_psymtabs_reader, NULL);
6550
6551 return 1;
6552}
6553
6554/* Traversal function for process_skeletonless_type_units. */
6555
6556static int
6557process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6558{
6559 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6560
6561 if (dwo_file->tus != NULL)
6562 {
6563 htab_traverse_noresize (dwo_file->tus,
6564 process_skeletonless_type_unit, info);
6565 }
6566
6567 return 1;
6568}
6569
6570/* Scan all TUs of DWO files, verifying we've processed them.
6571 This is needed in case a TU was emitted without its skeleton.
6572 Note: This can't be done until we know what all the DWO files are. */
6573
6574static void
6575process_skeletonless_type_units (struct objfile *objfile)
6576{
6577 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6578 if (get_dwp_file () == NULL
6579 && dwarf2_per_objfile->dwo_files != NULL)
6580 {
6581 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6582 process_dwo_file_for_skeletonless_type_units,
6583 objfile);
6584 }
348e048f
DE
6585}
6586
60606b2c
TT
6587/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6588
6589static void
6590psymtabs_addrmap_cleanup (void *o)
6591{
9a3c8263 6592 struct objfile *objfile = (struct objfile *) o;
ec61707d 6593
60606b2c
TT
6594 objfile->psymtabs_addrmap = NULL;
6595}
6596
95554aad
TT
6597/* Compute the 'user' field for each psymtab in OBJFILE. */
6598
6599static void
6600set_partial_user (struct objfile *objfile)
6601{
6602 int i;
6603
6604 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6605 {
8832e7e3 6606 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6607 struct partial_symtab *pst = per_cu->v.psymtab;
6608 int j;
6609
36586728
TT
6610 if (pst == NULL)
6611 continue;
6612
95554aad
TT
6613 for (j = 0; j < pst->number_of_dependencies; ++j)
6614 {
6615 /* Set the 'user' field only if it is not already set. */
6616 if (pst->dependencies[j]->user == NULL)
6617 pst->dependencies[j]->user = pst;
6618 }
6619 }
6620}
6621
93311388
DE
6622/* Build the partial symbol table by doing a quick pass through the
6623 .debug_info and .debug_abbrev sections. */
72bf9492 6624
93311388 6625static void
c67a9c90 6626dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6627{
60606b2c
TT
6628 struct cleanup *back_to, *addrmap_cleanup;
6629 struct obstack temp_obstack;
21b2bd31 6630 int i;
93311388 6631
b4f54984 6632 if (dwarf_read_debug)
45cfd468
DE
6633 {
6634 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6635 objfile_name (objfile));
45cfd468
DE
6636 }
6637
98bfdba5
PA
6638 dwarf2_per_objfile->reading_partial_symbols = 1;
6639
be391dca 6640 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6641
93311388
DE
6642 /* Any cached compilation units will be linked by the per-objfile
6643 read_in_chain. Make sure to free them when we're done. */
6644 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6645
348e048f
DE
6646 build_type_psymtabs (objfile);
6647
93311388 6648 create_all_comp_units (objfile);
c906108c 6649
60606b2c
TT
6650 /* Create a temporary address map on a temporary obstack. We later
6651 copy this to the final obstack. */
6652 obstack_init (&temp_obstack);
6653 make_cleanup_obstack_free (&temp_obstack);
6654 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6655 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6656
21b2bd31 6657 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6658 {
8832e7e3 6659 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6660
b93601f3 6661 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6662 }
ff013f42 6663
6aa5f3a6
DE
6664 /* This has to wait until we read the CUs, we need the list of DWOs. */
6665 process_skeletonless_type_units (objfile);
6666
6667 /* Now that all TUs have been processed we can fill in the dependencies. */
6668 if (dwarf2_per_objfile->type_unit_groups != NULL)
6669 {
6670 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6671 build_type_psymtab_dependencies, NULL);
6672 }
6673
b4f54984 6674 if (dwarf_read_debug)
6aa5f3a6
DE
6675 print_tu_stats ();
6676
95554aad
TT
6677 set_partial_user (objfile);
6678
ff013f42
JK
6679 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6680 &objfile->objfile_obstack);
60606b2c 6681 discard_cleanups (addrmap_cleanup);
ff013f42 6682
ae038cb0 6683 do_cleanups (back_to);
45cfd468 6684
b4f54984 6685 if (dwarf_read_debug)
45cfd468 6686 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6687 objfile_name (objfile));
ae038cb0
DJ
6688}
6689
3019eac3 6690/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6691
6692static void
dee91e82 6693load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6694 const gdb_byte *info_ptr,
dee91e82
DE
6695 struct die_info *comp_unit_die,
6696 int has_children,
6697 void *data)
ae038cb0 6698{
dee91e82 6699 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6700
95554aad 6701 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6702
ae038cb0
DJ
6703 /* Check if comp unit has_children.
6704 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6705 If not, there's no more debug_info for this comp unit. */
d85a05f0 6706 if (has_children)
dee91e82
DE
6707 load_partial_dies (reader, info_ptr, 0);
6708}
98bfdba5 6709
dee91e82
DE
6710/* Load the partial DIEs for a secondary CU into memory.
6711 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6712
dee91e82
DE
6713static void
6714load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6715{
f4dc4d17
DE
6716 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6717 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6718}
6719
ae038cb0 6720static void
36586728
TT
6721read_comp_units_from_section (struct objfile *objfile,
6722 struct dwarf2_section_info *section,
6723 unsigned int is_dwz,
6724 int *n_allocated,
6725 int *n_comp_units,
6726 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6727{
d521ce57 6728 const gdb_byte *info_ptr;
a32a8923 6729 bfd *abfd = get_section_bfd_owner (section);
be391dca 6730
b4f54984 6731 if (dwarf_read_debug)
bf6af496 6732 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6733 get_section_name (section),
6734 get_section_file_name (section));
bf6af496 6735
36586728 6736 dwarf2_read_section (objfile, section);
ae038cb0 6737
36586728 6738 info_ptr = section->buffer;
6e70227d 6739
36586728 6740 while (info_ptr < section->buffer + section->size)
ae038cb0 6741 {
c764a876 6742 unsigned int length, initial_length_size;
ae038cb0 6743 struct dwarf2_per_cu_data *this_cu;
ae038cb0 6744
9c541725 6745 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0
DJ
6746
6747 /* Read just enough information to find out where the next
6748 compilation unit is. */
36586728 6749 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6750
6751 /* Save the compilation unit for later lookup. */
8d749320 6752 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0 6753 memset (this_cu, 0, sizeof (*this_cu));
9c541725 6754 this_cu->sect_off = sect_off;
c764a876 6755 this_cu->length = length + initial_length_size;
36586728 6756 this_cu->is_dwz = is_dwz;
9291a0cd 6757 this_cu->objfile = objfile;
8a0459fd 6758 this_cu->section = section;
ae038cb0 6759
36586728 6760 if (*n_comp_units == *n_allocated)
ae038cb0 6761 {
36586728 6762 *n_allocated *= 2;
224c3ddb
SM
6763 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6764 *all_comp_units, *n_allocated);
ae038cb0 6765 }
36586728
TT
6766 (*all_comp_units)[*n_comp_units] = this_cu;
6767 ++*n_comp_units;
ae038cb0
DJ
6768
6769 info_ptr = info_ptr + this_cu->length;
6770 }
36586728
TT
6771}
6772
6773/* Create a list of all compilation units in OBJFILE.
6774 This is only done for -readnow and building partial symtabs. */
6775
6776static void
6777create_all_comp_units (struct objfile *objfile)
6778{
6779 int n_allocated;
6780 int n_comp_units;
6781 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6782 struct dwz_file *dwz;
36586728
TT
6783
6784 n_comp_units = 0;
6785 n_allocated = 10;
8d749320 6786 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6787
6788 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6789 &n_allocated, &n_comp_units, &all_comp_units);
6790
4db1a1dc
TT
6791 dwz = dwarf2_get_dwz_file ();
6792 if (dwz != NULL)
6793 read_comp_units_from_section (objfile, &dwz->info, 1,
6794 &n_allocated, &n_comp_units,
6795 &all_comp_units);
ae038cb0 6796
8d749320
SM
6797 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6798 struct dwarf2_per_cu_data *,
6799 n_comp_units);
ae038cb0
DJ
6800 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6801 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6802 xfree (all_comp_units);
6803 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6804}
6805
5734ee8b 6806/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6807 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6808 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6809 DW_AT_ranges). See the comments of add_partial_subprogram on how
6810 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6811
72bf9492
DJ
6812static void
6813scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6814 CORE_ADDR *highpc, int set_addrmap,
6815 struct dwarf2_cu *cu)
c906108c 6816{
72bf9492 6817 struct partial_die_info *pdi;
c906108c 6818
91c24f0a
DC
6819 /* Now, march along the PDI's, descending into ones which have
6820 interesting children but skipping the children of the other ones,
6821 until we reach the end of the compilation unit. */
c906108c 6822
72bf9492 6823 pdi = first_die;
91c24f0a 6824
72bf9492
DJ
6825 while (pdi != NULL)
6826 {
6827 fixup_partial_die (pdi, cu);
c906108c 6828
f55ee35c 6829 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6830 children, so we need to look at them. Ditto for anonymous
6831 enums. */
933c6fe4 6832
72bf9492 6833 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6834 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6835 || pdi->tag == DW_TAG_imported_unit)
c906108c 6836 {
72bf9492 6837 switch (pdi->tag)
c906108c
SS
6838 {
6839 case DW_TAG_subprogram:
cdc07690 6840 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6841 break;
72929c62 6842 case DW_TAG_constant:
c906108c
SS
6843 case DW_TAG_variable:
6844 case DW_TAG_typedef:
91c24f0a 6845 case DW_TAG_union_type:
72bf9492 6846 if (!pdi->is_declaration)
63d06c5c 6847 {
72bf9492 6848 add_partial_symbol (pdi, cu);
63d06c5c
DC
6849 }
6850 break;
c906108c 6851 case DW_TAG_class_type:
680b30c7 6852 case DW_TAG_interface_type:
c906108c 6853 case DW_TAG_structure_type:
72bf9492 6854 if (!pdi->is_declaration)
c906108c 6855 {
72bf9492 6856 add_partial_symbol (pdi, cu);
c906108c 6857 }
e98c9e7c
TT
6858 if (cu->language == language_rust && pdi->has_children)
6859 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6860 set_addrmap, cu);
c906108c 6861 break;
91c24f0a 6862 case DW_TAG_enumeration_type:
72bf9492
DJ
6863 if (!pdi->is_declaration)
6864 add_partial_enumeration (pdi, cu);
c906108c
SS
6865 break;
6866 case DW_TAG_base_type:
a02abb62 6867 case DW_TAG_subrange_type:
c906108c 6868 /* File scope base type definitions are added to the partial
c5aa993b 6869 symbol table. */
72bf9492 6870 add_partial_symbol (pdi, cu);
c906108c 6871 break;
d9fa45fe 6872 case DW_TAG_namespace:
cdc07690 6873 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6874 break;
5d7cb8df 6875 case DW_TAG_module:
cdc07690 6876 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6877 break;
95554aad
TT
6878 case DW_TAG_imported_unit:
6879 {
6880 struct dwarf2_per_cu_data *per_cu;
6881
f4dc4d17
DE
6882 /* For now we don't handle imported units in type units. */
6883 if (cu->per_cu->is_debug_types)
6884 {
6885 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6886 " supported in type units [in module %s]"),
4262abfb 6887 objfile_name (cu->objfile));
f4dc4d17
DE
6888 }
6889
9c541725 6890 per_cu = dwarf2_find_containing_comp_unit (pdi->d.sect_off,
36586728 6891 pdi->is_dwz,
95554aad
TT
6892 cu->objfile);
6893
6894 /* Go read the partial unit, if needed. */
6895 if (per_cu->v.psymtab == NULL)
b93601f3 6896 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6897
f4dc4d17 6898 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6899 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6900 }
6901 break;
74921315
KS
6902 case DW_TAG_imported_declaration:
6903 add_partial_symbol (pdi, cu);
6904 break;
c906108c
SS
6905 default:
6906 break;
6907 }
6908 }
6909
72bf9492
DJ
6910 /* If the die has a sibling, skip to the sibling. */
6911
6912 pdi = pdi->die_sibling;
6913 }
6914}
6915
6916/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6917
72bf9492 6918 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6919 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
6920 Enumerators are an exception; they use the scope of their parent
6921 enumeration type, i.e. the name of the enumeration type is not
6922 prepended to the enumerator.
91c24f0a 6923
72bf9492
DJ
6924 There are two complexities. One is DW_AT_specification; in this
6925 case "parent" means the parent of the target of the specification,
6926 instead of the direct parent of the DIE. The other is compilers
6927 which do not emit DW_TAG_namespace; in this case we try to guess
6928 the fully qualified name of structure types from their members'
6929 linkage names. This must be done using the DIE's children rather
6930 than the children of any DW_AT_specification target. We only need
6931 to do this for structures at the top level, i.e. if the target of
6932 any DW_AT_specification (if any; otherwise the DIE itself) does not
6933 have a parent. */
6934
6935/* Compute the scope prefix associated with PDI's parent, in
6936 compilation unit CU. The result will be allocated on CU's
6937 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6938 field. NULL is returned if no prefix is necessary. */
15d034d0 6939static const char *
72bf9492
DJ
6940partial_die_parent_scope (struct partial_die_info *pdi,
6941 struct dwarf2_cu *cu)
6942{
15d034d0 6943 const char *grandparent_scope;
72bf9492 6944 struct partial_die_info *parent, *real_pdi;
91c24f0a 6945
72bf9492
DJ
6946 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6947 then this means the parent of the specification DIE. */
6948
6949 real_pdi = pdi;
72bf9492 6950 while (real_pdi->has_specification)
36586728
TT
6951 real_pdi = find_partial_die (real_pdi->spec_offset,
6952 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6953
6954 parent = real_pdi->die_parent;
6955 if (parent == NULL)
6956 return NULL;
6957
6958 if (parent->scope_set)
6959 return parent->scope;
6960
6961 fixup_partial_die (parent, cu);
6962
10b3939b 6963 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6964
acebe513
UW
6965 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6966 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6967 Work around this problem here. */
6968 if (cu->language == language_cplus
6e70227d 6969 && parent->tag == DW_TAG_namespace
acebe513
UW
6970 && strcmp (parent->name, "::") == 0
6971 && grandparent_scope == NULL)
6972 {
6973 parent->scope = NULL;
6974 parent->scope_set = 1;
6975 return NULL;
6976 }
6977
9c6c53f7
SA
6978 if (pdi->tag == DW_TAG_enumerator)
6979 /* Enumerators should not get the name of the enumeration as a prefix. */
6980 parent->scope = grandparent_scope;
6981 else if (parent->tag == DW_TAG_namespace
f55ee35c 6982 || parent->tag == DW_TAG_module
72bf9492
DJ
6983 || parent->tag == DW_TAG_structure_type
6984 || parent->tag == DW_TAG_class_type
680b30c7 6985 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6986 || parent->tag == DW_TAG_union_type
6987 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6988 {
6989 if (grandparent_scope == NULL)
6990 parent->scope = parent->name;
6991 else
3e43a32a
MS
6992 parent->scope = typename_concat (&cu->comp_unit_obstack,
6993 grandparent_scope,
f55ee35c 6994 parent->name, 0, cu);
72bf9492 6995 }
72bf9492
DJ
6996 else
6997 {
6998 /* FIXME drow/2004-04-01: What should we be doing with
6999 function-local names? For partial symbols, we should probably be
7000 ignoring them. */
7001 complaint (&symfile_complaints,
e2e0b3e5 7002 _("unhandled containing DIE tag %d for DIE at %d"),
9c541725 7003 parent->tag, to_underlying (pdi->sect_off));
72bf9492 7004 parent->scope = grandparent_scope;
c906108c
SS
7005 }
7006
72bf9492
DJ
7007 parent->scope_set = 1;
7008 return parent->scope;
7009}
7010
7011/* Return the fully scoped name associated with PDI, from compilation unit
7012 CU. The result will be allocated with malloc. */
4568ecf9 7013
72bf9492
DJ
7014static char *
7015partial_die_full_name (struct partial_die_info *pdi,
7016 struct dwarf2_cu *cu)
7017{
15d034d0 7018 const char *parent_scope;
72bf9492 7019
98bfdba5
PA
7020 /* If this is a template instantiation, we can not work out the
7021 template arguments from partial DIEs. So, unfortunately, we have
7022 to go through the full DIEs. At least any work we do building
7023 types here will be reused if full symbols are loaded later. */
7024 if (pdi->has_template_arguments)
7025 {
7026 fixup_partial_die (pdi, cu);
7027
7028 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7029 {
7030 struct die_info *die;
7031 struct attribute attr;
7032 struct dwarf2_cu *ref_cu = cu;
7033
b64f50a1 7034 /* DW_FORM_ref_addr is using section offset. */
b4069958 7035 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7036 attr.form = DW_FORM_ref_addr;
9c541725 7037 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7038 die = follow_die_ref (NULL, &attr, &ref_cu);
7039
7040 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7041 }
7042 }
7043
72bf9492
DJ
7044 parent_scope = partial_die_parent_scope (pdi, cu);
7045 if (parent_scope == NULL)
7046 return NULL;
7047 else
f55ee35c 7048 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
7049}
7050
7051static void
72bf9492 7052add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7053{
e7c27a73 7054 struct objfile *objfile = cu->objfile;
3e29f34a 7055 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 7056 CORE_ADDR addr = 0;
15d034d0 7057 const char *actual_name = NULL;
e142c38c 7058 CORE_ADDR baseaddr;
15d034d0 7059 char *built_actual_name;
e142c38c
DJ
7060
7061 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7062
15d034d0
TT
7063 built_actual_name = partial_die_full_name (pdi, cu);
7064 if (built_actual_name != NULL)
7065 actual_name = built_actual_name;
63d06c5c 7066
72bf9492
DJ
7067 if (actual_name == NULL)
7068 actual_name = pdi->name;
7069
c906108c
SS
7070 switch (pdi->tag)
7071 {
7072 case DW_TAG_subprogram:
3e29f34a 7073 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7074 if (pdi->is_external || cu->language == language_ada)
c906108c 7075 {
2cfa0c8d
JB
7076 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7077 of the global scope. But in Ada, we want to be able to access
7078 nested procedures globally. So all Ada subprograms are stored
7079 in the global scope. */
f47fb265 7080 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7081 built_actual_name != NULL,
f47fb265
MS
7082 VAR_DOMAIN, LOC_BLOCK,
7083 &objfile->global_psymbols,
1762568f 7084 addr, cu->language, objfile);
c906108c
SS
7085 }
7086 else
7087 {
f47fb265 7088 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7089 built_actual_name != NULL,
f47fb265
MS
7090 VAR_DOMAIN, LOC_BLOCK,
7091 &objfile->static_psymbols,
1762568f 7092 addr, cu->language, objfile);
c906108c 7093 }
0c1b455e
TT
7094
7095 if (pdi->main_subprogram && actual_name != NULL)
7096 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7097 break;
72929c62
JB
7098 case DW_TAG_constant:
7099 {
7100 struct psymbol_allocation_list *list;
7101
7102 if (pdi->is_external)
7103 list = &objfile->global_psymbols;
7104 else
7105 list = &objfile->static_psymbols;
f47fb265 7106 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7107 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7108 list, 0, cu->language, objfile);
72929c62
JB
7109 }
7110 break;
c906108c 7111 case DW_TAG_variable:
95554aad
TT
7112 if (pdi->d.locdesc)
7113 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7114
95554aad 7115 if (pdi->d.locdesc
caac4577
JG
7116 && addr == 0
7117 && !dwarf2_per_objfile->has_section_at_zero)
7118 {
7119 /* A global or static variable may also have been stripped
7120 out by the linker if unused, in which case its address
7121 will be nullified; do not add such variables into partial
7122 symbol table then. */
7123 }
7124 else if (pdi->is_external)
c906108c
SS
7125 {
7126 /* Global Variable.
7127 Don't enter into the minimal symbol tables as there is
7128 a minimal symbol table entry from the ELF symbols already.
7129 Enter into partial symbol table if it has a location
7130 descriptor or a type.
7131 If the location descriptor is missing, new_symbol will create
7132 a LOC_UNRESOLVED symbol, the address of the variable will then
7133 be determined from the minimal symbol table whenever the variable
7134 is referenced.
7135 The address for the partial symbol table entry is not
7136 used by GDB, but it comes in handy for debugging partial symbol
7137 table building. */
7138
95554aad 7139 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7140 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7141 built_actual_name != NULL,
f47fb265
MS
7142 VAR_DOMAIN, LOC_STATIC,
7143 &objfile->global_psymbols,
1762568f 7144 addr + baseaddr,
f47fb265 7145 cu->language, objfile);
c906108c
SS
7146 }
7147 else
7148 {
ff908ebf
AW
7149 int has_loc = pdi->d.locdesc != NULL;
7150
7151 /* Static Variable. Skip symbols whose value we cannot know (those
7152 without location descriptors or constant values). */
7153 if (!has_loc && !pdi->has_const_value)
decbce07 7154 {
15d034d0 7155 xfree (built_actual_name);
decbce07
MS
7156 return;
7157 }
ff908ebf 7158
f47fb265 7159 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7160 built_actual_name != NULL,
f47fb265
MS
7161 VAR_DOMAIN, LOC_STATIC,
7162 &objfile->static_psymbols,
ff908ebf 7163 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7164 cu->language, objfile);
c906108c
SS
7165 }
7166 break;
7167 case DW_TAG_typedef:
7168 case DW_TAG_base_type:
a02abb62 7169 case DW_TAG_subrange_type:
38d518c9 7170 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7171 built_actual_name != NULL,
176620f1 7172 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7173 &objfile->static_psymbols,
1762568f 7174 0, cu->language, objfile);
c906108c 7175 break;
74921315 7176 case DW_TAG_imported_declaration:
72bf9492
DJ
7177 case DW_TAG_namespace:
7178 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7179 built_actual_name != NULL,
72bf9492
DJ
7180 VAR_DOMAIN, LOC_TYPEDEF,
7181 &objfile->global_psymbols,
1762568f 7182 0, cu->language, objfile);
72bf9492 7183 break;
530e8392
KB
7184 case DW_TAG_module:
7185 add_psymbol_to_list (actual_name, strlen (actual_name),
7186 built_actual_name != NULL,
7187 MODULE_DOMAIN, LOC_TYPEDEF,
7188 &objfile->global_psymbols,
1762568f 7189 0, cu->language, objfile);
530e8392 7190 break;
c906108c 7191 case DW_TAG_class_type:
680b30c7 7192 case DW_TAG_interface_type:
c906108c
SS
7193 case DW_TAG_structure_type:
7194 case DW_TAG_union_type:
7195 case DW_TAG_enumeration_type:
fa4028e9
JB
7196 /* Skip external references. The DWARF standard says in the section
7197 about "Structure, Union, and Class Type Entries": "An incomplete
7198 structure, union or class type is represented by a structure,
7199 union or class entry that does not have a byte size attribute
7200 and that has a DW_AT_declaration attribute." */
7201 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7202 {
15d034d0 7203 xfree (built_actual_name);
decbce07
MS
7204 return;
7205 }
fa4028e9 7206
63d06c5c
DC
7207 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7208 static vs. global. */
38d518c9 7209 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7210 built_actual_name != NULL,
176620f1 7211 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7212 cu->language == language_cplus
63d06c5c
DC
7213 ? &objfile->global_psymbols
7214 : &objfile->static_psymbols,
1762568f 7215 0, cu->language, objfile);
c906108c 7216
c906108c
SS
7217 break;
7218 case DW_TAG_enumerator:
38d518c9 7219 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7220 built_actual_name != NULL,
176620f1 7221 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7222 cu->language == language_cplus
f6fe98ef
DJ
7223 ? &objfile->global_psymbols
7224 : &objfile->static_psymbols,
1762568f 7225 0, cu->language, objfile);
c906108c
SS
7226 break;
7227 default:
7228 break;
7229 }
5c4e30ca 7230
15d034d0 7231 xfree (built_actual_name);
c906108c
SS
7232}
7233
5c4e30ca
DC
7234/* Read a partial die corresponding to a namespace; also, add a symbol
7235 corresponding to that namespace to the symbol table. NAMESPACE is
7236 the name of the enclosing namespace. */
91c24f0a 7237
72bf9492
DJ
7238static void
7239add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7240 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7241 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7242{
72bf9492 7243 /* Add a symbol for the namespace. */
e7c27a73 7244
72bf9492 7245 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7246
7247 /* Now scan partial symbols in that namespace. */
7248
91c24f0a 7249 if (pdi->has_children)
cdc07690 7250 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7251}
7252
5d7cb8df
JK
7253/* Read a partial die corresponding to a Fortran module. */
7254
7255static void
7256add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7257 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7258{
530e8392
KB
7259 /* Add a symbol for the namespace. */
7260
7261 add_partial_symbol (pdi, cu);
7262
f55ee35c 7263 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7264
7265 if (pdi->has_children)
cdc07690 7266 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7267}
7268
bc30ff58
JB
7269/* Read a partial die corresponding to a subprogram and create a partial
7270 symbol for that subprogram. When the CU language allows it, this
7271 routine also defines a partial symbol for each nested subprogram
cdc07690 7272 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7273 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7274 and highest PC values found in PDI.
6e70227d 7275
cdc07690
YQ
7276 PDI may also be a lexical block, in which case we simply search
7277 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7278 Again, this is only performed when the CU language allows this
7279 type of definitions. */
7280
7281static void
7282add_partial_subprogram (struct partial_die_info *pdi,
7283 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7284 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7285{
7286 if (pdi->tag == DW_TAG_subprogram)
7287 {
7288 if (pdi->has_pc_info)
7289 {
7290 if (pdi->lowpc < *lowpc)
7291 *lowpc = pdi->lowpc;
7292 if (pdi->highpc > *highpc)
7293 *highpc = pdi->highpc;
cdc07690 7294 if (set_addrmap)
5734ee8b 7295 {
5734ee8b 7296 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7297 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7298 CORE_ADDR baseaddr;
7299 CORE_ADDR highpc;
7300 CORE_ADDR lowpc;
5734ee8b
DJ
7301
7302 baseaddr = ANOFFSET (objfile->section_offsets,
7303 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7304 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7305 pdi->lowpc + baseaddr);
7306 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7307 pdi->highpc + baseaddr);
7308 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7309 cu->per_cu->v.psymtab);
5734ee8b 7310 }
481860b3
GB
7311 }
7312
7313 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7314 {
bc30ff58 7315 if (!pdi->is_declaration)
e8d05480
JB
7316 /* Ignore subprogram DIEs that do not have a name, they are
7317 illegal. Do not emit a complaint at this point, we will
7318 do so when we convert this psymtab into a symtab. */
7319 if (pdi->name)
7320 add_partial_symbol (pdi, cu);
bc30ff58
JB
7321 }
7322 }
6e70227d 7323
bc30ff58
JB
7324 if (! pdi->has_children)
7325 return;
7326
7327 if (cu->language == language_ada)
7328 {
7329 pdi = pdi->die_child;
7330 while (pdi != NULL)
7331 {
7332 fixup_partial_die (pdi, cu);
7333 if (pdi->tag == DW_TAG_subprogram
7334 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7335 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7336 pdi = pdi->die_sibling;
7337 }
7338 }
7339}
7340
91c24f0a
DC
7341/* Read a partial die corresponding to an enumeration type. */
7342
72bf9492
DJ
7343static void
7344add_partial_enumeration (struct partial_die_info *enum_pdi,
7345 struct dwarf2_cu *cu)
91c24f0a 7346{
72bf9492 7347 struct partial_die_info *pdi;
91c24f0a
DC
7348
7349 if (enum_pdi->name != NULL)
72bf9492
DJ
7350 add_partial_symbol (enum_pdi, cu);
7351
7352 pdi = enum_pdi->die_child;
7353 while (pdi)
91c24f0a 7354 {
72bf9492 7355 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7356 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7357 else
72bf9492
DJ
7358 add_partial_symbol (pdi, cu);
7359 pdi = pdi->die_sibling;
91c24f0a 7360 }
91c24f0a
DC
7361}
7362
6caca83c
CC
7363/* Return the initial uleb128 in the die at INFO_PTR. */
7364
7365static unsigned int
d521ce57 7366peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7367{
7368 unsigned int bytes_read;
7369
7370 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7371}
7372
4bb7a0a7
DJ
7373/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7374 Return the corresponding abbrev, or NULL if the number is zero (indicating
7375 an empty DIE). In either case *BYTES_READ will be set to the length of
7376 the initial number. */
7377
7378static struct abbrev_info *
d521ce57 7379peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7380 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7381{
7382 bfd *abfd = cu->objfile->obfd;
7383 unsigned int abbrev_number;
7384 struct abbrev_info *abbrev;
7385
7386 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7387
7388 if (abbrev_number == 0)
7389 return NULL;
7390
433df2d4 7391 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7392 if (!abbrev)
7393 {
422b9917
DE
7394 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7395 " at offset 0x%x [in module %s]"),
7396 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9c541725 7397 to_underlying (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
7398 }
7399
7400 return abbrev;
7401}
7402
93311388
DE
7403/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7404 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7405 DIE. Any children of the skipped DIEs will also be skipped. */
7406
d521ce57
TT
7407static const gdb_byte *
7408skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7409{
dee91e82 7410 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7411 struct abbrev_info *abbrev;
7412 unsigned int bytes_read;
7413
7414 while (1)
7415 {
7416 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7417 if (abbrev == NULL)
7418 return info_ptr + bytes_read;
7419 else
dee91e82 7420 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7421 }
7422}
7423
93311388
DE
7424/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7425 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7426 abbrev corresponding to that skipped uleb128 should be passed in
7427 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7428 children. */
7429
d521ce57
TT
7430static const gdb_byte *
7431skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7432 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7433{
7434 unsigned int bytes_read;
7435 struct attribute attr;
dee91e82
DE
7436 bfd *abfd = reader->abfd;
7437 struct dwarf2_cu *cu = reader->cu;
d521ce57 7438 const gdb_byte *buffer = reader->buffer;
f664829e 7439 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7440 unsigned int form, i;
7441
7442 for (i = 0; i < abbrev->num_attrs; i++)
7443 {
7444 /* The only abbrev we care about is DW_AT_sibling. */
7445 if (abbrev->attrs[i].name == DW_AT_sibling)
7446 {
dee91e82 7447 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7448 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7449 complaint (&symfile_complaints,
7450 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7451 else
b9502d3f 7452 {
9c541725
PA
7453 sect_offset off = dwarf2_get_ref_die_offset (&attr);
7454 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
7455
7456 if (sibling_ptr < info_ptr)
7457 complaint (&symfile_complaints,
7458 _("DW_AT_sibling points backwards"));
22869d73
KS
7459 else if (sibling_ptr > reader->buffer_end)
7460 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7461 else
7462 return sibling_ptr;
7463 }
4bb7a0a7
DJ
7464 }
7465
7466 /* If it isn't DW_AT_sibling, skip this attribute. */
7467 form = abbrev->attrs[i].form;
7468 skip_attribute:
7469 switch (form)
7470 {
4bb7a0a7 7471 case DW_FORM_ref_addr:
ae411497
TT
7472 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7473 and later it is offset sized. */
7474 if (cu->header.version == 2)
7475 info_ptr += cu->header.addr_size;
7476 else
7477 info_ptr += cu->header.offset_size;
7478 break;
36586728
TT
7479 case DW_FORM_GNU_ref_alt:
7480 info_ptr += cu->header.offset_size;
7481 break;
ae411497 7482 case DW_FORM_addr:
4bb7a0a7
DJ
7483 info_ptr += cu->header.addr_size;
7484 break;
7485 case DW_FORM_data1:
7486 case DW_FORM_ref1:
7487 case DW_FORM_flag:
7488 info_ptr += 1;
7489 break;
2dc7f7b3 7490 case DW_FORM_flag_present:
43988095 7491 case DW_FORM_implicit_const:
2dc7f7b3 7492 break;
4bb7a0a7
DJ
7493 case DW_FORM_data2:
7494 case DW_FORM_ref2:
7495 info_ptr += 2;
7496 break;
7497 case DW_FORM_data4:
7498 case DW_FORM_ref4:
7499 info_ptr += 4;
7500 break;
7501 case DW_FORM_data8:
7502 case DW_FORM_ref8:
55f1336d 7503 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7504 info_ptr += 8;
7505 break;
0224619f
JK
7506 case DW_FORM_data16:
7507 info_ptr += 16;
7508 break;
4bb7a0a7 7509 case DW_FORM_string:
9b1c24c8 7510 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7511 info_ptr += bytes_read;
7512 break;
2dc7f7b3 7513 case DW_FORM_sec_offset:
4bb7a0a7 7514 case DW_FORM_strp:
36586728 7515 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7516 info_ptr += cu->header.offset_size;
7517 break;
2dc7f7b3 7518 case DW_FORM_exprloc:
4bb7a0a7
DJ
7519 case DW_FORM_block:
7520 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7521 info_ptr += bytes_read;
7522 break;
7523 case DW_FORM_block1:
7524 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7525 break;
7526 case DW_FORM_block2:
7527 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7528 break;
7529 case DW_FORM_block4:
7530 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7531 break;
7532 case DW_FORM_sdata:
7533 case DW_FORM_udata:
7534 case DW_FORM_ref_udata:
3019eac3
DE
7535 case DW_FORM_GNU_addr_index:
7536 case DW_FORM_GNU_str_index:
d521ce57 7537 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7538 break;
7539 case DW_FORM_indirect:
7540 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7541 info_ptr += bytes_read;
7542 /* We need to continue parsing from here, so just go back to
7543 the top. */
7544 goto skip_attribute;
7545
7546 default:
3e43a32a
MS
7547 error (_("Dwarf Error: Cannot handle %s "
7548 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7549 dwarf_form_name (form),
7550 bfd_get_filename (abfd));
7551 }
7552 }
7553
7554 if (abbrev->has_children)
dee91e82 7555 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7556 else
7557 return info_ptr;
7558}
7559
93311388 7560/* Locate ORIG_PDI's sibling.
dee91e82 7561 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7562
d521ce57 7563static const gdb_byte *
dee91e82
DE
7564locate_pdi_sibling (const struct die_reader_specs *reader,
7565 struct partial_die_info *orig_pdi,
d521ce57 7566 const gdb_byte *info_ptr)
91c24f0a
DC
7567{
7568 /* Do we know the sibling already? */
72bf9492 7569
91c24f0a
DC
7570 if (orig_pdi->sibling)
7571 return orig_pdi->sibling;
7572
7573 /* Are there any children to deal with? */
7574
7575 if (!orig_pdi->has_children)
7576 return info_ptr;
7577
4bb7a0a7 7578 /* Skip the children the long way. */
91c24f0a 7579
dee91e82 7580 return skip_children (reader, info_ptr);
91c24f0a
DC
7581}
7582
257e7a09 7583/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7584 not NULL. */
c906108c
SS
7585
7586static void
257e7a09
YQ
7587dwarf2_read_symtab (struct partial_symtab *self,
7588 struct objfile *objfile)
c906108c 7589{
257e7a09 7590 if (self->readin)
c906108c 7591 {
442e4d9c 7592 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7593 self->filename);
442e4d9c
YQ
7594 }
7595 else
7596 {
7597 if (info_verbose)
c906108c 7598 {
442e4d9c 7599 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7600 self->filename);
442e4d9c 7601 gdb_flush (gdb_stdout);
c906108c 7602 }
c906108c 7603
442e4d9c 7604 /* Restore our global data. */
9a3c8263
SM
7605 dwarf2_per_objfile
7606 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7607 dwarf2_objfile_data_key);
10b3939b 7608
442e4d9c
YQ
7609 /* If this psymtab is constructed from a debug-only objfile, the
7610 has_section_at_zero flag will not necessarily be correct. We
7611 can get the correct value for this flag by looking at the data
7612 associated with the (presumably stripped) associated objfile. */
7613 if (objfile->separate_debug_objfile_backlink)
7614 {
7615 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7616 = ((struct dwarf2_per_objfile *)
7617 objfile_data (objfile->separate_debug_objfile_backlink,
7618 dwarf2_objfile_data_key));
9a619af0 7619
442e4d9c
YQ
7620 dwarf2_per_objfile->has_section_at_zero
7621 = dpo_backlink->has_section_at_zero;
7622 }
b2ab525c 7623
442e4d9c 7624 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7625
257e7a09 7626 psymtab_to_symtab_1 (self);
c906108c 7627
442e4d9c
YQ
7628 /* Finish up the debug error message. */
7629 if (info_verbose)
7630 printf_filtered (_("done.\n"));
c906108c 7631 }
95554aad
TT
7632
7633 process_cu_includes ();
c906108c 7634}
9cdd5dbd
DE
7635\f
7636/* Reading in full CUs. */
c906108c 7637
10b3939b
DJ
7638/* Add PER_CU to the queue. */
7639
7640static void
95554aad
TT
7641queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7642 enum language pretend_language)
10b3939b
DJ
7643{
7644 struct dwarf2_queue_item *item;
7645
7646 per_cu->queued = 1;
8d749320 7647 item = XNEW (struct dwarf2_queue_item);
10b3939b 7648 item->per_cu = per_cu;
95554aad 7649 item->pretend_language = pretend_language;
10b3939b
DJ
7650 item->next = NULL;
7651
7652 if (dwarf2_queue == NULL)
7653 dwarf2_queue = item;
7654 else
7655 dwarf2_queue_tail->next = item;
7656
7657 dwarf2_queue_tail = item;
7658}
7659
89e63ee4
DE
7660/* If PER_CU is not yet queued, add it to the queue.
7661 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7662 dependency.
0907af0c 7663 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7664 meaning either PER_CU is already queued or it is already loaded.
7665
7666 N.B. There is an invariant here that if a CU is queued then it is loaded.
7667 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7668
7669static int
89e63ee4 7670maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7671 struct dwarf2_per_cu_data *per_cu,
7672 enum language pretend_language)
7673{
7674 /* We may arrive here during partial symbol reading, if we need full
7675 DIEs to process an unusual case (e.g. template arguments). Do
7676 not queue PER_CU, just tell our caller to load its DIEs. */
7677 if (dwarf2_per_objfile->reading_partial_symbols)
7678 {
7679 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7680 return 1;
7681 return 0;
7682 }
7683
7684 /* Mark the dependence relation so that we don't flush PER_CU
7685 too early. */
89e63ee4
DE
7686 if (dependent_cu != NULL)
7687 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7688
7689 /* If it's already on the queue, we have nothing to do. */
7690 if (per_cu->queued)
7691 return 0;
7692
7693 /* If the compilation unit is already loaded, just mark it as
7694 used. */
7695 if (per_cu->cu != NULL)
7696 {
7697 per_cu->cu->last_used = 0;
7698 return 0;
7699 }
7700
7701 /* Add it to the queue. */
7702 queue_comp_unit (per_cu, pretend_language);
7703
7704 return 1;
7705}
7706
10b3939b
DJ
7707/* Process the queue. */
7708
7709static void
a0f42c21 7710process_queue (void)
10b3939b
DJ
7711{
7712 struct dwarf2_queue_item *item, *next_item;
7713
b4f54984 7714 if (dwarf_read_debug)
45cfd468
DE
7715 {
7716 fprintf_unfiltered (gdb_stdlog,
7717 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7718 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7719 }
7720
03dd20cc
DJ
7721 /* The queue starts out with one item, but following a DIE reference
7722 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7723 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7724 {
cc12ce38
DE
7725 if ((dwarf2_per_objfile->using_index
7726 ? !item->per_cu->v.quick->compunit_symtab
7727 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7728 /* Skip dummy CUs. */
7729 && item->per_cu->cu != NULL)
f4dc4d17
DE
7730 {
7731 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7732 unsigned int debug_print_threshold;
247f5c4f 7733 char buf[100];
f4dc4d17 7734
247f5c4f 7735 if (per_cu->is_debug_types)
f4dc4d17 7736 {
247f5c4f
DE
7737 struct signatured_type *sig_type =
7738 (struct signatured_type *) per_cu;
7739
7740 sprintf (buf, "TU %s at offset 0x%x",
73be47f5 7741 hex_string (sig_type->signature),
9c541725 7742 to_underlying (per_cu->sect_off));
73be47f5
DE
7743 /* There can be 100s of TUs.
7744 Only print them in verbose mode. */
7745 debug_print_threshold = 2;
f4dc4d17 7746 }
247f5c4f 7747 else
73be47f5 7748 {
9c541725
PA
7749 sprintf (buf, "CU at offset 0x%x",
7750 to_underlying (per_cu->sect_off));
73be47f5
DE
7751 debug_print_threshold = 1;
7752 }
247f5c4f 7753
b4f54984 7754 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7755 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7756
7757 if (per_cu->is_debug_types)
7758 process_full_type_unit (per_cu, item->pretend_language);
7759 else
7760 process_full_comp_unit (per_cu, item->pretend_language);
7761
b4f54984 7762 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7763 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7764 }
10b3939b
DJ
7765
7766 item->per_cu->queued = 0;
7767 next_item = item->next;
7768 xfree (item);
7769 }
7770
7771 dwarf2_queue_tail = NULL;
45cfd468 7772
b4f54984 7773 if (dwarf_read_debug)
45cfd468
DE
7774 {
7775 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7776 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7777 }
10b3939b
DJ
7778}
7779
7780/* Free all allocated queue entries. This function only releases anything if
7781 an error was thrown; if the queue was processed then it would have been
7782 freed as we went along. */
7783
7784static void
7785dwarf2_release_queue (void *dummy)
7786{
7787 struct dwarf2_queue_item *item, *last;
7788
7789 item = dwarf2_queue;
7790 while (item)
7791 {
7792 /* Anything still marked queued is likely to be in an
7793 inconsistent state, so discard it. */
7794 if (item->per_cu->queued)
7795 {
7796 if (item->per_cu->cu != NULL)
dee91e82 7797 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7798 item->per_cu->queued = 0;
7799 }
7800
7801 last = item;
7802 item = item->next;
7803 xfree (last);
7804 }
7805
7806 dwarf2_queue = dwarf2_queue_tail = NULL;
7807}
7808
7809/* Read in full symbols for PST, and anything it depends on. */
7810
c906108c 7811static void
fba45db2 7812psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7813{
10b3939b 7814 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7815 int i;
7816
95554aad
TT
7817 if (pst->readin)
7818 return;
7819
aaa75496 7820 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7821 if (!pst->dependencies[i]->readin
7822 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7823 {
7824 /* Inform about additional files that need to be read in. */
7825 if (info_verbose)
7826 {
a3f17187 7827 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7828 fputs_filtered (" ", gdb_stdout);
7829 wrap_here ("");
7830 fputs_filtered ("and ", gdb_stdout);
7831 wrap_here ("");
7832 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7833 wrap_here (""); /* Flush output. */
aaa75496
JB
7834 gdb_flush (gdb_stdout);
7835 }
7836 psymtab_to_symtab_1 (pst->dependencies[i]);
7837 }
7838
9a3c8263 7839 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7840
7841 if (per_cu == NULL)
aaa75496
JB
7842 {
7843 /* It's an include file, no symbols to read for it.
7844 Everything is in the parent symtab. */
7845 pst->readin = 1;
7846 return;
7847 }
c906108c 7848
a0f42c21 7849 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7850}
7851
dee91e82
DE
7852/* Trivial hash function for die_info: the hash value of a DIE
7853 is its offset in .debug_info for this objfile. */
10b3939b 7854
dee91e82
DE
7855static hashval_t
7856die_hash (const void *item)
10b3939b 7857{
9a3c8263 7858 const struct die_info *die = (const struct die_info *) item;
6502dd73 7859
9c541725 7860 return to_underlying (die->sect_off);
dee91e82 7861}
63d06c5c 7862
dee91e82
DE
7863/* Trivial comparison function for die_info structures: two DIEs
7864 are equal if they have the same offset. */
98bfdba5 7865
dee91e82
DE
7866static int
7867die_eq (const void *item_lhs, const void *item_rhs)
7868{
9a3c8263
SM
7869 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7870 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7871
9c541725 7872 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 7873}
c906108c 7874
dee91e82
DE
7875/* die_reader_func for load_full_comp_unit.
7876 This is identical to read_signatured_type_reader,
7877 but is kept separate for now. */
c906108c 7878
dee91e82
DE
7879static void
7880load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7881 const gdb_byte *info_ptr,
dee91e82
DE
7882 struct die_info *comp_unit_die,
7883 int has_children,
7884 void *data)
7885{
7886 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7887 enum language *language_ptr = (enum language *) data;
6caca83c 7888
dee91e82
DE
7889 gdb_assert (cu->die_hash == NULL);
7890 cu->die_hash =
7891 htab_create_alloc_ex (cu->header.length / 12,
7892 die_hash,
7893 die_eq,
7894 NULL,
7895 &cu->comp_unit_obstack,
7896 hashtab_obstack_allocate,
7897 dummy_obstack_deallocate);
e142c38c 7898
dee91e82
DE
7899 if (has_children)
7900 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7901 &info_ptr, comp_unit_die);
7902 cu->dies = comp_unit_die;
7903 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7904
7905 /* We try not to read any attributes in this function, because not
9cdd5dbd 7906 all CUs needed for references have been loaded yet, and symbol
10b3939b 7907 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7908 or we won't be able to build types correctly.
7909 Similarly, if we do not read the producer, we can not apply
7910 producer-specific interpretation. */
95554aad 7911 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7912}
10b3939b 7913
dee91e82 7914/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7915
dee91e82 7916static void
95554aad
TT
7917load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7918 enum language pretend_language)
dee91e82 7919{
3019eac3 7920 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7921
f4dc4d17
DE
7922 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7923 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7924}
7925
3da10d80
KS
7926/* Add a DIE to the delayed physname list. */
7927
7928static void
7929add_to_method_list (struct type *type, int fnfield_index, int index,
7930 const char *name, struct die_info *die,
7931 struct dwarf2_cu *cu)
7932{
7933 struct delayed_method_info mi;
7934 mi.type = type;
7935 mi.fnfield_index = fnfield_index;
7936 mi.index = index;
7937 mi.name = name;
7938 mi.die = die;
7939 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7940}
7941
7942/* A cleanup for freeing the delayed method list. */
7943
7944static void
7945free_delayed_list (void *ptr)
7946{
7947 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7948 if (cu->method_list != NULL)
7949 {
7950 VEC_free (delayed_method_info, cu->method_list);
7951 cu->method_list = NULL;
7952 }
7953}
7954
7955/* Compute the physnames of any methods on the CU's method list.
7956
7957 The computation of method physnames is delayed in order to avoid the
7958 (bad) condition that one of the method's formal parameters is of an as yet
7959 incomplete type. */
7960
7961static void
7962compute_delayed_physnames (struct dwarf2_cu *cu)
7963{
7964 int i;
7965 struct delayed_method_info *mi;
7966 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7967 {
1d06ead6 7968 const char *physname;
3da10d80
KS
7969 struct fn_fieldlist *fn_flp
7970 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7971 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7972 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7973 = physname ? physname : "";
3da10d80
KS
7974 }
7975}
7976
a766d390
DE
7977/* Go objects should be embedded in a DW_TAG_module DIE,
7978 and it's not clear if/how imported objects will appear.
7979 To keep Go support simple until that's worked out,
7980 go back through what we've read and create something usable.
7981 We could do this while processing each DIE, and feels kinda cleaner,
7982 but that way is more invasive.
7983 This is to, for example, allow the user to type "p var" or "b main"
7984 without having to specify the package name, and allow lookups
7985 of module.object to work in contexts that use the expression
7986 parser. */
7987
7988static void
7989fixup_go_packaging (struct dwarf2_cu *cu)
7990{
7991 char *package_name = NULL;
7992 struct pending *list;
7993 int i;
7994
7995 for (list = global_symbols; list != NULL; list = list->next)
7996 {
7997 for (i = 0; i < list->nsyms; ++i)
7998 {
7999 struct symbol *sym = list->symbol[i];
8000
8001 if (SYMBOL_LANGUAGE (sym) == language_go
8002 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8003 {
8004 char *this_package_name = go_symbol_package_name (sym);
8005
8006 if (this_package_name == NULL)
8007 continue;
8008 if (package_name == NULL)
8009 package_name = this_package_name;
8010 else
8011 {
8012 if (strcmp (package_name, this_package_name) != 0)
8013 complaint (&symfile_complaints,
8014 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8015 (symbol_symtab (sym) != NULL
8016 ? symtab_to_filename_for_display
8017 (symbol_symtab (sym))
4262abfb 8018 : objfile_name (cu->objfile)),
a766d390
DE
8019 this_package_name, package_name);
8020 xfree (this_package_name);
8021 }
8022 }
8023 }
8024 }
8025
8026 if (package_name != NULL)
8027 {
8028 struct objfile *objfile = cu->objfile;
34a68019 8029 const char *saved_package_name
224c3ddb
SM
8030 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8031 package_name,
8032 strlen (package_name));
19f392bc
UW
8033 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8034 saved_package_name);
a766d390
DE
8035 struct symbol *sym;
8036
8037 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8038
e623cf5d 8039 sym = allocate_symbol (objfile);
f85f34ed 8040 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
8041 SYMBOL_SET_NAMES (sym, saved_package_name,
8042 strlen (saved_package_name), 0, objfile);
a766d390
DE
8043 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8044 e.g., "main" finds the "main" module and not C's main(). */
8045 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8046 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8047 SYMBOL_TYPE (sym) = type;
8048
8049 add_symbol_to_list (sym, &global_symbols);
8050
8051 xfree (package_name);
8052 }
8053}
8054
95554aad
TT
8055/* Return the symtab for PER_CU. This works properly regardless of
8056 whether we're using the index or psymtabs. */
8057
43f3e411
DE
8058static struct compunit_symtab *
8059get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
8060{
8061 return (dwarf2_per_objfile->using_index
43f3e411
DE
8062 ? per_cu->v.quick->compunit_symtab
8063 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
8064}
8065
8066/* A helper function for computing the list of all symbol tables
8067 included by PER_CU. */
8068
8069static void
43f3e411 8070recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 8071 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 8072 struct dwarf2_per_cu_data *per_cu,
43f3e411 8073 struct compunit_symtab *immediate_parent)
95554aad
TT
8074{
8075 void **slot;
8076 int ix;
43f3e411 8077 struct compunit_symtab *cust;
95554aad
TT
8078 struct dwarf2_per_cu_data *iter;
8079
8080 slot = htab_find_slot (all_children, per_cu, INSERT);
8081 if (*slot != NULL)
8082 {
8083 /* This inclusion and its children have been processed. */
8084 return;
8085 }
8086
8087 *slot = per_cu;
8088 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8089 cust = get_compunit_symtab (per_cu);
8090 if (cust != NULL)
ec94af83
DE
8091 {
8092 /* If this is a type unit only add its symbol table if we haven't
8093 seen it yet (type unit per_cu's can share symtabs). */
8094 if (per_cu->is_debug_types)
8095 {
43f3e411 8096 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8097 if (*slot == NULL)
8098 {
43f3e411
DE
8099 *slot = cust;
8100 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8101 if (cust->user == NULL)
8102 cust->user = immediate_parent;
ec94af83
DE
8103 }
8104 }
8105 else
f9125b6c 8106 {
43f3e411
DE
8107 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8108 if (cust->user == NULL)
8109 cust->user = immediate_parent;
f9125b6c 8110 }
ec94af83 8111 }
95554aad
TT
8112
8113 for (ix = 0;
796a7ff8 8114 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8115 ++ix)
ec94af83
DE
8116 {
8117 recursively_compute_inclusions (result, all_children,
43f3e411 8118 all_type_symtabs, iter, cust);
ec94af83 8119 }
95554aad
TT
8120}
8121
43f3e411 8122/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8123 PER_CU. */
8124
8125static void
43f3e411 8126compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8127{
f4dc4d17
DE
8128 gdb_assert (! per_cu->is_debug_types);
8129
796a7ff8 8130 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8131 {
8132 int ix, len;
ec94af83 8133 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8134 struct compunit_symtab *compunit_symtab_iter;
8135 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8136 htab_t all_children, all_type_symtabs;
43f3e411 8137 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8138
8139 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8140 if (cust == NULL)
95554aad
TT
8141 return;
8142
8143 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8144 NULL, xcalloc, xfree);
ec94af83
DE
8145 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8146 NULL, xcalloc, xfree);
95554aad
TT
8147
8148 for (ix = 0;
796a7ff8 8149 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8150 ix, per_cu_iter);
95554aad 8151 ++ix)
ec94af83
DE
8152 {
8153 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8154 all_type_symtabs, per_cu_iter,
43f3e411 8155 cust);
ec94af83 8156 }
95554aad 8157
ec94af83 8158 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8159 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8160 cust->includes
8d749320
SM
8161 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8162 struct compunit_symtab *, len + 1);
95554aad 8163 for (ix = 0;
43f3e411
DE
8164 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8165 compunit_symtab_iter);
95554aad 8166 ++ix)
43f3e411
DE
8167 cust->includes[ix] = compunit_symtab_iter;
8168 cust->includes[len] = NULL;
95554aad 8169
43f3e411 8170 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8171 htab_delete (all_children);
ec94af83 8172 htab_delete (all_type_symtabs);
95554aad
TT
8173 }
8174}
8175
8176/* Compute the 'includes' field for the symtabs of all the CUs we just
8177 read. */
8178
8179static void
8180process_cu_includes (void)
8181{
8182 int ix;
8183 struct dwarf2_per_cu_data *iter;
8184
8185 for (ix = 0;
8186 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8187 ix, iter);
8188 ++ix)
f4dc4d17
DE
8189 {
8190 if (! iter->is_debug_types)
43f3e411 8191 compute_compunit_symtab_includes (iter);
f4dc4d17 8192 }
95554aad
TT
8193
8194 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8195}
8196
9cdd5dbd 8197/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8198 already been loaded into memory. */
8199
8200static void
95554aad
TT
8201process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8202 enum language pretend_language)
10b3939b 8203{
10b3939b 8204 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8205 struct objfile *objfile = per_cu->objfile;
3e29f34a 8206 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8207 CORE_ADDR lowpc, highpc;
43f3e411 8208 struct compunit_symtab *cust;
3da10d80 8209 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8210 CORE_ADDR baseaddr;
4359dff1 8211 struct block *static_block;
3e29f34a 8212 CORE_ADDR addr;
10b3939b
DJ
8213
8214 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8215
10b3939b
DJ
8216 buildsym_init ();
8217 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8218 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8219
8220 cu->list_in_scope = &file_symbols;
c906108c 8221
95554aad
TT
8222 cu->language = pretend_language;
8223 cu->language_defn = language_def (cu->language);
8224
c906108c 8225 /* Do line number decoding in read_file_scope () */
10b3939b 8226 process_die (cu->dies, cu);
c906108c 8227
a766d390
DE
8228 /* For now fudge the Go package. */
8229 if (cu->language == language_go)
8230 fixup_go_packaging (cu);
8231
3da10d80
KS
8232 /* Now that we have processed all the DIEs in the CU, all the types
8233 should be complete, and it should now be safe to compute all of the
8234 physnames. */
8235 compute_delayed_physnames (cu);
8236 do_cleanups (delayed_list_cleanup);
8237
fae299cd
DC
8238 /* Some compilers don't define a DW_AT_high_pc attribute for the
8239 compilation unit. If the DW_AT_high_pc is missing, synthesize
8240 it, by scanning the DIE's below the compilation unit. */
10b3939b 8241 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8242
3e29f34a
MR
8243 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8244 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8245
8246 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8247 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8248 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8249 addrmap to help ensure it has an accurate map of pc values belonging to
8250 this comp unit. */
8251 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8252
43f3e411
DE
8253 cust = end_symtab_from_static_block (static_block,
8254 SECT_OFF_TEXT (objfile), 0);
c906108c 8255
43f3e411 8256 if (cust != NULL)
c906108c 8257 {
df15bd07 8258 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8259
8be455d7
JK
8260 /* Set symtab language to language from DW_AT_language. If the
8261 compilation is from a C file generated by language preprocessors, do
8262 not set the language if it was already deduced by start_subfile. */
43f3e411 8263 if (!(cu->language == language_c
40e3ad0e 8264 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8265 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8266
8267 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8268 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8269 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8270 there were bugs in prologue debug info, fixed later in GCC-4.5
8271 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8272
8273 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8274 needed, it would be wrong due to missing DW_AT_producer there.
8275
8276 Still one can confuse GDB by using non-standard GCC compilation
8277 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8278 */
ab260dad 8279 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8280 cust->locations_valid = 1;
e0d00bc7
JK
8281
8282 if (gcc_4_minor >= 5)
43f3e411 8283 cust->epilogue_unwind_valid = 1;
96408a79 8284
43f3e411 8285 cust->call_site_htab = cu->call_site_htab;
c906108c 8286 }
9291a0cd
TT
8287
8288 if (dwarf2_per_objfile->using_index)
43f3e411 8289 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8290 else
8291 {
8292 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8293 pst->compunit_symtab = cust;
9291a0cd
TT
8294 pst->readin = 1;
8295 }
c906108c 8296
95554aad
TT
8297 /* Push it for inclusion processing later. */
8298 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8299
c906108c 8300 do_cleanups (back_to);
f4dc4d17 8301}
45cfd468 8302
f4dc4d17
DE
8303/* Generate full symbol information for type unit PER_CU, whose DIEs have
8304 already been loaded into memory. */
8305
8306static void
8307process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8308 enum language pretend_language)
8309{
8310 struct dwarf2_cu *cu = per_cu->cu;
8311 struct objfile *objfile = per_cu->objfile;
43f3e411 8312 struct compunit_symtab *cust;
f4dc4d17 8313 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8314 struct signatured_type *sig_type;
8315
8316 gdb_assert (per_cu->is_debug_types);
8317 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8318
8319 buildsym_init ();
8320 back_to = make_cleanup (really_free_pendings, NULL);
8321 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8322
8323 cu->list_in_scope = &file_symbols;
8324
8325 cu->language = pretend_language;
8326 cu->language_defn = language_def (cu->language);
8327
8328 /* The symbol tables are set up in read_type_unit_scope. */
8329 process_die (cu->dies, cu);
8330
8331 /* For now fudge the Go package. */
8332 if (cu->language == language_go)
8333 fixup_go_packaging (cu);
8334
8335 /* Now that we have processed all the DIEs in the CU, all the types
8336 should be complete, and it should now be safe to compute all of the
8337 physnames. */
8338 compute_delayed_physnames (cu);
8339 do_cleanups (delayed_list_cleanup);
8340
8341 /* TUs share symbol tables.
8342 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8343 of it with end_expandable_symtab. Otherwise, complete the addition of
8344 this TU's symbols to the existing symtab. */
43f3e411 8345 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8346 {
43f3e411
DE
8347 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8348 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8349
43f3e411 8350 if (cust != NULL)
f4dc4d17
DE
8351 {
8352 /* Set symtab language to language from DW_AT_language. If the
8353 compilation is from a C file generated by language preprocessors,
8354 do not set the language if it was already deduced by
8355 start_subfile. */
43f3e411
DE
8356 if (!(cu->language == language_c
8357 && COMPUNIT_FILETABS (cust)->language != language_c))
8358 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8359 }
8360 }
8361 else
8362 {
0ab9ce85 8363 augment_type_symtab ();
43f3e411 8364 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8365 }
8366
8367 if (dwarf2_per_objfile->using_index)
43f3e411 8368 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8369 else
8370 {
8371 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8372 pst->compunit_symtab = cust;
f4dc4d17 8373 pst->readin = 1;
45cfd468 8374 }
f4dc4d17
DE
8375
8376 do_cleanups (back_to);
c906108c
SS
8377}
8378
95554aad
TT
8379/* Process an imported unit DIE. */
8380
8381static void
8382process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8383{
8384 struct attribute *attr;
8385
f4dc4d17
DE
8386 /* For now we don't handle imported units in type units. */
8387 if (cu->per_cu->is_debug_types)
8388 {
8389 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8390 " supported in type units [in module %s]"),
4262abfb 8391 objfile_name (cu->objfile));
f4dc4d17
DE
8392 }
8393
95554aad
TT
8394 attr = dwarf2_attr (die, DW_AT_import, cu);
8395 if (attr != NULL)
8396 {
9c541725
PA
8397 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
8398 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8399 dwarf2_per_cu_data *per_cu
8400 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, cu->objfile);
95554aad 8401
69d751e3 8402 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8403 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8404 load_full_comp_unit (per_cu, cu->language);
8405
796a7ff8 8406 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8407 per_cu);
8408 }
8409}
8410
adde2bff
DE
8411/* Reset the in_process bit of a die. */
8412
8413static void
8414reset_die_in_process (void *arg)
8415{
9a3c8263 8416 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8417
adde2bff
DE
8418 die->in_process = 0;
8419}
8420
c906108c
SS
8421/* Process a die and its children. */
8422
8423static void
e7c27a73 8424process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8425{
adde2bff
DE
8426 struct cleanup *in_process;
8427
8428 /* We should only be processing those not already in process. */
8429 gdb_assert (!die->in_process);
8430
8431 die->in_process = 1;
8432 in_process = make_cleanup (reset_die_in_process,die);
8433
c906108c
SS
8434 switch (die->tag)
8435 {
8436 case DW_TAG_padding:
8437 break;
8438 case DW_TAG_compile_unit:
95554aad 8439 case DW_TAG_partial_unit:
e7c27a73 8440 read_file_scope (die, cu);
c906108c 8441 break;
348e048f
DE
8442 case DW_TAG_type_unit:
8443 read_type_unit_scope (die, cu);
8444 break;
c906108c 8445 case DW_TAG_subprogram:
c906108c 8446 case DW_TAG_inlined_subroutine:
edb3359d 8447 read_func_scope (die, cu);
c906108c
SS
8448 break;
8449 case DW_TAG_lexical_block:
14898363
L
8450 case DW_TAG_try_block:
8451 case DW_TAG_catch_block:
e7c27a73 8452 read_lexical_block_scope (die, cu);
c906108c 8453 break;
216f72a1 8454 case DW_TAG_call_site:
96408a79
SA
8455 case DW_TAG_GNU_call_site:
8456 read_call_site_scope (die, cu);
8457 break;
c906108c 8458 case DW_TAG_class_type:
680b30c7 8459 case DW_TAG_interface_type:
c906108c
SS
8460 case DW_TAG_structure_type:
8461 case DW_TAG_union_type:
134d01f1 8462 process_structure_scope (die, cu);
c906108c
SS
8463 break;
8464 case DW_TAG_enumeration_type:
134d01f1 8465 process_enumeration_scope (die, cu);
c906108c 8466 break;
134d01f1 8467
f792889a
DJ
8468 /* These dies have a type, but processing them does not create
8469 a symbol or recurse to process the children. Therefore we can
8470 read them on-demand through read_type_die. */
c906108c 8471 case DW_TAG_subroutine_type:
72019c9c 8472 case DW_TAG_set_type:
c906108c 8473 case DW_TAG_array_type:
c906108c 8474 case DW_TAG_pointer_type:
c906108c 8475 case DW_TAG_ptr_to_member_type:
c906108c 8476 case DW_TAG_reference_type:
4297a3f0 8477 case DW_TAG_rvalue_reference_type:
c906108c 8478 case DW_TAG_string_type:
c906108c 8479 break;
134d01f1 8480
c906108c 8481 case DW_TAG_base_type:
a02abb62 8482 case DW_TAG_subrange_type:
cb249c71 8483 case DW_TAG_typedef:
134d01f1
DJ
8484 /* Add a typedef symbol for the type definition, if it has a
8485 DW_AT_name. */
f792889a 8486 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8487 break;
c906108c 8488 case DW_TAG_common_block:
e7c27a73 8489 read_common_block (die, cu);
c906108c
SS
8490 break;
8491 case DW_TAG_common_inclusion:
8492 break;
d9fa45fe 8493 case DW_TAG_namespace:
4d4ec4e5 8494 cu->processing_has_namespace_info = 1;
e7c27a73 8495 read_namespace (die, cu);
d9fa45fe 8496 break;
5d7cb8df 8497 case DW_TAG_module:
4d4ec4e5 8498 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8499 read_module (die, cu);
8500 break;
d9fa45fe 8501 case DW_TAG_imported_declaration:
74921315
KS
8502 cu->processing_has_namespace_info = 1;
8503 if (read_namespace_alias (die, cu))
8504 break;
8505 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8506 case DW_TAG_imported_module:
4d4ec4e5 8507 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8508 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8509 || cu->language != language_fortran))
8510 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8511 dwarf_tag_name (die->tag));
8512 read_import_statement (die, cu);
d9fa45fe 8513 break;
95554aad
TT
8514
8515 case DW_TAG_imported_unit:
8516 process_imported_unit_die (die, cu);
8517 break;
8518
c906108c 8519 default:
e7c27a73 8520 new_symbol (die, NULL, cu);
c906108c
SS
8521 break;
8522 }
adde2bff
DE
8523
8524 do_cleanups (in_process);
c906108c 8525}
ca69b9e6
DE
8526\f
8527/* DWARF name computation. */
c906108c 8528
94af9270
KS
8529/* A helper function for dwarf2_compute_name which determines whether DIE
8530 needs to have the name of the scope prepended to the name listed in the
8531 die. */
8532
8533static int
8534die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8535{
1c809c68
TT
8536 struct attribute *attr;
8537
94af9270
KS
8538 switch (die->tag)
8539 {
8540 case DW_TAG_namespace:
8541 case DW_TAG_typedef:
8542 case DW_TAG_class_type:
8543 case DW_TAG_interface_type:
8544 case DW_TAG_structure_type:
8545 case DW_TAG_union_type:
8546 case DW_TAG_enumeration_type:
8547 case DW_TAG_enumerator:
8548 case DW_TAG_subprogram:
08a76f8a 8549 case DW_TAG_inlined_subroutine:
94af9270 8550 case DW_TAG_member:
74921315 8551 case DW_TAG_imported_declaration:
94af9270
KS
8552 return 1;
8553
8554 case DW_TAG_variable:
c2b0a229 8555 case DW_TAG_constant:
94af9270
KS
8556 /* We only need to prefix "globally" visible variables. These include
8557 any variable marked with DW_AT_external or any variable that
8558 lives in a namespace. [Variables in anonymous namespaces
8559 require prefixing, but they are not DW_AT_external.] */
8560
8561 if (dwarf2_attr (die, DW_AT_specification, cu))
8562 {
8563 struct dwarf2_cu *spec_cu = cu;
9a619af0 8564
94af9270
KS
8565 return die_needs_namespace (die_specification (die, &spec_cu),
8566 spec_cu);
8567 }
8568
1c809c68 8569 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8570 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8571 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8572 return 0;
8573 /* A variable in a lexical block of some kind does not need a
8574 namespace, even though in C++ such variables may be external
8575 and have a mangled name. */
8576 if (die->parent->tag == DW_TAG_lexical_block
8577 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8578 || die->parent->tag == DW_TAG_catch_block
8579 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8580 return 0;
8581 return 1;
94af9270
KS
8582
8583 default:
8584 return 0;
8585 }
8586}
8587
8588/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8589 compute the physname for the object, which include a method's:
9c37b5ae 8590 - formal parameters (C++),
a766d390 8591 - receiver type (Go),
a766d390
DE
8592
8593 The term "physname" is a bit confusing.
8594 For C++, for example, it is the demangled name.
8595 For Go, for example, it's the mangled name.
94af9270 8596
af6b7be1
JB
8597 For Ada, return the DIE's linkage name rather than the fully qualified
8598 name. PHYSNAME is ignored..
8599
94af9270
KS
8600 The result is allocated on the objfile_obstack and canonicalized. */
8601
8602static const char *
15d034d0
TT
8603dwarf2_compute_name (const char *name,
8604 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8605 int physname)
8606{
bb5ed363
DE
8607 struct objfile *objfile = cu->objfile;
8608
94af9270
KS
8609 if (name == NULL)
8610 name = dwarf2_name (die, cu);
8611
2ee7123e
DE
8612 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8613 but otherwise compute it by typename_concat inside GDB.
8614 FIXME: Actually this is not really true, or at least not always true.
8615 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8616 Fortran names because there is no mangling standard. So new_symbol_full
8617 will set the demangled name to the result of dwarf2_full_name, and it is
8618 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8619 if (cu->language == language_ada
8620 || (cu->language == language_fortran && physname))
8621 {
8622 /* For Ada unit, we prefer the linkage name over the name, as
8623 the former contains the exported name, which the user expects
8624 to be able to reference. Ideally, we want the user to be able
8625 to reference this entity using either natural or linkage name,
8626 but we haven't started looking at this enhancement yet. */
2ee7123e 8627 const char *linkage_name;
f55ee35c 8628
2ee7123e
DE
8629 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8630 if (linkage_name == NULL)
8631 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8632 if (linkage_name != NULL)
8633 return linkage_name;
f55ee35c
JK
8634 }
8635
94af9270
KS
8636 /* These are the only languages we know how to qualify names in. */
8637 if (name != NULL
9c37b5ae 8638 && (cu->language == language_cplus
c44af4eb
TT
8639 || cu->language == language_fortran || cu->language == language_d
8640 || cu->language == language_rust))
94af9270
KS
8641 {
8642 if (die_needs_namespace (die, cu))
8643 {
8644 long length;
0d5cff50 8645 const char *prefix;
34a68019 8646 const char *canonical_name = NULL;
94af9270 8647
d7e74731
PA
8648 string_file buf;
8649
94af9270 8650 prefix = determine_prefix (die, cu);
94af9270
KS
8651 if (*prefix != '\0')
8652 {
f55ee35c
JK
8653 char *prefixed_name = typename_concat (NULL, prefix, name,
8654 physname, cu);
9a619af0 8655
d7e74731 8656 buf.puts (prefixed_name);
94af9270
KS
8657 xfree (prefixed_name);
8658 }
8659 else
d7e74731 8660 buf.puts (name);
94af9270 8661
98bfdba5
PA
8662 /* Template parameters may be specified in the DIE's DW_AT_name, or
8663 as children with DW_TAG_template_type_param or
8664 DW_TAG_value_type_param. If the latter, add them to the name
8665 here. If the name already has template parameters, then
8666 skip this step; some versions of GCC emit both, and
8667 it is more efficient to use the pre-computed name.
8668
8669 Something to keep in mind about this process: it is very
8670 unlikely, or in some cases downright impossible, to produce
8671 something that will match the mangled name of a function.
8672 If the definition of the function has the same debug info,
8673 we should be able to match up with it anyway. But fallbacks
8674 using the minimal symbol, for instance to find a method
8675 implemented in a stripped copy of libstdc++, will not work.
8676 If we do not have debug info for the definition, we will have to
8677 match them up some other way.
8678
8679 When we do name matching there is a related problem with function
8680 templates; two instantiated function templates are allowed to
8681 differ only by their return types, which we do not add here. */
8682
8683 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8684 {
8685 struct attribute *attr;
8686 struct die_info *child;
8687 int first = 1;
8688
8689 die->building_fullname = 1;
8690
8691 for (child = die->child; child != NULL; child = child->sibling)
8692 {
8693 struct type *type;
12df843f 8694 LONGEST value;
d521ce57 8695 const gdb_byte *bytes;
98bfdba5
PA
8696 struct dwarf2_locexpr_baton *baton;
8697 struct value *v;
8698
8699 if (child->tag != DW_TAG_template_type_param
8700 && child->tag != DW_TAG_template_value_param)
8701 continue;
8702
8703 if (first)
8704 {
d7e74731 8705 buf.puts ("<");
98bfdba5
PA
8706 first = 0;
8707 }
8708 else
d7e74731 8709 buf.puts (", ");
98bfdba5
PA
8710
8711 attr = dwarf2_attr (child, DW_AT_type, cu);
8712 if (attr == NULL)
8713 {
8714 complaint (&symfile_complaints,
8715 _("template parameter missing DW_AT_type"));
d7e74731 8716 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8717 continue;
8718 }
8719 type = die_type (child, cu);
8720
8721 if (child->tag == DW_TAG_template_type_param)
8722 {
d7e74731 8723 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8724 continue;
8725 }
8726
8727 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8728 if (attr == NULL)
8729 {
8730 complaint (&symfile_complaints,
3e43a32a
MS
8731 _("template parameter missing "
8732 "DW_AT_const_value"));
d7e74731 8733 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8734 continue;
8735 }
8736
8737 dwarf2_const_value_attr (attr, type, name,
8738 &cu->comp_unit_obstack, cu,
8739 &value, &bytes, &baton);
8740
8741 if (TYPE_NOSIGN (type))
8742 /* GDB prints characters as NUMBER 'CHAR'. If that's
8743 changed, this can use value_print instead. */
d7e74731 8744 c_printchar (value, type, &buf);
98bfdba5
PA
8745 else
8746 {
8747 struct value_print_options opts;
8748
8749 if (baton != NULL)
8750 v = dwarf2_evaluate_loc_desc (type, NULL,
8751 baton->data,
8752 baton->size,
8753 baton->per_cu);
8754 else if (bytes != NULL)
8755 {
8756 v = allocate_value (type);
8757 memcpy (value_contents_writeable (v), bytes,
8758 TYPE_LENGTH (type));
8759 }
8760 else
8761 v = value_from_longest (type, value);
8762
3e43a32a
MS
8763 /* Specify decimal so that we do not depend on
8764 the radix. */
98bfdba5
PA
8765 get_formatted_print_options (&opts, 'd');
8766 opts.raw = 1;
d7e74731 8767 value_print (v, &buf, &opts);
98bfdba5
PA
8768 release_value (v);
8769 value_free (v);
8770 }
8771 }
8772
8773 die->building_fullname = 0;
8774
8775 if (!first)
8776 {
8777 /* Close the argument list, with a space if necessary
8778 (nested templates). */
d7e74731
PA
8779 if (!buf.empty () && buf.string ().back () == '>')
8780 buf.puts (" >");
98bfdba5 8781 else
d7e74731 8782 buf.puts (">");
98bfdba5
PA
8783 }
8784 }
8785
9c37b5ae 8786 /* For C++ methods, append formal parameter type
94af9270 8787 information, if PHYSNAME. */
6e70227d 8788
94af9270 8789 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8790 && cu->language == language_cplus)
94af9270
KS
8791 {
8792 struct type *type = read_type_die (die, cu);
8793
d7e74731 8794 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8795 &type_print_raw_options);
94af9270 8796
9c37b5ae 8797 if (cu->language == language_cplus)
94af9270 8798 {
60430eff
DJ
8799 /* Assume that an artificial first parameter is
8800 "this", but do not crash if it is not. RealView
8801 marks unnamed (and thus unused) parameters as
8802 artificial; there is no way to differentiate
8803 the two cases. */
94af9270
KS
8804 if (TYPE_NFIELDS (type) > 0
8805 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8806 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8807 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8808 0))))
d7e74731 8809 buf.puts (" const");
94af9270
KS
8810 }
8811 }
8812
d7e74731 8813 const std::string &intermediate_name = buf.string ();
94af9270
KS
8814
8815 if (cu->language == language_cplus)
34a68019 8816 canonical_name
322a8516 8817 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8818 &objfile->per_bfd->storage_obstack);
8819
8820 /* If we only computed INTERMEDIATE_NAME, or if
8821 INTERMEDIATE_NAME is already canonical, then we need to
8822 copy it to the appropriate obstack. */
322a8516 8823 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8824 name = ((const char *)
8825 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8826 intermediate_name.c_str (),
8827 intermediate_name.length ()));
34a68019
TT
8828 else
8829 name = canonical_name;
94af9270
KS
8830 }
8831 }
8832
8833 return name;
8834}
8835
0114d602
DJ
8836/* Return the fully qualified name of DIE, based on its DW_AT_name.
8837 If scope qualifiers are appropriate they will be added. The result
34a68019 8838 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8839 not have a name. NAME may either be from a previous call to
8840 dwarf2_name or NULL.
8841
9c37b5ae 8842 The output string will be canonicalized (if C++). */
0114d602
DJ
8843
8844static const char *
15d034d0 8845dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8846{
94af9270
KS
8847 return dwarf2_compute_name (name, die, cu, 0);
8848}
0114d602 8849
94af9270
KS
8850/* Construct a physname for the given DIE in CU. NAME may either be
8851 from a previous call to dwarf2_name or NULL. The result will be
8852 allocated on the objfile_objstack or NULL if the DIE does not have a
8853 name.
0114d602 8854
9c37b5ae 8855 The output string will be canonicalized (if C++). */
0114d602 8856
94af9270 8857static const char *
15d034d0 8858dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8859{
bb5ed363 8860 struct objfile *objfile = cu->objfile;
900e11f9
JK
8861 const char *retval, *mangled = NULL, *canon = NULL;
8862 struct cleanup *back_to;
8863 int need_copy = 1;
8864
8865 /* In this case dwarf2_compute_name is just a shortcut not building anything
8866 on its own. */
8867 if (!die_needs_namespace (die, cu))
8868 return dwarf2_compute_name (name, die, cu, 1);
8869
8870 back_to = make_cleanup (null_cleanup, NULL);
8871
7d45c7c3
KB
8872 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8873 if (mangled == NULL)
8874 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 8875
e98c9e7c
TT
8876 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8877 See https://github.com/rust-lang/rust/issues/32925. */
8878 if (cu->language == language_rust && mangled != NULL
8879 && strchr (mangled, '{') != NULL)
8880 mangled = NULL;
8881
900e11f9
JK
8882 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8883 has computed. */
7d45c7c3 8884 if (mangled != NULL)
900e11f9
JK
8885 {
8886 char *demangled;
8887
900e11f9
JK
8888 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8889 type. It is easier for GDB users to search for such functions as
8890 `name(params)' than `long name(params)'. In such case the minimal
8891 symbol names do not match the full symbol names but for template
8892 functions there is never a need to look up their definition from their
8893 declaration so the only disadvantage remains the minimal symbol
8894 variant `long name(params)' does not have the proper inferior type.
8895 */
8896
a766d390
DE
8897 if (cu->language == language_go)
8898 {
8899 /* This is a lie, but we already lie to the caller new_symbol_full.
8900 new_symbol_full assumes we return the mangled name.
8901 This just undoes that lie until things are cleaned up. */
8902 demangled = NULL;
8903 }
8904 else
8905 {
8de20a37 8906 demangled = gdb_demangle (mangled,
9c37b5ae 8907 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 8908 }
900e11f9
JK
8909 if (demangled)
8910 {
8911 make_cleanup (xfree, demangled);
8912 canon = demangled;
8913 }
8914 else
8915 {
8916 canon = mangled;
8917 need_copy = 0;
8918 }
8919 }
8920
8921 if (canon == NULL || check_physname)
8922 {
8923 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8924
8925 if (canon != NULL && strcmp (physname, canon) != 0)
8926 {
8927 /* It may not mean a bug in GDB. The compiler could also
8928 compute DW_AT_linkage_name incorrectly. But in such case
8929 GDB would need to be bug-to-bug compatible. */
8930
8931 complaint (&symfile_complaints,
8932 _("Computed physname <%s> does not match demangled <%s> "
8933 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
9c541725 8934 physname, canon, mangled, to_underlying (die->sect_off),
4262abfb 8935 objfile_name (objfile));
900e11f9
JK
8936
8937 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8938 is available here - over computed PHYSNAME. It is safer
8939 against both buggy GDB and buggy compilers. */
8940
8941 retval = canon;
8942 }
8943 else
8944 {
8945 retval = physname;
8946 need_copy = 0;
8947 }
8948 }
8949 else
8950 retval = canon;
8951
8952 if (need_copy)
224c3ddb
SM
8953 retval = ((const char *)
8954 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8955 retval, strlen (retval)));
900e11f9
JK
8956
8957 do_cleanups (back_to);
8958 return retval;
0114d602
DJ
8959}
8960
74921315
KS
8961/* Inspect DIE in CU for a namespace alias. If one exists, record
8962 a new symbol for it.
8963
8964 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8965
8966static int
8967read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8968{
8969 struct attribute *attr;
8970
8971 /* If the die does not have a name, this is not a namespace
8972 alias. */
8973 attr = dwarf2_attr (die, DW_AT_name, cu);
8974 if (attr != NULL)
8975 {
8976 int num;
8977 struct die_info *d = die;
8978 struct dwarf2_cu *imported_cu = cu;
8979
8980 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8981 keep inspecting DIEs until we hit the underlying import. */
8982#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8983 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8984 {
8985 attr = dwarf2_attr (d, DW_AT_import, cu);
8986 if (attr == NULL)
8987 break;
8988
8989 d = follow_die_ref (d, attr, &imported_cu);
8990 if (d->tag != DW_TAG_imported_declaration)
8991 break;
8992 }
8993
8994 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8995 {
8996 complaint (&symfile_complaints,
8997 _("DIE at 0x%x has too many recursively imported "
9c541725 8998 "declarations"), to_underlying (d->sect_off));
74921315
KS
8999 return 0;
9000 }
9001
9002 if (attr != NULL)
9003 {
9004 struct type *type;
9c541725 9005 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 9006
9c541725 9007 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
9008 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9009 {
9010 /* This declaration is a global namespace alias. Add
9011 a symbol for it whose type is the aliased namespace. */
9012 new_symbol (die, type, cu);
9013 return 1;
9014 }
9015 }
9016 }
9017
9018 return 0;
9019}
9020
22cee43f
PMR
9021/* Return the using directives repository (global or local?) to use in the
9022 current context for LANGUAGE.
9023
9024 For Ada, imported declarations can materialize renamings, which *may* be
9025 global. However it is impossible (for now?) in DWARF to distinguish
9026 "external" imported declarations and "static" ones. As all imported
9027 declarations seem to be static in all other languages, make them all CU-wide
9028 global only in Ada. */
9029
9030static struct using_direct **
9031using_directives (enum language language)
9032{
9033 if (language == language_ada && context_stack_depth == 0)
9034 return &global_using_directives;
9035 else
9036 return &local_using_directives;
9037}
9038
27aa8d6a
SW
9039/* Read the import statement specified by the given die and record it. */
9040
9041static void
9042read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9043{
bb5ed363 9044 struct objfile *objfile = cu->objfile;
27aa8d6a 9045 struct attribute *import_attr;
32019081 9046 struct die_info *imported_die, *child_die;
de4affc9 9047 struct dwarf2_cu *imported_cu;
27aa8d6a 9048 const char *imported_name;
794684b6 9049 const char *imported_name_prefix;
13387711
SW
9050 const char *canonical_name;
9051 const char *import_alias;
9052 const char *imported_declaration = NULL;
794684b6 9053 const char *import_prefix;
32019081
JK
9054 VEC (const_char_ptr) *excludes = NULL;
9055 struct cleanup *cleanups;
13387711 9056
27aa8d6a
SW
9057 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9058 if (import_attr == NULL)
9059 {
9060 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9061 dwarf_tag_name (die->tag));
9062 return;
9063 }
9064
de4affc9
CC
9065 imported_cu = cu;
9066 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9067 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
9068 if (imported_name == NULL)
9069 {
9070 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9071
9072 The import in the following code:
9073 namespace A
9074 {
9075 typedef int B;
9076 }
9077
9078 int main ()
9079 {
9080 using A::B;
9081 B b;
9082 return b;
9083 }
9084
9085 ...
9086 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9087 <52> DW_AT_decl_file : 1
9088 <53> DW_AT_decl_line : 6
9089 <54> DW_AT_import : <0x75>
9090 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9091 <59> DW_AT_name : B
9092 <5b> DW_AT_decl_file : 1
9093 <5c> DW_AT_decl_line : 2
9094 <5d> DW_AT_type : <0x6e>
9095 ...
9096 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9097 <76> DW_AT_byte_size : 4
9098 <77> DW_AT_encoding : 5 (signed)
9099
9100 imports the wrong die ( 0x75 instead of 0x58 ).
9101 This case will be ignored until the gcc bug is fixed. */
9102 return;
9103 }
9104
82856980
SW
9105 /* Figure out the local name after import. */
9106 import_alias = dwarf2_name (die, cu);
27aa8d6a 9107
794684b6
SW
9108 /* Figure out where the statement is being imported to. */
9109 import_prefix = determine_prefix (die, cu);
9110
9111 /* Figure out what the scope of the imported die is and prepend it
9112 to the name of the imported die. */
de4affc9 9113 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9114
f55ee35c
JK
9115 if (imported_die->tag != DW_TAG_namespace
9116 && imported_die->tag != DW_TAG_module)
794684b6 9117 {
13387711
SW
9118 imported_declaration = imported_name;
9119 canonical_name = imported_name_prefix;
794684b6 9120 }
13387711 9121 else if (strlen (imported_name_prefix) > 0)
12aaed36 9122 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9123 imported_name_prefix,
9124 (cu->language == language_d ? "." : "::"),
9125 imported_name, (char *) NULL);
13387711
SW
9126 else
9127 canonical_name = imported_name;
794684b6 9128
32019081
JK
9129 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9130
9131 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9132 for (child_die = die->child; child_die && child_die->tag;
9133 child_die = sibling_die (child_die))
9134 {
9135 /* DWARF-4: A Fortran use statement with a “rename list” may be
9136 represented by an imported module entry with an import attribute
9137 referring to the module and owned entries corresponding to those
9138 entities that are renamed as part of being imported. */
9139
9140 if (child_die->tag != DW_TAG_imported_declaration)
9141 {
9142 complaint (&symfile_complaints,
9143 _("child DW_TAG_imported_declaration expected "
9144 "- DIE at 0x%x [in module %s]"),
9c541725 9145 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9146 continue;
9147 }
9148
9149 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9150 if (import_attr == NULL)
9151 {
9152 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9153 dwarf_tag_name (child_die->tag));
9154 continue;
9155 }
9156
9157 imported_cu = cu;
9158 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9159 &imported_cu);
9160 imported_name = dwarf2_name (imported_die, imported_cu);
9161 if (imported_name == NULL)
9162 {
9163 complaint (&symfile_complaints,
9164 _("child DW_TAG_imported_declaration has unknown "
9165 "imported name - DIE at 0x%x [in module %s]"),
9c541725 9166 to_underlying (child_die->sect_off), objfile_name (objfile));
32019081
JK
9167 continue;
9168 }
9169
9170 VEC_safe_push (const_char_ptr, excludes, imported_name);
9171
9172 process_die (child_die, cu);
9173 }
9174
22cee43f
PMR
9175 add_using_directive (using_directives (cu->language),
9176 import_prefix,
9177 canonical_name,
9178 import_alias,
9179 imported_declaration,
9180 excludes,
9181 0,
9182 &objfile->objfile_obstack);
32019081
JK
9183
9184 do_cleanups (cleanups);
27aa8d6a
SW
9185}
9186
1b80a9fa
JK
9187/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9188 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9189 this, it was first present in GCC release 4.3.0. */
9190
9191static int
9192producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9193{
9194 if (!cu->checked_producer)
9195 check_producer (cu);
9196
9197 return cu->producer_is_gcc_lt_4_3;
9198}
9199
d721ba37
PA
9200static file_and_directory
9201find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9202{
d721ba37
PA
9203 file_and_directory res;
9204
9291a0cd
TT
9205 /* Find the filename. Do not use dwarf2_name here, since the filename
9206 is not a source language identifier. */
d721ba37
PA
9207 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9208 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9209
d721ba37
PA
9210 if (res.comp_dir == NULL
9211 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9212 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9213 {
d721ba37
PA
9214 res.comp_dir_storage = ldirname (res.name);
9215 if (!res.comp_dir_storage.empty ())
9216 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9217 }
d721ba37 9218 if (res.comp_dir != NULL)
9291a0cd
TT
9219 {
9220 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9221 directory, get rid of it. */
d721ba37 9222 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9223
d721ba37
PA
9224 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9225 res.comp_dir = cp + 1;
9291a0cd
TT
9226 }
9227
d721ba37
PA
9228 if (res.name == NULL)
9229 res.name = "<unknown>";
9230
9231 return res;
9291a0cd
TT
9232}
9233
f4dc4d17
DE
9234/* Handle DW_AT_stmt_list for a compilation unit.
9235 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9236 COMP_DIR is the compilation directory. LOWPC is passed to
9237 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9238
9239static void
9240handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9241 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9242{
527f3840 9243 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9244 struct attribute *attr;
527f3840
JK
9245 struct line_header line_header_local;
9246 hashval_t line_header_local_hash;
9247 unsigned u;
9248 void **slot;
9249 int decode_mapping;
2ab95328 9250
f4dc4d17
DE
9251 gdb_assert (! cu->per_cu->is_debug_types);
9252
2ab95328 9253 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9254 if (attr == NULL)
9255 return;
9256
9c541725 9257 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
9258
9259 /* The line header hash table is only created if needed (it exists to
9260 prevent redundant reading of the line table for partial_units).
9261 If we're given a partial_unit, we'll need it. If we're given a
9262 compile_unit, then use the line header hash table if it's already
9263 created, but don't create one just yet. */
9264
9265 if (dwarf2_per_objfile->line_header_hash == NULL
9266 && die->tag == DW_TAG_partial_unit)
2ab95328 9267 {
527f3840
JK
9268 dwarf2_per_objfile->line_header_hash
9269 = htab_create_alloc_ex (127, line_header_hash_voidp,
9270 line_header_eq_voidp,
9271 free_line_header_voidp,
9272 &objfile->objfile_obstack,
9273 hashtab_obstack_allocate,
9274 dummy_obstack_deallocate);
9275 }
2ab95328 9276
9c541725 9277 line_header_local.sect_off = line_offset;
527f3840
JK
9278 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9279 line_header_local_hash = line_header_hash (&line_header_local);
9280 if (dwarf2_per_objfile->line_header_hash != NULL)
9281 {
9282 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9283 &line_header_local,
9284 line_header_local_hash, NO_INSERT);
9285
9286 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9287 is not present in *SLOT (since if there is something in *SLOT then
9288 it will be for a partial_unit). */
9289 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9290 {
527f3840 9291 gdb_assert (*slot != NULL);
9a3c8263 9292 cu->line_header = (struct line_header *) *slot;
527f3840 9293 return;
dee91e82 9294 }
2ab95328 9295 }
527f3840
JK
9296
9297 /* dwarf_decode_line_header does not yet provide sufficient information.
9298 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
9299 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9300 if (lh == NULL)
527f3840 9301 return;
fff8551c 9302 cu->line_header = lh.get ();
527f3840
JK
9303
9304 if (dwarf2_per_objfile->line_header_hash == NULL)
9305 slot = NULL;
9306 else
9307 {
9308 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9309 &line_header_local,
9310 line_header_local_hash, INSERT);
9311 gdb_assert (slot != NULL);
9312 }
9313 if (slot != NULL && *slot == NULL)
9314 {
9315 /* This newly decoded line number information unit will be owned
9316 by line_header_hash hash table. */
9317 *slot = cu->line_header;
9318 }
9319 else
9320 {
9321 /* We cannot free any current entry in (*slot) as that struct line_header
9322 may be already used by multiple CUs. Create only temporary decoded
9323 line_header for this CU - it may happen at most once for each line
9324 number information unit. And if we're not using line_header_hash
9325 then this is what we want as well. */
9326 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
9327 }
9328 decode_mapping = (die->tag != DW_TAG_partial_unit);
9329 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9330 decode_mapping);
fff8551c
PA
9331
9332 lh.release ();
2ab95328
TT
9333}
9334
95554aad 9335/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9336
c906108c 9337static void
e7c27a73 9338read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9339{
dee91e82 9340 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9341 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9342 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9343 CORE_ADDR highpc = ((CORE_ADDR) 0);
9344 struct attribute *attr;
c906108c 9345 struct die_info *child_die;
e142c38c 9346 CORE_ADDR baseaddr;
6e70227d 9347
e142c38c 9348 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9349
fae299cd 9350 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9351
9352 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9353 from finish_block. */
2acceee2 9354 if (lowpc == ((CORE_ADDR) -1))
c906108c 9355 lowpc = highpc;
3e29f34a 9356 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9357
d721ba37 9358 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9359
95554aad 9360 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9361
f4b8a18d
KW
9362 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9363 standardised yet. As a workaround for the language detection we fall
9364 back to the DW_AT_producer string. */
9365 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9366 cu->language = language_opencl;
9367
3019eac3
DE
9368 /* Similar hack for Go. */
9369 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9370 set_cu_language (DW_LANG_Go, cu);
9371
d721ba37 9372 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9373
9374 /* Decode line number information if present. We do this before
9375 processing child DIEs, so that the line header table is available
9376 for DW_AT_decl_file. */
d721ba37 9377 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9378
9379 /* Process all dies in compilation unit. */
9380 if (die->child != NULL)
9381 {
9382 child_die = die->child;
9383 while (child_die && child_die->tag)
9384 {
9385 process_die (child_die, cu);
9386 child_die = sibling_die (child_die);
9387 }
9388 }
9389
9390 /* Decode macro information, if present. Dwarf 2 macro information
9391 refers to information in the line number info statement program
9392 header, so we can only read it if we've read the header
9393 successfully. */
0af92d60
JK
9394 attr = dwarf2_attr (die, DW_AT_macros, cu);
9395 if (attr == NULL)
9396 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9397 if (attr && cu->line_header)
9398 {
9399 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9400 complaint (&symfile_complaints,
0af92d60 9401 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9402
43f3e411 9403 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9404 }
9405 else
9406 {
9407 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9408 if (attr && cu->line_header)
9409 {
9410 unsigned int macro_offset = DW_UNSND (attr);
9411
43f3e411 9412 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9413 }
9414 }
3019eac3
DE
9415}
9416
f4dc4d17
DE
9417/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9418 Create the set of symtabs used by this TU, or if this TU is sharing
9419 symtabs with another TU and the symtabs have already been created
9420 then restore those symtabs in the line header.
9421 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9422
9423static void
f4dc4d17 9424setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9425{
f4dc4d17
DE
9426 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9427 struct type_unit_group *tu_group;
9428 int first_time;
3019eac3 9429 struct attribute *attr;
9c541725 9430 unsigned int i;
0186c6a7 9431 struct signatured_type *sig_type;
3019eac3 9432
f4dc4d17 9433 gdb_assert (per_cu->is_debug_types);
0186c6a7 9434 sig_type = (struct signatured_type *) per_cu;
3019eac3 9435
f4dc4d17 9436 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9437
f4dc4d17 9438 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9439 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9440 if (sig_type->type_unit_group == NULL)
9441 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9442 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9443
9444 /* If we've already processed this stmt_list there's no real need to
9445 do it again, we could fake it and just recreate the part we need
9446 (file name,index -> symtab mapping). If data shows this optimization
9447 is useful we can do it then. */
43f3e411 9448 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9449
9450 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9451 debug info. */
fff8551c 9452 line_header_up lh;
f4dc4d17 9453 if (attr != NULL)
3019eac3 9454 {
9c541725 9455 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
9456 lh = dwarf_decode_line_header (line_offset, cu);
9457 }
9458 if (lh == NULL)
9459 {
9460 if (first_time)
9461 dwarf2_start_symtab (cu, "", NULL, 0);
9462 else
9463 {
9464 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9465 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9466 }
f4dc4d17 9467 return;
3019eac3
DE
9468 }
9469
fff8551c 9470 cu->line_header = lh.get ();
3019eac3 9471
f4dc4d17
DE
9472 if (first_time)
9473 {
43f3e411 9474 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9475
1fd60fc0
DE
9476 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9477 still initializing it, and our caller (a few levels up)
9478 process_full_type_unit still needs to know if this is the first
9479 time. */
9480
fff8551c
PA
9481 tu_group->num_symtabs = lh->file_names.size ();
9482 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
3019eac3 9483
fff8551c 9484 for (i = 0; i < lh->file_names.size (); ++i)
f4dc4d17 9485 {
8c43009f 9486 file_entry &fe = lh->file_names[i];
3019eac3 9487
fff8551c 9488 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
3019eac3 9489
f4dc4d17
DE
9490 if (current_subfile->symtab == NULL)
9491 {
9492 /* NOTE: start_subfile will recognize when it's been passed
9493 a file it has already seen. So we can't assume there's a
43f3e411 9494 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9495 lh->file_names may contain dups. */
43f3e411
DE
9496 current_subfile->symtab
9497 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9498 }
9499
8c43009f
PA
9500 fe.symtab = current_subfile->symtab;
9501 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
9502 }
9503 }
9504 else
3019eac3 9505 {
0ab9ce85 9506 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9507
fff8551c 9508 for (i = 0; i < lh->file_names.size (); ++i)
f4dc4d17
DE
9509 {
9510 struct file_entry *fe = &lh->file_names[i];
9511
9512 fe->symtab = tu_group->symtabs[i];
9513 }
3019eac3
DE
9514 }
9515
fff8551c
PA
9516 lh.release ();
9517
f4dc4d17
DE
9518 /* The main symtab is allocated last. Type units don't have DW_AT_name
9519 so they don't have a "real" (so to speak) symtab anyway.
9520 There is later code that will assign the main symtab to all symbols
9521 that don't have one. We need to handle the case of a symbol with a
9522 missing symtab (DW_AT_decl_file) anyway. */
9523}
3019eac3 9524
f4dc4d17
DE
9525/* Process DW_TAG_type_unit.
9526 For TUs we want to skip the first top level sibling if it's not the
9527 actual type being defined by this TU. In this case the first top
9528 level sibling is there to provide context only. */
3019eac3 9529
f4dc4d17
DE
9530static void
9531read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9532{
9533 struct die_info *child_die;
3019eac3 9534
f4dc4d17
DE
9535 prepare_one_comp_unit (cu, die, language_minimal);
9536
9537 /* Initialize (or reinitialize) the machinery for building symtabs.
9538 We do this before processing child DIEs, so that the line header table
9539 is available for DW_AT_decl_file. */
9540 setup_type_unit_groups (die, cu);
9541
9542 if (die->child != NULL)
9543 {
9544 child_die = die->child;
9545 while (child_die && child_die->tag)
9546 {
9547 process_die (child_die, cu);
9548 child_die = sibling_die (child_die);
9549 }
9550 }
3019eac3
DE
9551}
9552\f
80626a55
DE
9553/* DWO/DWP files.
9554
9555 http://gcc.gnu.org/wiki/DebugFission
9556 http://gcc.gnu.org/wiki/DebugFissionDWP
9557
9558 To simplify handling of both DWO files ("object" files with the DWARF info)
9559 and DWP files (a file with the DWOs packaged up into one file), we treat
9560 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9561
9562static hashval_t
9563hash_dwo_file (const void *item)
9564{
9a3c8263 9565 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9566 hashval_t hash;
3019eac3 9567
a2ce51a0
DE
9568 hash = htab_hash_string (dwo_file->dwo_name);
9569 if (dwo_file->comp_dir != NULL)
9570 hash += htab_hash_string (dwo_file->comp_dir);
9571 return hash;
3019eac3
DE
9572}
9573
9574static int
9575eq_dwo_file (const void *item_lhs, const void *item_rhs)
9576{
9a3c8263
SM
9577 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9578 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9579
a2ce51a0
DE
9580 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9581 return 0;
9582 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9583 return lhs->comp_dir == rhs->comp_dir;
9584 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9585}
9586
9587/* Allocate a hash table for DWO files. */
9588
9589static htab_t
9590allocate_dwo_file_hash_table (void)
9591{
9592 struct objfile *objfile = dwarf2_per_objfile->objfile;
9593
9594 return htab_create_alloc_ex (41,
9595 hash_dwo_file,
9596 eq_dwo_file,
9597 NULL,
9598 &objfile->objfile_obstack,
9599 hashtab_obstack_allocate,
9600 dummy_obstack_deallocate);
9601}
9602
80626a55
DE
9603/* Lookup DWO file DWO_NAME. */
9604
9605static void **
0ac5b59e 9606lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9607{
9608 struct dwo_file find_entry;
9609 void **slot;
9610
9611 if (dwarf2_per_objfile->dwo_files == NULL)
9612 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9613
9614 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9615 find_entry.dwo_name = dwo_name;
9616 find_entry.comp_dir = comp_dir;
80626a55
DE
9617 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9618
9619 return slot;
9620}
9621
3019eac3
DE
9622static hashval_t
9623hash_dwo_unit (const void *item)
9624{
9a3c8263 9625 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9626
9627 /* This drops the top 32 bits of the id, but is ok for a hash. */
9628 return dwo_unit->signature;
9629}
9630
9631static int
9632eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9633{
9a3c8263
SM
9634 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9635 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9636
9637 /* The signature is assumed to be unique within the DWO file.
9638 So while object file CU dwo_id's always have the value zero,
9639 that's OK, assuming each object file DWO file has only one CU,
9640 and that's the rule for now. */
9641 return lhs->signature == rhs->signature;
9642}
9643
9644/* Allocate a hash table for DWO CUs,TUs.
9645 There is one of these tables for each of CUs,TUs for each DWO file. */
9646
9647static htab_t
9648allocate_dwo_unit_table (struct objfile *objfile)
9649{
9650 /* Start out with a pretty small number.
9651 Generally DWO files contain only one CU and maybe some TUs. */
9652 return htab_create_alloc_ex (3,
9653 hash_dwo_unit,
9654 eq_dwo_unit,
9655 NULL,
9656 &objfile->objfile_obstack,
9657 hashtab_obstack_allocate,
9658 dummy_obstack_deallocate);
9659}
9660
80626a55 9661/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9662
19c3d4c9 9663struct create_dwo_cu_data
3019eac3
DE
9664{
9665 struct dwo_file *dwo_file;
19c3d4c9 9666 struct dwo_unit dwo_unit;
3019eac3
DE
9667};
9668
19c3d4c9 9669/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9670
9671static void
19c3d4c9
DE
9672create_dwo_cu_reader (const struct die_reader_specs *reader,
9673 const gdb_byte *info_ptr,
9674 struct die_info *comp_unit_die,
9675 int has_children,
9676 void *datap)
3019eac3
DE
9677{
9678 struct dwarf2_cu *cu = reader->cu;
9c541725 9679 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 9680 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9681 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9682 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9683 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9684 struct attribute *attr;
3019eac3
DE
9685
9686 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9687 if (attr == NULL)
9688 {
19c3d4c9
DE
9689 complaint (&symfile_complaints,
9690 _("Dwarf Error: debug entry at offset 0x%x is missing"
9691 " its dwo_id [in module %s]"),
9c541725 9692 to_underlying (sect_off), dwo_file->dwo_name);
3019eac3
DE
9693 return;
9694 }
9695
3019eac3
DE
9696 dwo_unit->dwo_file = dwo_file;
9697 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9698 dwo_unit->section = section;
9c541725 9699 dwo_unit->sect_off = sect_off;
3019eac3
DE
9700 dwo_unit->length = cu->per_cu->length;
9701
b4f54984 9702 if (dwarf_read_debug)
4031ecc5 9703 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9c541725
PA
9704 to_underlying (sect_off),
9705 hex_string (dwo_unit->signature));
3019eac3
DE
9706}
9707
19c3d4c9
DE
9708/* Create the dwo_unit for the lone CU in DWO_FILE.
9709 Note: This function processes DWO files only, not DWP files. */
3019eac3 9710
19c3d4c9
DE
9711static struct dwo_unit *
9712create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9713{
9714 struct objfile *objfile = dwarf2_per_objfile->objfile;
9715 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9716 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9717 struct create_dwo_cu_data create_dwo_cu_data;
9718 struct dwo_unit *dwo_unit;
3019eac3
DE
9719
9720 dwarf2_read_section (objfile, section);
9721 info_ptr = section->buffer;
9722
9723 if (info_ptr == NULL)
9724 return NULL;
9725
b4f54984 9726 if (dwarf_read_debug)
19c3d4c9
DE
9727 {
9728 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9729 get_section_name (section),
9730 get_section_file_name (section));
19c3d4c9 9731 }
3019eac3 9732
19c3d4c9
DE
9733 create_dwo_cu_data.dwo_file = dwo_file;
9734 dwo_unit = NULL;
3019eac3
DE
9735
9736 end_ptr = info_ptr + section->size;
9737 while (info_ptr < end_ptr)
9738 {
9739 struct dwarf2_per_cu_data per_cu;
9740
19c3d4c9
DE
9741 memset (&create_dwo_cu_data.dwo_unit, 0,
9742 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9743 memset (&per_cu, 0, sizeof (per_cu));
9744 per_cu.objfile = objfile;
9745 per_cu.is_debug_types = 0;
9c541725 9746 per_cu.sect_off = sect_offset (info_ptr - section->buffer);
8a0459fd 9747 per_cu.section = section;
3019eac3 9748
33e80786 9749 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9750 create_dwo_cu_reader,
9751 &create_dwo_cu_data);
9752
9753 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9754 {
9755 /* If we've already found one, complain. We only support one
9756 because having more than one requires hacking the dwo_name of
9757 each to match, which is highly unlikely to happen. */
9758 if (dwo_unit != NULL)
9759 {
9760 complaint (&symfile_complaints,
9761 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9762 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9763 break;
9764 }
9765
9766 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9767 *dwo_unit = create_dwo_cu_data.dwo_unit;
9768 }
3019eac3
DE
9769
9770 info_ptr += per_cu.length;
9771 }
9772
19c3d4c9 9773 return dwo_unit;
3019eac3
DE
9774}
9775
80626a55
DE
9776/* DWP file .debug_{cu,tu}_index section format:
9777 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9778
d2415c6c
DE
9779 DWP Version 1:
9780
80626a55
DE
9781 Both index sections have the same format, and serve to map a 64-bit
9782 signature to a set of section numbers. Each section begins with a header,
9783 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9784 indexes, and a pool of 32-bit section numbers. The index sections will be
9785 aligned at 8-byte boundaries in the file.
9786
d2415c6c
DE
9787 The index section header consists of:
9788
9789 V, 32 bit version number
9790 -, 32 bits unused
9791 N, 32 bit number of compilation units or type units in the index
9792 M, 32 bit number of slots in the hash table
80626a55 9793
d2415c6c 9794 Numbers are recorded using the byte order of the application binary.
80626a55 9795
d2415c6c
DE
9796 The hash table begins at offset 16 in the section, and consists of an array
9797 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9798 order of the application binary). Unused slots in the hash table are 0.
9799 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9800
d2415c6c
DE
9801 The parallel table begins immediately after the hash table
9802 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9803 array of 32-bit indexes (using the byte order of the application binary),
9804 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9805 table contains a 32-bit index into the pool of section numbers. For unused
9806 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9807
73869dc2
DE
9808 The pool of section numbers begins immediately following the hash table
9809 (at offset 16 + 12 * M from the beginning of the section). The pool of
9810 section numbers consists of an array of 32-bit words (using the byte order
9811 of the application binary). Each item in the array is indexed starting
9812 from 0. The hash table entry provides the index of the first section
9813 number in the set. Additional section numbers in the set follow, and the
9814 set is terminated by a 0 entry (section number 0 is not used in ELF).
9815
9816 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9817 section must be the first entry in the set, and the .debug_abbrev.dwo must
9818 be the second entry. Other members of the set may follow in any order.
9819
9820 ---
9821
9822 DWP Version 2:
9823
9824 DWP Version 2 combines all the .debug_info, etc. sections into one,
9825 and the entries in the index tables are now offsets into these sections.
9826 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9827 section.
9828
9829 Index Section Contents:
9830 Header
9831 Hash Table of Signatures dwp_hash_table.hash_table
9832 Parallel Table of Indices dwp_hash_table.unit_table
9833 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9834 Table of Section Sizes dwp_hash_table.v2.sizes
9835
9836 The index section header consists of:
9837
9838 V, 32 bit version number
9839 L, 32 bit number of columns in the table of section offsets
9840 N, 32 bit number of compilation units or type units in the index
9841 M, 32 bit number of slots in the hash table
9842
9843 Numbers are recorded using the byte order of the application binary.
9844
9845 The hash table has the same format as version 1.
9846 The parallel table of indices has the same format as version 1,
9847 except that the entries are origin-1 indices into the table of sections
9848 offsets and the table of section sizes.
9849
9850 The table of offsets begins immediately following the parallel table
9851 (at offset 16 + 12 * M from the beginning of the section). The table is
9852 a two-dimensional array of 32-bit words (using the byte order of the
9853 application binary), with L columns and N+1 rows, in row-major order.
9854 Each row in the array is indexed starting from 0. The first row provides
9855 a key to the remaining rows: each column in this row provides an identifier
9856 for a debug section, and the offsets in the same column of subsequent rows
9857 refer to that section. The section identifiers are:
9858
9859 DW_SECT_INFO 1 .debug_info.dwo
9860 DW_SECT_TYPES 2 .debug_types.dwo
9861 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9862 DW_SECT_LINE 4 .debug_line.dwo
9863 DW_SECT_LOC 5 .debug_loc.dwo
9864 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9865 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9866 DW_SECT_MACRO 8 .debug_macro.dwo
9867
9868 The offsets provided by the CU and TU index sections are the base offsets
9869 for the contributions made by each CU or TU to the corresponding section
9870 in the package file. Each CU and TU header contains an abbrev_offset
9871 field, used to find the abbreviations table for that CU or TU within the
9872 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9873 be interpreted as relative to the base offset given in the index section.
9874 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9875 should be interpreted as relative to the base offset for .debug_line.dwo,
9876 and offsets into other debug sections obtained from DWARF attributes should
9877 also be interpreted as relative to the corresponding base offset.
9878
9879 The table of sizes begins immediately following the table of offsets.
9880 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9881 with L columns and N rows, in row-major order. Each row in the array is
9882 indexed starting from 1 (row 0 is shared by the two tables).
9883
9884 ---
9885
9886 Hash table lookup is handled the same in version 1 and 2:
9887
9888 We assume that N and M will not exceed 2^32 - 1.
9889 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9890
d2415c6c
DE
9891 Given a 64-bit compilation unit signature or a type signature S, an entry
9892 in the hash table is located as follows:
80626a55 9893
d2415c6c
DE
9894 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9895 the low-order k bits all set to 1.
80626a55 9896
d2415c6c 9897 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9898
d2415c6c
DE
9899 3) If the hash table entry at index H matches the signature, use that
9900 entry. If the hash table entry at index H is unused (all zeroes),
9901 terminate the search: the signature is not present in the table.
80626a55 9902
d2415c6c 9903 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9904
d2415c6c 9905 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9906 to stop at an unused slot or find the match. */
80626a55
DE
9907
9908/* Create a hash table to map DWO IDs to their CU/TU entry in
9909 .debug_{info,types}.dwo in DWP_FILE.
9910 Returns NULL if there isn't one.
9911 Note: This function processes DWP files only, not DWO files. */
9912
9913static struct dwp_hash_table *
9914create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9915{
9916 struct objfile *objfile = dwarf2_per_objfile->objfile;
9917 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9918 const gdb_byte *index_ptr, *index_end;
80626a55 9919 struct dwarf2_section_info *index;
73869dc2 9920 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9921 struct dwp_hash_table *htab;
9922
9923 if (is_debug_types)
9924 index = &dwp_file->sections.tu_index;
9925 else
9926 index = &dwp_file->sections.cu_index;
9927
9928 if (dwarf2_section_empty_p (index))
9929 return NULL;
9930 dwarf2_read_section (objfile, index);
9931
9932 index_ptr = index->buffer;
9933 index_end = index_ptr + index->size;
9934
9935 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9936 index_ptr += 4;
9937 if (version == 2)
9938 nr_columns = read_4_bytes (dbfd, index_ptr);
9939 else
9940 nr_columns = 0;
9941 index_ptr += 4;
80626a55
DE
9942 nr_units = read_4_bytes (dbfd, index_ptr);
9943 index_ptr += 4;
9944 nr_slots = read_4_bytes (dbfd, index_ptr);
9945 index_ptr += 4;
9946
73869dc2 9947 if (version != 1 && version != 2)
80626a55 9948 {
21aa081e 9949 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9950 " [in module %s]"),
21aa081e 9951 pulongest (version), dwp_file->name);
80626a55
DE
9952 }
9953 if (nr_slots != (nr_slots & -nr_slots))
9954 {
21aa081e 9955 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9956 " is not power of 2 [in module %s]"),
21aa081e 9957 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9958 }
9959
9960 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9961 htab->version = version;
9962 htab->nr_columns = nr_columns;
80626a55
DE
9963 htab->nr_units = nr_units;
9964 htab->nr_slots = nr_slots;
9965 htab->hash_table = index_ptr;
9966 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9967
9968 /* Exit early if the table is empty. */
9969 if (nr_slots == 0 || nr_units == 0
9970 || (version == 2 && nr_columns == 0))
9971 {
9972 /* All must be zero. */
9973 if (nr_slots != 0 || nr_units != 0
9974 || (version == 2 && nr_columns != 0))
9975 {
9976 complaint (&symfile_complaints,
9977 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9978 " all zero [in modules %s]"),
9979 dwp_file->name);
9980 }
9981 return htab;
9982 }
9983
9984 if (version == 1)
9985 {
9986 htab->section_pool.v1.indices =
9987 htab->unit_table + sizeof (uint32_t) * nr_slots;
9988 /* It's harder to decide whether the section is too small in v1.
9989 V1 is deprecated anyway so we punt. */
9990 }
9991 else
9992 {
9993 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9994 int *ids = htab->section_pool.v2.section_ids;
9995 /* Reverse map for error checking. */
9996 int ids_seen[DW_SECT_MAX + 1];
9997 int i;
9998
9999 if (nr_columns < 2)
10000 {
10001 error (_("Dwarf Error: bad DWP hash table, too few columns"
10002 " in section table [in module %s]"),
10003 dwp_file->name);
10004 }
10005 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10006 {
10007 error (_("Dwarf Error: bad DWP hash table, too many columns"
10008 " in section table [in module %s]"),
10009 dwp_file->name);
10010 }
10011 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10012 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10013 for (i = 0; i < nr_columns; ++i)
10014 {
10015 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10016
10017 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10018 {
10019 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10020 " in section table [in module %s]"),
10021 id, dwp_file->name);
10022 }
10023 if (ids_seen[id] != -1)
10024 {
10025 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10026 " id %d in section table [in module %s]"),
10027 id, dwp_file->name);
10028 }
10029 ids_seen[id] = i;
10030 ids[i] = id;
10031 }
10032 /* Must have exactly one info or types section. */
10033 if (((ids_seen[DW_SECT_INFO] != -1)
10034 + (ids_seen[DW_SECT_TYPES] != -1))
10035 != 1)
10036 {
10037 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10038 " DWO info/types section [in module %s]"),
10039 dwp_file->name);
10040 }
10041 /* Must have an abbrev section. */
10042 if (ids_seen[DW_SECT_ABBREV] == -1)
10043 {
10044 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10045 " section [in module %s]"),
10046 dwp_file->name);
10047 }
10048 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10049 htab->section_pool.v2.sizes =
10050 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10051 * nr_units * nr_columns);
10052 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10053 * nr_units * nr_columns))
10054 > index_end)
10055 {
10056 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10057 " [in module %s]"),
10058 dwp_file->name);
10059 }
10060 }
80626a55
DE
10061
10062 return htab;
10063}
10064
10065/* Update SECTIONS with the data from SECTP.
10066
10067 This function is like the other "locate" section routines that are
10068 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10069 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10070
10071 The result is non-zero for success, or zero if an error was found. */
10072
10073static int
73869dc2
DE
10074locate_v1_virtual_dwo_sections (asection *sectp,
10075 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10076{
10077 const struct dwop_section_names *names = &dwop_section_names;
10078
10079 if (section_is_p (sectp->name, &names->abbrev_dwo))
10080 {
10081 /* There can be only one. */
049412e3 10082 if (sections->abbrev.s.section != NULL)
80626a55 10083 return 0;
049412e3 10084 sections->abbrev.s.section = sectp;
80626a55
DE
10085 sections->abbrev.size = bfd_get_section_size (sectp);
10086 }
10087 else if (section_is_p (sectp->name, &names->info_dwo)
10088 || section_is_p (sectp->name, &names->types_dwo))
10089 {
10090 /* There can be only one. */
049412e3 10091 if (sections->info_or_types.s.section != NULL)
80626a55 10092 return 0;
049412e3 10093 sections->info_or_types.s.section = sectp;
80626a55
DE
10094 sections->info_or_types.size = bfd_get_section_size (sectp);
10095 }
10096 else if (section_is_p (sectp->name, &names->line_dwo))
10097 {
10098 /* There can be only one. */
049412e3 10099 if (sections->line.s.section != NULL)
80626a55 10100 return 0;
049412e3 10101 sections->line.s.section = sectp;
80626a55
DE
10102 sections->line.size = bfd_get_section_size (sectp);
10103 }
10104 else if (section_is_p (sectp->name, &names->loc_dwo))
10105 {
10106 /* There can be only one. */
049412e3 10107 if (sections->loc.s.section != NULL)
80626a55 10108 return 0;
049412e3 10109 sections->loc.s.section = sectp;
80626a55
DE
10110 sections->loc.size = bfd_get_section_size (sectp);
10111 }
10112 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10113 {
10114 /* There can be only one. */
049412e3 10115 if (sections->macinfo.s.section != NULL)
80626a55 10116 return 0;
049412e3 10117 sections->macinfo.s.section = sectp;
80626a55
DE
10118 sections->macinfo.size = bfd_get_section_size (sectp);
10119 }
10120 else if (section_is_p (sectp->name, &names->macro_dwo))
10121 {
10122 /* There can be only one. */
049412e3 10123 if (sections->macro.s.section != NULL)
80626a55 10124 return 0;
049412e3 10125 sections->macro.s.section = sectp;
80626a55
DE
10126 sections->macro.size = bfd_get_section_size (sectp);
10127 }
10128 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10129 {
10130 /* There can be only one. */
049412e3 10131 if (sections->str_offsets.s.section != NULL)
80626a55 10132 return 0;
049412e3 10133 sections->str_offsets.s.section = sectp;
80626a55
DE
10134 sections->str_offsets.size = bfd_get_section_size (sectp);
10135 }
10136 else
10137 {
10138 /* No other kind of section is valid. */
10139 return 0;
10140 }
10141
10142 return 1;
10143}
10144
73869dc2
DE
10145/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10146 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10147 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10148 This is for DWP version 1 files. */
80626a55
DE
10149
10150static struct dwo_unit *
73869dc2
DE
10151create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10152 uint32_t unit_index,
10153 const char *comp_dir,
10154 ULONGEST signature, int is_debug_types)
80626a55
DE
10155{
10156 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10157 const struct dwp_hash_table *dwp_htab =
10158 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10159 bfd *dbfd = dwp_file->dbfd;
10160 const char *kind = is_debug_types ? "TU" : "CU";
10161 struct dwo_file *dwo_file;
10162 struct dwo_unit *dwo_unit;
73869dc2 10163 struct virtual_v1_dwo_sections sections;
80626a55
DE
10164 void **dwo_file_slot;
10165 char *virtual_dwo_name;
80626a55
DE
10166 struct cleanup *cleanups;
10167 int i;
10168
73869dc2
DE
10169 gdb_assert (dwp_file->version == 1);
10170
b4f54984 10171 if (dwarf_read_debug)
80626a55 10172 {
73869dc2 10173 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10174 kind,
73869dc2 10175 pulongest (unit_index), hex_string (signature),
80626a55
DE
10176 dwp_file->name);
10177 }
10178
19ac8c2e 10179 /* Fetch the sections of this DWO unit.
80626a55
DE
10180 Put a limit on the number of sections we look for so that bad data
10181 doesn't cause us to loop forever. */
10182
73869dc2 10183#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10184 (1 /* .debug_info or .debug_types */ \
10185 + 1 /* .debug_abbrev */ \
10186 + 1 /* .debug_line */ \
10187 + 1 /* .debug_loc */ \
10188 + 1 /* .debug_str_offsets */ \
19ac8c2e 10189 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10190 + 1 /* trailing zero */)
10191
10192 memset (&sections, 0, sizeof (sections));
10193 cleanups = make_cleanup (null_cleanup, 0);
10194
73869dc2 10195 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10196 {
10197 asection *sectp;
10198 uint32_t section_nr =
10199 read_4_bytes (dbfd,
73869dc2
DE
10200 dwp_htab->section_pool.v1.indices
10201 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10202
10203 if (section_nr == 0)
10204 break;
10205 if (section_nr >= dwp_file->num_sections)
10206 {
10207 error (_("Dwarf Error: bad DWP hash table, section number too large"
10208 " [in module %s]"),
10209 dwp_file->name);
10210 }
10211
10212 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10213 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10214 {
10215 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10216 " [in module %s]"),
10217 dwp_file->name);
10218 }
10219 }
10220
10221 if (i < 2
a32a8923
DE
10222 || dwarf2_section_empty_p (&sections.info_or_types)
10223 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10224 {
10225 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10226 " [in module %s]"),
10227 dwp_file->name);
10228 }
73869dc2 10229 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10230 {
10231 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10232 " [in module %s]"),
10233 dwp_file->name);
10234 }
10235
10236 /* It's easier for the rest of the code if we fake a struct dwo_file and
10237 have dwo_unit "live" in that. At least for now.
10238
10239 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10240 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10241 file, we can combine them back into a virtual DWO file to save space
10242 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10243 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10244
2792b94d
PM
10245 virtual_dwo_name =
10246 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10247 get_section_id (&sections.abbrev),
10248 get_section_id (&sections.line),
10249 get_section_id (&sections.loc),
10250 get_section_id (&sections.str_offsets));
80626a55
DE
10251 make_cleanup (xfree, virtual_dwo_name);
10252 /* Can we use an existing virtual DWO file? */
0ac5b59e 10253 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10254 /* Create one if necessary. */
10255 if (*dwo_file_slot == NULL)
10256 {
b4f54984 10257 if (dwarf_read_debug)
80626a55
DE
10258 {
10259 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10260 virtual_dwo_name);
10261 }
10262 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10263 dwo_file->dwo_name
10264 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10265 virtual_dwo_name,
10266 strlen (virtual_dwo_name));
0ac5b59e 10267 dwo_file->comp_dir = comp_dir;
80626a55
DE
10268 dwo_file->sections.abbrev = sections.abbrev;
10269 dwo_file->sections.line = sections.line;
10270 dwo_file->sections.loc = sections.loc;
10271 dwo_file->sections.macinfo = sections.macinfo;
10272 dwo_file->sections.macro = sections.macro;
10273 dwo_file->sections.str_offsets = sections.str_offsets;
10274 /* The "str" section is global to the entire DWP file. */
10275 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10276 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10277 there's no need to record it in dwo_file.
10278 Also, we can't simply record type sections in dwo_file because
10279 we record a pointer into the vector in dwo_unit. As we collect more
10280 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10281 for it, invalidating all copies of pointers into the previous
10282 contents. */
80626a55
DE
10283 *dwo_file_slot = dwo_file;
10284 }
10285 else
10286 {
b4f54984 10287 if (dwarf_read_debug)
80626a55
DE
10288 {
10289 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10290 virtual_dwo_name);
10291 }
9a3c8263 10292 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10293 }
10294 do_cleanups (cleanups);
10295
10296 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10297 dwo_unit->dwo_file = dwo_file;
10298 dwo_unit->signature = signature;
8d749320
SM
10299 dwo_unit->section =
10300 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10301 *dwo_unit->section = sections.info_or_types;
57d63ce2 10302 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10303
10304 return dwo_unit;
10305}
10306
73869dc2
DE
10307/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10308 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10309 piece within that section used by a TU/CU, return a virtual section
10310 of just that piece. */
10311
10312static struct dwarf2_section_info
10313create_dwp_v2_section (struct dwarf2_section_info *section,
10314 bfd_size_type offset, bfd_size_type size)
10315{
10316 struct dwarf2_section_info result;
10317 asection *sectp;
10318
10319 gdb_assert (section != NULL);
10320 gdb_assert (!section->is_virtual);
10321
10322 memset (&result, 0, sizeof (result));
10323 result.s.containing_section = section;
10324 result.is_virtual = 1;
10325
10326 if (size == 0)
10327 return result;
10328
10329 sectp = get_section_bfd_section (section);
10330
10331 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10332 bounds of the real section. This is a pretty-rare event, so just
10333 flag an error (easier) instead of a warning and trying to cope. */
10334 if (sectp == NULL
10335 || offset + size > bfd_get_section_size (sectp))
10336 {
10337 bfd *abfd = sectp->owner;
10338
10339 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10340 " in section %s [in module %s]"),
10341 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10342 objfile_name (dwarf2_per_objfile->objfile));
10343 }
10344
10345 result.virtual_offset = offset;
10346 result.size = size;
10347 return result;
10348}
10349
10350/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10351 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10352 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10353 This is for DWP version 2 files. */
10354
10355static struct dwo_unit *
10356create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10357 uint32_t unit_index,
10358 const char *comp_dir,
10359 ULONGEST signature, int is_debug_types)
10360{
10361 struct objfile *objfile = dwarf2_per_objfile->objfile;
10362 const struct dwp_hash_table *dwp_htab =
10363 is_debug_types ? dwp_file->tus : dwp_file->cus;
10364 bfd *dbfd = dwp_file->dbfd;
10365 const char *kind = is_debug_types ? "TU" : "CU";
10366 struct dwo_file *dwo_file;
10367 struct dwo_unit *dwo_unit;
10368 struct virtual_v2_dwo_sections sections;
10369 void **dwo_file_slot;
10370 char *virtual_dwo_name;
73869dc2
DE
10371 struct cleanup *cleanups;
10372 int i;
10373
10374 gdb_assert (dwp_file->version == 2);
10375
b4f54984 10376 if (dwarf_read_debug)
73869dc2
DE
10377 {
10378 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10379 kind,
10380 pulongest (unit_index), hex_string (signature),
10381 dwp_file->name);
10382 }
10383
10384 /* Fetch the section offsets of this DWO unit. */
10385
10386 memset (&sections, 0, sizeof (sections));
10387 cleanups = make_cleanup (null_cleanup, 0);
10388
10389 for (i = 0; i < dwp_htab->nr_columns; ++i)
10390 {
10391 uint32_t offset = read_4_bytes (dbfd,
10392 dwp_htab->section_pool.v2.offsets
10393 + (((unit_index - 1) * dwp_htab->nr_columns
10394 + i)
10395 * sizeof (uint32_t)));
10396 uint32_t size = read_4_bytes (dbfd,
10397 dwp_htab->section_pool.v2.sizes
10398 + (((unit_index - 1) * dwp_htab->nr_columns
10399 + i)
10400 * sizeof (uint32_t)));
10401
10402 switch (dwp_htab->section_pool.v2.section_ids[i])
10403 {
10404 case DW_SECT_INFO:
10405 case DW_SECT_TYPES:
10406 sections.info_or_types_offset = offset;
10407 sections.info_or_types_size = size;
10408 break;
10409 case DW_SECT_ABBREV:
10410 sections.abbrev_offset = offset;
10411 sections.abbrev_size = size;
10412 break;
10413 case DW_SECT_LINE:
10414 sections.line_offset = offset;
10415 sections.line_size = size;
10416 break;
10417 case DW_SECT_LOC:
10418 sections.loc_offset = offset;
10419 sections.loc_size = size;
10420 break;
10421 case DW_SECT_STR_OFFSETS:
10422 sections.str_offsets_offset = offset;
10423 sections.str_offsets_size = size;
10424 break;
10425 case DW_SECT_MACINFO:
10426 sections.macinfo_offset = offset;
10427 sections.macinfo_size = size;
10428 break;
10429 case DW_SECT_MACRO:
10430 sections.macro_offset = offset;
10431 sections.macro_size = size;
10432 break;
10433 }
10434 }
10435
10436 /* It's easier for the rest of the code if we fake a struct dwo_file and
10437 have dwo_unit "live" in that. At least for now.
10438
10439 The DWP file can be made up of a random collection of CUs and TUs.
10440 However, for each CU + set of TUs that came from the same original DWO
10441 file, we can combine them back into a virtual DWO file to save space
10442 (fewer struct dwo_file objects to allocate). Remember that for really
10443 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10444
10445 virtual_dwo_name =
10446 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10447 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10448 (long) (sections.line_size ? sections.line_offset : 0),
10449 (long) (sections.loc_size ? sections.loc_offset : 0),
10450 (long) (sections.str_offsets_size
10451 ? sections.str_offsets_offset : 0));
10452 make_cleanup (xfree, virtual_dwo_name);
10453 /* Can we use an existing virtual DWO file? */
10454 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10455 /* Create one if necessary. */
10456 if (*dwo_file_slot == NULL)
10457 {
b4f54984 10458 if (dwarf_read_debug)
73869dc2
DE
10459 {
10460 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10461 virtual_dwo_name);
10462 }
10463 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10464 dwo_file->dwo_name
10465 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10466 virtual_dwo_name,
10467 strlen (virtual_dwo_name));
73869dc2
DE
10468 dwo_file->comp_dir = comp_dir;
10469 dwo_file->sections.abbrev =
10470 create_dwp_v2_section (&dwp_file->sections.abbrev,
10471 sections.abbrev_offset, sections.abbrev_size);
10472 dwo_file->sections.line =
10473 create_dwp_v2_section (&dwp_file->sections.line,
10474 sections.line_offset, sections.line_size);
10475 dwo_file->sections.loc =
10476 create_dwp_v2_section (&dwp_file->sections.loc,
10477 sections.loc_offset, sections.loc_size);
10478 dwo_file->sections.macinfo =
10479 create_dwp_v2_section (&dwp_file->sections.macinfo,
10480 sections.macinfo_offset, sections.macinfo_size);
10481 dwo_file->sections.macro =
10482 create_dwp_v2_section (&dwp_file->sections.macro,
10483 sections.macro_offset, sections.macro_size);
10484 dwo_file->sections.str_offsets =
10485 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10486 sections.str_offsets_offset,
10487 sections.str_offsets_size);
10488 /* The "str" section is global to the entire DWP file. */
10489 dwo_file->sections.str = dwp_file->sections.str;
10490 /* The info or types section is assigned below to dwo_unit,
10491 there's no need to record it in dwo_file.
10492 Also, we can't simply record type sections in dwo_file because
10493 we record a pointer into the vector in dwo_unit. As we collect more
10494 types we'll grow the vector and eventually have to reallocate space
10495 for it, invalidating all copies of pointers into the previous
10496 contents. */
10497 *dwo_file_slot = dwo_file;
10498 }
10499 else
10500 {
b4f54984 10501 if (dwarf_read_debug)
73869dc2
DE
10502 {
10503 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10504 virtual_dwo_name);
10505 }
9a3c8263 10506 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10507 }
10508 do_cleanups (cleanups);
10509
10510 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10511 dwo_unit->dwo_file = dwo_file;
10512 dwo_unit->signature = signature;
8d749320
SM
10513 dwo_unit->section =
10514 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10515 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10516 ? &dwp_file->sections.types
10517 : &dwp_file->sections.info,
10518 sections.info_or_types_offset,
10519 sections.info_or_types_size);
10520 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10521
10522 return dwo_unit;
10523}
10524
57d63ce2
DE
10525/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10526 Returns NULL if the signature isn't found. */
80626a55
DE
10527
10528static struct dwo_unit *
57d63ce2
DE
10529lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10530 ULONGEST signature, int is_debug_types)
80626a55 10531{
57d63ce2
DE
10532 const struct dwp_hash_table *dwp_htab =
10533 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10534 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10535 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10536 uint32_t hash = signature & mask;
10537 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10538 unsigned int i;
10539 void **slot;
870f88f7 10540 struct dwo_unit find_dwo_cu;
80626a55
DE
10541
10542 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10543 find_dwo_cu.signature = signature;
19ac8c2e
DE
10544 slot = htab_find_slot (is_debug_types
10545 ? dwp_file->loaded_tus
10546 : dwp_file->loaded_cus,
10547 &find_dwo_cu, INSERT);
80626a55
DE
10548
10549 if (*slot != NULL)
9a3c8263 10550 return (struct dwo_unit *) *slot;
80626a55
DE
10551
10552 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10553 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10554 {
10555 ULONGEST signature_in_table;
10556
10557 signature_in_table =
57d63ce2 10558 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10559 if (signature_in_table == signature)
10560 {
57d63ce2
DE
10561 uint32_t unit_index =
10562 read_4_bytes (dbfd,
10563 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10564
73869dc2
DE
10565 if (dwp_file->version == 1)
10566 {
10567 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10568 comp_dir, signature,
10569 is_debug_types);
10570 }
10571 else
10572 {
10573 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10574 comp_dir, signature,
10575 is_debug_types);
10576 }
9a3c8263 10577 return (struct dwo_unit *) *slot;
80626a55
DE
10578 }
10579 if (signature_in_table == 0)
10580 return NULL;
10581 hash = (hash + hash2) & mask;
10582 }
10583
10584 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10585 " [in module %s]"),
10586 dwp_file->name);
10587}
10588
ab5088bf 10589/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10590 Open the file specified by FILE_NAME and hand it off to BFD for
10591 preliminary analysis. Return a newly initialized bfd *, which
10592 includes a canonicalized copy of FILE_NAME.
80626a55 10593 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10594 SEARCH_CWD is true if the current directory is to be searched.
10595 It will be searched before debug-file-directory.
13aaf454
DE
10596 If successful, the file is added to the bfd include table of the
10597 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10598 If unable to find/open the file, return NULL.
3019eac3
DE
10599 NOTE: This function is derived from symfile_bfd_open. */
10600
192b62ce 10601static gdb_bfd_ref_ptr
6ac97d4c 10602try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10603{
80626a55 10604 int desc, flags;
3019eac3 10605 char *absolute_name;
9c02c129
DE
10606 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10607 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10608 to debug_file_directory. */
10609 char *search_path;
10610 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10611
6ac97d4c
DE
10612 if (search_cwd)
10613 {
10614 if (*debug_file_directory != '\0')
10615 search_path = concat (".", dirname_separator_string,
b36cec19 10616 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10617 else
10618 search_path = xstrdup (".");
10619 }
9c02c129 10620 else
6ac97d4c 10621 search_path = xstrdup (debug_file_directory);
3019eac3 10622
492c0ab7 10623 flags = OPF_RETURN_REALPATH;
80626a55
DE
10624 if (is_dwp)
10625 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10626 desc = openp (search_path, flags, file_name,
3019eac3 10627 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10628 xfree (search_path);
3019eac3
DE
10629 if (desc < 0)
10630 return NULL;
10631
192b62ce 10632 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10633 xfree (absolute_name);
9c02c129
DE
10634 if (sym_bfd == NULL)
10635 return NULL;
192b62ce 10636 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10637
192b62ce
TT
10638 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10639 return NULL;
3019eac3 10640
13aaf454
DE
10641 /* Success. Record the bfd as having been included by the objfile's bfd.
10642 This is important because things like demangled_names_hash lives in the
10643 objfile's per_bfd space and may have references to things like symbol
10644 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10645 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10646
3019eac3
DE
10647 return sym_bfd;
10648}
10649
ab5088bf 10650/* Try to open DWO file FILE_NAME.
3019eac3
DE
10651 COMP_DIR is the DW_AT_comp_dir attribute.
10652 The result is the bfd handle of the file.
10653 If there is a problem finding or opening the file, return NULL.
10654 Upon success, the canonicalized path of the file is stored in the bfd,
10655 same as symfile_bfd_open. */
10656
192b62ce 10657static gdb_bfd_ref_ptr
ab5088bf 10658open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10659{
80626a55 10660 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10661 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10662
10663 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10664
10665 if (comp_dir != NULL)
10666 {
b36cec19
PA
10667 char *path_to_try = concat (comp_dir, SLASH_STRING,
10668 file_name, (char *) NULL);
3019eac3
DE
10669
10670 /* NOTE: If comp_dir is a relative path, this will also try the
10671 search path, which seems useful. */
192b62ce
TT
10672 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10673 1 /*search_cwd*/));
3019eac3
DE
10674 xfree (path_to_try);
10675 if (abfd != NULL)
10676 return abfd;
10677 }
10678
10679 /* That didn't work, try debug-file-directory, which, despite its name,
10680 is a list of paths. */
10681
10682 if (*debug_file_directory == '\0')
10683 return NULL;
10684
6ac97d4c 10685 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10686}
10687
80626a55
DE
10688/* This function is mapped across the sections and remembers the offset and
10689 size of each of the DWO debugging sections we are interested in. */
10690
10691static void
10692dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10693{
9a3c8263 10694 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10695 const struct dwop_section_names *names = &dwop_section_names;
10696
10697 if (section_is_p (sectp->name, &names->abbrev_dwo))
10698 {
049412e3 10699 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10700 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10701 }
10702 else if (section_is_p (sectp->name, &names->info_dwo))
10703 {
049412e3 10704 dwo_sections->info.s.section = sectp;
80626a55
DE
10705 dwo_sections->info.size = bfd_get_section_size (sectp);
10706 }
10707 else if (section_is_p (sectp->name, &names->line_dwo))
10708 {
049412e3 10709 dwo_sections->line.s.section = sectp;
80626a55
DE
10710 dwo_sections->line.size = bfd_get_section_size (sectp);
10711 }
10712 else if (section_is_p (sectp->name, &names->loc_dwo))
10713 {
049412e3 10714 dwo_sections->loc.s.section = sectp;
80626a55
DE
10715 dwo_sections->loc.size = bfd_get_section_size (sectp);
10716 }
10717 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10718 {
049412e3 10719 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10720 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10721 }
10722 else if (section_is_p (sectp->name, &names->macro_dwo))
10723 {
049412e3 10724 dwo_sections->macro.s.section = sectp;
80626a55
DE
10725 dwo_sections->macro.size = bfd_get_section_size (sectp);
10726 }
10727 else if (section_is_p (sectp->name, &names->str_dwo))
10728 {
049412e3 10729 dwo_sections->str.s.section = sectp;
80626a55
DE
10730 dwo_sections->str.size = bfd_get_section_size (sectp);
10731 }
10732 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10733 {
049412e3 10734 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10735 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10736 }
10737 else if (section_is_p (sectp->name, &names->types_dwo))
10738 {
10739 struct dwarf2_section_info type_section;
10740
10741 memset (&type_section, 0, sizeof (type_section));
049412e3 10742 type_section.s.section = sectp;
80626a55
DE
10743 type_section.size = bfd_get_section_size (sectp);
10744 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10745 &type_section);
10746 }
10747}
10748
ab5088bf 10749/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10750 by PER_CU. This is for the non-DWP case.
80626a55 10751 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10752
10753static struct dwo_file *
0ac5b59e
DE
10754open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10755 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10756{
10757 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10758 struct dwo_file *dwo_file;
3019eac3
DE
10759 struct cleanup *cleanups;
10760
192b62ce 10761 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10762 if (dbfd == NULL)
10763 {
b4f54984 10764 if (dwarf_read_debug)
80626a55
DE
10765 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10766 return NULL;
10767 }
10768 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10769 dwo_file->dwo_name = dwo_name;
10770 dwo_file->comp_dir = comp_dir;
192b62ce 10771 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10772
10773 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10774
192b62ce
TT
10775 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10776 &dwo_file->sections);
3019eac3 10777
19c3d4c9 10778 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3 10779
78d4d2c5
JK
10780 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10781 dwo_file->tus);
3019eac3
DE
10782
10783 discard_cleanups (cleanups);
10784
b4f54984 10785 if (dwarf_read_debug)
80626a55
DE
10786 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10787
3019eac3
DE
10788 return dwo_file;
10789}
10790
80626a55 10791/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10792 size of each of the DWP debugging sections common to version 1 and 2 that
10793 we are interested in. */
3019eac3 10794
80626a55 10795static void
73869dc2
DE
10796dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10797 void *dwp_file_ptr)
3019eac3 10798{
9a3c8263 10799 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10800 const struct dwop_section_names *names = &dwop_section_names;
10801 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10802
80626a55 10803 /* Record the ELF section number for later lookup: this is what the
73869dc2 10804 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10805 gdb_assert (elf_section_nr < dwp_file->num_sections);
10806 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10807
80626a55
DE
10808 /* Look for specific sections that we need. */
10809 if (section_is_p (sectp->name, &names->str_dwo))
10810 {
049412e3 10811 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10812 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10813 }
10814 else if (section_is_p (sectp->name, &names->cu_index))
10815 {
049412e3 10816 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10817 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10818 }
10819 else if (section_is_p (sectp->name, &names->tu_index))
10820 {
049412e3 10821 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10822 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10823 }
10824}
3019eac3 10825
73869dc2
DE
10826/* This function is mapped across the sections and remembers the offset and
10827 size of each of the DWP version 2 debugging sections that we are interested
10828 in. This is split into a separate function because we don't know if we
10829 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10830
10831static void
10832dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10833{
9a3c8263 10834 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10835 const struct dwop_section_names *names = &dwop_section_names;
10836 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10837
10838 /* Record the ELF section number for later lookup: this is what the
10839 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10840 gdb_assert (elf_section_nr < dwp_file->num_sections);
10841 dwp_file->elf_sections[elf_section_nr] = sectp;
10842
10843 /* Look for specific sections that we need. */
10844 if (section_is_p (sectp->name, &names->abbrev_dwo))
10845 {
049412e3 10846 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10847 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10848 }
10849 else if (section_is_p (sectp->name, &names->info_dwo))
10850 {
049412e3 10851 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10852 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10853 }
10854 else if (section_is_p (sectp->name, &names->line_dwo))
10855 {
049412e3 10856 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10857 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10858 }
10859 else if (section_is_p (sectp->name, &names->loc_dwo))
10860 {
049412e3 10861 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10862 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10863 }
10864 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10865 {
049412e3 10866 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10867 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10868 }
10869 else if (section_is_p (sectp->name, &names->macro_dwo))
10870 {
049412e3 10871 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
10872 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10873 }
10874 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10875 {
049412e3 10876 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
10877 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10878 }
10879 else if (section_is_p (sectp->name, &names->types_dwo))
10880 {
049412e3 10881 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10882 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10883 }
10884}
10885
80626a55 10886/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10887
80626a55
DE
10888static hashval_t
10889hash_dwp_loaded_cutus (const void *item)
10890{
9a3c8263 10891 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10892
80626a55
DE
10893 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10894 return dwo_unit->signature;
3019eac3
DE
10895}
10896
80626a55 10897/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10898
80626a55
DE
10899static int
10900eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10901{
9a3c8263
SM
10902 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10903 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10904
80626a55
DE
10905 return dua->signature == dub->signature;
10906}
3019eac3 10907
80626a55 10908/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10909
80626a55
DE
10910static htab_t
10911allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10912{
10913 return htab_create_alloc_ex (3,
10914 hash_dwp_loaded_cutus,
10915 eq_dwp_loaded_cutus,
10916 NULL,
10917 &objfile->objfile_obstack,
10918 hashtab_obstack_allocate,
10919 dummy_obstack_deallocate);
10920}
3019eac3 10921
ab5088bf
DE
10922/* Try to open DWP file FILE_NAME.
10923 The result is the bfd handle of the file.
10924 If there is a problem finding or opening the file, return NULL.
10925 Upon success, the canonicalized path of the file is stored in the bfd,
10926 same as symfile_bfd_open. */
10927
192b62ce 10928static gdb_bfd_ref_ptr
ab5088bf
DE
10929open_dwp_file (const char *file_name)
10930{
192b62ce
TT
10931 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10932 1 /*search_cwd*/));
6ac97d4c
DE
10933 if (abfd != NULL)
10934 return abfd;
10935
10936 /* Work around upstream bug 15652.
10937 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10938 [Whether that's a "bug" is debatable, but it is getting in our way.]
10939 We have no real idea where the dwp file is, because gdb's realpath-ing
10940 of the executable's path may have discarded the needed info.
10941 [IWBN if the dwp file name was recorded in the executable, akin to
10942 .gnu_debuglink, but that doesn't exist yet.]
10943 Strip the directory from FILE_NAME and search again. */
10944 if (*debug_file_directory != '\0')
10945 {
10946 /* Don't implicitly search the current directory here.
10947 If the user wants to search "." to handle this case,
10948 it must be added to debug-file-directory. */
10949 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10950 0 /*search_cwd*/);
10951 }
10952
10953 return NULL;
ab5088bf
DE
10954}
10955
80626a55
DE
10956/* Initialize the use of the DWP file for the current objfile.
10957 By convention the name of the DWP file is ${objfile}.dwp.
10958 The result is NULL if it can't be found. */
a766d390 10959
80626a55 10960static struct dwp_file *
ab5088bf 10961open_and_init_dwp_file (void)
80626a55
DE
10962{
10963 struct objfile *objfile = dwarf2_per_objfile->objfile;
10964 struct dwp_file *dwp_file;
80626a55 10965
82bf32bc
JK
10966 /* Try to find first .dwp for the binary file before any symbolic links
10967 resolving. */
6c447423
DE
10968
10969 /* If the objfile is a debug file, find the name of the real binary
10970 file and get the name of dwp file from there. */
d721ba37 10971 std::string dwp_name;
6c447423
DE
10972 if (objfile->separate_debug_objfile_backlink != NULL)
10973 {
10974 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10975 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 10976
d721ba37 10977 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
10978 }
10979 else
d721ba37
PA
10980 dwp_name = objfile->original_name;
10981
10982 dwp_name += ".dwp";
80626a55 10983
d721ba37 10984 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
10985 if (dbfd == NULL
10986 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10987 {
10988 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
10989 dwp_name = objfile_name (objfile);
10990 dwp_name += ".dwp";
10991 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
10992 }
10993
80626a55
DE
10994 if (dbfd == NULL)
10995 {
b4f54984 10996 if (dwarf_read_debug)
d721ba37 10997 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 10998 return NULL;
3019eac3 10999 }
80626a55 11000 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
11001 dwp_file->name = bfd_get_filename (dbfd.get ());
11002 dwp_file->dbfd = dbfd.release ();
c906108c 11003
80626a55 11004 /* +1: section 0 is unused */
192b62ce 11005 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
11006 dwp_file->elf_sections =
11007 OBSTACK_CALLOC (&objfile->objfile_obstack,
11008 dwp_file->num_sections, asection *);
11009
192b62ce
TT
11010 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11011 dwp_file);
80626a55
DE
11012
11013 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11014
11015 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11016
73869dc2
DE
11017 /* The DWP file version is stored in the hash table. Oh well. */
11018 if (dwp_file->cus->version != dwp_file->tus->version)
11019 {
11020 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 11021 pretty bizarre. We use pulongest here because that's the established
4d65956b 11022 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
11023 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11024 " TU version %s [in DWP file %s]"),
11025 pulongest (dwp_file->cus->version),
d721ba37 11026 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2
DE
11027 }
11028 dwp_file->version = dwp_file->cus->version;
11029
11030 if (dwp_file->version == 2)
192b62ce
TT
11031 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11032 dwp_file);
73869dc2 11033
19ac8c2e
DE
11034 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11035 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 11036
b4f54984 11037 if (dwarf_read_debug)
80626a55
DE
11038 {
11039 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11040 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
11041 " %s CUs, %s TUs\n",
11042 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11043 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
11044 }
11045
11046 return dwp_file;
3019eac3 11047}
c906108c 11048
ab5088bf
DE
11049/* Wrapper around open_and_init_dwp_file, only open it once. */
11050
11051static struct dwp_file *
11052get_dwp_file (void)
11053{
11054 if (! dwarf2_per_objfile->dwp_checked)
11055 {
11056 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11057 dwarf2_per_objfile->dwp_checked = 1;
11058 }
11059 return dwarf2_per_objfile->dwp_file;
11060}
11061
80626a55
DE
11062/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11063 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11064 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11065 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11066 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11067
11068 This is called, for example, when wanting to read a variable with a
11069 complex location. Therefore we don't want to do file i/o for every call.
11070 Therefore we don't want to look for a DWO file on every call.
11071 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11072 then we check if we've already seen DWO_NAME, and only THEN do we check
11073 for a DWO file.
11074
1c658ad5 11075 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11076 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11077
3019eac3 11078static struct dwo_unit *
80626a55
DE
11079lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11080 const char *dwo_name, const char *comp_dir,
11081 ULONGEST signature, int is_debug_types)
3019eac3
DE
11082{
11083 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11084 const char *kind = is_debug_types ? "TU" : "CU";
11085 void **dwo_file_slot;
3019eac3 11086 struct dwo_file *dwo_file;
80626a55 11087 struct dwp_file *dwp_file;
cb1df416 11088
6a506a2d
DE
11089 /* First see if there's a DWP file.
11090 If we have a DWP file but didn't find the DWO inside it, don't
11091 look for the original DWO file. It makes gdb behave differently
11092 depending on whether one is debugging in the build tree. */
cf2c3c16 11093
ab5088bf 11094 dwp_file = get_dwp_file ();
80626a55 11095 if (dwp_file != NULL)
cf2c3c16 11096 {
80626a55
DE
11097 const struct dwp_hash_table *dwp_htab =
11098 is_debug_types ? dwp_file->tus : dwp_file->cus;
11099
11100 if (dwp_htab != NULL)
11101 {
11102 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11103 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11104 signature, is_debug_types);
80626a55
DE
11105
11106 if (dwo_cutu != NULL)
11107 {
b4f54984 11108 if (dwarf_read_debug)
80626a55
DE
11109 {
11110 fprintf_unfiltered (gdb_stdlog,
11111 "Virtual DWO %s %s found: @%s\n",
11112 kind, hex_string (signature),
11113 host_address_to_string (dwo_cutu));
11114 }
11115 return dwo_cutu;
11116 }
11117 }
11118 }
6a506a2d 11119 else
80626a55 11120 {
6a506a2d 11121 /* No DWP file, look for the DWO file. */
80626a55 11122
6a506a2d
DE
11123 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11124 if (*dwo_file_slot == NULL)
80626a55 11125 {
6a506a2d
DE
11126 /* Read in the file and build a table of the CUs/TUs it contains. */
11127 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11128 }
6a506a2d 11129 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11130 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11131
6a506a2d 11132 if (dwo_file != NULL)
19c3d4c9 11133 {
6a506a2d
DE
11134 struct dwo_unit *dwo_cutu = NULL;
11135
11136 if (is_debug_types && dwo_file->tus)
11137 {
11138 struct dwo_unit find_dwo_cutu;
11139
11140 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11141 find_dwo_cutu.signature = signature;
9a3c8263
SM
11142 dwo_cutu
11143 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
11144 }
11145 else if (!is_debug_types && dwo_file->cu)
80626a55 11146 {
6a506a2d
DE
11147 if (signature == dwo_file->cu->signature)
11148 dwo_cutu = dwo_file->cu;
11149 }
11150
11151 if (dwo_cutu != NULL)
11152 {
b4f54984 11153 if (dwarf_read_debug)
6a506a2d
DE
11154 {
11155 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11156 kind, dwo_name, hex_string (signature),
11157 host_address_to_string (dwo_cutu));
11158 }
11159 return dwo_cutu;
80626a55
DE
11160 }
11161 }
2e276125 11162 }
9cdd5dbd 11163
80626a55
DE
11164 /* We didn't find it. This could mean a dwo_id mismatch, or
11165 someone deleted the DWO/DWP file, or the search path isn't set up
11166 correctly to find the file. */
11167
b4f54984 11168 if (dwarf_read_debug)
80626a55
DE
11169 {
11170 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11171 kind, dwo_name, hex_string (signature));
11172 }
3019eac3 11173
6656a72d
DE
11174 /* This is a warning and not a complaint because it can be caused by
11175 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11176 {
11177 /* Print the name of the DWP file if we looked there, helps the user
11178 better diagnose the problem. */
11179 char *dwp_text = NULL;
11180 struct cleanup *cleanups;
11181
11182 if (dwp_file != NULL)
11183 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11184 cleanups = make_cleanup (xfree, dwp_text);
11185
11186 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11187 " [in module %s]"),
11188 kind, dwo_name, hex_string (signature),
11189 dwp_text != NULL ? dwp_text : "",
11190 this_unit->is_debug_types ? "TU" : "CU",
9c541725 11191 to_underlying (this_unit->sect_off), objfile_name (objfile));
43942612
DE
11192
11193 do_cleanups (cleanups);
11194 }
3019eac3 11195 return NULL;
5fb290d7
DJ
11196}
11197
80626a55
DE
11198/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11199 See lookup_dwo_cutu_unit for details. */
11200
11201static struct dwo_unit *
11202lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11203 const char *dwo_name, const char *comp_dir,
11204 ULONGEST signature)
11205{
11206 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11207}
11208
11209/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11210 See lookup_dwo_cutu_unit for details. */
11211
11212static struct dwo_unit *
11213lookup_dwo_type_unit (struct signatured_type *this_tu,
11214 const char *dwo_name, const char *comp_dir)
11215{
11216 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11217}
11218
89e63ee4
DE
11219/* Traversal function for queue_and_load_all_dwo_tus. */
11220
11221static int
11222queue_and_load_dwo_tu (void **slot, void *info)
11223{
11224 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11225 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11226 ULONGEST signature = dwo_unit->signature;
11227 struct signatured_type *sig_type =
11228 lookup_dwo_signatured_type (per_cu->cu, signature);
11229
11230 if (sig_type != NULL)
11231 {
11232 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11233
11234 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11235 a real dependency of PER_CU on SIG_TYPE. That is detected later
11236 while processing PER_CU. */
11237 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11238 load_full_type_unit (sig_cu);
11239 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11240 }
11241
11242 return 1;
11243}
11244
11245/* Queue all TUs contained in the DWO of PER_CU to be read in.
11246 The DWO may have the only definition of the type, though it may not be
11247 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11248 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11249
11250static void
11251queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11252{
11253 struct dwo_unit *dwo_unit;
11254 struct dwo_file *dwo_file;
11255
11256 gdb_assert (!per_cu->is_debug_types);
11257 gdb_assert (get_dwp_file () == NULL);
11258 gdb_assert (per_cu->cu != NULL);
11259
11260 dwo_unit = per_cu->cu->dwo_unit;
11261 gdb_assert (dwo_unit != NULL);
11262
11263 dwo_file = dwo_unit->dwo_file;
11264 if (dwo_file->tus != NULL)
11265 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11266}
11267
3019eac3
DE
11268/* Free all resources associated with DWO_FILE.
11269 Close the DWO file and munmap the sections.
11270 All memory should be on the objfile obstack. */
348e048f
DE
11271
11272static void
3019eac3 11273free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11274{
348e048f 11275
5c6fa7ab 11276 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11277 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11278
3019eac3
DE
11279 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11280}
348e048f 11281
3019eac3 11282/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11283
3019eac3
DE
11284static void
11285free_dwo_file_cleanup (void *arg)
11286{
11287 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11288 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11289
3019eac3
DE
11290 free_dwo_file (dwo_file, objfile);
11291}
348e048f 11292
3019eac3 11293/* Traversal function for free_dwo_files. */
2ab95328 11294
3019eac3
DE
11295static int
11296free_dwo_file_from_slot (void **slot, void *info)
11297{
11298 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11299 struct objfile *objfile = (struct objfile *) info;
348e048f 11300
3019eac3 11301 free_dwo_file (dwo_file, objfile);
348e048f 11302
3019eac3
DE
11303 return 1;
11304}
348e048f 11305
3019eac3 11306/* Free all resources associated with DWO_FILES. */
348e048f 11307
3019eac3
DE
11308static void
11309free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11310{
11311 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11312}
3019eac3
DE
11313\f
11314/* Read in various DIEs. */
348e048f 11315
d389af10
JK
11316/* qsort helper for inherit_abstract_dies. */
11317
11318static int
11319unsigned_int_compar (const void *ap, const void *bp)
11320{
11321 unsigned int a = *(unsigned int *) ap;
11322 unsigned int b = *(unsigned int *) bp;
11323
11324 return (a > b) - (b > a);
11325}
11326
11327/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11328 Inherit only the children of the DW_AT_abstract_origin DIE not being
11329 already referenced by DW_AT_abstract_origin from the children of the
11330 current DIE. */
d389af10
JK
11331
11332static void
11333inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11334{
11335 struct die_info *child_die;
11336 unsigned die_children_count;
11337 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11338 sect_offset *offsets;
11339 sect_offset *offsets_end, *offsetp;
d389af10
JK
11340 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11341 struct die_info *origin_die;
11342 /* Iterator of the ORIGIN_DIE children. */
11343 struct die_info *origin_child_die;
11344 struct cleanup *cleanups;
11345 struct attribute *attr;
cd02d79d
PA
11346 struct dwarf2_cu *origin_cu;
11347 struct pending **origin_previous_list_in_scope;
d389af10
JK
11348
11349 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11350 if (!attr)
11351 return;
11352
cd02d79d
PA
11353 /* Note that following die references may follow to a die in a
11354 different cu. */
11355
11356 origin_cu = cu;
11357 origin_die = follow_die_ref (die, attr, &origin_cu);
11358
11359 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11360 symbols in. */
11361 origin_previous_list_in_scope = origin_cu->list_in_scope;
11362 origin_cu->list_in_scope = cu->list_in_scope;
11363
edb3359d
DJ
11364 if (die->tag != origin_die->tag
11365 && !(die->tag == DW_TAG_inlined_subroutine
11366 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11367 complaint (&symfile_complaints,
11368 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9c541725
PA
11369 to_underlying (die->sect_off),
11370 to_underlying (origin_die->sect_off));
d389af10
JK
11371
11372 child_die = die->child;
11373 die_children_count = 0;
11374 while (child_die && child_die->tag)
11375 {
11376 child_die = sibling_die (child_die);
11377 die_children_count++;
11378 }
8d749320 11379 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11380 cleanups = make_cleanup (xfree, offsets);
11381
11382 offsets_end = offsets;
3ea89b92
PMR
11383 for (child_die = die->child;
11384 child_die && child_die->tag;
11385 child_die = sibling_die (child_die))
11386 {
11387 struct die_info *child_origin_die;
11388 struct dwarf2_cu *child_origin_cu;
11389
11390 /* We are trying to process concrete instance entries:
216f72a1 11391 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11392 it's not relevant to our analysis here. i.e. detecting DIEs that are
11393 present in the abstract instance but not referenced in the concrete
11394 one. */
216f72a1
JK
11395 if (child_die->tag == DW_TAG_call_site
11396 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11397 continue;
11398
c38f313d
DJ
11399 /* For each CHILD_DIE, find the corresponding child of
11400 ORIGIN_DIE. If there is more than one layer of
11401 DW_AT_abstract_origin, follow them all; there shouldn't be,
11402 but GCC versions at least through 4.4 generate this (GCC PR
11403 40573). */
3ea89b92
PMR
11404 child_origin_die = child_die;
11405 child_origin_cu = cu;
c38f313d
DJ
11406 while (1)
11407 {
cd02d79d
PA
11408 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11409 child_origin_cu);
c38f313d
DJ
11410 if (attr == NULL)
11411 break;
cd02d79d
PA
11412 child_origin_die = follow_die_ref (child_origin_die, attr,
11413 &child_origin_cu);
c38f313d
DJ
11414 }
11415
d389af10
JK
11416 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11417 counterpart may exist. */
c38f313d 11418 if (child_origin_die != child_die)
d389af10 11419 {
edb3359d
DJ
11420 if (child_die->tag != child_origin_die->tag
11421 && !(child_die->tag == DW_TAG_inlined_subroutine
11422 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11423 complaint (&symfile_complaints,
11424 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11425 "different tags"),
11426 to_underlying (child_die->sect_off),
11427 to_underlying (child_origin_die->sect_off));
c38f313d
DJ
11428 if (child_origin_die->parent != origin_die)
11429 complaint (&symfile_complaints,
11430 _("Child DIE 0x%x and its abstract origin 0x%x have "
9c541725
PA
11431 "different parents"),
11432 to_underlying (child_die->sect_off),
11433 to_underlying (child_origin_die->sect_off));
c38f313d 11434 else
9c541725 11435 *offsets_end++ = child_origin_die->sect_off;
d389af10 11436 }
d389af10
JK
11437 }
11438 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11439 unsigned_int_compar);
11440 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9c541725 11441 if (offsetp[-1] == *offsetp)
3e43a32a
MS
11442 complaint (&symfile_complaints,
11443 _("Multiple children of DIE 0x%x refer "
11444 "to DIE 0x%x as their abstract origin"),
9c541725 11445 to_underlying (die->sect_off), to_underlying (*offsetp));
d389af10
JK
11446
11447 offsetp = offsets;
11448 origin_child_die = origin_die->child;
11449 while (origin_child_die && origin_child_die->tag)
11450 {
11451 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 11452 while (offsetp < offsets_end
9c541725 11453 && *offsetp < origin_child_die->sect_off)
d389af10 11454 offsetp++;
b64f50a1 11455 if (offsetp >= offsets_end
9c541725 11456 || *offsetp > origin_child_die->sect_off)
d389af10 11457 {
adde2bff
DE
11458 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11459 Check whether we're already processing ORIGIN_CHILD_DIE.
11460 This can happen with mutually referenced abstract_origins.
11461 PR 16581. */
11462 if (!origin_child_die->in_process)
11463 process_die (origin_child_die, origin_cu);
d389af10
JK
11464 }
11465 origin_child_die = sibling_die (origin_child_die);
11466 }
cd02d79d 11467 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11468
11469 do_cleanups (cleanups);
11470}
11471
c906108c 11472static void
e7c27a73 11473read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11474{
e7c27a73 11475 struct objfile *objfile = cu->objfile;
3e29f34a 11476 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11477 struct context_stack *newobj;
c906108c
SS
11478 CORE_ADDR lowpc;
11479 CORE_ADDR highpc;
11480 struct die_info *child_die;
edb3359d 11481 struct attribute *attr, *call_line, *call_file;
15d034d0 11482 const char *name;
e142c38c 11483 CORE_ADDR baseaddr;
801e3a5b 11484 struct block *block;
edb3359d 11485 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11486 VEC (symbolp) *template_args = NULL;
11487 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11488
11489 if (inlined_func)
11490 {
11491 /* If we do not have call site information, we can't show the
11492 caller of this inlined function. That's too confusing, so
11493 only use the scope for local variables. */
11494 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11495 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11496 if (call_line == NULL || call_file == NULL)
11497 {
11498 read_lexical_block_scope (die, cu);
11499 return;
11500 }
11501 }
c906108c 11502
e142c38c
DJ
11503 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11504
94af9270 11505 name = dwarf2_name (die, cu);
c906108c 11506
e8d05480
JB
11507 /* Ignore functions with missing or empty names. These are actually
11508 illegal according to the DWARF standard. */
11509 if (name == NULL)
11510 {
11511 complaint (&symfile_complaints,
b64f50a1 11512 _("missing name for subprogram DIE at %d"),
9c541725 11513 to_underlying (die->sect_off));
e8d05480
JB
11514 return;
11515 }
11516
11517 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11518 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11519 <= PC_BOUNDS_INVALID)
e8d05480 11520 {
ae4d0c03
PM
11521 attr = dwarf2_attr (die, DW_AT_external, cu);
11522 if (!attr || !DW_UNSND (attr))
11523 complaint (&symfile_complaints,
3e43a32a
MS
11524 _("cannot get low and high bounds "
11525 "for subprogram DIE at %d"),
9c541725 11526 to_underlying (die->sect_off));
e8d05480
JB
11527 return;
11528 }
c906108c 11529
3e29f34a
MR
11530 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11531 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11532
34eaf542
TT
11533 /* If we have any template arguments, then we must allocate a
11534 different sort of symbol. */
11535 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11536 {
11537 if (child_die->tag == DW_TAG_template_type_param
11538 || child_die->tag == DW_TAG_template_value_param)
11539 {
e623cf5d 11540 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11541 templ_func->base.is_cplus_template_function = 1;
11542 break;
11543 }
11544 }
11545
fe978cb0
PA
11546 newobj = push_context (0, lowpc);
11547 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11548 (struct symbol *) templ_func);
4c2df51b 11549
4cecd739
DJ
11550 /* If there is a location expression for DW_AT_frame_base, record
11551 it. */
e142c38c 11552 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11553 if (attr)
fe978cb0 11554 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11555
63e43d3a
PMR
11556 /* If there is a location for the static link, record it. */
11557 newobj->static_link = NULL;
11558 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11559 if (attr)
11560 {
224c3ddb
SM
11561 newobj->static_link
11562 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11563 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11564 }
11565
e142c38c 11566 cu->list_in_scope = &local_symbols;
c906108c 11567
639d11d3 11568 if (die->child != NULL)
c906108c 11569 {
639d11d3 11570 child_die = die->child;
c906108c
SS
11571 while (child_die && child_die->tag)
11572 {
34eaf542
TT
11573 if (child_die->tag == DW_TAG_template_type_param
11574 || child_die->tag == DW_TAG_template_value_param)
11575 {
11576 struct symbol *arg = new_symbol (child_die, NULL, cu);
11577
f1078f66
DJ
11578 if (arg != NULL)
11579 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11580 }
11581 else
11582 process_die (child_die, cu);
c906108c
SS
11583 child_die = sibling_die (child_die);
11584 }
11585 }
11586
d389af10
JK
11587 inherit_abstract_dies (die, cu);
11588
4a811a97
UW
11589 /* If we have a DW_AT_specification, we might need to import using
11590 directives from the context of the specification DIE. See the
11591 comment in determine_prefix. */
11592 if (cu->language == language_cplus
11593 && dwarf2_attr (die, DW_AT_specification, cu))
11594 {
11595 struct dwarf2_cu *spec_cu = cu;
11596 struct die_info *spec_die = die_specification (die, &spec_cu);
11597
11598 while (spec_die)
11599 {
11600 child_die = spec_die->child;
11601 while (child_die && child_die->tag)
11602 {
11603 if (child_die->tag == DW_TAG_imported_module)
11604 process_die (child_die, spec_cu);
11605 child_die = sibling_die (child_die);
11606 }
11607
11608 /* In some cases, GCC generates specification DIEs that
11609 themselves contain DW_AT_specification attributes. */
11610 spec_die = die_specification (spec_die, &spec_cu);
11611 }
11612 }
11613
fe978cb0 11614 newobj = pop_context ();
c906108c 11615 /* Make a block for the local symbols within. */
fe978cb0 11616 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11617 newobj->static_link, lowpc, highpc);
801e3a5b 11618
df8a16a1 11619 /* For C++, set the block's scope. */
45280282
IB
11620 if ((cu->language == language_cplus
11621 || cu->language == language_fortran
c44af4eb
TT
11622 || cu->language == language_d
11623 || cu->language == language_rust)
4d4ec4e5 11624 && cu->processing_has_namespace_info)
195a3f6c
TT
11625 block_set_scope (block, determine_prefix (die, cu),
11626 &objfile->objfile_obstack);
df8a16a1 11627
801e3a5b
JB
11628 /* If we have address ranges, record them. */
11629 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11630
fe978cb0 11631 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11632
34eaf542
TT
11633 /* Attach template arguments to function. */
11634 if (! VEC_empty (symbolp, template_args))
11635 {
11636 gdb_assert (templ_func != NULL);
11637
11638 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11639 templ_func->template_arguments
8d749320
SM
11640 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11641 templ_func->n_template_arguments);
34eaf542
TT
11642 memcpy (templ_func->template_arguments,
11643 VEC_address (symbolp, template_args),
11644 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11645 VEC_free (symbolp, template_args);
11646 }
11647
208d8187
JB
11648 /* In C++, we can have functions nested inside functions (e.g., when
11649 a function declares a class that has methods). This means that
11650 when we finish processing a function scope, we may need to go
11651 back to building a containing block's symbol lists. */
fe978cb0 11652 local_symbols = newobj->locals;
22cee43f 11653 local_using_directives = newobj->local_using_directives;
208d8187 11654
921e78cf
JB
11655 /* If we've finished processing a top-level function, subsequent
11656 symbols go in the file symbol list. */
11657 if (outermost_context_p ())
e142c38c 11658 cu->list_in_scope = &file_symbols;
c906108c
SS
11659}
11660
11661/* Process all the DIES contained within a lexical block scope. Start
11662 a new scope, process the dies, and then close the scope. */
11663
11664static void
e7c27a73 11665read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11666{
e7c27a73 11667 struct objfile *objfile = cu->objfile;
3e29f34a 11668 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11669 struct context_stack *newobj;
c906108c
SS
11670 CORE_ADDR lowpc, highpc;
11671 struct die_info *child_die;
e142c38c
DJ
11672 CORE_ADDR baseaddr;
11673
11674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11675
11676 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11677 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11678 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11679 be nasty. Might be easier to properly extend generic blocks to
af34e669 11680 describe ranges. */
e385593e
JK
11681 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11682 {
11683 case PC_BOUNDS_NOT_PRESENT:
11684 /* DW_TAG_lexical_block has no attributes, process its children as if
11685 there was no wrapping by that DW_TAG_lexical_block.
11686 GCC does no longer produces such DWARF since GCC r224161. */
11687 for (child_die = die->child;
11688 child_die != NULL && child_die->tag;
11689 child_die = sibling_die (child_die))
11690 process_die (child_die, cu);
11691 return;
11692 case PC_BOUNDS_INVALID:
11693 return;
11694 }
3e29f34a
MR
11695 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11696 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11697
11698 push_context (0, lowpc);
639d11d3 11699 if (die->child != NULL)
c906108c 11700 {
639d11d3 11701 child_die = die->child;
c906108c
SS
11702 while (child_die && child_die->tag)
11703 {
e7c27a73 11704 process_die (child_die, cu);
c906108c
SS
11705 child_die = sibling_die (child_die);
11706 }
11707 }
3ea89b92 11708 inherit_abstract_dies (die, cu);
fe978cb0 11709 newobj = pop_context ();
c906108c 11710
22cee43f 11711 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11712 {
801e3a5b 11713 struct block *block
63e43d3a 11714 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11715 newobj->start_addr, highpc);
801e3a5b
JB
11716
11717 /* Note that recording ranges after traversing children, as we
11718 do here, means that recording a parent's ranges entails
11719 walking across all its children's ranges as they appear in
11720 the address map, which is quadratic behavior.
11721
11722 It would be nicer to record the parent's ranges before
11723 traversing its children, simply overriding whatever you find
11724 there. But since we don't even decide whether to create a
11725 block until after we've traversed its children, that's hard
11726 to do. */
11727 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11728 }
fe978cb0 11729 local_symbols = newobj->locals;
22cee43f 11730 local_using_directives = newobj->local_using_directives;
c906108c
SS
11731}
11732
216f72a1 11733/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11734
11735static void
11736read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11737{
11738 struct objfile *objfile = cu->objfile;
11739 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11740 CORE_ADDR pc, baseaddr;
11741 struct attribute *attr;
11742 struct call_site *call_site, call_site_local;
11743 void **slot;
11744 int nparams;
11745 struct die_info *child_die;
11746
11747 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11748
216f72a1
JK
11749 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11750 if (attr == NULL)
11751 {
11752 /* This was a pre-DWARF-5 GNU extension alias
11753 for DW_AT_call_return_pc. */
11754 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11755 }
96408a79
SA
11756 if (!attr)
11757 {
11758 complaint (&symfile_complaints,
216f72a1 11759 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11760 "DIE 0x%x [in module %s]"),
9c541725 11761 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11762 return;
11763 }
31aa7e4e 11764 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11765 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11766
11767 if (cu->call_site_htab == NULL)
11768 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11769 NULL, &objfile->objfile_obstack,
11770 hashtab_obstack_allocate, NULL);
11771 call_site_local.pc = pc;
11772 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11773 if (*slot != NULL)
11774 {
11775 complaint (&symfile_complaints,
216f72a1 11776 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11777 "DIE 0x%x [in module %s]"),
9c541725 11778 paddress (gdbarch, pc), to_underlying (die->sect_off),
4262abfb 11779 objfile_name (objfile));
96408a79
SA
11780 return;
11781 }
11782
11783 /* Count parameters at the caller. */
11784
11785 nparams = 0;
11786 for (child_die = die->child; child_die && child_die->tag;
11787 child_die = sibling_die (child_die))
11788 {
216f72a1
JK
11789 if (child_die->tag != DW_TAG_call_site_parameter
11790 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11791 {
11792 complaint (&symfile_complaints,
216f72a1
JK
11793 _("Tag %d is not DW_TAG_call_site_parameter in "
11794 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11795 child_die->tag, to_underlying (child_die->sect_off),
4262abfb 11796 objfile_name (objfile));
96408a79
SA
11797 continue;
11798 }
11799
11800 nparams++;
11801 }
11802
224c3ddb
SM
11803 call_site
11804 = ((struct call_site *)
11805 obstack_alloc (&objfile->objfile_obstack,
11806 sizeof (*call_site)
11807 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11808 *slot = call_site;
11809 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11810 call_site->pc = pc;
11811
216f72a1
JK
11812 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11813 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11814 {
11815 struct die_info *func_die;
11816
11817 /* Skip also over DW_TAG_inlined_subroutine. */
11818 for (func_die = die->parent;
11819 func_die && func_die->tag != DW_TAG_subprogram
11820 && func_die->tag != DW_TAG_subroutine_type;
11821 func_die = func_die->parent);
11822
216f72a1
JK
11823 /* DW_AT_call_all_calls is a superset
11824 of DW_AT_call_all_tail_calls. */
96408a79 11825 if (func_die
216f72a1 11826 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11827 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11828 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11829 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11830 {
11831 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11832 not complete. But keep CALL_SITE for look ups via call_site_htab,
11833 both the initial caller containing the real return address PC and
11834 the final callee containing the current PC of a chain of tail
11835 calls do not need to have the tail call list complete. But any
11836 function candidate for a virtual tail call frame searched via
11837 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11838 determined unambiguously. */
11839 }
11840 else
11841 {
11842 struct type *func_type = NULL;
11843
11844 if (func_die)
11845 func_type = get_die_type (func_die, cu);
11846 if (func_type != NULL)
11847 {
11848 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11849
11850 /* Enlist this call site to the function. */
11851 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11852 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11853 }
11854 else
11855 complaint (&symfile_complaints,
216f72a1 11856 _("Cannot find function owning DW_TAG_call_site "
96408a79 11857 "DIE 0x%x [in module %s]"),
9c541725 11858 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11859 }
11860 }
11861
216f72a1
JK
11862 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11863 if (attr == NULL)
11864 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11865 if (attr == NULL)
11866 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 11867 if (attr == NULL)
216f72a1
JK
11868 {
11869 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11870 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11871 }
96408a79
SA
11872 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11873 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11874 /* Keep NULL DWARF_BLOCK. */;
11875 else if (attr_form_is_block (attr))
11876 {
11877 struct dwarf2_locexpr_baton *dlbaton;
11878
8d749320 11879 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
11880 dlbaton->data = DW_BLOCK (attr)->data;
11881 dlbaton->size = DW_BLOCK (attr)->size;
11882 dlbaton->per_cu = cu->per_cu;
11883
11884 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11885 }
7771576e 11886 else if (attr_form_is_ref (attr))
96408a79 11887 {
96408a79
SA
11888 struct dwarf2_cu *target_cu = cu;
11889 struct die_info *target_die;
11890
ac9ec31b 11891 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11892 gdb_assert (target_cu->objfile == objfile);
11893 if (die_is_declaration (target_die, target_cu))
11894 {
7d45c7c3 11895 const char *target_physname;
9112db09
JK
11896
11897 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11898 target_physname = dwarf2_string_attr (target_die,
11899 DW_AT_linkage_name,
11900 target_cu);
11901 if (target_physname == NULL)
11902 target_physname = dwarf2_string_attr (target_die,
11903 DW_AT_MIPS_linkage_name,
11904 target_cu);
11905 if (target_physname == NULL)
9112db09 11906 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11907 if (target_physname == NULL)
11908 complaint (&symfile_complaints,
216f72a1 11909 _("DW_AT_call_target target DIE has invalid "
96408a79 11910 "physname, for referencing DIE 0x%x [in module %s]"),
9c541725 11911 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 11912 else
7d455152 11913 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11914 }
11915 else
11916 {
11917 CORE_ADDR lowpc;
11918
11919 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11920 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11921 <= PC_BOUNDS_INVALID)
96408a79 11922 complaint (&symfile_complaints,
216f72a1 11923 _("DW_AT_call_target target DIE has invalid "
96408a79 11924 "low pc, for referencing DIE 0x%x [in module %s]"),
9c541725 11925 to_underlying (die->sect_off), objfile_name (objfile));
96408a79 11926 else
3e29f34a
MR
11927 {
11928 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11929 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11930 }
96408a79
SA
11931 }
11932 }
11933 else
11934 complaint (&symfile_complaints,
216f72a1 11935 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 11936 "block nor reference, for DIE 0x%x [in module %s]"),
9c541725 11937 to_underlying (die->sect_off), objfile_name (objfile));
96408a79
SA
11938
11939 call_site->per_cu = cu->per_cu;
11940
11941 for (child_die = die->child;
11942 child_die && child_die->tag;
11943 child_die = sibling_die (child_die))
11944 {
96408a79 11945 struct call_site_parameter *parameter;
1788b2d3 11946 struct attribute *loc, *origin;
96408a79 11947
216f72a1
JK
11948 if (child_die->tag != DW_TAG_call_site_parameter
11949 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11950 {
11951 /* Already printed the complaint above. */
11952 continue;
11953 }
11954
11955 gdb_assert (call_site->parameter_count < nparams);
11956 parameter = &call_site->parameter[call_site->parameter_count];
11957
1788b2d3
JK
11958 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11959 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 11960 register is contained in DW_AT_call_value. */
96408a79 11961
24c5c679 11962 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
11963 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11964 if (origin == NULL)
11965 {
11966 /* This was a pre-DWARF-5 GNU extension alias
11967 for DW_AT_call_parameter. */
11968 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11969 }
7771576e 11970 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 11971 {
1788b2d3 11972 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
11973
11974 sect_offset sect_off
11975 = (sect_offset) dwarf2_get_ref_die_offset (origin);
11976 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
11977 {
11978 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11979 binding can be done only inside one CU. Such referenced DIE
11980 therefore cannot be even moved to DW_TAG_partial_unit. */
11981 complaint (&symfile_complaints,
216f72a1
JK
11982 _("DW_AT_call_parameter offset is not in CU for "
11983 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
11984 to_underlying (child_die->sect_off),
11985 objfile_name (objfile));
d76b7dbc
JK
11986 continue;
11987 }
9c541725
PA
11988 parameter->u.param_cu_off
11989 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
11990 }
11991 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11992 {
11993 complaint (&symfile_complaints,
11994 _("No DW_FORM_block* DW_AT_location for "
216f72a1 11995 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725 11996 to_underlying (child_die->sect_off), objfile_name (objfile));
96408a79
SA
11997 continue;
11998 }
24c5c679 11999 else
96408a79 12000 {
24c5c679
JK
12001 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12002 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12003 if (parameter->u.dwarf_reg != -1)
12004 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12005 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12006 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12007 &parameter->u.fb_offset))
12008 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12009 else
12010 {
12011 complaint (&symfile_complaints,
12012 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12013 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 12014 "DW_TAG_call_site child DIE 0x%x "
24c5c679 12015 "[in module %s]"),
9c541725
PA
12016 to_underlying (child_die->sect_off),
12017 objfile_name (objfile));
24c5c679
JK
12018 continue;
12019 }
96408a79
SA
12020 }
12021
216f72a1
JK
12022 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12023 if (attr == NULL)
12024 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
12025 if (!attr_form_is_block (attr))
12026 {
12027 complaint (&symfile_complaints,
216f72a1
JK
12028 _("No DW_FORM_block* DW_AT_call_value for "
12029 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12030 to_underlying (child_die->sect_off),
12031 objfile_name (objfile));
96408a79
SA
12032 continue;
12033 }
12034 parameter->value = DW_BLOCK (attr)->data;
12035 parameter->value_size = DW_BLOCK (attr)->size;
12036
12037 /* Parameters are not pre-cleared by memset above. */
12038 parameter->data_value = NULL;
12039 parameter->data_value_size = 0;
12040 call_site->parameter_count++;
12041
216f72a1
JK
12042 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12043 if (attr == NULL)
12044 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
12045 if (attr)
12046 {
12047 if (!attr_form_is_block (attr))
12048 complaint (&symfile_complaints,
216f72a1
JK
12049 _("No DW_FORM_block* DW_AT_call_data_value for "
12050 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
9c541725
PA
12051 to_underlying (child_die->sect_off),
12052 objfile_name (objfile));
96408a79
SA
12053 else
12054 {
12055 parameter->data_value = DW_BLOCK (attr)->data;
12056 parameter->data_value_size = DW_BLOCK (attr)->size;
12057 }
12058 }
12059 }
12060}
12061
43988095
JK
12062/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12063 reading .debug_rnglists.
12064 Callback's type should be:
12065 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12066 Return true if the attributes are present and valid, otherwise,
12067 return false. */
12068
12069template <typename Callback>
12070static bool
12071dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12072 Callback &&callback)
12073{
12074 struct objfile *objfile = cu->objfile;
12075 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12076 struct comp_unit_head *cu_header = &cu->header;
12077 bfd *obfd = objfile->obfd;
12078 unsigned int addr_size = cu_header->addr_size;
12079 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12080 /* Base address selection entry. */
12081 CORE_ADDR base;
12082 int found_base;
12083 unsigned int dummy;
12084 const gdb_byte *buffer;
12085 CORE_ADDR low = 0;
12086 CORE_ADDR high = 0;
12087 CORE_ADDR baseaddr;
12088 bool overflow = false;
12089
12090 found_base = cu->base_known;
12091 base = cu->base_address;
12092
12093 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12094 if (offset >= dwarf2_per_objfile->rnglists.size)
12095 {
12096 complaint (&symfile_complaints,
12097 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12098 offset);
12099 return false;
12100 }
12101 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12102
12103 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12104
12105 while (1)
12106 {
7814882a
JK
12107 /* Initialize it due to a false compiler warning. */
12108 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12109 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12110 + dwarf2_per_objfile->rnglists.size);
12111 unsigned int bytes_read;
12112
12113 if (buffer == buf_end)
12114 {
12115 overflow = true;
12116 break;
12117 }
12118 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12119 switch (rlet)
12120 {
12121 case DW_RLE_end_of_list:
12122 break;
12123 case DW_RLE_base_address:
12124 if (buffer + cu->header.addr_size > buf_end)
12125 {
12126 overflow = true;
12127 break;
12128 }
12129 base = read_address (obfd, buffer, cu, &bytes_read);
12130 found_base = 1;
12131 buffer += bytes_read;
12132 break;
12133 case DW_RLE_start_length:
12134 if (buffer + cu->header.addr_size > buf_end)
12135 {
12136 overflow = true;
12137 break;
12138 }
12139 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12140 buffer += bytes_read;
12141 range_end = (range_beginning
12142 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12143 buffer += bytes_read;
12144 if (buffer > buf_end)
12145 {
12146 overflow = true;
12147 break;
12148 }
12149 break;
12150 case DW_RLE_offset_pair:
12151 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12152 buffer += bytes_read;
12153 if (buffer > buf_end)
12154 {
12155 overflow = true;
12156 break;
12157 }
12158 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12159 buffer += bytes_read;
12160 if (buffer > buf_end)
12161 {
12162 overflow = true;
12163 break;
12164 }
12165 break;
12166 case DW_RLE_start_end:
12167 if (buffer + 2 * cu->header.addr_size > buf_end)
12168 {
12169 overflow = true;
12170 break;
12171 }
12172 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12173 buffer += bytes_read;
12174 range_end = read_address (obfd, buffer, cu, &bytes_read);
12175 buffer += bytes_read;
12176 break;
12177 default:
12178 complaint (&symfile_complaints,
12179 _("Invalid .debug_rnglists data (no base address)"));
12180 return false;
12181 }
12182 if (rlet == DW_RLE_end_of_list || overflow)
12183 break;
12184 if (rlet == DW_RLE_base_address)
12185 continue;
12186
12187 if (!found_base)
12188 {
12189 /* We have no valid base address for the ranges
12190 data. */
12191 complaint (&symfile_complaints,
12192 _("Invalid .debug_rnglists data (no base address)"));
12193 return false;
12194 }
12195
12196 if (range_beginning > range_end)
12197 {
12198 /* Inverted range entries are invalid. */
12199 complaint (&symfile_complaints,
12200 _("Invalid .debug_rnglists data (inverted range)"));
12201 return false;
12202 }
12203
12204 /* Empty range entries have no effect. */
12205 if (range_beginning == range_end)
12206 continue;
12207
12208 range_beginning += base;
12209 range_end += base;
12210
12211 /* A not-uncommon case of bad debug info.
12212 Don't pollute the addrmap with bad data. */
12213 if (range_beginning + baseaddr == 0
12214 && !dwarf2_per_objfile->has_section_at_zero)
12215 {
12216 complaint (&symfile_complaints,
12217 _(".debug_rnglists entry has start address of zero"
12218 " [in module %s]"), objfile_name (objfile));
12219 continue;
12220 }
12221
12222 callback (range_beginning, range_end);
12223 }
12224
12225 if (overflow)
12226 {
12227 complaint (&symfile_complaints,
12228 _("Offset %d is not terminated "
12229 "for DW_AT_ranges attribute"),
12230 offset);
12231 return false;
12232 }
12233
12234 return true;
12235}
12236
12237/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12238 Callback's type should be:
12239 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12240 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12241
43988095 12242template <typename Callback>
43039443 12243static int
5f46c5a5 12244dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12245 Callback &&callback)
43039443
JK
12246{
12247 struct objfile *objfile = cu->objfile;
3e29f34a 12248 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12249 struct comp_unit_head *cu_header = &cu->header;
12250 bfd *obfd = objfile->obfd;
12251 unsigned int addr_size = cu_header->addr_size;
12252 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12253 /* Base address selection entry. */
12254 CORE_ADDR base;
12255 int found_base;
12256 unsigned int dummy;
d521ce57 12257 const gdb_byte *buffer;
ff013f42 12258 CORE_ADDR baseaddr;
43039443 12259
43988095
JK
12260 if (cu_header->version >= 5)
12261 return dwarf2_rnglists_process (offset, cu, callback);
12262
d00adf39
DE
12263 found_base = cu->base_known;
12264 base = cu->base_address;
43039443 12265
be391dca 12266 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12267 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12268 {
12269 complaint (&symfile_complaints,
12270 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12271 offset);
12272 return 0;
12273 }
dce234bc 12274 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12275
e7030f15 12276 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12277
43039443
JK
12278 while (1)
12279 {
12280 CORE_ADDR range_beginning, range_end;
12281
12282 range_beginning = read_address (obfd, buffer, cu, &dummy);
12283 buffer += addr_size;
12284 range_end = read_address (obfd, buffer, cu, &dummy);
12285 buffer += addr_size;
12286 offset += 2 * addr_size;
12287
12288 /* An end of list marker is a pair of zero addresses. */
12289 if (range_beginning == 0 && range_end == 0)
12290 /* Found the end of list entry. */
12291 break;
12292
12293 /* Each base address selection entry is a pair of 2 values.
12294 The first is the largest possible address, the second is
12295 the base address. Check for a base address here. */
12296 if ((range_beginning & mask) == mask)
12297 {
28d2bfb9
AB
12298 /* If we found the largest possible address, then we already
12299 have the base address in range_end. */
12300 base = range_end;
43039443
JK
12301 found_base = 1;
12302 continue;
12303 }
12304
12305 if (!found_base)
12306 {
12307 /* We have no valid base address for the ranges
12308 data. */
12309 complaint (&symfile_complaints,
12310 _("Invalid .debug_ranges data (no base address)"));
12311 return 0;
12312 }
12313
9277c30c
UW
12314 if (range_beginning > range_end)
12315 {
12316 /* Inverted range entries are invalid. */
12317 complaint (&symfile_complaints,
12318 _("Invalid .debug_ranges data (inverted range)"));
12319 return 0;
12320 }
12321
12322 /* Empty range entries have no effect. */
12323 if (range_beginning == range_end)
12324 continue;
12325
43039443
JK
12326 range_beginning += base;
12327 range_end += base;
12328
01093045
DE
12329 /* A not-uncommon case of bad debug info.
12330 Don't pollute the addrmap with bad data. */
12331 if (range_beginning + baseaddr == 0
12332 && !dwarf2_per_objfile->has_section_at_zero)
12333 {
12334 complaint (&symfile_complaints,
12335 _(".debug_ranges entry has start address of zero"
4262abfb 12336 " [in module %s]"), objfile_name (objfile));
01093045
DE
12337 continue;
12338 }
12339
5f46c5a5
JK
12340 callback (range_beginning, range_end);
12341 }
12342
12343 return 1;
12344}
12345
12346/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12347 Return 1 if the attributes are present and valid, otherwise, return 0.
12348 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12349
12350static int
12351dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12352 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12353 struct partial_symtab *ranges_pst)
12354{
12355 struct objfile *objfile = cu->objfile;
12356 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12357 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12358 SECT_OFF_TEXT (objfile));
12359 int low_set = 0;
12360 CORE_ADDR low = 0;
12361 CORE_ADDR high = 0;
12362 int retval;
12363
12364 retval = dwarf2_ranges_process (offset, cu,
12365 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12366 {
9277c30c 12367 if (ranges_pst != NULL)
3e29f34a
MR
12368 {
12369 CORE_ADDR lowpc;
12370 CORE_ADDR highpc;
12371
12372 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12373 range_beginning + baseaddr);
12374 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12375 range_end + baseaddr);
12376 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12377 ranges_pst);
12378 }
ff013f42 12379
43039443
JK
12380 /* FIXME: This is recording everything as a low-high
12381 segment of consecutive addresses. We should have a
12382 data structure for discontiguous block ranges
12383 instead. */
12384 if (! low_set)
12385 {
12386 low = range_beginning;
12387 high = range_end;
12388 low_set = 1;
12389 }
12390 else
12391 {
12392 if (range_beginning < low)
12393 low = range_beginning;
12394 if (range_end > high)
12395 high = range_end;
12396 }
5f46c5a5
JK
12397 });
12398 if (!retval)
12399 return 0;
43039443
JK
12400
12401 if (! low_set)
12402 /* If the first entry is an end-of-list marker, the range
12403 describes an empty scope, i.e. no instructions. */
12404 return 0;
12405
12406 if (low_return)
12407 *low_return = low;
12408 if (high_return)
12409 *high_return = high;
12410 return 1;
12411}
12412
3a2b436a
JK
12413/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12414 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12415 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12416
3a2b436a 12417static enum pc_bounds_kind
af34e669 12418dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12419 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12420 struct partial_symtab *pst)
c906108c
SS
12421{
12422 struct attribute *attr;
91da1414 12423 struct attribute *attr_high;
af34e669
DJ
12424 CORE_ADDR low = 0;
12425 CORE_ADDR high = 0;
e385593e 12426 enum pc_bounds_kind ret;
c906108c 12427
91da1414
MW
12428 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12429 if (attr_high)
af34e669 12430 {
e142c38c 12431 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12432 if (attr)
91da1414 12433 {
31aa7e4e
JB
12434 low = attr_value_as_address (attr);
12435 high = attr_value_as_address (attr_high);
12436 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12437 high += low;
91da1414 12438 }
af34e669
DJ
12439 else
12440 /* Found high w/o low attribute. */
e385593e 12441 return PC_BOUNDS_INVALID;
af34e669
DJ
12442
12443 /* Found consecutive range of addresses. */
3a2b436a 12444 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12445 }
c906108c 12446 else
af34e669 12447 {
e142c38c 12448 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12449 if (attr != NULL)
12450 {
ab435259
DE
12451 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12452 We take advantage of the fact that DW_AT_ranges does not appear
12453 in DW_TAG_compile_unit of DWO files. */
12454 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12455 unsigned int ranges_offset = (DW_UNSND (attr)
12456 + (need_ranges_base
12457 ? cu->ranges_base
12458 : 0));
2e3cf129 12459
af34e669 12460 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12461 .debug_ranges section. */
2e3cf129 12462 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12463 return PC_BOUNDS_INVALID;
43039443 12464 /* Found discontinuous range of addresses. */
3a2b436a 12465 ret = PC_BOUNDS_RANGES;
af34e669 12466 }
e385593e
JK
12467 else
12468 return PC_BOUNDS_NOT_PRESENT;
af34e669 12469 }
c906108c 12470
9373cf26
JK
12471 /* read_partial_die has also the strict LOW < HIGH requirement. */
12472 if (high <= low)
e385593e 12473 return PC_BOUNDS_INVALID;
c906108c
SS
12474
12475 /* When using the GNU linker, .gnu.linkonce. sections are used to
12476 eliminate duplicate copies of functions and vtables and such.
12477 The linker will arbitrarily choose one and discard the others.
12478 The AT_*_pc values for such functions refer to local labels in
12479 these sections. If the section from that file was discarded, the
12480 labels are not in the output, so the relocs get a value of 0.
12481 If this is a discarded function, mark the pc bounds as invalid,
12482 so that GDB will ignore it. */
72dca2f5 12483 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12484 return PC_BOUNDS_INVALID;
c906108c
SS
12485
12486 *lowpc = low;
96408a79
SA
12487 if (highpc)
12488 *highpc = high;
af34e669 12489 return ret;
c906108c
SS
12490}
12491
b084d499
JB
12492/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12493 its low and high PC addresses. Do nothing if these addresses could not
12494 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12495 and HIGHPC to the high address if greater than HIGHPC. */
12496
12497static void
12498dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12499 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12500 struct dwarf2_cu *cu)
12501{
12502 CORE_ADDR low, high;
12503 struct die_info *child = die->child;
12504
e385593e 12505 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12506 {
325fac50
PA
12507 *lowpc = std::min (*lowpc, low);
12508 *highpc = std::max (*highpc, high);
b084d499
JB
12509 }
12510
12511 /* If the language does not allow nested subprograms (either inside
12512 subprograms or lexical blocks), we're done. */
12513 if (cu->language != language_ada)
12514 return;
6e70227d 12515
b084d499
JB
12516 /* Check all the children of the given DIE. If it contains nested
12517 subprograms, then check their pc bounds. Likewise, we need to
12518 check lexical blocks as well, as they may also contain subprogram
12519 definitions. */
12520 while (child && child->tag)
12521 {
12522 if (child->tag == DW_TAG_subprogram
12523 || child->tag == DW_TAG_lexical_block)
12524 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12525 child = sibling_die (child);
12526 }
12527}
12528
fae299cd
DC
12529/* Get the low and high pc's represented by the scope DIE, and store
12530 them in *LOWPC and *HIGHPC. If the correct values can't be
12531 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12532
12533static void
12534get_scope_pc_bounds (struct die_info *die,
12535 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12536 struct dwarf2_cu *cu)
12537{
12538 CORE_ADDR best_low = (CORE_ADDR) -1;
12539 CORE_ADDR best_high = (CORE_ADDR) 0;
12540 CORE_ADDR current_low, current_high;
12541
3a2b436a 12542 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12543 >= PC_BOUNDS_RANGES)
fae299cd
DC
12544 {
12545 best_low = current_low;
12546 best_high = current_high;
12547 }
12548 else
12549 {
12550 struct die_info *child = die->child;
12551
12552 while (child && child->tag)
12553 {
12554 switch (child->tag) {
12555 case DW_TAG_subprogram:
b084d499 12556 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12557 break;
12558 case DW_TAG_namespace:
f55ee35c 12559 case DW_TAG_module:
fae299cd
DC
12560 /* FIXME: carlton/2004-01-16: Should we do this for
12561 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12562 that current GCC's always emit the DIEs corresponding
12563 to definitions of methods of classes as children of a
12564 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12565 the DIEs giving the declarations, which could be
12566 anywhere). But I don't see any reason why the
12567 standards says that they have to be there. */
12568 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12569
12570 if (current_low != ((CORE_ADDR) -1))
12571 {
325fac50
PA
12572 best_low = std::min (best_low, current_low);
12573 best_high = std::max (best_high, current_high);
fae299cd
DC
12574 }
12575 break;
12576 default:
0963b4bd 12577 /* Ignore. */
fae299cd
DC
12578 break;
12579 }
12580
12581 child = sibling_die (child);
12582 }
12583 }
12584
12585 *lowpc = best_low;
12586 *highpc = best_high;
12587}
12588
801e3a5b
JB
12589/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12590 in DIE. */
380bca97 12591
801e3a5b
JB
12592static void
12593dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12594 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12595{
bb5ed363 12596 struct objfile *objfile = cu->objfile;
3e29f34a 12597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12598 struct attribute *attr;
91da1414 12599 struct attribute *attr_high;
801e3a5b 12600
91da1414
MW
12601 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12602 if (attr_high)
801e3a5b 12603 {
801e3a5b
JB
12604 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12605 if (attr)
12606 {
31aa7e4e
JB
12607 CORE_ADDR low = attr_value_as_address (attr);
12608 CORE_ADDR high = attr_value_as_address (attr_high);
12609
12610 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12611 high += low;
9a619af0 12612
3e29f34a
MR
12613 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12614 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12615 record_block_range (block, low, high - 1);
801e3a5b
JB
12616 }
12617 }
12618
12619 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12620 if (attr)
12621 {
bb5ed363 12622 bfd *obfd = objfile->obfd;
ab435259
DE
12623 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12624 We take advantage of the fact that DW_AT_ranges does not appear
12625 in DW_TAG_compile_unit of DWO files. */
12626 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12627
12628 /* The value of the DW_AT_ranges attribute is the offset of the
12629 address range list in the .debug_ranges section. */
ab435259
DE
12630 unsigned long offset = (DW_UNSND (attr)
12631 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12632 const gdb_byte *buffer;
801e3a5b
JB
12633
12634 /* For some target architectures, but not others, the
12635 read_address function sign-extends the addresses it returns.
12636 To recognize base address selection entries, we need a
12637 mask. */
12638 unsigned int addr_size = cu->header.addr_size;
12639 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12640
12641 /* The base address, to which the next pair is relative. Note
12642 that this 'base' is a DWARF concept: most entries in a range
12643 list are relative, to reduce the number of relocs against the
12644 debugging information. This is separate from this function's
12645 'baseaddr' argument, which GDB uses to relocate debugging
12646 information from a shared library based on the address at
12647 which the library was loaded. */
d00adf39
DE
12648 CORE_ADDR base = cu->base_address;
12649 int base_known = cu->base_known;
801e3a5b 12650
5f46c5a5
JK
12651 dwarf2_ranges_process (offset, cu,
12652 [&] (CORE_ADDR start, CORE_ADDR end)
12653 {
58fdfd2c
JK
12654 start += baseaddr;
12655 end += baseaddr;
5f46c5a5
JK
12656 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12657 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12658 record_block_range (block, start, end - 1);
12659 });
801e3a5b
JB
12660 }
12661}
12662
685b1105
JK
12663/* Check whether the producer field indicates either of GCC < 4.6, or the
12664 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12665
685b1105
JK
12666static void
12667check_producer (struct dwarf2_cu *cu)
60d5a603 12668{
38360086 12669 int major, minor;
60d5a603
JK
12670
12671 if (cu->producer == NULL)
12672 {
12673 /* For unknown compilers expect their behavior is DWARF version
12674 compliant.
12675
12676 GCC started to support .debug_types sections by -gdwarf-4 since
12677 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12678 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12679 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12680 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12681 }
b1ffba5a 12682 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12683 {
38360086
MW
12684 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12685 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12686 }
61012eef 12687 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12688 cu->producer_is_icc = 1;
12689 else
12690 {
12691 /* For other non-GCC compilers, expect their behavior is DWARF version
12692 compliant. */
60d5a603
JK
12693 }
12694
ba919b58 12695 cu->checked_producer = 1;
685b1105 12696}
ba919b58 12697
685b1105
JK
12698/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12699 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12700 during 4.6.0 experimental. */
12701
12702static int
12703producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12704{
12705 if (!cu->checked_producer)
12706 check_producer (cu);
12707
12708 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12709}
12710
12711/* Return the default accessibility type if it is not overriden by
12712 DW_AT_accessibility. */
12713
12714static enum dwarf_access_attribute
12715dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12716{
12717 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12718 {
12719 /* The default DWARF 2 accessibility for members is public, the default
12720 accessibility for inheritance is private. */
12721
12722 if (die->tag != DW_TAG_inheritance)
12723 return DW_ACCESS_public;
12724 else
12725 return DW_ACCESS_private;
12726 }
12727 else
12728 {
12729 /* DWARF 3+ defines the default accessibility a different way. The same
12730 rules apply now for DW_TAG_inheritance as for the members and it only
12731 depends on the container kind. */
12732
12733 if (die->parent->tag == DW_TAG_class_type)
12734 return DW_ACCESS_private;
12735 else
12736 return DW_ACCESS_public;
12737 }
12738}
12739
74ac6d43
TT
12740/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12741 offset. If the attribute was not found return 0, otherwise return
12742 1. If it was found but could not properly be handled, set *OFFSET
12743 to 0. */
12744
12745static int
12746handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12747 LONGEST *offset)
12748{
12749 struct attribute *attr;
12750
12751 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12752 if (attr != NULL)
12753 {
12754 *offset = 0;
12755
12756 /* Note that we do not check for a section offset first here.
12757 This is because DW_AT_data_member_location is new in DWARF 4,
12758 so if we see it, we can assume that a constant form is really
12759 a constant and not a section offset. */
12760 if (attr_form_is_constant (attr))
12761 *offset = dwarf2_get_attr_constant_value (attr, 0);
12762 else if (attr_form_is_section_offset (attr))
12763 dwarf2_complex_location_expr_complaint ();
12764 else if (attr_form_is_block (attr))
12765 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12766 else
12767 dwarf2_complex_location_expr_complaint ();
12768
12769 return 1;
12770 }
12771
12772 return 0;
12773}
12774
c906108c
SS
12775/* Add an aggregate field to the field list. */
12776
12777static void
107d2387 12778dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12779 struct dwarf2_cu *cu)
6e70227d 12780{
e7c27a73 12781 struct objfile *objfile = cu->objfile;
5e2b427d 12782 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12783 struct nextfield *new_field;
12784 struct attribute *attr;
12785 struct field *fp;
15d034d0 12786 const char *fieldname = "";
c906108c
SS
12787
12788 /* Allocate a new field list entry and link it in. */
8d749320 12789 new_field = XNEW (struct nextfield);
b8c9b27d 12790 make_cleanup (xfree, new_field);
c906108c 12791 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12792
12793 if (die->tag == DW_TAG_inheritance)
12794 {
12795 new_field->next = fip->baseclasses;
12796 fip->baseclasses = new_field;
12797 }
12798 else
12799 {
12800 new_field->next = fip->fields;
12801 fip->fields = new_field;
12802 }
c906108c
SS
12803 fip->nfields++;
12804
e142c38c 12805 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12806 if (attr)
12807 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12808 else
12809 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12810 if (new_field->accessibility != DW_ACCESS_public)
12811 fip->non_public_fields = 1;
60d5a603 12812
e142c38c 12813 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12814 if (attr)
12815 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12816 else
12817 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12818
12819 fp = &new_field->field;
a9a9bd0f 12820
e142c38c 12821 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12822 {
74ac6d43
TT
12823 LONGEST offset;
12824
a9a9bd0f 12825 /* Data member other than a C++ static data member. */
6e70227d 12826
c906108c 12827 /* Get type of field. */
e7c27a73 12828 fp->type = die_type (die, cu);
c906108c 12829
d6a843b5 12830 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12831
c906108c 12832 /* Get bit size of field (zero if none). */
e142c38c 12833 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12834 if (attr)
12835 {
12836 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12837 }
12838 else
12839 {
12840 FIELD_BITSIZE (*fp) = 0;
12841 }
12842
12843 /* Get bit offset of field. */
74ac6d43
TT
12844 if (handle_data_member_location (die, cu, &offset))
12845 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12846 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12847 if (attr)
12848 {
5e2b427d 12849 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12850 {
12851 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12852 additional bit offset from the MSB of the containing
12853 anonymous object to the MSB of the field. We don't
12854 have to do anything special since we don't need to
12855 know the size of the anonymous object. */
f41f5e61 12856 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12857 }
12858 else
12859 {
12860 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12861 MSB of the anonymous object, subtract off the number of
12862 bits from the MSB of the field to the MSB of the
12863 object, and then subtract off the number of bits of
12864 the field itself. The result is the bit offset of
12865 the LSB of the field. */
c906108c
SS
12866 int anonymous_size;
12867 int bit_offset = DW_UNSND (attr);
12868
e142c38c 12869 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12870 if (attr)
12871 {
12872 /* The size of the anonymous object containing
12873 the bit field is explicit, so use the
12874 indicated size (in bytes). */
12875 anonymous_size = DW_UNSND (attr);
12876 }
12877 else
12878 {
12879 /* The size of the anonymous object containing
12880 the bit field must be inferred from the type
12881 attribute of the data member containing the
12882 bit field. */
12883 anonymous_size = TYPE_LENGTH (fp->type);
12884 }
f41f5e61
PA
12885 SET_FIELD_BITPOS (*fp,
12886 (FIELD_BITPOS (*fp)
12887 + anonymous_size * bits_per_byte
12888 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12889 }
12890 }
da5b30da
AA
12891 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12892 if (attr != NULL)
12893 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12894 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
12895
12896 /* Get name of field. */
39cbfefa
DJ
12897 fieldname = dwarf2_name (die, cu);
12898 if (fieldname == NULL)
12899 fieldname = "";
d8151005
DJ
12900
12901 /* The name is already allocated along with this objfile, so we don't
12902 need to duplicate it for the type. */
12903 fp->name = fieldname;
c906108c
SS
12904
12905 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12906 pointer or virtual base class pointer) to private. */
e142c38c 12907 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12908 {
d48cc9dd 12909 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12910 new_field->accessibility = DW_ACCESS_private;
12911 fip->non_public_fields = 1;
12912 }
12913 }
a9a9bd0f 12914 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12915 {
a9a9bd0f
DC
12916 /* C++ static member. */
12917
12918 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12919 is a declaration, but all versions of G++ as of this writing
12920 (so through at least 3.2.1) incorrectly generate
12921 DW_TAG_variable tags. */
6e70227d 12922
ff355380 12923 const char *physname;
c906108c 12924
a9a9bd0f 12925 /* Get name of field. */
39cbfefa
DJ
12926 fieldname = dwarf2_name (die, cu);
12927 if (fieldname == NULL)
c906108c
SS
12928 return;
12929
254e6b9e 12930 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12931 if (attr
12932 /* Only create a symbol if this is an external value.
12933 new_symbol checks this and puts the value in the global symbol
12934 table, which we want. If it is not external, new_symbol
12935 will try to put the value in cu->list_in_scope which is wrong. */
12936 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12937 {
12938 /* A static const member, not much different than an enum as far as
12939 we're concerned, except that we can support more types. */
12940 new_symbol (die, NULL, cu);
12941 }
12942
2df3850c 12943 /* Get physical name. */
ff355380 12944 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12945
d8151005
DJ
12946 /* The name is already allocated along with this objfile, so we don't
12947 need to duplicate it for the type. */
12948 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12949 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12950 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12951 }
12952 else if (die->tag == DW_TAG_inheritance)
12953 {
74ac6d43 12954 LONGEST offset;
d4b96c9a 12955
74ac6d43
TT
12956 /* C++ base class field. */
12957 if (handle_data_member_location (die, cu, &offset))
12958 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12959 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12960 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12961 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12962 fip->nbaseclasses++;
12963 }
12964}
12965
98751a41
JK
12966/* Add a typedef defined in the scope of the FIP's class. */
12967
12968static void
12969dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12970 struct dwarf2_cu *cu)
6e70227d 12971{
98751a41 12972 struct typedef_field_list *new_field;
98751a41 12973 struct typedef_field *fp;
98751a41
JK
12974
12975 /* Allocate a new field list entry and link it in. */
8d749320 12976 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
12977 make_cleanup (xfree, new_field);
12978
12979 gdb_assert (die->tag == DW_TAG_typedef);
12980
12981 fp = &new_field->field;
12982
12983 /* Get name of field. */
12984 fp->name = dwarf2_name (die, cu);
12985 if (fp->name == NULL)
12986 return;
12987
12988 fp->type = read_type_die (die, cu);
12989
12990 new_field->next = fip->typedef_field_list;
12991 fip->typedef_field_list = new_field;
12992 fip->typedef_field_list_count++;
12993}
12994
c906108c
SS
12995/* Create the vector of fields, and attach it to the type. */
12996
12997static void
fba45db2 12998dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12999 struct dwarf2_cu *cu)
c906108c
SS
13000{
13001 int nfields = fip->nfields;
13002
13003 /* Record the field count, allocate space for the array of fields,
13004 and create blank accessibility bitfields if necessary. */
13005 TYPE_NFIELDS (type) = nfields;
13006 TYPE_FIELDS (type) = (struct field *)
13007 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13008 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13009
b4ba55a1 13010 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
13011 {
13012 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13013
13014 TYPE_FIELD_PRIVATE_BITS (type) =
13015 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13016 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13017
13018 TYPE_FIELD_PROTECTED_BITS (type) =
13019 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13020 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13021
774b6a14
TT
13022 TYPE_FIELD_IGNORE_BITS (type) =
13023 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13024 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
13025 }
13026
13027 /* If the type has baseclasses, allocate and clear a bit vector for
13028 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 13029 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
13030 {
13031 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 13032 unsigned char *pointer;
c906108c
SS
13033
13034 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 13035 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 13036 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
13037 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13038 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13039 }
13040
3e43a32a
MS
13041 /* Copy the saved-up fields into the field vector. Start from the head of
13042 the list, adding to the tail of the field array, so that they end up in
13043 the same order in the array in which they were added to the list. */
c906108c
SS
13044 while (nfields-- > 0)
13045 {
7d0ccb61
DJ
13046 struct nextfield *fieldp;
13047
13048 if (fip->fields)
13049 {
13050 fieldp = fip->fields;
13051 fip->fields = fieldp->next;
13052 }
13053 else
13054 {
13055 fieldp = fip->baseclasses;
13056 fip->baseclasses = fieldp->next;
13057 }
13058
13059 TYPE_FIELD (type, nfields) = fieldp->field;
13060 switch (fieldp->accessibility)
c906108c 13061 {
c5aa993b 13062 case DW_ACCESS_private:
b4ba55a1
JB
13063 if (cu->language != language_ada)
13064 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 13065 break;
c906108c 13066
c5aa993b 13067 case DW_ACCESS_protected:
b4ba55a1
JB
13068 if (cu->language != language_ada)
13069 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13070 break;
c906108c 13071
c5aa993b
JM
13072 case DW_ACCESS_public:
13073 break;
c906108c 13074
c5aa993b
JM
13075 default:
13076 /* Unknown accessibility. Complain and treat it as public. */
13077 {
e2e0b3e5 13078 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13079 fieldp->accessibility);
c5aa993b
JM
13080 }
13081 break;
c906108c
SS
13082 }
13083 if (nfields < fip->nbaseclasses)
13084 {
7d0ccb61 13085 switch (fieldp->virtuality)
c906108c 13086 {
c5aa993b
JM
13087 case DW_VIRTUALITY_virtual:
13088 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13089 if (cu->language == language_ada)
a73c6dcd 13090 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13091 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13092 break;
c906108c
SS
13093 }
13094 }
c906108c
SS
13095 }
13096}
13097
7d27a96d
TT
13098/* Return true if this member function is a constructor, false
13099 otherwise. */
13100
13101static int
13102dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13103{
13104 const char *fieldname;
fe978cb0 13105 const char *type_name;
7d27a96d
TT
13106 int len;
13107
13108 if (die->parent == NULL)
13109 return 0;
13110
13111 if (die->parent->tag != DW_TAG_structure_type
13112 && die->parent->tag != DW_TAG_union_type
13113 && die->parent->tag != DW_TAG_class_type)
13114 return 0;
13115
13116 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13117 type_name = dwarf2_name (die->parent, cu);
13118 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13119 return 0;
13120
13121 len = strlen (fieldname);
fe978cb0
PA
13122 return (strncmp (fieldname, type_name, len) == 0
13123 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13124}
13125
c906108c
SS
13126/* Add a member function to the proper fieldlist. */
13127
13128static void
107d2387 13129dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13130 struct type *type, struct dwarf2_cu *cu)
c906108c 13131{
e7c27a73 13132 struct objfile *objfile = cu->objfile;
c906108c
SS
13133 struct attribute *attr;
13134 struct fnfieldlist *flp;
13135 int i;
13136 struct fn_field *fnp;
15d034d0 13137 const char *fieldname;
c906108c 13138 struct nextfnfield *new_fnfield;
f792889a 13139 struct type *this_type;
60d5a603 13140 enum dwarf_access_attribute accessibility;
c906108c 13141
b4ba55a1 13142 if (cu->language == language_ada)
a73c6dcd 13143 error (_("unexpected member function in Ada type"));
b4ba55a1 13144
2df3850c 13145 /* Get name of member function. */
39cbfefa
DJ
13146 fieldname = dwarf2_name (die, cu);
13147 if (fieldname == NULL)
2df3850c 13148 return;
c906108c 13149
c906108c
SS
13150 /* Look up member function name in fieldlist. */
13151 for (i = 0; i < fip->nfnfields; i++)
13152 {
27bfe10e 13153 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13154 break;
13155 }
13156
13157 /* Create new list element if necessary. */
13158 if (i < fip->nfnfields)
13159 flp = &fip->fnfieldlists[i];
13160 else
13161 {
13162 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13163 {
13164 fip->fnfieldlists = (struct fnfieldlist *)
13165 xrealloc (fip->fnfieldlists,
13166 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13167 * sizeof (struct fnfieldlist));
c906108c 13168 if (fip->nfnfields == 0)
c13c43fd 13169 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13170 }
13171 flp = &fip->fnfieldlists[fip->nfnfields];
13172 flp->name = fieldname;
13173 flp->length = 0;
13174 flp->head = NULL;
3da10d80 13175 i = fip->nfnfields++;
c906108c
SS
13176 }
13177
13178 /* Create a new member function field and chain it to the field list
0963b4bd 13179 entry. */
8d749320 13180 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13181 make_cleanup (xfree, new_fnfield);
c906108c
SS
13182 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13183 new_fnfield->next = flp->head;
13184 flp->head = new_fnfield;
13185 flp->length++;
13186
13187 /* Fill in the member function field info. */
13188 fnp = &new_fnfield->fnfield;
3da10d80
KS
13189
13190 /* Delay processing of the physname until later. */
9c37b5ae 13191 if (cu->language == language_cplus)
3da10d80
KS
13192 {
13193 add_to_method_list (type, i, flp->length - 1, fieldname,
13194 die, cu);
13195 }
13196 else
13197 {
1d06ead6 13198 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13199 fnp->physname = physname ? physname : "";
13200 }
13201
c906108c 13202 fnp->type = alloc_type (objfile);
f792889a
DJ
13203 this_type = read_type_die (die, cu);
13204 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13205 {
f792889a 13206 int nparams = TYPE_NFIELDS (this_type);
c906108c 13207
f792889a 13208 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13209 of the method itself (TYPE_CODE_METHOD). */
13210 smash_to_method_type (fnp->type, type,
f792889a
DJ
13211 TYPE_TARGET_TYPE (this_type),
13212 TYPE_FIELDS (this_type),
13213 TYPE_NFIELDS (this_type),
13214 TYPE_VARARGS (this_type));
c906108c
SS
13215
13216 /* Handle static member functions.
c5aa993b 13217 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13218 member functions. G++ helps GDB by marking the first
13219 parameter for non-static member functions (which is the this
13220 pointer) as artificial. We obtain this information from
13221 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13222 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13223 fnp->voffset = VOFFSET_STATIC;
13224 }
13225 else
e2e0b3e5 13226 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13227 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13228
13229 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13230 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13231 fnp->fcontext = die_containing_type (die, cu);
c906108c 13232
3e43a32a
MS
13233 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13234 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13235
13236 /* Get accessibility. */
e142c38c 13237 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13238 if (attr)
aead7601 13239 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13240 else
13241 accessibility = dwarf2_default_access_attribute (die, cu);
13242 switch (accessibility)
c906108c 13243 {
60d5a603
JK
13244 case DW_ACCESS_private:
13245 fnp->is_private = 1;
13246 break;
13247 case DW_ACCESS_protected:
13248 fnp->is_protected = 1;
13249 break;
c906108c
SS
13250 }
13251
b02dede2 13252 /* Check for artificial methods. */
e142c38c 13253 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13254 if (attr && DW_UNSND (attr) != 0)
13255 fnp->is_artificial = 1;
13256
7d27a96d
TT
13257 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13258
0d564a31 13259 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13260 function. For older versions of GCC, this is an offset in the
13261 appropriate virtual table, as specified by DW_AT_containing_type.
13262 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13263 to the object address. */
13264
e142c38c 13265 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13266 if (attr)
8e19ed76 13267 {
aec5aa8b 13268 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13269 {
aec5aa8b
TT
13270 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13271 {
13272 /* Old-style GCC. */
13273 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13274 }
13275 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13276 || (DW_BLOCK (attr)->size > 1
13277 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13278 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13279 {
aec5aa8b
TT
13280 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13281 if ((fnp->voffset % cu->header.addr_size) != 0)
13282 dwarf2_complex_location_expr_complaint ();
13283 else
13284 fnp->voffset /= cu->header.addr_size;
13285 fnp->voffset += 2;
13286 }
13287 else
13288 dwarf2_complex_location_expr_complaint ();
13289
13290 if (!fnp->fcontext)
7e993ebf
KS
13291 {
13292 /* If there is no `this' field and no DW_AT_containing_type,
13293 we cannot actually find a base class context for the
13294 vtable! */
13295 if (TYPE_NFIELDS (this_type) == 0
13296 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13297 {
13298 complaint (&symfile_complaints,
13299 _("cannot determine context for virtual member "
13300 "function \"%s\" (offset %d)"),
9c541725 13301 fieldname, to_underlying (die->sect_off));
7e993ebf
KS
13302 }
13303 else
13304 {
13305 fnp->fcontext
13306 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13307 }
13308 }
aec5aa8b 13309 }
3690dd37 13310 else if (attr_form_is_section_offset (attr))
8e19ed76 13311 {
4d3c2250 13312 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13313 }
13314 else
13315 {
4d3c2250
KB
13316 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13317 fieldname);
8e19ed76 13318 }
0d564a31 13319 }
d48cc9dd
DJ
13320 else
13321 {
13322 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13323 if (attr && DW_UNSND (attr))
13324 {
13325 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13326 complaint (&symfile_complaints,
3e43a32a
MS
13327 _("Member function \"%s\" (offset %d) is virtual "
13328 "but the vtable offset is not specified"),
9c541725 13329 fieldname, to_underlying (die->sect_off));
9655fd1a 13330 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13331 TYPE_CPLUS_DYNAMIC (type) = 1;
13332 }
13333 }
c906108c
SS
13334}
13335
13336/* Create the vector of member function fields, and attach it to the type. */
13337
13338static void
fba45db2 13339dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13340 struct dwarf2_cu *cu)
c906108c
SS
13341{
13342 struct fnfieldlist *flp;
c906108c
SS
13343 int i;
13344
b4ba55a1 13345 if (cu->language == language_ada)
a73c6dcd 13346 error (_("unexpected member functions in Ada type"));
b4ba55a1 13347
c906108c
SS
13348 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13349 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13350 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13351
13352 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13353 {
13354 struct nextfnfield *nfp = flp->head;
13355 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13356 int k;
13357
13358 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13359 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13360 fn_flp->fn_fields = (struct fn_field *)
13361 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13362 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13363 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13364 }
13365
13366 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13367}
13368
1168df01
JB
13369/* Returns non-zero if NAME is the name of a vtable member in CU's
13370 language, zero otherwise. */
13371static int
13372is_vtable_name (const char *name, struct dwarf2_cu *cu)
13373{
13374 static const char vptr[] = "_vptr";
987504bb 13375 static const char vtable[] = "vtable";
1168df01 13376
9c37b5ae
TT
13377 /* Look for the C++ form of the vtable. */
13378 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13379 return 1;
13380
13381 return 0;
13382}
13383
c0dd20ea 13384/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13385 functions, with the ABI-specified layout. If TYPE describes
13386 such a structure, smash it into a member function type.
61049d3b
DJ
13387
13388 GCC shouldn't do this; it should just output pointer to member DIEs.
13389 This is GCC PR debug/28767. */
c0dd20ea 13390
0b92b5bb
TT
13391static void
13392quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13393{
09e2d7c7 13394 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13395
13396 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13397 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13398 return;
c0dd20ea
DJ
13399
13400 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13401 if (TYPE_FIELD_NAME (type, 0) == NULL
13402 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13403 || TYPE_FIELD_NAME (type, 1) == NULL
13404 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13405 return;
c0dd20ea
DJ
13406
13407 /* Find the type of the method. */
0b92b5bb 13408 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13409 if (pfn_type == NULL
13410 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13411 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13412 return;
c0dd20ea
DJ
13413
13414 /* Look for the "this" argument. */
13415 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13416 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13417 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13418 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13419 return;
c0dd20ea 13420
09e2d7c7 13421 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13422 new_type = alloc_type (objfile);
09e2d7c7 13423 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13424 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13425 TYPE_VARARGS (pfn_type));
0b92b5bb 13426 smash_to_methodptr_type (type, new_type);
c0dd20ea 13427}
1168df01 13428
685b1105
JK
13429/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13430 (icc). */
13431
13432static int
13433producer_is_icc (struct dwarf2_cu *cu)
13434{
13435 if (!cu->checked_producer)
13436 check_producer (cu);
13437
13438 return cu->producer_is_icc;
13439}
13440
c906108c 13441/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13442 (definition) to create a type for the structure or union. Fill in
13443 the type's name and general properties; the members will not be
83655187
DE
13444 processed until process_structure_scope. A symbol table entry for
13445 the type will also not be done until process_structure_scope (assuming
13446 the type has a name).
c906108c 13447
c767944b
DJ
13448 NOTE: we need to call these functions regardless of whether or not the
13449 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13450 structure or union. This gets the type entered into our set of
83655187 13451 user defined types. */
c906108c 13452
f792889a 13453static struct type *
134d01f1 13454read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13455{
e7c27a73 13456 struct objfile *objfile = cu->objfile;
c906108c
SS
13457 struct type *type;
13458 struct attribute *attr;
15d034d0 13459 const char *name;
c906108c 13460
348e048f
DE
13461 /* If the definition of this type lives in .debug_types, read that type.
13462 Don't follow DW_AT_specification though, that will take us back up
13463 the chain and we want to go down. */
45e58e77 13464 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13465 if (attr)
13466 {
ac9ec31b 13467 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13468
ac9ec31b 13469 /* The type's CU may not be the same as CU.
02142a6c 13470 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13471 return set_die_type (die, type, cu);
13472 }
13473
c0dd20ea 13474 type = alloc_type (objfile);
c906108c 13475 INIT_CPLUS_SPECIFIC (type);
93311388 13476
39cbfefa
DJ
13477 name = dwarf2_name (die, cu);
13478 if (name != NULL)
c906108c 13479 {
987504bb 13480 if (cu->language == language_cplus
c44af4eb
TT
13481 || cu->language == language_d
13482 || cu->language == language_rust)
63d06c5c 13483 {
15d034d0 13484 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13485
13486 /* dwarf2_full_name might have already finished building the DIE's
13487 type. If so, there is no need to continue. */
13488 if (get_die_type (die, cu) != NULL)
13489 return get_die_type (die, cu);
13490
13491 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13492 if (die->tag == DW_TAG_structure_type
13493 || die->tag == DW_TAG_class_type)
13494 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13495 }
13496 else
13497 {
d8151005
DJ
13498 /* The name is already allocated along with this objfile, so
13499 we don't need to duplicate it for the type. */
7d455152 13500 TYPE_TAG_NAME (type) = name;
94af9270
KS
13501 if (die->tag == DW_TAG_class_type)
13502 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13503 }
c906108c
SS
13504 }
13505
13506 if (die->tag == DW_TAG_structure_type)
13507 {
13508 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13509 }
13510 else if (die->tag == DW_TAG_union_type)
13511 {
13512 TYPE_CODE (type) = TYPE_CODE_UNION;
13513 }
13514 else
13515 {
4753d33b 13516 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13517 }
13518
0cc2414c
TT
13519 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13520 TYPE_DECLARED_CLASS (type) = 1;
13521
e142c38c 13522 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13523 if (attr)
13524 {
155bfbd3
JB
13525 if (attr_form_is_constant (attr))
13526 TYPE_LENGTH (type) = DW_UNSND (attr);
13527 else
13528 {
13529 /* For the moment, dynamic type sizes are not supported
13530 by GDB's struct type. The actual size is determined
13531 on-demand when resolving the type of a given object,
13532 so set the type's length to zero for now. Otherwise,
13533 we record an expression as the length, and that expression
13534 could lead to a very large value, which could eventually
13535 lead to us trying to allocate that much memory when creating
13536 a value of that type. */
13537 TYPE_LENGTH (type) = 0;
13538 }
c906108c
SS
13539 }
13540 else
13541 {
13542 TYPE_LENGTH (type) = 0;
13543 }
13544
422b1cb0 13545 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13546 {
13547 /* ICC does not output the required DW_AT_declaration
13548 on incomplete types, but gives them a size of zero. */
422b1cb0 13549 TYPE_STUB (type) = 1;
685b1105
JK
13550 }
13551 else
13552 TYPE_STUB_SUPPORTED (type) = 1;
13553
dc718098 13554 if (die_is_declaration (die, cu))
876cecd0 13555 TYPE_STUB (type) = 1;
a6c727b2
DJ
13556 else if (attr == NULL && die->child == NULL
13557 && producer_is_realview (cu->producer))
13558 /* RealView does not output the required DW_AT_declaration
13559 on incomplete types. */
13560 TYPE_STUB (type) = 1;
dc718098 13561
c906108c
SS
13562 /* We need to add the type field to the die immediately so we don't
13563 infinitely recurse when dealing with pointers to the structure
0963b4bd 13564 type within the structure itself. */
1c379e20 13565 set_die_type (die, type, cu);
c906108c 13566
7e314c57
JK
13567 /* set_die_type should be already done. */
13568 set_descriptive_type (type, die, cu);
13569
c767944b
DJ
13570 return type;
13571}
13572
13573/* Finish creating a structure or union type, including filling in
13574 its members and creating a symbol for it. */
13575
13576static void
13577process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13578{
13579 struct objfile *objfile = cu->objfile;
ca040673 13580 struct die_info *child_die;
c767944b
DJ
13581 struct type *type;
13582
13583 type = get_die_type (die, cu);
13584 if (type == NULL)
13585 type = read_structure_type (die, cu);
13586
e142c38c 13587 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13588 {
13589 struct field_info fi;
34eaf542 13590 VEC (symbolp) *template_args = NULL;
c767944b 13591 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13592
13593 memset (&fi, 0, sizeof (struct field_info));
13594
639d11d3 13595 child_die = die->child;
c906108c
SS
13596
13597 while (child_die && child_die->tag)
13598 {
a9a9bd0f
DC
13599 if (child_die->tag == DW_TAG_member
13600 || child_die->tag == DW_TAG_variable)
c906108c 13601 {
a9a9bd0f
DC
13602 /* NOTE: carlton/2002-11-05: A C++ static data member
13603 should be a DW_TAG_member that is a declaration, but
13604 all versions of G++ as of this writing (so through at
13605 least 3.2.1) incorrectly generate DW_TAG_variable
13606 tags for them instead. */
e7c27a73 13607 dwarf2_add_field (&fi, child_die, cu);
c906108c 13608 }
8713b1b1 13609 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13610 {
e98c9e7c
TT
13611 /* Rust doesn't have member functions in the C++ sense.
13612 However, it does emit ordinary functions as children
13613 of a struct DIE. */
13614 if (cu->language == language_rust)
13615 read_func_scope (child_die, cu);
13616 else
13617 {
13618 /* C++ member function. */
13619 dwarf2_add_member_fn (&fi, child_die, type, cu);
13620 }
c906108c
SS
13621 }
13622 else if (child_die->tag == DW_TAG_inheritance)
13623 {
13624 /* C++ base class field. */
e7c27a73 13625 dwarf2_add_field (&fi, child_die, cu);
c906108c 13626 }
98751a41
JK
13627 else if (child_die->tag == DW_TAG_typedef)
13628 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13629 else if (child_die->tag == DW_TAG_template_type_param
13630 || child_die->tag == DW_TAG_template_value_param)
13631 {
13632 struct symbol *arg = new_symbol (child_die, NULL, cu);
13633
f1078f66
DJ
13634 if (arg != NULL)
13635 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13636 }
13637
c906108c
SS
13638 child_die = sibling_die (child_die);
13639 }
13640
34eaf542
TT
13641 /* Attach template arguments to type. */
13642 if (! VEC_empty (symbolp, template_args))
13643 {
13644 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13645 TYPE_N_TEMPLATE_ARGUMENTS (type)
13646 = VEC_length (symbolp, template_args);
13647 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13648 = XOBNEWVEC (&objfile->objfile_obstack,
13649 struct symbol *,
13650 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13651 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13652 VEC_address (symbolp, template_args),
13653 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13654 * sizeof (struct symbol *)));
13655 VEC_free (symbolp, template_args);
13656 }
13657
c906108c
SS
13658 /* Attach fields and member functions to the type. */
13659 if (fi.nfields)
e7c27a73 13660 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13661 if (fi.nfnfields)
13662 {
e7c27a73 13663 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13664
c5aa993b 13665 /* Get the type which refers to the base class (possibly this
c906108c 13666 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13667 class from the DW_AT_containing_type attribute. This use of
13668 DW_AT_containing_type is a GNU extension. */
c906108c 13669
e142c38c 13670 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13671 {
e7c27a73 13672 struct type *t = die_containing_type (die, cu);
c906108c 13673
ae6ae975 13674 set_type_vptr_basetype (type, t);
c906108c
SS
13675 if (type == t)
13676 {
c906108c
SS
13677 int i;
13678
13679 /* Our own class provides vtbl ptr. */
13680 for (i = TYPE_NFIELDS (t) - 1;
13681 i >= TYPE_N_BASECLASSES (t);
13682 --i)
13683 {
0d5cff50 13684 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13685
1168df01 13686 if (is_vtable_name (fieldname, cu))
c906108c 13687 {
ae6ae975 13688 set_type_vptr_fieldno (type, i);
c906108c
SS
13689 break;
13690 }
13691 }
13692
13693 /* Complain if virtual function table field not found. */
13694 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13695 complaint (&symfile_complaints,
3e43a32a
MS
13696 _("virtual function table pointer "
13697 "not found when defining class '%s'"),
4d3c2250
KB
13698 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13699 "");
c906108c
SS
13700 }
13701 else
13702 {
ae6ae975 13703 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13704 }
13705 }
f6235d4c 13706 else if (cu->producer
61012eef 13707 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13708 {
13709 /* The IBM XLC compiler does not provide direct indication
13710 of the containing type, but the vtable pointer is
13711 always named __vfp. */
13712
13713 int i;
13714
13715 for (i = TYPE_NFIELDS (type) - 1;
13716 i >= TYPE_N_BASECLASSES (type);
13717 --i)
13718 {
13719 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13720 {
ae6ae975
DE
13721 set_type_vptr_fieldno (type, i);
13722 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13723 break;
13724 }
13725 }
13726 }
c906108c 13727 }
98751a41
JK
13728
13729 /* Copy fi.typedef_field_list linked list elements content into the
13730 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13731 if (fi.typedef_field_list)
13732 {
13733 int i = fi.typedef_field_list_count;
13734
a0d7a4ff 13735 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13736 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13737 = ((struct typedef_field *)
13738 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13739 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13740
13741 /* Reverse the list order to keep the debug info elements order. */
13742 while (--i >= 0)
13743 {
13744 struct typedef_field *dest, *src;
6e70227d 13745
98751a41
JK
13746 dest = &TYPE_TYPEDEF_FIELD (type, i);
13747 src = &fi.typedef_field_list->field;
13748 fi.typedef_field_list = fi.typedef_field_list->next;
13749 *dest = *src;
13750 }
13751 }
c767944b
DJ
13752
13753 do_cleanups (back_to);
c906108c 13754 }
63d06c5c 13755
bb5ed363 13756 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13757
90aeadfc
DC
13758 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13759 snapshots) has been known to create a die giving a declaration
13760 for a class that has, as a child, a die giving a definition for a
13761 nested class. So we have to process our children even if the
13762 current die is a declaration. Normally, of course, a declaration
13763 won't have any children at all. */
134d01f1 13764
ca040673
DE
13765 child_die = die->child;
13766
90aeadfc
DC
13767 while (child_die != NULL && child_die->tag)
13768 {
13769 if (child_die->tag == DW_TAG_member
13770 || child_die->tag == DW_TAG_variable
34eaf542
TT
13771 || child_die->tag == DW_TAG_inheritance
13772 || child_die->tag == DW_TAG_template_value_param
13773 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13774 {
90aeadfc 13775 /* Do nothing. */
134d01f1 13776 }
90aeadfc
DC
13777 else
13778 process_die (child_die, cu);
134d01f1 13779
90aeadfc 13780 child_die = sibling_die (child_die);
134d01f1
DJ
13781 }
13782
fa4028e9
JB
13783 /* Do not consider external references. According to the DWARF standard,
13784 these DIEs are identified by the fact that they have no byte_size
13785 attribute, and a declaration attribute. */
13786 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13787 || !die_is_declaration (die, cu))
c767944b 13788 new_symbol (die, type, cu);
134d01f1
DJ
13789}
13790
55426c9d
JB
13791/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13792 update TYPE using some information only available in DIE's children. */
13793
13794static void
13795update_enumeration_type_from_children (struct die_info *die,
13796 struct type *type,
13797 struct dwarf2_cu *cu)
13798{
13799 struct obstack obstack;
60f7655a 13800 struct die_info *child_die;
55426c9d
JB
13801 int unsigned_enum = 1;
13802 int flag_enum = 1;
13803 ULONGEST mask = 0;
13804 struct cleanup *old_chain;
13805
13806 obstack_init (&obstack);
13807 old_chain = make_cleanup_obstack_free (&obstack);
13808
60f7655a
DE
13809 for (child_die = die->child;
13810 child_die != NULL && child_die->tag;
13811 child_die = sibling_die (child_die))
55426c9d
JB
13812 {
13813 struct attribute *attr;
13814 LONGEST value;
13815 const gdb_byte *bytes;
13816 struct dwarf2_locexpr_baton *baton;
13817 const char *name;
60f7655a 13818
55426c9d
JB
13819 if (child_die->tag != DW_TAG_enumerator)
13820 continue;
13821
13822 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13823 if (attr == NULL)
13824 continue;
13825
13826 name = dwarf2_name (child_die, cu);
13827 if (name == NULL)
13828 name = "<anonymous enumerator>";
13829
13830 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13831 &value, &bytes, &baton);
13832 if (value < 0)
13833 {
13834 unsigned_enum = 0;
13835 flag_enum = 0;
13836 }
13837 else if ((mask & value) != 0)
13838 flag_enum = 0;
13839 else
13840 mask |= value;
13841
13842 /* If we already know that the enum type is neither unsigned, nor
13843 a flag type, no need to look at the rest of the enumerates. */
13844 if (!unsigned_enum && !flag_enum)
13845 break;
55426c9d
JB
13846 }
13847
13848 if (unsigned_enum)
13849 TYPE_UNSIGNED (type) = 1;
13850 if (flag_enum)
13851 TYPE_FLAG_ENUM (type) = 1;
13852
13853 do_cleanups (old_chain);
13854}
13855
134d01f1
DJ
13856/* Given a DW_AT_enumeration_type die, set its type. We do not
13857 complete the type's fields yet, or create any symbols. */
c906108c 13858
f792889a 13859static struct type *
134d01f1 13860read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13861{
e7c27a73 13862 struct objfile *objfile = cu->objfile;
c906108c 13863 struct type *type;
c906108c 13864 struct attribute *attr;
0114d602 13865 const char *name;
134d01f1 13866
348e048f
DE
13867 /* If the definition of this type lives in .debug_types, read that type.
13868 Don't follow DW_AT_specification though, that will take us back up
13869 the chain and we want to go down. */
45e58e77 13870 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13871 if (attr)
13872 {
ac9ec31b 13873 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13874
ac9ec31b 13875 /* The type's CU may not be the same as CU.
02142a6c 13876 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13877 return set_die_type (die, type, cu);
13878 }
13879
c906108c
SS
13880 type = alloc_type (objfile);
13881
13882 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13883 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13884 if (name != NULL)
7d455152 13885 TYPE_TAG_NAME (type) = name;
c906108c 13886
0626fc76
TT
13887 attr = dwarf2_attr (die, DW_AT_type, cu);
13888 if (attr != NULL)
13889 {
13890 struct type *underlying_type = die_type (die, cu);
13891
13892 TYPE_TARGET_TYPE (type) = underlying_type;
13893 }
13894
e142c38c 13895 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13896 if (attr)
13897 {
13898 TYPE_LENGTH (type) = DW_UNSND (attr);
13899 }
13900 else
13901 {
13902 TYPE_LENGTH (type) = 0;
13903 }
13904
137033e9
JB
13905 /* The enumeration DIE can be incomplete. In Ada, any type can be
13906 declared as private in the package spec, and then defined only
13907 inside the package body. Such types are known as Taft Amendment
13908 Types. When another package uses such a type, an incomplete DIE
13909 may be generated by the compiler. */
02eb380e 13910 if (die_is_declaration (die, cu))
876cecd0 13911 TYPE_STUB (type) = 1;
02eb380e 13912
0626fc76
TT
13913 /* Finish the creation of this type by using the enum's children.
13914 We must call this even when the underlying type has been provided
13915 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13916 update_enumeration_type_from_children (die, type, cu);
13917
0626fc76
TT
13918 /* If this type has an underlying type that is not a stub, then we
13919 may use its attributes. We always use the "unsigned" attribute
13920 in this situation, because ordinarily we guess whether the type
13921 is unsigned -- but the guess can be wrong and the underlying type
13922 can tell us the reality. However, we defer to a local size
13923 attribute if one exists, because this lets the compiler override
13924 the underlying type if needed. */
13925 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13926 {
13927 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13928 if (TYPE_LENGTH (type) == 0)
13929 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13930 }
13931
3d567982
TT
13932 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13933
f792889a 13934 return set_die_type (die, type, cu);
134d01f1
DJ
13935}
13936
13937/* Given a pointer to a die which begins an enumeration, process all
13938 the dies that define the members of the enumeration, and create the
13939 symbol for the enumeration type.
13940
13941 NOTE: We reverse the order of the element list. */
13942
13943static void
13944process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13945{
f792889a 13946 struct type *this_type;
134d01f1 13947
f792889a
DJ
13948 this_type = get_die_type (die, cu);
13949 if (this_type == NULL)
13950 this_type = read_enumeration_type (die, cu);
9dc481d3 13951
639d11d3 13952 if (die->child != NULL)
c906108c 13953 {
9dc481d3
DE
13954 struct die_info *child_die;
13955 struct symbol *sym;
13956 struct field *fields = NULL;
13957 int num_fields = 0;
15d034d0 13958 const char *name;
9dc481d3 13959
639d11d3 13960 child_die = die->child;
c906108c
SS
13961 while (child_die && child_die->tag)
13962 {
13963 if (child_die->tag != DW_TAG_enumerator)
13964 {
e7c27a73 13965 process_die (child_die, cu);
c906108c
SS
13966 }
13967 else
13968 {
39cbfefa
DJ
13969 name = dwarf2_name (child_die, cu);
13970 if (name)
c906108c 13971 {
f792889a 13972 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13973
13974 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13975 {
13976 fields = (struct field *)
13977 xrealloc (fields,
13978 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13979 * sizeof (struct field));
c906108c
SS
13980 }
13981
3567439c 13982 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13983 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13984 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13985 FIELD_BITSIZE (fields[num_fields]) = 0;
13986
13987 num_fields++;
13988 }
13989 }
13990
13991 child_die = sibling_die (child_die);
13992 }
13993
13994 if (num_fields)
13995 {
f792889a
DJ
13996 TYPE_NFIELDS (this_type) = num_fields;
13997 TYPE_FIELDS (this_type) = (struct field *)
13998 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13999 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 14000 sizeof (struct field) * num_fields);
b8c9b27d 14001 xfree (fields);
c906108c 14002 }
c906108c 14003 }
134d01f1 14004
6c83ed52
TT
14005 /* If we are reading an enum from a .debug_types unit, and the enum
14006 is a declaration, and the enum is not the signatured type in the
14007 unit, then we do not want to add a symbol for it. Adding a
14008 symbol would in some cases obscure the true definition of the
14009 enum, giving users an incomplete type when the definition is
14010 actually available. Note that we do not want to do this for all
14011 enums which are just declarations, because C++0x allows forward
14012 enum declarations. */
3019eac3 14013 if (cu->per_cu->is_debug_types
6c83ed52
TT
14014 && die_is_declaration (die, cu))
14015 {
52dc124a 14016 struct signatured_type *sig_type;
6c83ed52 14017
c0f78cd4 14018 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
14019 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
14020 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
14021 return;
14022 }
14023
f792889a 14024 new_symbol (die, this_type, cu);
c906108c
SS
14025}
14026
14027/* Extract all information from a DW_TAG_array_type DIE and put it in
14028 the DIE's type field. For now, this only handles one dimensional
14029 arrays. */
14030
f792889a 14031static struct type *
e7c27a73 14032read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14033{
e7c27a73 14034 struct objfile *objfile = cu->objfile;
c906108c 14035 struct die_info *child_die;
7e314c57 14036 struct type *type;
c906108c
SS
14037 struct type *element_type, *range_type, *index_type;
14038 struct type **range_types = NULL;
14039 struct attribute *attr;
14040 int ndim = 0;
14041 struct cleanup *back_to;
15d034d0 14042 const char *name;
dc53a7ad 14043 unsigned int bit_stride = 0;
c906108c 14044
e7c27a73 14045 element_type = die_type (die, cu);
c906108c 14046
7e314c57
JK
14047 /* The die_type call above may have already set the type for this DIE. */
14048 type = get_die_type (die, cu);
14049 if (type)
14050 return type;
14051
dc53a7ad
JB
14052 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14053 if (attr != NULL)
14054 bit_stride = DW_UNSND (attr) * 8;
14055
14056 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14057 if (attr != NULL)
14058 bit_stride = DW_UNSND (attr);
14059
c906108c
SS
14060 /* Irix 6.2 native cc creates array types without children for
14061 arrays with unspecified length. */
639d11d3 14062 if (die->child == NULL)
c906108c 14063 {
46bf5051 14064 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14065 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14066 type = create_array_type_with_stride (NULL, element_type, range_type,
14067 bit_stride);
f792889a 14068 return set_die_type (die, type, cu);
c906108c
SS
14069 }
14070
14071 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14072 child_die = die->child;
c906108c
SS
14073 while (child_die && child_die->tag)
14074 {
14075 if (child_die->tag == DW_TAG_subrange_type)
14076 {
f792889a 14077 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14078
f792889a 14079 if (child_type != NULL)
a02abb62 14080 {
0963b4bd
MS
14081 /* The range type was succesfully read. Save it for the
14082 array type creation. */
a02abb62
JB
14083 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14084 {
14085 range_types = (struct type **)
14086 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14087 * sizeof (struct type *));
14088 if (ndim == 0)
14089 make_cleanup (free_current_contents, &range_types);
14090 }
f792889a 14091 range_types[ndim++] = child_type;
a02abb62 14092 }
c906108c
SS
14093 }
14094 child_die = sibling_die (child_die);
14095 }
14096
14097 /* Dwarf2 dimensions are output from left to right, create the
14098 necessary array types in backwards order. */
7ca2d3a3 14099
c906108c 14100 type = element_type;
7ca2d3a3
DL
14101
14102 if (read_array_order (die, cu) == DW_ORD_col_major)
14103 {
14104 int i = 0;
9a619af0 14105
7ca2d3a3 14106 while (i < ndim)
dc53a7ad
JB
14107 type = create_array_type_with_stride (NULL, type, range_types[i++],
14108 bit_stride);
7ca2d3a3
DL
14109 }
14110 else
14111 {
14112 while (ndim-- > 0)
dc53a7ad
JB
14113 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14114 bit_stride);
7ca2d3a3 14115 }
c906108c 14116
f5f8a009
EZ
14117 /* Understand Dwarf2 support for vector types (like they occur on
14118 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14119 array type. This is not part of the Dwarf2/3 standard yet, but a
14120 custom vendor extension. The main difference between a regular
14121 array and the vector variant is that vectors are passed by value
14122 to functions. */
e142c38c 14123 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14124 if (attr)
ea37ba09 14125 make_vector_type (type);
f5f8a009 14126
dbc98a8b
KW
14127 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14128 implementation may choose to implement triple vectors using this
14129 attribute. */
14130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14131 if (attr)
14132 {
14133 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14134 TYPE_LENGTH (type) = DW_UNSND (attr);
14135 else
3e43a32a
MS
14136 complaint (&symfile_complaints,
14137 _("DW_AT_byte_size for array type smaller "
14138 "than the total size of elements"));
dbc98a8b
KW
14139 }
14140
39cbfefa
DJ
14141 name = dwarf2_name (die, cu);
14142 if (name)
14143 TYPE_NAME (type) = name;
6e70227d 14144
0963b4bd 14145 /* Install the type in the die. */
7e314c57
JK
14146 set_die_type (die, type, cu);
14147
14148 /* set_die_type should be already done. */
b4ba55a1
JB
14149 set_descriptive_type (type, die, cu);
14150
c906108c
SS
14151 do_cleanups (back_to);
14152
7e314c57 14153 return type;
c906108c
SS
14154}
14155
7ca2d3a3 14156static enum dwarf_array_dim_ordering
6e70227d 14157read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14158{
14159 struct attribute *attr;
14160
14161 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14162
aead7601
SM
14163 if (attr)
14164 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14165
0963b4bd
MS
14166 /* GNU F77 is a special case, as at 08/2004 array type info is the
14167 opposite order to the dwarf2 specification, but data is still
14168 laid out as per normal fortran.
7ca2d3a3 14169
0963b4bd
MS
14170 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14171 version checking. */
7ca2d3a3 14172
905e0470
PM
14173 if (cu->language == language_fortran
14174 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14175 {
14176 return DW_ORD_row_major;
14177 }
14178
6e70227d 14179 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14180 {
14181 case array_column_major:
14182 return DW_ORD_col_major;
14183 case array_row_major:
14184 default:
14185 return DW_ORD_row_major;
14186 };
14187}
14188
72019c9c 14189/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14190 the DIE's type field. */
72019c9c 14191
f792889a 14192static struct type *
72019c9c
GM
14193read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14194{
7e314c57
JK
14195 struct type *domain_type, *set_type;
14196 struct attribute *attr;
f792889a 14197
7e314c57
JK
14198 domain_type = die_type (die, cu);
14199
14200 /* The die_type call above may have already set the type for this DIE. */
14201 set_type = get_die_type (die, cu);
14202 if (set_type)
14203 return set_type;
14204
14205 set_type = create_set_type (NULL, domain_type);
14206
14207 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14208 if (attr)
14209 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14210
f792889a 14211 return set_die_type (die, set_type, cu);
72019c9c 14212}
7ca2d3a3 14213
0971de02
TT
14214/* A helper for read_common_block that creates a locexpr baton.
14215 SYM is the symbol which we are marking as computed.
14216 COMMON_DIE is the DIE for the common block.
14217 COMMON_LOC is the location expression attribute for the common
14218 block itself.
14219 MEMBER_LOC is the location expression attribute for the particular
14220 member of the common block that we are processing.
14221 CU is the CU from which the above come. */
14222
14223static void
14224mark_common_block_symbol_computed (struct symbol *sym,
14225 struct die_info *common_die,
14226 struct attribute *common_loc,
14227 struct attribute *member_loc,
14228 struct dwarf2_cu *cu)
14229{
14230 struct objfile *objfile = dwarf2_per_objfile->objfile;
14231 struct dwarf2_locexpr_baton *baton;
14232 gdb_byte *ptr;
14233 unsigned int cu_off;
14234 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14235 LONGEST offset = 0;
14236
14237 gdb_assert (common_loc && member_loc);
14238 gdb_assert (attr_form_is_block (common_loc));
14239 gdb_assert (attr_form_is_block (member_loc)
14240 || attr_form_is_constant (member_loc));
14241
8d749320 14242 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14243 baton->per_cu = cu->per_cu;
14244 gdb_assert (baton->per_cu);
14245
14246 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14247
14248 if (attr_form_is_constant (member_loc))
14249 {
14250 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14251 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14252 }
14253 else
14254 baton->size += DW_BLOCK (member_loc)->size;
14255
224c3ddb 14256 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14257 baton->data = ptr;
14258
14259 *ptr++ = DW_OP_call4;
9c541725 14260 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
14261 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14262 ptr += 4;
14263
14264 if (attr_form_is_constant (member_loc))
14265 {
14266 *ptr++ = DW_OP_addr;
14267 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14268 ptr += cu->header.addr_size;
14269 }
14270 else
14271 {
14272 /* We have to copy the data here, because DW_OP_call4 will only
14273 use a DW_AT_location attribute. */
14274 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14275 ptr += DW_BLOCK (member_loc)->size;
14276 }
14277
14278 *ptr++ = DW_OP_plus;
14279 gdb_assert (ptr - baton->data == baton->size);
14280
0971de02 14281 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14282 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14283}
14284
4357ac6c
TT
14285/* Create appropriate locally-scoped variables for all the
14286 DW_TAG_common_block entries. Also create a struct common_block
14287 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14288 is used to sepate the common blocks name namespace from regular
14289 variable names. */
c906108c
SS
14290
14291static void
e7c27a73 14292read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14293{
0971de02
TT
14294 struct attribute *attr;
14295
14296 attr = dwarf2_attr (die, DW_AT_location, cu);
14297 if (attr)
14298 {
14299 /* Support the .debug_loc offsets. */
14300 if (attr_form_is_block (attr))
14301 {
14302 /* Ok. */
14303 }
14304 else if (attr_form_is_section_offset (attr))
14305 {
14306 dwarf2_complex_location_expr_complaint ();
14307 attr = NULL;
14308 }
14309 else
14310 {
14311 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14312 "common block member");
14313 attr = NULL;
14314 }
14315 }
14316
639d11d3 14317 if (die->child != NULL)
c906108c 14318 {
4357ac6c
TT
14319 struct objfile *objfile = cu->objfile;
14320 struct die_info *child_die;
14321 size_t n_entries = 0, size;
14322 struct common_block *common_block;
14323 struct symbol *sym;
74ac6d43 14324
4357ac6c
TT
14325 for (child_die = die->child;
14326 child_die && child_die->tag;
14327 child_die = sibling_die (child_die))
14328 ++n_entries;
14329
14330 size = (sizeof (struct common_block)
14331 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14332 common_block
14333 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14334 size);
4357ac6c
TT
14335 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14336 common_block->n_entries = 0;
14337
14338 for (child_die = die->child;
14339 child_die && child_die->tag;
14340 child_die = sibling_die (child_die))
14341 {
14342 /* Create the symbol in the DW_TAG_common_block block in the current
14343 symbol scope. */
e7c27a73 14344 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14345 if (sym != NULL)
14346 {
14347 struct attribute *member_loc;
14348
14349 common_block->contents[common_block->n_entries++] = sym;
14350
14351 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14352 cu);
14353 if (member_loc)
14354 {
14355 /* GDB has handled this for a long time, but it is
14356 not specified by DWARF. It seems to have been
14357 emitted by gfortran at least as recently as:
14358 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14359 complaint (&symfile_complaints,
14360 _("Variable in common block has "
14361 "DW_AT_data_member_location "
14362 "- DIE at 0x%x [in module %s]"),
9c541725 14363 to_underlying (child_die->sect_off),
4262abfb 14364 objfile_name (cu->objfile));
0971de02
TT
14365
14366 if (attr_form_is_section_offset (member_loc))
14367 dwarf2_complex_location_expr_complaint ();
14368 else if (attr_form_is_constant (member_loc)
14369 || attr_form_is_block (member_loc))
14370 {
14371 if (attr)
14372 mark_common_block_symbol_computed (sym, die, attr,
14373 member_loc, cu);
14374 }
14375 else
14376 dwarf2_complex_location_expr_complaint ();
14377 }
14378 }
c906108c 14379 }
4357ac6c
TT
14380
14381 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14382 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14383 }
14384}
14385
0114d602 14386/* Create a type for a C++ namespace. */
d9fa45fe 14387
0114d602
DJ
14388static struct type *
14389read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14390{
e7c27a73 14391 struct objfile *objfile = cu->objfile;
0114d602 14392 const char *previous_prefix, *name;
9219021c 14393 int is_anonymous;
0114d602
DJ
14394 struct type *type;
14395
14396 /* For extensions, reuse the type of the original namespace. */
14397 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14398 {
14399 struct die_info *ext_die;
14400 struct dwarf2_cu *ext_cu = cu;
9a619af0 14401
0114d602
DJ
14402 ext_die = dwarf2_extension (die, &ext_cu);
14403 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14404
14405 /* EXT_CU may not be the same as CU.
02142a6c 14406 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14407 return set_die_type (die, type, cu);
14408 }
9219021c 14409
e142c38c 14410 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14411
14412 /* Now build the name of the current namespace. */
14413
0114d602
DJ
14414 previous_prefix = determine_prefix (die, cu);
14415 if (previous_prefix[0] != '\0')
14416 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14417 previous_prefix, name, 0, cu);
0114d602
DJ
14418
14419 /* Create the type. */
19f392bc 14420 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14421 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14422
60531b24 14423 return set_die_type (die, type, cu);
0114d602
DJ
14424}
14425
22cee43f 14426/* Read a namespace scope. */
0114d602
DJ
14427
14428static void
14429read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14430{
14431 struct objfile *objfile = cu->objfile;
0114d602 14432 int is_anonymous;
9219021c 14433
5c4e30ca
DC
14434 /* Add a symbol associated to this if we haven't seen the namespace
14435 before. Also, add a using directive if it's an anonymous
14436 namespace. */
9219021c 14437
f2f0e013 14438 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14439 {
14440 struct type *type;
14441
0114d602 14442 type = read_type_die (die, cu);
e7c27a73 14443 new_symbol (die, type, cu);
5c4e30ca 14444
e8e80198 14445 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14446 if (is_anonymous)
0114d602
DJ
14447 {
14448 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14449
22cee43f
PMR
14450 add_using_directive (using_directives (cu->language),
14451 previous_prefix, TYPE_NAME (type), NULL,
14452 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14453 }
5c4e30ca 14454 }
9219021c 14455
639d11d3 14456 if (die->child != NULL)
d9fa45fe 14457 {
639d11d3 14458 struct die_info *child_die = die->child;
6e70227d 14459
d9fa45fe
DC
14460 while (child_die && child_die->tag)
14461 {
e7c27a73 14462 process_die (child_die, cu);
d9fa45fe
DC
14463 child_die = sibling_die (child_die);
14464 }
14465 }
38d518c9
EZ
14466}
14467
f55ee35c
JK
14468/* Read a Fortran module as type. This DIE can be only a declaration used for
14469 imported module. Still we need that type as local Fortran "use ... only"
14470 declaration imports depend on the created type in determine_prefix. */
14471
14472static struct type *
14473read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14474{
14475 struct objfile *objfile = cu->objfile;
15d034d0 14476 const char *module_name;
f55ee35c
JK
14477 struct type *type;
14478
14479 module_name = dwarf2_name (die, cu);
14480 if (!module_name)
3e43a32a
MS
14481 complaint (&symfile_complaints,
14482 _("DW_TAG_module has no name, offset 0x%x"),
9c541725 14483 to_underlying (die->sect_off));
19f392bc 14484 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14485
14486 /* determine_prefix uses TYPE_TAG_NAME. */
14487 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14488
14489 return set_die_type (die, type, cu);
14490}
14491
5d7cb8df
JK
14492/* Read a Fortran module. */
14493
14494static void
14495read_module (struct die_info *die, struct dwarf2_cu *cu)
14496{
14497 struct die_info *child_die = die->child;
530e8392
KB
14498 struct type *type;
14499
14500 type = read_type_die (die, cu);
14501 new_symbol (die, type, cu);
5d7cb8df 14502
5d7cb8df
JK
14503 while (child_die && child_die->tag)
14504 {
14505 process_die (child_die, cu);
14506 child_die = sibling_die (child_die);
14507 }
14508}
14509
38d518c9
EZ
14510/* Return the name of the namespace represented by DIE. Set
14511 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14512 namespace. */
14513
14514static const char *
e142c38c 14515namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14516{
14517 struct die_info *current_die;
14518 const char *name = NULL;
14519
14520 /* Loop through the extensions until we find a name. */
14521
14522 for (current_die = die;
14523 current_die != NULL;
f2f0e013 14524 current_die = dwarf2_extension (die, &cu))
38d518c9 14525 {
96553a0c
DE
14526 /* We don't use dwarf2_name here so that we can detect the absence
14527 of a name -> anonymous namespace. */
7d45c7c3 14528 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14529
38d518c9
EZ
14530 if (name != NULL)
14531 break;
14532 }
14533
14534 /* Is it an anonymous namespace? */
14535
14536 *is_anonymous = (name == NULL);
14537 if (*is_anonymous)
2b1dbab0 14538 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14539
14540 return name;
d9fa45fe
DC
14541}
14542
c906108c
SS
14543/* Extract all information from a DW_TAG_pointer_type DIE and add to
14544 the user defined type vector. */
14545
f792889a 14546static struct type *
e7c27a73 14547read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14548{
5e2b427d 14549 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14550 struct comp_unit_head *cu_header = &cu->header;
c906108c 14551 struct type *type;
8b2dbe47
KB
14552 struct attribute *attr_byte_size;
14553 struct attribute *attr_address_class;
14554 int byte_size, addr_class;
7e314c57
JK
14555 struct type *target_type;
14556
14557 target_type = die_type (die, cu);
c906108c 14558
7e314c57
JK
14559 /* The die_type call above may have already set the type for this DIE. */
14560 type = get_die_type (die, cu);
14561 if (type)
14562 return type;
14563
14564 type = lookup_pointer_type (target_type);
8b2dbe47 14565
e142c38c 14566 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14567 if (attr_byte_size)
14568 byte_size = DW_UNSND (attr_byte_size);
c906108c 14569 else
8b2dbe47
KB
14570 byte_size = cu_header->addr_size;
14571
e142c38c 14572 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14573 if (attr_address_class)
14574 addr_class = DW_UNSND (attr_address_class);
14575 else
14576 addr_class = DW_ADDR_none;
14577
14578 /* If the pointer size or address class is different than the
14579 default, create a type variant marked as such and set the
14580 length accordingly. */
14581 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14582 {
5e2b427d 14583 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14584 {
14585 int type_flags;
14586
849957d9 14587 type_flags = gdbarch_address_class_type_flags
5e2b427d 14588 (gdbarch, byte_size, addr_class);
876cecd0
TT
14589 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14590 == 0);
8b2dbe47
KB
14591 type = make_type_with_address_space (type, type_flags);
14592 }
14593 else if (TYPE_LENGTH (type) != byte_size)
14594 {
3e43a32a
MS
14595 complaint (&symfile_complaints,
14596 _("invalid pointer size %d"), byte_size);
8b2dbe47 14597 }
6e70227d 14598 else
9a619af0
MS
14599 {
14600 /* Should we also complain about unhandled address classes? */
14601 }
c906108c 14602 }
8b2dbe47
KB
14603
14604 TYPE_LENGTH (type) = byte_size;
f792889a 14605 return set_die_type (die, type, cu);
c906108c
SS
14606}
14607
14608/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14609 the user defined type vector. */
14610
f792889a 14611static struct type *
e7c27a73 14612read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14613{
14614 struct type *type;
14615 struct type *to_type;
14616 struct type *domain;
14617
e7c27a73
DJ
14618 to_type = die_type (die, cu);
14619 domain = die_containing_type (die, cu);
0d5de010 14620
7e314c57
JK
14621 /* The calls above may have already set the type for this DIE. */
14622 type = get_die_type (die, cu);
14623 if (type)
14624 return type;
14625
0d5de010
DJ
14626 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14627 type = lookup_methodptr_type (to_type);
7078baeb
TT
14628 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14629 {
14630 struct type *new_type = alloc_type (cu->objfile);
14631
14632 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14633 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14634 TYPE_VARARGS (to_type));
14635 type = lookup_methodptr_type (new_type);
14636 }
0d5de010
DJ
14637 else
14638 type = lookup_memberptr_type (to_type, domain);
c906108c 14639
f792889a 14640 return set_die_type (die, type, cu);
c906108c
SS
14641}
14642
4297a3f0 14643/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14644 the user defined type vector. */
14645
f792889a 14646static struct type *
4297a3f0
AV
14647read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14648 enum type_code refcode)
c906108c 14649{
e7c27a73 14650 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14651 struct type *type, *target_type;
c906108c
SS
14652 struct attribute *attr;
14653
4297a3f0
AV
14654 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14655
7e314c57
JK
14656 target_type = die_type (die, cu);
14657
14658 /* The die_type call above may have already set the type for this DIE. */
14659 type = get_die_type (die, cu);
14660 if (type)
14661 return type;
14662
4297a3f0 14663 type = lookup_reference_type (target_type, refcode);
e142c38c 14664 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14665 if (attr)
14666 {
14667 TYPE_LENGTH (type) = DW_UNSND (attr);
14668 }
14669 else
14670 {
107d2387 14671 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14672 }
f792889a 14673 return set_die_type (die, type, cu);
c906108c
SS
14674}
14675
cf363f18
MW
14676/* Add the given cv-qualifiers to the element type of the array. GCC
14677 outputs DWARF type qualifiers that apply to an array, not the
14678 element type. But GDB relies on the array element type to carry
14679 the cv-qualifiers. This mimics section 6.7.3 of the C99
14680 specification. */
14681
14682static struct type *
14683add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14684 struct type *base_type, int cnst, int voltl)
14685{
14686 struct type *el_type, *inner_array;
14687
14688 base_type = copy_type (base_type);
14689 inner_array = base_type;
14690
14691 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14692 {
14693 TYPE_TARGET_TYPE (inner_array) =
14694 copy_type (TYPE_TARGET_TYPE (inner_array));
14695 inner_array = TYPE_TARGET_TYPE (inner_array);
14696 }
14697
14698 el_type = TYPE_TARGET_TYPE (inner_array);
14699 cnst |= TYPE_CONST (el_type);
14700 voltl |= TYPE_VOLATILE (el_type);
14701 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14702
14703 return set_die_type (die, base_type, cu);
14704}
14705
f792889a 14706static struct type *
e7c27a73 14707read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14708{
f792889a 14709 struct type *base_type, *cv_type;
c906108c 14710
e7c27a73 14711 base_type = die_type (die, cu);
7e314c57
JK
14712
14713 /* The die_type call above may have already set the type for this DIE. */
14714 cv_type = get_die_type (die, cu);
14715 if (cv_type)
14716 return cv_type;
14717
2f608a3a
KW
14718 /* In case the const qualifier is applied to an array type, the element type
14719 is so qualified, not the array type (section 6.7.3 of C99). */
14720 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14721 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14722
f792889a
DJ
14723 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14724 return set_die_type (die, cv_type, cu);
c906108c
SS
14725}
14726
f792889a 14727static struct type *
e7c27a73 14728read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14729{
f792889a 14730 struct type *base_type, *cv_type;
c906108c 14731
e7c27a73 14732 base_type = die_type (die, cu);
7e314c57
JK
14733
14734 /* The die_type call above may have already set the type for this DIE. */
14735 cv_type = get_die_type (die, cu);
14736 if (cv_type)
14737 return cv_type;
14738
cf363f18
MW
14739 /* In case the volatile qualifier is applied to an array type, the
14740 element type is so qualified, not the array type (section 6.7.3
14741 of C99). */
14742 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14743 return add_array_cv_type (die, cu, base_type, 0, 1);
14744
f792889a
DJ
14745 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14746 return set_die_type (die, cv_type, cu);
c906108c
SS
14747}
14748
06d66ee9
TT
14749/* Handle DW_TAG_restrict_type. */
14750
14751static struct type *
14752read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14753{
14754 struct type *base_type, *cv_type;
14755
14756 base_type = die_type (die, cu);
14757
14758 /* The die_type call above may have already set the type for this DIE. */
14759 cv_type = get_die_type (die, cu);
14760 if (cv_type)
14761 return cv_type;
14762
14763 cv_type = make_restrict_type (base_type);
14764 return set_die_type (die, cv_type, cu);
14765}
14766
a2c2acaf
MW
14767/* Handle DW_TAG_atomic_type. */
14768
14769static struct type *
14770read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14771{
14772 struct type *base_type, *cv_type;
14773
14774 base_type = die_type (die, cu);
14775
14776 /* The die_type call above may have already set the type for this DIE. */
14777 cv_type = get_die_type (die, cu);
14778 if (cv_type)
14779 return cv_type;
14780
14781 cv_type = make_atomic_type (base_type);
14782 return set_die_type (die, cv_type, cu);
14783}
14784
c906108c
SS
14785/* Extract all information from a DW_TAG_string_type DIE and add to
14786 the user defined type vector. It isn't really a user defined type,
14787 but it behaves like one, with other DIE's using an AT_user_def_type
14788 attribute to reference it. */
14789
f792889a 14790static struct type *
e7c27a73 14791read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14792{
e7c27a73 14793 struct objfile *objfile = cu->objfile;
3b7538c0 14794 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14795 struct type *type, *range_type, *index_type, *char_type;
14796 struct attribute *attr;
14797 unsigned int length;
14798
e142c38c 14799 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14800 if (attr)
14801 {
14802 length = DW_UNSND (attr);
14803 }
14804 else
14805 {
0963b4bd 14806 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14807 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14808 if (attr)
14809 {
14810 length = DW_UNSND (attr);
14811 }
14812 else
14813 {
14814 length = 1;
14815 }
c906108c 14816 }
6ccb9162 14817
46bf5051 14818 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14819 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14820 char_type = language_string_char_type (cu->language_defn, gdbarch);
14821 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14822
f792889a 14823 return set_die_type (die, type, cu);
c906108c
SS
14824}
14825
4d804846
JB
14826/* Assuming that DIE corresponds to a function, returns nonzero
14827 if the function is prototyped. */
14828
14829static int
14830prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14831{
14832 struct attribute *attr;
14833
14834 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14835 if (attr && (DW_UNSND (attr) != 0))
14836 return 1;
14837
14838 /* The DWARF standard implies that the DW_AT_prototyped attribute
14839 is only meaninful for C, but the concept also extends to other
14840 languages that allow unprototyped functions (Eg: Objective C).
14841 For all other languages, assume that functions are always
14842 prototyped. */
14843 if (cu->language != language_c
14844 && cu->language != language_objc
14845 && cu->language != language_opencl)
14846 return 1;
14847
14848 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14849 prototyped and unprototyped functions; default to prototyped,
14850 since that is more common in modern code (and RealView warns
14851 about unprototyped functions). */
14852 if (producer_is_realview (cu->producer))
14853 return 1;
14854
14855 return 0;
14856}
14857
c906108c
SS
14858/* Handle DIES due to C code like:
14859
14860 struct foo
c5aa993b
JM
14861 {
14862 int (*funcp)(int a, long l);
14863 int b;
14864 };
c906108c 14865
0963b4bd 14866 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14867
f792889a 14868static struct type *
e7c27a73 14869read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14870{
bb5ed363 14871 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14872 struct type *type; /* Type that this function returns. */
14873 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14874 struct attribute *attr;
14875
e7c27a73 14876 type = die_type (die, cu);
7e314c57
JK
14877
14878 /* The die_type call above may have already set the type for this DIE. */
14879 ftype = get_die_type (die, cu);
14880 if (ftype)
14881 return ftype;
14882
0c8b41f1 14883 ftype = lookup_function_type (type);
c906108c 14884
4d804846 14885 if (prototyped_function_p (die, cu))
a6c727b2 14886 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14887
c055b101
CV
14888 /* Store the calling convention in the type if it's available in
14889 the subroutine die. Otherwise set the calling convention to
14890 the default value DW_CC_normal. */
14891 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14892 if (attr)
14893 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14894 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14895 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14896 else
14897 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14898
743649fd
MW
14899 /* Record whether the function returns normally to its caller or not
14900 if the DWARF producer set that information. */
14901 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14902 if (attr && (DW_UNSND (attr) != 0))
14903 TYPE_NO_RETURN (ftype) = 1;
14904
76c10ea2
GM
14905 /* We need to add the subroutine type to the die immediately so
14906 we don't infinitely recurse when dealing with parameters
0963b4bd 14907 declared as the same subroutine type. */
76c10ea2 14908 set_die_type (die, ftype, cu);
6e70227d 14909
639d11d3 14910 if (die->child != NULL)
c906108c 14911 {
bb5ed363 14912 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14913 struct die_info *child_die;
8072405b 14914 int nparams, iparams;
c906108c
SS
14915
14916 /* Count the number of parameters.
14917 FIXME: GDB currently ignores vararg functions, but knows about
14918 vararg member functions. */
8072405b 14919 nparams = 0;
639d11d3 14920 child_die = die->child;
c906108c
SS
14921 while (child_die && child_die->tag)
14922 {
14923 if (child_die->tag == DW_TAG_formal_parameter)
14924 nparams++;
14925 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14926 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14927 child_die = sibling_die (child_die);
14928 }
14929
14930 /* Allocate storage for parameters and fill them in. */
14931 TYPE_NFIELDS (ftype) = nparams;
14932 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14933 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14934
8072405b
JK
14935 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14936 even if we error out during the parameters reading below. */
14937 for (iparams = 0; iparams < nparams; iparams++)
14938 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14939
14940 iparams = 0;
639d11d3 14941 child_die = die->child;
c906108c
SS
14942 while (child_die && child_die->tag)
14943 {
14944 if (child_die->tag == DW_TAG_formal_parameter)
14945 {
3ce3b1ba
PA
14946 struct type *arg_type;
14947
14948 /* DWARF version 2 has no clean way to discern C++
14949 static and non-static member functions. G++ helps
14950 GDB by marking the first parameter for non-static
14951 member functions (which is the this pointer) as
14952 artificial. We pass this information to
14953 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14954
14955 DWARF version 3 added DW_AT_object_pointer, which GCC
14956 4.5 does not yet generate. */
e142c38c 14957 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14958 if (attr)
14959 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14960 else
9c37b5ae 14961 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
14962 arg_type = die_type (child_die, cu);
14963
14964 /* RealView does not mark THIS as const, which the testsuite
14965 expects. GCC marks THIS as const in method definitions,
14966 but not in the class specifications (GCC PR 43053). */
14967 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14968 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14969 {
14970 int is_this = 0;
14971 struct dwarf2_cu *arg_cu = cu;
14972 const char *name = dwarf2_name (child_die, cu);
14973
14974 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14975 if (attr)
14976 {
14977 /* If the compiler emits this, use it. */
14978 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14979 is_this = 1;
14980 }
14981 else if (name && strcmp (name, "this") == 0)
14982 /* Function definitions will have the argument names. */
14983 is_this = 1;
14984 else if (name == NULL && iparams == 0)
14985 /* Declarations may not have the names, so like
14986 elsewhere in GDB, assume an artificial first
14987 argument is "this". */
14988 is_this = 1;
14989
14990 if (is_this)
14991 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14992 arg_type, 0);
14993 }
14994
14995 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14996 iparams++;
14997 }
14998 child_die = sibling_die (child_die);
14999 }
15000 }
15001
76c10ea2 15002 return ftype;
c906108c
SS
15003}
15004
f792889a 15005static struct type *
e7c27a73 15006read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15007{
e7c27a73 15008 struct objfile *objfile = cu->objfile;
0114d602 15009 const char *name = NULL;
3c8e0968 15010 struct type *this_type, *target_type;
c906108c 15011
94af9270 15012 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
15013 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15014 TYPE_TARGET_STUB (this_type) = 1;
f792889a 15015 set_die_type (die, this_type, cu);
3c8e0968
DE
15016 target_type = die_type (die, cu);
15017 if (target_type != this_type)
15018 TYPE_TARGET_TYPE (this_type) = target_type;
15019 else
15020 {
15021 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15022 spec and cause infinite loops in GDB. */
15023 complaint (&symfile_complaints,
15024 _("Self-referential DW_TAG_typedef "
15025 "- DIE at 0x%x [in module %s]"),
9c541725 15026 to_underlying (die->sect_off), objfile_name (objfile));
3c8e0968
DE
15027 TYPE_TARGET_TYPE (this_type) = NULL;
15028 }
f792889a 15029 return this_type;
c906108c
SS
15030}
15031
9b790ce7
UW
15032/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15033 (which may be different from NAME) to the architecture back-end to allow
15034 it to guess the correct format if necessary. */
15035
15036static struct type *
15037dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15038 const char *name_hint)
15039{
15040 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15041 const struct floatformat **format;
15042 struct type *type;
15043
15044 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15045 if (format)
15046 type = init_float_type (objfile, bits, name, format);
15047 else
15048 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15049
15050 return type;
15051}
15052
c906108c
SS
15053/* Find a representation of a given base type and install
15054 it in the TYPE field of the die. */
15055
f792889a 15056static struct type *
e7c27a73 15057read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15058{
e7c27a73 15059 struct objfile *objfile = cu->objfile;
c906108c
SS
15060 struct type *type;
15061 struct attribute *attr;
19f392bc 15062 int encoding = 0, bits = 0;
15d034d0 15063 const char *name;
c906108c 15064
e142c38c 15065 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15066 if (attr)
15067 {
15068 encoding = DW_UNSND (attr);
15069 }
e142c38c 15070 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15071 if (attr)
15072 {
19f392bc 15073 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15074 }
39cbfefa 15075 name = dwarf2_name (die, cu);
6ccb9162 15076 if (!name)
c906108c 15077 {
6ccb9162
UW
15078 complaint (&symfile_complaints,
15079 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15080 }
6ccb9162
UW
15081
15082 switch (encoding)
c906108c 15083 {
6ccb9162
UW
15084 case DW_ATE_address:
15085 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15086 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15087 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15088 break;
15089 case DW_ATE_boolean:
19f392bc 15090 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15091 break;
15092 case DW_ATE_complex_float:
9b790ce7 15093 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15094 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15095 break;
15096 case DW_ATE_decimal_float:
19f392bc 15097 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15098 break;
15099 case DW_ATE_float:
9b790ce7 15100 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15101 break;
15102 case DW_ATE_signed:
19f392bc 15103 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15104 break;
15105 case DW_ATE_unsigned:
3b2b8fea
TT
15106 if (cu->language == language_fortran
15107 && name
61012eef 15108 && startswith (name, "character("))
19f392bc
UW
15109 type = init_character_type (objfile, bits, 1, name);
15110 else
15111 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15112 break;
15113 case DW_ATE_signed_char:
6e70227d 15114 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15115 || cu->language == language_pascal
15116 || cu->language == language_fortran)
19f392bc
UW
15117 type = init_character_type (objfile, bits, 0, name);
15118 else
15119 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15120 break;
15121 case DW_ATE_unsigned_char:
868a0084 15122 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15123 || cu->language == language_pascal
c44af4eb
TT
15124 || cu->language == language_fortran
15125 || cu->language == language_rust)
19f392bc
UW
15126 type = init_character_type (objfile, bits, 1, name);
15127 else
15128 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15129 break;
75079b2b 15130 case DW_ATE_UTF:
53e710ac
PA
15131 {
15132 gdbarch *arch = get_objfile_arch (objfile);
15133
15134 if (bits == 16)
15135 type = builtin_type (arch)->builtin_char16;
15136 else if (bits == 32)
15137 type = builtin_type (arch)->builtin_char32;
15138 else
15139 {
15140 complaint (&symfile_complaints,
15141 _("unsupported DW_ATE_UTF bit size: '%d'"),
15142 bits);
15143 type = init_integer_type (objfile, bits, 1, name);
15144 }
15145 return set_die_type (die, type, cu);
15146 }
75079b2b
TT
15147 break;
15148
6ccb9162
UW
15149 default:
15150 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15151 dwarf_type_encoding_name (encoding));
19f392bc
UW
15152 type = init_type (objfile, TYPE_CODE_ERROR,
15153 bits / TARGET_CHAR_BIT, name);
6ccb9162 15154 break;
c906108c 15155 }
6ccb9162 15156
0114d602 15157 if (name && strcmp (name, "char") == 0)
876cecd0 15158 TYPE_NOSIGN (type) = 1;
0114d602 15159
f792889a 15160 return set_die_type (die, type, cu);
c906108c
SS
15161}
15162
80180f79
SA
15163/* Parse dwarf attribute if it's a block, reference or constant and put the
15164 resulting value of the attribute into struct bound_prop.
15165 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15166
15167static int
15168attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15169 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15170{
15171 struct dwarf2_property_baton *baton;
15172 struct obstack *obstack = &cu->objfile->objfile_obstack;
15173
15174 if (attr == NULL || prop == NULL)
15175 return 0;
15176
15177 if (attr_form_is_block (attr))
15178 {
8d749320 15179 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15180 baton->referenced_type = NULL;
15181 baton->locexpr.per_cu = cu->per_cu;
15182 baton->locexpr.size = DW_BLOCK (attr)->size;
15183 baton->locexpr.data = DW_BLOCK (attr)->data;
15184 prop->data.baton = baton;
15185 prop->kind = PROP_LOCEXPR;
15186 gdb_assert (prop->data.baton != NULL);
15187 }
15188 else if (attr_form_is_ref (attr))
15189 {
15190 struct dwarf2_cu *target_cu = cu;
15191 struct die_info *target_die;
15192 struct attribute *target_attr;
15193
15194 target_die = follow_die_ref (die, attr, &target_cu);
15195 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15196 if (target_attr == NULL)
15197 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15198 target_cu);
80180f79
SA
15199 if (target_attr == NULL)
15200 return 0;
15201
df25ebbd 15202 switch (target_attr->name)
80180f79 15203 {
df25ebbd
JB
15204 case DW_AT_location:
15205 if (attr_form_is_section_offset (target_attr))
15206 {
8d749320 15207 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15208 baton->referenced_type = die_type (target_die, target_cu);
15209 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15210 prop->data.baton = baton;
15211 prop->kind = PROP_LOCLIST;
15212 gdb_assert (prop->data.baton != NULL);
15213 }
15214 else if (attr_form_is_block (target_attr))
15215 {
8d749320 15216 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15217 baton->referenced_type = die_type (target_die, target_cu);
15218 baton->locexpr.per_cu = cu->per_cu;
15219 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15220 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15221 prop->data.baton = baton;
15222 prop->kind = PROP_LOCEXPR;
15223 gdb_assert (prop->data.baton != NULL);
15224 }
15225 else
15226 {
15227 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15228 "dynamic property");
15229 return 0;
15230 }
15231 break;
15232 case DW_AT_data_member_location:
15233 {
15234 LONGEST offset;
15235
15236 if (!handle_data_member_location (target_die, target_cu,
15237 &offset))
15238 return 0;
15239
8d749320 15240 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15241 baton->referenced_type = read_type_die (target_die->parent,
15242 target_cu);
df25ebbd
JB
15243 baton->offset_info.offset = offset;
15244 baton->offset_info.type = die_type (target_die, target_cu);
15245 prop->data.baton = baton;
15246 prop->kind = PROP_ADDR_OFFSET;
15247 break;
15248 }
80180f79
SA
15249 }
15250 }
15251 else if (attr_form_is_constant (attr))
15252 {
15253 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15254 prop->kind = PROP_CONST;
15255 }
15256 else
15257 {
15258 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15259 dwarf2_name (die, cu));
15260 return 0;
15261 }
15262
15263 return 1;
15264}
15265
a02abb62
JB
15266/* Read the given DW_AT_subrange DIE. */
15267
f792889a 15268static struct type *
a02abb62
JB
15269read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15270{
4c9ad8c2 15271 struct type *base_type, *orig_base_type;
a02abb62
JB
15272 struct type *range_type;
15273 struct attribute *attr;
729efb13 15274 struct dynamic_prop low, high;
4fae6e18 15275 int low_default_is_valid;
c451ebe5 15276 int high_bound_is_count = 0;
15d034d0 15277 const char *name;
43bbcdc2 15278 LONGEST negative_mask;
e77813c8 15279
4c9ad8c2
TT
15280 orig_base_type = die_type (die, cu);
15281 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15282 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15283 creating the range type, but we use the result of check_typedef
15284 when examining properties of the type. */
15285 base_type = check_typedef (orig_base_type);
a02abb62 15286
7e314c57
JK
15287 /* The die_type call above may have already set the type for this DIE. */
15288 range_type = get_die_type (die, cu);
15289 if (range_type)
15290 return range_type;
15291
729efb13
SA
15292 low.kind = PROP_CONST;
15293 high.kind = PROP_CONST;
15294 high.data.const_val = 0;
15295
4fae6e18
JK
15296 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15297 omitting DW_AT_lower_bound. */
15298 switch (cu->language)
6e70227d 15299 {
4fae6e18
JK
15300 case language_c:
15301 case language_cplus:
729efb13 15302 low.data.const_val = 0;
4fae6e18
JK
15303 low_default_is_valid = 1;
15304 break;
15305 case language_fortran:
729efb13 15306 low.data.const_val = 1;
4fae6e18
JK
15307 low_default_is_valid = 1;
15308 break;
15309 case language_d:
4fae6e18 15310 case language_objc:
c44af4eb 15311 case language_rust:
729efb13 15312 low.data.const_val = 0;
4fae6e18
JK
15313 low_default_is_valid = (cu->header.version >= 4);
15314 break;
15315 case language_ada:
15316 case language_m2:
15317 case language_pascal:
729efb13 15318 low.data.const_val = 1;
4fae6e18
JK
15319 low_default_is_valid = (cu->header.version >= 4);
15320 break;
15321 default:
729efb13 15322 low.data.const_val = 0;
4fae6e18
JK
15323 low_default_is_valid = 0;
15324 break;
a02abb62
JB
15325 }
15326
e142c38c 15327 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15328 if (attr)
11c1ba78 15329 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15330 else if (!low_default_is_valid)
15331 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15332 "- DIE at 0x%x [in module %s]"),
9c541725 15333 to_underlying (die->sect_off), objfile_name (cu->objfile));
a02abb62 15334
e142c38c 15335 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15336 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15337 {
15338 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15339 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15340 {
c451ebe5
SA
15341 /* If bounds are constant do the final calculation here. */
15342 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15343 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15344 else
15345 high_bound_is_count = 1;
c2ff108b 15346 }
e77813c8
PM
15347 }
15348
15349 /* Dwarf-2 specifications explicitly allows to create subrange types
15350 without specifying a base type.
15351 In that case, the base type must be set to the type of
15352 the lower bound, upper bound or count, in that order, if any of these
15353 three attributes references an object that has a type.
15354 If no base type is found, the Dwarf-2 specifications say that
15355 a signed integer type of size equal to the size of an address should
15356 be used.
15357 For the following C code: `extern char gdb_int [];'
15358 GCC produces an empty range DIE.
15359 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15360 high bound or count are not yet handled by this code. */
e77813c8
PM
15361 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15362 {
15363 struct objfile *objfile = cu->objfile;
15364 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15365 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15366 struct type *int_type = objfile_type (objfile)->builtin_int;
15367
15368 /* Test "int", "long int", and "long long int" objfile types,
15369 and select the first one having a size above or equal to the
15370 architecture address size. */
15371 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15372 base_type = int_type;
15373 else
15374 {
15375 int_type = objfile_type (objfile)->builtin_long;
15376 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15377 base_type = int_type;
15378 else
15379 {
15380 int_type = objfile_type (objfile)->builtin_long_long;
15381 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15382 base_type = int_type;
15383 }
15384 }
15385 }
a02abb62 15386
dbb9c2b1
JB
15387 /* Normally, the DWARF producers are expected to use a signed
15388 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15389 But this is unfortunately not always the case, as witnessed
15390 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15391 is used instead. To work around that ambiguity, we treat
15392 the bounds as signed, and thus sign-extend their values, when
15393 the base type is signed. */
6e70227d 15394 negative_mask =
66c6502d 15395 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15396 if (low.kind == PROP_CONST
15397 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15398 low.data.const_val |= negative_mask;
15399 if (high.kind == PROP_CONST
15400 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15401 high.data.const_val |= negative_mask;
43bbcdc2 15402
729efb13 15403 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15404
c451ebe5
SA
15405 if (high_bound_is_count)
15406 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15407
c2ff108b
JK
15408 /* Ada expects an empty array on no boundary attributes. */
15409 if (attr == NULL && cu->language != language_ada)
729efb13 15410 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15411
39cbfefa
DJ
15412 name = dwarf2_name (die, cu);
15413 if (name)
15414 TYPE_NAME (range_type) = name;
6e70227d 15415
e142c38c 15416 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15417 if (attr)
15418 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15419
7e314c57
JK
15420 set_die_type (die, range_type, cu);
15421
15422 /* set_die_type should be already done. */
b4ba55a1
JB
15423 set_descriptive_type (range_type, die, cu);
15424
7e314c57 15425 return range_type;
a02abb62 15426}
6e70227d 15427
f792889a 15428static struct type *
81a17f79
JB
15429read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15430{
15431 struct type *type;
81a17f79 15432
81a17f79
JB
15433 /* For now, we only support the C meaning of an unspecified type: void. */
15434
19f392bc 15435 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15436 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15437
f792889a 15438 return set_die_type (die, type, cu);
81a17f79 15439}
a02abb62 15440
639d11d3
DC
15441/* Read a single die and all its descendents. Set the die's sibling
15442 field to NULL; set other fields in the die correctly, and set all
15443 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15444 location of the info_ptr after reading all of those dies. PARENT
15445 is the parent of the die in question. */
15446
15447static struct die_info *
dee91e82 15448read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15449 const gdb_byte *info_ptr,
15450 const gdb_byte **new_info_ptr,
dee91e82 15451 struct die_info *parent)
639d11d3
DC
15452{
15453 struct die_info *die;
d521ce57 15454 const gdb_byte *cur_ptr;
639d11d3
DC
15455 int has_children;
15456
bf6af496 15457 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15458 if (die == NULL)
15459 {
15460 *new_info_ptr = cur_ptr;
15461 return NULL;
15462 }
93311388 15463 store_in_ref_table (die, reader->cu);
639d11d3
DC
15464
15465 if (has_children)
bf6af496 15466 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15467 else
15468 {
15469 die->child = NULL;
15470 *new_info_ptr = cur_ptr;
15471 }
15472
15473 die->sibling = NULL;
15474 die->parent = parent;
15475 return die;
15476}
15477
15478/* Read a die, all of its descendents, and all of its siblings; set
15479 all of the fields of all of the dies correctly. Arguments are as
15480 in read_die_and_children. */
15481
15482static struct die_info *
bf6af496 15483read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15484 const gdb_byte *info_ptr,
15485 const gdb_byte **new_info_ptr,
bf6af496 15486 struct die_info *parent)
639d11d3
DC
15487{
15488 struct die_info *first_die, *last_sibling;
d521ce57 15489 const gdb_byte *cur_ptr;
639d11d3 15490
c906108c 15491 cur_ptr = info_ptr;
639d11d3
DC
15492 first_die = last_sibling = NULL;
15493
15494 while (1)
c906108c 15495 {
639d11d3 15496 struct die_info *die
dee91e82 15497 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15498
1d325ec1 15499 if (die == NULL)
c906108c 15500 {
639d11d3
DC
15501 *new_info_ptr = cur_ptr;
15502 return first_die;
c906108c 15503 }
1d325ec1
DJ
15504
15505 if (!first_die)
15506 first_die = die;
c906108c 15507 else
1d325ec1
DJ
15508 last_sibling->sibling = die;
15509
15510 last_sibling = die;
c906108c 15511 }
c906108c
SS
15512}
15513
bf6af496
DE
15514/* Read a die, all of its descendents, and all of its siblings; set
15515 all of the fields of all of the dies correctly. Arguments are as
15516 in read_die_and_children.
15517 This the main entry point for reading a DIE and all its children. */
15518
15519static struct die_info *
15520read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15521 const gdb_byte *info_ptr,
15522 const gdb_byte **new_info_ptr,
bf6af496
DE
15523 struct die_info *parent)
15524{
15525 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15526 new_info_ptr, parent);
15527
b4f54984 15528 if (dwarf_die_debug)
bf6af496
DE
15529 {
15530 fprintf_unfiltered (gdb_stdlog,
15531 "Read die from %s@0x%x of %s:\n",
a32a8923 15532 get_section_name (reader->die_section),
bf6af496
DE
15533 (unsigned) (info_ptr - reader->die_section->buffer),
15534 bfd_get_filename (reader->abfd));
b4f54984 15535 dump_die (die, dwarf_die_debug);
bf6af496
DE
15536 }
15537
15538 return die;
15539}
15540
3019eac3
DE
15541/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15542 attributes.
15543 The caller is responsible for filling in the extra attributes
15544 and updating (*DIEP)->num_attrs.
15545 Set DIEP to point to a newly allocated die with its information,
15546 except for its child, sibling, and parent fields.
15547 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15548
d521ce57 15549static const gdb_byte *
3019eac3 15550read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15551 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15552 int *has_children, int num_extra_attrs)
93311388 15553{
b64f50a1 15554 unsigned int abbrev_number, bytes_read, i;
93311388
DE
15555 struct abbrev_info *abbrev;
15556 struct die_info *die;
15557 struct dwarf2_cu *cu = reader->cu;
15558 bfd *abfd = reader->abfd;
15559
9c541725 15560 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
15561 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15562 info_ptr += bytes_read;
15563 if (!abbrev_number)
15564 {
15565 *diep = NULL;
15566 *has_children = 0;
15567 return info_ptr;
15568 }
15569
433df2d4 15570 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15571 if (!abbrev)
348e048f
DE
15572 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15573 abbrev_number,
15574 bfd_get_filename (abfd));
15575
3019eac3 15576 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 15577 die->sect_off = sect_off;
93311388
DE
15578 die->tag = abbrev->tag;
15579 die->abbrev = abbrev_number;
15580
3019eac3
DE
15581 /* Make the result usable.
15582 The caller needs to update num_attrs after adding the extra
15583 attributes. */
93311388
DE
15584 die->num_attrs = abbrev->num_attrs;
15585
15586 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15587 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15588 info_ptr);
93311388
DE
15589
15590 *diep = die;
15591 *has_children = abbrev->has_children;
15592 return info_ptr;
15593}
15594
3019eac3
DE
15595/* Read a die and all its attributes.
15596 Set DIEP to point to a newly allocated die with its information,
15597 except for its child, sibling, and parent fields.
15598 Set HAS_CHILDREN to tell whether the die has children or not. */
15599
d521ce57 15600static const gdb_byte *
3019eac3 15601read_full_die (const struct die_reader_specs *reader,
d521ce57 15602 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15603 int *has_children)
15604{
d521ce57 15605 const gdb_byte *result;
bf6af496
DE
15606
15607 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15608
b4f54984 15609 if (dwarf_die_debug)
bf6af496
DE
15610 {
15611 fprintf_unfiltered (gdb_stdlog,
15612 "Read die from %s@0x%x of %s:\n",
a32a8923 15613 get_section_name (reader->die_section),
bf6af496
DE
15614 (unsigned) (info_ptr - reader->die_section->buffer),
15615 bfd_get_filename (reader->abfd));
b4f54984 15616 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15617 }
15618
15619 return result;
3019eac3 15620}
433df2d4
DE
15621\f
15622/* Abbreviation tables.
3019eac3 15623
433df2d4 15624 In DWARF version 2, the description of the debugging information is
c906108c
SS
15625 stored in a separate .debug_abbrev section. Before we read any
15626 dies from a section we read in all abbreviations and install them
433df2d4
DE
15627 in a hash table. */
15628
15629/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15630
15631static struct abbrev_info *
15632abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15633{
15634 struct abbrev_info *abbrev;
15635
8d749320 15636 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15637 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15638
433df2d4
DE
15639 return abbrev;
15640}
15641
15642/* Add an abbreviation to the table. */
c906108c
SS
15643
15644static void
433df2d4
DE
15645abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15646 unsigned int abbrev_number,
15647 struct abbrev_info *abbrev)
15648{
15649 unsigned int hash_number;
15650
15651 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15652 abbrev->next = abbrev_table->abbrevs[hash_number];
15653 abbrev_table->abbrevs[hash_number] = abbrev;
15654}
dee91e82 15655
433df2d4
DE
15656/* Look up an abbrev in the table.
15657 Returns NULL if the abbrev is not found. */
15658
15659static struct abbrev_info *
15660abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15661 unsigned int abbrev_number)
c906108c 15662{
433df2d4
DE
15663 unsigned int hash_number;
15664 struct abbrev_info *abbrev;
15665
15666 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15667 abbrev = abbrev_table->abbrevs[hash_number];
15668
15669 while (abbrev)
15670 {
15671 if (abbrev->number == abbrev_number)
15672 return abbrev;
15673 abbrev = abbrev->next;
15674 }
15675 return NULL;
15676}
15677
15678/* Read in an abbrev table. */
15679
15680static struct abbrev_table *
15681abbrev_table_read_table (struct dwarf2_section_info *section,
9c541725 15682 sect_offset sect_off)
433df2d4
DE
15683{
15684 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15685 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15686 struct abbrev_table *abbrev_table;
d521ce57 15687 const gdb_byte *abbrev_ptr;
c906108c
SS
15688 struct abbrev_info *cur_abbrev;
15689 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15690 unsigned int abbrev_form;
f3dd6933
DJ
15691 struct attr_abbrev *cur_attrs;
15692 unsigned int allocated_attrs;
c906108c 15693
70ba0933 15694 abbrev_table = XNEW (struct abbrev_table);
9c541725 15695 abbrev_table->sect_off = sect_off;
433df2d4 15696 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15697 abbrev_table->abbrevs =
15698 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15699 ABBREV_HASH_SIZE);
433df2d4
DE
15700 memset (abbrev_table->abbrevs, 0,
15701 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15702
433df2d4 15703 dwarf2_read_section (objfile, section);
9c541725 15704 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
15705 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15706 abbrev_ptr += bytes_read;
15707
f3dd6933 15708 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15709 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15710
0963b4bd 15711 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15712 while (abbrev_number)
15713 {
433df2d4 15714 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15715
15716 /* read in abbrev header */
15717 cur_abbrev->number = abbrev_number;
aead7601
SM
15718 cur_abbrev->tag
15719 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15720 abbrev_ptr += bytes_read;
15721 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15722 abbrev_ptr += 1;
15723
15724 /* now read in declarations */
22d2f3ab 15725 for (;;)
c906108c 15726 {
43988095
JK
15727 LONGEST implicit_const;
15728
22d2f3ab
JK
15729 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15730 abbrev_ptr += bytes_read;
15731 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15732 abbrev_ptr += bytes_read;
43988095
JK
15733 if (abbrev_form == DW_FORM_implicit_const)
15734 {
15735 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15736 &bytes_read);
15737 abbrev_ptr += bytes_read;
15738 }
15739 else
15740 {
15741 /* Initialize it due to a false compiler warning. */
15742 implicit_const = -1;
15743 }
22d2f3ab
JK
15744
15745 if (abbrev_name == 0)
15746 break;
15747
f3dd6933 15748 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15749 {
f3dd6933
DJ
15750 allocated_attrs += ATTR_ALLOC_CHUNK;
15751 cur_attrs
224c3ddb 15752 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15753 }
ae038cb0 15754
aead7601
SM
15755 cur_attrs[cur_abbrev->num_attrs].name
15756 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15757 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15758 = (enum dwarf_form) abbrev_form;
43988095 15759 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15760 ++cur_abbrev->num_attrs;
c906108c
SS
15761 }
15762
8d749320
SM
15763 cur_abbrev->attrs =
15764 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15765 cur_abbrev->num_attrs);
f3dd6933
DJ
15766 memcpy (cur_abbrev->attrs, cur_attrs,
15767 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15768
433df2d4 15769 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15770
15771 /* Get next abbreviation.
15772 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15773 always properly terminated with an abbrev number of 0.
15774 Exit loop if we encounter an abbreviation which we have
15775 already read (which means we are about to read the abbreviations
15776 for the next compile unit) or if the end of the abbreviation
15777 table is reached. */
433df2d4 15778 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15779 break;
15780 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15781 abbrev_ptr += bytes_read;
433df2d4 15782 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15783 break;
15784 }
f3dd6933
DJ
15785
15786 xfree (cur_attrs);
433df2d4 15787 return abbrev_table;
c906108c
SS
15788}
15789
433df2d4 15790/* Free the resources held by ABBREV_TABLE. */
c906108c 15791
c906108c 15792static void
433df2d4 15793abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15794{
433df2d4
DE
15795 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15796 xfree (abbrev_table);
c906108c
SS
15797}
15798
f4dc4d17
DE
15799/* Same as abbrev_table_free but as a cleanup.
15800 We pass in a pointer to the pointer to the table so that we can
15801 set the pointer to NULL when we're done. It also simplifies
73051182 15802 build_type_psymtabs_1. */
f4dc4d17
DE
15803
15804static void
15805abbrev_table_free_cleanup (void *table_ptr)
15806{
9a3c8263 15807 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15808
15809 if (*abbrev_table_ptr != NULL)
15810 abbrev_table_free (*abbrev_table_ptr);
15811 *abbrev_table_ptr = NULL;
15812}
15813
433df2d4
DE
15814/* Read the abbrev table for CU from ABBREV_SECTION. */
15815
15816static void
15817dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15818 struct dwarf2_section_info *abbrev_section)
c906108c 15819{
433df2d4 15820 cu->abbrev_table =
9c541725 15821 abbrev_table_read_table (abbrev_section, cu->header.abbrev_sect_off);
433df2d4 15822}
c906108c 15823
433df2d4 15824/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15825
433df2d4
DE
15826static void
15827dwarf2_free_abbrev_table (void *ptr_to_cu)
15828{
9a3c8263 15829 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15830
a2ce51a0
DE
15831 if (cu->abbrev_table != NULL)
15832 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15833 /* Set this to NULL so that we SEGV if we try to read it later,
15834 and also because free_comp_unit verifies this is NULL. */
15835 cu->abbrev_table = NULL;
15836}
15837\f
72bf9492
DJ
15838/* Returns nonzero if TAG represents a type that we might generate a partial
15839 symbol for. */
15840
15841static int
15842is_type_tag_for_partial (int tag)
15843{
15844 switch (tag)
15845 {
15846#if 0
15847 /* Some types that would be reasonable to generate partial symbols for,
15848 that we don't at present. */
15849 case DW_TAG_array_type:
15850 case DW_TAG_file_type:
15851 case DW_TAG_ptr_to_member_type:
15852 case DW_TAG_set_type:
15853 case DW_TAG_string_type:
15854 case DW_TAG_subroutine_type:
15855#endif
15856 case DW_TAG_base_type:
15857 case DW_TAG_class_type:
680b30c7 15858 case DW_TAG_interface_type:
72bf9492
DJ
15859 case DW_TAG_enumeration_type:
15860 case DW_TAG_structure_type:
15861 case DW_TAG_subrange_type:
15862 case DW_TAG_typedef:
15863 case DW_TAG_union_type:
15864 return 1;
15865 default:
15866 return 0;
15867 }
15868}
15869
15870/* Load all DIEs that are interesting for partial symbols into memory. */
15871
15872static struct partial_die_info *
dee91e82 15873load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15874 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15875{
dee91e82 15876 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15877 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15878 struct partial_die_info *part_die;
15879 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15880 struct abbrev_info *abbrev;
15881 unsigned int bytes_read;
5afb4e99 15882 unsigned int load_all = 0;
72bf9492
DJ
15883 int nesting_level = 1;
15884
15885 parent_die = NULL;
15886 last_die = NULL;
15887
7adf1e79
DE
15888 gdb_assert (cu->per_cu != NULL);
15889 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15890 load_all = 1;
15891
72bf9492
DJ
15892 cu->partial_dies
15893 = htab_create_alloc_ex (cu->header.length / 12,
15894 partial_die_hash,
15895 partial_die_eq,
15896 NULL,
15897 &cu->comp_unit_obstack,
15898 hashtab_obstack_allocate,
15899 dummy_obstack_deallocate);
15900
8d749320 15901 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15902
15903 while (1)
15904 {
15905 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15906
15907 /* A NULL abbrev means the end of a series of children. */
15908 if (abbrev == NULL)
15909 {
15910 if (--nesting_level == 0)
15911 {
15912 /* PART_DIE was probably the last thing allocated on the
15913 comp_unit_obstack, so we could call obstack_free
15914 here. We don't do that because the waste is small,
15915 and will be cleaned up when we're done with this
15916 compilation unit. This way, we're also more robust
15917 against other users of the comp_unit_obstack. */
15918 return first_die;
15919 }
15920 info_ptr += bytes_read;
15921 last_die = parent_die;
15922 parent_die = parent_die->die_parent;
15923 continue;
15924 }
15925
98bfdba5
PA
15926 /* Check for template arguments. We never save these; if
15927 they're seen, we just mark the parent, and go on our way. */
15928 if (parent_die != NULL
15929 && cu->language == language_cplus
15930 && (abbrev->tag == DW_TAG_template_type_param
15931 || abbrev->tag == DW_TAG_template_value_param))
15932 {
15933 parent_die->has_template_arguments = 1;
15934
15935 if (!load_all)
15936 {
15937 /* We don't need a partial DIE for the template argument. */
dee91e82 15938 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15939 continue;
15940 }
15941 }
15942
0d99eb77 15943 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15944 Skip their other children. */
15945 if (!load_all
15946 && cu->language == language_cplus
15947 && parent_die != NULL
15948 && parent_die->tag == DW_TAG_subprogram)
15949 {
dee91e82 15950 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15951 continue;
15952 }
15953
5afb4e99
DJ
15954 /* Check whether this DIE is interesting enough to save. Normally
15955 we would not be interested in members here, but there may be
15956 later variables referencing them via DW_AT_specification (for
15957 static members). */
15958 if (!load_all
15959 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15960 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15961 && abbrev->tag != DW_TAG_enumerator
15962 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15963 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15964 && abbrev->tag != DW_TAG_variable
5afb4e99 15965 && abbrev->tag != DW_TAG_namespace
f55ee35c 15966 && abbrev->tag != DW_TAG_module
95554aad 15967 && abbrev->tag != DW_TAG_member
74921315
KS
15968 && abbrev->tag != DW_TAG_imported_unit
15969 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15970 {
15971 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15972 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15973 continue;
15974 }
15975
dee91e82
DE
15976 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15977 info_ptr);
72bf9492
DJ
15978
15979 /* This two-pass algorithm for processing partial symbols has a
15980 high cost in cache pressure. Thus, handle some simple cases
15981 here which cover the majority of C partial symbols. DIEs
15982 which neither have specification tags in them, nor could have
15983 specification tags elsewhere pointing at them, can simply be
15984 processed and discarded.
15985
15986 This segment is also optional; scan_partial_symbols and
15987 add_partial_symbol will handle these DIEs if we chain
15988 them in normally. When compilers which do not emit large
15989 quantities of duplicate debug information are more common,
15990 this code can probably be removed. */
15991
15992 /* Any complete simple types at the top level (pretty much all
15993 of them, for a language without namespaces), can be processed
15994 directly. */
15995 if (parent_die == NULL
15996 && part_die->has_specification == 0
15997 && part_die->is_declaration == 0
d8228535 15998 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15999 || part_die->tag == DW_TAG_base_type
16000 || part_die->tag == DW_TAG_subrange_type))
16001 {
16002 if (building_psymtab && part_die->name != NULL)
04a679b8 16003 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16004 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 16005 &objfile->static_psymbols,
1762568f 16006 0, cu->language, objfile);
dee91e82 16007 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16008 continue;
16009 }
16010
d8228535
JK
16011 /* The exception for DW_TAG_typedef with has_children above is
16012 a workaround of GCC PR debug/47510. In the case of this complaint
16013 type_name_no_tag_or_error will error on such types later.
16014
16015 GDB skipped children of DW_TAG_typedef by the shortcut above and then
16016 it could not find the child DIEs referenced later, this is checked
16017 above. In correct DWARF DW_TAG_typedef should have no children. */
16018
16019 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16020 complaint (&symfile_complaints,
16021 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16022 "- DIE at 0x%x [in module %s]"),
9c541725 16023 to_underlying (part_die->sect_off), objfile_name (objfile));
d8228535 16024
72bf9492
DJ
16025 /* If we're at the second level, and we're an enumerator, and
16026 our parent has no specification (meaning possibly lives in a
16027 namespace elsewhere), then we can add the partial symbol now
16028 instead of queueing it. */
16029 if (part_die->tag == DW_TAG_enumerator
16030 && parent_die != NULL
16031 && parent_die->die_parent == NULL
16032 && parent_die->tag == DW_TAG_enumeration_type
16033 && parent_die->has_specification == 0)
16034 {
16035 if (part_die->name == NULL)
3e43a32a
MS
16036 complaint (&symfile_complaints,
16037 _("malformed enumerator DIE ignored"));
72bf9492 16038 else if (building_psymtab)
04a679b8 16039 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 16040 VAR_DOMAIN, LOC_CONST,
9c37b5ae 16041 cu->language == language_cplus
bb5ed363
DE
16042 ? &objfile->global_psymbols
16043 : &objfile->static_psymbols,
1762568f 16044 0, cu->language, objfile);
72bf9492 16045
dee91e82 16046 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
16047 continue;
16048 }
16049
16050 /* We'll save this DIE so link it in. */
16051 part_die->die_parent = parent_die;
16052 part_die->die_sibling = NULL;
16053 part_die->die_child = NULL;
16054
16055 if (last_die && last_die == parent_die)
16056 last_die->die_child = part_die;
16057 else if (last_die)
16058 last_die->die_sibling = part_die;
16059
16060 last_die = part_die;
16061
16062 if (first_die == NULL)
16063 first_die = part_die;
16064
16065 /* Maybe add the DIE to the hash table. Not all DIEs that we
16066 find interesting need to be in the hash table, because we
16067 also have the parent/sibling/child chains; only those that we
16068 might refer to by offset later during partial symbol reading.
16069
16070 For now this means things that might have be the target of a
16071 DW_AT_specification, DW_AT_abstract_origin, or
16072 DW_AT_extension. DW_AT_extension will refer only to
16073 namespaces; DW_AT_abstract_origin refers to functions (and
16074 many things under the function DIE, but we do not recurse
16075 into function DIEs during partial symbol reading) and
16076 possibly variables as well; DW_AT_specification refers to
16077 declarations. Declarations ought to have the DW_AT_declaration
16078 flag. It happens that GCC forgets to put it in sometimes, but
16079 only for functions, not for types.
16080
16081 Adding more things than necessary to the hash table is harmless
16082 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16083 wasted time in find_partial_die, when we reread the compilation
16084 unit with load_all_dies set. */
72bf9492 16085
5afb4e99 16086 if (load_all
72929c62 16087 || abbrev->tag == DW_TAG_constant
5afb4e99 16088 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16089 || abbrev->tag == DW_TAG_variable
16090 || abbrev->tag == DW_TAG_namespace
16091 || part_die->is_declaration)
16092 {
16093 void **slot;
16094
16095 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
16096 to_underlying (part_die->sect_off),
16097 INSERT);
72bf9492
DJ
16098 *slot = part_die;
16099 }
16100
8d749320 16101 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16102
16103 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16104 we have no reason to follow the children of structures; for other
98bfdba5
PA
16105 languages we have to, so that we can get at method physnames
16106 to infer fully qualified class names, for DW_AT_specification,
16107 and for C++ template arguments. For C++, we also look one level
16108 inside functions to find template arguments (if the name of the
16109 function does not already contain the template arguments).
bc30ff58
JB
16110
16111 For Ada, we need to scan the children of subprograms and lexical
16112 blocks as well because Ada allows the definition of nested
16113 entities that could be interesting for the debugger, such as
16114 nested subprograms for instance. */
72bf9492 16115 if (last_die->has_children
5afb4e99
DJ
16116 && (load_all
16117 || last_die->tag == DW_TAG_namespace
f55ee35c 16118 || last_die->tag == DW_TAG_module
72bf9492 16119 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16120 || (cu->language == language_cplus
16121 && last_die->tag == DW_TAG_subprogram
16122 && (last_die->name == NULL
16123 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16124 || (cu->language != language_c
16125 && (last_die->tag == DW_TAG_class_type
680b30c7 16126 || last_die->tag == DW_TAG_interface_type
72bf9492 16127 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16128 || last_die->tag == DW_TAG_union_type))
16129 || (cu->language == language_ada
16130 && (last_die->tag == DW_TAG_subprogram
16131 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16132 {
16133 nesting_level++;
16134 parent_die = last_die;
16135 continue;
16136 }
16137
16138 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16139 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16140
16141 /* Back to the top, do it again. */
16142 }
16143}
16144
c906108c
SS
16145/* Read a minimal amount of information into the minimal die structure. */
16146
d521ce57 16147static const gdb_byte *
dee91e82
DE
16148read_partial_die (const struct die_reader_specs *reader,
16149 struct partial_die_info *part_die,
16150 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16151 const gdb_byte *info_ptr)
c906108c 16152{
dee91e82 16153 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16154 struct objfile *objfile = cu->objfile;
d521ce57 16155 const gdb_byte *buffer = reader->buffer;
fa238c03 16156 unsigned int i;
c906108c 16157 struct attribute attr;
c5aa993b 16158 int has_low_pc_attr = 0;
c906108c 16159 int has_high_pc_attr = 0;
91da1414 16160 int high_pc_relative = 0;
c906108c 16161
72bf9492 16162 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16163
9c541725 16164 part_die->sect_off = (sect_offset) (info_ptr - buffer);
72bf9492
DJ
16165
16166 info_ptr += abbrev_len;
16167
16168 if (abbrev == NULL)
16169 return info_ptr;
16170
c906108c
SS
16171 part_die->tag = abbrev->tag;
16172 part_die->has_children = abbrev->has_children;
c906108c
SS
16173
16174 for (i = 0; i < abbrev->num_attrs; ++i)
16175 {
dee91e82 16176 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16177
16178 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16179 partial symbol table. */
c906108c
SS
16180 switch (attr.name)
16181 {
16182 case DW_AT_name:
71c25dea
TT
16183 switch (part_die->tag)
16184 {
16185 case DW_TAG_compile_unit:
95554aad 16186 case DW_TAG_partial_unit:
348e048f 16187 case DW_TAG_type_unit:
71c25dea
TT
16188 /* Compilation units have a DW_AT_name that is a filename, not
16189 a source language identifier. */
16190 case DW_TAG_enumeration_type:
16191 case DW_TAG_enumerator:
16192 /* These tags always have simple identifiers already; no need
16193 to canonicalize them. */
16194 part_die->name = DW_STRING (&attr);
16195 break;
16196 default:
16197 part_die->name
16198 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16199 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16200 break;
16201 }
c906108c 16202 break;
31ef98ae 16203 case DW_AT_linkage_name:
c906108c 16204 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16205 /* Note that both forms of linkage name might appear. We
16206 assume they will be the same, and we only store the last
16207 one we see. */
94af9270
KS
16208 if (cu->language == language_ada)
16209 part_die->name = DW_STRING (&attr);
abc72ce4 16210 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16211 break;
16212 case DW_AT_low_pc:
16213 has_low_pc_attr = 1;
31aa7e4e 16214 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16215 break;
16216 case DW_AT_high_pc:
16217 has_high_pc_attr = 1;
31aa7e4e
JB
16218 part_die->highpc = attr_value_as_address (&attr);
16219 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16220 high_pc_relative = 1;
c906108c
SS
16221 break;
16222 case DW_AT_location:
0963b4bd 16223 /* Support the .debug_loc offsets. */
8e19ed76
PS
16224 if (attr_form_is_block (&attr))
16225 {
95554aad 16226 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16227 }
3690dd37 16228 else if (attr_form_is_section_offset (&attr))
8e19ed76 16229 {
4d3c2250 16230 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16231 }
16232 else
16233 {
4d3c2250
KB
16234 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16235 "partial symbol information");
8e19ed76 16236 }
c906108c 16237 break;
c906108c
SS
16238 case DW_AT_external:
16239 part_die->is_external = DW_UNSND (&attr);
16240 break;
16241 case DW_AT_declaration:
16242 part_die->is_declaration = DW_UNSND (&attr);
16243 break;
16244 case DW_AT_type:
16245 part_die->has_type = 1;
16246 break;
16247 case DW_AT_abstract_origin:
16248 case DW_AT_specification:
72bf9492
DJ
16249 case DW_AT_extension:
16250 part_die->has_specification = 1;
c764a876 16251 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16252 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16253 || cu->per_cu->is_dwz);
c906108c
SS
16254 break;
16255 case DW_AT_sibling:
16256 /* Ignore absolute siblings, they might point outside of
16257 the current compile unit. */
16258 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16259 complaint (&symfile_complaints,
16260 _("ignoring absolute DW_AT_sibling"));
c906108c 16261 else
b9502d3f 16262 {
9c541725
PA
16263 sect_offset off = dwarf2_get_ref_die_offset (&attr);
16264 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
16265
16266 if (sibling_ptr < info_ptr)
16267 complaint (&symfile_complaints,
16268 _("DW_AT_sibling points backwards"));
22869d73
KS
16269 else if (sibling_ptr > reader->buffer_end)
16270 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16271 else
16272 part_die->sibling = sibling_ptr;
16273 }
c906108c 16274 break;
fa4028e9
JB
16275 case DW_AT_byte_size:
16276 part_die->has_byte_size = 1;
16277 break;
ff908ebf
AW
16278 case DW_AT_const_value:
16279 part_die->has_const_value = 1;
16280 break;
68511cec
CES
16281 case DW_AT_calling_convention:
16282 /* DWARF doesn't provide a way to identify a program's source-level
16283 entry point. DW_AT_calling_convention attributes are only meant
16284 to describe functions' calling conventions.
16285
16286 However, because it's a necessary piece of information in
0c1b455e
TT
16287 Fortran, and before DWARF 4 DW_CC_program was the only
16288 piece of debugging information whose definition refers to
16289 a 'main program' at all, several compilers marked Fortran
16290 main programs with DW_CC_program --- even when those
16291 functions use the standard calling conventions.
16292
16293 Although DWARF now specifies a way to provide this
16294 information, we support this practice for backward
16295 compatibility. */
68511cec 16296 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16297 && cu->language == language_fortran)
16298 part_die->main_subprogram = 1;
68511cec 16299 break;
481860b3
GB
16300 case DW_AT_inline:
16301 if (DW_UNSND (&attr) == DW_INL_inlined
16302 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16303 part_die->may_be_inlined = 1;
16304 break;
95554aad
TT
16305
16306 case DW_AT_import:
16307 if (part_die->tag == DW_TAG_imported_unit)
36586728 16308 {
9c541725 16309 part_die->d.sect_off = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16310 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16311 || cu->per_cu->is_dwz);
16312 }
95554aad
TT
16313 break;
16314
0c1b455e
TT
16315 case DW_AT_main_subprogram:
16316 part_die->main_subprogram = DW_UNSND (&attr);
16317 break;
16318
c906108c
SS
16319 default:
16320 break;
16321 }
16322 }
16323
91da1414
MW
16324 if (high_pc_relative)
16325 part_die->highpc += part_die->lowpc;
16326
9373cf26
JK
16327 if (has_low_pc_attr && has_high_pc_attr)
16328 {
16329 /* When using the GNU linker, .gnu.linkonce. sections are used to
16330 eliminate duplicate copies of functions and vtables and such.
16331 The linker will arbitrarily choose one and discard the others.
16332 The AT_*_pc values for such functions refer to local labels in
16333 these sections. If the section from that file was discarded, the
16334 labels are not in the output, so the relocs get a value of 0.
16335 If this is a discarded function, mark the pc bounds as invalid,
16336 so that GDB will ignore it. */
16337 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16338 {
bb5ed363 16339 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16340
16341 complaint (&symfile_complaints,
16342 _("DW_AT_low_pc %s is zero "
16343 "for DIE at 0x%x [in module %s]"),
16344 paddress (gdbarch, part_die->lowpc),
9c541725 16345 to_underlying (part_die->sect_off), objfile_name (objfile));
9373cf26
JK
16346 }
16347 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16348 else if (part_die->lowpc >= part_die->highpc)
16349 {
bb5ed363 16350 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16351
16352 complaint (&symfile_complaints,
16353 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16354 "for DIE at 0x%x [in module %s]"),
16355 paddress (gdbarch, part_die->lowpc),
16356 paddress (gdbarch, part_die->highpc),
9c541725
PA
16357 to_underlying (part_die->sect_off),
16358 objfile_name (objfile));
9373cf26
JK
16359 }
16360 else
16361 part_die->has_pc_info = 1;
16362 }
85cbf3d3 16363
c906108c
SS
16364 return info_ptr;
16365}
16366
72bf9492
DJ
16367/* Find a cached partial DIE at OFFSET in CU. */
16368
16369static struct partial_die_info *
9c541725 16370find_partial_die_in_comp_unit (sect_offset sect_off, struct dwarf2_cu *cu)
72bf9492
DJ
16371{
16372 struct partial_die_info *lookup_die = NULL;
16373 struct partial_die_info part_die;
16374
9c541725 16375 part_die.sect_off = sect_off;
9a3c8263
SM
16376 lookup_die = ((struct partial_die_info *)
16377 htab_find_with_hash (cu->partial_dies, &part_die,
9c541725 16378 to_underlying (sect_off)));
72bf9492 16379
72bf9492
DJ
16380 return lookup_die;
16381}
16382
348e048f
DE
16383/* Find a partial DIE at OFFSET, which may or may not be in CU,
16384 except in the case of .debug_types DIEs which do not reference
16385 outside their CU (they do however referencing other types via
55f1336d 16386 DW_FORM_ref_sig8). */
72bf9492
DJ
16387
16388static struct partial_die_info *
9c541725 16389find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16390{
bb5ed363 16391 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16392 struct dwarf2_per_cu_data *per_cu = NULL;
16393 struct partial_die_info *pd = NULL;
72bf9492 16394
36586728 16395 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 16396 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 16397 {
9c541725 16398 pd = find_partial_die_in_comp_unit (sect_off, cu);
5afb4e99
DJ
16399 if (pd != NULL)
16400 return pd;
0d99eb77
DE
16401 /* We missed recording what we needed.
16402 Load all dies and try again. */
16403 per_cu = cu->per_cu;
5afb4e99 16404 }
0d99eb77
DE
16405 else
16406 {
16407 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16408 if (cu->per_cu->is_debug_types)
0d99eb77 16409 {
9c541725
PA
16410 error (_("Dwarf Error: Type Unit at offset 0x%x contains"
16411 " external reference to offset 0x%x [in module %s].\n"),
16412 to_underlying (cu->header.sect_off), to_underlying (sect_off),
0d99eb77
DE
16413 bfd_get_filename (objfile->obfd));
16414 }
9c541725 16415 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 16416 objfile);
72bf9492 16417
0d99eb77
DE
16418 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16419 load_partial_comp_unit (per_cu);
ae038cb0 16420
0d99eb77 16421 per_cu->cu->last_used = 0;
9c541725 16422 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
0d99eb77 16423 }
5afb4e99 16424
dee91e82
DE
16425 /* If we didn't find it, and not all dies have been loaded,
16426 load them all and try again. */
16427
5afb4e99
DJ
16428 if (pd == NULL && per_cu->load_all_dies == 0)
16429 {
5afb4e99 16430 per_cu->load_all_dies = 1;
fd820528
DE
16431
16432 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16433 THIS_CU->cu may already be in use. So we can't just free it and
16434 replace its DIEs with the ones we read in. Instead, we leave those
16435 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16436 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16437 set. */
dee91e82 16438 load_partial_comp_unit (per_cu);
5afb4e99 16439
9c541725 16440 pd = find_partial_die_in_comp_unit (sect_off, per_cu->cu);
5afb4e99
DJ
16441 }
16442
16443 if (pd == NULL)
16444 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16445 _("could not find partial DIE 0x%x "
16446 "in cache [from module %s]\n"),
9c541725 16447 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 16448 return pd;
72bf9492
DJ
16449}
16450
abc72ce4
DE
16451/* See if we can figure out if the class lives in a namespace. We do
16452 this by looking for a member function; its demangled name will
16453 contain namespace info, if there is any. */
16454
16455static void
16456guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16457 struct dwarf2_cu *cu)
16458{
16459 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16460 what template types look like, because the demangler
16461 frequently doesn't give the same name as the debug info. We
16462 could fix this by only using the demangled name to get the
16463 prefix (but see comment in read_structure_type). */
16464
16465 struct partial_die_info *real_pdi;
16466 struct partial_die_info *child_pdi;
16467
16468 /* If this DIE (this DIE's specification, if any) has a parent, then
16469 we should not do this. We'll prepend the parent's fully qualified
16470 name when we create the partial symbol. */
16471
16472 real_pdi = struct_pdi;
16473 while (real_pdi->has_specification)
36586728
TT
16474 real_pdi = find_partial_die (real_pdi->spec_offset,
16475 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16476
16477 if (real_pdi->die_parent != NULL)
16478 return;
16479
16480 for (child_pdi = struct_pdi->die_child;
16481 child_pdi != NULL;
16482 child_pdi = child_pdi->die_sibling)
16483 {
16484 if (child_pdi->tag == DW_TAG_subprogram
16485 && child_pdi->linkage_name != NULL)
16486 {
16487 char *actual_class_name
16488 = language_class_name_from_physname (cu->language_defn,
16489 child_pdi->linkage_name);
16490 if (actual_class_name != NULL)
16491 {
16492 struct_pdi->name
224c3ddb
SM
16493 = ((const char *)
16494 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16495 actual_class_name,
16496 strlen (actual_class_name)));
abc72ce4
DE
16497 xfree (actual_class_name);
16498 }
16499 break;
16500 }
16501 }
16502}
16503
72bf9492
DJ
16504/* Adjust PART_DIE before generating a symbol for it. This function
16505 may set the is_external flag or change the DIE's name. */
16506
16507static void
16508fixup_partial_die (struct partial_die_info *part_die,
16509 struct dwarf2_cu *cu)
16510{
abc72ce4
DE
16511 /* Once we've fixed up a die, there's no point in doing so again.
16512 This also avoids a memory leak if we were to call
16513 guess_partial_die_structure_name multiple times. */
16514 if (part_die->fixup_called)
16515 return;
16516
72bf9492
DJ
16517 /* If we found a reference attribute and the DIE has no name, try
16518 to find a name in the referred to DIE. */
16519
16520 if (part_die->name == NULL && part_die->has_specification)
16521 {
16522 struct partial_die_info *spec_die;
72bf9492 16523
36586728
TT
16524 spec_die = find_partial_die (part_die->spec_offset,
16525 part_die->spec_is_dwz, cu);
72bf9492 16526
10b3939b 16527 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16528
16529 if (spec_die->name)
16530 {
16531 part_die->name = spec_die->name;
16532
16533 /* Copy DW_AT_external attribute if it is set. */
16534 if (spec_die->is_external)
16535 part_die->is_external = spec_die->is_external;
16536 }
16537 }
16538
16539 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16540
16541 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16542 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16543
abc72ce4
DE
16544 /* If there is no parent die to provide a namespace, and there are
16545 children, see if we can determine the namespace from their linkage
122d1940 16546 name. */
abc72ce4 16547 if (cu->language == language_cplus
8b70b953 16548 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16549 && part_die->die_parent == NULL
16550 && part_die->has_children
16551 && (part_die->tag == DW_TAG_class_type
16552 || part_die->tag == DW_TAG_structure_type
16553 || part_die->tag == DW_TAG_union_type))
16554 guess_partial_die_structure_name (part_die, cu);
16555
53832f31
TT
16556 /* GCC might emit a nameless struct or union that has a linkage
16557 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16558 if (part_die->name == NULL
96408a79
SA
16559 && (part_die->tag == DW_TAG_class_type
16560 || part_die->tag == DW_TAG_interface_type
16561 || part_die->tag == DW_TAG_structure_type
16562 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16563 && part_die->linkage_name != NULL)
16564 {
16565 char *demangled;
16566
8de20a37 16567 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16568 if (demangled)
16569 {
96408a79
SA
16570 const char *base;
16571
16572 /* Strip any leading namespaces/classes, keep only the base name.
16573 DW_AT_name for named DIEs does not contain the prefixes. */
16574 base = strrchr (demangled, ':');
16575 if (base && base > demangled && base[-1] == ':')
16576 base++;
16577 else
16578 base = demangled;
16579
34a68019 16580 part_die->name
224c3ddb
SM
16581 = ((const char *)
16582 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16583 base, strlen (base)));
53832f31
TT
16584 xfree (demangled);
16585 }
16586 }
16587
abc72ce4 16588 part_die->fixup_called = 1;
72bf9492
DJ
16589}
16590
a8329558 16591/* Read an attribute value described by an attribute form. */
c906108c 16592
d521ce57 16593static const gdb_byte *
dee91e82
DE
16594read_attribute_value (const struct die_reader_specs *reader,
16595 struct attribute *attr, unsigned form,
43988095 16596 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16597{
dee91e82 16598 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16599 struct objfile *objfile = cu->objfile;
16600 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16601 bfd *abfd = reader->abfd;
e7c27a73 16602 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16603 unsigned int bytes_read;
16604 struct dwarf_block *blk;
16605
aead7601 16606 attr->form = (enum dwarf_form) form;
a8329558 16607 switch (form)
c906108c 16608 {
c906108c 16609 case DW_FORM_ref_addr:
ae411497 16610 if (cu->header.version == 2)
4568ecf9 16611 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16612 else
4568ecf9
DE
16613 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16614 &cu->header, &bytes_read);
ae411497
TT
16615 info_ptr += bytes_read;
16616 break;
36586728
TT
16617 case DW_FORM_GNU_ref_alt:
16618 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16619 info_ptr += bytes_read;
16620 break;
ae411497 16621 case DW_FORM_addr:
e7c27a73 16622 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16623 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16624 info_ptr += bytes_read;
c906108c
SS
16625 break;
16626 case DW_FORM_block2:
7b5a2f43 16627 blk = dwarf_alloc_block (cu);
c906108c
SS
16628 blk->size = read_2_bytes (abfd, info_ptr);
16629 info_ptr += 2;
16630 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16631 info_ptr += blk->size;
16632 DW_BLOCK (attr) = blk;
16633 break;
16634 case DW_FORM_block4:
7b5a2f43 16635 blk = dwarf_alloc_block (cu);
c906108c
SS
16636 blk->size = read_4_bytes (abfd, info_ptr);
16637 info_ptr += 4;
16638 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16639 info_ptr += blk->size;
16640 DW_BLOCK (attr) = blk;
16641 break;
16642 case DW_FORM_data2:
16643 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16644 info_ptr += 2;
16645 break;
16646 case DW_FORM_data4:
16647 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16648 info_ptr += 4;
16649 break;
16650 case DW_FORM_data8:
16651 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16652 info_ptr += 8;
16653 break;
0224619f
JK
16654 case DW_FORM_data16:
16655 blk = dwarf_alloc_block (cu);
16656 blk->size = 16;
16657 blk->data = read_n_bytes (abfd, info_ptr, 16);
16658 info_ptr += 16;
16659 DW_BLOCK (attr) = blk;
16660 break;
2dc7f7b3
TT
16661 case DW_FORM_sec_offset:
16662 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16663 info_ptr += bytes_read;
16664 break;
c906108c 16665 case DW_FORM_string:
9b1c24c8 16666 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16667 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16668 info_ptr += bytes_read;
16669 break;
4bdf3d34 16670 case DW_FORM_strp:
36586728
TT
16671 if (!cu->per_cu->is_dwz)
16672 {
16673 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16674 &bytes_read);
16675 DW_STRING_IS_CANONICAL (attr) = 0;
16676 info_ptr += bytes_read;
16677 break;
16678 }
16679 /* FALLTHROUGH */
43988095
JK
16680 case DW_FORM_line_strp:
16681 if (!cu->per_cu->is_dwz)
16682 {
16683 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16684 cu_header, &bytes_read);
16685 DW_STRING_IS_CANONICAL (attr) = 0;
16686 info_ptr += bytes_read;
16687 break;
16688 }
16689 /* FALLTHROUGH */
36586728
TT
16690 case DW_FORM_GNU_strp_alt:
16691 {
16692 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16693 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16694 &bytes_read);
16695
16696 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16697 DW_STRING_IS_CANONICAL (attr) = 0;
16698 info_ptr += bytes_read;
16699 }
4bdf3d34 16700 break;
2dc7f7b3 16701 case DW_FORM_exprloc:
c906108c 16702 case DW_FORM_block:
7b5a2f43 16703 blk = dwarf_alloc_block (cu);
c906108c
SS
16704 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16705 info_ptr += bytes_read;
16706 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16707 info_ptr += blk->size;
16708 DW_BLOCK (attr) = blk;
16709 break;
16710 case DW_FORM_block1:
7b5a2f43 16711 blk = dwarf_alloc_block (cu);
c906108c
SS
16712 blk->size = read_1_byte (abfd, info_ptr);
16713 info_ptr += 1;
16714 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16715 info_ptr += blk->size;
16716 DW_BLOCK (attr) = blk;
16717 break;
16718 case DW_FORM_data1:
16719 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16720 info_ptr += 1;
16721 break;
16722 case DW_FORM_flag:
16723 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16724 info_ptr += 1;
16725 break;
2dc7f7b3
TT
16726 case DW_FORM_flag_present:
16727 DW_UNSND (attr) = 1;
16728 break;
c906108c
SS
16729 case DW_FORM_sdata:
16730 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16731 info_ptr += bytes_read;
16732 break;
16733 case DW_FORM_udata:
16734 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16735 info_ptr += bytes_read;
16736 break;
16737 case DW_FORM_ref1:
9c541725 16738 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16739 + read_1_byte (abfd, info_ptr));
c906108c
SS
16740 info_ptr += 1;
16741 break;
16742 case DW_FORM_ref2:
9c541725 16743 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16744 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16745 info_ptr += 2;
16746 break;
16747 case DW_FORM_ref4:
9c541725 16748 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16749 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16750 info_ptr += 4;
16751 break;
613e1657 16752 case DW_FORM_ref8:
9c541725 16753 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16754 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16755 info_ptr += 8;
16756 break;
55f1336d 16757 case DW_FORM_ref_sig8:
ac9ec31b 16758 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16759 info_ptr += 8;
16760 break;
c906108c 16761 case DW_FORM_ref_udata:
9c541725 16762 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 16763 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16764 info_ptr += bytes_read;
16765 break;
c906108c 16766 case DW_FORM_indirect:
a8329558
KW
16767 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16768 info_ptr += bytes_read;
43988095
JK
16769 if (form == DW_FORM_implicit_const)
16770 {
16771 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16772 info_ptr += bytes_read;
16773 }
16774 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16775 info_ptr);
16776 break;
16777 case DW_FORM_implicit_const:
16778 DW_SND (attr) = implicit_const;
a8329558 16779 break;
3019eac3
DE
16780 case DW_FORM_GNU_addr_index:
16781 if (reader->dwo_file == NULL)
16782 {
16783 /* For now flag a hard error.
16784 Later we can turn this into a complaint. */
16785 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16786 dwarf_form_name (form),
16787 bfd_get_filename (abfd));
16788 }
16789 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16790 info_ptr += bytes_read;
16791 break;
16792 case DW_FORM_GNU_str_index:
16793 if (reader->dwo_file == NULL)
16794 {
16795 /* For now flag a hard error.
16796 Later we can turn this into a complaint if warranted. */
16797 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16798 dwarf_form_name (form),
16799 bfd_get_filename (abfd));
16800 }
16801 {
16802 ULONGEST str_index =
16803 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16804
342587c4 16805 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16806 DW_STRING_IS_CANONICAL (attr) = 0;
16807 info_ptr += bytes_read;
16808 }
16809 break;
c906108c 16810 default:
8a3fe4f8 16811 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16812 dwarf_form_name (form),
16813 bfd_get_filename (abfd));
c906108c 16814 }
28e94949 16815
36586728 16816 /* Super hack. */
7771576e 16817 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16818 attr->form = DW_FORM_GNU_ref_alt;
16819
28e94949
JB
16820 /* We have seen instances where the compiler tried to emit a byte
16821 size attribute of -1 which ended up being encoded as an unsigned
16822 0xffffffff. Although 0xffffffff is technically a valid size value,
16823 an object of this size seems pretty unlikely so we can relatively
16824 safely treat these cases as if the size attribute was invalid and
16825 treat them as zero by default. */
16826 if (attr->name == DW_AT_byte_size
16827 && form == DW_FORM_data4
16828 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16829 {
16830 complaint
16831 (&symfile_complaints,
43bbcdc2
PH
16832 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16833 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16834 DW_UNSND (attr) = 0;
16835 }
28e94949 16836
c906108c
SS
16837 return info_ptr;
16838}
16839
a8329558
KW
16840/* Read an attribute described by an abbreviated attribute. */
16841
d521ce57 16842static const gdb_byte *
dee91e82
DE
16843read_attribute (const struct die_reader_specs *reader,
16844 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16845 const gdb_byte *info_ptr)
a8329558
KW
16846{
16847 attr->name = abbrev->name;
43988095
JK
16848 return read_attribute_value (reader, attr, abbrev->form,
16849 abbrev->implicit_const, info_ptr);
a8329558
KW
16850}
16851
0963b4bd 16852/* Read dwarf information from a buffer. */
c906108c
SS
16853
16854static unsigned int
a1855c1d 16855read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16856{
fe1b8b76 16857 return bfd_get_8 (abfd, buf);
c906108c
SS
16858}
16859
16860static int
a1855c1d 16861read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16862{
fe1b8b76 16863 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16864}
16865
16866static unsigned int
a1855c1d 16867read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16868{
fe1b8b76 16869 return bfd_get_16 (abfd, buf);
c906108c
SS
16870}
16871
21ae7a4d 16872static int
a1855c1d 16873read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16874{
16875 return bfd_get_signed_16 (abfd, buf);
16876}
16877
c906108c 16878static unsigned int
a1855c1d 16879read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16880{
fe1b8b76 16881 return bfd_get_32 (abfd, buf);
c906108c
SS
16882}
16883
21ae7a4d 16884static int
a1855c1d 16885read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16886{
16887 return bfd_get_signed_32 (abfd, buf);
16888}
16889
93311388 16890static ULONGEST
a1855c1d 16891read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16892{
fe1b8b76 16893 return bfd_get_64 (abfd, buf);
c906108c
SS
16894}
16895
16896static CORE_ADDR
d521ce57 16897read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16898 unsigned int *bytes_read)
c906108c 16899{
e7c27a73 16900 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16901 CORE_ADDR retval = 0;
16902
107d2387 16903 if (cu_header->signed_addr_p)
c906108c 16904 {
107d2387
AC
16905 switch (cu_header->addr_size)
16906 {
16907 case 2:
fe1b8b76 16908 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16909 break;
16910 case 4:
fe1b8b76 16911 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16912 break;
16913 case 8:
fe1b8b76 16914 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16915 break;
16916 default:
8e65ff28 16917 internal_error (__FILE__, __LINE__,
e2e0b3e5 16918 _("read_address: bad switch, signed [in module %s]"),
659b0389 16919 bfd_get_filename (abfd));
107d2387
AC
16920 }
16921 }
16922 else
16923 {
16924 switch (cu_header->addr_size)
16925 {
16926 case 2:
fe1b8b76 16927 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16928 break;
16929 case 4:
fe1b8b76 16930 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16931 break;
16932 case 8:
fe1b8b76 16933 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16934 break;
16935 default:
8e65ff28 16936 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16937 _("read_address: bad switch, "
16938 "unsigned [in module %s]"),
659b0389 16939 bfd_get_filename (abfd));
107d2387 16940 }
c906108c 16941 }
64367e0a 16942
107d2387
AC
16943 *bytes_read = cu_header->addr_size;
16944 return retval;
c906108c
SS
16945}
16946
f7ef9339 16947/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16948 specification allows the initial length to take up either 4 bytes
16949 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16950 bytes describe the length and all offsets will be 8 bytes in length
16951 instead of 4.
16952
f7ef9339
KB
16953 An older, non-standard 64-bit format is also handled by this
16954 function. The older format in question stores the initial length
16955 as an 8-byte quantity without an escape value. Lengths greater
16956 than 2^32 aren't very common which means that the initial 4 bytes
16957 is almost always zero. Since a length value of zero doesn't make
16958 sense for the 32-bit format, this initial zero can be considered to
16959 be an escape value which indicates the presence of the older 64-bit
16960 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16961 greater than 4GB. If it becomes necessary to handle lengths
16962 somewhat larger than 4GB, we could allow other small values (such
16963 as the non-sensical values of 1, 2, and 3) to also be used as
16964 escape values indicating the presence of the old format.
f7ef9339 16965
917c78fc
MK
16966 The value returned via bytes_read should be used to increment the
16967 relevant pointer after calling read_initial_length().
c764a876 16968
613e1657
KB
16969 [ Note: read_initial_length() and read_offset() are based on the
16970 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16971 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16972 from:
16973
f7ef9339 16974 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16975
613e1657
KB
16976 This document is only a draft and is subject to change. (So beware.)
16977
f7ef9339 16978 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16979 determined empirically by examining 64-bit ELF files produced by
16980 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16981
16982 - Kevin, July 16, 2002
613e1657
KB
16983 ] */
16984
16985static LONGEST
d521ce57 16986read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16987{
fe1b8b76 16988 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16989
dd373385 16990 if (length == 0xffffffff)
613e1657 16991 {
fe1b8b76 16992 length = bfd_get_64 (abfd, buf + 4);
613e1657 16993 *bytes_read = 12;
613e1657 16994 }
dd373385 16995 else if (length == 0)
f7ef9339 16996 {
dd373385 16997 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16998 length = bfd_get_64 (abfd, buf);
f7ef9339 16999 *bytes_read = 8;
f7ef9339 17000 }
613e1657
KB
17001 else
17002 {
17003 *bytes_read = 4;
613e1657
KB
17004 }
17005
c764a876
DE
17006 return length;
17007}
dd373385 17008
c764a876
DE
17009/* Cover function for read_initial_length.
17010 Returns the length of the object at BUF, and stores the size of the
17011 initial length in *BYTES_READ and stores the size that offsets will be in
17012 *OFFSET_SIZE.
17013 If the initial length size is not equivalent to that specified in
17014 CU_HEADER then issue a complaint.
17015 This is useful when reading non-comp-unit headers. */
dd373385 17016
c764a876 17017static LONGEST
d521ce57 17018read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
17019 const struct comp_unit_head *cu_header,
17020 unsigned int *bytes_read,
17021 unsigned int *offset_size)
17022{
17023 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17024
17025 gdb_assert (cu_header->initial_length_size == 4
17026 || cu_header->initial_length_size == 8
17027 || cu_header->initial_length_size == 12);
17028
17029 if (cu_header->initial_length_size != *bytes_read)
17030 complaint (&symfile_complaints,
17031 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 17032
c764a876 17033 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 17034 return length;
613e1657
KB
17035}
17036
17037/* Read an offset from the data stream. The size of the offset is
917c78fc 17038 given by cu_header->offset_size. */
613e1657
KB
17039
17040static LONGEST
d521ce57
TT
17041read_offset (bfd *abfd, const gdb_byte *buf,
17042 const struct comp_unit_head *cu_header,
891d2f0b 17043 unsigned int *bytes_read)
c764a876
DE
17044{
17045 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 17046
c764a876
DE
17047 *bytes_read = cu_header->offset_size;
17048 return offset;
17049}
17050
17051/* Read an offset from the data stream. */
17052
17053static LONGEST
d521ce57 17054read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
17055{
17056 LONGEST retval = 0;
17057
c764a876 17058 switch (offset_size)
613e1657
KB
17059 {
17060 case 4:
fe1b8b76 17061 retval = bfd_get_32 (abfd, buf);
613e1657
KB
17062 break;
17063 case 8:
fe1b8b76 17064 retval = bfd_get_64 (abfd, buf);
613e1657
KB
17065 break;
17066 default:
8e65ff28 17067 internal_error (__FILE__, __LINE__,
c764a876 17068 _("read_offset_1: bad switch [in module %s]"),
659b0389 17069 bfd_get_filename (abfd));
613e1657
KB
17070 }
17071
917c78fc 17072 return retval;
613e1657
KB
17073}
17074
d521ce57
TT
17075static const gdb_byte *
17076read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
17077{
17078 /* If the size of a host char is 8 bits, we can return a pointer
17079 to the buffer, otherwise we have to copy the data to a buffer
17080 allocated on the temporary obstack. */
4bdf3d34 17081 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17082 return buf;
c906108c
SS
17083}
17084
d521ce57
TT
17085static const char *
17086read_direct_string (bfd *abfd, const gdb_byte *buf,
17087 unsigned int *bytes_read_ptr)
c906108c
SS
17088{
17089 /* If the size of a host char is 8 bits, we can return a pointer
17090 to the string, otherwise we have to copy the string to a buffer
17091 allocated on the temporary obstack. */
4bdf3d34 17092 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17093 if (*buf == '\0')
17094 {
17095 *bytes_read_ptr = 1;
17096 return NULL;
17097 }
d521ce57
TT
17098 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17099 return (const char *) buf;
4bdf3d34
JJ
17100}
17101
43988095
JK
17102/* Return pointer to string at section SECT offset STR_OFFSET with error
17103 reporting strings FORM_NAME and SECT_NAME. */
17104
d521ce57 17105static const char *
43988095
JK
17106read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17107 struct dwarf2_section_info *sect,
17108 const char *form_name,
17109 const char *sect_name)
17110{
17111 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17112 if (sect->buffer == NULL)
17113 error (_("%s used without %s section [in module %s]"),
17114 form_name, sect_name, bfd_get_filename (abfd));
17115 if (str_offset >= sect->size)
17116 error (_("%s pointing outside of %s section [in module %s]"),
17117 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17118 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17119 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17120 return NULL;
43988095
JK
17121 return (const char *) (sect->buffer + str_offset);
17122}
17123
17124/* Return pointer to string at .debug_str offset STR_OFFSET. */
17125
17126static const char *
17127read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17128{
17129 return read_indirect_string_at_offset_from (abfd, str_offset,
17130 &dwarf2_per_objfile->str,
17131 "DW_FORM_strp", ".debug_str");
17132}
17133
17134/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17135
17136static const char *
17137read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17138{
17139 return read_indirect_string_at_offset_from (abfd, str_offset,
17140 &dwarf2_per_objfile->line_str,
17141 "DW_FORM_line_strp",
17142 ".debug_line_str");
c906108c
SS
17143}
17144
36586728
TT
17145/* Read a string at offset STR_OFFSET in the .debug_str section from
17146 the .dwz file DWZ. Throw an error if the offset is too large. If
17147 the string consists of a single NUL byte, return NULL; otherwise
17148 return a pointer to the string. */
17149
d521ce57 17150static const char *
36586728
TT
17151read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17152{
17153 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17154
17155 if (dwz->str.buffer == NULL)
17156 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17157 "section [in module %s]"),
17158 bfd_get_filename (dwz->dwz_bfd));
17159 if (str_offset >= dwz->str.size)
17160 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17161 ".debug_str section [in module %s]"),
17162 bfd_get_filename (dwz->dwz_bfd));
17163 gdb_assert (HOST_CHAR_BIT == 8);
17164 if (dwz->str.buffer[str_offset] == '\0')
17165 return NULL;
d521ce57 17166 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17167}
17168
43988095
JK
17169/* Return pointer to string at .debug_str offset as read from BUF.
17170 BUF is assumed to be in a compilation unit described by CU_HEADER.
17171 Return *BYTES_READ_PTR count of bytes read from BUF. */
17172
d521ce57
TT
17173static const char *
17174read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17175 const struct comp_unit_head *cu_header,
17176 unsigned int *bytes_read_ptr)
17177{
17178 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17179
17180 return read_indirect_string_at_offset (abfd, str_offset);
17181}
17182
43988095
JK
17183/* Return pointer to string at .debug_line_str offset as read from BUF.
17184 BUF is assumed to be in a compilation unit described by CU_HEADER.
17185 Return *BYTES_READ_PTR count of bytes read from BUF. */
17186
17187static const char *
17188read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17189 const struct comp_unit_head *cu_header,
17190 unsigned int *bytes_read_ptr)
17191{
17192 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17193
17194 return read_indirect_line_string_at_offset (abfd, str_offset);
17195}
17196
17197ULONGEST
d521ce57 17198read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17199 unsigned int *bytes_read_ptr)
c906108c 17200{
12df843f 17201 ULONGEST result;
ce5d95e1 17202 unsigned int num_read;
870f88f7 17203 int shift;
c906108c
SS
17204 unsigned char byte;
17205
17206 result = 0;
17207 shift = 0;
17208 num_read = 0;
c906108c
SS
17209 while (1)
17210 {
fe1b8b76 17211 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17212 buf++;
17213 num_read++;
12df843f 17214 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17215 if ((byte & 128) == 0)
17216 {
17217 break;
17218 }
17219 shift += 7;
17220 }
17221 *bytes_read_ptr = num_read;
17222 return result;
17223}
17224
12df843f 17225static LONGEST
d521ce57
TT
17226read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17227 unsigned int *bytes_read_ptr)
c906108c 17228{
12df843f 17229 LONGEST result;
870f88f7 17230 int shift, num_read;
c906108c
SS
17231 unsigned char byte;
17232
17233 result = 0;
17234 shift = 0;
c906108c 17235 num_read = 0;
c906108c
SS
17236 while (1)
17237 {
fe1b8b76 17238 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17239 buf++;
17240 num_read++;
12df843f 17241 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17242 shift += 7;
17243 if ((byte & 128) == 0)
17244 {
17245 break;
17246 }
17247 }
77e0b926 17248 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17249 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17250 *bytes_read_ptr = num_read;
17251 return result;
17252}
17253
3019eac3
DE
17254/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17255 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17256 ADDR_SIZE is the size of addresses from the CU header. */
17257
17258static CORE_ADDR
17259read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17260{
17261 struct objfile *objfile = dwarf2_per_objfile->objfile;
17262 bfd *abfd = objfile->obfd;
17263 const gdb_byte *info_ptr;
17264
17265 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17266 if (dwarf2_per_objfile->addr.buffer == NULL)
17267 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17268 objfile_name (objfile));
3019eac3
DE
17269 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17270 error (_("DW_FORM_addr_index pointing outside of "
17271 ".debug_addr section [in module %s]"),
4262abfb 17272 objfile_name (objfile));
3019eac3
DE
17273 info_ptr = (dwarf2_per_objfile->addr.buffer
17274 + addr_base + addr_index * addr_size);
17275 if (addr_size == 4)
17276 return bfd_get_32 (abfd, info_ptr);
17277 else
17278 return bfd_get_64 (abfd, info_ptr);
17279}
17280
17281/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17282
17283static CORE_ADDR
17284read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17285{
17286 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17287}
17288
17289/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17290
17291static CORE_ADDR
d521ce57 17292read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17293 unsigned int *bytes_read)
17294{
17295 bfd *abfd = cu->objfile->obfd;
17296 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17297
17298 return read_addr_index (cu, addr_index);
17299}
17300
17301/* Data structure to pass results from dwarf2_read_addr_index_reader
17302 back to dwarf2_read_addr_index. */
17303
17304struct dwarf2_read_addr_index_data
17305{
17306 ULONGEST addr_base;
17307 int addr_size;
17308};
17309
17310/* die_reader_func for dwarf2_read_addr_index. */
17311
17312static void
17313dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17314 const gdb_byte *info_ptr,
3019eac3
DE
17315 struct die_info *comp_unit_die,
17316 int has_children,
17317 void *data)
17318{
17319 struct dwarf2_cu *cu = reader->cu;
17320 struct dwarf2_read_addr_index_data *aidata =
17321 (struct dwarf2_read_addr_index_data *) data;
17322
17323 aidata->addr_base = cu->addr_base;
17324 aidata->addr_size = cu->header.addr_size;
17325}
17326
17327/* Given an index in .debug_addr, fetch the value.
17328 NOTE: This can be called during dwarf expression evaluation,
17329 long after the debug information has been read, and thus per_cu->cu
17330 may no longer exist. */
17331
17332CORE_ADDR
17333dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17334 unsigned int addr_index)
17335{
17336 struct objfile *objfile = per_cu->objfile;
17337 struct dwarf2_cu *cu = per_cu->cu;
17338 ULONGEST addr_base;
17339 int addr_size;
17340
17341 /* This is intended to be called from outside this file. */
17342 dw2_setup (objfile);
17343
17344 /* We need addr_base and addr_size.
17345 If we don't have PER_CU->cu, we have to get it.
17346 Nasty, but the alternative is storing the needed info in PER_CU,
17347 which at this point doesn't seem justified: it's not clear how frequently
17348 it would get used and it would increase the size of every PER_CU.
17349 Entry points like dwarf2_per_cu_addr_size do a similar thing
17350 so we're not in uncharted territory here.
17351 Alas we need to be a bit more complicated as addr_base is contained
17352 in the DIE.
17353
17354 We don't need to read the entire CU(/TU).
17355 We just need the header and top level die.
a1b64ce1 17356
3019eac3 17357 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17358 For now we skip this optimization. */
3019eac3
DE
17359
17360 if (cu != NULL)
17361 {
17362 addr_base = cu->addr_base;
17363 addr_size = cu->header.addr_size;
17364 }
17365 else
17366 {
17367 struct dwarf2_read_addr_index_data aidata;
17368
a1b64ce1
DE
17369 /* Note: We can't use init_cutu_and_read_dies_simple here,
17370 we need addr_base. */
17371 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17372 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17373 addr_base = aidata.addr_base;
17374 addr_size = aidata.addr_size;
17375 }
17376
17377 return read_addr_index_1 (addr_index, addr_base, addr_size);
17378}
17379
57d63ce2
DE
17380/* Given a DW_FORM_GNU_str_index, fetch the string.
17381 This is only used by the Fission support. */
3019eac3 17382
d521ce57 17383static const char *
342587c4 17384read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17385{
17386 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17387 const char *objf_name = objfile_name (objfile);
3019eac3 17388 bfd *abfd = objfile->obfd;
342587c4 17389 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17390 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17391 struct dwarf2_section_info *str_offsets_section =
17392 &reader->dwo_file->sections.str_offsets;
d521ce57 17393 const gdb_byte *info_ptr;
3019eac3 17394 ULONGEST str_offset;
57d63ce2 17395 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17396
73869dc2
DE
17397 dwarf2_read_section (objfile, str_section);
17398 dwarf2_read_section (objfile, str_offsets_section);
17399 if (str_section->buffer == NULL)
57d63ce2 17400 error (_("%s used without .debug_str.dwo section"
9c541725
PA
17401 " in CU at offset 0x%x [in module %s]"),
17402 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17403 if (str_offsets_section->buffer == NULL)
57d63ce2 17404 error (_("%s used without .debug_str_offsets.dwo section"
9c541725
PA
17405 " in CU at offset 0x%x [in module %s]"),
17406 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17407 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17408 error (_("%s pointing outside of .debug_str_offsets.dwo"
9c541725
PA
17409 " section in CU at offset 0x%x [in module %s]"),
17410 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17411 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17412 + str_index * cu->header.offset_size);
17413 if (cu->header.offset_size == 4)
17414 str_offset = bfd_get_32 (abfd, info_ptr);
17415 else
17416 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17417 if (str_offset >= str_section->size)
57d63ce2 17418 error (_("Offset from %s pointing outside of"
9c541725
PA
17419 " .debug_str.dwo section in CU at offset 0x%x [in module %s]"),
17420 form_name, to_underlying (cu->header.sect_off), objf_name);
73869dc2 17421 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17422}
17423
3019eac3
DE
17424/* Return the length of an LEB128 number in BUF. */
17425
17426static int
17427leb128_size (const gdb_byte *buf)
17428{
17429 const gdb_byte *begin = buf;
17430 gdb_byte byte;
17431
17432 while (1)
17433 {
17434 byte = *buf++;
17435 if ((byte & 128) == 0)
17436 return buf - begin;
17437 }
17438}
17439
c906108c 17440static void
e142c38c 17441set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17442{
17443 switch (lang)
17444 {
17445 case DW_LANG_C89:
76bee0cc 17446 case DW_LANG_C99:
0cfd832f 17447 case DW_LANG_C11:
c906108c 17448 case DW_LANG_C:
d1be3247 17449 case DW_LANG_UPC:
e142c38c 17450 cu->language = language_c;
c906108c 17451 break;
9c37b5ae 17452 case DW_LANG_Java:
c906108c 17453 case DW_LANG_C_plus_plus:
0cfd832f
MW
17454 case DW_LANG_C_plus_plus_11:
17455 case DW_LANG_C_plus_plus_14:
e142c38c 17456 cu->language = language_cplus;
c906108c 17457 break;
6aecb9c2
JB
17458 case DW_LANG_D:
17459 cu->language = language_d;
17460 break;
c906108c
SS
17461 case DW_LANG_Fortran77:
17462 case DW_LANG_Fortran90:
b21b22e0 17463 case DW_LANG_Fortran95:
f7de9aab
MW
17464 case DW_LANG_Fortran03:
17465 case DW_LANG_Fortran08:
e142c38c 17466 cu->language = language_fortran;
c906108c 17467 break;
a766d390
DE
17468 case DW_LANG_Go:
17469 cu->language = language_go;
17470 break;
c906108c 17471 case DW_LANG_Mips_Assembler:
e142c38c 17472 cu->language = language_asm;
c906108c
SS
17473 break;
17474 case DW_LANG_Ada83:
8aaf0b47 17475 case DW_LANG_Ada95:
bc5f45f8
JB
17476 cu->language = language_ada;
17477 break;
72019c9c
GM
17478 case DW_LANG_Modula2:
17479 cu->language = language_m2;
17480 break;
fe8e67fd
PM
17481 case DW_LANG_Pascal83:
17482 cu->language = language_pascal;
17483 break;
22566fbd
DJ
17484 case DW_LANG_ObjC:
17485 cu->language = language_objc;
17486 break;
c44af4eb
TT
17487 case DW_LANG_Rust:
17488 case DW_LANG_Rust_old:
17489 cu->language = language_rust;
17490 break;
c906108c
SS
17491 case DW_LANG_Cobol74:
17492 case DW_LANG_Cobol85:
c906108c 17493 default:
e142c38c 17494 cu->language = language_minimal;
c906108c
SS
17495 break;
17496 }
e142c38c 17497 cu->language_defn = language_def (cu->language);
c906108c
SS
17498}
17499
17500/* Return the named attribute or NULL if not there. */
17501
17502static struct attribute *
e142c38c 17503dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17504{
a48e046c 17505 for (;;)
c906108c 17506 {
a48e046c
TT
17507 unsigned int i;
17508 struct attribute *spec = NULL;
17509
17510 for (i = 0; i < die->num_attrs; ++i)
17511 {
17512 if (die->attrs[i].name == name)
17513 return &die->attrs[i];
17514 if (die->attrs[i].name == DW_AT_specification
17515 || die->attrs[i].name == DW_AT_abstract_origin)
17516 spec = &die->attrs[i];
17517 }
17518
17519 if (!spec)
17520 break;
c906108c 17521
f2f0e013 17522 die = follow_die_ref (die, spec, &cu);
f2f0e013 17523 }
c5aa993b 17524
c906108c
SS
17525 return NULL;
17526}
17527
348e048f
DE
17528/* Return the named attribute or NULL if not there,
17529 but do not follow DW_AT_specification, etc.
17530 This is for use in contexts where we're reading .debug_types dies.
17531 Following DW_AT_specification, DW_AT_abstract_origin will take us
17532 back up the chain, and we want to go down. */
17533
17534static struct attribute *
45e58e77 17535dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17536{
17537 unsigned int i;
17538
17539 for (i = 0; i < die->num_attrs; ++i)
17540 if (die->attrs[i].name == name)
17541 return &die->attrs[i];
17542
17543 return NULL;
17544}
17545
7d45c7c3
KB
17546/* Return the string associated with a string-typed attribute, or NULL if it
17547 is either not found or is of an incorrect type. */
17548
17549static const char *
17550dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17551{
17552 struct attribute *attr;
17553 const char *str = NULL;
17554
17555 attr = dwarf2_attr (die, name, cu);
17556
17557 if (attr != NULL)
17558 {
43988095
JK
17559 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17560 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17561 str = DW_STRING (attr);
17562 else
17563 complaint (&symfile_complaints,
17564 _("string type expected for attribute %s for "
17565 "DIE at 0x%x in module %s"),
9c541725 17566 dwarf_attr_name (name), to_underlying (die->sect_off),
7d45c7c3
KB
17567 objfile_name (cu->objfile));
17568 }
17569
17570 return str;
17571}
17572
05cf31d1
JB
17573/* Return non-zero iff the attribute NAME is defined for the given DIE,
17574 and holds a non-zero value. This function should only be used for
2dc7f7b3 17575 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17576
17577static int
17578dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17579{
17580 struct attribute *attr = dwarf2_attr (die, name, cu);
17581
17582 return (attr && DW_UNSND (attr));
17583}
17584
3ca72b44 17585static int
e142c38c 17586die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17587{
05cf31d1
JB
17588 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17589 which value is non-zero. However, we have to be careful with
17590 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17591 (via dwarf2_flag_true_p) follows this attribute. So we may
17592 end up accidently finding a declaration attribute that belongs
17593 to a different DIE referenced by the specification attribute,
17594 even though the given DIE does not have a declaration attribute. */
17595 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17596 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17597}
17598
63d06c5c 17599/* Return the die giving the specification for DIE, if there is
f2f0e013 17600 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17601 containing the return value on output. If there is no
17602 specification, but there is an abstract origin, that is
17603 returned. */
63d06c5c
DC
17604
17605static struct die_info *
f2f0e013 17606die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17607{
f2f0e013
DJ
17608 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17609 *spec_cu);
63d06c5c 17610
edb3359d
DJ
17611 if (spec_attr == NULL)
17612 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17613
63d06c5c
DC
17614 if (spec_attr == NULL)
17615 return NULL;
17616 else
f2f0e013 17617 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17618}
c906108c 17619
527f3840
JK
17620/* Stub for free_line_header to match void * callback types. */
17621
17622static void
17623free_line_header_voidp (void *arg)
17624{
9a3c8263 17625 struct line_header *lh = (struct line_header *) arg;
527f3840 17626
fff8551c 17627 delete lh;
527f3840
JK
17628}
17629
fff8551c
PA
17630void
17631line_header::add_include_dir (const char *include_dir)
c906108c 17632{
27e0867f 17633 if (dwarf_line_debug >= 2)
fff8551c
PA
17634 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17635 include_dirs.size () + 1, include_dir);
27e0867f 17636
fff8551c 17637 include_dirs.push_back (include_dir);
debd256d 17638}
6e70227d 17639
fff8551c
PA
17640void
17641line_header::add_file_name (const char *name,
ecfb656c 17642 dir_index d_index,
fff8551c
PA
17643 unsigned int mod_time,
17644 unsigned int length)
debd256d 17645{
27e0867f
DE
17646 if (dwarf_line_debug >= 2)
17647 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 17648 (unsigned) file_names.size () + 1, name);
27e0867f 17649
ecfb656c 17650 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 17651}
6e70227d 17652
83769d0b 17653/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17654
17655static struct dwarf2_section_info *
17656get_debug_line_section (struct dwarf2_cu *cu)
17657{
17658 struct dwarf2_section_info *section;
17659
17660 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17661 DWO file. */
17662 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17663 section = &cu->dwo_unit->dwo_file->sections.line;
17664 else if (cu->per_cu->is_dwz)
17665 {
17666 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17667
17668 section = &dwz->line;
17669 }
17670 else
17671 section = &dwarf2_per_objfile->line;
17672
17673 return section;
17674}
17675
43988095
JK
17676/* Read directory or file name entry format, starting with byte of
17677 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17678 entries count and the entries themselves in the described entry
17679 format. */
17680
17681static void
17682read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17683 struct line_header *lh,
17684 const struct comp_unit_head *cu_header,
17685 void (*callback) (struct line_header *lh,
17686 const char *name,
ecfb656c 17687 dir_index d_index,
43988095
JK
17688 unsigned int mod_time,
17689 unsigned int length))
17690{
17691 gdb_byte format_count, formati;
17692 ULONGEST data_count, datai;
17693 const gdb_byte *buf = *bufp;
17694 const gdb_byte *format_header_data;
17695 int i;
17696 unsigned int bytes_read;
17697
17698 format_count = read_1_byte (abfd, buf);
17699 buf += 1;
17700 format_header_data = buf;
17701 for (formati = 0; formati < format_count; formati++)
17702 {
17703 read_unsigned_leb128 (abfd, buf, &bytes_read);
17704 buf += bytes_read;
17705 read_unsigned_leb128 (abfd, buf, &bytes_read);
17706 buf += bytes_read;
17707 }
17708
17709 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17710 buf += bytes_read;
17711 for (datai = 0; datai < data_count; datai++)
17712 {
17713 const gdb_byte *format = format_header_data;
17714 struct file_entry fe;
17715
43988095
JK
17716 for (formati = 0; formati < format_count; formati++)
17717 {
ecfb656c 17718 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17719 format += bytes_read;
43988095 17720
ecfb656c 17721 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 17722 format += bytes_read;
ecfb656c
PA
17723
17724 gdb::optional<const char *> string;
17725 gdb::optional<unsigned int> uint;
17726
43988095
JK
17727 switch (form)
17728 {
17729 case DW_FORM_string:
ecfb656c 17730 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
17731 buf += bytes_read;
17732 break;
17733
17734 case DW_FORM_line_strp:
ecfb656c
PA
17735 string.emplace (read_indirect_line_string (abfd, buf,
17736 cu_header,
17737 &bytes_read));
43988095
JK
17738 buf += bytes_read;
17739 break;
17740
17741 case DW_FORM_data1:
ecfb656c 17742 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
17743 buf += 1;
17744 break;
17745
17746 case DW_FORM_data2:
ecfb656c 17747 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
17748 buf += 2;
17749 break;
17750
17751 case DW_FORM_data4:
ecfb656c 17752 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
17753 buf += 4;
17754 break;
17755
17756 case DW_FORM_data8:
ecfb656c 17757 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
17758 buf += 8;
17759 break;
17760
17761 case DW_FORM_udata:
ecfb656c 17762 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
17763 buf += bytes_read;
17764 break;
17765
17766 case DW_FORM_block:
17767 /* It is valid only for DW_LNCT_timestamp which is ignored by
17768 current GDB. */
17769 break;
17770 }
ecfb656c
PA
17771
17772 switch (content_type)
17773 {
17774 case DW_LNCT_path:
17775 if (string.has_value ())
17776 fe.name = *string;
17777 break;
17778 case DW_LNCT_directory_index:
17779 if (uint.has_value ())
17780 fe.d_index = (dir_index) *uint;
17781 break;
17782 case DW_LNCT_timestamp:
17783 if (uint.has_value ())
17784 fe.mod_time = *uint;
17785 break;
17786 case DW_LNCT_size:
17787 if (uint.has_value ())
17788 fe.length = *uint;
17789 break;
17790 case DW_LNCT_MD5:
17791 break;
17792 default:
17793 complaint (&symfile_complaints,
17794 _("Unknown format content type %s"),
17795 pulongest (content_type));
17796 }
43988095
JK
17797 }
17798
ecfb656c 17799 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
17800 }
17801
17802 *bufp = buf;
17803}
17804
debd256d 17805/* Read the statement program header starting at OFFSET in
3019eac3 17806 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17807 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17808 Returns NULL if there is a problem reading the header, e.g., if it
17809 has a version we don't understand.
debd256d
JB
17810
17811 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17812 the returned object point into the dwarf line section buffer,
17813 and must not be freed. */
ae2de4f8 17814
fff8551c 17815static line_header_up
9c541725 17816dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 17817{
d521ce57 17818 const gdb_byte *line_ptr;
c764a876 17819 unsigned int bytes_read, offset_size;
debd256d 17820 int i;
d521ce57 17821 const char *cur_dir, *cur_file;
3019eac3
DE
17822 struct dwarf2_section_info *section;
17823 bfd *abfd;
17824
36586728 17825 section = get_debug_line_section (cu);
3019eac3
DE
17826 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17827 if (section->buffer == NULL)
debd256d 17828 {
3019eac3
DE
17829 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17830 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17831 else
17832 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17833 return 0;
17834 }
17835
fceca515
DE
17836 /* We can't do this until we know the section is non-empty.
17837 Only then do we know we have such a section. */
a32a8923 17838 abfd = get_section_bfd_owner (section);
fceca515 17839
a738430d
MK
17840 /* Make sure that at least there's room for the total_length field.
17841 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 17842 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 17843 {
4d3c2250 17844 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17845 return 0;
17846 }
17847
fff8551c 17848 line_header_up lh (new line_header ());
debd256d 17849
9c541725 17850 lh->sect_off = sect_off;
527f3840
JK
17851 lh->offset_in_dwz = cu->per_cu->is_dwz;
17852
9c541725 17853 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 17854
a738430d 17855 /* Read in the header. */
6e70227d 17856 lh->total_length =
c764a876
DE
17857 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17858 &bytes_read, &offset_size);
debd256d 17859 line_ptr += bytes_read;
3019eac3 17860 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17861 {
4d3c2250 17862 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17863 return 0;
17864 }
17865 lh->statement_program_end = line_ptr + lh->total_length;
17866 lh->version = read_2_bytes (abfd, line_ptr);
17867 line_ptr += 2;
43988095 17868 if (lh->version > 5)
cd366ee8
DE
17869 {
17870 /* This is a version we don't understand. The format could have
17871 changed in ways we don't handle properly so just punt. */
17872 complaint (&symfile_complaints,
17873 _("unsupported version in .debug_line section"));
17874 return NULL;
17875 }
43988095
JK
17876 if (lh->version >= 5)
17877 {
17878 gdb_byte segment_selector_size;
17879
17880 /* Skip address size. */
17881 read_1_byte (abfd, line_ptr);
17882 line_ptr += 1;
17883
17884 segment_selector_size = read_1_byte (abfd, line_ptr);
17885 line_ptr += 1;
17886 if (segment_selector_size != 0)
17887 {
17888 complaint (&symfile_complaints,
17889 _("unsupported segment selector size %u "
17890 "in .debug_line section"),
17891 segment_selector_size);
17892 return NULL;
17893 }
17894 }
c764a876
DE
17895 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17896 line_ptr += offset_size;
debd256d
JB
17897 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17898 line_ptr += 1;
2dc7f7b3
TT
17899 if (lh->version >= 4)
17900 {
17901 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17902 line_ptr += 1;
17903 }
17904 else
17905 lh->maximum_ops_per_instruction = 1;
17906
17907 if (lh->maximum_ops_per_instruction == 0)
17908 {
17909 lh->maximum_ops_per_instruction = 1;
17910 complaint (&symfile_complaints,
3e43a32a
MS
17911 _("invalid maximum_ops_per_instruction "
17912 "in `.debug_line' section"));
2dc7f7b3
TT
17913 }
17914
debd256d
JB
17915 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17916 line_ptr += 1;
17917 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17918 line_ptr += 1;
17919 lh->line_range = read_1_byte (abfd, line_ptr);
17920 line_ptr += 1;
17921 lh->opcode_base = read_1_byte (abfd, line_ptr);
17922 line_ptr += 1;
fff8551c 17923 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
17924
17925 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17926 for (i = 1; i < lh->opcode_base; ++i)
17927 {
17928 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17929 line_ptr += 1;
17930 }
17931
43988095 17932 if (lh->version >= 5)
debd256d 17933 {
43988095 17934 /* Read directory table. */
fff8551c
PA
17935 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17936 [] (struct line_header *lh, const char *name,
ecfb656c 17937 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17938 unsigned int length)
17939 {
17940 lh->add_include_dir (name);
17941 });
debd256d 17942
43988095 17943 /* Read file name table. */
fff8551c
PA
17944 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17945 [] (struct line_header *lh, const char *name,
ecfb656c 17946 dir_index d_index, unsigned int mod_time,
fff8551c
PA
17947 unsigned int length)
17948 {
ecfb656c 17949 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 17950 });
43988095
JK
17951 }
17952 else
debd256d 17953 {
43988095
JK
17954 /* Read directory table. */
17955 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17956 {
17957 line_ptr += bytes_read;
fff8551c 17958 lh->add_include_dir (cur_dir);
43988095 17959 }
debd256d
JB
17960 line_ptr += bytes_read;
17961
43988095
JK
17962 /* Read file name table. */
17963 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17964 {
ecfb656c
PA
17965 unsigned int mod_time, length;
17966 dir_index d_index;
43988095
JK
17967
17968 line_ptr += bytes_read;
ecfb656c 17969 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
17970 line_ptr += bytes_read;
17971 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17972 line_ptr += bytes_read;
17973 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17974 line_ptr += bytes_read;
17975
ecfb656c 17976 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
17977 }
17978 line_ptr += bytes_read;
debd256d 17979 }
6e70227d 17980 lh->statement_program_start = line_ptr;
debd256d 17981
3019eac3 17982 if (line_ptr > (section->buffer + section->size))
4d3c2250 17983 complaint (&symfile_complaints,
3e43a32a
MS
17984 _("line number info header doesn't "
17985 "fit in `.debug_line' section"));
debd256d 17986
debd256d
JB
17987 return lh;
17988}
c906108c 17989
c6da4cef
DE
17990/* Subroutine of dwarf_decode_lines to simplify it.
17991 Return the file name of the psymtab for included file FILE_INDEX
17992 in line header LH of PST.
17993 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17994 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17995 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17996
17997 The function creates dangling cleanup registration. */
c6da4cef 17998
d521ce57 17999static const char *
c6da4cef
DE
18000psymtab_include_file_name (const struct line_header *lh, int file_index,
18001 const struct partial_symtab *pst,
18002 const char *comp_dir)
18003{
8c43009f 18004 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
18005 const char *include_name = fe.name;
18006 const char *include_name_to_compare = include_name;
72b9f47f
TT
18007 const char *pst_filename;
18008 char *copied_name = NULL;
c6da4cef
DE
18009 int file_is_pst;
18010
8c43009f 18011 const char *dir_name = fe.include_dir (lh);
c6da4cef
DE
18012
18013 if (!IS_ABSOLUTE_PATH (include_name)
18014 && (dir_name != NULL || comp_dir != NULL))
18015 {
18016 /* Avoid creating a duplicate psymtab for PST.
18017 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18018 Before we do the comparison, however, we need to account
18019 for DIR_NAME and COMP_DIR.
18020 First prepend dir_name (if non-NULL). If we still don't
18021 have an absolute path prepend comp_dir (if non-NULL).
18022 However, the directory we record in the include-file's
18023 psymtab does not contain COMP_DIR (to match the
18024 corresponding symtab(s)).
18025
18026 Example:
18027
18028 bash$ cd /tmp
18029 bash$ gcc -g ./hello.c
18030 include_name = "hello.c"
18031 dir_name = "."
18032 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18033 DW_AT_name = "./hello.c"
18034
18035 */
c6da4cef
DE
18036
18037 if (dir_name != NULL)
18038 {
d521ce57
TT
18039 char *tem = concat (dir_name, SLASH_STRING,
18040 include_name, (char *)NULL);
18041
18042 make_cleanup (xfree, tem);
18043 include_name = tem;
c6da4cef 18044 include_name_to_compare = include_name;
c6da4cef
DE
18045 }
18046 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18047 {
d521ce57
TT
18048 char *tem = concat (comp_dir, SLASH_STRING,
18049 include_name, (char *)NULL);
18050
18051 make_cleanup (xfree, tem);
18052 include_name_to_compare = tem;
c6da4cef
DE
18053 }
18054 }
18055
18056 pst_filename = pst->filename;
18057 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18058 {
72b9f47f
TT
18059 copied_name = concat (pst->dirname, SLASH_STRING,
18060 pst_filename, (char *)NULL);
18061 pst_filename = copied_name;
c6da4cef
DE
18062 }
18063
1e3fad37 18064 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18065
72b9f47f
TT
18066 if (copied_name != NULL)
18067 xfree (copied_name);
c6da4cef
DE
18068
18069 if (file_is_pst)
18070 return NULL;
18071 return include_name;
18072}
18073
d9b3de22
DE
18074/* State machine to track the state of the line number program. */
18075
6f77053d 18076class lnp_state_machine
d9b3de22 18077{
6f77053d
PA
18078public:
18079 /* Initialize a machine state for the start of a line number
18080 program. */
18081 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
18082
8c43009f
PA
18083 file_entry *current_file ()
18084 {
18085 /* lh->file_names is 0-based, but the file name numbers in the
18086 statement program are 1-based. */
6f77053d
PA
18087 return m_line_header->file_name_at (m_file);
18088 }
18089
18090 /* Record the line in the state machine. END_SEQUENCE is true if
18091 we're processing the end of a sequence. */
18092 void record_line (bool end_sequence);
18093
18094 /* Check address and if invalid nop-out the rest of the lines in this
18095 sequence. */
18096 void check_line_address (struct dwarf2_cu *cu,
18097 const gdb_byte *line_ptr,
18098 CORE_ADDR lowpc, CORE_ADDR address);
18099
18100 void handle_set_discriminator (unsigned int discriminator)
18101 {
18102 m_discriminator = discriminator;
18103 m_line_has_non_zero_discriminator |= discriminator != 0;
18104 }
18105
18106 /* Handle DW_LNE_set_address. */
18107 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
18108 {
18109 m_op_index = 0;
18110 address += baseaddr;
18111 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
18112 }
18113
18114 /* Handle DW_LNS_advance_pc. */
18115 void handle_advance_pc (CORE_ADDR adjust);
18116
18117 /* Handle a special opcode. */
18118 void handle_special_opcode (unsigned char op_code);
18119
18120 /* Handle DW_LNS_advance_line. */
18121 void handle_advance_line (int line_delta)
18122 {
18123 advance_line (line_delta);
18124 }
18125
18126 /* Handle DW_LNS_set_file. */
18127 void handle_set_file (file_name_index file);
18128
18129 /* Handle DW_LNS_negate_stmt. */
18130 void handle_negate_stmt ()
18131 {
18132 m_is_stmt = !m_is_stmt;
18133 }
18134
18135 /* Handle DW_LNS_const_add_pc. */
18136 void handle_const_add_pc ();
18137
18138 /* Handle DW_LNS_fixed_advance_pc. */
18139 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18140 {
18141 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18142 m_op_index = 0;
18143 }
18144
18145 /* Handle DW_LNS_copy. */
18146 void handle_copy ()
18147 {
18148 record_line (false);
18149 m_discriminator = 0;
18150 }
18151
18152 /* Handle DW_LNE_end_sequence. */
18153 void handle_end_sequence ()
18154 {
18155 m_record_line_callback = ::record_line;
18156 }
18157
18158private:
18159 /* Advance the line by LINE_DELTA. */
18160 void advance_line (int line_delta)
18161 {
18162 m_line += line_delta;
18163
18164 if (line_delta != 0)
18165 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
18166 }
18167
6f77053d
PA
18168 gdbarch *m_gdbarch;
18169
18170 /* True if we're recording lines.
18171 Otherwise we're building partial symtabs and are just interested in
18172 finding include files mentioned by the line number program. */
18173 bool m_record_lines_p;
18174
8c43009f 18175 /* The line number header. */
6f77053d 18176 line_header *m_line_header;
8c43009f 18177
6f77053d
PA
18178 /* These are part of the standard DWARF line number state machine,
18179 and initialized according to the DWARF spec. */
d9b3de22 18180
6f77053d 18181 unsigned char m_op_index = 0;
8c43009f 18182 /* The line table index (1-based) of the current file. */
6f77053d
PA
18183 file_name_index m_file = (file_name_index) 1;
18184 unsigned int m_line = 1;
18185
18186 /* These are initialized in the constructor. */
18187
18188 CORE_ADDR m_address;
18189 bool m_is_stmt;
18190 unsigned int m_discriminator;
d9b3de22
DE
18191
18192 /* Additional bits of state we need to track. */
18193
18194 /* The last file that we called dwarf2_start_subfile for.
18195 This is only used for TLLs. */
6f77053d 18196 unsigned int m_last_file = 0;
d9b3de22 18197 /* The last file a line number was recorded for. */
6f77053d 18198 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
18199
18200 /* The function to call to record a line. */
6f77053d 18201 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
18202
18203 /* The last line number that was recorded, used to coalesce
18204 consecutive entries for the same line. This can happen, for
18205 example, when discriminators are present. PR 17276. */
6f77053d
PA
18206 unsigned int m_last_line = 0;
18207 bool m_line_has_non_zero_discriminator = false;
8c43009f 18208};
d9b3de22 18209
6f77053d
PA
18210void
18211lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18212{
18213 CORE_ADDR addr_adj = (((m_op_index + adjust)
18214 / m_line_header->maximum_ops_per_instruction)
18215 * m_line_header->minimum_instruction_length);
18216 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18217 m_op_index = ((m_op_index + adjust)
18218 % m_line_header->maximum_ops_per_instruction);
18219}
d9b3de22 18220
6f77053d
PA
18221void
18222lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 18223{
6f77053d
PA
18224 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18225 CORE_ADDR addr_adj = (((m_op_index
18226 + (adj_opcode / m_line_header->line_range))
18227 / m_line_header->maximum_ops_per_instruction)
18228 * m_line_header->minimum_instruction_length);
18229 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18230 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
18231 % m_line_header->maximum_ops_per_instruction);
d9b3de22 18232
6f77053d
PA
18233 int line_delta = (m_line_header->line_base
18234 + (adj_opcode % m_line_header->line_range));
18235 advance_line (line_delta);
18236 record_line (false);
18237 m_discriminator = 0;
18238}
d9b3de22 18239
6f77053d
PA
18240void
18241lnp_state_machine::handle_set_file (file_name_index file)
18242{
18243 m_file = file;
18244
18245 const file_entry *fe = current_file ();
18246 if (fe == NULL)
18247 dwarf2_debug_line_missing_file_complaint ();
18248 else if (m_record_lines_p)
18249 {
18250 const char *dir = fe->include_dir (m_line_header);
18251
18252 m_last_subfile = current_subfile;
18253 m_line_has_non_zero_discriminator = m_discriminator != 0;
18254 dwarf2_start_subfile (fe->name, dir);
18255 }
18256}
18257
18258void
18259lnp_state_machine::handle_const_add_pc ()
18260{
18261 CORE_ADDR adjust
18262 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18263
18264 CORE_ADDR addr_adj
18265 = (((m_op_index + adjust)
18266 / m_line_header->maximum_ops_per_instruction)
18267 * m_line_header->minimum_instruction_length);
18268
18269 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18270 m_op_index = ((m_op_index + adjust)
18271 % m_line_header->maximum_ops_per_instruction);
18272}
d9b3de22 18273
c91513d8
PP
18274/* Ignore this record_line request. */
18275
18276static void
18277noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18278{
18279 return;
18280}
18281
a05a36a5
DE
18282/* Return non-zero if we should add LINE to the line number table.
18283 LINE is the line to add, LAST_LINE is the last line that was added,
18284 LAST_SUBFILE is the subfile for LAST_LINE.
18285 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18286 had a non-zero discriminator.
18287
18288 We have to be careful in the presence of discriminators.
18289 E.g., for this line:
18290
18291 for (i = 0; i < 100000; i++);
18292
18293 clang can emit four line number entries for that one line,
18294 each with a different discriminator.
18295 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18296
18297 However, we want gdb to coalesce all four entries into one.
18298 Otherwise the user could stepi into the middle of the line and
18299 gdb would get confused about whether the pc really was in the
18300 middle of the line.
18301
18302 Things are further complicated by the fact that two consecutive
18303 line number entries for the same line is a heuristic used by gcc
18304 to denote the end of the prologue. So we can't just discard duplicate
18305 entries, we have to be selective about it. The heuristic we use is
18306 that we only collapse consecutive entries for the same line if at least
18307 one of those entries has a non-zero discriminator. PR 17276.
18308
18309 Note: Addresses in the line number state machine can never go backwards
18310 within one sequence, thus this coalescing is ok. */
18311
18312static int
18313dwarf_record_line_p (unsigned int line, unsigned int last_line,
18314 int line_has_non_zero_discriminator,
18315 struct subfile *last_subfile)
18316{
18317 if (current_subfile != last_subfile)
18318 return 1;
18319 if (line != last_line)
18320 return 1;
18321 /* Same line for the same file that we've seen already.
18322 As a last check, for pr 17276, only record the line if the line
18323 has never had a non-zero discriminator. */
18324 if (!line_has_non_zero_discriminator)
18325 return 1;
18326 return 0;
18327}
18328
252a6764
DE
18329/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18330 in the line table of subfile SUBFILE. */
18331
18332static void
d9b3de22
DE
18333dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18334 unsigned int line, CORE_ADDR address,
18335 record_line_ftype p_record_line)
252a6764
DE
18336{
18337 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18338
27e0867f
DE
18339 if (dwarf_line_debug)
18340 {
18341 fprintf_unfiltered (gdb_stdlog,
18342 "Recording line %u, file %s, address %s\n",
18343 line, lbasename (subfile->name),
18344 paddress (gdbarch, address));
18345 }
18346
d5962de5 18347 (*p_record_line) (subfile, line, addr);
252a6764
DE
18348}
18349
18350/* Subroutine of dwarf_decode_lines_1 to simplify it.
18351 Mark the end of a set of line number records.
d9b3de22 18352 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18353 If SUBFILE is NULL the request is ignored. */
18354
18355static void
18356dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18357 CORE_ADDR address, record_line_ftype p_record_line)
18358{
27e0867f
DE
18359 if (subfile == NULL)
18360 return;
18361
18362 if (dwarf_line_debug)
18363 {
18364 fprintf_unfiltered (gdb_stdlog,
18365 "Finishing current line, file %s, address %s\n",
18366 lbasename (subfile->name),
18367 paddress (gdbarch, address));
18368 }
18369
d9b3de22
DE
18370 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18371}
18372
6f77053d
PA
18373void
18374lnp_state_machine::record_line (bool end_sequence)
d9b3de22 18375{
d9b3de22
DE
18376 if (dwarf_line_debug)
18377 {
18378 fprintf_unfiltered (gdb_stdlog,
18379 "Processing actual line %u: file %u,"
18380 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
18381 m_line, to_underlying (m_file),
18382 paddress (m_gdbarch, m_address),
18383 m_is_stmt, m_discriminator);
d9b3de22
DE
18384 }
18385
6f77053d 18386 file_entry *fe = current_file ();
8c43009f
PA
18387
18388 if (fe == NULL)
d9b3de22
DE
18389 dwarf2_debug_line_missing_file_complaint ();
18390 /* For now we ignore lines not starting on an instruction boundary.
18391 But not when processing end_sequence for compatibility with the
18392 previous version of the code. */
6f77053d 18393 else if (m_op_index == 0 || end_sequence)
d9b3de22 18394 {
8c43009f 18395 fe->included_p = 1;
6f77053d 18396 if (m_record_lines_p && m_is_stmt)
d9b3de22 18397 {
6f77053d 18398 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 18399 {
6f77053d
PA
18400 dwarf_finish_line (m_gdbarch, m_last_subfile,
18401 m_address, m_record_line_callback);
d9b3de22
DE
18402 }
18403
18404 if (!end_sequence)
18405 {
6f77053d
PA
18406 if (dwarf_record_line_p (m_line, m_last_line,
18407 m_line_has_non_zero_discriminator,
18408 m_last_subfile))
d9b3de22 18409 {
6f77053d
PA
18410 dwarf_record_line_1 (m_gdbarch, current_subfile,
18411 m_line, m_address,
18412 m_record_line_callback);
d9b3de22 18413 }
6f77053d
PA
18414 m_last_subfile = current_subfile;
18415 m_last_line = m_line;
d9b3de22
DE
18416 }
18417 }
18418 }
18419}
18420
6f77053d
PA
18421lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
18422 bool record_lines_p)
d9b3de22 18423{
6f77053d
PA
18424 m_gdbarch = arch;
18425 m_record_lines_p = record_lines_p;
18426 m_line_header = lh;
d9b3de22 18427
6f77053d 18428 m_record_line_callback = ::record_line;
d9b3de22 18429
d9b3de22
DE
18430 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18431 was a line entry for it so that the backend has a chance to adjust it
18432 and also record it in case it needs it. This is currently used by MIPS
18433 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
18434 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
18435 m_is_stmt = lh->default_is_stmt;
18436 m_discriminator = 0;
252a6764
DE
18437}
18438
6f77053d
PA
18439void
18440lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18441 const gdb_byte *line_ptr,
18442 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
18443{
18444 /* If address < lowpc then it's not a usable value, it's outside the
18445 pc range of the CU. However, we restrict the test to only address
18446 values of zero to preserve GDB's previous behaviour which is to
18447 handle the specific case of a function being GC'd by the linker. */
18448
18449 if (address == 0 && address < lowpc)
18450 {
18451 /* This line table is for a function which has been
18452 GCd by the linker. Ignore it. PR gdb/12528 */
18453
18454 struct objfile *objfile = cu->objfile;
18455 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18456
18457 complaint (&symfile_complaints,
18458 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18459 line_offset, objfile_name (objfile));
6f77053d
PA
18460 m_record_line_callback = noop_record_line;
18461 /* Note: record_line_callback is left as noop_record_line until
18462 we see DW_LNE_end_sequence. */
924c2928
DE
18463 }
18464}
18465
f3f5162e 18466/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18467 Process the line number information in LH.
18468 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18469 program in order to set included_p for every referenced header. */
debd256d 18470
c906108c 18471static void
43f3e411
DE
18472dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18473 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18474{
d521ce57
TT
18475 const gdb_byte *line_ptr, *extended_end;
18476 const gdb_byte *line_end;
a8c50c1f 18477 unsigned int bytes_read, extended_len;
699ca60a 18478 unsigned char op_code, extended_op;
e142c38c
DJ
18479 CORE_ADDR baseaddr;
18480 struct objfile *objfile = cu->objfile;
f3f5162e 18481 bfd *abfd = objfile->obfd;
fbf65064 18482 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
18483 /* True if we're recording line info (as opposed to building partial
18484 symtabs and just interested in finding include files mentioned by
18485 the line number program). */
18486 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
18487
18488 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18489
debd256d
JB
18490 line_ptr = lh->statement_program_start;
18491 line_end = lh->statement_program_end;
c906108c
SS
18492
18493 /* Read the statement sequences until there's nothing left. */
18494 while (line_ptr < line_end)
18495 {
6f77053d
PA
18496 /* The DWARF line number program state machine. Reset the state
18497 machine at the start of each sequence. */
18498 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
18499 bool end_sequence = false;
d9b3de22 18500
8c43009f 18501 if (record_lines_p)
c906108c 18502 {
8c43009f
PA
18503 /* Start a subfile for the current file of the state
18504 machine. */
18505 const file_entry *fe = state_machine.current_file ();
18506
18507 if (fe != NULL)
18508 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
18509 }
18510
a738430d 18511 /* Decode the table. */
d9b3de22 18512 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18513 {
18514 op_code = read_1_byte (abfd, line_ptr);
18515 line_ptr += 1;
9aa1fe7e 18516
debd256d 18517 if (op_code >= lh->opcode_base)
6e70227d 18518 {
8e07a239 18519 /* Special opcode. */
6f77053d 18520 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
18521 }
18522 else switch (op_code)
c906108c
SS
18523 {
18524 case DW_LNS_extended_op:
3e43a32a
MS
18525 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18526 &bytes_read);
473b7be6 18527 line_ptr += bytes_read;
a8c50c1f 18528 extended_end = line_ptr + extended_len;
c906108c
SS
18529 extended_op = read_1_byte (abfd, line_ptr);
18530 line_ptr += 1;
18531 switch (extended_op)
18532 {
18533 case DW_LNE_end_sequence:
6f77053d
PA
18534 state_machine.handle_end_sequence ();
18535 end_sequence = true;
c906108c
SS
18536 break;
18537 case DW_LNE_set_address:
d9b3de22
DE
18538 {
18539 CORE_ADDR address
18540 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 18541 line_ptr += bytes_read;
6f77053d
PA
18542
18543 state_machine.check_line_address (cu, line_ptr,
18544 lowpc, address);
18545 state_machine.handle_set_address (baseaddr, address);
d9b3de22 18546 }
c906108c
SS
18547 break;
18548 case DW_LNE_define_file:
debd256d 18549 {
d521ce57 18550 const char *cur_file;
ecfb656c
PA
18551 unsigned int mod_time, length;
18552 dir_index dindex;
6e70227d 18553
3e43a32a
MS
18554 cur_file = read_direct_string (abfd, line_ptr,
18555 &bytes_read);
debd256d 18556 line_ptr += bytes_read;
ecfb656c 18557 dindex = (dir_index)
debd256d
JB
18558 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18559 line_ptr += bytes_read;
18560 mod_time =
18561 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18562 line_ptr += bytes_read;
18563 length =
18564 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18565 line_ptr += bytes_read;
ecfb656c 18566 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 18567 }
c906108c 18568 break;
d0c6ba3d 18569 case DW_LNE_set_discriminator:
6f77053d
PA
18570 {
18571 /* The discriminator is not interesting to the
18572 debugger; just ignore it. We still need to
18573 check its value though:
18574 if there are consecutive entries for the same
18575 (non-prologue) line we want to coalesce them.
18576 PR 17276. */
18577 unsigned int discr
18578 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18579 line_ptr += bytes_read;
18580
18581 state_machine.handle_set_discriminator (discr);
18582 }
d0c6ba3d 18583 break;
c906108c 18584 default:
4d3c2250 18585 complaint (&symfile_complaints,
e2e0b3e5 18586 _("mangled .debug_line section"));
debd256d 18587 return;
c906108c 18588 }
a8c50c1f
DJ
18589 /* Make sure that we parsed the extended op correctly. If e.g.
18590 we expected a different address size than the producer used,
18591 we may have read the wrong number of bytes. */
18592 if (line_ptr != extended_end)
18593 {
18594 complaint (&symfile_complaints,
18595 _("mangled .debug_line section"));
18596 return;
18597 }
c906108c
SS
18598 break;
18599 case DW_LNS_copy:
6f77053d 18600 state_machine.handle_copy ();
c906108c
SS
18601 break;
18602 case DW_LNS_advance_pc:
2dc7f7b3
TT
18603 {
18604 CORE_ADDR adjust
18605 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 18606 line_ptr += bytes_read;
6f77053d
PA
18607
18608 state_machine.handle_advance_pc (adjust);
2dc7f7b3 18609 }
c906108c
SS
18610 break;
18611 case DW_LNS_advance_line:
a05a36a5
DE
18612 {
18613 int line_delta
18614 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 18615 line_ptr += bytes_read;
6f77053d
PA
18616
18617 state_machine.handle_advance_line (line_delta);
a05a36a5 18618 }
c906108c
SS
18619 break;
18620 case DW_LNS_set_file:
d9b3de22 18621 {
6f77053d 18622 file_name_index file
ecfb656c
PA
18623 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18624 &bytes_read);
d9b3de22 18625 line_ptr += bytes_read;
8c43009f 18626
6f77053d 18627 state_machine.handle_set_file (file);
d9b3de22 18628 }
c906108c
SS
18629 break;
18630 case DW_LNS_set_column:
0ad93d4f 18631 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18632 line_ptr += bytes_read;
18633 break;
18634 case DW_LNS_negate_stmt:
6f77053d 18635 state_machine.handle_negate_stmt ();
c906108c
SS
18636 break;
18637 case DW_LNS_set_basic_block:
c906108c 18638 break;
c2c6d25f
JM
18639 /* Add to the address register of the state machine the
18640 address increment value corresponding to special opcode
a738430d
MK
18641 255. I.e., this value is scaled by the minimum
18642 instruction length since special opcode 255 would have
b021a221 18643 scaled the increment. */
c906108c 18644 case DW_LNS_const_add_pc:
6f77053d 18645 state_machine.handle_const_add_pc ();
c906108c
SS
18646 break;
18647 case DW_LNS_fixed_advance_pc:
3e29f34a 18648 {
6f77053d 18649 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 18650 line_ptr += 2;
6f77053d
PA
18651
18652 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 18653 }
c906108c 18654 break;
9aa1fe7e 18655 default:
a738430d
MK
18656 {
18657 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18658 int i;
a738430d 18659
debd256d 18660 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18661 {
18662 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18663 line_ptr += bytes_read;
18664 }
18665 }
c906108c
SS
18666 }
18667 }
d9b3de22
DE
18668
18669 if (!end_sequence)
18670 dwarf2_debug_line_missing_end_sequence_complaint ();
18671
18672 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18673 in which case we still finish recording the last line). */
6f77053d 18674 state_machine.record_line (true);
c906108c 18675 }
f3f5162e
DE
18676}
18677
18678/* Decode the Line Number Program (LNP) for the given line_header
18679 structure and CU. The actual information extracted and the type
18680 of structures created from the LNP depends on the value of PST.
18681
18682 1. If PST is NULL, then this procedure uses the data from the program
18683 to create all necessary symbol tables, and their linetables.
18684
18685 2. If PST is not NULL, this procedure reads the program to determine
18686 the list of files included by the unit represented by PST, and
18687 builds all the associated partial symbol tables.
18688
18689 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18690 It is used for relative paths in the line table.
18691 NOTE: When processing partial symtabs (pst != NULL),
18692 comp_dir == pst->dirname.
18693
18694 NOTE: It is important that psymtabs have the same file name (via strcmp)
18695 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18696 symtab we don't use it in the name of the psymtabs we create.
18697 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18698 A good testcase for this is mb-inline.exp.
18699
527f3840
JK
18700 LOWPC is the lowest address in CU (or 0 if not known).
18701
18702 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18703 for its PC<->lines mapping information. Otherwise only the filename
18704 table is read in. */
f3f5162e
DE
18705
18706static void
18707dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18708 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18709 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18710{
18711 struct objfile *objfile = cu->objfile;
18712 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18713
527f3840
JK
18714 if (decode_mapping)
18715 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18716
18717 if (decode_for_pst_p)
18718 {
18719 int file_index;
18720
18721 /* Now that we're done scanning the Line Header Program, we can
18722 create the psymtab of each included file. */
fff8551c 18723 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
18724 if (lh->file_names[file_index].included_p == 1)
18725 {
d521ce57 18726 const char *include_name =
c6da4cef
DE
18727 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18728 if (include_name != NULL)
aaa75496
JB
18729 dwarf2_create_include_psymtab (include_name, pst, objfile);
18730 }
18731 }
cb1df416
DJ
18732 else
18733 {
18734 /* Make sure a symtab is created for every file, even files
18735 which contain only variables (i.e. no code with associated
18736 line numbers). */
43f3e411 18737 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18738 int i;
cb1df416 18739
fff8551c 18740 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 18741 {
8c43009f 18742 file_entry &fe = lh->file_names[i];
9a619af0 18743
8c43009f 18744 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 18745
cb1df416 18746 if (current_subfile->symtab == NULL)
43f3e411
DE
18747 {
18748 current_subfile->symtab
18749 = allocate_symtab (cust, current_subfile->name);
18750 }
8c43009f 18751 fe.symtab = current_subfile->symtab;
cb1df416
DJ
18752 }
18753 }
c906108c
SS
18754}
18755
18756/* Start a subfile for DWARF. FILENAME is the name of the file and
18757 DIRNAME the name of the source directory which contains FILENAME
4d663531 18758 or NULL if not known.
c906108c
SS
18759 This routine tries to keep line numbers from identical absolute and
18760 relative file names in a common subfile.
18761
18762 Using the `list' example from the GDB testsuite, which resides in
18763 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18764 of /srcdir/list0.c yields the following debugging information for list0.c:
18765
c5aa993b 18766 DW_AT_name: /srcdir/list0.c
4d663531 18767 DW_AT_comp_dir: /compdir
357e46e7 18768 files.files[0].name: list0.h
c5aa993b 18769 files.files[0].dir: /srcdir
357e46e7 18770 files.files[1].name: list0.c
c5aa993b 18771 files.files[1].dir: /srcdir
c906108c
SS
18772
18773 The line number information for list0.c has to end up in a single
4f1520fb
FR
18774 subfile, so that `break /srcdir/list0.c:1' works as expected.
18775 start_subfile will ensure that this happens provided that we pass the
18776 concatenation of files.files[1].dir and files.files[1].name as the
18777 subfile's name. */
c906108c
SS
18778
18779static void
4d663531 18780dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18781{
d521ce57 18782 char *copy = NULL;
4f1520fb 18783
4d663531 18784 /* In order not to lose the line information directory,
4f1520fb
FR
18785 we concatenate it to the filename when it makes sense.
18786 Note that the Dwarf3 standard says (speaking of filenames in line
18787 information): ``The directory index is ignored for file names
18788 that represent full path names''. Thus ignoring dirname in the
18789 `else' branch below isn't an issue. */
c906108c 18790
d5166ae1 18791 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18792 {
18793 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18794 filename = copy;
18795 }
c906108c 18796
4d663531 18797 start_subfile (filename);
4f1520fb 18798
d521ce57
TT
18799 if (copy != NULL)
18800 xfree (copy);
c906108c
SS
18801}
18802
f4dc4d17
DE
18803/* Start a symtab for DWARF.
18804 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18805
43f3e411 18806static struct compunit_symtab *
f4dc4d17 18807dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18808 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18809{
43f3e411
DE
18810 struct compunit_symtab *cust
18811 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18812
f4dc4d17
DE
18813 record_debugformat ("DWARF 2");
18814 record_producer (cu->producer);
18815
18816 /* We assume that we're processing GCC output. */
18817 processing_gcc_compilation = 2;
18818
4d4ec4e5 18819 cu->processing_has_namespace_info = 0;
43f3e411
DE
18820
18821 return cust;
f4dc4d17
DE
18822}
18823
4c2df51b
DJ
18824static void
18825var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18826 struct dwarf2_cu *cu)
4c2df51b 18827{
e7c27a73
DJ
18828 struct objfile *objfile = cu->objfile;
18829 struct comp_unit_head *cu_header = &cu->header;
18830
4c2df51b
DJ
18831 /* NOTE drow/2003-01-30: There used to be a comment and some special
18832 code here to turn a symbol with DW_AT_external and a
18833 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18834 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18835 with some versions of binutils) where shared libraries could have
18836 relocations against symbols in their debug information - the
18837 minimal symbol would have the right address, but the debug info
18838 would not. It's no longer necessary, because we will explicitly
18839 apply relocations when we read in the debug information now. */
18840
18841 /* A DW_AT_location attribute with no contents indicates that a
18842 variable has been optimized away. */
18843 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18844 {
f1e6e072 18845 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18846 return;
18847 }
18848
18849 /* Handle one degenerate form of location expression specially, to
18850 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18851 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18852 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18853
18854 if (attr_form_is_block (attr)
3019eac3
DE
18855 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18856 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18857 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18858 && (DW_BLOCK (attr)->size
18859 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18860 {
891d2f0b 18861 unsigned int dummy;
4c2df51b 18862
3019eac3
DE
18863 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18864 SYMBOL_VALUE_ADDRESS (sym) =
18865 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18866 else
18867 SYMBOL_VALUE_ADDRESS (sym) =
18868 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18869 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18870 fixup_symbol_section (sym, objfile);
18871 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18872 SYMBOL_SECTION (sym));
4c2df51b
DJ
18873 return;
18874 }
18875
18876 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18877 expression evaluator, and use LOC_COMPUTED only when necessary
18878 (i.e. when the value of a register or memory location is
18879 referenced, or a thread-local block, etc.). Then again, it might
18880 not be worthwhile. I'm assuming that it isn't unless performance
18881 or memory numbers show me otherwise. */
18882
f1e6e072 18883 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18884
f1e6e072 18885 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18886 cu->has_loclist = 1;
4c2df51b
DJ
18887}
18888
c906108c
SS
18889/* Given a pointer to a DWARF information entry, figure out if we need
18890 to make a symbol table entry for it, and if so, create a new entry
18891 and return a pointer to it.
18892 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18893 used the passed type.
18894 If SPACE is not NULL, use it to hold the new symbol. If it is
18895 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18896
18897static struct symbol *
34eaf542
TT
18898new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18899 struct symbol *space)
c906108c 18900{
e7c27a73 18901 struct objfile *objfile = cu->objfile;
3e29f34a 18902 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18903 struct symbol *sym = NULL;
15d034d0 18904 const char *name;
c906108c
SS
18905 struct attribute *attr = NULL;
18906 struct attribute *attr2 = NULL;
e142c38c 18907 CORE_ADDR baseaddr;
e37fd15a
SW
18908 struct pending **list_to_add = NULL;
18909
edb3359d 18910 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18911
18912 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18913
94af9270 18914 name = dwarf2_name (die, cu);
c906108c
SS
18915 if (name)
18916 {
94af9270 18917 const char *linkagename;
34eaf542 18918 int suppress_add = 0;
94af9270 18919
34eaf542
TT
18920 if (space)
18921 sym = space;
18922 else
e623cf5d 18923 sym = allocate_symbol (objfile);
c906108c 18924 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18925
18926 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18927 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18928 linkagename = dwarf2_physname (name, die, cu);
18929 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18930
f55ee35c
JK
18931 /* Fortran does not have mangling standard and the mangling does differ
18932 between gfortran, iFort etc. */
18933 if (cu->language == language_fortran
b250c185 18934 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18935 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18936 dwarf2_full_name (name, die, cu),
29df156d 18937 NULL);
f55ee35c 18938
c906108c 18939 /* Default assumptions.
c5aa993b 18940 Use the passed type or decode it from the die. */
176620f1 18941 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18942 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18943 if (type != NULL)
18944 SYMBOL_TYPE (sym) = type;
18945 else
e7c27a73 18946 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18947 attr = dwarf2_attr (die,
18948 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18949 cu);
c906108c
SS
18950 if (attr)
18951 {
18952 SYMBOL_LINE (sym) = DW_UNSND (attr);
18953 }
cb1df416 18954
edb3359d
DJ
18955 attr = dwarf2_attr (die,
18956 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18957 cu);
cb1df416
DJ
18958 if (attr)
18959 {
ecfb656c 18960 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 18961 struct file_entry *fe;
9a619af0 18962
ecfb656c
PA
18963 if (cu->line_header != NULL)
18964 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
18965 else
18966 fe = NULL;
18967
18968 if (fe == NULL)
cb1df416
DJ
18969 complaint (&symfile_complaints,
18970 _("file index out of range"));
8c43009f
PA
18971 else
18972 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18973 }
18974
c906108c
SS
18975 switch (die->tag)
18976 {
18977 case DW_TAG_label:
e142c38c 18978 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18979 if (attr)
3e29f34a
MR
18980 {
18981 CORE_ADDR addr;
18982
18983 addr = attr_value_as_address (attr);
18984 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18985 SYMBOL_VALUE_ADDRESS (sym) = addr;
18986 }
0f5238ed
TT
18987 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18988 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18989 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18990 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18991 break;
18992 case DW_TAG_subprogram:
18993 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18994 finish_block. */
f1e6e072 18995 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18996 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18997 if ((attr2 && (DW_UNSND (attr2) != 0))
18998 || cu->language == language_ada)
c906108c 18999 {
2cfa0c8d
JB
19000 /* Subprograms marked external are stored as a global symbol.
19001 Ada subprograms, whether marked external or not, are always
19002 stored as a global symbol, because we want to be able to
19003 access them globally. For instance, we want to be able
19004 to break on a nested subprogram without having to
19005 specify the context. */
e37fd15a 19006 list_to_add = &global_symbols;
c906108c
SS
19007 }
19008 else
19009 {
e37fd15a 19010 list_to_add = cu->list_in_scope;
c906108c
SS
19011 }
19012 break;
edb3359d
DJ
19013 case DW_TAG_inlined_subroutine:
19014 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19015 finish_block. */
f1e6e072 19016 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 19017 SYMBOL_INLINED (sym) = 1;
481860b3 19018 list_to_add = cu->list_in_scope;
edb3359d 19019 break;
34eaf542
TT
19020 case DW_TAG_template_value_param:
19021 suppress_add = 1;
19022 /* Fall through. */
72929c62 19023 case DW_TAG_constant:
c906108c 19024 case DW_TAG_variable:
254e6b9e 19025 case DW_TAG_member:
0963b4bd
MS
19026 /* Compilation with minimal debug info may result in
19027 variables with missing type entries. Change the
19028 misleading `void' type to something sensible. */
c906108c 19029 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 19030 SYMBOL_TYPE (sym)
46bf5051 19031 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 19032
e142c38c 19033 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
19034 /* In the case of DW_TAG_member, we should only be called for
19035 static const members. */
19036 if (die->tag == DW_TAG_member)
19037 {
3863f96c
DE
19038 /* dwarf2_add_field uses die_is_declaration,
19039 so we do the same. */
254e6b9e
DE
19040 gdb_assert (die_is_declaration (die, cu));
19041 gdb_assert (attr);
19042 }
c906108c
SS
19043 if (attr)
19044 {
e7c27a73 19045 dwarf2_const_value (attr, sym, cu);
e142c38c 19046 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 19047 if (!suppress_add)
34eaf542
TT
19048 {
19049 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 19050 list_to_add = &global_symbols;
34eaf542 19051 else
e37fd15a 19052 list_to_add = cu->list_in_scope;
34eaf542 19053 }
c906108c
SS
19054 break;
19055 }
e142c38c 19056 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19057 if (attr)
19058 {
e7c27a73 19059 var_decode_location (attr, sym, cu);
e142c38c 19060 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19061
19062 /* Fortran explicitly imports any global symbols to the local
19063 scope by DW_TAG_common_block. */
19064 if (cu->language == language_fortran && die->parent
19065 && die->parent->tag == DW_TAG_common_block)
19066 attr2 = NULL;
19067
caac4577
JG
19068 if (SYMBOL_CLASS (sym) == LOC_STATIC
19069 && SYMBOL_VALUE_ADDRESS (sym) == 0
19070 && !dwarf2_per_objfile->has_section_at_zero)
19071 {
19072 /* When a static variable is eliminated by the linker,
19073 the corresponding debug information is not stripped
19074 out, but the variable address is set to null;
19075 do not add such variables into symbol table. */
19076 }
19077 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19078 {
f55ee35c
JK
19079 /* Workaround gfortran PR debug/40040 - it uses
19080 DW_AT_location for variables in -fPIC libraries which may
19081 get overriden by other libraries/executable and get
19082 a different address. Resolve it by the minimal symbol
19083 which may come from inferior's executable using copy
19084 relocation. Make this workaround only for gfortran as for
19085 other compilers GDB cannot guess the minimal symbol
19086 Fortran mangling kind. */
19087 if (cu->language == language_fortran && die->parent
19088 && die->parent->tag == DW_TAG_module
19089 && cu->producer
28586665 19090 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19091 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19092
1c809c68
TT
19093 /* A variable with DW_AT_external is never static,
19094 but it may be block-scoped. */
19095 list_to_add = (cu->list_in_scope == &file_symbols
19096 ? &global_symbols : cu->list_in_scope);
1c809c68 19097 }
c906108c 19098 else
e37fd15a 19099 list_to_add = cu->list_in_scope;
c906108c
SS
19100 }
19101 else
19102 {
19103 /* We do not know the address of this symbol.
c5aa993b
JM
19104 If it is an external symbol and we have type information
19105 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19106 The address of the variable will then be determined from
19107 the minimal symbol table whenever the variable is
19108 referenced. */
e142c38c 19109 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19110
19111 /* Fortran explicitly imports any global symbols to the local
19112 scope by DW_TAG_common_block. */
19113 if (cu->language == language_fortran && die->parent
19114 && die->parent->tag == DW_TAG_common_block)
19115 {
19116 /* SYMBOL_CLASS doesn't matter here because
19117 read_common_block is going to reset it. */
19118 if (!suppress_add)
19119 list_to_add = cu->list_in_scope;
19120 }
19121 else if (attr2 && (DW_UNSND (attr2) != 0)
19122 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19123 {
0fe7935b
DJ
19124 /* A variable with DW_AT_external is never static, but it
19125 may be block-scoped. */
19126 list_to_add = (cu->list_in_scope == &file_symbols
19127 ? &global_symbols : cu->list_in_scope);
19128
f1e6e072 19129 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19130 }
442ddf59
JK
19131 else if (!die_is_declaration (die, cu))
19132 {
19133 /* Use the default LOC_OPTIMIZED_OUT class. */
19134 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19135 if (!suppress_add)
19136 list_to_add = cu->list_in_scope;
442ddf59 19137 }
c906108c
SS
19138 }
19139 break;
19140 case DW_TAG_formal_parameter:
edb3359d
DJ
19141 /* If we are inside a function, mark this as an argument. If
19142 not, we might be looking at an argument to an inlined function
19143 when we do not have enough information to show inlined frames;
19144 pretend it's a local variable in that case so that the user can
19145 still see it. */
19146 if (context_stack_depth > 0
19147 && context_stack[context_stack_depth - 1].name != NULL)
19148 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19149 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19150 if (attr)
19151 {
e7c27a73 19152 var_decode_location (attr, sym, cu);
c906108c 19153 }
e142c38c 19154 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19155 if (attr)
19156 {
e7c27a73 19157 dwarf2_const_value (attr, sym, cu);
c906108c 19158 }
f346a30d 19159
e37fd15a 19160 list_to_add = cu->list_in_scope;
c906108c
SS
19161 break;
19162 case DW_TAG_unspecified_parameters:
19163 /* From varargs functions; gdb doesn't seem to have any
19164 interest in this information, so just ignore it for now.
19165 (FIXME?) */
19166 break;
34eaf542
TT
19167 case DW_TAG_template_type_param:
19168 suppress_add = 1;
19169 /* Fall through. */
c906108c 19170 case DW_TAG_class_type:
680b30c7 19171 case DW_TAG_interface_type:
c906108c
SS
19172 case DW_TAG_structure_type:
19173 case DW_TAG_union_type:
72019c9c 19174 case DW_TAG_set_type:
c906108c 19175 case DW_TAG_enumeration_type:
f1e6e072 19176 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19177 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19178
63d06c5c 19179 {
9c37b5ae 19180 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19181 really ever be static objects: otherwise, if you try
19182 to, say, break of a class's method and you're in a file
19183 which doesn't mention that class, it won't work unless
19184 the check for all static symbols in lookup_symbol_aux
19185 saves you. See the OtherFileClass tests in
19186 gdb.c++/namespace.exp. */
19187
e37fd15a 19188 if (!suppress_add)
34eaf542 19189 {
34eaf542 19190 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19191 && cu->language == language_cplus
34eaf542 19192 ? &global_symbols : cu->list_in_scope);
63d06c5c 19193
64382290 19194 /* The semantics of C++ state that "struct foo {
9c37b5ae 19195 ... }" also defines a typedef for "foo". */
64382290 19196 if (cu->language == language_cplus
45280282 19197 || cu->language == language_ada
c44af4eb
TT
19198 || cu->language == language_d
19199 || cu->language == language_rust)
64382290
TT
19200 {
19201 /* The symbol's name is already allocated along
19202 with this objfile, so we don't need to
19203 duplicate it for the type. */
19204 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19205 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19206 }
63d06c5c
DC
19207 }
19208 }
c906108c
SS
19209 break;
19210 case DW_TAG_typedef:
f1e6e072 19211 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19212 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19213 list_to_add = cu->list_in_scope;
63d06c5c 19214 break;
c906108c 19215 case DW_TAG_base_type:
a02abb62 19216 case DW_TAG_subrange_type:
f1e6e072 19217 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19218 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19219 list_to_add = cu->list_in_scope;
c906108c
SS
19220 break;
19221 case DW_TAG_enumerator:
e142c38c 19222 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19223 if (attr)
19224 {
e7c27a73 19225 dwarf2_const_value (attr, sym, cu);
c906108c 19226 }
63d06c5c
DC
19227 {
19228 /* NOTE: carlton/2003-11-10: See comment above in the
19229 DW_TAG_class_type, etc. block. */
19230
e142c38c 19231 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19232 && cu->language == language_cplus
e142c38c 19233 ? &global_symbols : cu->list_in_scope);
63d06c5c 19234 }
c906108c 19235 break;
74921315 19236 case DW_TAG_imported_declaration:
5c4e30ca 19237 case DW_TAG_namespace:
f1e6e072 19238 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19239 list_to_add = &global_symbols;
5c4e30ca 19240 break;
530e8392
KB
19241 case DW_TAG_module:
19242 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19243 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19244 list_to_add = &global_symbols;
19245 break;
4357ac6c 19246 case DW_TAG_common_block:
f1e6e072 19247 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19248 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19249 add_symbol_to_list (sym, cu->list_in_scope);
19250 break;
c906108c
SS
19251 default:
19252 /* Not a tag we recognize. Hopefully we aren't processing
19253 trash data, but since we must specifically ignore things
19254 we don't recognize, there is nothing else we should do at
0963b4bd 19255 this point. */
e2e0b3e5 19256 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19257 dwarf_tag_name (die->tag));
c906108c
SS
19258 break;
19259 }
df8a16a1 19260
e37fd15a
SW
19261 if (suppress_add)
19262 {
19263 sym->hash_next = objfile->template_symbols;
19264 objfile->template_symbols = sym;
19265 list_to_add = NULL;
19266 }
19267
19268 if (list_to_add != NULL)
19269 add_symbol_to_list (sym, list_to_add);
19270
df8a16a1
DJ
19271 /* For the benefit of old versions of GCC, check for anonymous
19272 namespaces based on the demangled name. */
4d4ec4e5 19273 if (!cu->processing_has_namespace_info
94af9270 19274 && cu->language == language_cplus)
a10964d1 19275 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19276 }
19277 return (sym);
19278}
19279
34eaf542
TT
19280/* A wrapper for new_symbol_full that always allocates a new symbol. */
19281
19282static struct symbol *
19283new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19284{
19285 return new_symbol_full (die, type, cu, NULL);
19286}
19287
98bfdba5
PA
19288/* Given an attr with a DW_FORM_dataN value in host byte order,
19289 zero-extend it as appropriate for the symbol's type. The DWARF
19290 standard (v4) is not entirely clear about the meaning of using
19291 DW_FORM_dataN for a constant with a signed type, where the type is
19292 wider than the data. The conclusion of a discussion on the DWARF
19293 list was that this is unspecified. We choose to always zero-extend
19294 because that is the interpretation long in use by GCC. */
c906108c 19295
98bfdba5 19296static gdb_byte *
ff39bb5e 19297dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19298 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19299{
e7c27a73 19300 struct objfile *objfile = cu->objfile;
e17a4113
UW
19301 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19302 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19303 LONGEST l = DW_UNSND (attr);
19304
19305 if (bits < sizeof (*value) * 8)
19306 {
19307 l &= ((LONGEST) 1 << bits) - 1;
19308 *value = l;
19309 }
19310 else if (bits == sizeof (*value) * 8)
19311 *value = l;
19312 else
19313 {
224c3ddb 19314 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19315 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19316 return bytes;
19317 }
19318
19319 return NULL;
19320}
19321
19322/* Read a constant value from an attribute. Either set *VALUE, or if
19323 the value does not fit in *VALUE, set *BYTES - either already
19324 allocated on the objfile obstack, or newly allocated on OBSTACK,
19325 or, set *BATON, if we translated the constant to a location
19326 expression. */
19327
19328static void
ff39bb5e 19329dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19330 const char *name, struct obstack *obstack,
19331 struct dwarf2_cu *cu,
d521ce57 19332 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19333 struct dwarf2_locexpr_baton **baton)
19334{
19335 struct objfile *objfile = cu->objfile;
19336 struct comp_unit_head *cu_header = &cu->header;
c906108c 19337 struct dwarf_block *blk;
98bfdba5
PA
19338 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19339 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19340
19341 *value = 0;
19342 *bytes = NULL;
19343 *baton = NULL;
c906108c
SS
19344
19345 switch (attr->form)
19346 {
19347 case DW_FORM_addr:
3019eac3 19348 case DW_FORM_GNU_addr_index:
ac56253d 19349 {
ac56253d
TT
19350 gdb_byte *data;
19351
98bfdba5
PA
19352 if (TYPE_LENGTH (type) != cu_header->addr_size)
19353 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19354 cu_header->addr_size,
98bfdba5 19355 TYPE_LENGTH (type));
ac56253d
TT
19356 /* Symbols of this form are reasonably rare, so we just
19357 piggyback on the existing location code rather than writing
19358 a new implementation of symbol_computed_ops. */
8d749320 19359 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19360 (*baton)->per_cu = cu->per_cu;
19361 gdb_assert ((*baton)->per_cu);
ac56253d 19362
98bfdba5 19363 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19364 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19365 (*baton)->data = data;
ac56253d
TT
19366
19367 data[0] = DW_OP_addr;
19368 store_unsigned_integer (&data[1], cu_header->addr_size,
19369 byte_order, DW_ADDR (attr));
19370 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19371 }
c906108c 19372 break;
4ac36638 19373 case DW_FORM_string:
93b5768b 19374 case DW_FORM_strp:
3019eac3 19375 case DW_FORM_GNU_str_index:
36586728 19376 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19377 /* DW_STRING is already allocated on the objfile obstack, point
19378 directly to it. */
d521ce57 19379 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19380 break;
c906108c
SS
19381 case DW_FORM_block1:
19382 case DW_FORM_block2:
19383 case DW_FORM_block4:
19384 case DW_FORM_block:
2dc7f7b3 19385 case DW_FORM_exprloc:
0224619f 19386 case DW_FORM_data16:
c906108c 19387 blk = DW_BLOCK (attr);
98bfdba5
PA
19388 if (TYPE_LENGTH (type) != blk->size)
19389 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19390 TYPE_LENGTH (type));
19391 *bytes = blk->data;
c906108c 19392 break;
2df3850c
JM
19393
19394 /* The DW_AT_const_value attributes are supposed to carry the
19395 symbol's value "represented as it would be on the target
19396 architecture." By the time we get here, it's already been
19397 converted to host endianness, so we just need to sign- or
19398 zero-extend it as appropriate. */
19399 case DW_FORM_data1:
3aef2284 19400 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19401 break;
c906108c 19402 case DW_FORM_data2:
3aef2284 19403 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19404 break;
c906108c 19405 case DW_FORM_data4:
3aef2284 19406 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19407 break;
c906108c 19408 case DW_FORM_data8:
3aef2284 19409 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19410 break;
19411
c906108c 19412 case DW_FORM_sdata:
98bfdba5 19413 *value = DW_SND (attr);
2df3850c
JM
19414 break;
19415
c906108c 19416 case DW_FORM_udata:
98bfdba5 19417 *value = DW_UNSND (attr);
c906108c 19418 break;
2df3850c 19419
c906108c 19420 default:
4d3c2250 19421 complaint (&symfile_complaints,
e2e0b3e5 19422 _("unsupported const value attribute form: '%s'"),
4d3c2250 19423 dwarf_form_name (attr->form));
98bfdba5 19424 *value = 0;
c906108c
SS
19425 break;
19426 }
19427}
19428
2df3850c 19429
98bfdba5
PA
19430/* Copy constant value from an attribute to a symbol. */
19431
2df3850c 19432static void
ff39bb5e 19433dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19434 struct dwarf2_cu *cu)
2df3850c 19435{
98bfdba5 19436 struct objfile *objfile = cu->objfile;
12df843f 19437 LONGEST value;
d521ce57 19438 const gdb_byte *bytes;
98bfdba5 19439 struct dwarf2_locexpr_baton *baton;
2df3850c 19440
98bfdba5
PA
19441 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19442 SYMBOL_PRINT_NAME (sym),
19443 &objfile->objfile_obstack, cu,
19444 &value, &bytes, &baton);
2df3850c 19445
98bfdba5
PA
19446 if (baton != NULL)
19447 {
98bfdba5 19448 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19449 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19450 }
19451 else if (bytes != NULL)
19452 {
19453 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19454 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19455 }
19456 else
19457 {
19458 SYMBOL_VALUE (sym) = value;
f1e6e072 19459 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19460 }
2df3850c
JM
19461}
19462
c906108c
SS
19463/* Return the type of the die in question using its DW_AT_type attribute. */
19464
19465static struct type *
e7c27a73 19466die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19467{
c906108c 19468 struct attribute *type_attr;
c906108c 19469
e142c38c 19470 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19471 if (!type_attr)
19472 {
19473 /* A missing DW_AT_type represents a void type. */
46bf5051 19474 return objfile_type (cu->objfile)->builtin_void;
c906108c 19475 }
348e048f 19476
673bfd45 19477 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19478}
19479
b4ba55a1
JB
19480/* True iff CU's producer generates GNAT Ada auxiliary information
19481 that allows to find parallel types through that information instead
19482 of having to do expensive parallel lookups by type name. */
19483
19484static int
19485need_gnat_info (struct dwarf2_cu *cu)
19486{
19487 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19488 of GNAT produces this auxiliary information, without any indication
19489 that it is produced. Part of enhancing the FSF version of GNAT
19490 to produce that information will be to put in place an indicator
19491 that we can use in order to determine whether the descriptive type
19492 info is available or not. One suggestion that has been made is
19493 to use a new attribute, attached to the CU die. For now, assume
19494 that the descriptive type info is not available. */
19495 return 0;
19496}
19497
b4ba55a1
JB
19498/* Return the auxiliary type of the die in question using its
19499 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19500 attribute is not present. */
19501
19502static struct type *
19503die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19504{
b4ba55a1 19505 struct attribute *type_attr;
b4ba55a1
JB
19506
19507 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19508 if (!type_attr)
19509 return NULL;
19510
673bfd45 19511 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19512}
19513
19514/* If DIE has a descriptive_type attribute, then set the TYPE's
19515 descriptive type accordingly. */
19516
19517static void
19518set_descriptive_type (struct type *type, struct die_info *die,
19519 struct dwarf2_cu *cu)
19520{
19521 struct type *descriptive_type = die_descriptive_type (die, cu);
19522
19523 if (descriptive_type)
19524 {
19525 ALLOCATE_GNAT_AUX_TYPE (type);
19526 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19527 }
19528}
19529
c906108c
SS
19530/* Return the containing type of the die in question using its
19531 DW_AT_containing_type attribute. */
19532
19533static struct type *
e7c27a73 19534die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19535{
c906108c 19536 struct attribute *type_attr;
c906108c 19537
e142c38c 19538 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19539 if (!type_attr)
19540 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19541 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19542
673bfd45 19543 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19544}
19545
ac9ec31b
DE
19546/* Return an error marker type to use for the ill formed type in DIE/CU. */
19547
19548static struct type *
19549build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19550{
19551 struct objfile *objfile = dwarf2_per_objfile->objfile;
19552 char *message, *saved;
19553
19554 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19555 objfile_name (objfile),
9c541725
PA
19556 to_underlying (cu->header.sect_off),
19557 to_underlying (die->sect_off));
224c3ddb
SM
19558 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19559 message, strlen (message));
ac9ec31b
DE
19560 xfree (message);
19561
19f392bc 19562 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19563}
19564
673bfd45 19565/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19566 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19567 DW_AT_containing_type.
673bfd45
DE
19568 If there is no type substitute an error marker. */
19569
c906108c 19570static struct type *
ff39bb5e 19571lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19572 struct dwarf2_cu *cu)
c906108c 19573{
bb5ed363 19574 struct objfile *objfile = cu->objfile;
f792889a
DJ
19575 struct type *this_type;
19576
ac9ec31b
DE
19577 gdb_assert (attr->name == DW_AT_type
19578 || attr->name == DW_AT_GNAT_descriptive_type
19579 || attr->name == DW_AT_containing_type);
19580
673bfd45
DE
19581 /* First see if we have it cached. */
19582
36586728
TT
19583 if (attr->form == DW_FORM_GNU_ref_alt)
19584 {
19585 struct dwarf2_per_cu_data *per_cu;
9c541725 19586 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 19587
9c541725
PA
19588 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, cu->objfile);
19589 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 19590 }
7771576e 19591 else if (attr_form_is_ref (attr))
673bfd45 19592 {
9c541725 19593 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 19594
9c541725 19595 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 19596 }
55f1336d 19597 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19598 {
ac9ec31b 19599 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19600
ac9ec31b 19601 return get_signatured_type (die, signature, cu);
673bfd45
DE
19602 }
19603 else
19604 {
ac9ec31b
DE
19605 complaint (&symfile_complaints,
19606 _("Dwarf Error: Bad type attribute %s in DIE"
19607 " at 0x%x [in module %s]"),
9c541725 19608 dwarf_attr_name (attr->name), to_underlying (die->sect_off),
4262abfb 19609 objfile_name (objfile));
ac9ec31b 19610 return build_error_marker_type (cu, die);
673bfd45
DE
19611 }
19612
19613 /* If not cached we need to read it in. */
19614
19615 if (this_type == NULL)
19616 {
ac9ec31b 19617 struct die_info *type_die = NULL;
673bfd45
DE
19618 struct dwarf2_cu *type_cu = cu;
19619
7771576e 19620 if (attr_form_is_ref (attr))
ac9ec31b
DE
19621 type_die = follow_die_ref (die, attr, &type_cu);
19622 if (type_die == NULL)
19623 return build_error_marker_type (cu, die);
19624 /* If we find the type now, it's probably because the type came
3019eac3
DE
19625 from an inter-CU reference and the type's CU got expanded before
19626 ours. */
ac9ec31b 19627 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19628 }
19629
19630 /* If we still don't have a type use an error marker. */
19631
19632 if (this_type == NULL)
ac9ec31b 19633 return build_error_marker_type (cu, die);
673bfd45 19634
f792889a 19635 return this_type;
c906108c
SS
19636}
19637
673bfd45
DE
19638/* Return the type in DIE, CU.
19639 Returns NULL for invalid types.
19640
02142a6c 19641 This first does a lookup in die_type_hash,
673bfd45
DE
19642 and only reads the die in if necessary.
19643
19644 NOTE: This can be called when reading in partial or full symbols. */
19645
f792889a 19646static struct type *
e7c27a73 19647read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19648{
f792889a
DJ
19649 struct type *this_type;
19650
19651 this_type = get_die_type (die, cu);
19652 if (this_type)
19653 return this_type;
19654
673bfd45
DE
19655 return read_type_die_1 (die, cu);
19656}
19657
19658/* Read the type in DIE, CU.
19659 Returns NULL for invalid types. */
19660
19661static struct type *
19662read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19663{
19664 struct type *this_type = NULL;
19665
c906108c
SS
19666 switch (die->tag)
19667 {
19668 case DW_TAG_class_type:
680b30c7 19669 case DW_TAG_interface_type:
c906108c
SS
19670 case DW_TAG_structure_type:
19671 case DW_TAG_union_type:
f792889a 19672 this_type = read_structure_type (die, cu);
c906108c
SS
19673 break;
19674 case DW_TAG_enumeration_type:
f792889a 19675 this_type = read_enumeration_type (die, cu);
c906108c
SS
19676 break;
19677 case DW_TAG_subprogram:
19678 case DW_TAG_subroutine_type:
edb3359d 19679 case DW_TAG_inlined_subroutine:
f792889a 19680 this_type = read_subroutine_type (die, cu);
c906108c
SS
19681 break;
19682 case DW_TAG_array_type:
f792889a 19683 this_type = read_array_type (die, cu);
c906108c 19684 break;
72019c9c 19685 case DW_TAG_set_type:
f792889a 19686 this_type = read_set_type (die, cu);
72019c9c 19687 break;
c906108c 19688 case DW_TAG_pointer_type:
f792889a 19689 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19690 break;
19691 case DW_TAG_ptr_to_member_type:
f792889a 19692 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19693 break;
19694 case DW_TAG_reference_type:
4297a3f0
AV
19695 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19696 break;
19697 case DW_TAG_rvalue_reference_type:
19698 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19699 break;
19700 case DW_TAG_const_type:
f792889a 19701 this_type = read_tag_const_type (die, cu);
c906108c
SS
19702 break;
19703 case DW_TAG_volatile_type:
f792889a 19704 this_type = read_tag_volatile_type (die, cu);
c906108c 19705 break;
06d66ee9
TT
19706 case DW_TAG_restrict_type:
19707 this_type = read_tag_restrict_type (die, cu);
19708 break;
c906108c 19709 case DW_TAG_string_type:
f792889a 19710 this_type = read_tag_string_type (die, cu);
c906108c
SS
19711 break;
19712 case DW_TAG_typedef:
f792889a 19713 this_type = read_typedef (die, cu);
c906108c 19714 break;
a02abb62 19715 case DW_TAG_subrange_type:
f792889a 19716 this_type = read_subrange_type (die, cu);
a02abb62 19717 break;
c906108c 19718 case DW_TAG_base_type:
f792889a 19719 this_type = read_base_type (die, cu);
c906108c 19720 break;
81a17f79 19721 case DW_TAG_unspecified_type:
f792889a 19722 this_type = read_unspecified_type (die, cu);
81a17f79 19723 break;
0114d602
DJ
19724 case DW_TAG_namespace:
19725 this_type = read_namespace_type (die, cu);
19726 break;
f55ee35c
JK
19727 case DW_TAG_module:
19728 this_type = read_module_type (die, cu);
19729 break;
a2c2acaf
MW
19730 case DW_TAG_atomic_type:
19731 this_type = read_tag_atomic_type (die, cu);
19732 break;
c906108c 19733 default:
3e43a32a
MS
19734 complaint (&symfile_complaints,
19735 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19736 dwarf_tag_name (die->tag));
c906108c
SS
19737 break;
19738 }
63d06c5c 19739
f792889a 19740 return this_type;
63d06c5c
DC
19741}
19742
abc72ce4
DE
19743/* See if we can figure out if the class lives in a namespace. We do
19744 this by looking for a member function; its demangled name will
19745 contain namespace info, if there is any.
19746 Return the computed name or NULL.
19747 Space for the result is allocated on the objfile's obstack.
19748 This is the full-die version of guess_partial_die_structure_name.
19749 In this case we know DIE has no useful parent. */
19750
19751static char *
19752guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19753{
19754 struct die_info *spec_die;
19755 struct dwarf2_cu *spec_cu;
19756 struct die_info *child;
19757
19758 spec_cu = cu;
19759 spec_die = die_specification (die, &spec_cu);
19760 if (spec_die != NULL)
19761 {
19762 die = spec_die;
19763 cu = spec_cu;
19764 }
19765
19766 for (child = die->child;
19767 child != NULL;
19768 child = child->sibling)
19769 {
19770 if (child->tag == DW_TAG_subprogram)
19771 {
7d45c7c3 19772 const char *linkage_name;
abc72ce4 19773
7d45c7c3
KB
19774 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19775 if (linkage_name == NULL)
19776 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19777 cu);
19778 if (linkage_name != NULL)
abc72ce4
DE
19779 {
19780 char *actual_name
19781 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19782 linkage_name);
abc72ce4
DE
19783 char *name = NULL;
19784
19785 if (actual_name != NULL)
19786 {
15d034d0 19787 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19788
19789 if (die_name != NULL
19790 && strcmp (die_name, actual_name) != 0)
19791 {
19792 /* Strip off the class name from the full name.
19793 We want the prefix. */
19794 int die_name_len = strlen (die_name);
19795 int actual_name_len = strlen (actual_name);
19796
19797 /* Test for '::' as a sanity check. */
19798 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19799 && actual_name[actual_name_len
19800 - die_name_len - 1] == ':')
224c3ddb
SM
19801 name = (char *) obstack_copy0 (
19802 &cu->objfile->per_bfd->storage_obstack,
19803 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19804 }
19805 }
19806 xfree (actual_name);
19807 return name;
19808 }
19809 }
19810 }
19811
19812 return NULL;
19813}
19814
96408a79
SA
19815/* GCC might emit a nameless typedef that has a linkage name. Determine the
19816 prefix part in such case. See
19817 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19818
a121b7c1 19819static const char *
96408a79
SA
19820anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19821{
19822 struct attribute *attr;
e6a959d6 19823 const char *base;
96408a79
SA
19824
19825 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19826 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19827 return NULL;
19828
7d45c7c3 19829 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19830 return NULL;
19831
19832 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19833 if (attr == NULL)
19834 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19835 if (attr == NULL || DW_STRING (attr) == NULL)
19836 return NULL;
19837
19838 /* dwarf2_name had to be already called. */
19839 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19840
19841 /* Strip the base name, keep any leading namespaces/classes. */
19842 base = strrchr (DW_STRING (attr), ':');
19843 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19844 return "";
19845
224c3ddb
SM
19846 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19847 DW_STRING (attr),
19848 &base[-1] - DW_STRING (attr));
96408a79
SA
19849}
19850
fdde2d81 19851/* Return the name of the namespace/class that DIE is defined within,
0114d602 19852 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19853
0114d602
DJ
19854 For example, if we're within the method foo() in the following
19855 code:
19856
19857 namespace N {
19858 class C {
19859 void foo () {
19860 }
19861 };
19862 }
19863
19864 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19865
0d5cff50 19866static const char *
e142c38c 19867determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19868{
0114d602
DJ
19869 struct die_info *parent, *spec_die;
19870 struct dwarf2_cu *spec_cu;
19871 struct type *parent_type;
a121b7c1 19872 const char *retval;
63d06c5c 19873
9c37b5ae 19874 if (cu->language != language_cplus
c44af4eb
TT
19875 && cu->language != language_fortran && cu->language != language_d
19876 && cu->language != language_rust)
0114d602
DJ
19877 return "";
19878
96408a79
SA
19879 retval = anonymous_struct_prefix (die, cu);
19880 if (retval)
19881 return retval;
19882
0114d602
DJ
19883 /* We have to be careful in the presence of DW_AT_specification.
19884 For example, with GCC 3.4, given the code
19885
19886 namespace N {
19887 void foo() {
19888 // Definition of N::foo.
19889 }
19890 }
19891
19892 then we'll have a tree of DIEs like this:
19893
19894 1: DW_TAG_compile_unit
19895 2: DW_TAG_namespace // N
19896 3: DW_TAG_subprogram // declaration of N::foo
19897 4: DW_TAG_subprogram // definition of N::foo
19898 DW_AT_specification // refers to die #3
19899
19900 Thus, when processing die #4, we have to pretend that we're in
19901 the context of its DW_AT_specification, namely the contex of die
19902 #3. */
19903 spec_cu = cu;
19904 spec_die = die_specification (die, &spec_cu);
19905 if (spec_die == NULL)
19906 parent = die->parent;
19907 else
63d06c5c 19908 {
0114d602
DJ
19909 parent = spec_die->parent;
19910 cu = spec_cu;
63d06c5c 19911 }
0114d602
DJ
19912
19913 if (parent == NULL)
19914 return "";
98bfdba5
PA
19915 else if (parent->building_fullname)
19916 {
19917 const char *name;
19918 const char *parent_name;
19919
19920 /* It has been seen on RealView 2.2 built binaries,
19921 DW_TAG_template_type_param types actually _defined_ as
19922 children of the parent class:
19923
19924 enum E {};
19925 template class <class Enum> Class{};
19926 Class<enum E> class_e;
19927
19928 1: DW_TAG_class_type (Class)
19929 2: DW_TAG_enumeration_type (E)
19930 3: DW_TAG_enumerator (enum1:0)
19931 3: DW_TAG_enumerator (enum2:1)
19932 ...
19933 2: DW_TAG_template_type_param
19934 DW_AT_type DW_FORM_ref_udata (E)
19935
19936 Besides being broken debug info, it can put GDB into an
19937 infinite loop. Consider:
19938
19939 When we're building the full name for Class<E>, we'll start
19940 at Class, and go look over its template type parameters,
19941 finding E. We'll then try to build the full name of E, and
19942 reach here. We're now trying to build the full name of E,
19943 and look over the parent DIE for containing scope. In the
19944 broken case, if we followed the parent DIE of E, we'd again
19945 find Class, and once again go look at its template type
19946 arguments, etc., etc. Simply don't consider such parent die
19947 as source-level parent of this die (it can't be, the language
19948 doesn't allow it), and break the loop here. */
19949 name = dwarf2_name (die, cu);
19950 parent_name = dwarf2_name (parent, cu);
19951 complaint (&symfile_complaints,
19952 _("template param type '%s' defined within parent '%s'"),
19953 name ? name : "<unknown>",
19954 parent_name ? parent_name : "<unknown>");
19955 return "";
19956 }
63d06c5c 19957 else
0114d602
DJ
19958 switch (parent->tag)
19959 {
63d06c5c 19960 case DW_TAG_namespace:
0114d602 19961 parent_type = read_type_die (parent, cu);
acebe513
UW
19962 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19963 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19964 Work around this problem here. */
19965 if (cu->language == language_cplus
19966 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19967 return "";
0114d602
DJ
19968 /* We give a name to even anonymous namespaces. */
19969 return TYPE_TAG_NAME (parent_type);
63d06c5c 19970 case DW_TAG_class_type:
680b30c7 19971 case DW_TAG_interface_type:
63d06c5c 19972 case DW_TAG_structure_type:
0114d602 19973 case DW_TAG_union_type:
f55ee35c 19974 case DW_TAG_module:
0114d602
DJ
19975 parent_type = read_type_die (parent, cu);
19976 if (TYPE_TAG_NAME (parent_type) != NULL)
19977 return TYPE_TAG_NAME (parent_type);
19978 else
19979 /* An anonymous structure is only allowed non-static data
19980 members; no typedefs, no member functions, et cetera.
19981 So it does not need a prefix. */
19982 return "";
abc72ce4 19983 case DW_TAG_compile_unit:
95554aad 19984 case DW_TAG_partial_unit:
abc72ce4
DE
19985 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19986 if (cu->language == language_cplus
8b70b953 19987 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19988 && die->child != NULL
19989 && (die->tag == DW_TAG_class_type
19990 || die->tag == DW_TAG_structure_type
19991 || die->tag == DW_TAG_union_type))
19992 {
19993 char *name = guess_full_die_structure_name (die, cu);
19994 if (name != NULL)
19995 return name;
19996 }
19997 return "";
3d567982
TT
19998 case DW_TAG_enumeration_type:
19999 parent_type = read_type_die (parent, cu);
20000 if (TYPE_DECLARED_CLASS (parent_type))
20001 {
20002 if (TYPE_TAG_NAME (parent_type) != NULL)
20003 return TYPE_TAG_NAME (parent_type);
20004 return "";
20005 }
20006 /* Fall through. */
63d06c5c 20007 default:
8176b9b8 20008 return determine_prefix (parent, cu);
63d06c5c 20009 }
63d06c5c
DC
20010}
20011
3e43a32a
MS
20012/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20013 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20014 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20015 an obconcat, otherwise allocate storage for the result. The CU argument is
20016 used to determine the language and hence, the appropriate separator. */
987504bb 20017
f55ee35c 20018#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
20019
20020static char *
f55ee35c
JK
20021typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20022 int physname, struct dwarf2_cu *cu)
63d06c5c 20023{
f55ee35c 20024 const char *lead = "";
5c315b68 20025 const char *sep;
63d06c5c 20026
3e43a32a
MS
20027 if (suffix == NULL || suffix[0] == '\0'
20028 || prefix == NULL || prefix[0] == '\0')
987504bb 20029 sep = "";
45280282
IB
20030 else if (cu->language == language_d)
20031 {
20032 /* For D, the 'main' function could be defined in any module, but it
20033 should never be prefixed. */
20034 if (strcmp (suffix, "D main") == 0)
20035 {
20036 prefix = "";
20037 sep = "";
20038 }
20039 else
20040 sep = ".";
20041 }
f55ee35c
JK
20042 else if (cu->language == language_fortran && physname)
20043 {
20044 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20045 DW_AT_MIPS_linkage_name is preferred and used instead. */
20046
20047 lead = "__";
20048 sep = "_MOD_";
20049 }
987504bb
JJ
20050 else
20051 sep = "::";
63d06c5c 20052
6dd47d34
DE
20053 if (prefix == NULL)
20054 prefix = "";
20055 if (suffix == NULL)
20056 suffix = "";
20057
987504bb
JJ
20058 if (obs == NULL)
20059 {
3e43a32a 20060 char *retval
224c3ddb
SM
20061 = ((char *)
20062 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20063
f55ee35c
JK
20064 strcpy (retval, lead);
20065 strcat (retval, prefix);
6dd47d34
DE
20066 strcat (retval, sep);
20067 strcat (retval, suffix);
63d06c5c
DC
20068 return retval;
20069 }
987504bb
JJ
20070 else
20071 {
20072 /* We have an obstack. */
f55ee35c 20073 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20074 }
63d06c5c
DC
20075}
20076
c906108c
SS
20077/* Return sibling of die, NULL if no sibling. */
20078
f9aca02d 20079static struct die_info *
fba45db2 20080sibling_die (struct die_info *die)
c906108c 20081{
639d11d3 20082 return die->sibling;
c906108c
SS
20083}
20084
71c25dea
TT
20085/* Get name of a die, return NULL if not found. */
20086
15d034d0
TT
20087static const char *
20088dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20089 struct obstack *obstack)
20090{
20091 if (name && cu->language == language_cplus)
20092 {
2f408ecb 20093 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20094
2f408ecb 20095 if (!canon_name.empty ())
71c25dea 20096 {
2f408ecb
PA
20097 if (canon_name != name)
20098 name = (const char *) obstack_copy0 (obstack,
20099 canon_name.c_str (),
20100 canon_name.length ());
71c25dea
TT
20101 }
20102 }
20103
20104 return name;
c906108c
SS
20105}
20106
96553a0c
DE
20107/* Get name of a die, return NULL if not found.
20108 Anonymous namespaces are converted to their magic string. */
9219021c 20109
15d034d0 20110static const char *
e142c38c 20111dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20112{
20113 struct attribute *attr;
20114
e142c38c 20115 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20116 if ((!attr || !DW_STRING (attr))
96553a0c 20117 && die->tag != DW_TAG_namespace
53832f31
TT
20118 && die->tag != DW_TAG_class_type
20119 && die->tag != DW_TAG_interface_type
20120 && die->tag != DW_TAG_structure_type
20121 && die->tag != DW_TAG_union_type)
71c25dea
TT
20122 return NULL;
20123
20124 switch (die->tag)
20125 {
20126 case DW_TAG_compile_unit:
95554aad 20127 case DW_TAG_partial_unit:
71c25dea
TT
20128 /* Compilation units have a DW_AT_name that is a filename, not
20129 a source language identifier. */
20130 case DW_TAG_enumeration_type:
20131 case DW_TAG_enumerator:
20132 /* These tags always have simple identifiers already; no need
20133 to canonicalize them. */
20134 return DW_STRING (attr);
907af001 20135
96553a0c
DE
20136 case DW_TAG_namespace:
20137 if (attr != NULL && DW_STRING (attr) != NULL)
20138 return DW_STRING (attr);
20139 return CP_ANONYMOUS_NAMESPACE_STR;
20140
907af001
UW
20141 case DW_TAG_class_type:
20142 case DW_TAG_interface_type:
20143 case DW_TAG_structure_type:
20144 case DW_TAG_union_type:
20145 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20146 structures or unions. These were of the form "._%d" in GCC 4.1,
20147 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20148 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20149 if (attr && DW_STRING (attr)
61012eef
GB
20150 && (startswith (DW_STRING (attr), "._")
20151 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20152 return NULL;
53832f31
TT
20153
20154 /* GCC might emit a nameless typedef that has a linkage name. See
20155 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20156 if (!attr || DW_STRING (attr) == NULL)
20157 {
df5c6c50 20158 char *demangled = NULL;
53832f31
TT
20159
20160 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20161 if (attr == NULL)
20162 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20163
20164 if (attr == NULL || DW_STRING (attr) == NULL)
20165 return NULL;
20166
df5c6c50
JK
20167 /* Avoid demangling DW_STRING (attr) the second time on a second
20168 call for the same DIE. */
20169 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20170 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20171
20172 if (demangled)
20173 {
e6a959d6 20174 const char *base;
96408a79 20175
53832f31 20176 /* FIXME: we already did this for the partial symbol... */
34a68019 20177 DW_STRING (attr)
224c3ddb
SM
20178 = ((const char *)
20179 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20180 demangled, strlen (demangled)));
53832f31
TT
20181 DW_STRING_IS_CANONICAL (attr) = 1;
20182 xfree (demangled);
96408a79
SA
20183
20184 /* Strip any leading namespaces/classes, keep only the base name.
20185 DW_AT_name for named DIEs does not contain the prefixes. */
20186 base = strrchr (DW_STRING (attr), ':');
20187 if (base && base > DW_STRING (attr) && base[-1] == ':')
20188 return &base[1];
20189 else
20190 return DW_STRING (attr);
53832f31
TT
20191 }
20192 }
907af001
UW
20193 break;
20194
71c25dea 20195 default:
907af001
UW
20196 break;
20197 }
20198
20199 if (!DW_STRING_IS_CANONICAL (attr))
20200 {
20201 DW_STRING (attr)
20202 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20203 &cu->objfile->per_bfd->storage_obstack);
907af001 20204 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20205 }
907af001 20206 return DW_STRING (attr);
9219021c
DC
20207}
20208
20209/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20210 is none. *EXT_CU is the CU containing DIE on input, and the CU
20211 containing the return value on output. */
9219021c
DC
20212
20213static struct die_info *
f2f0e013 20214dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20215{
20216 struct attribute *attr;
9219021c 20217
f2f0e013 20218 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20219 if (attr == NULL)
20220 return NULL;
20221
f2f0e013 20222 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20223}
20224
c906108c
SS
20225/* Convert a DIE tag into its string name. */
20226
f39c6ffd 20227static const char *
aa1ee363 20228dwarf_tag_name (unsigned tag)
c906108c 20229{
f39c6ffd
TT
20230 const char *name = get_DW_TAG_name (tag);
20231
20232 if (name == NULL)
20233 return "DW_TAG_<unknown>";
20234
20235 return name;
c906108c
SS
20236}
20237
20238/* Convert a DWARF attribute code into its string name. */
20239
f39c6ffd 20240static const char *
aa1ee363 20241dwarf_attr_name (unsigned attr)
c906108c 20242{
f39c6ffd
TT
20243 const char *name;
20244
c764a876 20245#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20246 if (attr == DW_AT_MIPS_fde)
20247 return "DW_AT_MIPS_fde";
20248#else
20249 if (attr == DW_AT_HP_block_index)
20250 return "DW_AT_HP_block_index";
c764a876 20251#endif
f39c6ffd
TT
20252
20253 name = get_DW_AT_name (attr);
20254
20255 if (name == NULL)
20256 return "DW_AT_<unknown>";
20257
20258 return name;
c906108c
SS
20259}
20260
20261/* Convert a DWARF value form code into its string name. */
20262
f39c6ffd 20263static const char *
aa1ee363 20264dwarf_form_name (unsigned form)
c906108c 20265{
f39c6ffd
TT
20266 const char *name = get_DW_FORM_name (form);
20267
20268 if (name == NULL)
20269 return "DW_FORM_<unknown>";
20270
20271 return name;
c906108c
SS
20272}
20273
a121b7c1 20274static const char *
fba45db2 20275dwarf_bool_name (unsigned mybool)
c906108c
SS
20276{
20277 if (mybool)
20278 return "TRUE";
20279 else
20280 return "FALSE";
20281}
20282
20283/* Convert a DWARF type code into its string name. */
20284
f39c6ffd 20285static const char *
aa1ee363 20286dwarf_type_encoding_name (unsigned enc)
c906108c 20287{
f39c6ffd 20288 const char *name = get_DW_ATE_name (enc);
c906108c 20289
f39c6ffd
TT
20290 if (name == NULL)
20291 return "DW_ATE_<unknown>";
c906108c 20292
f39c6ffd 20293 return name;
c906108c 20294}
c906108c 20295
f9aca02d 20296static void
d97bc12b 20297dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20298{
20299 unsigned int i;
20300
d97bc12b
DE
20301 print_spaces (indent, f);
20302 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
9c541725
PA
20303 dwarf_tag_name (die->tag), die->abbrev,
20304 to_underlying (die->sect_off));
d97bc12b
DE
20305
20306 if (die->parent != NULL)
20307 {
20308 print_spaces (indent, f);
20309 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9c541725 20310 to_underlying (die->parent->sect_off));
d97bc12b
DE
20311 }
20312
20313 print_spaces (indent, f);
20314 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20315 dwarf_bool_name (die->child != NULL));
c906108c 20316
d97bc12b
DE
20317 print_spaces (indent, f);
20318 fprintf_unfiltered (f, " attributes:\n");
20319
c906108c
SS
20320 for (i = 0; i < die->num_attrs; ++i)
20321 {
d97bc12b
DE
20322 print_spaces (indent, f);
20323 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20324 dwarf_attr_name (die->attrs[i].name),
20325 dwarf_form_name (die->attrs[i].form));
d97bc12b 20326
c906108c
SS
20327 switch (die->attrs[i].form)
20328 {
c906108c 20329 case DW_FORM_addr:
3019eac3 20330 case DW_FORM_GNU_addr_index:
d97bc12b 20331 fprintf_unfiltered (f, "address: ");
5af949e3 20332 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20333 break;
20334 case DW_FORM_block2:
20335 case DW_FORM_block4:
20336 case DW_FORM_block:
20337 case DW_FORM_block1:
56eb65bd
SP
20338 fprintf_unfiltered (f, "block: size %s",
20339 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20340 break;
2dc7f7b3 20341 case DW_FORM_exprloc:
56eb65bd
SP
20342 fprintf_unfiltered (f, "expression: size %s",
20343 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20344 break;
0224619f
JK
20345 case DW_FORM_data16:
20346 fprintf_unfiltered (f, "constant of 16 bytes");
20347 break;
4568ecf9
DE
20348 case DW_FORM_ref_addr:
20349 fprintf_unfiltered (f, "ref address: ");
20350 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20351 break;
36586728
TT
20352 case DW_FORM_GNU_ref_alt:
20353 fprintf_unfiltered (f, "alt ref address: ");
20354 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20355 break;
10b3939b
DJ
20356 case DW_FORM_ref1:
20357 case DW_FORM_ref2:
20358 case DW_FORM_ref4:
4568ecf9
DE
20359 case DW_FORM_ref8:
20360 case DW_FORM_ref_udata:
d97bc12b 20361 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20362 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20363 break;
c906108c
SS
20364 case DW_FORM_data1:
20365 case DW_FORM_data2:
20366 case DW_FORM_data4:
ce5d95e1 20367 case DW_FORM_data8:
c906108c
SS
20368 case DW_FORM_udata:
20369 case DW_FORM_sdata:
43bbcdc2
PH
20370 fprintf_unfiltered (f, "constant: %s",
20371 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20372 break;
2dc7f7b3
TT
20373 case DW_FORM_sec_offset:
20374 fprintf_unfiltered (f, "section offset: %s",
20375 pulongest (DW_UNSND (&die->attrs[i])));
20376 break;
55f1336d 20377 case DW_FORM_ref_sig8:
ac9ec31b
DE
20378 fprintf_unfiltered (f, "signature: %s",
20379 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20380 break;
c906108c 20381 case DW_FORM_string:
4bdf3d34 20382 case DW_FORM_strp:
43988095 20383 case DW_FORM_line_strp:
3019eac3 20384 case DW_FORM_GNU_str_index:
36586728 20385 case DW_FORM_GNU_strp_alt:
8285870a 20386 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20387 DW_STRING (&die->attrs[i])
8285870a
JK
20388 ? DW_STRING (&die->attrs[i]) : "",
20389 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20390 break;
20391 case DW_FORM_flag:
20392 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20393 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20394 else
d97bc12b 20395 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20396 break;
2dc7f7b3
TT
20397 case DW_FORM_flag_present:
20398 fprintf_unfiltered (f, "flag: TRUE");
20399 break;
a8329558 20400 case DW_FORM_indirect:
0963b4bd
MS
20401 /* The reader will have reduced the indirect form to
20402 the "base form" so this form should not occur. */
3e43a32a
MS
20403 fprintf_unfiltered (f,
20404 "unexpected attribute form: DW_FORM_indirect");
a8329558 20405 break;
c906108c 20406 default:
d97bc12b 20407 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20408 die->attrs[i].form);
d97bc12b 20409 break;
c906108c 20410 }
d97bc12b 20411 fprintf_unfiltered (f, "\n");
c906108c
SS
20412 }
20413}
20414
f9aca02d 20415static void
d97bc12b 20416dump_die_for_error (struct die_info *die)
c906108c 20417{
d97bc12b
DE
20418 dump_die_shallow (gdb_stderr, 0, die);
20419}
20420
20421static void
20422dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20423{
20424 int indent = level * 4;
20425
20426 gdb_assert (die != NULL);
20427
20428 if (level >= max_level)
20429 return;
20430
20431 dump_die_shallow (f, indent, die);
20432
20433 if (die->child != NULL)
c906108c 20434 {
d97bc12b
DE
20435 print_spaces (indent, f);
20436 fprintf_unfiltered (f, " Children:");
20437 if (level + 1 < max_level)
20438 {
20439 fprintf_unfiltered (f, "\n");
20440 dump_die_1 (f, level + 1, max_level, die->child);
20441 }
20442 else
20443 {
3e43a32a
MS
20444 fprintf_unfiltered (f,
20445 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20446 }
20447 }
20448
20449 if (die->sibling != NULL && level > 0)
20450 {
20451 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20452 }
20453}
20454
d97bc12b
DE
20455/* This is called from the pdie macro in gdbinit.in.
20456 It's not static so gcc will keep a copy callable from gdb. */
20457
20458void
20459dump_die (struct die_info *die, int max_level)
20460{
20461 dump_die_1 (gdb_stdlog, 0, max_level, die);
20462}
20463
f9aca02d 20464static void
51545339 20465store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20466{
51545339 20467 void **slot;
c906108c 20468
9c541725
PA
20469 slot = htab_find_slot_with_hash (cu->die_hash, die,
20470 to_underlying (die->sect_off),
b64f50a1 20471 INSERT);
51545339
DJ
20472
20473 *slot = die;
c906108c
SS
20474}
20475
b64f50a1
JK
20476/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20477 required kind. */
20478
20479static sect_offset
ff39bb5e 20480dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20481{
7771576e 20482 if (attr_form_is_ref (attr))
9c541725 20483 return (sect_offset) DW_UNSND (attr);
93311388
DE
20484
20485 complaint (&symfile_complaints,
20486 _("unsupported die ref attribute form: '%s'"),
20487 dwarf_form_name (attr->form));
9c541725 20488 return {};
c906108c
SS
20489}
20490
43bbcdc2
PH
20491/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20492 * the value held by the attribute is not constant. */
a02abb62 20493
43bbcdc2 20494static LONGEST
ff39bb5e 20495dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20496{
20497 if (attr->form == DW_FORM_sdata)
20498 return DW_SND (attr);
20499 else if (attr->form == DW_FORM_udata
20500 || attr->form == DW_FORM_data1
20501 || attr->form == DW_FORM_data2
20502 || attr->form == DW_FORM_data4
20503 || attr->form == DW_FORM_data8)
20504 return DW_UNSND (attr);
20505 else
20506 {
0224619f 20507 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20508 complaint (&symfile_complaints,
20509 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20510 dwarf_form_name (attr->form));
20511 return default_value;
20512 }
20513}
20514
348e048f
DE
20515/* Follow reference or signature attribute ATTR of SRC_DIE.
20516 On entry *REF_CU is the CU of SRC_DIE.
20517 On exit *REF_CU is the CU of the result. */
20518
20519static struct die_info *
ff39bb5e 20520follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20521 struct dwarf2_cu **ref_cu)
20522{
20523 struct die_info *die;
20524
7771576e 20525 if (attr_form_is_ref (attr))
348e048f 20526 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20527 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20528 die = follow_die_sig (src_die, attr, ref_cu);
20529 else
20530 {
20531 dump_die_for_error (src_die);
20532 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20533 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20534 }
20535
20536 return die;
03dd20cc
DJ
20537}
20538
5c631832 20539/* Follow reference OFFSET.
673bfd45
DE
20540 On entry *REF_CU is the CU of the source die referencing OFFSET.
20541 On exit *REF_CU is the CU of the result.
20542 Returns NULL if OFFSET is invalid. */
f504f079 20543
f9aca02d 20544static struct die_info *
9c541725 20545follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 20546 struct dwarf2_cu **ref_cu)
c906108c 20547{
10b3939b 20548 struct die_info temp_die;
f2f0e013 20549 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20550
348e048f
DE
20551 gdb_assert (cu->per_cu != NULL);
20552
98bfdba5
PA
20553 target_cu = cu;
20554
3019eac3 20555 if (cu->per_cu->is_debug_types)
348e048f
DE
20556 {
20557 /* .debug_types CUs cannot reference anything outside their CU.
20558 If they need to, they have to reference a signatured type via
55f1336d 20559 DW_FORM_ref_sig8. */
9c541725 20560 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 20561 return NULL;
348e048f 20562 }
36586728 20563 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 20564 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
20565 {
20566 struct dwarf2_per_cu_data *per_cu;
9a619af0 20567
9c541725 20568 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
36586728 20569 cu->objfile);
03dd20cc
DJ
20570
20571 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20572 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20573 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20574
10b3939b
DJ
20575 target_cu = per_cu->cu;
20576 }
98bfdba5
PA
20577 else if (cu->dies == NULL)
20578 {
20579 /* We're loading full DIEs during partial symbol reading. */
20580 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20581 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20582 }
c906108c 20583
f2f0e013 20584 *ref_cu = target_cu;
9c541725 20585 temp_die.sect_off = sect_off;
9a3c8263 20586 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
20587 &temp_die,
20588 to_underlying (sect_off));
5c631832 20589}
10b3939b 20590
5c631832
JK
20591/* Follow reference attribute ATTR of SRC_DIE.
20592 On entry *REF_CU is the CU of SRC_DIE.
20593 On exit *REF_CU is the CU of the result. */
20594
20595static struct die_info *
ff39bb5e 20596follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20597 struct dwarf2_cu **ref_cu)
20598{
9c541725 20599 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20600 struct dwarf2_cu *cu = *ref_cu;
20601 struct die_info *die;
20602
9c541725 20603 die = follow_die_offset (sect_off,
36586728
TT
20604 (attr->form == DW_FORM_GNU_ref_alt
20605 || cu->per_cu->is_dwz),
20606 ref_cu);
5c631832
JK
20607 if (!die)
20608 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20609 "at 0x%x [in module %s]"),
9c541725 20610 to_underlying (sect_off), to_underlying (src_die->sect_off),
4262abfb 20611 objfile_name (cu->objfile));
348e048f 20612
5c631832
JK
20613 return die;
20614}
20615
9c541725 20616/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b
JK
20617 Returned value is intended for DW_OP_call*. Returned
20618 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20619
20620struct dwarf2_locexpr_baton
9c541725 20621dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
20622 struct dwarf2_per_cu_data *per_cu,
20623 CORE_ADDR (*get_frame_pc) (void *baton),
20624 void *baton)
5c631832 20625{
918dd910 20626 struct dwarf2_cu *cu;
5c631832
JK
20627 struct die_info *die;
20628 struct attribute *attr;
20629 struct dwarf2_locexpr_baton retval;
20630
8cf6f0b1
TT
20631 dw2_setup (per_cu->objfile);
20632
918dd910
JK
20633 if (per_cu->cu == NULL)
20634 load_cu (per_cu);
20635 cu = per_cu->cu;
cc12ce38
DE
20636 if (cu == NULL)
20637 {
20638 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20639 Instead just throw an error, not much else we can do. */
20640 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20641 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20642 }
918dd910 20643
9c541725 20644 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832
JK
20645 if (!die)
20646 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20647 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20648
20649 attr = dwarf2_attr (die, DW_AT_location, cu);
20650 if (!attr)
20651 {
e103e986
JK
20652 /* DWARF: "If there is no such attribute, then there is no effect.".
20653 DATA is ignored if SIZE is 0. */
5c631832 20654
e103e986 20655 retval.data = NULL;
5c631832
JK
20656 retval.size = 0;
20657 }
8cf6f0b1
TT
20658 else if (attr_form_is_section_offset (attr))
20659 {
20660 struct dwarf2_loclist_baton loclist_baton;
20661 CORE_ADDR pc = (*get_frame_pc) (baton);
20662 size_t size;
20663
20664 fill_in_loclist_baton (cu, &loclist_baton, attr);
20665
20666 retval.data = dwarf2_find_location_expression (&loclist_baton,
20667 &size, pc);
20668 retval.size = size;
20669 }
5c631832
JK
20670 else
20671 {
20672 if (!attr_form_is_block (attr))
20673 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20674 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9c541725 20675 to_underlying (sect_off), objfile_name (per_cu->objfile));
5c631832
JK
20676
20677 retval.data = DW_BLOCK (attr)->data;
20678 retval.size = DW_BLOCK (attr)->size;
20679 }
20680 retval.per_cu = cu->per_cu;
918dd910 20681
918dd910
JK
20682 age_cached_comp_units ();
20683
5c631832 20684 return retval;
348e048f
DE
20685}
20686
8b9737bf
TT
20687/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20688 offset. */
20689
20690struct dwarf2_locexpr_baton
20691dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20692 struct dwarf2_per_cu_data *per_cu,
20693 CORE_ADDR (*get_frame_pc) (void *baton),
20694 void *baton)
20695{
9c541725 20696 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 20697
9c541725 20698 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
20699}
20700
b6807d98
TT
20701/* Write a constant of a given type as target-ordered bytes into
20702 OBSTACK. */
20703
20704static const gdb_byte *
20705write_constant_as_bytes (struct obstack *obstack,
20706 enum bfd_endian byte_order,
20707 struct type *type,
20708 ULONGEST value,
20709 LONGEST *len)
20710{
20711 gdb_byte *result;
20712
20713 *len = TYPE_LENGTH (type);
224c3ddb 20714 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20715 store_unsigned_integer (result, *len, byte_order, value);
20716
20717 return result;
20718}
20719
20720/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20721 pointer to the constant bytes and set LEN to the length of the
20722 data. If memory is needed, allocate it on OBSTACK. If the DIE
20723 does not have a DW_AT_const_value, return NULL. */
20724
20725const gdb_byte *
9c541725 20726dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
20727 struct dwarf2_per_cu_data *per_cu,
20728 struct obstack *obstack,
20729 LONGEST *len)
20730{
20731 struct dwarf2_cu *cu;
20732 struct die_info *die;
20733 struct attribute *attr;
20734 const gdb_byte *result = NULL;
20735 struct type *type;
20736 LONGEST value;
20737 enum bfd_endian byte_order;
20738
20739 dw2_setup (per_cu->objfile);
20740
20741 if (per_cu->cu == NULL)
20742 load_cu (per_cu);
20743 cu = per_cu->cu;
cc12ce38
DE
20744 if (cu == NULL)
20745 {
20746 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20747 Instead just throw an error, not much else we can do. */
20748 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
9c541725 20749 to_underlying (sect_off), objfile_name (per_cu->objfile));
cc12ce38 20750 }
b6807d98 20751
9c541725 20752 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98
TT
20753 if (!die)
20754 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
9c541725 20755 to_underlying (sect_off), objfile_name (per_cu->objfile));
b6807d98
TT
20756
20757
20758 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20759 if (attr == NULL)
20760 return NULL;
20761
20762 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20763 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20764
20765 switch (attr->form)
20766 {
20767 case DW_FORM_addr:
20768 case DW_FORM_GNU_addr_index:
20769 {
20770 gdb_byte *tem;
20771
20772 *len = cu->header.addr_size;
224c3ddb 20773 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20774 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20775 result = tem;
20776 }
20777 break;
20778 case DW_FORM_string:
20779 case DW_FORM_strp:
20780 case DW_FORM_GNU_str_index:
20781 case DW_FORM_GNU_strp_alt:
20782 /* DW_STRING is already allocated on the objfile obstack, point
20783 directly to it. */
20784 result = (const gdb_byte *) DW_STRING (attr);
20785 *len = strlen (DW_STRING (attr));
20786 break;
20787 case DW_FORM_block1:
20788 case DW_FORM_block2:
20789 case DW_FORM_block4:
20790 case DW_FORM_block:
20791 case DW_FORM_exprloc:
0224619f 20792 case DW_FORM_data16:
b6807d98
TT
20793 result = DW_BLOCK (attr)->data;
20794 *len = DW_BLOCK (attr)->size;
20795 break;
20796
20797 /* The DW_AT_const_value attributes are supposed to carry the
20798 symbol's value "represented as it would be on the target
20799 architecture." By the time we get here, it's already been
20800 converted to host endianness, so we just need to sign- or
20801 zero-extend it as appropriate. */
20802 case DW_FORM_data1:
20803 type = die_type (die, cu);
20804 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20805 if (result == NULL)
20806 result = write_constant_as_bytes (obstack, byte_order,
20807 type, value, len);
20808 break;
20809 case DW_FORM_data2:
20810 type = die_type (die, cu);
20811 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20812 if (result == NULL)
20813 result = write_constant_as_bytes (obstack, byte_order,
20814 type, value, len);
20815 break;
20816 case DW_FORM_data4:
20817 type = die_type (die, cu);
20818 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20819 if (result == NULL)
20820 result = write_constant_as_bytes (obstack, byte_order,
20821 type, value, len);
20822 break;
20823 case DW_FORM_data8:
20824 type = die_type (die, cu);
20825 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20826 if (result == NULL)
20827 result = write_constant_as_bytes (obstack, byte_order,
20828 type, value, len);
20829 break;
20830
20831 case DW_FORM_sdata:
20832 type = die_type (die, cu);
20833 result = write_constant_as_bytes (obstack, byte_order,
20834 type, DW_SND (attr), len);
20835 break;
20836
20837 case DW_FORM_udata:
20838 type = die_type (die, cu);
20839 result = write_constant_as_bytes (obstack, byte_order,
20840 type, DW_UNSND (attr), len);
20841 break;
20842
20843 default:
20844 complaint (&symfile_complaints,
20845 _("unsupported const value attribute form: '%s'"),
20846 dwarf_form_name (attr->form));
20847 break;
20848 }
20849
20850 return result;
20851}
20852
7942e96e
AA
20853/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20854 valid type for this die is found. */
20855
20856struct type *
9c541725 20857dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
20858 struct dwarf2_per_cu_data *per_cu)
20859{
20860 struct dwarf2_cu *cu;
20861 struct die_info *die;
20862
20863 dw2_setup (per_cu->objfile);
20864
20865 if (per_cu->cu == NULL)
20866 load_cu (per_cu);
20867 cu = per_cu->cu;
20868 if (!cu)
20869 return NULL;
20870
9c541725 20871 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
20872 if (!die)
20873 return NULL;
20874
20875 return die_type (die, cu);
20876}
20877
8a9b8146
TT
20878/* Return the type of the DIE at DIE_OFFSET in the CU named by
20879 PER_CU. */
20880
20881struct type *
b64f50a1 20882dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20883 struct dwarf2_per_cu_data *per_cu)
20884{
8a9b8146 20885 dw2_setup (per_cu->objfile);
b64f50a1 20886
9c541725 20887 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 20888 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20889}
20890
ac9ec31b 20891/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20892 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20893 On exit *REF_CU is the CU of the result.
20894 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20895
20896static struct die_info *
ac9ec31b
DE
20897follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20898 struct dwarf2_cu **ref_cu)
348e048f 20899{
348e048f 20900 struct die_info temp_die;
348e048f
DE
20901 struct dwarf2_cu *sig_cu;
20902 struct die_info *die;
20903
ac9ec31b
DE
20904 /* While it might be nice to assert sig_type->type == NULL here,
20905 we can get here for DW_AT_imported_declaration where we need
20906 the DIE not the type. */
348e048f
DE
20907
20908 /* If necessary, add it to the queue and load its DIEs. */
20909
95554aad 20910 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20911 read_signatured_type (sig_type);
348e048f 20912
348e048f 20913 sig_cu = sig_type->per_cu.cu;
69d751e3 20914 gdb_assert (sig_cu != NULL);
9c541725
PA
20915 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20916 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 20917 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 20918 to_underlying (temp_die.sect_off));
348e048f
DE
20919 if (die)
20920 {
796a7ff8
DE
20921 /* For .gdb_index version 7 keep track of included TUs.
20922 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20923 if (dwarf2_per_objfile->index_table != NULL
20924 && dwarf2_per_objfile->index_table->version <= 7)
20925 {
20926 VEC_safe_push (dwarf2_per_cu_ptr,
20927 (*ref_cu)->per_cu->imported_symtabs,
20928 sig_cu->per_cu);
20929 }
20930
348e048f
DE
20931 *ref_cu = sig_cu;
20932 return die;
20933 }
20934
ac9ec31b
DE
20935 return NULL;
20936}
20937
20938/* Follow signatured type referenced by ATTR in SRC_DIE.
20939 On entry *REF_CU is the CU of SRC_DIE.
20940 On exit *REF_CU is the CU of the result.
20941 The result is the DIE of the type.
20942 If the referenced type cannot be found an error is thrown. */
20943
20944static struct die_info *
ff39bb5e 20945follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20946 struct dwarf2_cu **ref_cu)
20947{
20948 ULONGEST signature = DW_SIGNATURE (attr);
20949 struct signatured_type *sig_type;
20950 struct die_info *die;
20951
20952 gdb_assert (attr->form == DW_FORM_ref_sig8);
20953
a2ce51a0 20954 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20955 /* sig_type will be NULL if the signatured type is missing from
20956 the debug info. */
20957 if (sig_type == NULL)
20958 {
20959 error (_("Dwarf Error: Cannot find 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 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20966 if (die == NULL)
20967 {
20968 dump_die_for_error (src_die);
20969 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20970 " from DIE at 0x%x [in module %s]"),
9c541725 20971 hex_string (signature), to_underlying (src_die->sect_off),
4262abfb 20972 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20973 }
20974
20975 return die;
20976}
20977
20978/* Get the type specified by SIGNATURE referenced in DIE/CU,
20979 reading in and processing the type unit if necessary. */
20980
20981static struct type *
20982get_signatured_type (struct die_info *die, ULONGEST signature,
20983 struct dwarf2_cu *cu)
20984{
20985 struct signatured_type *sig_type;
20986 struct dwarf2_cu *type_cu;
20987 struct die_info *type_die;
20988 struct type *type;
20989
a2ce51a0 20990 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20991 /* sig_type will be NULL if the signatured type is missing from
20992 the debug info. */
20993 if (sig_type == NULL)
20994 {
20995 complaint (&symfile_complaints,
20996 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20997 " from DIE at 0x%x [in module %s]"),
9c541725 20998 hex_string (signature), to_underlying (die->sect_off),
4262abfb 20999 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21000 return build_error_marker_type (cu, die);
21001 }
21002
21003 /* If we already know the type we're done. */
21004 if (sig_type->type != NULL)
21005 return sig_type->type;
21006
21007 type_cu = cu;
21008 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
21009 if (type_die != NULL)
21010 {
21011 /* N.B. We need to call get_die_type to ensure only one type for this DIE
21012 is created. This is important, for example, because for c++ classes
21013 we need TYPE_NAME set which is only done by new_symbol. Blech. */
21014 type = read_type_die (type_die, type_cu);
21015 if (type == NULL)
21016 {
21017 complaint (&symfile_complaints,
21018 _("Dwarf Error: Cannot build signatured type %s"
21019 " referenced 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 }
21025 else
21026 {
21027 complaint (&symfile_complaints,
21028 _("Dwarf Error: Problem reading signatured DIE %s referenced"
21029 " from DIE at 0x%x [in module %s]"),
9c541725 21030 hex_string (signature), to_underlying (die->sect_off),
4262abfb 21031 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21032 type = build_error_marker_type (cu, die);
21033 }
21034 sig_type->type = type;
21035
21036 return type;
21037}
21038
21039/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21040 reading in and processing the type unit if necessary. */
21041
21042static struct type *
ff39bb5e 21043get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 21044 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
21045{
21046 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 21047 if (attr_form_is_ref (attr))
ac9ec31b
DE
21048 {
21049 struct dwarf2_cu *type_cu = cu;
21050 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21051
21052 return read_type_die (type_die, type_cu);
21053 }
21054 else if (attr->form == DW_FORM_ref_sig8)
21055 {
21056 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21057 }
21058 else
21059 {
21060 complaint (&symfile_complaints,
21061 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21062 " at 0x%x [in module %s]"),
9c541725 21063 dwarf_form_name (attr->form), to_underlying (die->sect_off),
4262abfb 21064 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21065 return build_error_marker_type (cu, die);
21066 }
348e048f
DE
21067}
21068
e5fe5e75 21069/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21070
21071static void
e5fe5e75 21072load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21073{
52dc124a 21074 struct signatured_type *sig_type;
348e048f 21075
f4dc4d17
DE
21076 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21077 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21078
6721b2ec
DE
21079 /* We have the per_cu, but we need the signatured_type.
21080 Fortunately this is an easy translation. */
21081 gdb_assert (per_cu->is_debug_types);
21082 sig_type = (struct signatured_type *) per_cu;
348e048f 21083
6721b2ec 21084 gdb_assert (per_cu->cu == NULL);
348e048f 21085
52dc124a 21086 read_signatured_type (sig_type);
348e048f 21087
6721b2ec 21088 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21089}
21090
dee91e82
DE
21091/* die_reader_func for read_signatured_type.
21092 This is identical to load_full_comp_unit_reader,
21093 but is kept separate for now. */
348e048f
DE
21094
21095static void
dee91e82 21096read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21097 const gdb_byte *info_ptr,
dee91e82
DE
21098 struct die_info *comp_unit_die,
21099 int has_children,
21100 void *data)
348e048f 21101{
dee91e82 21102 struct dwarf2_cu *cu = reader->cu;
348e048f 21103
dee91e82
DE
21104 gdb_assert (cu->die_hash == NULL);
21105 cu->die_hash =
21106 htab_create_alloc_ex (cu->header.length / 12,
21107 die_hash,
21108 die_eq,
21109 NULL,
21110 &cu->comp_unit_obstack,
21111 hashtab_obstack_allocate,
21112 dummy_obstack_deallocate);
348e048f 21113
dee91e82
DE
21114 if (has_children)
21115 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21116 &info_ptr, comp_unit_die);
21117 cu->dies = comp_unit_die;
21118 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21119
21120 /* We try not to read any attributes in this function, because not
9cdd5dbd 21121 all CUs needed for references have been loaded yet, and symbol
348e048f 21122 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21123 or we won't be able to build types correctly.
21124 Similarly, if we do not read the producer, we can not apply
21125 producer-specific interpretation. */
95554aad 21126 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21127}
348e048f 21128
3019eac3
DE
21129/* Read in a signatured type and build its CU and DIEs.
21130 If the type is a stub for the real type in a DWO file,
21131 read in the real type from the DWO file as well. */
dee91e82
DE
21132
21133static void
21134read_signatured_type (struct signatured_type *sig_type)
21135{
21136 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21137
3019eac3 21138 gdb_assert (per_cu->is_debug_types);
dee91e82 21139 gdb_assert (per_cu->cu == NULL);
348e048f 21140
f4dc4d17
DE
21141 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21142 read_signatured_type_reader, NULL);
7ee85ab1 21143 sig_type->per_cu.tu_read = 1;
c906108c
SS
21144}
21145
c906108c
SS
21146/* Decode simple location descriptions.
21147 Given a pointer to a dwarf block that defines a location, compute
21148 the location and return the value.
21149
4cecd739
DJ
21150 NOTE drow/2003-11-18: This function is called in two situations
21151 now: for the address of static or global variables (partial symbols
21152 only) and for offsets into structures which are expected to be
21153 (more or less) constant. The partial symbol case should go away,
21154 and only the constant case should remain. That will let this
21155 function complain more accurately. A few special modes are allowed
21156 without complaint for global variables (for instance, global
21157 register values and thread-local values).
c906108c
SS
21158
21159 A location description containing no operations indicates that the
4cecd739 21160 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21161 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21162 callers will only want a very basic result and this can become a
21ae7a4d
JK
21163 complaint.
21164
21165 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21166
21167static CORE_ADDR
e7c27a73 21168decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21169{
e7c27a73 21170 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21171 size_t i;
21172 size_t size = blk->size;
d521ce57 21173 const gdb_byte *data = blk->data;
21ae7a4d
JK
21174 CORE_ADDR stack[64];
21175 int stacki;
21176 unsigned int bytes_read, unsnd;
21177 gdb_byte op;
c906108c 21178
21ae7a4d
JK
21179 i = 0;
21180 stacki = 0;
21181 stack[stacki] = 0;
21182 stack[++stacki] = 0;
21183
21184 while (i < size)
21185 {
21186 op = data[i++];
21187 switch (op)
21188 {
21189 case DW_OP_lit0:
21190 case DW_OP_lit1:
21191 case DW_OP_lit2:
21192 case DW_OP_lit3:
21193 case DW_OP_lit4:
21194 case DW_OP_lit5:
21195 case DW_OP_lit6:
21196 case DW_OP_lit7:
21197 case DW_OP_lit8:
21198 case DW_OP_lit9:
21199 case DW_OP_lit10:
21200 case DW_OP_lit11:
21201 case DW_OP_lit12:
21202 case DW_OP_lit13:
21203 case DW_OP_lit14:
21204 case DW_OP_lit15:
21205 case DW_OP_lit16:
21206 case DW_OP_lit17:
21207 case DW_OP_lit18:
21208 case DW_OP_lit19:
21209 case DW_OP_lit20:
21210 case DW_OP_lit21:
21211 case DW_OP_lit22:
21212 case DW_OP_lit23:
21213 case DW_OP_lit24:
21214 case DW_OP_lit25:
21215 case DW_OP_lit26:
21216 case DW_OP_lit27:
21217 case DW_OP_lit28:
21218 case DW_OP_lit29:
21219 case DW_OP_lit30:
21220 case DW_OP_lit31:
21221 stack[++stacki] = op - DW_OP_lit0;
21222 break;
f1bea926 21223
21ae7a4d
JK
21224 case DW_OP_reg0:
21225 case DW_OP_reg1:
21226 case DW_OP_reg2:
21227 case DW_OP_reg3:
21228 case DW_OP_reg4:
21229 case DW_OP_reg5:
21230 case DW_OP_reg6:
21231 case DW_OP_reg7:
21232 case DW_OP_reg8:
21233 case DW_OP_reg9:
21234 case DW_OP_reg10:
21235 case DW_OP_reg11:
21236 case DW_OP_reg12:
21237 case DW_OP_reg13:
21238 case DW_OP_reg14:
21239 case DW_OP_reg15:
21240 case DW_OP_reg16:
21241 case DW_OP_reg17:
21242 case DW_OP_reg18:
21243 case DW_OP_reg19:
21244 case DW_OP_reg20:
21245 case DW_OP_reg21:
21246 case DW_OP_reg22:
21247 case DW_OP_reg23:
21248 case DW_OP_reg24:
21249 case DW_OP_reg25:
21250 case DW_OP_reg26:
21251 case DW_OP_reg27:
21252 case DW_OP_reg28:
21253 case DW_OP_reg29:
21254 case DW_OP_reg30:
21255 case DW_OP_reg31:
21256 stack[++stacki] = op - DW_OP_reg0;
21257 if (i < size)
21258 dwarf2_complex_location_expr_complaint ();
21259 break;
c906108c 21260
21ae7a4d
JK
21261 case DW_OP_regx:
21262 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21263 i += bytes_read;
21264 stack[++stacki] = unsnd;
21265 if (i < size)
21266 dwarf2_complex_location_expr_complaint ();
21267 break;
c906108c 21268
21ae7a4d
JK
21269 case DW_OP_addr:
21270 stack[++stacki] = read_address (objfile->obfd, &data[i],
21271 cu, &bytes_read);
21272 i += bytes_read;
21273 break;
d53d4ac5 21274
21ae7a4d
JK
21275 case DW_OP_const1u:
21276 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21277 i += 1;
21278 break;
21279
21280 case DW_OP_const1s:
21281 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21282 i += 1;
21283 break;
21284
21285 case DW_OP_const2u:
21286 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21287 i += 2;
21288 break;
21289
21290 case DW_OP_const2s:
21291 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21292 i += 2;
21293 break;
d53d4ac5 21294
21ae7a4d
JK
21295 case DW_OP_const4u:
21296 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21297 i += 4;
21298 break;
21299
21300 case DW_OP_const4s:
21301 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21302 i += 4;
21303 break;
21304
585861ea
JK
21305 case DW_OP_const8u:
21306 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21307 i += 8;
21308 break;
21309
21ae7a4d
JK
21310 case DW_OP_constu:
21311 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21312 &bytes_read);
21313 i += bytes_read;
21314 break;
21315
21316 case DW_OP_consts:
21317 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21318 i += bytes_read;
21319 break;
21320
21321 case DW_OP_dup:
21322 stack[stacki + 1] = stack[stacki];
21323 stacki++;
21324 break;
21325
21326 case DW_OP_plus:
21327 stack[stacki - 1] += stack[stacki];
21328 stacki--;
21329 break;
21330
21331 case DW_OP_plus_uconst:
21332 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21333 &bytes_read);
21334 i += bytes_read;
21335 break;
21336
21337 case DW_OP_minus:
21338 stack[stacki - 1] -= stack[stacki];
21339 stacki--;
21340 break;
21341
21342 case DW_OP_deref:
21343 /* If we're not the last op, then we definitely can't encode
21344 this using GDB's address_class enum. This is valid for partial
21345 global symbols, although the variable's address will be bogus
21346 in the psymtab. */
21347 if (i < size)
21348 dwarf2_complex_location_expr_complaint ();
21349 break;
21350
21351 case DW_OP_GNU_push_tls_address:
4aa4e28b 21352 case DW_OP_form_tls_address:
21ae7a4d
JK
21353 /* The top of the stack has the offset from the beginning
21354 of the thread control block at which the variable is located. */
21355 /* Nothing should follow this operator, so the top of stack would
21356 be returned. */
21357 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21358 address will be bogus in the psymtab. Make it always at least
21359 non-zero to not look as a variable garbage collected by linker
21360 which have DW_OP_addr 0. */
21ae7a4d
JK
21361 if (i < size)
21362 dwarf2_complex_location_expr_complaint ();
585861ea 21363 stack[stacki]++;
21ae7a4d
JK
21364 break;
21365
21366 case DW_OP_GNU_uninit:
21367 break;
21368
3019eac3 21369 case DW_OP_GNU_addr_index:
49f6c839 21370 case DW_OP_GNU_const_index:
3019eac3
DE
21371 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21372 &bytes_read);
21373 i += bytes_read;
21374 break;
21375
21ae7a4d
JK
21376 default:
21377 {
f39c6ffd 21378 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21379
21380 if (name)
21381 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21382 name);
21383 else
21384 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21385 op);
21386 }
21387
21388 return (stack[stacki]);
d53d4ac5 21389 }
3c6e0cb3 21390
21ae7a4d
JK
21391 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21392 outside of the allocated space. Also enforce minimum>0. */
21393 if (stacki >= ARRAY_SIZE (stack) - 1)
21394 {
21395 complaint (&symfile_complaints,
21396 _("location description stack overflow"));
21397 return 0;
21398 }
21399
21400 if (stacki <= 0)
21401 {
21402 complaint (&symfile_complaints,
21403 _("location description stack underflow"));
21404 return 0;
21405 }
21406 }
21407 return (stack[stacki]);
c906108c
SS
21408}
21409
21410/* memory allocation interface */
21411
c906108c 21412static struct dwarf_block *
7b5a2f43 21413dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21414{
8d749320 21415 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21416}
21417
c906108c 21418static struct die_info *
b60c80d6 21419dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21420{
21421 struct die_info *die;
b60c80d6
DJ
21422 size_t size = sizeof (struct die_info);
21423
21424 if (num_attrs > 1)
21425 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21426
b60c80d6 21427 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21428 memset (die, 0, sizeof (struct die_info));
21429 return (die);
21430}
2e276125
JB
21431
21432\f
21433/* Macro support. */
21434
233d95b5
JK
21435/* Return file name relative to the compilation directory of file number I in
21436 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21437 responsible for freeing it. */
233d95b5 21438
2e276125 21439static char *
233d95b5 21440file_file_name (int file, struct line_header *lh)
2e276125 21441{
6a83a1e6
EZ
21442 /* Is the file number a valid index into the line header's file name
21443 table? Remember that file numbers start with one, not zero. */
fff8551c 21444 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 21445 {
8c43009f 21446 const file_entry &fe = lh->file_names[file - 1];
6e70227d 21447
8c43009f
PA
21448 if (!IS_ABSOLUTE_PATH (fe.name))
21449 {
21450 const char *dir = fe.include_dir (lh);
21451 if (dir != NULL)
21452 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21453 }
21454 return xstrdup (fe.name);
6a83a1e6 21455 }
2e276125
JB
21456 else
21457 {
6a83a1e6
EZ
21458 /* The compiler produced a bogus file number. We can at least
21459 record the macro definitions made in the file, even if we
21460 won't be able to find the file by name. */
21461 char fake_name[80];
9a619af0 21462
8c042590
PM
21463 xsnprintf (fake_name, sizeof (fake_name),
21464 "<bad macro file number %d>", file);
2e276125 21465
6e70227d 21466 complaint (&symfile_complaints,
6a83a1e6
EZ
21467 _("bad file number in macro information (%d)"),
21468 file);
2e276125 21469
6a83a1e6 21470 return xstrdup (fake_name);
2e276125
JB
21471 }
21472}
21473
233d95b5
JK
21474/* Return the full name of file number I in *LH's file name table.
21475 Use COMP_DIR as the name of the current directory of the
21476 compilation. The result is allocated using xmalloc; the caller is
21477 responsible for freeing it. */
21478static char *
21479file_full_name (int file, struct line_header *lh, const char *comp_dir)
21480{
21481 /* Is the file number a valid index into the line header's file name
21482 table? Remember that file numbers start with one, not zero. */
fff8551c 21483 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
21484 {
21485 char *relative = file_file_name (file, lh);
21486
21487 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21488 return relative;
b36cec19
PA
21489 return reconcat (relative, comp_dir, SLASH_STRING,
21490 relative, (char *) NULL);
233d95b5
JK
21491 }
21492 else
21493 return file_file_name (file, lh);
21494}
21495
2e276125
JB
21496
21497static struct macro_source_file *
21498macro_start_file (int file, int line,
21499 struct macro_source_file *current_file,
43f3e411 21500 struct line_header *lh)
2e276125 21501{
233d95b5
JK
21502 /* File name relative to the compilation directory of this source file. */
21503 char *file_name = file_file_name (file, lh);
2e276125 21504
2e276125 21505 if (! current_file)
abc9d0dc 21506 {
fc474241
DE
21507 /* Note: We don't create a macro table for this compilation unit
21508 at all until we actually get a filename. */
43f3e411 21509 struct macro_table *macro_table = get_macro_table ();
fc474241 21510
abc9d0dc
TT
21511 /* If we have no current file, then this must be the start_file
21512 directive for the compilation unit's main source file. */
fc474241
DE
21513 current_file = macro_set_main (macro_table, file_name);
21514 macro_define_special (macro_table);
abc9d0dc 21515 }
2e276125 21516 else
233d95b5 21517 current_file = macro_include (current_file, line, file_name);
2e276125 21518
233d95b5 21519 xfree (file_name);
6e70227d 21520
2e276125
JB
21521 return current_file;
21522}
21523
21524
21525/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21526 followed by a null byte. */
21527static char *
21528copy_string (const char *buf, int len)
21529{
224c3ddb 21530 char *s = (char *) xmalloc (len + 1);
9a619af0 21531
2e276125
JB
21532 memcpy (s, buf, len);
21533 s[len] = '\0';
2e276125
JB
21534 return s;
21535}
21536
21537
21538static const char *
21539consume_improper_spaces (const char *p, const char *body)
21540{
21541 if (*p == ' ')
21542 {
4d3c2250 21543 complaint (&symfile_complaints,
3e43a32a
MS
21544 _("macro definition contains spaces "
21545 "in formal argument list:\n`%s'"),
4d3c2250 21546 body);
2e276125
JB
21547
21548 while (*p == ' ')
21549 p++;
21550 }
21551
21552 return p;
21553}
21554
21555
21556static void
21557parse_macro_definition (struct macro_source_file *file, int line,
21558 const char *body)
21559{
21560 const char *p;
21561
21562 /* The body string takes one of two forms. For object-like macro
21563 definitions, it should be:
21564
21565 <macro name> " " <definition>
21566
21567 For function-like macro definitions, it should be:
21568
21569 <macro name> "() " <definition>
21570 or
21571 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21572
21573 Spaces may appear only where explicitly indicated, and in the
21574 <definition>.
21575
21576 The Dwarf 2 spec says that an object-like macro's name is always
21577 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21578 the space when the macro's definition is the empty string.
2e276125
JB
21579
21580 The Dwarf 2 spec says that there should be no spaces between the
21581 formal arguments in a function-like macro's formal argument list,
21582 but versions of GCC around March 2002 include spaces after the
21583 commas. */
21584
21585
21586 /* Find the extent of the macro name. The macro name is terminated
21587 by either a space or null character (for an object-like macro) or
21588 an opening paren (for a function-like macro). */
21589 for (p = body; *p; p++)
21590 if (*p == ' ' || *p == '(')
21591 break;
21592
21593 if (*p == ' ' || *p == '\0')
21594 {
21595 /* It's an object-like macro. */
21596 int name_len = p - body;
21597 char *name = copy_string (body, name_len);
21598 const char *replacement;
21599
21600 if (*p == ' ')
21601 replacement = body + name_len + 1;
21602 else
21603 {
4d3c2250 21604 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21605 replacement = body + name_len;
21606 }
6e70227d 21607
2e276125
JB
21608 macro_define_object (file, line, name, replacement);
21609
21610 xfree (name);
21611 }
21612 else if (*p == '(')
21613 {
21614 /* It's a function-like macro. */
21615 char *name = copy_string (body, p - body);
21616 int argc = 0;
21617 int argv_size = 1;
8d749320 21618 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21619
21620 p++;
21621
21622 p = consume_improper_spaces (p, body);
21623
21624 /* Parse the formal argument list. */
21625 while (*p && *p != ')')
21626 {
21627 /* Find the extent of the current argument name. */
21628 const char *arg_start = p;
21629
21630 while (*p && *p != ',' && *p != ')' && *p != ' ')
21631 p++;
21632
21633 if (! *p || p == arg_start)
4d3c2250 21634 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21635 else
21636 {
21637 /* Make sure argv has room for the new argument. */
21638 if (argc >= argv_size)
21639 {
21640 argv_size *= 2;
224c3ddb 21641 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21642 }
21643
21644 argv[argc++] = copy_string (arg_start, p - arg_start);
21645 }
21646
21647 p = consume_improper_spaces (p, body);
21648
21649 /* Consume the comma, if present. */
21650 if (*p == ',')
21651 {
21652 p++;
21653
21654 p = consume_improper_spaces (p, body);
21655 }
21656 }
21657
21658 if (*p == ')')
21659 {
21660 p++;
21661
21662 if (*p == ' ')
21663 /* Perfectly formed definition, no complaints. */
21664 macro_define_function (file, line, name,
6e70227d 21665 argc, (const char **) argv,
2e276125
JB
21666 p + 1);
21667 else if (*p == '\0')
21668 {
21669 /* Complain, but do define it. */
4d3c2250 21670 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21671 macro_define_function (file, line, name,
6e70227d 21672 argc, (const char **) argv,
2e276125
JB
21673 p);
21674 }
21675 else
21676 /* Just complain. */
4d3c2250 21677 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21678 }
21679 else
21680 /* Just complain. */
4d3c2250 21681 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21682
21683 xfree (name);
21684 {
21685 int i;
21686
21687 for (i = 0; i < argc; i++)
21688 xfree (argv[i]);
21689 }
21690 xfree (argv);
21691 }
21692 else
4d3c2250 21693 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21694}
21695
cf2c3c16
TT
21696/* Skip some bytes from BYTES according to the form given in FORM.
21697 Returns the new pointer. */
2e276125 21698
d521ce57
TT
21699static const gdb_byte *
21700skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21701 enum dwarf_form form,
21702 unsigned int offset_size,
21703 struct dwarf2_section_info *section)
2e276125 21704{
cf2c3c16 21705 unsigned int bytes_read;
2e276125 21706
cf2c3c16 21707 switch (form)
2e276125 21708 {
cf2c3c16
TT
21709 case DW_FORM_data1:
21710 case DW_FORM_flag:
21711 ++bytes;
21712 break;
21713
21714 case DW_FORM_data2:
21715 bytes += 2;
21716 break;
21717
21718 case DW_FORM_data4:
21719 bytes += 4;
21720 break;
21721
21722 case DW_FORM_data8:
21723 bytes += 8;
21724 break;
21725
0224619f
JK
21726 case DW_FORM_data16:
21727 bytes += 16;
21728 break;
21729
cf2c3c16
TT
21730 case DW_FORM_string:
21731 read_direct_string (abfd, bytes, &bytes_read);
21732 bytes += bytes_read;
21733 break;
21734
21735 case DW_FORM_sec_offset:
21736 case DW_FORM_strp:
36586728 21737 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21738 bytes += offset_size;
21739 break;
21740
21741 case DW_FORM_block:
21742 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21743 bytes += bytes_read;
21744 break;
21745
21746 case DW_FORM_block1:
21747 bytes += 1 + read_1_byte (abfd, bytes);
21748 break;
21749 case DW_FORM_block2:
21750 bytes += 2 + read_2_bytes (abfd, bytes);
21751 break;
21752 case DW_FORM_block4:
21753 bytes += 4 + read_4_bytes (abfd, bytes);
21754 break;
21755
21756 case DW_FORM_sdata:
21757 case DW_FORM_udata:
3019eac3
DE
21758 case DW_FORM_GNU_addr_index:
21759 case DW_FORM_GNU_str_index:
d521ce57 21760 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21761 if (bytes == NULL)
21762 {
21763 dwarf2_section_buffer_overflow_complaint (section);
21764 return NULL;
21765 }
cf2c3c16
TT
21766 break;
21767
21768 default:
21769 {
21770 complain:
21771 complaint (&symfile_complaints,
21772 _("invalid form 0x%x in `%s'"),
a32a8923 21773 form, get_section_name (section));
cf2c3c16
TT
21774 return NULL;
21775 }
2e276125
JB
21776 }
21777
cf2c3c16
TT
21778 return bytes;
21779}
757a13d0 21780
cf2c3c16
TT
21781/* A helper for dwarf_decode_macros that handles skipping an unknown
21782 opcode. Returns an updated pointer to the macro data buffer; or,
21783 on error, issues a complaint and returns NULL. */
757a13d0 21784
d521ce57 21785static const gdb_byte *
cf2c3c16 21786skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21787 const gdb_byte **opcode_definitions,
21788 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21789 bfd *abfd,
21790 unsigned int offset_size,
21791 struct dwarf2_section_info *section)
21792{
21793 unsigned int bytes_read, i;
21794 unsigned long arg;
d521ce57 21795 const gdb_byte *defn;
2e276125 21796
cf2c3c16 21797 if (opcode_definitions[opcode] == NULL)
2e276125 21798 {
cf2c3c16
TT
21799 complaint (&symfile_complaints,
21800 _("unrecognized DW_MACFINO opcode 0x%x"),
21801 opcode);
21802 return NULL;
21803 }
2e276125 21804
cf2c3c16
TT
21805 defn = opcode_definitions[opcode];
21806 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21807 defn += bytes_read;
2e276125 21808
cf2c3c16
TT
21809 for (i = 0; i < arg; ++i)
21810 {
aead7601
SM
21811 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21812 (enum dwarf_form) defn[i], offset_size,
f664829e 21813 section);
cf2c3c16
TT
21814 if (mac_ptr == NULL)
21815 {
21816 /* skip_form_bytes already issued the complaint. */
21817 return NULL;
21818 }
21819 }
757a13d0 21820
cf2c3c16
TT
21821 return mac_ptr;
21822}
757a13d0 21823
cf2c3c16
TT
21824/* A helper function which parses the header of a macro section.
21825 If the macro section is the extended (for now called "GNU") type,
21826 then this updates *OFFSET_SIZE. Returns a pointer to just after
21827 the header, or issues a complaint and returns NULL on error. */
757a13d0 21828
d521ce57
TT
21829static const gdb_byte *
21830dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21831 bfd *abfd,
d521ce57 21832 const gdb_byte *mac_ptr,
cf2c3c16
TT
21833 unsigned int *offset_size,
21834 int section_is_gnu)
21835{
21836 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21837
cf2c3c16
TT
21838 if (section_is_gnu)
21839 {
21840 unsigned int version, flags;
757a13d0 21841
cf2c3c16 21842 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21843 if (version != 4 && version != 5)
cf2c3c16
TT
21844 {
21845 complaint (&symfile_complaints,
21846 _("unrecognized version `%d' in .debug_macro section"),
21847 version);
21848 return NULL;
21849 }
21850 mac_ptr += 2;
757a13d0 21851
cf2c3c16
TT
21852 flags = read_1_byte (abfd, mac_ptr);
21853 ++mac_ptr;
21854 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21855
cf2c3c16
TT
21856 if ((flags & 2) != 0)
21857 /* We don't need the line table offset. */
21858 mac_ptr += *offset_size;
757a13d0 21859
cf2c3c16
TT
21860 /* Vendor opcode descriptions. */
21861 if ((flags & 4) != 0)
21862 {
21863 unsigned int i, count;
757a13d0 21864
cf2c3c16
TT
21865 count = read_1_byte (abfd, mac_ptr);
21866 ++mac_ptr;
21867 for (i = 0; i < count; ++i)
21868 {
21869 unsigned int opcode, bytes_read;
21870 unsigned long arg;
21871
21872 opcode = read_1_byte (abfd, mac_ptr);
21873 ++mac_ptr;
21874 opcode_definitions[opcode] = mac_ptr;
21875 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21876 mac_ptr += bytes_read;
21877 mac_ptr += arg;
21878 }
757a13d0 21879 }
cf2c3c16 21880 }
757a13d0 21881
cf2c3c16
TT
21882 return mac_ptr;
21883}
757a13d0 21884
cf2c3c16 21885/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21886 including DW_MACRO_import. */
cf2c3c16
TT
21887
21888static void
d521ce57
TT
21889dwarf_decode_macro_bytes (bfd *abfd,
21890 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21891 struct macro_source_file *current_file,
43f3e411 21892 struct line_header *lh,
cf2c3c16 21893 struct dwarf2_section_info *section,
36586728 21894 int section_is_gnu, int section_is_dwz,
cf2c3c16 21895 unsigned int offset_size,
8fc3fc34 21896 htab_t include_hash)
cf2c3c16 21897{
4d663531 21898 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21899 enum dwarf_macro_record_type macinfo_type;
21900 int at_commandline;
d521ce57 21901 const gdb_byte *opcode_definitions[256];
757a13d0 21902
cf2c3c16
TT
21903 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21904 &offset_size, section_is_gnu);
21905 if (mac_ptr == NULL)
21906 {
21907 /* We already issued a complaint. */
21908 return;
21909 }
757a13d0
JK
21910
21911 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21912 GDB is still reading the definitions from command line. First
21913 DW_MACINFO_start_file will need to be ignored as it was already executed
21914 to create CURRENT_FILE for the main source holding also the command line
21915 definitions. On first met DW_MACINFO_start_file this flag is reset to
21916 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21917
21918 at_commandline = 1;
21919
21920 do
21921 {
21922 /* Do we at least have room for a macinfo type byte? */
21923 if (mac_ptr >= mac_end)
21924 {
f664829e 21925 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21926 break;
21927 }
21928
aead7601 21929 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21930 mac_ptr++;
21931
cf2c3c16
TT
21932 /* Note that we rely on the fact that the corresponding GNU and
21933 DWARF constants are the same. */
757a13d0
JK
21934 switch (macinfo_type)
21935 {
21936 /* A zero macinfo type indicates the end of the macro
21937 information. */
21938 case 0:
21939 break;
2e276125 21940
0af92d60
JK
21941 case DW_MACRO_define:
21942 case DW_MACRO_undef:
21943 case DW_MACRO_define_strp:
21944 case DW_MACRO_undef_strp:
21945 case DW_MACRO_define_sup:
21946 case DW_MACRO_undef_sup:
2e276125 21947 {
891d2f0b 21948 unsigned int bytes_read;
2e276125 21949 int line;
d521ce57 21950 const char *body;
cf2c3c16 21951 int is_define;
2e276125 21952
cf2c3c16
TT
21953 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21954 mac_ptr += bytes_read;
21955
0af92d60
JK
21956 if (macinfo_type == DW_MACRO_define
21957 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
21958 {
21959 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21960 mac_ptr += bytes_read;
21961 }
21962 else
21963 {
21964 LONGEST str_offset;
21965
21966 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21967 mac_ptr += offset_size;
2e276125 21968
0af92d60
JK
21969 if (macinfo_type == DW_MACRO_define_sup
21970 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 21971 || section_is_dwz)
36586728
TT
21972 {
21973 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21974
21975 body = read_indirect_string_from_dwz (dwz, str_offset);
21976 }
21977 else
21978 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21979 }
21980
0af92d60
JK
21981 is_define = (macinfo_type == DW_MACRO_define
21982 || macinfo_type == DW_MACRO_define_strp
21983 || macinfo_type == DW_MACRO_define_sup);
2e276125 21984 if (! current_file)
757a13d0
JK
21985 {
21986 /* DWARF violation as no main source is present. */
21987 complaint (&symfile_complaints,
21988 _("debug info with no main source gives macro %s "
21989 "on line %d: %s"),
cf2c3c16
TT
21990 is_define ? _("definition") : _("undefinition"),
21991 line, body);
757a13d0
JK
21992 break;
21993 }
3e43a32a
MS
21994 if ((line == 0 && !at_commandline)
21995 || (line != 0 && at_commandline))
4d3c2250 21996 complaint (&symfile_complaints,
757a13d0
JK
21997 _("debug info gives %s macro %s with %s line %d: %s"),
21998 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21999 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
22000 line == 0 ? _("zero") : _("non-zero"), line, body);
22001
cf2c3c16 22002 if (is_define)
757a13d0 22003 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
22004 else
22005 {
0af92d60
JK
22006 gdb_assert (macinfo_type == DW_MACRO_undef
22007 || macinfo_type == DW_MACRO_undef_strp
22008 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
22009 macro_undef (current_file, line, body);
22010 }
2e276125
JB
22011 }
22012 break;
22013
0af92d60 22014 case DW_MACRO_start_file:
2e276125 22015 {
891d2f0b 22016 unsigned int bytes_read;
2e276125
JB
22017 int line, file;
22018
22019 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22020 mac_ptr += bytes_read;
22021 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22022 mac_ptr += bytes_read;
22023
3e43a32a
MS
22024 if ((line == 0 && !at_commandline)
22025 || (line != 0 && at_commandline))
757a13d0
JK
22026 complaint (&symfile_complaints,
22027 _("debug info gives source %d included "
22028 "from %s at %s line %d"),
22029 file, at_commandline ? _("command-line") : _("file"),
22030 line == 0 ? _("zero") : _("non-zero"), line);
22031
22032 if (at_commandline)
22033 {
0af92d60 22034 /* This DW_MACRO_start_file was executed in the
cf2c3c16 22035 pass one. */
757a13d0
JK
22036 at_commandline = 0;
22037 }
22038 else
43f3e411 22039 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
22040 }
22041 break;
22042
0af92d60 22043 case DW_MACRO_end_file:
2e276125 22044 if (! current_file)
4d3c2250 22045 complaint (&symfile_complaints,
3e43a32a
MS
22046 _("macro debug info has an unmatched "
22047 "`close_file' directive"));
2e276125
JB
22048 else
22049 {
22050 current_file = current_file->included_by;
22051 if (! current_file)
22052 {
cf2c3c16 22053 enum dwarf_macro_record_type next_type;
2e276125
JB
22054
22055 /* GCC circa March 2002 doesn't produce the zero
22056 type byte marking the end of the compilation
22057 unit. Complain if it's not there, but exit no
22058 matter what. */
22059
22060 /* Do we at least have room for a macinfo type byte? */
22061 if (mac_ptr >= mac_end)
22062 {
f664829e 22063 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22064 return;
22065 }
22066
22067 /* We don't increment mac_ptr here, so this is just
22068 a look-ahead. */
aead7601
SM
22069 next_type
22070 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22071 mac_ptr);
2e276125 22072 if (next_type != 0)
4d3c2250 22073 complaint (&symfile_complaints,
3e43a32a
MS
22074 _("no terminating 0-type entry for "
22075 "macros in `.debug_macinfo' section"));
2e276125
JB
22076
22077 return;
22078 }
22079 }
22080 break;
22081
0af92d60
JK
22082 case DW_MACRO_import:
22083 case DW_MACRO_import_sup:
cf2c3c16
TT
22084 {
22085 LONGEST offset;
8fc3fc34 22086 void **slot;
a036ba48
TT
22087 bfd *include_bfd = abfd;
22088 struct dwarf2_section_info *include_section = section;
d521ce57 22089 const gdb_byte *include_mac_end = mac_end;
a036ba48 22090 int is_dwz = section_is_dwz;
d521ce57 22091 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22092
22093 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22094 mac_ptr += offset_size;
22095
0af92d60 22096 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22097 {
22098 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22099
4d663531 22100 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22101
a036ba48 22102 include_section = &dwz->macro;
a32a8923 22103 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22104 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22105 is_dwz = 1;
22106 }
22107
22108 new_mac_ptr = include_section->buffer + offset;
22109 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22110
8fc3fc34
TT
22111 if (*slot != NULL)
22112 {
22113 /* This has actually happened; see
22114 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22115 complaint (&symfile_complaints,
0af92d60 22116 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22117 ".debug_macro section"));
22118 }
22119 else
22120 {
d521ce57 22121 *slot = (void *) new_mac_ptr;
36586728 22122
a036ba48 22123 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22124 include_mac_end, current_file, lh,
36586728 22125 section, section_is_gnu, is_dwz,
4d663531 22126 offset_size, include_hash);
8fc3fc34 22127
d521ce57 22128 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22129 }
cf2c3c16
TT
22130 }
22131 break;
22132
2e276125 22133 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22134 if (!section_is_gnu)
22135 {
22136 unsigned int bytes_read;
2e276125 22137
ac298888
TT
22138 /* This reads the constant, but since we don't recognize
22139 any vendor extensions, we ignore it. */
22140 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22141 mac_ptr += bytes_read;
22142 read_direct_string (abfd, mac_ptr, &bytes_read);
22143 mac_ptr += bytes_read;
2e276125 22144
cf2c3c16
TT
22145 /* We don't recognize any vendor extensions. */
22146 break;
22147 }
22148 /* FALLTHROUGH */
22149
22150 default:
22151 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22152 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22153 section);
22154 if (mac_ptr == NULL)
22155 return;
22156 break;
2e276125 22157 }
757a13d0 22158 } while (macinfo_type != 0);
2e276125 22159}
8e19ed76 22160
cf2c3c16 22161static void
09262596 22162dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22163 int section_is_gnu)
cf2c3c16 22164{
bb5ed363 22165 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22166 struct line_header *lh = cu->line_header;
22167 bfd *abfd;
d521ce57 22168 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22169 struct macro_source_file *current_file = 0;
22170 enum dwarf_macro_record_type macinfo_type;
22171 unsigned int offset_size = cu->header.offset_size;
d521ce57 22172 const gdb_byte *opcode_definitions[256];
8fc3fc34 22173 struct cleanup *cleanup;
8fc3fc34 22174 void **slot;
09262596
DE
22175 struct dwarf2_section_info *section;
22176 const char *section_name;
22177
22178 if (cu->dwo_unit != NULL)
22179 {
22180 if (section_is_gnu)
22181 {
22182 section = &cu->dwo_unit->dwo_file->sections.macro;
22183 section_name = ".debug_macro.dwo";
22184 }
22185 else
22186 {
22187 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22188 section_name = ".debug_macinfo.dwo";
22189 }
22190 }
22191 else
22192 {
22193 if (section_is_gnu)
22194 {
22195 section = &dwarf2_per_objfile->macro;
22196 section_name = ".debug_macro";
22197 }
22198 else
22199 {
22200 section = &dwarf2_per_objfile->macinfo;
22201 section_name = ".debug_macinfo";
22202 }
22203 }
cf2c3c16 22204
bb5ed363 22205 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22206 if (section->buffer == NULL)
22207 {
fceca515 22208 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22209 return;
22210 }
a32a8923 22211 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22212
22213 /* First pass: Find the name of the base filename.
22214 This filename is needed in order to process all macros whose definition
22215 (or undefinition) comes from the command line. These macros are defined
22216 before the first DW_MACINFO_start_file entry, and yet still need to be
22217 associated to the base file.
22218
22219 To determine the base file name, we scan the macro definitions until we
22220 reach the first DW_MACINFO_start_file entry. We then initialize
22221 CURRENT_FILE accordingly so that any macro definition found before the
22222 first DW_MACINFO_start_file can still be associated to the base file. */
22223
22224 mac_ptr = section->buffer + offset;
22225 mac_end = section->buffer + section->size;
22226
22227 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22228 &offset_size, section_is_gnu);
22229 if (mac_ptr == NULL)
22230 {
22231 /* We already issued a complaint. */
22232 return;
22233 }
22234
22235 do
22236 {
22237 /* Do we at least have room for a macinfo type byte? */
22238 if (mac_ptr >= mac_end)
22239 {
22240 /* Complaint is printed during the second pass as GDB will probably
22241 stop the first pass earlier upon finding
22242 DW_MACINFO_start_file. */
22243 break;
22244 }
22245
aead7601 22246 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22247 mac_ptr++;
22248
22249 /* Note that we rely on the fact that the corresponding GNU and
22250 DWARF constants are the same. */
22251 switch (macinfo_type)
22252 {
22253 /* A zero macinfo type indicates the end of the macro
22254 information. */
22255 case 0:
22256 break;
22257
0af92d60
JK
22258 case DW_MACRO_define:
22259 case DW_MACRO_undef:
cf2c3c16
TT
22260 /* Only skip the data by MAC_PTR. */
22261 {
22262 unsigned int bytes_read;
22263
22264 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22265 mac_ptr += bytes_read;
22266 read_direct_string (abfd, mac_ptr, &bytes_read);
22267 mac_ptr += bytes_read;
22268 }
22269 break;
22270
0af92d60 22271 case DW_MACRO_start_file:
cf2c3c16
TT
22272 {
22273 unsigned int bytes_read;
22274 int line, file;
22275
22276 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22277 mac_ptr += bytes_read;
22278 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22279 mac_ptr += bytes_read;
22280
43f3e411 22281 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22282 }
22283 break;
22284
0af92d60 22285 case DW_MACRO_end_file:
cf2c3c16
TT
22286 /* No data to skip by MAC_PTR. */
22287 break;
22288
0af92d60
JK
22289 case DW_MACRO_define_strp:
22290 case DW_MACRO_undef_strp:
22291 case DW_MACRO_define_sup:
22292 case DW_MACRO_undef_sup:
cf2c3c16
TT
22293 {
22294 unsigned int bytes_read;
22295
22296 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22297 mac_ptr += bytes_read;
22298 mac_ptr += offset_size;
22299 }
22300 break;
22301
0af92d60
JK
22302 case DW_MACRO_import:
22303 case DW_MACRO_import_sup:
cf2c3c16 22304 /* Note that, according to the spec, a transparent include
0af92d60 22305 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22306 skip this opcode. */
22307 mac_ptr += offset_size;
22308 break;
22309
22310 case DW_MACINFO_vendor_ext:
22311 /* Only skip the data by MAC_PTR. */
22312 if (!section_is_gnu)
22313 {
22314 unsigned int bytes_read;
22315
22316 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22317 mac_ptr += bytes_read;
22318 read_direct_string (abfd, mac_ptr, &bytes_read);
22319 mac_ptr += bytes_read;
22320 }
22321 /* FALLTHROUGH */
22322
22323 default:
22324 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22325 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22326 section);
22327 if (mac_ptr == NULL)
22328 return;
22329 break;
22330 }
22331 } while (macinfo_type != 0 && current_file == NULL);
22332
22333 /* Second pass: Process all entries.
22334
22335 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22336 command-line macro definitions/undefinitions. This flag is unset when we
22337 reach the first DW_MACINFO_start_file entry. */
22338
fc4007c9
TT
22339 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22340 htab_eq_pointer,
22341 NULL, xcalloc, xfree));
8fc3fc34 22342 mac_ptr = section->buffer + offset;
fc4007c9 22343 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22344 *slot = (void *) mac_ptr;
8fc3fc34 22345 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22346 current_file, lh, section,
fc4007c9
TT
22347 section_is_gnu, 0, offset_size,
22348 include_hash.get ());
cf2c3c16
TT
22349}
22350
8e19ed76 22351/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22352 if so return true else false. */
380bca97 22353
8e19ed76 22354static int
6e5a29e1 22355attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22356{
22357 return (attr == NULL ? 0 :
22358 attr->form == DW_FORM_block1
22359 || attr->form == DW_FORM_block2
22360 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22361 || attr->form == DW_FORM_block
22362 || attr->form == DW_FORM_exprloc);
8e19ed76 22363}
4c2df51b 22364
c6a0999f
JB
22365/* Return non-zero if ATTR's value is a section offset --- classes
22366 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22367 You may use DW_UNSND (attr) to retrieve such offsets.
22368
22369 Section 7.5.4, "Attribute Encodings", explains that no attribute
22370 may have a value that belongs to more than one of these classes; it
22371 would be ambiguous if we did, because we use the same forms for all
22372 of them. */
380bca97 22373
3690dd37 22374static int
6e5a29e1 22375attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22376{
22377 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22378 || attr->form == DW_FORM_data8
22379 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22380}
22381
3690dd37
JB
22382/* Return non-zero if ATTR's value falls in the 'constant' class, or
22383 zero otherwise. When this function returns true, you can apply
22384 dwarf2_get_attr_constant_value to it.
22385
22386 However, note that for some attributes you must check
22387 attr_form_is_section_offset before using this test. DW_FORM_data4
22388 and DW_FORM_data8 are members of both the constant class, and of
22389 the classes that contain offsets into other debug sections
22390 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22391 that, if an attribute's can be either a constant or one of the
22392 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22393 taken as section offsets, not constants.
22394
22395 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22396 cannot handle that. */
380bca97 22397
3690dd37 22398static int
6e5a29e1 22399attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22400{
22401 switch (attr->form)
22402 {
22403 case DW_FORM_sdata:
22404 case DW_FORM_udata:
22405 case DW_FORM_data1:
22406 case DW_FORM_data2:
22407 case DW_FORM_data4:
22408 case DW_FORM_data8:
22409 return 1;
22410 default:
22411 return 0;
22412 }
22413}
22414
7771576e
SA
22415
22416/* DW_ADDR is always stored already as sect_offset; despite for the forms
22417 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22418
22419static int
6e5a29e1 22420attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22421{
22422 switch (attr->form)
22423 {
22424 case DW_FORM_ref_addr:
22425 case DW_FORM_ref1:
22426 case DW_FORM_ref2:
22427 case DW_FORM_ref4:
22428 case DW_FORM_ref8:
22429 case DW_FORM_ref_udata:
22430 case DW_FORM_GNU_ref_alt:
22431 return 1;
22432 default:
22433 return 0;
22434 }
22435}
22436
3019eac3
DE
22437/* Return the .debug_loc section to use for CU.
22438 For DWO files use .debug_loc.dwo. */
22439
22440static struct dwarf2_section_info *
22441cu_debug_loc_section (struct dwarf2_cu *cu)
22442{
22443 if (cu->dwo_unit)
43988095
JK
22444 {
22445 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22446
22447 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22448 }
22449 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22450 : &dwarf2_per_objfile->loc);
3019eac3
DE
22451}
22452
8cf6f0b1
TT
22453/* A helper function that fills in a dwarf2_loclist_baton. */
22454
22455static void
22456fill_in_loclist_baton (struct dwarf2_cu *cu,
22457 struct dwarf2_loclist_baton *baton,
ff39bb5e 22458 const struct attribute *attr)
8cf6f0b1 22459{
3019eac3
DE
22460 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22461
22462 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22463
22464 baton->per_cu = cu->per_cu;
22465 gdb_assert (baton->per_cu);
22466 /* We don't know how long the location list is, but make sure we
22467 don't run off the edge of the section. */
3019eac3
DE
22468 baton->size = section->size - DW_UNSND (attr);
22469 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22470 baton->base_address = cu->base_address;
f664829e 22471 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22472}
22473
4c2df51b 22474static void
ff39bb5e 22475dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22476 struct dwarf2_cu *cu, int is_block)
4c2df51b 22477{
bb5ed363 22478 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22479 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22480
3690dd37 22481 if (attr_form_is_section_offset (attr)
3019eac3 22482 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22483 the section. If so, fall through to the complaint in the
22484 other branch. */
3019eac3 22485 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22486 {
0d53c4c4 22487 struct dwarf2_loclist_baton *baton;
4c2df51b 22488
8d749320 22489 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22490
8cf6f0b1 22491 fill_in_loclist_baton (cu, baton, attr);
be391dca 22492
d00adf39 22493 if (cu->base_known == 0)
0d53c4c4 22494 complaint (&symfile_complaints,
3e43a32a
MS
22495 _("Location list used without "
22496 "specifying the CU base address."));
4c2df51b 22497
f1e6e072
TT
22498 SYMBOL_ACLASS_INDEX (sym) = (is_block
22499 ? dwarf2_loclist_block_index
22500 : dwarf2_loclist_index);
0d53c4c4
DJ
22501 SYMBOL_LOCATION_BATON (sym) = baton;
22502 }
22503 else
22504 {
22505 struct dwarf2_locexpr_baton *baton;
22506
8d749320 22507 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22508 baton->per_cu = cu->per_cu;
22509 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22510
22511 if (attr_form_is_block (attr))
22512 {
22513 /* Note that we're just copying the block's data pointer
22514 here, not the actual data. We're still pointing into the
6502dd73
DJ
22515 info_buffer for SYM's objfile; right now we never release
22516 that buffer, but when we do clean up properly this may
22517 need to change. */
0d53c4c4
DJ
22518 baton->size = DW_BLOCK (attr)->size;
22519 baton->data = DW_BLOCK (attr)->data;
22520 }
22521 else
22522 {
22523 dwarf2_invalid_attrib_class_complaint ("location description",
22524 SYMBOL_NATURAL_NAME (sym));
22525 baton->size = 0;
0d53c4c4 22526 }
6e70227d 22527
f1e6e072
TT
22528 SYMBOL_ACLASS_INDEX (sym) = (is_block
22529 ? dwarf2_locexpr_block_index
22530 : dwarf2_locexpr_index);
0d53c4c4
DJ
22531 SYMBOL_LOCATION_BATON (sym) = baton;
22532 }
4c2df51b 22533}
6502dd73 22534
9aa1f1e3
TT
22535/* Return the OBJFILE associated with the compilation unit CU. If CU
22536 came from a separate debuginfo file, then the master objfile is
22537 returned. */
ae0d2f24
UW
22538
22539struct objfile *
22540dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22541{
9291a0cd 22542 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22543
22544 /* Return the master objfile, so that we can report and look up the
22545 correct file containing this variable. */
22546 if (objfile->separate_debug_objfile_backlink)
22547 objfile = objfile->separate_debug_objfile_backlink;
22548
22549 return objfile;
22550}
22551
96408a79
SA
22552/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22553 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22554 CU_HEADERP first. */
22555
22556static const struct comp_unit_head *
22557per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22558 struct dwarf2_per_cu_data *per_cu)
22559{
d521ce57 22560 const gdb_byte *info_ptr;
96408a79
SA
22561
22562 if (per_cu->cu)
22563 return &per_cu->cu->header;
22564
9c541725 22565 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22566
22567 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22568 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22569 rcuh_kind::COMPILE);
96408a79
SA
22570
22571 return cu_headerp;
22572}
22573
ae0d2f24
UW
22574/* Return the address size given in the compilation unit header for CU. */
22575
98714339 22576int
ae0d2f24
UW
22577dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22578{
96408a79
SA
22579 struct comp_unit_head cu_header_local;
22580 const struct comp_unit_head *cu_headerp;
c471e790 22581
96408a79
SA
22582 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22583
22584 return cu_headerp->addr_size;
ae0d2f24
UW
22585}
22586
9eae7c52
TT
22587/* Return the offset size given in the compilation unit header for CU. */
22588
22589int
22590dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22591{
96408a79
SA
22592 struct comp_unit_head cu_header_local;
22593 const struct comp_unit_head *cu_headerp;
9c6c53f7 22594
96408a79
SA
22595 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22596
22597 return cu_headerp->offset_size;
22598}
22599
22600/* See its dwarf2loc.h declaration. */
22601
22602int
22603dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22604{
22605 struct comp_unit_head cu_header_local;
22606 const struct comp_unit_head *cu_headerp;
22607
22608 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22609
22610 if (cu_headerp->version == 2)
22611 return cu_headerp->addr_size;
22612 else
22613 return cu_headerp->offset_size;
181cebd4
JK
22614}
22615
9aa1f1e3
TT
22616/* Return the text offset of the CU. The returned offset comes from
22617 this CU's objfile. If this objfile came from a separate debuginfo
22618 file, then the offset may be different from the corresponding
22619 offset in the parent objfile. */
22620
22621CORE_ADDR
22622dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22623{
bb3fa9d0 22624 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22625
22626 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22627}
22628
43988095
JK
22629/* Return DWARF version number of PER_CU. */
22630
22631short
22632dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22633{
22634 return per_cu->dwarf_version;
22635}
22636
348e048f
DE
22637/* Locate the .debug_info compilation unit from CU's objfile which contains
22638 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22639
22640static struct dwarf2_per_cu_data *
9c541725 22641dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 22642 unsigned int offset_in_dwz,
ae038cb0
DJ
22643 struct objfile *objfile)
22644{
22645 struct dwarf2_per_cu_data *this_cu;
22646 int low, high;
36586728 22647 const sect_offset *cu_off;
ae038cb0 22648
ae038cb0
DJ
22649 low = 0;
22650 high = dwarf2_per_objfile->n_comp_units - 1;
22651 while (high > low)
22652 {
36586728 22653 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22654 int mid = low + (high - low) / 2;
9a619af0 22655
36586728 22656 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 22657 cu_off = &mid_cu->sect_off;
36586728 22658 if (mid_cu->is_dwz > offset_in_dwz
9c541725 22659 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
22660 high = mid;
22661 else
22662 low = mid + 1;
22663 }
22664 gdb_assert (low == high);
36586728 22665 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
22666 cu_off = &this_cu->sect_off;
22667 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 22668 {
36586728 22669 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22670 error (_("Dwarf Error: could not find partial DIE containing "
9c541725
PA
22671 "offset 0x%x [in module %s]"),
22672 to_underlying (sect_off), bfd_get_filename (objfile->obfd));
10b3939b 22673
9c541725
PA
22674 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22675 <= sect_off);
ae038cb0
DJ
22676 return dwarf2_per_objfile->all_comp_units[low-1];
22677 }
22678 else
22679 {
22680 this_cu = dwarf2_per_objfile->all_comp_units[low];
22681 if (low == dwarf2_per_objfile->n_comp_units - 1
9c541725
PA
22682 && sect_off >= this_cu->sect_off + this_cu->length)
22683 error (_("invalid dwarf2 offset %u"), to_underlying (sect_off));
22684 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22685 return this_cu;
22686 }
22687}
22688
23745b47 22689/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22690
9816fde3 22691static void
23745b47 22692init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22693{
9816fde3 22694 memset (cu, 0, sizeof (*cu));
23745b47
DE
22695 per_cu->cu = cu;
22696 cu->per_cu = per_cu;
22697 cu->objfile = per_cu->objfile;
93311388 22698 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22699}
22700
22701/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22702
22703static void
95554aad
TT
22704prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22705 enum language pretend_language)
9816fde3
JK
22706{
22707 struct attribute *attr;
22708
22709 /* Set the language we're debugging. */
22710 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22711 if (attr)
22712 set_cu_language (DW_UNSND (attr), cu);
22713 else
9cded63f 22714 {
95554aad 22715 cu->language = pretend_language;
9cded63f
TT
22716 cu->language_defn = language_def (cu->language);
22717 }
dee91e82 22718
7d45c7c3 22719 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22720}
22721
ae038cb0
DJ
22722/* Release one cached compilation unit, CU. We unlink it from the tree
22723 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22724 the caller is responsible for that.
22725 NOTE: DATA is a void * because this function is also used as a
22726 cleanup routine. */
ae038cb0
DJ
22727
22728static void
68dc6402 22729free_heap_comp_unit (void *data)
ae038cb0 22730{
9a3c8263 22731 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22732
23745b47
DE
22733 gdb_assert (cu->per_cu != NULL);
22734 cu->per_cu->cu = NULL;
ae038cb0
DJ
22735 cu->per_cu = NULL;
22736
22737 obstack_free (&cu->comp_unit_obstack, NULL);
22738
22739 xfree (cu);
22740}
22741
72bf9492 22742/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22743 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22744 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22745
22746static void
22747free_stack_comp_unit (void *data)
22748{
9a3c8263 22749 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22750
23745b47
DE
22751 gdb_assert (cu->per_cu != NULL);
22752 cu->per_cu->cu = NULL;
22753 cu->per_cu = NULL;
22754
72bf9492
DJ
22755 obstack_free (&cu->comp_unit_obstack, NULL);
22756 cu->partial_dies = NULL;
ae038cb0
DJ
22757}
22758
22759/* Free all cached compilation units. */
22760
22761static void
22762free_cached_comp_units (void *data)
22763{
22764 struct dwarf2_per_cu_data *per_cu, **last_chain;
22765
22766 per_cu = dwarf2_per_objfile->read_in_chain;
22767 last_chain = &dwarf2_per_objfile->read_in_chain;
22768 while (per_cu != NULL)
22769 {
22770 struct dwarf2_per_cu_data *next_cu;
22771
22772 next_cu = per_cu->cu->read_in_chain;
22773
68dc6402 22774 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22775 *last_chain = next_cu;
22776
22777 per_cu = next_cu;
22778 }
22779}
22780
22781/* Increase the age counter on each cached compilation unit, and free
22782 any that are too old. */
22783
22784static void
22785age_cached_comp_units (void)
22786{
22787 struct dwarf2_per_cu_data *per_cu, **last_chain;
22788
22789 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22790 per_cu = dwarf2_per_objfile->read_in_chain;
22791 while (per_cu != NULL)
22792 {
22793 per_cu->cu->last_used ++;
b4f54984 22794 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22795 dwarf2_mark (per_cu->cu);
22796 per_cu = per_cu->cu->read_in_chain;
22797 }
22798
22799 per_cu = dwarf2_per_objfile->read_in_chain;
22800 last_chain = &dwarf2_per_objfile->read_in_chain;
22801 while (per_cu != NULL)
22802 {
22803 struct dwarf2_per_cu_data *next_cu;
22804
22805 next_cu = per_cu->cu->read_in_chain;
22806
22807 if (!per_cu->cu->mark)
22808 {
68dc6402 22809 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22810 *last_chain = next_cu;
22811 }
22812 else
22813 last_chain = &per_cu->cu->read_in_chain;
22814
22815 per_cu = next_cu;
22816 }
22817}
22818
22819/* Remove a single compilation unit from the cache. */
22820
22821static void
dee91e82 22822free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22823{
22824 struct dwarf2_per_cu_data *per_cu, **last_chain;
22825
22826 per_cu = dwarf2_per_objfile->read_in_chain;
22827 last_chain = &dwarf2_per_objfile->read_in_chain;
22828 while (per_cu != NULL)
22829 {
22830 struct dwarf2_per_cu_data *next_cu;
22831
22832 next_cu = per_cu->cu->read_in_chain;
22833
dee91e82 22834 if (per_cu == target_per_cu)
ae038cb0 22835 {
68dc6402 22836 free_heap_comp_unit (per_cu->cu);
dee91e82 22837 per_cu->cu = NULL;
ae038cb0
DJ
22838 *last_chain = next_cu;
22839 break;
22840 }
22841 else
22842 last_chain = &per_cu->cu->read_in_chain;
22843
22844 per_cu = next_cu;
22845 }
22846}
22847
fe3e1990
DJ
22848/* Release all extra memory associated with OBJFILE. */
22849
22850void
22851dwarf2_free_objfile (struct objfile *objfile)
22852{
9a3c8263
SM
22853 dwarf2_per_objfile
22854 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22855 dwarf2_objfile_data_key);
fe3e1990
DJ
22856
22857 if (dwarf2_per_objfile == NULL)
22858 return;
22859
22860 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22861 free_cached_comp_units (NULL);
22862
7b9f3c50
DE
22863 if (dwarf2_per_objfile->quick_file_names_table)
22864 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22865
527f3840
JK
22866 if (dwarf2_per_objfile->line_header_hash)
22867 htab_delete (dwarf2_per_objfile->line_header_hash);
22868
fe3e1990
DJ
22869 /* Everything else should be on the objfile obstack. */
22870}
22871
dee91e82
DE
22872/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22873 We store these in a hash table separate from the DIEs, and preserve them
22874 when the DIEs are flushed out of cache.
22875
22876 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22877 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22878 or the type may come from a DWO file. Furthermore, while it's more logical
22879 to use per_cu->section+offset, with Fission the section with the data is in
22880 the DWO file but we don't know that section at the point we need it.
22881 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22882 because we can enter the lookup routine, get_die_type_at_offset, from
22883 outside this file, and thus won't necessarily have PER_CU->cu.
22884 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22885
dee91e82 22886struct dwarf2_per_cu_offset_and_type
1c379e20 22887{
dee91e82 22888 const struct dwarf2_per_cu_data *per_cu;
9c541725 22889 sect_offset sect_off;
1c379e20
DJ
22890 struct type *type;
22891};
22892
dee91e82 22893/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22894
22895static hashval_t
dee91e82 22896per_cu_offset_and_type_hash (const void *item)
1c379e20 22897{
9a3c8263
SM
22898 const struct dwarf2_per_cu_offset_and_type *ofs
22899 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22900
9c541725 22901 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
22902}
22903
dee91e82 22904/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22905
22906static int
dee91e82 22907per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22908{
9a3c8263
SM
22909 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22910 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22911 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22912 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22913
dee91e82 22914 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 22915 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
22916}
22917
22918/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22919 table if necessary. For convenience, return TYPE.
22920
22921 The DIEs reading must have careful ordering to:
22922 * Not cause infite loops trying to read in DIEs as a prerequisite for
22923 reading current DIE.
22924 * Not trying to dereference contents of still incompletely read in types
22925 while reading in other DIEs.
22926 * Enable referencing still incompletely read in types just by a pointer to
22927 the type without accessing its fields.
22928
22929 Therefore caller should follow these rules:
22930 * Try to fetch any prerequisite types we may need to build this DIE type
22931 before building the type and calling set_die_type.
e71ec853 22932 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22933 possible before fetching more types to complete the current type.
22934 * Make the type as complete as possible before fetching more types. */
1c379e20 22935
f792889a 22936static struct type *
1c379e20
DJ
22937set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22938{
dee91e82 22939 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22940 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22941 struct attribute *attr;
22942 struct dynamic_prop prop;
1c379e20 22943
b4ba55a1
JB
22944 /* For Ada types, make sure that the gnat-specific data is always
22945 initialized (if not already set). There are a few types where
22946 we should not be doing so, because the type-specific area is
22947 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22948 where the type-specific area is used to store the floatformat).
22949 But this is not a problem, because the gnat-specific information
22950 is actually not needed for these types. */
22951 if (need_gnat_info (cu)
22952 && TYPE_CODE (type) != TYPE_CODE_FUNC
22953 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22954 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22955 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22956 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22957 && !HAVE_GNAT_AUX_INFO (type))
22958 INIT_GNAT_SPECIFIC (type);
22959
3f2f83dd
KB
22960 /* Read DW_AT_allocated and set in type. */
22961 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22962 if (attr_form_is_block (attr))
22963 {
22964 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22965 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22966 }
22967 else if (attr != NULL)
22968 {
22969 complaint (&symfile_complaints,
9c541725
PA
22970 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22971 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22972 to_underlying (die->sect_off));
3f2f83dd
KB
22973 }
22974
22975 /* Read DW_AT_associated and set in type. */
22976 attr = dwarf2_attr (die, DW_AT_associated, cu);
22977 if (attr_form_is_block (attr))
22978 {
22979 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22980 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22981 }
22982 else if (attr != NULL)
22983 {
22984 complaint (&symfile_complaints,
9c541725
PA
22985 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22986 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22987 to_underlying (die->sect_off));
3f2f83dd
KB
22988 }
22989
3cdcd0ce
JB
22990 /* Read DW_AT_data_location and set in type. */
22991 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22992 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22993 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22994
dee91e82 22995 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22996 {
dee91e82
DE
22997 dwarf2_per_objfile->die_type_hash =
22998 htab_create_alloc_ex (127,
22999 per_cu_offset_and_type_hash,
23000 per_cu_offset_and_type_eq,
23001 NULL,
23002 &objfile->objfile_obstack,
23003 hashtab_obstack_allocate,
23004 dummy_obstack_deallocate);
f792889a 23005 }
1c379e20 23006
dee91e82 23007 ofs.per_cu = cu->per_cu;
9c541725 23008 ofs.sect_off = die->sect_off;
1c379e20 23009 ofs.type = type;
dee91e82
DE
23010 slot = (struct dwarf2_per_cu_offset_and_type **)
23011 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
23012 if (*slot)
23013 complaint (&symfile_complaints,
23014 _("A problem internal to GDB: DIE 0x%x has type already set"),
9c541725 23015 to_underlying (die->sect_off));
8d749320
SM
23016 *slot = XOBNEW (&objfile->objfile_obstack,
23017 struct dwarf2_per_cu_offset_and_type);
1c379e20 23018 **slot = ofs;
f792889a 23019 return type;
1c379e20
DJ
23020}
23021
9c541725 23022/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23023 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23024
23025static struct type *
9c541725 23026get_die_type_at_offset (sect_offset sect_off,
673bfd45 23027 struct dwarf2_per_cu_data *per_cu)
1c379e20 23028{
dee91e82 23029 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23030
dee91e82 23031 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23032 return NULL;
1c379e20 23033
dee91e82 23034 ofs.per_cu = per_cu;
9c541725 23035 ofs.sect_off = sect_off;
9a3c8263
SM
23036 slot = ((struct dwarf2_per_cu_offset_and_type *)
23037 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
23038 if (slot)
23039 return slot->type;
23040 else
23041 return NULL;
23042}
23043
02142a6c 23044/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23045 or return NULL if DIE does not have a saved type. */
23046
23047static struct type *
23048get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23049{
9c541725 23050 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23051}
23052
10b3939b
DJ
23053/* Add a dependence relationship from CU to REF_PER_CU. */
23054
23055static void
23056dwarf2_add_dependence (struct dwarf2_cu *cu,
23057 struct dwarf2_per_cu_data *ref_per_cu)
23058{
23059 void **slot;
23060
23061 if (cu->dependencies == NULL)
23062 cu->dependencies
23063 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23064 NULL, &cu->comp_unit_obstack,
23065 hashtab_obstack_allocate,
23066 dummy_obstack_deallocate);
23067
23068 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23069 if (*slot == NULL)
23070 *slot = ref_per_cu;
23071}
1c379e20 23072
f504f079
DE
23073/* Subroutine of dwarf2_mark to pass to htab_traverse.
23074 Set the mark field in every compilation unit in the
ae038cb0
DJ
23075 cache that we must keep because we are keeping CU. */
23076
10b3939b
DJ
23077static int
23078dwarf2_mark_helper (void **slot, void *data)
23079{
23080 struct dwarf2_per_cu_data *per_cu;
23081
23082 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23083
23084 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23085 reading of the chain. As such dependencies remain valid it is not much
23086 useful to track and undo them during QUIT cleanups. */
23087 if (per_cu->cu == NULL)
23088 return 1;
23089
10b3939b
DJ
23090 if (per_cu->cu->mark)
23091 return 1;
23092 per_cu->cu->mark = 1;
23093
23094 if (per_cu->cu->dependencies != NULL)
23095 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23096
23097 return 1;
23098}
23099
f504f079
DE
23100/* Set the mark field in CU and in every other compilation unit in the
23101 cache that we must keep because we are keeping CU. */
23102
ae038cb0
DJ
23103static void
23104dwarf2_mark (struct dwarf2_cu *cu)
23105{
23106 if (cu->mark)
23107 return;
23108 cu->mark = 1;
10b3939b
DJ
23109 if (cu->dependencies != NULL)
23110 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23111}
23112
23113static void
23114dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23115{
23116 while (per_cu)
23117 {
23118 per_cu->cu->mark = 0;
23119 per_cu = per_cu->cu->read_in_chain;
23120 }
72bf9492
DJ
23121}
23122
72bf9492
DJ
23123/* Trivial hash function for partial_die_info: the hash value of a DIE
23124 is its offset in .debug_info for this objfile. */
23125
23126static hashval_t
23127partial_die_hash (const void *item)
23128{
9a3c8263
SM
23129 const struct partial_die_info *part_die
23130 = (const struct partial_die_info *) item;
9a619af0 23131
9c541725 23132 return to_underlying (part_die->sect_off);
72bf9492
DJ
23133}
23134
23135/* Trivial comparison function for partial_die_info structures: two DIEs
23136 are equal if they have the same offset. */
23137
23138static int
23139partial_die_eq (const void *item_lhs, const void *item_rhs)
23140{
9a3c8263
SM
23141 const struct partial_die_info *part_die_lhs
23142 = (const struct partial_die_info *) item_lhs;
23143 const struct partial_die_info *part_die_rhs
23144 = (const struct partial_die_info *) item_rhs;
9a619af0 23145
9c541725 23146 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23147}
23148
b4f54984
DE
23149static struct cmd_list_element *set_dwarf_cmdlist;
23150static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23151
23152static void
b4f54984 23153set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23154{
b4f54984 23155 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23156 gdb_stdout);
ae038cb0
DJ
23157}
23158
23159static void
b4f54984 23160show_dwarf_cmd (char *args, int from_tty)
6e70227d 23161{
b4f54984 23162 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23163}
23164
4bf44c1c 23165/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23166
23167static void
c1bd65d0 23168dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23169{
9a3c8263 23170 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23171 int ix;
8b70b953 23172
626f2d1c
TT
23173 /* Make sure we don't accidentally use dwarf2_per_objfile while
23174 cleaning up. */
23175 dwarf2_per_objfile = NULL;
23176
59b0c7c1
JB
23177 for (ix = 0; ix < data->n_comp_units; ++ix)
23178 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23179
59b0c7c1 23180 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23181 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23182 data->all_type_units[ix]->per_cu.imported_symtabs);
23183 xfree (data->all_type_units);
95554aad 23184
8b70b953 23185 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23186
23187 if (data->dwo_files)
23188 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23189 if (data->dwp_file)
23190 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23191
23192 if (data->dwz_file && data->dwz_file->dwz_bfd)
23193 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23194}
23195
23196\f
ae2de4f8 23197/* The "save gdb-index" command. */
9291a0cd 23198
bc8f2430
JK
23199/* In-memory buffer to prepare data to be written later to a file. */
23200class data_buf
9291a0cd 23201{
bc8f2430 23202public:
bc8f2430
JK
23203 /* Copy DATA to the end of the buffer. */
23204 template<typename T>
23205 void append_data (const T &data)
23206 {
23207 std::copy (reinterpret_cast<const gdb_byte *> (&data),
23208 reinterpret_cast<const gdb_byte *> (&data + 1),
c2f134ac 23209 grow (sizeof (data)));
bc8f2430 23210 }
b89be57b 23211
c2f134ac
PA
23212 /* Copy CSTR (a zero-terminated string) to the end of buffer. The
23213 terminating zero is appended too. */
bc8f2430
JK
23214 void append_cstr0 (const char *cstr)
23215 {
23216 const size_t size = strlen (cstr) + 1;
c2f134ac
PA
23217 std::copy (cstr, cstr + size, grow (size));
23218 }
23219
23220 /* Accept a host-format integer in VAL and append it to the buffer
23221 as a target-format integer which is LEN bytes long. */
23222 void append_uint (size_t len, bfd_endian byte_order, ULONGEST val)
23223 {
23224 ::store_unsigned_integer (grow (len), len, byte_order, val);
bc8f2430 23225 }
9291a0cd 23226
bc8f2430
JK
23227 /* Return the size of the buffer. */
23228 size_t size () const
23229 {
23230 return m_vec.size ();
23231 }
23232
23233 /* Write the buffer to FILE. */
23234 void file_write (FILE *file) const
23235 {
a81e6d4d
PA
23236 if (::fwrite (m_vec.data (), 1, m_vec.size (), file) != m_vec.size ())
23237 error (_("couldn't write data to file"));
bc8f2430
JK
23238 }
23239
23240private:
c2f134ac
PA
23241 /* Grow SIZE bytes at the end of the buffer. Returns a pointer to
23242 the start of the new block. */
23243 gdb_byte *grow (size_t size)
23244 {
23245 m_vec.resize (m_vec.size () + size);
23246 return &*m_vec.end () - size;
23247 }
23248
d5722aa2 23249 gdb::byte_vector m_vec;
bc8f2430 23250};
9291a0cd
TT
23251
23252/* An entry in the symbol table. */
23253struct symtab_index_entry
23254{
23255 /* The name of the symbol. */
23256 const char *name;
23257 /* The offset of the name in the constant pool. */
23258 offset_type index_offset;
23259 /* A sorted vector of the indices of all the CUs that hold an object
23260 of this name. */
bc8f2430 23261 std::vector<offset_type> cu_indices;
9291a0cd
TT
23262};
23263
23264/* The symbol table. This is a power-of-2-sized hash table. */
23265struct mapped_symtab
23266{
bc8f2430
JK
23267 mapped_symtab ()
23268 {
23269 data.resize (1024);
23270 }
b89be57b 23271
bc8f2430 23272 offset_type n_elements = 0;
4b76cda9 23273 std::vector<symtab_index_entry> data;
bc8f2430 23274};
9291a0cd 23275
bc8f2430 23276/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to
559a7a62
JK
23277 the slot.
23278
23279 Function is used only during write_hash_table so no index format backward
23280 compatibility is needed. */
b89be57b 23281
4b76cda9 23282static symtab_index_entry &
9291a0cd
TT
23283find_slot (struct mapped_symtab *symtab, const char *name)
23284{
559a7a62 23285 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd 23286
bc8f2430
JK
23287 index = hash & (symtab->data.size () - 1);
23288 step = ((hash * 17) & (symtab->data.size () - 1)) | 1;
9291a0cd
TT
23289
23290 for (;;)
23291 {
4b76cda9
PA
23292 if (symtab->data[index].name == NULL
23293 || strcmp (name, symtab->data[index].name) == 0)
bc8f2430
JK
23294 return symtab->data[index];
23295 index = (index + step) & (symtab->data.size () - 1);
9291a0cd
TT
23296 }
23297}
23298
23299/* Expand SYMTAB's hash table. */
b89be57b 23300
9291a0cd
TT
23301static void
23302hash_expand (struct mapped_symtab *symtab)
23303{
bc8f2430 23304 auto old_entries = std::move (symtab->data);
9291a0cd 23305
bc8f2430
JK
23306 symtab->data.clear ();
23307 symtab->data.resize (old_entries.size () * 2);
9291a0cd 23308
bc8f2430 23309 for (auto &it : old_entries)
4b76cda9 23310 if (it.name != NULL)
bc8f2430 23311 {
4b76cda9 23312 auto &ref = find_slot (symtab, it.name);
bc8f2430
JK
23313 ref = std::move (it);
23314 }
9291a0cd
TT
23315}
23316
156942c7
DE
23317/* Add an entry to SYMTAB. NAME is the name of the symbol.
23318 CU_INDEX is the index of the CU in which the symbol appears.
23319 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23320
9291a0cd
TT
23321static void
23322add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23323 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23324 offset_type cu_index)
23325{
156942c7 23326 offset_type cu_index_and_attrs;
9291a0cd
TT
23327
23328 ++symtab->n_elements;
bc8f2430 23329 if (4 * symtab->n_elements / 3 >= symtab->data.size ())
9291a0cd
TT
23330 hash_expand (symtab);
23331
4b76cda9
PA
23332 symtab_index_entry &slot = find_slot (symtab, name);
23333 if (slot.name == NULL)
9291a0cd 23334 {
4b76cda9 23335 slot.name = name;
156942c7 23336 /* index_offset is set later. */
9291a0cd 23337 }
156942c7
DE
23338
23339 cu_index_and_attrs = 0;
23340 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23341 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23342 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23343
23344 /* We don't want to record an index value twice as we want to avoid the
23345 duplication.
23346 We process all global symbols and then all static symbols
23347 (which would allow us to avoid the duplication by only having to check
23348 the last entry pushed), but a symbol could have multiple kinds in one CU.
23349 To keep things simple we don't worry about the duplication here and
23350 sort and uniqufy the list after we've processed all symbols. */
4b76cda9 23351 slot.cu_indices.push_back (cu_index_and_attrs);
156942c7
DE
23352}
23353
23354/* Sort and remove duplicates of all symbols' cu_indices lists. */
23355
23356static void
23357uniquify_cu_indices (struct mapped_symtab *symtab)
23358{
4b76cda9 23359 for (auto &entry : symtab->data)
156942c7 23360 {
4b76cda9 23361 if (entry.name != NULL && !entry.cu_indices.empty ())
156942c7 23362 {
4b76cda9 23363 auto &cu_indices = entry.cu_indices;
6fd931f2
PA
23364 std::sort (cu_indices.begin (), cu_indices.end ());
23365 auto from = std::unique (cu_indices.begin (), cu_indices.end ());
23366 cu_indices.erase (from, cu_indices.end ());
156942c7
DE
23367 }
23368 }
9291a0cd
TT
23369}
23370
bc8f2430
JK
23371/* A form of 'const char *' suitable for container keys. Only the
23372 pointer is stored. The strings themselves are compared, not the
23373 pointers. */
23374class c_str_view
9291a0cd 23375{
bc8f2430
JK
23376public:
23377 c_str_view (const char *cstr)
23378 : m_cstr (cstr)
23379 {}
9291a0cd 23380
bc8f2430
JK
23381 bool operator== (const c_str_view &other) const
23382 {
23383 return strcmp (m_cstr, other.m_cstr) == 0;
23384 }
9291a0cd 23385
bc8f2430
JK
23386private:
23387 friend class c_str_view_hasher;
23388 const char *const m_cstr;
23389};
9291a0cd 23390
bc8f2430
JK
23391/* A std::unordered_map::hasher for c_str_view that uses the right
23392 hash function for strings in a mapped index. */
23393class c_str_view_hasher
23394{
23395public:
23396 size_t operator () (const c_str_view &x) const
23397 {
23398 return mapped_index_string_hash (INT_MAX, x.m_cstr);
23399 }
23400};
b89be57b 23401
bc8f2430
JK
23402/* A std::unordered_map::hasher for std::vector<>. */
23403template<typename T>
23404class vector_hasher
9291a0cd 23405{
bc8f2430
JK
23406public:
23407 size_t operator () (const std::vector<T> &key) const
23408 {
23409 return iterative_hash (key.data (),
23410 sizeof (key.front ()) * key.size (), 0);
23411 }
23412};
9291a0cd 23413
bc8f2430
JK
23414/* Write the mapped hash table SYMTAB to the data buffer OUTPUT, with
23415 constant pool entries going into the data buffer CPOOL. */
3876f04e 23416
bc8f2430
JK
23417static void
23418write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool)
23419{
23420 {
23421 /* Elements are sorted vectors of the indices of all the CUs that
23422 hold an object of this name. */
23423 std::unordered_map<std::vector<offset_type>, offset_type,
23424 vector_hasher<offset_type>>
23425 symbol_hash_table;
23426
23427 /* We add all the index vectors to the constant pool first, to
23428 ensure alignment is ok. */
4b76cda9 23429 for (symtab_index_entry &entry : symtab->data)
bc8f2430 23430 {
4b76cda9 23431 if (entry.name == NULL)
bc8f2430 23432 continue;
4b76cda9 23433 gdb_assert (entry.index_offset == 0);
70a1152b
PA
23434
23435 /* Finding before inserting is faster than always trying to
23436 insert, because inserting always allocates a node, does the
23437 lookup, and then destroys the new node if another node
23438 already had the same key. C++17 try_emplace will avoid
23439 this. */
23440 const auto found
4b76cda9 23441 = symbol_hash_table.find (entry.cu_indices);
70a1152b
PA
23442 if (found != symbol_hash_table.end ())
23443 {
4b76cda9 23444 entry.index_offset = found->second;
70a1152b
PA
23445 continue;
23446 }
23447
4b76cda9
PA
23448 symbol_hash_table.emplace (entry.cu_indices, cpool.size ());
23449 entry.index_offset = cpool.size ();
23450 cpool.append_data (MAYBE_SWAP (entry.cu_indices.size ()));
23451 for (const auto index : entry.cu_indices)
23452 cpool.append_data (MAYBE_SWAP (index));
bc8f2430
JK
23453 }
23454 }
9291a0cd
TT
23455
23456 /* Now write out the hash table. */
bc8f2430 23457 std::unordered_map<c_str_view, offset_type, c_str_view_hasher> str_table;
4b76cda9 23458 for (const auto &entry : symtab->data)
9291a0cd
TT
23459 {
23460 offset_type str_off, vec_off;
23461
4b76cda9 23462 if (entry.name != NULL)
9291a0cd 23463 {
4b76cda9 23464 const auto insertpair = str_table.emplace (entry.name, cpool.size ());
bc8f2430 23465 if (insertpair.second)
4b76cda9 23466 cpool.append_cstr0 (entry.name);
bc8f2430 23467 str_off = insertpair.first->second;
4b76cda9 23468 vec_off = entry.index_offset;
9291a0cd
TT
23469 }
23470 else
23471 {
23472 /* While 0 is a valid constant pool index, it is not valid
23473 to have 0 for both offsets. */
23474 str_off = 0;
23475 vec_off = 0;
23476 }
23477
bc8f2430
JK
23478 output.append_data (MAYBE_SWAP (str_off));
23479 output.append_data (MAYBE_SWAP (vec_off));
9291a0cd 23480 }
9291a0cd
TT
23481}
23482
bc8f2430 23483typedef std::unordered_map<partial_symtab *, unsigned int> psym_index_map;
0a5429f6
DE
23484
23485/* Helper struct for building the address table. */
23486struct addrmap_index_data
23487{
bc8f2430
JK
23488 addrmap_index_data (data_buf &addr_vec_, psym_index_map &cu_index_htab_)
23489 : addr_vec (addr_vec_), cu_index_htab (cu_index_htab_)
23490 {}
23491
0a5429f6 23492 struct objfile *objfile;
bc8f2430
JK
23493 data_buf &addr_vec;
23494 psym_index_map &cu_index_htab;
0a5429f6
DE
23495
23496 /* Non-zero if the previous_* fields are valid.
23497 We can't write an entry until we see the next entry (since it is only then
23498 that we know the end of the entry). */
23499 int previous_valid;
23500 /* Index of the CU in the table of all CUs in the index file. */
23501 unsigned int previous_cu_index;
0963b4bd 23502 /* Start address of the CU. */
0a5429f6
DE
23503 CORE_ADDR previous_cu_start;
23504};
23505
bc8f2430 23506/* Write an address entry to ADDR_VEC. */
b89be57b 23507
9291a0cd 23508static void
bc8f2430 23509add_address_entry (struct objfile *objfile, data_buf &addr_vec,
0a5429f6 23510 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23511{
9291a0cd
TT
23512 CORE_ADDR baseaddr;
23513
23514 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23515
c2f134ac
PA
23516 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, start - baseaddr);
23517 addr_vec.append_uint (8, BFD_ENDIAN_LITTLE, end - baseaddr);
bc8f2430 23518 addr_vec.append_data (MAYBE_SWAP (cu_index));
0a5429f6
DE
23519}
23520
23521/* Worker function for traversing an addrmap to build the address table. */
23522
23523static int
23524add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23525{
9a3c8263
SM
23526 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23527 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23528
23529 if (data->previous_valid)
bc8f2430 23530 add_address_entry (data->objfile, data->addr_vec,
0a5429f6
DE
23531 data->previous_cu_start, start_addr,
23532 data->previous_cu_index);
23533
23534 data->previous_cu_start = start_addr;
23535 if (pst != NULL)
23536 {
bc8f2430
JK
23537 const auto it = data->cu_index_htab.find (pst);
23538 gdb_assert (it != data->cu_index_htab.cend ());
23539 data->previous_cu_index = it->second;
0a5429f6
DE
23540 data->previous_valid = 1;
23541 }
23542 else
bc8f2430 23543 data->previous_valid = 0;
0a5429f6
DE
23544
23545 return 0;
23546}
23547
bc8f2430 23548/* Write OBJFILE's address map to ADDR_VEC.
0a5429f6
DE
23549 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23550 in the index file. */
23551
23552static void
bc8f2430
JK
23553write_address_map (struct objfile *objfile, data_buf &addr_vec,
23554 psym_index_map &cu_index_htab)
0a5429f6 23555{
bc8f2430 23556 struct addrmap_index_data addrmap_index_data (addr_vec, cu_index_htab);
0a5429f6
DE
23557
23558 /* When writing the address table, we have to cope with the fact that
23559 the addrmap iterator only provides the start of a region; we have to
23560 wait until the next invocation to get the start of the next region. */
23561
23562 addrmap_index_data.objfile = objfile;
0a5429f6
DE
23563 addrmap_index_data.previous_valid = 0;
23564
23565 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23566 &addrmap_index_data);
23567
23568 /* It's highly unlikely the last entry (end address = 0xff...ff)
23569 is valid, but we should still handle it.
23570 The end address is recorded as the start of the next region, but that
23571 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23572 anyway. */
23573 if (addrmap_index_data.previous_valid)
bc8f2430 23574 add_address_entry (objfile, addr_vec,
0a5429f6
DE
23575 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23576 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23577}
23578
156942c7
DE
23579/* Return the symbol kind of PSYM. */
23580
23581static gdb_index_symbol_kind
23582symbol_kind (struct partial_symbol *psym)
23583{
23584 domain_enum domain = PSYMBOL_DOMAIN (psym);
23585 enum address_class aclass = PSYMBOL_CLASS (psym);
23586
23587 switch (domain)
23588 {
23589 case VAR_DOMAIN:
23590 switch (aclass)
23591 {
23592 case LOC_BLOCK:
23593 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23594 case LOC_TYPEDEF:
23595 return GDB_INDEX_SYMBOL_KIND_TYPE;
23596 case LOC_COMPUTED:
23597 case LOC_CONST_BYTES:
23598 case LOC_OPTIMIZED_OUT:
23599 case LOC_STATIC:
23600 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23601 case LOC_CONST:
23602 /* Note: It's currently impossible to recognize psyms as enum values
23603 short of reading the type info. For now punt. */
23604 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23605 default:
23606 /* There are other LOC_FOO values that one might want to classify
23607 as variables, but dwarf2read.c doesn't currently use them. */
23608 return GDB_INDEX_SYMBOL_KIND_OTHER;
23609 }
23610 case STRUCT_DOMAIN:
23611 return GDB_INDEX_SYMBOL_KIND_TYPE;
23612 default:
23613 return GDB_INDEX_SYMBOL_KIND_OTHER;
23614 }
23615}
23616
9291a0cd 23617/* Add a list of partial symbols to SYMTAB. */
b89be57b 23618
9291a0cd
TT
23619static void
23620write_psymbols (struct mapped_symtab *symtab,
bc8f2430 23621 std::unordered_set<partial_symbol *> &psyms_seen,
9291a0cd
TT
23622 struct partial_symbol **psymp,
23623 int count,
987d643c
TT
23624 offset_type cu_index,
23625 int is_static)
9291a0cd
TT
23626{
23627 for (; count-- > 0; ++psymp)
23628 {
156942c7 23629 struct partial_symbol *psym = *psymp;
987d643c 23630
156942c7 23631 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23632 error (_("Ada is not currently supported by the index"));
987d643c 23633
987d643c 23634 /* Only add a given psymbol once. */
bc8f2430 23635 if (psyms_seen.insert (psym).second)
987d643c 23636 {
156942c7
DE
23637 gdb_index_symbol_kind kind = symbol_kind (psym);
23638
156942c7
DE
23639 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23640 is_static, kind, cu_index);
987d643c 23641 }
9291a0cd
TT
23642 }
23643}
23644
1fd400ff
TT
23645/* A helper struct used when iterating over debug_types. */
23646struct signatured_type_index_data
23647{
bc8f2430
JK
23648 signatured_type_index_data (data_buf &types_list_,
23649 std::unordered_set<partial_symbol *> &psyms_seen_)
23650 : types_list (types_list_), psyms_seen (psyms_seen_)
23651 {}
23652
1fd400ff
TT
23653 struct objfile *objfile;
23654 struct mapped_symtab *symtab;
bc8f2430
JK
23655 data_buf &types_list;
23656 std::unordered_set<partial_symbol *> &psyms_seen;
1fd400ff
TT
23657 int cu_index;
23658};
23659
23660/* A helper function that writes a single signatured_type to an
23661 obstack. */
b89be57b 23662
1fd400ff
TT
23663static int
23664write_one_signatured_type (void **slot, void *d)
23665{
9a3c8263
SM
23666 struct signatured_type_index_data *info
23667 = (struct signatured_type_index_data *) d;
1fd400ff 23668 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23669 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23670
23671 write_psymbols (info->symtab,
987d643c 23672 info->psyms_seen,
3e43a32a
MS
23673 info->objfile->global_psymbols.list
23674 + psymtab->globals_offset,
987d643c
TT
23675 psymtab->n_global_syms, info->cu_index,
23676 0);
1fd400ff 23677 write_psymbols (info->symtab,
987d643c 23678 info->psyms_seen,
3e43a32a
MS
23679 info->objfile->static_psymbols.list
23680 + psymtab->statics_offset,
987d643c
TT
23681 psymtab->n_static_syms, info->cu_index,
23682 1);
1fd400ff 23683
c2f134ac
PA
23684 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23685 to_underlying (entry->per_cu.sect_off));
23686 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE,
23687 to_underlying (entry->type_offset_in_tu));
23688 info->types_list.append_uint (8, BFD_ENDIAN_LITTLE, entry->signature);
1fd400ff
TT
23689
23690 ++info->cu_index;
23691
23692 return 1;
23693}
23694
e8f8bcb3
PA
23695/* Recurse into all "included" dependencies and count their symbols as
23696 if they appeared in this psymtab. */
23697
23698static void
23699recursively_count_psymbols (struct partial_symtab *psymtab,
23700 size_t &psyms_seen)
23701{
23702 for (int i = 0; i < psymtab->number_of_dependencies; ++i)
23703 if (psymtab->dependencies[i]->user != NULL)
23704 recursively_count_psymbols (psymtab->dependencies[i],
23705 psyms_seen);
23706
23707 psyms_seen += psymtab->n_global_syms;
23708 psyms_seen += psymtab->n_static_syms;
23709}
23710
95554aad
TT
23711/* Recurse into all "included" dependencies and write their symbols as
23712 if they appeared in this psymtab. */
23713
23714static void
23715recursively_write_psymbols (struct objfile *objfile,
23716 struct partial_symtab *psymtab,
23717 struct mapped_symtab *symtab,
bc8f2430 23718 std::unordered_set<partial_symbol *> &psyms_seen,
95554aad
TT
23719 offset_type cu_index)
23720{
23721 int i;
23722
23723 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23724 if (psymtab->dependencies[i]->user != NULL)
23725 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23726 symtab, psyms_seen, cu_index);
23727
23728 write_psymbols (symtab,
23729 psyms_seen,
23730 objfile->global_psymbols.list + psymtab->globals_offset,
23731 psymtab->n_global_syms, cu_index,
23732 0);
23733 write_psymbols (symtab,
23734 psyms_seen,
23735 objfile->static_psymbols.list + psymtab->statics_offset,
23736 psymtab->n_static_syms, cu_index,
23737 1);
23738}
23739
bc8f2430
JK
23740/* Closes FILE on scope exit. */
23741struct file_closer
23742{
23743 explicit file_closer (FILE *file)
23744 : m_file (file)
23745 {}
23746
23747 ~file_closer ()
23748 { fclose (m_file); }
23749
23750private:
23751 FILE *m_file;
23752};
23753
9291a0cd 23754/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23755
9291a0cd
TT
23756static void
23757write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23758{
9291a0cd
TT
23759 if (dwarf2_per_objfile->using_index)
23760 error (_("Cannot use an index to create the index"));
23761
8b70b953
TT
23762 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23763 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23764
260b681b
DE
23765 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23766 return;
23767
bc8f2430 23768 struct stat st;
4262abfb
JK
23769 if (stat (objfile_name (objfile), &st) < 0)
23770 perror_with_name (objfile_name (objfile));
9291a0cd 23771
bc8f2430
JK
23772 std::string filename (std::string (dir) + SLASH_STRING
23773 + lbasename (objfile_name (objfile)) + INDEX_SUFFIX);
9291a0cd 23774
bc8f2430 23775 FILE *out_file = gdb_fopen_cloexec (filename.c_str (), "wb");
9291a0cd 23776 if (!out_file)
bc8f2430 23777 error (_("Can't open `%s' for writing"), filename.c_str ());
9291a0cd 23778
bc8f2430
JK
23779 file_closer close_out_file (out_file);
23780 gdb::unlinker unlink_file (filename.c_str ());
9291a0cd 23781
bc8f2430
JK
23782 mapped_symtab symtab;
23783 data_buf cu_list;
987d643c 23784
0a5429f6
DE
23785 /* While we're scanning CU's create a table that maps a psymtab pointer
23786 (which is what addrmap records) to its index (which is what is recorded
23787 in the index file). This will later be needed to write the address
23788 table. */
bc8f2430
JK
23789 psym_index_map cu_index_htab;
23790 cu_index_htab.reserve (dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23791
23792 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23793 work here. Also, the debug_types entries do not appear in
23794 all_comp_units, but only in their own hash table. */
e8f8bcb3
PA
23795
23796 /* The psyms_seen set is potentially going to be largish (~40k
23797 elements when indexing a -g3 build of GDB itself). Estimate the
23798 number of elements in order to avoid too many rehashes, which
23799 require rebuilding buckets and thus many trips to
23800 malloc/free. */
23801 size_t psyms_count = 0;
23802 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23803 {
23804 struct dwarf2_per_cu_data *per_cu
23805 = dwarf2_per_objfile->all_comp_units[i];
23806 struct partial_symtab *psymtab = per_cu->v.psymtab;
23807
23808 if (psymtab != NULL && psymtab->user == NULL)
23809 recursively_count_psymbols (psymtab, psyms_count);
23810 }
23811 /* Generating an index for gdb itself shows a ratio of
23812 TOTAL_SEEN_SYMS/UNIQUE_SYMS or ~5. 4 seems like a good bet. */
23813 std::unordered_set<partial_symbol *> psyms_seen (psyms_count / 4);
bc8f2430 23814 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd 23815 {
3e43a32a
MS
23816 struct dwarf2_per_cu_data *per_cu
23817 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23818 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23819
92fac807
JK
23820 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23821 It may be referenced from a local scope but in such case it does not
23822 need to be present in .gdb_index. */
23823 if (psymtab == NULL)
23824 continue;
23825
95554aad 23826 if (psymtab->user == NULL)
bc8f2430
JK
23827 recursively_write_psymbols (objfile, psymtab, &symtab,
23828 psyms_seen, i);
9291a0cd 23829
bc8f2430
JK
23830 const auto insertpair = cu_index_htab.emplace (psymtab, i);
23831 gdb_assert (insertpair.second);
9291a0cd 23832
c2f134ac
PA
23833 cu_list.append_uint (8, BFD_ENDIAN_LITTLE,
23834 to_underlying (per_cu->sect_off));
23835 cu_list.append_uint (8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23836 }
23837
0a5429f6 23838 /* Dump the address map. */
bc8f2430
JK
23839 data_buf addr_vec;
23840 write_address_map (objfile, addr_vec, cu_index_htab);
0a5429f6 23841
1fd400ff 23842 /* Write out the .debug_type entries, if any. */
bc8f2430 23843 data_buf types_cu_list;
1fd400ff
TT
23844 if (dwarf2_per_objfile->signatured_types)
23845 {
bc8f2430
JK
23846 signatured_type_index_data sig_data (types_cu_list,
23847 psyms_seen);
1fd400ff
TT
23848
23849 sig_data.objfile = objfile;
bc8f2430 23850 sig_data.symtab = &symtab;
1fd400ff
TT
23851 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23852 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23853 write_one_signatured_type, &sig_data);
23854 }
23855
156942c7
DE
23856 /* Now that we've processed all symbols we can shrink their cu_indices
23857 lists. */
bc8f2430 23858 uniquify_cu_indices (&symtab);
156942c7 23859
bc8f2430
JK
23860 data_buf symtab_vec, constant_pool;
23861 write_hash_table (&symtab, symtab_vec, constant_pool);
9291a0cd 23862
bc8f2430
JK
23863 data_buf contents;
23864 const offset_type size_of_contents = 6 * sizeof (offset_type);
23865 offset_type total_len = size_of_contents;
9291a0cd
TT
23866
23867 /* The version number. */
bc8f2430 23868 contents.append_data (MAYBE_SWAP (8));
9291a0cd
TT
23869
23870 /* The offset of the CU list from the start of the file. */
bc8f2430
JK
23871 contents.append_data (MAYBE_SWAP (total_len));
23872 total_len += cu_list.size ();
9291a0cd 23873
1fd400ff 23874 /* The offset of the types CU list from the start of the file. */
bc8f2430
JK
23875 contents.append_data (MAYBE_SWAP (total_len));
23876 total_len += types_cu_list.size ();
1fd400ff 23877
9291a0cd 23878 /* The offset of the address table from the start of the file. */
bc8f2430
JK
23879 contents.append_data (MAYBE_SWAP (total_len));
23880 total_len += addr_vec.size ();
9291a0cd
TT
23881
23882 /* The offset of the symbol table from the start of the file. */
bc8f2430
JK
23883 contents.append_data (MAYBE_SWAP (total_len));
23884 total_len += symtab_vec.size ();
9291a0cd
TT
23885
23886 /* The offset of the constant pool from the start of the file. */
bc8f2430
JK
23887 contents.append_data (MAYBE_SWAP (total_len));
23888 total_len += constant_pool.size ();
9291a0cd 23889
bc8f2430 23890 gdb_assert (contents.size () == size_of_contents);
9291a0cd 23891
bc8f2430
JK
23892 contents.file_write (out_file);
23893 cu_list.file_write (out_file);
23894 types_cu_list.file_write (out_file);
23895 addr_vec.file_write (out_file);
23896 symtab_vec.file_write (out_file);
23897 constant_pool.file_write (out_file);
9291a0cd 23898
bef155c3
TT
23899 /* We want to keep the file. */
23900 unlink_file.keep ();
9291a0cd
TT
23901}
23902
90476074
TT
23903/* Implementation of the `save gdb-index' command.
23904
23905 Note that the file format used by this command is documented in the
23906 GDB manual. Any changes here must be documented there. */
11570e71 23907
9291a0cd
TT
23908static void
23909save_gdb_index_command (char *arg, int from_tty)
23910{
23911 struct objfile *objfile;
23912
23913 if (!arg || !*arg)
96d19272 23914 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23915
23916 ALL_OBJFILES (objfile)
23917 {
23918 struct stat st;
23919
23920 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23921 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23922 continue;
23923
9a3c8263
SM
23924 dwarf2_per_objfile
23925 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23926 dwarf2_objfile_data_key);
9291a0cd
TT
23927 if (dwarf2_per_objfile)
23928 {
9291a0cd 23929
492d29ea 23930 TRY
9291a0cd
TT
23931 {
23932 write_psymtabs_to_index (objfile, arg);
23933 }
492d29ea
PA
23934 CATCH (except, RETURN_MASK_ERROR)
23935 {
23936 exception_fprintf (gdb_stderr, except,
23937 _("Error while writing index for `%s': "),
23938 objfile_name (objfile));
23939 }
23940 END_CATCH
9291a0cd
TT
23941 }
23942 }
dce234bc
PP
23943}
23944
9291a0cd
TT
23945\f
23946
b4f54984 23947int dwarf_always_disassemble;
9eae7c52
TT
23948
23949static void
b4f54984
DE
23950show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23951 struct cmd_list_element *c, const char *value)
9eae7c52 23952{
3e43a32a
MS
23953 fprintf_filtered (file,
23954 _("Whether to always disassemble "
23955 "DWARF expressions is %s.\n"),
9eae7c52
TT
23956 value);
23957}
23958
900e11f9
JK
23959static void
23960show_check_physname (struct ui_file *file, int from_tty,
23961 struct cmd_list_element *c, const char *value)
23962{
23963 fprintf_filtered (file,
23964 _("Whether to check \"physname\" is %s.\n"),
23965 value);
23966}
23967
6502dd73
DJ
23968void _initialize_dwarf2_read (void);
23969
23970void
23971_initialize_dwarf2_read (void)
23972{
96d19272
JK
23973 struct cmd_list_element *c;
23974
dce234bc 23975 dwarf2_objfile_data_key
c1bd65d0 23976 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 23977
b4f54984
DE
23978 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23979Set DWARF specific variables.\n\
23980Configure DWARF variables such as the cache size"),
23981 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
23982 0/*allow-unknown*/, &maintenance_set_cmdlist);
23983
b4f54984
DE
23984 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23985Show DWARF specific variables\n\
23986Show DWARF variables such as the cache size"),
23987 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
23988 0/*allow-unknown*/, &maintenance_show_cmdlist);
23989
23990 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
23991 &dwarf_max_cache_age, _("\
23992Set the upper bound on the age of cached DWARF compilation units."), _("\
23993Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
23994A higher limit means that cached compilation units will be stored\n\
23995in memory longer, and more total memory will be used. Zero disables\n\
23996caching, which can slow down startup."),
2c5b56ce 23997 NULL,
b4f54984
DE
23998 show_dwarf_max_cache_age,
23999 &set_dwarf_cmdlist,
24000 &show_dwarf_cmdlist);
d97bc12b 24001
9eae7c52 24002 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24003 &dwarf_always_disassemble, _("\
9eae7c52
TT
24004Set whether `info address' always disassembles DWARF expressions."), _("\
24005Show whether `info address' always disassembles DWARF expressions."), _("\
24006When enabled, DWARF expressions are always printed in an assembly-like\n\
24007syntax. When disabled, expressions will be printed in a more\n\
24008conversational style, when possible."),
24009 NULL,
b4f54984
DE
24010 show_dwarf_always_disassemble,
24011 &set_dwarf_cmdlist,
24012 &show_dwarf_cmdlist);
24013
24014 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24015Set debugging of the DWARF reader."), _("\
24016Show debugging of the DWARF reader."), _("\
24017When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24018reading and symtab expansion. A value of 1 (one) provides basic\n\
24019information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24020 NULL,
24021 NULL,
24022 &setdebuglist, &showdebuglist);
24023
b4f54984
DE
24024 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24025Set debugging of the DWARF DIE reader."), _("\
24026Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24027When enabled (non-zero), DIEs are dumped after they are read in.\n\
24028The value is the maximum depth to print."),
ccce17b0
YQ
24029 NULL,
24030 NULL,
24031 &setdebuglist, &showdebuglist);
9291a0cd 24032
27e0867f
DE
24033 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24034Set debugging of the dwarf line reader."), _("\
24035Show debugging of the dwarf line reader."), _("\
24036When enabled (non-zero), line number entries are dumped as they are read in.\n\
24037A value of 1 (one) provides basic information.\n\
24038A value greater than 1 provides more verbose information."),
24039 NULL,
24040 NULL,
24041 &setdebuglist, &showdebuglist);
24042
900e11f9
JK
24043 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24044Set cross-checking of \"physname\" code against demangler."), _("\
24045Show cross-checking of \"physname\" code against demangler."), _("\
24046When enabled, GDB's internal \"physname\" code is checked against\n\
24047the demangler."),
24048 NULL, show_check_physname,
24049 &setdebuglist, &showdebuglist);
24050
e615022a
DE
24051 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24052 no_class, &use_deprecated_index_sections, _("\
24053Set whether to use deprecated gdb_index sections."), _("\
24054Show whether to use deprecated gdb_index sections."), _("\
24055When enabled, deprecated .gdb_index sections are used anyway.\n\
24056Normally they are ignored either because of a missing feature or\n\
24057performance issue.\n\
24058Warning: This option must be enabled before gdb reads the file."),
24059 NULL,
24060 NULL,
24061 &setlist, &showlist);
24062
96d19272 24063 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24064 _("\
fc1a9d6e 24065Save a gdb-index file.\n\
11570e71 24066Usage: save gdb-index DIRECTORY"),
96d19272
JK
24067 &save_cmdlist);
24068 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24069
24070 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24071 &dwarf2_locexpr_funcs);
24072 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24073 &dwarf2_loclist_funcs);
24074
24075 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24076 &dwarf2_block_frame_base_locexpr_funcs);
24077 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24078 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24079}
This page took 4.079721 seconds and 4 git commands to generate.