Implement printing of rvalue reference types and values
[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"
4c2df51b 74
c906108c 75#include <fcntl.h>
c906108c 76#include <sys/types.h>
325fac50 77#include <algorithm>
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
73be47f5
DE
82/* When == 1, print basic high level tracing messages.
83 When > 1, be more verbose.
b4f54984
DE
84 This is in contrast to the low level DIE reading of dwarf_die_debug. */
85static unsigned int dwarf_read_debug = 0;
45cfd468 86
d97bc12b 87/* When non-zero, dump DIEs after they are read in. */
b4f54984 88static unsigned int dwarf_die_debug = 0;
d97bc12b 89
27e0867f
DE
90/* When non-zero, dump line number entries as they are read in. */
91static unsigned int dwarf_line_debug = 0;
92
900e11f9
JK
93/* When non-zero, cross-check physname against demangler. */
94static int check_physname = 0;
95
481860b3 96/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 97static int use_deprecated_index_sections = 0;
481860b3 98
6502dd73
DJ
99static const struct objfile_data *dwarf2_objfile_data_key;
100
f1e6e072
TT
101/* The "aclass" indices for various kinds of computed DWARF symbols. */
102
103static int dwarf2_locexpr_index;
104static int dwarf2_loclist_index;
105static int dwarf2_locexpr_block_index;
106static int dwarf2_loclist_block_index;
107
73869dc2
DE
108/* A descriptor for dwarf sections.
109
110 S.ASECTION, SIZE are typically initialized when the objfile is first
111 scanned. BUFFER, READIN are filled in later when the section is read.
112 If the section contained compressed data then SIZE is updated to record
113 the uncompressed size of the section.
114
115 DWP file format V2 introduces a wrinkle that is easiest to handle by
116 creating the concept of virtual sections contained within a real section.
117 In DWP V2 the sections of the input DWO files are concatenated together
118 into one section, but section offsets are kept relative to the original
119 input section.
120 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
121 the real section this "virtual" section is contained in, and BUFFER,SIZE
122 describe the virtual section. */
123
dce234bc
PP
124struct dwarf2_section_info
125{
73869dc2
DE
126 union
127 {
e5aa3347 128 /* If this is a real section, the bfd section. */
049412e3 129 asection *section;
73869dc2 130 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 131 section. */
73869dc2
DE
132 struct dwarf2_section_info *containing_section;
133 } s;
19ac8c2e 134 /* Pointer to section data, only valid if readin. */
d521ce57 135 const gdb_byte *buffer;
73869dc2 136 /* The size of the section, real or virtual. */
dce234bc 137 bfd_size_type size;
73869dc2
DE
138 /* If this is a virtual section, the offset in the real section.
139 Only valid if is_virtual. */
140 bfd_size_type virtual_offset;
be391dca 141 /* True if we have tried to read this section. */
73869dc2
DE
142 char readin;
143 /* True if this is a virtual section, False otherwise.
049412e3 144 This specifies which of s.section and s.containing_section to use. */
73869dc2 145 char is_virtual;
dce234bc
PP
146};
147
8b70b953
TT
148typedef struct dwarf2_section_info dwarf2_section_info_def;
149DEF_VEC_O (dwarf2_section_info_def);
150
9291a0cd
TT
151/* All offsets in the index are of this type. It must be
152 architecture-independent. */
153typedef uint32_t offset_type;
154
155DEF_VEC_I (offset_type);
156
156942c7
DE
157/* Ensure only legit values are used. */
158#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
159 do { \
160 gdb_assert ((unsigned int) (value) <= 1); \
161 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
162 } while (0)
163
164/* Ensure only legit values are used. */
165#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
166 do { \
167 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
168 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
169 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172/* Ensure we don't use more than the alloted nuber of bits for the CU. */
173#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
176 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
177 } while (0)
178
9291a0cd
TT
179/* A description of the mapped index. The file format is described in
180 a comment by the code that writes the index. */
181struct mapped_index
182{
559a7a62
JK
183 /* Index data format version. */
184 int version;
185
9291a0cd
TT
186 /* The total length of the buffer. */
187 off_t total_size;
b11b1f88 188
9291a0cd
TT
189 /* A pointer to the address table data. */
190 const gdb_byte *address_table;
b11b1f88 191
9291a0cd
TT
192 /* Size of the address table data in bytes. */
193 offset_type address_table_size;
b11b1f88 194
3876f04e
DE
195 /* The symbol table, implemented as a hash table. */
196 const offset_type *symbol_table;
b11b1f88 197
9291a0cd 198 /* Size in slots, each slot is 2 offset_types. */
3876f04e 199 offset_type symbol_table_slots;
b11b1f88 200
9291a0cd
TT
201 /* A pointer to the constant pool. */
202 const char *constant_pool;
203};
204
95554aad
TT
205typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
206DEF_VEC_P (dwarf2_per_cu_ptr);
207
52059ffd
TT
208struct tu_stats
209{
210 int nr_uniq_abbrev_tables;
211 int nr_symtabs;
212 int nr_symtab_sharers;
213 int nr_stmt_less_type_units;
214 int nr_all_type_units_reallocs;
215};
216
9cdd5dbd
DE
217/* Collection of data recorded per objfile.
218 This hangs off of dwarf2_objfile_data_key. */
219
6502dd73
DJ
220struct dwarf2_per_objfile
221{
dce234bc
PP
222 struct dwarf2_section_info info;
223 struct dwarf2_section_info abbrev;
224 struct dwarf2_section_info line;
dce234bc 225 struct dwarf2_section_info loc;
43988095 226 struct dwarf2_section_info loclists;
dce234bc 227 struct dwarf2_section_info macinfo;
cf2c3c16 228 struct dwarf2_section_info macro;
dce234bc 229 struct dwarf2_section_info str;
43988095 230 struct dwarf2_section_info line_str;
dce234bc 231 struct dwarf2_section_info ranges;
43988095 232 struct dwarf2_section_info rnglists;
3019eac3 233 struct dwarf2_section_info addr;
dce234bc
PP
234 struct dwarf2_section_info frame;
235 struct dwarf2_section_info eh_frame;
9291a0cd 236 struct dwarf2_section_info gdb_index;
ae038cb0 237
8b70b953
TT
238 VEC (dwarf2_section_info_def) *types;
239
be391dca
TT
240 /* Back link. */
241 struct objfile *objfile;
242
d467dd73 243 /* Table of all the compilation units. This is used to locate
10b3939b 244 the target compilation unit of a particular reference. */
ae038cb0
DJ
245 struct dwarf2_per_cu_data **all_comp_units;
246
247 /* The number of compilation units in ALL_COMP_UNITS. */
248 int n_comp_units;
249
1fd400ff 250 /* The number of .debug_types-related CUs. */
d467dd73 251 int n_type_units;
1fd400ff 252
6aa5f3a6
DE
253 /* The number of elements allocated in all_type_units.
254 If there are skeleton-less TUs, we add them to all_type_units lazily. */
255 int n_allocated_type_units;
256
a2ce51a0
DE
257 /* The .debug_types-related CUs (TUs).
258 This is stored in malloc space because we may realloc it. */
b4dd5633 259 struct signatured_type **all_type_units;
1fd400ff 260
f4dc4d17
DE
261 /* Table of struct type_unit_group objects.
262 The hash key is the DW_AT_stmt_list value. */
263 htab_t type_unit_groups;
72dca2f5 264
348e048f
DE
265 /* A table mapping .debug_types signatures to its signatured_type entry.
266 This is NULL if the .debug_types section hasn't been read in yet. */
267 htab_t signatured_types;
268
f4dc4d17
DE
269 /* Type unit statistics, to see how well the scaling improvements
270 are doing. */
52059ffd 271 struct tu_stats tu_stats;
f4dc4d17
DE
272
273 /* A chain of compilation units that are currently read in, so that
274 they can be freed later. */
275 struct dwarf2_per_cu_data *read_in_chain;
276
3019eac3
DE
277 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
278 This is NULL if the table hasn't been allocated yet. */
279 htab_t dwo_files;
280
80626a55
DE
281 /* Non-zero if we've check for whether there is a DWP file. */
282 int dwp_checked;
283
284 /* The DWP file if there is one, or NULL. */
285 struct dwp_file *dwp_file;
286
36586728
TT
287 /* The shared '.dwz' file, if one exists. This is used when the
288 original data was compressed using 'dwz -m'. */
289 struct dwz_file *dwz_file;
290
72dca2f5
FR
291 /* A flag indicating wether this objfile has a section loaded at a
292 VMA of 0. */
293 int has_section_at_zero;
9291a0cd 294
ae2de4f8
DE
295 /* True if we are using the mapped index,
296 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
297 unsigned char using_index;
298
ae2de4f8 299 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 300 struct mapped_index *index_table;
98bfdba5 301
7b9f3c50 302 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
303 TUs typically share line table entries with a CU, so we maintain a
304 separate table of all line table entries to support the sharing.
305 Note that while there can be way more TUs than CUs, we've already
306 sorted all the TUs into "type unit groups", grouped by their
307 DW_AT_stmt_list value. Therefore the only sharing done here is with a
308 CU and its associated TU group if there is one. */
7b9f3c50
DE
309 htab_t quick_file_names_table;
310
98bfdba5
PA
311 /* Set during partial symbol reading, to prevent queueing of full
312 symbols. */
313 int reading_partial_symbols;
673bfd45 314
dee91e82 315 /* Table mapping type DIEs to their struct type *.
673bfd45 316 This is NULL if not allocated yet.
02142a6c 317 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 318 htab_t die_type_hash;
95554aad
TT
319
320 /* The CUs we recently read. */
321 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
322
323 /* Table containing line_header indexed by offset and offset_in_dwz. */
324 htab_t line_header_hash;
6502dd73
DJ
325};
326
327static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 328
251d32d9 329/* Default names of the debugging sections. */
c906108c 330
233a11ab
CS
331/* Note that if the debugging section has been compressed, it might
332 have a name like .zdebug_info. */
333
9cdd5dbd
DE
334static const struct dwarf2_debug_sections dwarf2_elf_names =
335{
251d32d9
TG
336 { ".debug_info", ".zdebug_info" },
337 { ".debug_abbrev", ".zdebug_abbrev" },
338 { ".debug_line", ".zdebug_line" },
339 { ".debug_loc", ".zdebug_loc" },
43988095 340 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 341 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 342 { ".debug_macro", ".zdebug_macro" },
251d32d9 343 { ".debug_str", ".zdebug_str" },
43988095 344 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 345 { ".debug_ranges", ".zdebug_ranges" },
43988095 346 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 347 { ".debug_types", ".zdebug_types" },
3019eac3 348 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
349 { ".debug_frame", ".zdebug_frame" },
350 { ".eh_frame", NULL },
24d3216f
TT
351 { ".gdb_index", ".zgdb_index" },
352 23
251d32d9 353};
c906108c 354
80626a55 355/* List of DWO/DWP sections. */
3019eac3 356
80626a55 357static const struct dwop_section_names
3019eac3
DE
358{
359 struct dwarf2_section_names abbrev_dwo;
360 struct dwarf2_section_names info_dwo;
361 struct dwarf2_section_names line_dwo;
362 struct dwarf2_section_names loc_dwo;
43988095 363 struct dwarf2_section_names loclists_dwo;
09262596
DE
364 struct dwarf2_section_names macinfo_dwo;
365 struct dwarf2_section_names macro_dwo;
3019eac3
DE
366 struct dwarf2_section_names str_dwo;
367 struct dwarf2_section_names str_offsets_dwo;
368 struct dwarf2_section_names types_dwo;
80626a55
DE
369 struct dwarf2_section_names cu_index;
370 struct dwarf2_section_names tu_index;
3019eac3 371}
80626a55 372dwop_section_names =
3019eac3
DE
373{
374 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
375 { ".debug_info.dwo", ".zdebug_info.dwo" },
376 { ".debug_line.dwo", ".zdebug_line.dwo" },
377 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 378 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
379 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
380 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
381 { ".debug_str.dwo", ".zdebug_str.dwo" },
382 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
383 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
384 { ".debug_cu_index", ".zdebug_cu_index" },
385 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
386};
387
c906108c
SS
388/* local data types */
389
107d2387
AC
390/* The data in a compilation unit header, after target2host
391 translation, looks like this. */
c906108c 392struct comp_unit_head
a738430d 393{
c764a876 394 unsigned int length;
a738430d 395 short version;
a738430d
MK
396 unsigned char addr_size;
397 unsigned char signed_addr_p;
b64f50a1 398 sect_offset abbrev_offset;
57349743 399
a738430d
MK
400 /* Size of file offsets; either 4 or 8. */
401 unsigned int offset_size;
57349743 402
a738430d
MK
403 /* Size of the length field; either 4 or 12. */
404 unsigned int initial_length_size;
57349743 405
43988095
JK
406 enum dwarf_unit_type unit_type;
407
a738430d
MK
408 /* Offset to the first byte of this compilation unit header in the
409 .debug_info section, for resolving relative reference dies. */
b64f50a1 410 sect_offset offset;
57349743 411
d00adf39
DE
412 /* Offset to first die in this cu from the start of the cu.
413 This will be the first byte following the compilation unit header. */
b64f50a1 414 cu_offset first_die_offset;
43988095
JK
415
416 /* 64-bit signature of this type unit - it is valid only for
417 UNIT_TYPE DW_UT_type. */
418 ULONGEST signature;
419
420 /* For types, offset in the type's DIE of the type defined by this TU. */
421 cu_offset type_offset_in_tu;
a738430d 422};
c906108c 423
3da10d80
KS
424/* Type used for delaying computation of method physnames.
425 See comments for compute_delayed_physnames. */
426struct delayed_method_info
427{
428 /* The type to which the method is attached, i.e., its parent class. */
429 struct type *type;
430
431 /* The index of the method in the type's function fieldlists. */
432 int fnfield_index;
433
434 /* The index of the method in the fieldlist. */
435 int index;
436
437 /* The name of the DIE. */
438 const char *name;
439
440 /* The DIE associated with this method. */
441 struct die_info *die;
442};
443
444typedef struct delayed_method_info delayed_method_info;
445DEF_VEC_O (delayed_method_info);
446
e7c27a73
DJ
447/* Internal state when decoding a particular compilation unit. */
448struct dwarf2_cu
449{
450 /* The objfile containing this compilation unit. */
451 struct objfile *objfile;
452
d00adf39 453 /* The header of the compilation unit. */
e7c27a73 454 struct comp_unit_head header;
e142c38c 455
d00adf39
DE
456 /* Base address of this compilation unit. */
457 CORE_ADDR base_address;
458
459 /* Non-zero if base_address has been set. */
460 int base_known;
461
e142c38c
DJ
462 /* The language we are debugging. */
463 enum language language;
464 const struct language_defn *language_defn;
465
b0f35d58
DL
466 const char *producer;
467
e142c38c
DJ
468 /* The generic symbol table building routines have separate lists for
469 file scope symbols and all all other scopes (local scopes). So
470 we need to select the right one to pass to add_symbol_to_list().
471 We do it by keeping a pointer to the correct list in list_in_scope.
472
473 FIXME: The original dwarf code just treated the file scope as the
474 first local scope, and all other local scopes as nested local
475 scopes, and worked fine. Check to see if we really need to
476 distinguish these in buildsym.c. */
477 struct pending **list_in_scope;
478
433df2d4
DE
479 /* The abbrev table for this CU.
480 Normally this points to the abbrev table in the objfile.
481 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
482 struct abbrev_table *abbrev_table;
72bf9492 483
b64f50a1
JK
484 /* Hash table holding all the loaded partial DIEs
485 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
486 htab_t partial_dies;
487
488 /* Storage for things with the same lifetime as this read-in compilation
489 unit, including partial DIEs. */
490 struct obstack comp_unit_obstack;
491
ae038cb0
DJ
492 /* When multiple dwarf2_cu structures are living in memory, this field
493 chains them all together, so that they can be released efficiently.
494 We will probably also want a generation counter so that most-recently-used
495 compilation units are cached... */
496 struct dwarf2_per_cu_data *read_in_chain;
497
69d751e3 498 /* Backlink to our per_cu entry. */
ae038cb0
DJ
499 struct dwarf2_per_cu_data *per_cu;
500
501 /* How many compilation units ago was this CU last referenced? */
502 int last_used;
503
b64f50a1
JK
504 /* A hash table of DIE cu_offset for following references with
505 die_info->offset.sect_off as hash. */
51545339 506 htab_t die_hash;
10b3939b
DJ
507
508 /* Full DIEs if read in. */
509 struct die_info *dies;
510
511 /* A set of pointers to dwarf2_per_cu_data objects for compilation
512 units referenced by this one. Only set during full symbol processing;
513 partial symbol tables do not have dependencies. */
514 htab_t dependencies;
515
cb1df416
DJ
516 /* Header data from the line table, during full symbol processing. */
517 struct line_header *line_header;
518
3da10d80
KS
519 /* A list of methods which need to have physnames computed
520 after all type information has been read. */
521 VEC (delayed_method_info) *method_list;
522
96408a79
SA
523 /* To be copied to symtab->call_site_htab. */
524 htab_t call_site_htab;
525
034e5797
DE
526 /* Non-NULL if this CU came from a DWO file.
527 There is an invariant here that is important to remember:
528 Except for attributes copied from the top level DIE in the "main"
529 (or "stub") file in preparation for reading the DWO file
530 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
531 Either there isn't a DWO file (in which case this is NULL and the point
532 is moot), or there is and either we're not going to read it (in which
533 case this is NULL) or there is and we are reading it (in which case this
534 is non-NULL). */
3019eac3
DE
535 struct dwo_unit *dwo_unit;
536
537 /* The DW_AT_addr_base attribute if present, zero otherwise
538 (zero is a valid value though).
1dbab08b 539 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
540 ULONGEST addr_base;
541
2e3cf129
DE
542 /* The DW_AT_ranges_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.
2e3cf129 545 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
546 be used without needing to know whether DWO files are in use or not.
547 N.B. This does not apply to DW_AT_ranges appearing in
548 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
549 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
550 DW_AT_ranges_base *would* have to be applied, and we'd have to care
551 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
552 ULONGEST ranges_base;
553
ae038cb0
DJ
554 /* Mark used when releasing cached dies. */
555 unsigned int mark : 1;
556
8be455d7
JK
557 /* This CU references .debug_loc. See the symtab->locations_valid field.
558 This test is imperfect as there may exist optimized debug code not using
559 any location list and still facing inlining issues if handled as
560 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 561 unsigned int has_loclist : 1;
ba919b58 562
1b80a9fa
JK
563 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
564 if all the producer_is_* fields are valid. This information is cached
565 because profiling CU expansion showed excessive time spent in
566 producer_is_gxx_lt_4_6. */
ba919b58
TT
567 unsigned int checked_producer : 1;
568 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 569 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 570 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
571
572 /* When set, the file that we're processing is known to have
573 debugging info for C++ namespaces. GCC 3.3.x did not produce
574 this information, but later versions do. */
575
576 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
577};
578
10b3939b
DJ
579/* Persistent data held for a compilation unit, even when not
580 processing it. We put a pointer to this structure in the
28dee7f5 581 read_symtab_private field of the psymtab. */
10b3939b 582
ae038cb0
DJ
583struct dwarf2_per_cu_data
584{
36586728 585 /* The start offset and length of this compilation unit.
45452591 586 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
587 initial_length_size.
588 If the DIE refers to a DWO file, this is always of the original die,
589 not the DWO file. */
b64f50a1 590 sect_offset offset;
36586728 591 unsigned int length;
ae038cb0 592
43988095
JK
593 /* DWARF standard version this data has been read from (such as 4 or 5). */
594 short dwarf_version;
595
ae038cb0
DJ
596 /* Flag indicating this compilation unit will be read in before
597 any of the current compilation units are processed. */
c764a876 598 unsigned int queued : 1;
ae038cb0 599
0d99eb77
DE
600 /* This flag will be set when reading partial DIEs if we need to load
601 absolutely all DIEs for this compilation unit, instead of just the ones
602 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
603 hash table and don't find it. */
604 unsigned int load_all_dies : 1;
605
0186c6a7
DE
606 /* Non-zero if this CU is from .debug_types.
607 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
608 this is non-zero. */
3019eac3
DE
609 unsigned int is_debug_types : 1;
610
36586728
TT
611 /* Non-zero if this CU is from the .dwz file. */
612 unsigned int is_dwz : 1;
613
a2ce51a0
DE
614 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
615 This flag is only valid if is_debug_types is true.
616 We can't read a CU directly from a DWO file: There are required
617 attributes in the stub. */
618 unsigned int reading_dwo_directly : 1;
619
7ee85ab1
DE
620 /* Non-zero if the TU has been read.
621 This is used to assist the "Stay in DWO Optimization" for Fission:
622 When reading a DWO, it's faster to read TUs from the DWO instead of
623 fetching them from random other DWOs (due to comdat folding).
624 If the TU has already been read, the optimization is unnecessary
625 (and unwise - we don't want to change where gdb thinks the TU lives
626 "midflight").
627 This flag is only valid if is_debug_types is true. */
628 unsigned int tu_read : 1;
629
3019eac3
DE
630 /* The section this CU/TU lives in.
631 If the DIE refers to a DWO file, this is always the original die,
632 not the DWO file. */
8a0459fd 633 struct dwarf2_section_info *section;
348e048f 634
17ea53c3 635 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
636 of the CU cache it gets reset to NULL again. This is left as NULL for
637 dummy CUs (a CU header, but nothing else). */
ae038cb0 638 struct dwarf2_cu *cu;
1c379e20 639
9cdd5dbd
DE
640 /* The corresponding objfile.
641 Normally we can get the objfile from dwarf2_per_objfile.
642 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
643 struct objfile *objfile;
644
fffbe6a8
YQ
645 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
646 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
647 union
648 {
649 /* The partial symbol table associated with this compilation unit,
95554aad 650 or NULL for unread partial units. */
9291a0cd
TT
651 struct partial_symtab *psymtab;
652
653 /* Data needed by the "quick" functions. */
654 struct dwarf2_per_cu_quick_data *quick;
655 } v;
95554aad 656
796a7ff8
DE
657 /* The CUs we import using DW_TAG_imported_unit. This is filled in
658 while reading psymtabs, used to compute the psymtab dependencies,
659 and then cleared. Then it is filled in again while reading full
660 symbols, and only deleted when the objfile is destroyed.
661
662 This is also used to work around a difference between the way gold
663 generates .gdb_index version <=7 and the way gdb does. Arguably this
664 is a gold bug. For symbols coming from TUs, gold records in the index
665 the CU that includes the TU instead of the TU itself. This breaks
666 dw2_lookup_symbol: It assumes that if the index says symbol X lives
667 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
668 will find X. Alas TUs live in their own symtab, so after expanding CU Y
669 we need to look in TU Z to find X. Fortunately, this is akin to
670 DW_TAG_imported_unit, so we just use the same mechanism: For
671 .gdb_index version <=7 this also records the TUs that the CU referred
672 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
673 indices so we only pay a price for gold generated indices.
674 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 675 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
676};
677
348e048f
DE
678/* Entry in the signatured_types hash table. */
679
680struct signatured_type
681{
42e7ad6c 682 /* The "per_cu" object of this type.
ac9ec31b 683 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
684 N.B.: This is the first member so that it's easy to convert pointers
685 between them. */
686 struct dwarf2_per_cu_data per_cu;
687
3019eac3 688 /* The type's signature. */
348e048f
DE
689 ULONGEST signature;
690
3019eac3 691 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
692 If this TU is a DWO stub and the definition lives in a DWO file
693 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
694 cu_offset type_offset_in_tu;
695
696 /* Offset in the section of the type's DIE.
697 If the definition lives in a DWO file, this is the offset in the
698 .debug_types.dwo section.
699 The value is zero until the actual value is known.
700 Zero is otherwise not a valid section offset. */
701 sect_offset type_offset_in_section;
0186c6a7
DE
702
703 /* Type units are grouped by their DW_AT_stmt_list entry so that they
704 can share them. This points to the containing symtab. */
705 struct type_unit_group *type_unit_group;
ac9ec31b
DE
706
707 /* The type.
708 The first time we encounter this type we fully read it in and install it
709 in the symbol tables. Subsequent times we only need the type. */
710 struct type *type;
a2ce51a0
DE
711
712 /* Containing DWO unit.
713 This field is valid iff per_cu.reading_dwo_directly. */
714 struct dwo_unit *dwo_unit;
348e048f
DE
715};
716
0186c6a7
DE
717typedef struct signatured_type *sig_type_ptr;
718DEF_VEC_P (sig_type_ptr);
719
094b34ac
DE
720/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
721 This includes type_unit_group and quick_file_names. */
722
723struct stmt_list_hash
724{
725 /* The DWO unit this table is from or NULL if there is none. */
726 struct dwo_unit *dwo_unit;
727
728 /* Offset in .debug_line or .debug_line.dwo. */
729 sect_offset line_offset;
730};
731
f4dc4d17
DE
732/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
733 an object of this type. */
734
735struct type_unit_group
736{
0186c6a7 737 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
738 To simplify things we create an artificial CU that "includes" all the
739 type units using this stmt_list so that the rest of the code still has
740 a "per_cu" handle on the symtab.
741 This PER_CU is recognized by having no section. */
8a0459fd 742#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
743 struct dwarf2_per_cu_data per_cu;
744
0186c6a7
DE
745 /* The TUs that share this DW_AT_stmt_list entry.
746 This is added to while parsing type units to build partial symtabs,
747 and is deleted afterwards and not used again. */
748 VEC (sig_type_ptr) *tus;
f4dc4d17 749
43f3e411 750 /* The compunit symtab.
094b34ac 751 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
752 so we create an essentially anonymous symtab as the compunit symtab. */
753 struct compunit_symtab *compunit_symtab;
f4dc4d17 754
094b34ac
DE
755 /* The data used to construct the hash key. */
756 struct stmt_list_hash hash;
f4dc4d17
DE
757
758 /* The number of symtabs from the line header.
759 The value here must match line_header.num_file_names. */
760 unsigned int num_symtabs;
761
762 /* The symbol tables for this TU (obtained from the files listed in
763 DW_AT_stmt_list).
764 WARNING: The order of entries here must match the order of entries
765 in the line header. After the first TU using this type_unit_group, the
766 line header for the subsequent TUs is recreated from this. This is done
767 because we need to use the same symtabs for each TU using the same
768 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
769 there's no guarantee the line header doesn't have duplicate entries. */
770 struct symtab **symtabs;
771};
772
73869dc2 773/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
774
775struct dwo_sections
776{
777 struct dwarf2_section_info abbrev;
3019eac3
DE
778 struct dwarf2_section_info line;
779 struct dwarf2_section_info loc;
43988095 780 struct dwarf2_section_info loclists;
09262596
DE
781 struct dwarf2_section_info macinfo;
782 struct dwarf2_section_info macro;
3019eac3
DE
783 struct dwarf2_section_info str;
784 struct dwarf2_section_info str_offsets;
80626a55
DE
785 /* In the case of a virtual DWO file, these two are unused. */
786 struct dwarf2_section_info info;
3019eac3
DE
787 VEC (dwarf2_section_info_def) *types;
788};
789
c88ee1f0 790/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
791
792struct dwo_unit
793{
794 /* Backlink to the containing struct dwo_file. */
795 struct dwo_file *dwo_file;
796
797 /* The "id" that distinguishes this CU/TU.
798 .debug_info calls this "dwo_id", .debug_types calls this "signature".
799 Since signatures came first, we stick with it for consistency. */
800 ULONGEST signature;
801
802 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 803 struct dwarf2_section_info *section;
3019eac3 804
19ac8c2e 805 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
3019eac3
DE
806 sect_offset offset;
807 unsigned int length;
808
809 /* For types, offset in the type's DIE of the type defined by this TU. */
810 cu_offset type_offset_in_tu;
811};
812
73869dc2
DE
813/* include/dwarf2.h defines the DWP section codes.
814 It defines a max value but it doesn't define a min value, which we
815 use for error checking, so provide one. */
816
817enum dwp_v2_section_ids
818{
819 DW_SECT_MIN = 1
820};
821
80626a55 822/* Data for one DWO file.
57d63ce2
DE
823
824 This includes virtual DWO files (a virtual DWO file is a DWO file as it
825 appears in a DWP file). DWP files don't really have DWO files per se -
826 comdat folding of types "loses" the DWO file they came from, and from
827 a high level view DWP files appear to contain a mass of random types.
828 However, to maintain consistency with the non-DWP case we pretend DWP
829 files contain virtual DWO files, and we assign each TU with one virtual
830 DWO file (generally based on the line and abbrev section offsets -
831 a heuristic that seems to work in practice). */
3019eac3
DE
832
833struct dwo_file
834{
0ac5b59e 835 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
836 For virtual DWO files the name is constructed from the section offsets
837 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
838 from related CU+TUs. */
0ac5b59e
DE
839 const char *dwo_name;
840
841 /* The DW_AT_comp_dir attribute. */
842 const char *comp_dir;
3019eac3 843
80626a55
DE
844 /* The bfd, when the file is open. Otherwise this is NULL.
845 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
846 bfd *dbfd;
3019eac3 847
73869dc2
DE
848 /* The sections that make up this DWO file.
849 Remember that for virtual DWO files in DWP V2, these are virtual
850 sections (for lack of a better name). */
3019eac3
DE
851 struct dwo_sections sections;
852
19c3d4c9
DE
853 /* The CU in the file.
854 We only support one because having more than one requires hacking the
855 dwo_name of each to match, which is highly unlikely to happen.
856 Doing this means all TUs can share comp_dir: We also assume that
857 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
858 struct dwo_unit *cu;
3019eac3
DE
859
860 /* Table of TUs in the file.
861 Each element is a struct dwo_unit. */
862 htab_t tus;
863};
864
80626a55
DE
865/* These sections are what may appear in a DWP file. */
866
867struct dwp_sections
868{
73869dc2 869 /* These are used by both DWP version 1 and 2. */
80626a55
DE
870 struct dwarf2_section_info str;
871 struct dwarf2_section_info cu_index;
872 struct dwarf2_section_info tu_index;
73869dc2
DE
873
874 /* These are only used by DWP version 2 files.
875 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
876 sections are referenced by section number, and are not recorded here.
877 In DWP version 2 there is at most one copy of all these sections, each
878 section being (effectively) comprised of the concatenation of all of the
879 individual sections that exist in the version 1 format.
880 To keep the code simple we treat each of these concatenated pieces as a
881 section itself (a virtual section?). */
882 struct dwarf2_section_info abbrev;
883 struct dwarf2_section_info info;
884 struct dwarf2_section_info line;
885 struct dwarf2_section_info loc;
886 struct dwarf2_section_info macinfo;
887 struct dwarf2_section_info macro;
888 struct dwarf2_section_info str_offsets;
889 struct dwarf2_section_info types;
80626a55
DE
890};
891
73869dc2
DE
892/* These sections are what may appear in a virtual DWO file in DWP version 1.
893 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 894
73869dc2 895struct virtual_v1_dwo_sections
80626a55
DE
896{
897 struct dwarf2_section_info abbrev;
898 struct dwarf2_section_info line;
899 struct dwarf2_section_info loc;
900 struct dwarf2_section_info macinfo;
901 struct dwarf2_section_info macro;
902 struct dwarf2_section_info str_offsets;
903 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 904 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
905 struct dwarf2_section_info info_or_types;
906};
907
73869dc2
DE
908/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
909 In version 2, the sections of the DWO files are concatenated together
910 and stored in one section of that name. Thus each ELF section contains
911 several "virtual" sections. */
912
913struct virtual_v2_dwo_sections
914{
915 bfd_size_type abbrev_offset;
916 bfd_size_type abbrev_size;
917
918 bfd_size_type line_offset;
919 bfd_size_type line_size;
920
921 bfd_size_type loc_offset;
922 bfd_size_type loc_size;
923
924 bfd_size_type macinfo_offset;
925 bfd_size_type macinfo_size;
926
927 bfd_size_type macro_offset;
928 bfd_size_type macro_size;
929
930 bfd_size_type str_offsets_offset;
931 bfd_size_type str_offsets_size;
932
933 /* Each DWP hash table entry records one CU or one TU.
934 That is recorded here, and copied to dwo_unit.section. */
935 bfd_size_type info_or_types_offset;
936 bfd_size_type info_or_types_size;
937};
938
80626a55
DE
939/* Contents of DWP hash tables. */
940
941struct dwp_hash_table
942{
73869dc2 943 uint32_t version, nr_columns;
80626a55 944 uint32_t nr_units, nr_slots;
73869dc2
DE
945 const gdb_byte *hash_table, *unit_table;
946 union
947 {
948 struct
949 {
950 const gdb_byte *indices;
951 } v1;
952 struct
953 {
954 /* This is indexed by column number and gives the id of the section
955 in that column. */
956#define MAX_NR_V2_DWO_SECTIONS \
957 (1 /* .debug_info or .debug_types */ \
958 + 1 /* .debug_abbrev */ \
959 + 1 /* .debug_line */ \
960 + 1 /* .debug_loc */ \
961 + 1 /* .debug_str_offsets */ \
962 + 1 /* .debug_macro or .debug_macinfo */)
963 int section_ids[MAX_NR_V2_DWO_SECTIONS];
964 const gdb_byte *offsets;
965 const gdb_byte *sizes;
966 } v2;
967 } section_pool;
80626a55
DE
968};
969
970/* Data for one DWP file. */
971
972struct dwp_file
973{
974 /* Name of the file. */
975 const char *name;
976
73869dc2
DE
977 /* File format version. */
978 int version;
979
93417882 980 /* The bfd. */
80626a55
DE
981 bfd *dbfd;
982
983 /* Section info for this file. */
984 struct dwp_sections sections;
985
57d63ce2 986 /* Table of CUs in the file. */
80626a55
DE
987 const struct dwp_hash_table *cus;
988
989 /* Table of TUs in the file. */
990 const struct dwp_hash_table *tus;
991
19ac8c2e
DE
992 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
993 htab_t loaded_cus;
994 htab_t loaded_tus;
80626a55 995
73869dc2
DE
996 /* Table to map ELF section numbers to their sections.
997 This is only needed for the DWP V1 file format. */
80626a55
DE
998 unsigned int num_sections;
999 asection **elf_sections;
1000};
1001
36586728
TT
1002/* This represents a '.dwz' file. */
1003
1004struct dwz_file
1005{
1006 /* A dwz file can only contain a few sections. */
1007 struct dwarf2_section_info abbrev;
1008 struct dwarf2_section_info info;
1009 struct dwarf2_section_info str;
1010 struct dwarf2_section_info line;
1011 struct dwarf2_section_info macro;
2ec9a5e0 1012 struct dwarf2_section_info gdb_index;
36586728
TT
1013
1014 /* The dwz's BFD. */
1015 bfd *dwz_bfd;
1016};
1017
0963b4bd
MS
1018/* Struct used to pass misc. parameters to read_die_and_children, et
1019 al. which are used for both .debug_info and .debug_types dies.
1020 All parameters here are unchanging for the life of the call. This
dee91e82 1021 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1022
1023struct die_reader_specs
1024{
a32a8923 1025 /* The bfd of die_section. */
93311388
DE
1026 bfd* abfd;
1027
1028 /* The CU of the DIE we are parsing. */
1029 struct dwarf2_cu *cu;
1030
80626a55 1031 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1032 struct dwo_file *dwo_file;
1033
dee91e82 1034 /* The section the die comes from.
3019eac3 1035 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1036 struct dwarf2_section_info *die_section;
1037
1038 /* die_section->buffer. */
d521ce57 1039 const gdb_byte *buffer;
f664829e
DE
1040
1041 /* The end of the buffer. */
1042 const gdb_byte *buffer_end;
a2ce51a0
DE
1043
1044 /* The value of the DW_AT_comp_dir attribute. */
1045 const char *comp_dir;
93311388
DE
1046};
1047
fd820528 1048/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1049typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1050 const gdb_byte *info_ptr,
dee91e82
DE
1051 struct die_info *comp_unit_die,
1052 int has_children,
1053 void *data);
1054
52059ffd
TT
1055struct file_entry
1056{
1057 const char *name;
1058 unsigned int dir_index;
1059 unsigned int mod_time;
1060 unsigned int length;
83769d0b
DE
1061 /* Non-zero if referenced by the Line Number Program. */
1062 int included_p;
1063 /* The associated symbol table, if any. */
1064 struct symtab *symtab;
52059ffd
TT
1065};
1066
debd256d
JB
1067/* The line number information for a compilation unit (found in the
1068 .debug_line section) begins with a "statement program header",
1069 which contains the following information. */
1070struct line_header
1071{
527f3840
JK
1072 /* Offset of line number information in .debug_line section. */
1073 sect_offset offset;
1074
1075 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1076 unsigned offset_in_dwz : 1;
1077
debd256d
JB
1078 unsigned int total_length;
1079 unsigned short version;
1080 unsigned int header_length;
1081 unsigned char minimum_instruction_length;
2dc7f7b3 1082 unsigned char maximum_ops_per_instruction;
debd256d
JB
1083 unsigned char default_is_stmt;
1084 int line_base;
1085 unsigned char line_range;
1086 unsigned char opcode_base;
1087
1088 /* standard_opcode_lengths[i] is the number of operands for the
1089 standard opcode whose value is i. This means that
1090 standard_opcode_lengths[0] is unused, and the last meaningful
1091 element is standard_opcode_lengths[opcode_base - 1]. */
1092 unsigned char *standard_opcode_lengths;
1093
1094 /* The include_directories table. NOTE! These strings are not
1095 allocated with xmalloc; instead, they are pointers into
1096 debug_line_buffer. If you try to free them, `free' will get
1097 indigestion. */
1098 unsigned int num_include_dirs, include_dirs_size;
d521ce57 1099 const char **include_dirs;
debd256d
JB
1100
1101 /* The file_names table. NOTE! These strings are not allocated
1102 with xmalloc; instead, they are pointers into debug_line_buffer.
1103 Don't try to free them directly. */
1104 unsigned int num_file_names, file_names_size;
52059ffd 1105 struct file_entry *file_names;
debd256d
JB
1106
1107 /* The start and end of the statement program following this
6502dd73 1108 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 1109 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 1110};
c906108c
SS
1111
1112/* When we construct a partial symbol table entry we only
0963b4bd 1113 need this much information. */
c906108c
SS
1114struct partial_die_info
1115 {
72bf9492 1116 /* Offset of this DIE. */
b64f50a1 1117 sect_offset offset;
72bf9492
DJ
1118
1119 /* DWARF-2 tag for this DIE. */
1120 ENUM_BITFIELD(dwarf_tag) tag : 16;
1121
72bf9492
DJ
1122 /* Assorted flags describing the data found in this DIE. */
1123 unsigned int has_children : 1;
1124 unsigned int is_external : 1;
1125 unsigned int is_declaration : 1;
1126 unsigned int has_type : 1;
1127 unsigned int has_specification : 1;
1128 unsigned int has_pc_info : 1;
481860b3 1129 unsigned int may_be_inlined : 1;
72bf9492 1130
0c1b455e
TT
1131 /* This DIE has been marked DW_AT_main_subprogram. */
1132 unsigned int main_subprogram : 1;
1133
72bf9492
DJ
1134 /* Flag set if the SCOPE field of this structure has been
1135 computed. */
1136 unsigned int scope_set : 1;
1137
fa4028e9
JB
1138 /* Flag set if the DIE has a byte_size attribute. */
1139 unsigned int has_byte_size : 1;
1140
ff908ebf
AW
1141 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1142 unsigned int has_const_value : 1;
1143
98bfdba5
PA
1144 /* Flag set if any of the DIE's children are template arguments. */
1145 unsigned int has_template_arguments : 1;
1146
abc72ce4
DE
1147 /* Flag set if fixup_partial_die has been called on this die. */
1148 unsigned int fixup_called : 1;
1149
36586728
TT
1150 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1151 unsigned int is_dwz : 1;
1152
1153 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1154 unsigned int spec_is_dwz : 1;
1155
72bf9492 1156 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1157 sometimes a default name for unnamed DIEs. */
15d034d0 1158 const char *name;
72bf9492 1159
abc72ce4
DE
1160 /* The linkage name, if present. */
1161 const char *linkage_name;
1162
72bf9492
DJ
1163 /* The scope to prepend to our children. This is generally
1164 allocated on the comp_unit_obstack, so will disappear
1165 when this compilation unit leaves the cache. */
15d034d0 1166 const char *scope;
72bf9492 1167
95554aad
TT
1168 /* Some data associated with the partial DIE. The tag determines
1169 which field is live. */
1170 union
1171 {
1172 /* The location description associated with this DIE, if any. */
1173 struct dwarf_block *locdesc;
1174 /* The offset of an import, for DW_TAG_imported_unit. */
1175 sect_offset offset;
1176 } d;
72bf9492
DJ
1177
1178 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1179 CORE_ADDR lowpc;
1180 CORE_ADDR highpc;
72bf9492 1181
93311388 1182 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1183 DW_AT_sibling, if any. */
abc72ce4
DE
1184 /* NOTE: This member isn't strictly necessary, read_partial_die could
1185 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1186 const gdb_byte *sibling;
72bf9492
DJ
1187
1188 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1189 DW_AT_specification (or DW_AT_abstract_origin or
1190 DW_AT_extension). */
b64f50a1 1191 sect_offset spec_offset;
72bf9492
DJ
1192
1193 /* Pointers to this DIE's parent, first child, and next sibling,
1194 if any. */
1195 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1196 };
1197
0963b4bd 1198/* This data structure holds the information of an abbrev. */
c906108c
SS
1199struct abbrev_info
1200 {
1201 unsigned int number; /* number identifying abbrev */
1202 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1203 unsigned short has_children; /* boolean */
1204 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1205 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1206 struct abbrev_info *next; /* next in chain */
1207 };
1208
1209struct attr_abbrev
1210 {
9d25dd43
DE
1211 ENUM_BITFIELD(dwarf_attribute) name : 16;
1212 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1213
1214 /* It is valid only if FORM is DW_FORM_implicit_const. */
1215 LONGEST implicit_const;
c906108c
SS
1216 };
1217
433df2d4
DE
1218/* Size of abbrev_table.abbrev_hash_table. */
1219#define ABBREV_HASH_SIZE 121
1220
1221/* Top level data structure to contain an abbreviation table. */
1222
1223struct abbrev_table
1224{
f4dc4d17
DE
1225 /* Where the abbrev table came from.
1226 This is used as a sanity check when the table is used. */
433df2d4
DE
1227 sect_offset offset;
1228
1229 /* Storage for the abbrev table. */
1230 struct obstack abbrev_obstack;
1231
1232 /* Hash table of abbrevs.
1233 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1234 It could be statically allocated, but the previous code didn't so we
1235 don't either. */
1236 struct abbrev_info **abbrevs;
1237};
1238
0963b4bd 1239/* Attributes have a name and a value. */
b60c80d6
DJ
1240struct attribute
1241 {
9d25dd43 1242 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1243 ENUM_BITFIELD(dwarf_form) form : 15;
1244
1245 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1246 field should be in u.str (existing only for DW_STRING) but it is kept
1247 here for better struct attribute alignment. */
1248 unsigned int string_is_canonical : 1;
1249
b60c80d6
DJ
1250 union
1251 {
15d034d0 1252 const char *str;
b60c80d6 1253 struct dwarf_block *blk;
43bbcdc2
PH
1254 ULONGEST unsnd;
1255 LONGEST snd;
b60c80d6 1256 CORE_ADDR addr;
ac9ec31b 1257 ULONGEST signature;
b60c80d6
DJ
1258 }
1259 u;
1260 };
1261
0963b4bd 1262/* This data structure holds a complete die structure. */
c906108c
SS
1263struct die_info
1264 {
76815b17
DE
1265 /* DWARF-2 tag for this DIE. */
1266 ENUM_BITFIELD(dwarf_tag) tag : 16;
1267
1268 /* Number of attributes */
98bfdba5
PA
1269 unsigned char num_attrs;
1270
1271 /* True if we're presently building the full type name for the
1272 type derived from this DIE. */
1273 unsigned char building_fullname : 1;
76815b17 1274
adde2bff
DE
1275 /* True if this die is in process. PR 16581. */
1276 unsigned char in_process : 1;
1277
76815b17
DE
1278 /* Abbrev number */
1279 unsigned int abbrev;
1280
93311388 1281 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1282 sect_offset offset;
78ba4af6
JB
1283
1284 /* The dies in a compilation unit form an n-ary tree. PARENT
1285 points to this die's parent; CHILD points to the first child of
1286 this node; and all the children of a given node are chained
4950bc1c 1287 together via their SIBLING fields. */
639d11d3
DC
1288 struct die_info *child; /* Its first child, if any. */
1289 struct die_info *sibling; /* Its next sibling, if any. */
1290 struct die_info *parent; /* Its parent, if any. */
c906108c 1291
b60c80d6
DJ
1292 /* An array of attributes, with NUM_ATTRS elements. There may be
1293 zero, but it's not common and zero-sized arrays are not
1294 sufficiently portable C. */
1295 struct attribute attrs[1];
c906108c
SS
1296 };
1297
0963b4bd 1298/* Get at parts of an attribute structure. */
c906108c
SS
1299
1300#define DW_STRING(attr) ((attr)->u.str)
8285870a 1301#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1302#define DW_UNSND(attr) ((attr)->u.unsnd)
1303#define DW_BLOCK(attr) ((attr)->u.blk)
1304#define DW_SND(attr) ((attr)->u.snd)
1305#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1306#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1307
0963b4bd 1308/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1309struct dwarf_block
1310 {
56eb65bd 1311 size_t size;
1d6edc3c
JK
1312
1313 /* Valid only if SIZE is not zero. */
d521ce57 1314 const gdb_byte *data;
c906108c
SS
1315 };
1316
c906108c
SS
1317#ifndef ATTR_ALLOC_CHUNK
1318#define ATTR_ALLOC_CHUNK 4
1319#endif
1320
c906108c
SS
1321/* Allocate fields for structs, unions and enums in this size. */
1322#ifndef DW_FIELD_ALLOC_CHUNK
1323#define DW_FIELD_ALLOC_CHUNK 4
1324#endif
1325
c906108c
SS
1326/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1327 but this would require a corresponding change in unpack_field_as_long
1328 and friends. */
1329static int bits_per_byte = 8;
1330
52059ffd
TT
1331struct nextfield
1332{
1333 struct nextfield *next;
1334 int accessibility;
1335 int virtuality;
1336 struct field field;
1337};
1338
1339struct nextfnfield
1340{
1341 struct nextfnfield *next;
1342 struct fn_field fnfield;
1343};
1344
1345struct fnfieldlist
1346{
1347 const char *name;
1348 int length;
1349 struct nextfnfield *head;
1350};
1351
1352struct typedef_field_list
1353{
1354 struct typedef_field field;
1355 struct typedef_field_list *next;
1356};
1357
c906108c
SS
1358/* The routines that read and process dies for a C struct or C++ class
1359 pass lists of data member fields and lists of member function fields
1360 in an instance of a field_info structure, as defined below. */
1361struct field_info
c5aa993b 1362 {
0963b4bd 1363 /* List of data member and baseclasses fields. */
52059ffd 1364 struct nextfield *fields, *baseclasses;
c906108c 1365
7d0ccb61 1366 /* Number of fields (including baseclasses). */
c5aa993b 1367 int nfields;
c906108c 1368
c5aa993b
JM
1369 /* Number of baseclasses. */
1370 int nbaseclasses;
c906108c 1371
c5aa993b
JM
1372 /* Set if the accesibility of one of the fields is not public. */
1373 int non_public_fields;
c906108c 1374
c5aa993b
JM
1375 /* Member function fields array, entries are allocated in the order they
1376 are encountered in the object file. */
52059ffd 1377 struct nextfnfield *fnfields;
c906108c 1378
c5aa993b
JM
1379 /* Member function fieldlist array, contains name of possibly overloaded
1380 member function, number of overloaded member functions and a pointer
1381 to the head of the member function field chain. */
52059ffd 1382 struct fnfieldlist *fnfieldlists;
c906108c 1383
c5aa993b
JM
1384 /* Number of entries in the fnfieldlists array. */
1385 int nfnfields;
98751a41
JK
1386
1387 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1388 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1389 struct typedef_field_list *typedef_field_list;
98751a41 1390 unsigned typedef_field_list_count;
c5aa993b 1391 };
c906108c 1392
10b3939b
DJ
1393/* One item on the queue of compilation units to read in full symbols
1394 for. */
1395struct dwarf2_queue_item
1396{
1397 struct dwarf2_per_cu_data *per_cu;
95554aad 1398 enum language pretend_language;
10b3939b
DJ
1399 struct dwarf2_queue_item *next;
1400};
1401
1402/* The current queue. */
1403static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1404
ae038cb0
DJ
1405/* Loaded secondary compilation units are kept in memory until they
1406 have not been referenced for the processing of this many
1407 compilation units. Set this to zero to disable caching. Cache
1408 sizes of up to at least twenty will improve startup time for
1409 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1410static int dwarf_max_cache_age = 5;
920d2a44 1411static void
b4f54984
DE
1412show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1413 struct cmd_list_element *c, const char *value)
920d2a44 1414{
3e43a32a 1415 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1416 "DWARF compilation units is %s.\n"),
920d2a44
AC
1417 value);
1418}
4390d890 1419\f
c906108c
SS
1420/* local function prototypes */
1421
a32a8923
DE
1422static const char *get_section_name (const struct dwarf2_section_info *);
1423
1424static const char *get_section_file_name (const struct dwarf2_section_info *);
1425
4efb68b1 1426static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1427
918dd910
JK
1428static void dwarf2_find_base_address (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
0018ea6f
DE
1431static struct partial_symtab *create_partial_symtab
1432 (struct dwarf2_per_cu_data *per_cu, const char *name);
1433
c67a9c90 1434static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1435
72bf9492
DJ
1436static void scan_partial_symbols (struct partial_die_info *,
1437 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1438 int, struct dwarf2_cu *);
c906108c 1439
72bf9492
DJ
1440static void add_partial_symbol (struct partial_die_info *,
1441 struct dwarf2_cu *);
63d06c5c 1442
72bf9492
DJ
1443static void add_partial_namespace (struct partial_die_info *pdi,
1444 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1445 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1446
5d7cb8df 1447static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1448 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1449 struct dwarf2_cu *cu);
1450
72bf9492
DJ
1451static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1452 struct dwarf2_cu *cu);
91c24f0a 1453
bc30ff58
JB
1454static void add_partial_subprogram (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1456 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1457
257e7a09
YQ
1458static void dwarf2_read_symtab (struct partial_symtab *,
1459 struct objfile *);
c906108c 1460
a14ed312 1461static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1462
433df2d4
DE
1463static struct abbrev_info *abbrev_table_lookup_abbrev
1464 (const struct abbrev_table *, unsigned int);
1465
1466static struct abbrev_table *abbrev_table_read_table
1467 (struct dwarf2_section_info *, sect_offset);
1468
1469static void abbrev_table_free (struct abbrev_table *);
1470
f4dc4d17
DE
1471static void abbrev_table_free_cleanup (void *);
1472
dee91e82
DE
1473static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1474 struct dwarf2_section_info *);
c906108c 1475
f3dd6933 1476static void dwarf2_free_abbrev_table (void *);
c906108c 1477
d521ce57 1478static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1479
dee91e82 1480static struct partial_die_info *load_partial_dies
d521ce57 1481 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1482
d521ce57
TT
1483static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1484 struct partial_die_info *,
1485 struct abbrev_info *,
1486 unsigned int,
1487 const gdb_byte *);
c906108c 1488
36586728 1489static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1490 struct dwarf2_cu *);
72bf9492
DJ
1491
1492static void fixup_partial_die (struct partial_die_info *,
1493 struct dwarf2_cu *);
1494
d521ce57
TT
1495static const gdb_byte *read_attribute (const struct die_reader_specs *,
1496 struct attribute *, struct attr_abbrev *,
1497 const gdb_byte *);
a8329558 1498
a1855c1d 1499static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1500
a1855c1d 1501static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1502
a1855c1d 1503static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1504
a1855c1d 1505static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1506
a1855c1d 1507static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1508
d521ce57 1509static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1510 unsigned int *);
c906108c 1511
d521ce57 1512static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1513
1514static LONGEST read_checked_initial_length_and_offset
d521ce57 1515 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1516 unsigned int *, unsigned int *);
613e1657 1517
d521ce57
TT
1518static LONGEST read_offset (bfd *, const gdb_byte *,
1519 const struct comp_unit_head *,
c764a876
DE
1520 unsigned int *);
1521
d521ce57 1522static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1523
f4dc4d17
DE
1524static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1525 sect_offset);
1526
d521ce57 1527static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1528
d521ce57 1529static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1530
d521ce57
TT
1531static const char *read_indirect_string (bfd *, const gdb_byte *,
1532 const struct comp_unit_head *,
1533 unsigned int *);
4bdf3d34 1534
43988095
JK
1535static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1536 const struct comp_unit_head *,
1537 unsigned int *);
36586728 1538
43988095 1539static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1540
d521ce57 1541static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1542
d521ce57
TT
1543static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1544 const gdb_byte *,
3019eac3
DE
1545 unsigned int *);
1546
d521ce57 1547static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1548 ULONGEST str_index);
3019eac3 1549
e142c38c 1550static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1551
e142c38c
DJ
1552static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1553 struct dwarf2_cu *);
c906108c 1554
348e048f 1555static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1556 unsigned int);
348e048f 1557
7d45c7c3
KB
1558static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1559 struct dwarf2_cu *cu);
1560
05cf31d1
JB
1561static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1562 struct dwarf2_cu *cu);
1563
e142c38c 1564static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1565
e142c38c 1566static struct die_info *die_specification (struct die_info *die,
f2f0e013 1567 struct dwarf2_cu **);
63d06c5c 1568
debd256d
JB
1569static void free_line_header (struct line_header *lh);
1570
3019eac3
DE
1571static struct line_header *dwarf_decode_line_header (unsigned int offset,
1572 struct dwarf2_cu *cu);
debd256d 1573
f3f5162e 1574static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1575 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1576 CORE_ADDR, int decode_mapping);
c906108c 1577
4d663531 1578static void dwarf2_start_subfile (const char *, const char *);
c906108c 1579
43f3e411
DE
1580static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1581 const char *, const char *,
1582 CORE_ADDR);
f4dc4d17 1583
a14ed312 1584static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1585 struct dwarf2_cu *);
c906108c 1586
34eaf542
TT
1587static struct symbol *new_symbol_full (struct die_info *, struct type *,
1588 struct dwarf2_cu *, struct symbol *);
1589
ff39bb5e 1590static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1591 struct dwarf2_cu *);
c906108c 1592
ff39bb5e 1593static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1594 struct type *type,
1595 const char *name,
1596 struct obstack *obstack,
12df843f 1597 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1598 const gdb_byte **bytes,
98bfdba5 1599 struct dwarf2_locexpr_baton **baton);
2df3850c 1600
e7c27a73 1601static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1602
b4ba55a1
JB
1603static int need_gnat_info (struct dwarf2_cu *);
1604
3e43a32a
MS
1605static struct type *die_descriptive_type (struct die_info *,
1606 struct dwarf2_cu *);
b4ba55a1
JB
1607
1608static void set_descriptive_type (struct type *, struct die_info *,
1609 struct dwarf2_cu *);
1610
e7c27a73
DJ
1611static struct type *die_containing_type (struct die_info *,
1612 struct dwarf2_cu *);
c906108c 1613
ff39bb5e 1614static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1615 struct dwarf2_cu *);
c906108c 1616
f792889a 1617static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1618
673bfd45
DE
1619static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1620
0d5cff50 1621static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1622
6e70227d 1623static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1624 const char *suffix, int physname,
1625 struct dwarf2_cu *cu);
63d06c5c 1626
e7c27a73 1627static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1628
348e048f
DE
1629static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1630
e7c27a73 1631static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1632
e7c27a73 1633static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1634
96408a79
SA
1635static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1636
ff013f42
JK
1637static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1638 struct dwarf2_cu *, struct partial_symtab *);
1639
3a2b436a 1640/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1641 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1642enum pc_bounds_kind
1643{
e385593e 1644 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1645 PC_BOUNDS_NOT_PRESENT,
1646
e385593e
JK
1647 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1648 were present but they do not form a valid range of PC addresses. */
1649 PC_BOUNDS_INVALID,
1650
3a2b436a
JK
1651 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1652 PC_BOUNDS_RANGES,
1653
1654 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1655 PC_BOUNDS_HIGH_LOW,
1656};
1657
1658static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1659 CORE_ADDR *, CORE_ADDR *,
1660 struct dwarf2_cu *,
1661 struct partial_symtab *);
c906108c 1662
fae299cd
DC
1663static void get_scope_pc_bounds (struct die_info *,
1664 CORE_ADDR *, CORE_ADDR *,
1665 struct dwarf2_cu *);
1666
801e3a5b
JB
1667static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1668 CORE_ADDR, struct dwarf2_cu *);
1669
a14ed312 1670static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1671 struct dwarf2_cu *);
c906108c 1672
a14ed312 1673static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1674 struct type *, struct dwarf2_cu *);
c906108c 1675
a14ed312 1676static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1677 struct die_info *, struct type *,
e7c27a73 1678 struct dwarf2_cu *);
c906108c 1679
a14ed312 1680static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1681 struct type *,
1682 struct dwarf2_cu *);
c906108c 1683
134d01f1 1684static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1685
e7c27a73 1686static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1687
e7c27a73 1688static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1689
5d7cb8df
JK
1690static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1691
22cee43f
PMR
1692static struct using_direct **using_directives (enum language);
1693
27aa8d6a
SW
1694static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1695
74921315
KS
1696static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1697
f55ee35c
JK
1698static struct type *read_module_type (struct die_info *die,
1699 struct dwarf2_cu *cu);
1700
38d518c9 1701static const char *namespace_name (struct die_info *die,
e142c38c 1702 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1703
134d01f1 1704static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1705
e7c27a73 1706static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1707
6e70227d 1708static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1709 struct dwarf2_cu *);
1710
bf6af496 1711static struct die_info *read_die_and_siblings_1
d521ce57 1712 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1713 struct die_info *);
639d11d3 1714
dee91e82 1715static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1716 const gdb_byte *info_ptr,
1717 const gdb_byte **new_info_ptr,
639d11d3
DC
1718 struct die_info *parent);
1719
d521ce57
TT
1720static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1721 struct die_info **, const gdb_byte *,
1722 int *, int);
3019eac3 1723
d521ce57
TT
1724static const gdb_byte *read_full_die (const struct die_reader_specs *,
1725 struct die_info **, const gdb_byte *,
1726 int *);
93311388 1727
e7c27a73 1728static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1729
15d034d0
TT
1730static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1731 struct obstack *);
71c25dea 1732
15d034d0 1733static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1734
15d034d0 1735static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1736 struct die_info *die,
1737 struct dwarf2_cu *cu);
1738
ca69b9e6
DE
1739static const char *dwarf2_physname (const char *name, struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
e142c38c 1742static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1743 struct dwarf2_cu **);
9219021c 1744
f39c6ffd 1745static const char *dwarf_tag_name (unsigned int);
c906108c 1746
f39c6ffd 1747static const char *dwarf_attr_name (unsigned int);
c906108c 1748
f39c6ffd 1749static const char *dwarf_form_name (unsigned int);
c906108c 1750
a14ed312 1751static char *dwarf_bool_name (unsigned int);
c906108c 1752
f39c6ffd 1753static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1754
f9aca02d 1755static struct die_info *sibling_die (struct die_info *);
c906108c 1756
d97bc12b
DE
1757static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1758
1759static void dump_die_for_error (struct die_info *);
1760
1761static void dump_die_1 (struct ui_file *, int level, int max_level,
1762 struct die_info *);
c906108c 1763
d97bc12b 1764/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1765
51545339 1766static void store_in_ref_table (struct die_info *,
10b3939b 1767 struct dwarf2_cu *);
c906108c 1768
ff39bb5e 1769static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1770
ff39bb5e 1771static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1772
348e048f 1773static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1774 const struct attribute *,
348e048f
DE
1775 struct dwarf2_cu **);
1776
10b3939b 1777static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1778 const struct attribute *,
f2f0e013 1779 struct dwarf2_cu **);
c906108c 1780
348e048f 1781static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1782 const struct attribute *,
348e048f
DE
1783 struct dwarf2_cu **);
1784
ac9ec31b
DE
1785static struct type *get_signatured_type (struct die_info *, ULONGEST,
1786 struct dwarf2_cu *);
1787
1788static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1789 const struct attribute *,
ac9ec31b
DE
1790 struct dwarf2_cu *);
1791
e5fe5e75 1792static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1793
52dc124a 1794static void read_signatured_type (struct signatured_type *);
348e048f 1795
63e43d3a
PMR
1796static int attr_to_dynamic_prop (const struct attribute *attr,
1797 struct die_info *die, struct dwarf2_cu *cu,
1798 struct dynamic_prop *prop);
1799
c906108c
SS
1800/* memory allocation interface */
1801
7b5a2f43 1802static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1803
b60c80d6 1804static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1805
43f3e411 1806static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1807
6e5a29e1 1808static int attr_form_is_block (const struct attribute *);
8e19ed76 1809
6e5a29e1 1810static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1811
6e5a29e1 1812static int attr_form_is_constant (const struct attribute *);
3690dd37 1813
6e5a29e1 1814static int attr_form_is_ref (const struct attribute *);
7771576e 1815
8cf6f0b1
TT
1816static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1817 struct dwarf2_loclist_baton *baton,
ff39bb5e 1818 const struct attribute *attr);
8cf6f0b1 1819
ff39bb5e 1820static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1821 struct symbol *sym,
f1e6e072
TT
1822 struct dwarf2_cu *cu,
1823 int is_block);
4c2df51b 1824
d521ce57
TT
1825static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1826 const gdb_byte *info_ptr,
1827 struct abbrev_info *abbrev);
4bb7a0a7 1828
72bf9492
DJ
1829static void free_stack_comp_unit (void *);
1830
72bf9492
DJ
1831static hashval_t partial_die_hash (const void *item);
1832
1833static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1834
ae038cb0 1835static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1836 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1837
9816fde3 1838static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1839 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1840
1841static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1842 struct die_info *comp_unit_die,
1843 enum language pretend_language);
93311388 1844
68dc6402 1845static void free_heap_comp_unit (void *);
ae038cb0
DJ
1846
1847static void free_cached_comp_units (void *);
1848
1849static void age_cached_comp_units (void);
1850
dee91e82 1851static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1852
f792889a
DJ
1853static struct type *set_die_type (struct die_info *, struct type *,
1854 struct dwarf2_cu *);
1c379e20 1855
ae038cb0
DJ
1856static void create_all_comp_units (struct objfile *);
1857
0e50663e 1858static int create_all_type_units (struct objfile *);
1fd400ff 1859
95554aad
TT
1860static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1861 enum language);
10b3939b 1862
95554aad
TT
1863static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1864 enum language);
10b3939b 1865
f4dc4d17
DE
1866static void process_full_type_unit (struct dwarf2_per_cu_data *,
1867 enum language);
1868
10b3939b
DJ
1869static void dwarf2_add_dependence (struct dwarf2_cu *,
1870 struct dwarf2_per_cu_data *);
1871
ae038cb0
DJ
1872static void dwarf2_mark (struct dwarf2_cu *);
1873
1874static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1875
b64f50a1 1876static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1877 struct dwarf2_per_cu_data *);
673bfd45 1878
f792889a 1879static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1880
9291a0cd
TT
1881static void dwarf2_release_queue (void *dummy);
1882
95554aad
TT
1883static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1884 enum language pretend_language);
1885
a0f42c21 1886static void process_queue (void);
9291a0cd
TT
1887
1888static void find_file_and_directory (struct die_info *die,
1889 struct dwarf2_cu *cu,
15d034d0 1890 const char **name, const char **comp_dir);
9291a0cd
TT
1891
1892static char *file_full_name (int file, struct line_header *lh,
1893 const char *comp_dir);
1894
43988095
JK
1895/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1896enum class rcuh_kind { COMPILE, TYPE };
1897
d521ce57 1898static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1899 (struct comp_unit_head *header,
1900 struct dwarf2_section_info *section,
d521ce57 1901 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1902 rcuh_kind section_kind);
36586728 1903
fd820528 1904static void init_cutu_and_read_dies
f4dc4d17
DE
1905 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1906 int use_existing_cu, int keep,
3019eac3
DE
1907 die_reader_func_ftype *die_reader_func, void *data);
1908
dee91e82
DE
1909static void init_cutu_and_read_dies_simple
1910 (struct dwarf2_per_cu_data *this_cu,
1911 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1912
673bfd45 1913static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1914
3019eac3
DE
1915static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1916
57d63ce2
DE
1917static struct dwo_unit *lookup_dwo_unit_in_dwp
1918 (struct dwp_file *dwp_file, const char *comp_dir,
1919 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
1920
1921static struct dwp_file *get_dwp_file (void);
1922
3019eac3 1923static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1924 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1925
1926static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1927 (struct signatured_type *, const char *, const char *);
3019eac3 1928
89e63ee4
DE
1929static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1930
3019eac3
DE
1931static void free_dwo_file_cleanup (void *);
1932
95554aad
TT
1933static void process_cu_includes (void);
1934
1b80a9fa 1935static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1936
1937static void free_line_header_voidp (void *arg);
4390d890
DE
1938\f
1939/* Various complaints about symbol reading that don't abort the process. */
1940
1941static void
1942dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1943{
1944 complaint (&symfile_complaints,
1945 _("statement list doesn't fit in .debug_line section"));
1946}
1947
1948static void
1949dwarf2_debug_line_missing_file_complaint (void)
1950{
1951 complaint (&symfile_complaints,
1952 _(".debug_line section has line data without a file"));
1953}
1954
1955static void
1956dwarf2_debug_line_missing_end_sequence_complaint (void)
1957{
1958 complaint (&symfile_complaints,
1959 _(".debug_line section has line "
1960 "program sequence without an end"));
1961}
1962
1963static void
1964dwarf2_complex_location_expr_complaint (void)
1965{
1966 complaint (&symfile_complaints, _("location expression too complex"));
1967}
1968
1969static void
1970dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1971 int arg3)
1972{
1973 complaint (&symfile_complaints,
1974 _("const value length mismatch for '%s', got %d, expected %d"),
1975 arg1, arg2, arg3);
1976}
1977
1978static void
1979dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1980{
1981 complaint (&symfile_complaints,
1982 _("debug info runs off end of %s section"
1983 " [in module %s]"),
a32a8923
DE
1984 get_section_name (section),
1985 get_section_file_name (section));
4390d890 1986}
1b80a9fa 1987
4390d890
DE
1988static void
1989dwarf2_macro_malformed_definition_complaint (const char *arg1)
1990{
1991 complaint (&symfile_complaints,
1992 _("macro debug info contains a "
1993 "malformed macro definition:\n`%s'"),
1994 arg1);
1995}
1996
1997static void
1998dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1999{
2000 complaint (&symfile_complaints,
2001 _("invalid attribute class or form for '%s' in '%s'"),
2002 arg1, arg2);
2003}
527f3840
JK
2004
2005/* Hash function for line_header_hash. */
2006
2007static hashval_t
2008line_header_hash (const struct line_header *ofs)
2009{
2010 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
2011}
2012
2013/* Hash function for htab_create_alloc_ex for line_header_hash. */
2014
2015static hashval_t
2016line_header_hash_voidp (const void *item)
2017{
9a3c8263 2018 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2019
2020 return line_header_hash (ofs);
2021}
2022
2023/* Equality function for line_header_hash. */
2024
2025static int
2026line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2027{
9a3c8263
SM
2028 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2029 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840
JK
2030
2031 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
2032 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2033}
2034
4390d890 2035\f
9291a0cd
TT
2036#if WORDS_BIGENDIAN
2037
2038/* Convert VALUE between big- and little-endian. */
2039static offset_type
2040byte_swap (offset_type value)
2041{
2042 offset_type result;
2043
2044 result = (value & 0xff) << 24;
2045 result |= (value & 0xff00) << 8;
2046 result |= (value & 0xff0000) >> 8;
2047 result |= (value & 0xff000000) >> 24;
2048 return result;
2049}
2050
2051#define MAYBE_SWAP(V) byte_swap (V)
2052
2053#else
2054#define MAYBE_SWAP(V) (V)
2055#endif /* WORDS_BIGENDIAN */
2056
31aa7e4e
JB
2057/* Read the given attribute value as an address, taking the attribute's
2058 form into account. */
2059
2060static CORE_ADDR
2061attr_value_as_address (struct attribute *attr)
2062{
2063 CORE_ADDR addr;
2064
2065 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2066 {
2067 /* Aside from a few clearly defined exceptions, attributes that
2068 contain an address must always be in DW_FORM_addr form.
2069 Unfortunately, some compilers happen to be violating this
2070 requirement by encoding addresses using other forms, such
2071 as DW_FORM_data4 for example. For those broken compilers,
2072 we try to do our best, without any guarantee of success,
2073 to interpret the address correctly. It would also be nice
2074 to generate a complaint, but that would require us to maintain
2075 a list of legitimate cases where a non-address form is allowed,
2076 as well as update callers to pass in at least the CU's DWARF
2077 version. This is more overhead than what we're willing to
2078 expand for a pretty rare case. */
2079 addr = DW_UNSND (attr);
2080 }
2081 else
2082 addr = DW_ADDR (attr);
2083
2084 return addr;
2085}
2086
9291a0cd
TT
2087/* The suffix for an index file. */
2088#define INDEX_SUFFIX ".gdb-index"
2089
c906108c 2090/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2091 information and return true if we have enough to do something.
2092 NAMES points to the dwarf2 section names, or is NULL if the standard
2093 ELF names are used. */
c906108c
SS
2094
2095int
251d32d9
TG
2096dwarf2_has_info (struct objfile *objfile,
2097 const struct dwarf2_debug_sections *names)
c906108c 2098{
9a3c8263
SM
2099 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2100 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2101 if (!dwarf2_per_objfile)
2102 {
2103 /* Initialize per-objfile state. */
2104 struct dwarf2_per_objfile *data
8d749320 2105 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2106
be391dca
TT
2107 memset (data, 0, sizeof (*data));
2108 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2109 dwarf2_per_objfile = data;
6502dd73 2110
251d32d9
TG
2111 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2112 (void *) names);
be391dca
TT
2113 dwarf2_per_objfile->objfile = objfile;
2114 }
73869dc2 2115 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2116 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2117 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2118 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2119}
2120
2121/* Return the containing section of virtual section SECTION. */
2122
2123static struct dwarf2_section_info *
2124get_containing_section (const struct dwarf2_section_info *section)
2125{
2126 gdb_assert (section->is_virtual);
2127 return section->s.containing_section;
c906108c
SS
2128}
2129
a32a8923
DE
2130/* Return the bfd owner of SECTION. */
2131
2132static struct bfd *
2133get_section_bfd_owner (const struct dwarf2_section_info *section)
2134{
73869dc2
DE
2135 if (section->is_virtual)
2136 {
2137 section = get_containing_section (section);
2138 gdb_assert (!section->is_virtual);
2139 }
049412e3 2140 return section->s.section->owner;
a32a8923
DE
2141}
2142
2143/* Return the bfd section of SECTION.
2144 Returns NULL if the section is not present. */
2145
2146static asection *
2147get_section_bfd_section (const struct dwarf2_section_info *section)
2148{
73869dc2
DE
2149 if (section->is_virtual)
2150 {
2151 section = get_containing_section (section);
2152 gdb_assert (!section->is_virtual);
2153 }
049412e3 2154 return section->s.section;
a32a8923
DE
2155}
2156
2157/* Return the name of SECTION. */
2158
2159static const char *
2160get_section_name (const struct dwarf2_section_info *section)
2161{
2162 asection *sectp = get_section_bfd_section (section);
2163
2164 gdb_assert (sectp != NULL);
2165 return bfd_section_name (get_section_bfd_owner (section), sectp);
2166}
2167
2168/* Return the name of the file SECTION is in. */
2169
2170static const char *
2171get_section_file_name (const struct dwarf2_section_info *section)
2172{
2173 bfd *abfd = get_section_bfd_owner (section);
2174
2175 return bfd_get_filename (abfd);
2176}
2177
2178/* Return the id of SECTION.
2179 Returns 0 if SECTION doesn't exist. */
2180
2181static int
2182get_section_id (const struct dwarf2_section_info *section)
2183{
2184 asection *sectp = get_section_bfd_section (section);
2185
2186 if (sectp == NULL)
2187 return 0;
2188 return sectp->id;
2189}
2190
2191/* Return the flags of SECTION.
73869dc2 2192 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2193
2194static int
2195get_section_flags (const struct dwarf2_section_info *section)
2196{
2197 asection *sectp = get_section_bfd_section (section);
2198
2199 gdb_assert (sectp != NULL);
2200 return bfd_get_section_flags (sectp->owner, sectp);
2201}
2202
251d32d9
TG
2203/* When loading sections, we look either for uncompressed section or for
2204 compressed section names. */
233a11ab
CS
2205
2206static int
251d32d9
TG
2207section_is_p (const char *section_name,
2208 const struct dwarf2_section_names *names)
233a11ab 2209{
251d32d9
TG
2210 if (names->normal != NULL
2211 && strcmp (section_name, names->normal) == 0)
2212 return 1;
2213 if (names->compressed != NULL
2214 && strcmp (section_name, names->compressed) == 0)
2215 return 1;
2216 return 0;
233a11ab
CS
2217}
2218
c906108c
SS
2219/* This function is mapped across the sections and remembers the
2220 offset and size of each of the debugging sections we are interested
2221 in. */
2222
2223static void
251d32d9 2224dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2225{
251d32d9 2226 const struct dwarf2_debug_sections *names;
dc7650b8 2227 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2228
2229 if (vnames == NULL)
2230 names = &dwarf2_elf_names;
2231 else
2232 names = (const struct dwarf2_debug_sections *) vnames;
2233
dc7650b8
JK
2234 if ((aflag & SEC_HAS_CONTENTS) == 0)
2235 {
2236 }
2237 else if (section_is_p (sectp->name, &names->info))
c906108c 2238 {
049412e3 2239 dwarf2_per_objfile->info.s.section = sectp;
dce234bc 2240 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2241 }
251d32d9 2242 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2243 {
049412e3 2244 dwarf2_per_objfile->abbrev.s.section = sectp;
dce234bc 2245 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2246 }
251d32d9 2247 else if (section_is_p (sectp->name, &names->line))
c906108c 2248 {
049412e3 2249 dwarf2_per_objfile->line.s.section = sectp;
dce234bc 2250 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2251 }
251d32d9 2252 else if (section_is_p (sectp->name, &names->loc))
c906108c 2253 {
049412e3 2254 dwarf2_per_objfile->loc.s.section = sectp;
dce234bc 2255 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2256 }
43988095
JK
2257 else if (section_is_p (sectp->name, &names->loclists))
2258 {
2259 dwarf2_per_objfile->loclists.s.section = sectp;
2260 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2261 }
251d32d9 2262 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2263 {
049412e3 2264 dwarf2_per_objfile->macinfo.s.section = sectp;
dce234bc 2265 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2266 }
cf2c3c16
TT
2267 else if (section_is_p (sectp->name, &names->macro))
2268 {
049412e3 2269 dwarf2_per_objfile->macro.s.section = sectp;
cf2c3c16
TT
2270 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2271 }
251d32d9 2272 else if (section_is_p (sectp->name, &names->str))
c906108c 2273 {
049412e3 2274 dwarf2_per_objfile->str.s.section = sectp;
dce234bc 2275 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2276 }
43988095
JK
2277 else if (section_is_p (sectp->name, &names->line_str))
2278 {
2279 dwarf2_per_objfile->line_str.s.section = sectp;
2280 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2281 }
3019eac3
DE
2282 else if (section_is_p (sectp->name, &names->addr))
2283 {
049412e3 2284 dwarf2_per_objfile->addr.s.section = sectp;
3019eac3
DE
2285 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2286 }
251d32d9 2287 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2288 {
049412e3 2289 dwarf2_per_objfile->frame.s.section = sectp;
dce234bc 2290 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2291 }
251d32d9 2292 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2293 {
049412e3 2294 dwarf2_per_objfile->eh_frame.s.section = sectp;
dc7650b8 2295 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2296 }
251d32d9 2297 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2298 {
049412e3 2299 dwarf2_per_objfile->ranges.s.section = sectp;
dce234bc 2300 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2301 }
43988095
JK
2302 else if (section_is_p (sectp->name, &names->rnglists))
2303 {
2304 dwarf2_per_objfile->rnglists.s.section = sectp;
2305 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2306 }
251d32d9 2307 else if (section_is_p (sectp->name, &names->types))
348e048f 2308 {
8b70b953
TT
2309 struct dwarf2_section_info type_section;
2310
2311 memset (&type_section, 0, sizeof (type_section));
049412e3 2312 type_section.s.section = sectp;
8b70b953
TT
2313 type_section.size = bfd_get_section_size (sectp);
2314
2315 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2316 &type_section);
348e048f 2317 }
251d32d9 2318 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2319 {
049412e3 2320 dwarf2_per_objfile->gdb_index.s.section = sectp;
9291a0cd
TT
2321 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2322 }
dce234bc 2323
b4e1fd61 2324 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2325 && bfd_section_vma (abfd, sectp) == 0)
2326 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2327}
2328
fceca515
DE
2329/* A helper function that decides whether a section is empty,
2330 or not present. */
9e0ac564
TT
2331
2332static int
19ac8c2e 2333dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2334{
73869dc2
DE
2335 if (section->is_virtual)
2336 return section->size == 0;
049412e3 2337 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2338}
2339
3019eac3
DE
2340/* Read the contents of the section INFO.
2341 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2342 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2343 of the DWO file.
dce234bc 2344 If the section is compressed, uncompress it before returning. */
c906108c 2345
dce234bc
PP
2346static void
2347dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2348{
a32a8923 2349 asection *sectp;
3019eac3 2350 bfd *abfd;
dce234bc 2351 gdb_byte *buf, *retbuf;
c906108c 2352
be391dca
TT
2353 if (info->readin)
2354 return;
dce234bc 2355 info->buffer = NULL;
be391dca 2356 info->readin = 1;
188dd5d6 2357
9e0ac564 2358 if (dwarf2_section_empty_p (info))
dce234bc 2359 return;
c906108c 2360
a32a8923 2361 sectp = get_section_bfd_section (info);
3019eac3 2362
73869dc2
DE
2363 /* If this is a virtual section we need to read in the real one first. */
2364 if (info->is_virtual)
2365 {
2366 struct dwarf2_section_info *containing_section =
2367 get_containing_section (info);
2368
2369 gdb_assert (sectp != NULL);
2370 if ((sectp->flags & SEC_RELOC) != 0)
2371 {
2372 error (_("Dwarf Error: DWP format V2 with relocations is not"
2373 " supported in section %s [in module %s]"),
2374 get_section_name (info), get_section_file_name (info));
2375 }
2376 dwarf2_read_section (objfile, containing_section);
2377 /* Other code should have already caught virtual sections that don't
2378 fit. */
2379 gdb_assert (info->virtual_offset + info->size
2380 <= containing_section->size);
2381 /* If the real section is empty or there was a problem reading the
2382 section we shouldn't get here. */
2383 gdb_assert (containing_section->buffer != NULL);
2384 info->buffer = containing_section->buffer + info->virtual_offset;
2385 return;
2386 }
2387
4bf44c1c
TT
2388 /* If the section has relocations, we must read it ourselves.
2389 Otherwise we attach it to the BFD. */
2390 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2391 {
d521ce57 2392 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2393 return;
dce234bc 2394 }
dce234bc 2395
224c3ddb 2396 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2397 info->buffer = buf;
dce234bc
PP
2398
2399 /* When debugging .o files, we may need to apply relocations; see
2400 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2401 We never compress sections in .o files, so we only need to
2402 try this when the section is not compressed. */
ac8035ab 2403 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2404 if (retbuf != NULL)
2405 {
2406 info->buffer = retbuf;
2407 return;
2408 }
2409
a32a8923
DE
2410 abfd = get_section_bfd_owner (info);
2411 gdb_assert (abfd != NULL);
2412
dce234bc
PP
2413 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2414 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2415 {
2416 error (_("Dwarf Error: Can't read DWARF data"
2417 " in section %s [in module %s]"),
2418 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2419 }
dce234bc
PP
2420}
2421
9e0ac564
TT
2422/* A helper function that returns the size of a section in a safe way.
2423 If you are positive that the section has been read before using the
2424 size, then it is safe to refer to the dwarf2_section_info object's
2425 "size" field directly. In other cases, you must call this
2426 function, because for compressed sections the size field is not set
2427 correctly until the section has been read. */
2428
2429static bfd_size_type
2430dwarf2_section_size (struct objfile *objfile,
2431 struct dwarf2_section_info *info)
2432{
2433 if (!info->readin)
2434 dwarf2_read_section (objfile, info);
2435 return info->size;
2436}
2437
dce234bc 2438/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2439 SECTION_NAME. */
af34e669 2440
dce234bc 2441void
3017a003
TG
2442dwarf2_get_section_info (struct objfile *objfile,
2443 enum dwarf2_section_enum sect,
d521ce57 2444 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2445 bfd_size_type *sizep)
2446{
2447 struct dwarf2_per_objfile *data
9a3c8263
SM
2448 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2449 dwarf2_objfile_data_key);
dce234bc 2450 struct dwarf2_section_info *info;
a3b2a86b
TT
2451
2452 /* We may see an objfile without any DWARF, in which case we just
2453 return nothing. */
2454 if (data == NULL)
2455 {
2456 *sectp = NULL;
2457 *bufp = NULL;
2458 *sizep = 0;
2459 return;
2460 }
3017a003
TG
2461 switch (sect)
2462 {
2463 case DWARF2_DEBUG_FRAME:
2464 info = &data->frame;
2465 break;
2466 case DWARF2_EH_FRAME:
2467 info = &data->eh_frame;
2468 break;
2469 default:
2470 gdb_assert_not_reached ("unexpected section");
2471 }
dce234bc 2472
9e0ac564 2473 dwarf2_read_section (objfile, info);
dce234bc 2474
a32a8923 2475 *sectp = get_section_bfd_section (info);
dce234bc
PP
2476 *bufp = info->buffer;
2477 *sizep = info->size;
2478}
2479
36586728
TT
2480/* A helper function to find the sections for a .dwz file. */
2481
2482static void
2483locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2484{
9a3c8263 2485 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2486
2487 /* Note that we only support the standard ELF names, because .dwz
2488 is ELF-only (at the time of writing). */
2489 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2490 {
049412e3 2491 dwz_file->abbrev.s.section = sectp;
36586728
TT
2492 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2493 }
2494 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2495 {
049412e3 2496 dwz_file->info.s.section = sectp;
36586728
TT
2497 dwz_file->info.size = bfd_get_section_size (sectp);
2498 }
2499 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2500 {
049412e3 2501 dwz_file->str.s.section = sectp;
36586728
TT
2502 dwz_file->str.size = bfd_get_section_size (sectp);
2503 }
2504 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2505 {
049412e3 2506 dwz_file->line.s.section = sectp;
36586728
TT
2507 dwz_file->line.size = bfd_get_section_size (sectp);
2508 }
2509 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2510 {
049412e3 2511 dwz_file->macro.s.section = sectp;
36586728
TT
2512 dwz_file->macro.size = bfd_get_section_size (sectp);
2513 }
2ec9a5e0
TT
2514 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2515 {
049412e3 2516 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2517 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2518 }
36586728
TT
2519}
2520
4db1a1dc
TT
2521/* Open the separate '.dwz' debug file, if needed. Return NULL if
2522 there is no .gnu_debugaltlink section in the file. Error if there
2523 is such a section but the file cannot be found. */
36586728
TT
2524
2525static struct dwz_file *
2526dwarf2_get_dwz_file (void)
2527{
4db1a1dc 2528 char *data;
36586728
TT
2529 struct cleanup *cleanup;
2530 const char *filename;
2531 struct dwz_file *result;
acd13123 2532 bfd_size_type buildid_len_arg;
dc294be5
TT
2533 size_t buildid_len;
2534 bfd_byte *buildid;
36586728
TT
2535
2536 if (dwarf2_per_objfile->dwz_file != NULL)
2537 return dwarf2_per_objfile->dwz_file;
2538
4db1a1dc
TT
2539 bfd_set_error (bfd_error_no_error);
2540 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2541 &buildid_len_arg, &buildid);
4db1a1dc
TT
2542 if (data == NULL)
2543 {
2544 if (bfd_get_error () == bfd_error_no_error)
2545 return NULL;
2546 error (_("could not read '.gnu_debugaltlink' section: %s"),
2547 bfd_errmsg (bfd_get_error ()));
2548 }
36586728 2549 cleanup = make_cleanup (xfree, data);
dc294be5 2550 make_cleanup (xfree, buildid);
36586728 2551
acd13123
TT
2552 buildid_len = (size_t) buildid_len_arg;
2553
f9d83a0b 2554 filename = (const char *) data;
36586728
TT
2555 if (!IS_ABSOLUTE_PATH (filename))
2556 {
4262abfb 2557 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2558 char *rel;
2559
2560 make_cleanup (xfree, abs);
2561 abs = ldirname (abs);
2562 make_cleanup (xfree, abs);
2563
2564 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2565 make_cleanup (xfree, rel);
2566 filename = rel;
2567 }
2568
dc294be5
TT
2569 /* First try the file name given in the section. If that doesn't
2570 work, try to use the build-id instead. */
192b62ce 2571 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2572 if (dwz_bfd != NULL)
36586728 2573 {
192b62ce
TT
2574 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2575 dwz_bfd.release ();
36586728
TT
2576 }
2577
dc294be5
TT
2578 if (dwz_bfd == NULL)
2579 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2580
2581 if (dwz_bfd == NULL)
2582 error (_("could not find '.gnu_debugaltlink' file for %s"),
2583 objfile_name (dwarf2_per_objfile->objfile));
2584
36586728
TT
2585 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2586 struct dwz_file);
192b62ce 2587 result->dwz_bfd = dwz_bfd.release ();
36586728 2588
192b62ce 2589 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2590
2591 do_cleanups (cleanup);
2592
192b62ce 2593 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2594 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2595 return result;
2596}
9291a0cd 2597\f
7b9f3c50
DE
2598/* DWARF quick_symbols_functions support. */
2599
2600/* TUs can share .debug_line entries, and there can be a lot more TUs than
2601 unique line tables, so we maintain a separate table of all .debug_line
2602 derived entries to support the sharing.
2603 All the quick functions need is the list of file names. We discard the
2604 line_header when we're done and don't need to record it here. */
2605struct quick_file_names
2606{
094b34ac
DE
2607 /* The data used to construct the hash key. */
2608 struct stmt_list_hash hash;
7b9f3c50
DE
2609
2610 /* The number of entries in file_names, real_names. */
2611 unsigned int num_file_names;
2612
2613 /* The file names from the line table, after being run through
2614 file_full_name. */
2615 const char **file_names;
2616
2617 /* The file names from the line table after being run through
2618 gdb_realpath. These are computed lazily. */
2619 const char **real_names;
2620};
2621
2622/* When using the index (and thus not using psymtabs), each CU has an
2623 object of this type. This is used to hold information needed by
2624 the various "quick" methods. */
2625struct dwarf2_per_cu_quick_data
2626{
2627 /* The file table. This can be NULL if there was no file table
2628 or it's currently not read in.
2629 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2630 struct quick_file_names *file_names;
2631
2632 /* The corresponding symbol table. This is NULL if symbols for this
2633 CU have not yet been read. */
43f3e411 2634 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2635
2636 /* A temporary mark bit used when iterating over all CUs in
2637 expand_symtabs_matching. */
2638 unsigned int mark : 1;
2639
2640 /* True if we've tried to read the file table and found there isn't one.
2641 There will be no point in trying to read it again next time. */
2642 unsigned int no_file_data : 1;
2643};
2644
094b34ac
DE
2645/* Utility hash function for a stmt_list_hash. */
2646
2647static hashval_t
2648hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2649{
2650 hashval_t v = 0;
2651
2652 if (stmt_list_hash->dwo_unit != NULL)
2653 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2654 v += stmt_list_hash->line_offset.sect_off;
2655 return v;
2656}
2657
2658/* Utility equality function for a stmt_list_hash. */
2659
2660static int
2661eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2662 const struct stmt_list_hash *rhs)
2663{
2664 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2665 return 0;
2666 if (lhs->dwo_unit != NULL
2667 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2668 return 0;
2669
2670 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2671}
2672
7b9f3c50
DE
2673/* Hash function for a quick_file_names. */
2674
2675static hashval_t
2676hash_file_name_entry (const void *e)
2677{
9a3c8263
SM
2678 const struct quick_file_names *file_data
2679 = (const struct quick_file_names *) e;
7b9f3c50 2680
094b34ac 2681 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2682}
2683
2684/* Equality function for a quick_file_names. */
2685
2686static int
2687eq_file_name_entry (const void *a, const void *b)
2688{
9a3c8263
SM
2689 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2690 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2691
094b34ac 2692 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2693}
2694
2695/* Delete function for a quick_file_names. */
2696
2697static void
2698delete_file_name_entry (void *e)
2699{
9a3c8263 2700 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2701 int i;
2702
2703 for (i = 0; i < file_data->num_file_names; ++i)
2704 {
2705 xfree ((void*) file_data->file_names[i]);
2706 if (file_data->real_names)
2707 xfree ((void*) file_data->real_names[i]);
2708 }
2709
2710 /* The space for the struct itself lives on objfile_obstack,
2711 so we don't free it here. */
2712}
2713
2714/* Create a quick_file_names hash table. */
2715
2716static htab_t
2717create_quick_file_names_table (unsigned int nr_initial_entries)
2718{
2719 return htab_create_alloc (nr_initial_entries,
2720 hash_file_name_entry, eq_file_name_entry,
2721 delete_file_name_entry, xcalloc, xfree);
2722}
9291a0cd 2723
918dd910
JK
2724/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2725 have to be created afterwards. You should call age_cached_comp_units after
2726 processing PER_CU->CU. dw2_setup must have been already called. */
2727
2728static void
2729load_cu (struct dwarf2_per_cu_data *per_cu)
2730{
3019eac3 2731 if (per_cu->is_debug_types)
e5fe5e75 2732 load_full_type_unit (per_cu);
918dd910 2733 else
95554aad 2734 load_full_comp_unit (per_cu, language_minimal);
918dd910 2735
cc12ce38
DE
2736 if (per_cu->cu == NULL)
2737 return; /* Dummy CU. */
2dc860c0
DE
2738
2739 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2740}
2741
a0f42c21 2742/* Read in the symbols for PER_CU. */
2fdf6df6 2743
9291a0cd 2744static void
a0f42c21 2745dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2746{
2747 struct cleanup *back_to;
2748
f4dc4d17
DE
2749 /* Skip type_unit_groups, reading the type units they contain
2750 is handled elsewhere. */
2751 if (IS_TYPE_UNIT_GROUP (per_cu))
2752 return;
2753
9291a0cd
TT
2754 back_to = make_cleanup (dwarf2_release_queue, NULL);
2755
95554aad 2756 if (dwarf2_per_objfile->using_index
43f3e411 2757 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2758 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2759 {
2760 queue_comp_unit (per_cu, language_minimal);
2761 load_cu (per_cu);
89e63ee4
DE
2762
2763 /* If we just loaded a CU from a DWO, and we're working with an index
2764 that may badly handle TUs, load all the TUs in that DWO as well.
2765 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2766 if (!per_cu->is_debug_types
cc12ce38 2767 && per_cu->cu != NULL
89e63ee4
DE
2768 && per_cu->cu->dwo_unit != NULL
2769 && dwarf2_per_objfile->index_table != NULL
2770 && dwarf2_per_objfile->index_table->version <= 7
2771 /* DWP files aren't supported yet. */
2772 && get_dwp_file () == NULL)
2773 queue_and_load_all_dwo_tus (per_cu);
95554aad 2774 }
9291a0cd 2775
a0f42c21 2776 process_queue ();
9291a0cd
TT
2777
2778 /* Age the cache, releasing compilation units that have not
2779 been used recently. */
2780 age_cached_comp_units ();
2781
2782 do_cleanups (back_to);
2783}
2784
2785/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2786 the objfile from which this CU came. Returns the resulting symbol
2787 table. */
2fdf6df6 2788
43f3e411 2789static struct compunit_symtab *
a0f42c21 2790dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2791{
95554aad 2792 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2793 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2794 {
2795 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2796 increment_reading_symtab ();
a0f42c21 2797 dw2_do_instantiate_symtab (per_cu);
95554aad 2798 process_cu_includes ();
9291a0cd
TT
2799 do_cleanups (back_to);
2800 }
f194fefb 2801
43f3e411 2802 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2803}
2804
8832e7e3 2805/* Return the CU/TU given its index.
f4dc4d17
DE
2806
2807 This is intended for loops like:
2808
2809 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2810 + dwarf2_per_objfile->n_type_units); ++i)
2811 {
8832e7e3 2812 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2813
2814 ...;
2815 }
2816*/
2fdf6df6 2817
1fd400ff 2818static struct dwarf2_per_cu_data *
8832e7e3 2819dw2_get_cutu (int index)
1fd400ff
TT
2820{
2821 if (index >= dwarf2_per_objfile->n_comp_units)
2822 {
f4dc4d17 2823 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2824 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2825 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2826 }
2827
2828 return dwarf2_per_objfile->all_comp_units[index];
2829}
2830
8832e7e3
DE
2831/* Return the CU given its index.
2832 This differs from dw2_get_cutu in that it's for when you know INDEX
2833 refers to a CU. */
f4dc4d17
DE
2834
2835static struct dwarf2_per_cu_data *
8832e7e3 2836dw2_get_cu (int index)
f4dc4d17 2837{
8832e7e3 2838 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2839
1fd400ff
TT
2840 return dwarf2_per_objfile->all_comp_units[index];
2841}
2842
2ec9a5e0
TT
2843/* A helper for create_cus_from_index that handles a given list of
2844 CUs. */
2fdf6df6 2845
74a0d9f6 2846static void
2ec9a5e0
TT
2847create_cus_from_index_list (struct objfile *objfile,
2848 const gdb_byte *cu_list, offset_type n_elements,
2849 struct dwarf2_section_info *section,
2850 int is_dwz,
2851 int base_offset)
9291a0cd
TT
2852{
2853 offset_type i;
9291a0cd 2854
2ec9a5e0 2855 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2856 {
2857 struct dwarf2_per_cu_data *the_cu;
2858 ULONGEST offset, length;
2859
74a0d9f6
JK
2860 gdb_static_assert (sizeof (ULONGEST) >= 8);
2861 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2862 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2863 cu_list += 2 * 8;
2864
2865 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2866 struct dwarf2_per_cu_data);
b64f50a1 2867 the_cu->offset.sect_off = offset;
9291a0cd
TT
2868 the_cu->length = length;
2869 the_cu->objfile = objfile;
8a0459fd 2870 the_cu->section = section;
9291a0cd
TT
2871 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2872 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2873 the_cu->is_dwz = is_dwz;
2874 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2875 }
9291a0cd
TT
2876}
2877
2ec9a5e0 2878/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2879 the CU objects for this objfile. */
2ec9a5e0 2880
74a0d9f6 2881static void
2ec9a5e0
TT
2882create_cus_from_index (struct objfile *objfile,
2883 const gdb_byte *cu_list, offset_type cu_list_elements,
2884 const gdb_byte *dwz_list, offset_type dwz_elements)
2885{
2886 struct dwz_file *dwz;
2887
2888 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
2889 dwarf2_per_objfile->all_comp_units =
2890 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2891 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 2892
74a0d9f6
JK
2893 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2894 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2895
2896 if (dwz_elements == 0)
74a0d9f6 2897 return;
2ec9a5e0
TT
2898
2899 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2900 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2901 cu_list_elements / 2);
2ec9a5e0
TT
2902}
2903
1fd400ff 2904/* Create the signatured type hash table from the index. */
673bfd45 2905
74a0d9f6 2906static void
673bfd45 2907create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2908 struct dwarf2_section_info *section,
673bfd45
DE
2909 const gdb_byte *bytes,
2910 offset_type elements)
1fd400ff
TT
2911{
2912 offset_type i;
673bfd45 2913 htab_t sig_types_hash;
1fd400ff 2914
6aa5f3a6
DE
2915 dwarf2_per_objfile->n_type_units
2916 = dwarf2_per_objfile->n_allocated_type_units
2917 = elements / 3;
8d749320
SM
2918 dwarf2_per_objfile->all_type_units =
2919 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 2920
673bfd45 2921 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2922
2923 for (i = 0; i < elements; i += 3)
2924 {
52dc124a
DE
2925 struct signatured_type *sig_type;
2926 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2927 void **slot;
2928
74a0d9f6
JK
2929 gdb_static_assert (sizeof (ULONGEST) >= 8);
2930 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2931 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2932 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2933 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2934 bytes += 3 * 8;
2935
52dc124a 2936 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2937 struct signatured_type);
52dc124a 2938 sig_type->signature = signature;
3019eac3
DE
2939 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2940 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2941 sig_type->per_cu.section = section;
52dc124a
DE
2942 sig_type->per_cu.offset.sect_off = offset;
2943 sig_type->per_cu.objfile = objfile;
2944 sig_type->per_cu.v.quick
1fd400ff
TT
2945 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2946 struct dwarf2_per_cu_quick_data);
2947
52dc124a
DE
2948 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2949 *slot = sig_type;
1fd400ff 2950
b4dd5633 2951 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2952 }
2953
673bfd45 2954 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2955}
2956
9291a0cd
TT
2957/* Read the address map data from the mapped index, and use it to
2958 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2959
9291a0cd
TT
2960static void
2961create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2962{
3e29f34a 2963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd
TT
2964 const gdb_byte *iter, *end;
2965 struct obstack temp_obstack;
2966 struct addrmap *mutable_map;
2967 struct cleanup *cleanup;
2968 CORE_ADDR baseaddr;
2969
2970 obstack_init (&temp_obstack);
2971 cleanup = make_cleanup_obstack_free (&temp_obstack);
2972 mutable_map = addrmap_create_mutable (&temp_obstack);
2973
2974 iter = index->address_table;
2975 end = iter + index->address_table_size;
2976
2977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2978
2979 while (iter < end)
2980 {
2981 ULONGEST hi, lo, cu_index;
2982 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2983 iter += 8;
2984 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2985 iter += 8;
2986 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2987 iter += 4;
f652bce2 2988
24a55014 2989 if (lo > hi)
f652bce2 2990 {
24a55014
DE
2991 complaint (&symfile_complaints,
2992 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2993 hex_string (lo), hex_string (hi));
24a55014 2994 continue;
f652bce2 2995 }
24a55014
DE
2996
2997 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2998 {
2999 complaint (&symfile_complaints,
3000 _(".gdb_index address table has invalid CU number %u"),
3001 (unsigned) cu_index);
24a55014 3002 continue;
f652bce2 3003 }
24a55014 3004
3e29f34a
MR
3005 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3006 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3007 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3008 }
3009
3010 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3011 &objfile->objfile_obstack);
3012 do_cleanups (cleanup);
3013}
3014
59d7bcaf
JK
3015/* The hash function for strings in the mapped index. This is the same as
3016 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3017 implementation. This is necessary because the hash function is tied to the
3018 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3019 SYMBOL_HASH_NEXT.
3020
3021 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3022
9291a0cd 3023static hashval_t
559a7a62 3024mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3025{
3026 const unsigned char *str = (const unsigned char *) p;
3027 hashval_t r = 0;
3028 unsigned char c;
3029
3030 while ((c = *str++) != 0)
559a7a62
JK
3031 {
3032 if (index_version >= 5)
3033 c = tolower (c);
3034 r = r * 67 + c - 113;
3035 }
9291a0cd
TT
3036
3037 return r;
3038}
3039
3040/* Find a slot in the mapped index INDEX for the object named NAME.
3041 If NAME is found, set *VEC_OUT to point to the CU vector in the
3042 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3043
9291a0cd
TT
3044static int
3045find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3046 offset_type **vec_out)
3047{
0cf03b49
JK
3048 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3049 offset_type hash;
9291a0cd 3050 offset_type slot, step;
559a7a62 3051 int (*cmp) (const char *, const char *);
9291a0cd 3052
0cf03b49 3053 if (current_language->la_language == language_cplus
45280282
IB
3054 || current_language->la_language == language_fortran
3055 || current_language->la_language == language_d)
0cf03b49
JK
3056 {
3057 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3058 not contain any. */
a8719064 3059
72998fb3 3060 if (strchr (name, '(') != NULL)
0cf03b49 3061 {
72998fb3 3062 char *without_params = cp_remove_params (name);
0cf03b49 3063
72998fb3
DE
3064 if (without_params != NULL)
3065 {
3066 make_cleanup (xfree, without_params);
3067 name = without_params;
3068 }
0cf03b49
JK
3069 }
3070 }
3071
559a7a62 3072 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3073 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3074 simulate our NAME being searched is also lowercased. */
3075 hash = mapped_index_string_hash ((index->version == 4
3076 && case_sensitivity == case_sensitive_off
3077 ? 5 : index->version),
3078 name);
3079
3876f04e
DE
3080 slot = hash & (index->symbol_table_slots - 1);
3081 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3082 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3083
3084 for (;;)
3085 {
3086 /* Convert a slot number to an offset into the table. */
3087 offset_type i = 2 * slot;
3088 const char *str;
3876f04e 3089 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3090 {
3091 do_cleanups (back_to);
3092 return 0;
3093 }
9291a0cd 3094
3876f04e 3095 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3096 if (!cmp (name, str))
9291a0cd
TT
3097 {
3098 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3099 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3100 do_cleanups (back_to);
9291a0cd
TT
3101 return 1;
3102 }
3103
3876f04e 3104 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3105 }
3106}
3107
2ec9a5e0
TT
3108/* A helper function that reads the .gdb_index from SECTION and fills
3109 in MAP. FILENAME is the name of the file containing the section;
3110 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3111 ok to use deprecated sections.
3112
3113 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3114 out parameters that are filled in with information about the CU and
3115 TU lists in the section.
3116
3117 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3118
9291a0cd 3119static int
2ec9a5e0
TT
3120read_index_from_section (struct objfile *objfile,
3121 const char *filename,
3122 int deprecated_ok,
3123 struct dwarf2_section_info *section,
3124 struct mapped_index *map,
3125 const gdb_byte **cu_list,
3126 offset_type *cu_list_elements,
3127 const gdb_byte **types_list,
3128 offset_type *types_list_elements)
9291a0cd 3129{
948f8e3d 3130 const gdb_byte *addr;
2ec9a5e0 3131 offset_type version;
b3b272e1 3132 offset_type *metadata;
1fd400ff 3133 int i;
9291a0cd 3134
2ec9a5e0 3135 if (dwarf2_section_empty_p (section))
9291a0cd 3136 return 0;
82430852
JK
3137
3138 /* Older elfutils strip versions could keep the section in the main
3139 executable while splitting it for the separate debug info file. */
a32a8923 3140 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3141 return 0;
3142
2ec9a5e0 3143 dwarf2_read_section (objfile, section);
9291a0cd 3144
2ec9a5e0 3145 addr = section->buffer;
9291a0cd 3146 /* Version check. */
1fd400ff 3147 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3148 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3149 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3150 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3151 indices. */
831adc1f 3152 if (version < 4)
481860b3
GB
3153 {
3154 static int warning_printed = 0;
3155 if (!warning_printed)
3156 {
3157 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3158 filename);
481860b3
GB
3159 warning_printed = 1;
3160 }
3161 return 0;
3162 }
3163 /* Index version 4 uses a different hash function than index version
3164 5 and later.
3165
3166 Versions earlier than 6 did not emit psymbols for inlined
3167 functions. Using these files will cause GDB not to be able to
3168 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3169 indices unless the user has done
3170 "set use-deprecated-index-sections on". */
2ec9a5e0 3171 if (version < 6 && !deprecated_ok)
481860b3
GB
3172 {
3173 static int warning_printed = 0;
3174 if (!warning_printed)
3175 {
e615022a
DE
3176 warning (_("\
3177Skipping deprecated .gdb_index section in %s.\n\
3178Do \"set use-deprecated-index-sections on\" before the file is read\n\
3179to use the section anyway."),
2ec9a5e0 3180 filename);
481860b3
GB
3181 warning_printed = 1;
3182 }
3183 return 0;
3184 }
796a7ff8 3185 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3186 of the TU (for symbols coming from TUs),
3187 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3188 Plus gold-generated indices can have duplicate entries for global symbols,
3189 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3190 These are just performance bugs, and we can't distinguish gdb-generated
3191 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3192
481860b3 3193 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3194 longer backward compatible. */
796a7ff8 3195 if (version > 8)
594e8718 3196 return 0;
9291a0cd 3197
559a7a62 3198 map->version = version;
2ec9a5e0 3199 map->total_size = section->size;
9291a0cd
TT
3200
3201 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3202
3203 i = 0;
2ec9a5e0
TT
3204 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3205 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3206 / 8);
1fd400ff
TT
3207 ++i;
3208
2ec9a5e0
TT
3209 *types_list = addr + MAYBE_SWAP (metadata[i]);
3210 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3211 - MAYBE_SWAP (metadata[i]))
3212 / 8);
987d643c 3213 ++i;
1fd400ff
TT
3214
3215 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3216 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3217 - MAYBE_SWAP (metadata[i]));
3218 ++i;
3219
3876f04e
DE
3220 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3221 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3222 - MAYBE_SWAP (metadata[i]))
3223 / (2 * sizeof (offset_type)));
1fd400ff 3224 ++i;
9291a0cd 3225
f9d83a0b 3226 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3227
2ec9a5e0
TT
3228 return 1;
3229}
3230
3231
3232/* Read the index file. If everything went ok, initialize the "quick"
3233 elements of all the CUs and return 1. Otherwise, return 0. */
3234
3235static int
3236dwarf2_read_index (struct objfile *objfile)
3237{
3238 struct mapped_index local_map, *map;
3239 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3240 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3241 struct dwz_file *dwz;
2ec9a5e0 3242
4262abfb 3243 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3244 use_deprecated_index_sections,
3245 &dwarf2_per_objfile->gdb_index, &local_map,
3246 &cu_list, &cu_list_elements,
3247 &types_list, &types_list_elements))
3248 return 0;
3249
0fefef59 3250 /* Don't use the index if it's empty. */
2ec9a5e0 3251 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3252 return 0;
3253
2ec9a5e0
TT
3254 /* If there is a .dwz file, read it so we can get its CU list as
3255 well. */
4db1a1dc
TT
3256 dwz = dwarf2_get_dwz_file ();
3257 if (dwz != NULL)
2ec9a5e0 3258 {
2ec9a5e0
TT
3259 struct mapped_index dwz_map;
3260 const gdb_byte *dwz_types_ignore;
3261 offset_type dwz_types_elements_ignore;
3262
3263 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3264 1,
3265 &dwz->gdb_index, &dwz_map,
3266 &dwz_list, &dwz_list_elements,
3267 &dwz_types_ignore,
3268 &dwz_types_elements_ignore))
3269 {
3270 warning (_("could not read '.gdb_index' section from %s; skipping"),
3271 bfd_get_filename (dwz->dwz_bfd));
3272 return 0;
3273 }
3274 }
3275
74a0d9f6
JK
3276 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3277 dwz_list_elements);
1fd400ff 3278
8b70b953
TT
3279 if (types_list_elements)
3280 {
3281 struct dwarf2_section_info *section;
3282
3283 /* We can only handle a single .debug_types when we have an
3284 index. */
3285 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3286 return 0;
3287
3288 section = VEC_index (dwarf2_section_info_def,
3289 dwarf2_per_objfile->types, 0);
3290
74a0d9f6
JK
3291 create_signatured_type_table_from_index (objfile, section, types_list,
3292 types_list_elements);
8b70b953 3293 }
9291a0cd 3294
2ec9a5e0
TT
3295 create_addrmap_from_index (objfile, &local_map);
3296
8d749320 3297 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3298 *map = local_map;
9291a0cd
TT
3299
3300 dwarf2_per_objfile->index_table = map;
3301 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3302 dwarf2_per_objfile->quick_file_names_table =
3303 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3304
3305 return 1;
3306}
3307
3308/* A helper for the "quick" functions which sets the global
3309 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3310
9291a0cd
TT
3311static void
3312dw2_setup (struct objfile *objfile)
3313{
9a3c8263
SM
3314 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3315 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3316 gdb_assert (dwarf2_per_objfile);
3317}
3318
dee91e82 3319/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3320
dee91e82
DE
3321static void
3322dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3323 const gdb_byte *info_ptr,
dee91e82
DE
3324 struct die_info *comp_unit_die,
3325 int has_children,
3326 void *data)
9291a0cd 3327{
dee91e82
DE
3328 struct dwarf2_cu *cu = reader->cu;
3329 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3330 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3331 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 3332 struct line_header *lh;
9291a0cd 3333 struct attribute *attr;
dee91e82 3334 int i;
15d034d0 3335 const char *name, *comp_dir;
7b9f3c50
DE
3336 void **slot;
3337 struct quick_file_names *qfn;
3338 unsigned int line_offset;
9291a0cd 3339
0186c6a7
DE
3340 gdb_assert (! this_cu->is_debug_types);
3341
07261596
TT
3342 /* Our callers never want to match partial units -- instead they
3343 will match the enclosing full CU. */
3344 if (comp_unit_die->tag == DW_TAG_partial_unit)
3345 {
3346 this_cu->v.quick->no_file_data = 1;
3347 return;
3348 }
3349
0186c6a7 3350 lh_cu = this_cu;
7b9f3c50
DE
3351 lh = NULL;
3352 slot = NULL;
3353 line_offset = 0;
dee91e82
DE
3354
3355 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3356 if (attr)
3357 {
7b9f3c50
DE
3358 struct quick_file_names find_entry;
3359
3360 line_offset = DW_UNSND (attr);
3361
3362 /* We may have already read in this line header (TU line header sharing).
3363 If we have we're done. */
094b34ac
DE
3364 find_entry.hash.dwo_unit = cu->dwo_unit;
3365 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3366 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3367 &find_entry, INSERT);
3368 if (*slot != NULL)
3369 {
9a3c8263 3370 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3371 return;
7b9f3c50
DE
3372 }
3373
3019eac3 3374 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3375 }
3376 if (lh == NULL)
3377 {
094b34ac 3378 lh_cu->v.quick->no_file_data = 1;
dee91e82 3379 return;
9291a0cd
TT
3380 }
3381
8d749320 3382 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac
DE
3383 qfn->hash.dwo_unit = cu->dwo_unit;
3384 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3385 gdb_assert (slot != NULL);
3386 *slot = qfn;
9291a0cd 3387
dee91e82 3388 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 3389
7b9f3c50 3390 qfn->num_file_names = lh->num_file_names;
8d749320
SM
3391 qfn->file_names =
3392 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
9291a0cd 3393 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
3394 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3395 qfn->real_names = NULL;
9291a0cd 3396
7b9f3c50 3397 free_line_header (lh);
7b9f3c50 3398
094b34ac 3399 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3400}
3401
3402/* A helper for the "quick" functions which attempts to read the line
3403 table for THIS_CU. */
3404
3405static struct quick_file_names *
e4a48d9d 3406dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3407{
0186c6a7
DE
3408 /* This should never be called for TUs. */
3409 gdb_assert (! this_cu->is_debug_types);
3410 /* Nor type unit groups. */
3411 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3412
dee91e82
DE
3413 if (this_cu->v.quick->file_names != NULL)
3414 return this_cu->v.quick->file_names;
3415 /* If we know there is no line data, no point in looking again. */
3416 if (this_cu->v.quick->no_file_data)
3417 return NULL;
3418
0186c6a7 3419 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3420
3421 if (this_cu->v.quick->no_file_data)
3422 return NULL;
3423 return this_cu->v.quick->file_names;
9291a0cd
TT
3424}
3425
3426/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3427 real path for a given file name from the line table. */
2fdf6df6 3428
9291a0cd 3429static const char *
7b9f3c50
DE
3430dw2_get_real_path (struct objfile *objfile,
3431 struct quick_file_names *qfn, int index)
9291a0cd 3432{
7b9f3c50
DE
3433 if (qfn->real_names == NULL)
3434 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3435 qfn->num_file_names, const char *);
9291a0cd 3436
7b9f3c50
DE
3437 if (qfn->real_names[index] == NULL)
3438 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3439
7b9f3c50 3440 return qfn->real_names[index];
9291a0cd
TT
3441}
3442
3443static struct symtab *
3444dw2_find_last_source_symtab (struct objfile *objfile)
3445{
43f3e411 3446 struct compunit_symtab *cust;
9291a0cd 3447 int index;
ae2de4f8 3448
9291a0cd
TT
3449 dw2_setup (objfile);
3450 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3451 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3452 if (cust == NULL)
3453 return NULL;
3454 return compunit_primary_filetab (cust);
9291a0cd
TT
3455}
3456
7b9f3c50
DE
3457/* Traversal function for dw2_forget_cached_source_info. */
3458
3459static int
3460dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3461{
7b9f3c50 3462 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3463
7b9f3c50 3464 if (file_data->real_names)
9291a0cd 3465 {
7b9f3c50 3466 int i;
9291a0cd 3467
7b9f3c50 3468 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3469 {
7b9f3c50
DE
3470 xfree ((void*) file_data->real_names[i]);
3471 file_data->real_names[i] = NULL;
9291a0cd
TT
3472 }
3473 }
7b9f3c50
DE
3474
3475 return 1;
3476}
3477
3478static void
3479dw2_forget_cached_source_info (struct objfile *objfile)
3480{
3481 dw2_setup (objfile);
3482
3483 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3484 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3485}
3486
f8eba3c6
TT
3487/* Helper function for dw2_map_symtabs_matching_filename that expands
3488 the symtabs and calls the iterator. */
3489
3490static int
3491dw2_map_expand_apply (struct objfile *objfile,
3492 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3493 const char *name, const char *real_path,
14bc53a8 3494 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3495{
43f3e411 3496 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3497
3498 /* Don't visit already-expanded CUs. */
43f3e411 3499 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3500 return 0;
3501
3502 /* This may expand more than one symtab, and we want to iterate over
3503 all of them. */
a0f42c21 3504 dw2_instantiate_symtab (per_cu);
f8eba3c6 3505
14bc53a8
PA
3506 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3507 last_made, callback);
f8eba3c6
TT
3508}
3509
3510/* Implementation of the map_symtabs_matching_filename method. */
3511
14bc53a8
PA
3512static bool
3513dw2_map_symtabs_matching_filename
3514 (struct objfile *objfile, const char *name, const char *real_path,
3515 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3516{
3517 int i;
c011a4f4 3518 const char *name_basename = lbasename (name);
9291a0cd
TT
3519
3520 dw2_setup (objfile);
ae2de4f8 3521
848e3e78
DE
3522 /* The rule is CUs specify all the files, including those used by
3523 any TU, so there's no need to scan TUs here. */
f4dc4d17 3524
848e3e78 3525 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3526 {
3527 int j;
8832e7e3 3528 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3529 struct quick_file_names *file_data;
9291a0cd 3530
3d7bb9d9 3531 /* We only need to look at symtabs not already expanded. */
43f3e411 3532 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3533 continue;
3534
e4a48d9d 3535 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3536 if (file_data == NULL)
9291a0cd
TT
3537 continue;
3538
7b9f3c50 3539 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3540 {
7b9f3c50 3541 const char *this_name = file_data->file_names[j];
da235a7c 3542 const char *this_real_name;
9291a0cd 3543
af529f8f 3544 if (compare_filenames_for_search (this_name, name))
9291a0cd 3545 {
f5b95b50 3546 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3547 callback))
3548 return true;
288e77a7 3549 continue;
4aac40c8 3550 }
9291a0cd 3551
c011a4f4
DE
3552 /* Before we invoke realpath, which can get expensive when many
3553 files are involved, do a quick comparison of the basenames. */
3554 if (! basenames_may_differ
3555 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3556 continue;
3557
da235a7c
JK
3558 this_real_name = dw2_get_real_path (objfile, file_data, j);
3559 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3560 {
da235a7c 3561 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3562 callback))
3563 return true;
288e77a7 3564 continue;
da235a7c 3565 }
9291a0cd 3566
da235a7c
JK
3567 if (real_path != NULL)
3568 {
af529f8f
JK
3569 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3570 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3571 if (this_real_name != NULL
af529f8f 3572 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3573 {
f5b95b50 3574 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3575 callback))
3576 return true;
288e77a7 3577 continue;
9291a0cd
TT
3578 }
3579 }
3580 }
3581 }
3582
14bc53a8 3583 return false;
9291a0cd
TT
3584}
3585
da51c347
DE
3586/* Struct used to manage iterating over all CUs looking for a symbol. */
3587
3588struct dw2_symtab_iterator
9291a0cd 3589{
da51c347
DE
3590 /* The internalized form of .gdb_index. */
3591 struct mapped_index *index;
3592 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3593 int want_specific_block;
3594 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3595 Unused if !WANT_SPECIFIC_BLOCK. */
3596 int block_index;
3597 /* The kind of symbol we're looking for. */
3598 domain_enum domain;
3599 /* The list of CUs from the index entry of the symbol,
3600 or NULL if not found. */
3601 offset_type *vec;
3602 /* The next element in VEC to look at. */
3603 int next;
3604 /* The number of elements in VEC, or zero if there is no match. */
3605 int length;
8943b874
DE
3606 /* Have we seen a global version of the symbol?
3607 If so we can ignore all further global instances.
3608 This is to work around gold/15646, inefficient gold-generated
3609 indices. */
3610 int global_seen;
da51c347 3611};
9291a0cd 3612
da51c347
DE
3613/* Initialize the index symtab iterator ITER.
3614 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3615 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3616
9291a0cd 3617static void
da51c347
DE
3618dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3619 struct mapped_index *index,
3620 int want_specific_block,
3621 int block_index,
3622 domain_enum domain,
3623 const char *name)
3624{
3625 iter->index = index;
3626 iter->want_specific_block = want_specific_block;
3627 iter->block_index = block_index;
3628 iter->domain = domain;
3629 iter->next = 0;
8943b874 3630 iter->global_seen = 0;
da51c347
DE
3631
3632 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3633 iter->length = MAYBE_SWAP (*iter->vec);
3634 else
3635 {
3636 iter->vec = NULL;
3637 iter->length = 0;
3638 }
3639}
3640
3641/* Return the next matching CU or NULL if there are no more. */
3642
3643static struct dwarf2_per_cu_data *
3644dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3645{
3646 for ( ; iter->next < iter->length; ++iter->next)
3647 {
3648 offset_type cu_index_and_attrs =
3649 MAYBE_SWAP (iter->vec[iter->next + 1]);
3650 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3651 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3652 int want_static = iter->block_index != GLOBAL_BLOCK;
3653 /* This value is only valid for index versions >= 7. */
3654 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3655 gdb_index_symbol_kind symbol_kind =
3656 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3657 /* Only check the symbol attributes if they're present.
3658 Indices prior to version 7 don't record them,
3659 and indices >= 7 may elide them for certain symbols
3660 (gold does this). */
3661 int attrs_valid =
3662 (iter->index->version >= 7
3663 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3664
3190f0c6
DE
3665 /* Don't crash on bad data. */
3666 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3667 + dwarf2_per_objfile->n_type_units))
3668 {
3669 complaint (&symfile_complaints,
3670 _(".gdb_index entry has bad CU index"
4262abfb
JK
3671 " [in module %s]"),
3672 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3673 continue;
3674 }
3675
8832e7e3 3676 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3677
da51c347 3678 /* Skip if already read in. */
43f3e411 3679 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3680 continue;
3681
8943b874
DE
3682 /* Check static vs global. */
3683 if (attrs_valid)
3684 {
3685 if (iter->want_specific_block
3686 && want_static != is_static)
3687 continue;
3688 /* Work around gold/15646. */
3689 if (!is_static && iter->global_seen)
3690 continue;
3691 if (!is_static)
3692 iter->global_seen = 1;
3693 }
da51c347
DE
3694
3695 /* Only check the symbol's kind if it has one. */
3696 if (attrs_valid)
3697 {
3698 switch (iter->domain)
3699 {
3700 case VAR_DOMAIN:
3701 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3702 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3703 /* Some types are also in VAR_DOMAIN. */
3704 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3705 continue;
3706 break;
3707 case STRUCT_DOMAIN:
3708 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3709 continue;
3710 break;
3711 case LABEL_DOMAIN:
3712 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3713 continue;
3714 break;
3715 default:
3716 break;
3717 }
3718 }
3719
3720 ++iter->next;
3721 return per_cu;
3722 }
3723
3724 return NULL;
3725}
3726
43f3e411 3727static struct compunit_symtab *
da51c347
DE
3728dw2_lookup_symbol (struct objfile *objfile, int block_index,
3729 const char *name, domain_enum domain)
9291a0cd 3730{
43f3e411 3731 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3732 struct mapped_index *index;
3733
9291a0cd
TT
3734 dw2_setup (objfile);
3735
156942c7
DE
3736 index = dwarf2_per_objfile->index_table;
3737
da51c347 3738 /* index is NULL if OBJF_READNOW. */
156942c7 3739 if (index)
9291a0cd 3740 {
da51c347
DE
3741 struct dw2_symtab_iterator iter;
3742 struct dwarf2_per_cu_data *per_cu;
3743
3744 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3745
da51c347 3746 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3747 {
b2e2f908 3748 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3749 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3750 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3751 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3752
b2e2f908
DE
3753 sym = block_find_symbol (block, name, domain,
3754 block_find_non_opaque_type_preferred,
3755 &with_opaque);
3756
da51c347
DE
3757 /* Some caution must be observed with overloaded functions
3758 and methods, since the index will not contain any overload
3759 information (but NAME might contain it). */
da51c347 3760
b2e2f908
DE
3761 if (sym != NULL
3762 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3763 return stab;
3764 if (with_opaque != NULL
3765 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3766 stab_best = stab;
da51c347
DE
3767
3768 /* Keep looking through other CUs. */
9291a0cd
TT
3769 }
3770 }
9291a0cd 3771
da51c347 3772 return stab_best;
9291a0cd
TT
3773}
3774
3775static void
3776dw2_print_stats (struct objfile *objfile)
3777{
e4a48d9d 3778 int i, total, count;
9291a0cd
TT
3779
3780 dw2_setup (objfile);
e4a48d9d 3781 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3782 count = 0;
e4a48d9d 3783 for (i = 0; i < total; ++i)
9291a0cd 3784 {
8832e7e3 3785 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3786
43f3e411 3787 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3788 ++count;
3789 }
e4a48d9d 3790 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3791 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3792}
3793
779bd270
DE
3794/* This dumps minimal information about the index.
3795 It is called via "mt print objfiles".
3796 One use is to verify .gdb_index has been loaded by the
3797 gdb.dwarf2/gdb-index.exp testcase. */
3798
9291a0cd
TT
3799static void
3800dw2_dump (struct objfile *objfile)
3801{
779bd270
DE
3802 dw2_setup (objfile);
3803 gdb_assert (dwarf2_per_objfile->using_index);
3804 printf_filtered (".gdb_index:");
3805 if (dwarf2_per_objfile->index_table != NULL)
3806 {
3807 printf_filtered (" version %d\n",
3808 dwarf2_per_objfile->index_table->version);
3809 }
3810 else
3811 printf_filtered (" faked for \"readnow\"\n");
3812 printf_filtered ("\n");
9291a0cd
TT
3813}
3814
3815static void
3189cb12
DE
3816dw2_relocate (struct objfile *objfile,
3817 const struct section_offsets *new_offsets,
3818 const struct section_offsets *delta)
9291a0cd
TT
3819{
3820 /* There's nothing to relocate here. */
3821}
3822
3823static void
3824dw2_expand_symtabs_for_function (struct objfile *objfile,
3825 const char *func_name)
3826{
da51c347
DE
3827 struct mapped_index *index;
3828
3829 dw2_setup (objfile);
3830
3831 index = dwarf2_per_objfile->index_table;
3832
3833 /* index is NULL if OBJF_READNOW. */
3834 if (index)
3835 {
3836 struct dw2_symtab_iterator iter;
3837 struct dwarf2_per_cu_data *per_cu;
3838
3839 /* Note: It doesn't matter what we pass for block_index here. */
3840 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3841 func_name);
3842
3843 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3844 dw2_instantiate_symtab (per_cu);
3845 }
9291a0cd
TT
3846}
3847
3848static void
3849dw2_expand_all_symtabs (struct objfile *objfile)
3850{
3851 int i;
3852
3853 dw2_setup (objfile);
1fd400ff
TT
3854
3855 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3856 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3857 {
8832e7e3 3858 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3859
a0f42c21 3860 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3861 }
3862}
3863
3864static void
652a8996
JK
3865dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3866 const char *fullname)
9291a0cd
TT
3867{
3868 int i;
3869
3870 dw2_setup (objfile);
d4637a04
DE
3871
3872 /* We don't need to consider type units here.
3873 This is only called for examining code, e.g. expand_line_sal.
3874 There can be an order of magnitude (or more) more type units
3875 than comp units, and we avoid them if we can. */
3876
3877 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3878 {
3879 int j;
8832e7e3 3880 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3881 struct quick_file_names *file_data;
9291a0cd 3882
3d7bb9d9 3883 /* We only need to look at symtabs not already expanded. */
43f3e411 3884 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3885 continue;
3886
e4a48d9d 3887 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3888 if (file_data == NULL)
9291a0cd
TT
3889 continue;
3890
7b9f3c50 3891 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3892 {
652a8996
JK
3893 const char *this_fullname = file_data->file_names[j];
3894
3895 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3896 {
a0f42c21 3897 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3898 break;
3899 }
3900 }
3901 }
3902}
3903
9291a0cd 3904static void
ade7ed9e 3905dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 3906 const char * name, domain_enum domain,
ade7ed9e 3907 int global,
40658b94
PH
3908 int (*callback) (struct block *,
3909 struct symbol *, void *),
2edb89d3
JK
3910 void *data, symbol_compare_ftype *match,
3911 symbol_compare_ftype *ordered_compare)
9291a0cd 3912{
40658b94 3913 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3914 current language is Ada for a non-Ada objfile using GNU index. As Ada
3915 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3916}
3917
3918static void
f8eba3c6
TT
3919dw2_expand_symtabs_matching
3920 (struct objfile *objfile,
14bc53a8
PA
3921 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
3924 enum search_domain kind)
9291a0cd
TT
3925{
3926 int i;
3927 offset_type iter;
4b5246aa 3928 struct mapped_index *index;
9291a0cd
TT
3929
3930 dw2_setup (objfile);
ae2de4f8
DE
3931
3932 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3933 if (!dwarf2_per_objfile->index_table)
3934 return;
4b5246aa 3935 index = dwarf2_per_objfile->index_table;
9291a0cd 3936
7b08b9eb 3937 if (file_matcher != NULL)
24c79950 3938 {
fc4007c9
TT
3939 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3940 htab_eq_pointer,
3941 NULL, xcalloc, xfree));
3942 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3943 htab_eq_pointer,
3944 NULL, xcalloc, xfree));
24c79950 3945
848e3e78
DE
3946 /* The rule is CUs specify all the files, including those used by
3947 any TU, so there's no need to scan TUs here. */
3948
3949 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3950 {
3951 int j;
8832e7e3 3952 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
3953 struct quick_file_names *file_data;
3954 void **slot;
7b08b9eb 3955
61d96d7e
DE
3956 QUIT;
3957
24c79950 3958 per_cu->v.quick->mark = 0;
3d7bb9d9 3959
24c79950 3960 /* We only need to look at symtabs not already expanded. */
43f3e411 3961 if (per_cu->v.quick->compunit_symtab)
24c79950 3962 continue;
7b08b9eb 3963
e4a48d9d 3964 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3965 if (file_data == NULL)
3966 continue;
7b08b9eb 3967
fc4007c9 3968 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 3969 continue;
fc4007c9 3970 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
3971 {
3972 per_cu->v.quick->mark = 1;
3973 continue;
3974 }
3975
3976 for (j = 0; j < file_data->num_file_names; ++j)
3977 {
da235a7c
JK
3978 const char *this_real_name;
3979
14bc53a8 3980 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
3981 {
3982 per_cu->v.quick->mark = 1;
3983 break;
3984 }
da235a7c
JK
3985
3986 /* Before we invoke realpath, which can get expensive when many
3987 files are involved, do a quick comparison of the basenames. */
3988 if (!basenames_may_differ
3989 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 3990 true))
da235a7c
JK
3991 continue;
3992
3993 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 3994 if (file_matcher (this_real_name, false))
da235a7c
JK
3995 {
3996 per_cu->v.quick->mark = 1;
3997 break;
3998 }
24c79950
TT
3999 }
4000
4001 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4002 ? visited_found.get ()
4003 : visited_not_found.get (),
24c79950
TT
4004 file_data, INSERT);
4005 *slot = file_data;
4006 }
24c79950 4007 }
9291a0cd 4008
3876f04e 4009 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4010 {
4011 offset_type idx = 2 * iter;
4012 const char *name;
4013 offset_type *vec, vec_len, vec_idx;
8943b874 4014 int global_seen = 0;
9291a0cd 4015
61d96d7e
DE
4016 QUIT;
4017
3876f04e 4018 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4019 continue;
4020
3876f04e 4021 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4022
14bc53a8 4023 if (!symbol_matcher (name))
9291a0cd
TT
4024 continue;
4025
4026 /* The name was matched, now expand corresponding CUs that were
4027 marked. */
4b5246aa 4028 vec = (offset_type *) (index->constant_pool
3876f04e 4029 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4030 vec_len = MAYBE_SWAP (vec[0]);
4031 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4032 {
e254ef6a 4033 struct dwarf2_per_cu_data *per_cu;
156942c7 4034 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4035 /* This value is only valid for index versions >= 7. */
4036 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4037 gdb_index_symbol_kind symbol_kind =
4038 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4039 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4040 /* Only check the symbol attributes if they're present.
4041 Indices prior to version 7 don't record them,
4042 and indices >= 7 may elide them for certain symbols
4043 (gold does this). */
4044 int attrs_valid =
4045 (index->version >= 7
4046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4047
8943b874
DE
4048 /* Work around gold/15646. */
4049 if (attrs_valid)
4050 {
4051 if (!is_static && global_seen)
4052 continue;
4053 if (!is_static)
4054 global_seen = 1;
4055 }
4056
3190f0c6
DE
4057 /* Only check the symbol's kind if it has one. */
4058 if (attrs_valid)
156942c7
DE
4059 {
4060 switch (kind)
4061 {
4062 case VARIABLES_DOMAIN:
4063 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4064 continue;
4065 break;
4066 case FUNCTIONS_DOMAIN:
4067 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4068 continue;
4069 break;
4070 case TYPES_DOMAIN:
4071 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4072 continue;
4073 break;
4074 default:
4075 break;
4076 }
4077 }
4078
3190f0c6
DE
4079 /* Don't crash on bad data. */
4080 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4081 + dwarf2_per_objfile->n_type_units))
4082 {
4083 complaint (&symfile_complaints,
4084 _(".gdb_index entry has bad CU index"
4262abfb 4085 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4086 continue;
4087 }
4088
8832e7e3 4089 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4090 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4091 {
4092 int symtab_was_null =
4093 (per_cu->v.quick->compunit_symtab == NULL);
4094
4095 dw2_instantiate_symtab (per_cu);
4096
4097 if (expansion_notify != NULL
4098 && symtab_was_null
4099 && per_cu->v.quick->compunit_symtab != NULL)
4100 {
14bc53a8 4101 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4102 }
4103 }
9291a0cd
TT
4104 }
4105 }
4106}
4107
43f3e411 4108/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4109 symtab. */
4110
43f3e411
DE
4111static struct compunit_symtab *
4112recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4113 CORE_ADDR pc)
9703b513
TT
4114{
4115 int i;
4116
43f3e411
DE
4117 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4118 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4119 return cust;
9703b513 4120
43f3e411 4121 if (cust->includes == NULL)
a3ec0bb1
DE
4122 return NULL;
4123
43f3e411 4124 for (i = 0; cust->includes[i]; ++i)
9703b513 4125 {
43f3e411 4126 struct compunit_symtab *s = cust->includes[i];
9703b513 4127
43f3e411 4128 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4129 if (s != NULL)
4130 return s;
4131 }
4132
4133 return NULL;
4134}
4135
43f3e411
DE
4136static struct compunit_symtab *
4137dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4138 struct bound_minimal_symbol msymbol,
4139 CORE_ADDR pc,
4140 struct obj_section *section,
4141 int warn_if_readin)
9291a0cd
TT
4142{
4143 struct dwarf2_per_cu_data *data;
43f3e411 4144 struct compunit_symtab *result;
9291a0cd
TT
4145
4146 dw2_setup (objfile);
4147
4148 if (!objfile->psymtabs_addrmap)
4149 return NULL;
4150
9a3c8263
SM
4151 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4152 pc);
9291a0cd
TT
4153 if (!data)
4154 return NULL;
4155
43f3e411 4156 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4157 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4158 paddress (get_objfile_arch (objfile), pc));
4159
43f3e411
DE
4160 result
4161 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4162 pc);
9703b513
TT
4163 gdb_assert (result != NULL);
4164 return result;
9291a0cd
TT
4165}
4166
9291a0cd 4167static void
44b13c5a 4168dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4169 void *data, int need_fullname)
9291a0cd
TT
4170{
4171 int i;
fc4007c9
TT
4172 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4173 NULL, xcalloc, xfree));
9291a0cd
TT
4174
4175 dw2_setup (objfile);
ae2de4f8 4176
848e3e78
DE
4177 /* The rule is CUs specify all the files, including those used by
4178 any TU, so there's no need to scan TUs here.
4179 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 4180
848e3e78 4181 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4182 {
8832e7e3 4183 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4184
43f3e411 4185 if (per_cu->v.quick->compunit_symtab)
24c79950 4186 {
fc4007c9
TT
4187 void **slot = htab_find_slot (visited.get (),
4188 per_cu->v.quick->file_names,
24c79950
TT
4189 INSERT);
4190
4191 *slot = per_cu->v.quick->file_names;
4192 }
4193 }
4194
848e3e78 4195 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4196 {
4197 int j;
8832e7e3 4198 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4199 struct quick_file_names *file_data;
24c79950 4200 void **slot;
9291a0cd 4201
3d7bb9d9 4202 /* We only need to look at symtabs not already expanded. */
43f3e411 4203 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4204 continue;
4205
e4a48d9d 4206 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4207 if (file_data == NULL)
9291a0cd
TT
4208 continue;
4209
fc4007c9 4210 slot = htab_find_slot (visited.get (), file_data, INSERT);
24c79950
TT
4211 if (*slot)
4212 {
4213 /* Already visited. */
4214 continue;
4215 }
4216 *slot = file_data;
4217
7b9f3c50 4218 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4219 {
74e2f255
DE
4220 const char *this_real_name;
4221
4222 if (need_fullname)
4223 this_real_name = dw2_get_real_path (objfile, file_data, j);
4224 else
4225 this_real_name = NULL;
7b9f3c50 4226 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4227 }
4228 }
4229}
4230
4231static int
4232dw2_has_symbols (struct objfile *objfile)
4233{
4234 return 1;
4235}
4236
4237const struct quick_symbol_functions dwarf2_gdb_index_functions =
4238{
4239 dw2_has_symbols,
4240 dw2_find_last_source_symtab,
4241 dw2_forget_cached_source_info,
f8eba3c6 4242 dw2_map_symtabs_matching_filename,
9291a0cd 4243 dw2_lookup_symbol,
9291a0cd
TT
4244 dw2_print_stats,
4245 dw2_dump,
4246 dw2_relocate,
4247 dw2_expand_symtabs_for_function,
4248 dw2_expand_all_symtabs,
652a8996 4249 dw2_expand_symtabs_with_fullname,
40658b94 4250 dw2_map_matching_symbols,
9291a0cd 4251 dw2_expand_symtabs_matching,
43f3e411 4252 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4253 dw2_map_symbol_filenames
4254};
4255
4256/* Initialize for reading DWARF for this objfile. Return 0 if this
4257 file will use psymtabs, or 1 if using the GNU index. */
4258
4259int
4260dwarf2_initialize_objfile (struct objfile *objfile)
4261{
4262 /* If we're about to read full symbols, don't bother with the
4263 indices. In this case we also don't care if some other debug
4264 format is making psymtabs, because they are all about to be
4265 expanded anyway. */
4266 if ((objfile->flags & OBJF_READNOW))
4267 {
4268 int i;
4269
4270 dwarf2_per_objfile->using_index = 1;
4271 create_all_comp_units (objfile);
0e50663e 4272 create_all_type_units (objfile);
7b9f3c50
DE
4273 dwarf2_per_objfile->quick_file_names_table =
4274 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4275
1fd400ff 4276 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4277 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4278 {
8832e7e3 4279 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4280
e254ef6a
DE
4281 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4282 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4283 }
4284
4285 /* Return 1 so that gdb sees the "quick" functions. However,
4286 these functions will be no-ops because we will have expanded
4287 all symtabs. */
4288 return 1;
4289 }
4290
4291 if (dwarf2_read_index (objfile))
4292 return 1;
4293
9291a0cd
TT
4294 return 0;
4295}
4296
4297\f
4298
dce234bc
PP
4299/* Build a partial symbol table. */
4300
4301void
f29dff0a 4302dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4303{
c9bf0622 4304
f29dff0a 4305 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4306 {
4307 init_psymbol_list (objfile, 1024);
4308 }
4309
492d29ea 4310 TRY
c9bf0622
TT
4311 {
4312 /* This isn't really ideal: all the data we allocate on the
4313 objfile's obstack is still uselessly kept around. However,
4314 freeing it seems unsafe. */
906768f9 4315 psymtab_discarder psymtabs (objfile);
c9bf0622 4316 dwarf2_build_psymtabs_hard (objfile);
906768f9 4317 psymtabs.keep ();
c9bf0622 4318 }
492d29ea
PA
4319 CATCH (except, RETURN_MASK_ERROR)
4320 {
4321 exception_print (gdb_stderr, except);
4322 }
4323 END_CATCH
c906108c 4324}
c906108c 4325
1ce1cefd
DE
4326/* Return the total length of the CU described by HEADER. */
4327
4328static unsigned int
4329get_cu_length (const struct comp_unit_head *header)
4330{
4331 return header->initial_length_size + header->length;
4332}
4333
45452591
DE
4334/* Return TRUE if OFFSET is within CU_HEADER. */
4335
4336static inline int
b64f50a1 4337offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 4338{
b64f50a1 4339 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 4340 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 4341
b64f50a1 4342 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
4343}
4344
3b80fe9b
DE
4345/* Find the base address of the compilation unit for range lists and
4346 location lists. It will normally be specified by DW_AT_low_pc.
4347 In DWARF-3 draft 4, the base address could be overridden by
4348 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4349 compilation units with discontinuous ranges. */
4350
4351static void
4352dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4353{
4354 struct attribute *attr;
4355
4356 cu->base_known = 0;
4357 cu->base_address = 0;
4358
4359 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4360 if (attr)
4361 {
31aa7e4e 4362 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4363 cu->base_known = 1;
4364 }
4365 else
4366 {
4367 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4368 if (attr)
4369 {
31aa7e4e 4370 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4371 cu->base_known = 1;
4372 }
4373 }
4374}
4375
93311388 4376/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4377 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4378 NOTE: This leaves members offset, first_die_offset to be filled in
4379 by the caller. */
107d2387 4380
d521ce57 4381static const gdb_byte *
107d2387 4382read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4383 const gdb_byte *info_ptr,
4384 struct dwarf2_section_info *section,
4385 rcuh_kind section_kind)
107d2387
AC
4386{
4387 int signed_addr;
891d2f0b 4388 unsigned int bytes_read;
43988095
JK
4389 const char *filename = get_section_file_name (section);
4390 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4391
4392 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4393 cu_header->initial_length_size = bytes_read;
4394 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4395 info_ptr += bytes_read;
107d2387
AC
4396 cu_header->version = read_2_bytes (abfd, info_ptr);
4397 info_ptr += 2;
43988095
JK
4398 if (cu_header->version < 5)
4399 switch (section_kind)
4400 {
4401 case rcuh_kind::COMPILE:
4402 cu_header->unit_type = DW_UT_compile;
4403 break;
4404 case rcuh_kind::TYPE:
4405 cu_header->unit_type = DW_UT_type;
4406 break;
4407 default:
4408 internal_error (__FILE__, __LINE__,
4409 _("read_comp_unit_head: invalid section_kind"));
4410 }
4411 else
4412 {
4413 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4414 (read_1_byte (abfd, info_ptr));
4415 info_ptr += 1;
4416 switch (cu_header->unit_type)
4417 {
4418 case DW_UT_compile:
4419 if (section_kind != rcuh_kind::COMPILE)
4420 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4421 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4422 filename);
4423 break;
4424 case DW_UT_type:
4425 section_kind = rcuh_kind::TYPE;
4426 break;
4427 default:
4428 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4429 "(is %d, should be %d or %d) [in module %s]"),
4430 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4431 }
4432
4433 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4434 info_ptr += 1;
4435 }
b64f50a1
JK
4436 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4437 &bytes_read);
613e1657 4438 info_ptr += bytes_read;
43988095
JK
4439 if (cu_header->version < 5)
4440 {
4441 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4442 info_ptr += 1;
4443 }
107d2387
AC
4444 signed_addr = bfd_get_sign_extend_vma (abfd);
4445 if (signed_addr < 0)
8e65ff28 4446 internal_error (__FILE__, __LINE__,
e2e0b3e5 4447 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4448 cu_header->signed_addr_p = signed_addr;
c764a876 4449
43988095
JK
4450 if (section_kind == rcuh_kind::TYPE)
4451 {
4452 LONGEST type_offset;
4453
4454 cu_header->signature = read_8_bytes (abfd, info_ptr);
4455 info_ptr += 8;
4456
4457 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4458 info_ptr += bytes_read;
4459 cu_header->type_offset_in_tu.cu_off = type_offset;
4460 if (cu_header->type_offset_in_tu.cu_off != type_offset)
4461 error (_("Dwarf Error: Too big type_offset in compilation unit "
4462 "header (is %s) [in module %s]"), plongest (type_offset),
4463 filename);
4464 }
4465
107d2387
AC
4466 return info_ptr;
4467}
4468
36586728
TT
4469/* Helper function that returns the proper abbrev section for
4470 THIS_CU. */
4471
4472static struct dwarf2_section_info *
4473get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4474{
4475 struct dwarf2_section_info *abbrev;
4476
4477 if (this_cu->is_dwz)
4478 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4479 else
4480 abbrev = &dwarf2_per_objfile->abbrev;
4481
4482 return abbrev;
4483}
4484
9ff913ba
DE
4485/* Subroutine of read_and_check_comp_unit_head and
4486 read_and_check_type_unit_head to simplify them.
4487 Perform various error checking on the header. */
4488
4489static void
4490error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4491 struct dwarf2_section_info *section,
4492 struct dwarf2_section_info *abbrev_section)
9ff913ba 4493{
a32a8923 4494 const char *filename = get_section_file_name (section);
9ff913ba 4495
43988095 4496 if (header->version < 2 || header->version > 5)
9ff913ba 4497 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4498 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4499 filename);
4500
b64f50a1 4501 if (header->abbrev_offset.sect_off
36586728 4502 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4503 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4504 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4505 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4506 filename);
4507
4508 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4509 avoid potential 32-bit overflow. */
1ce1cefd 4510 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4511 > section->size)
4512 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4513 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4514 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4515 filename);
4516}
4517
4518/* Read in a CU/TU header and perform some basic error checking.
4519 The contents of the header are stored in HEADER.
4520 The result is a pointer to the start of the first DIE. */
adabb602 4521
d521ce57 4522static const gdb_byte *
9ff913ba
DE
4523read_and_check_comp_unit_head (struct comp_unit_head *header,
4524 struct dwarf2_section_info *section,
4bdcc0c1 4525 struct dwarf2_section_info *abbrev_section,
d521ce57 4526 const gdb_byte *info_ptr,
43988095 4527 rcuh_kind section_kind)
72bf9492 4528{
d521ce57 4529 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4530 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4531
b64f50a1 4532 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4533
43988095 4534 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4535
b64f50a1 4536 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4537
4bdcc0c1 4538 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4539
4540 return info_ptr;
348e048f
DE
4541}
4542
f4dc4d17
DE
4543/* Fetch the abbreviation table offset from a comp or type unit header. */
4544
4545static sect_offset
4546read_abbrev_offset (struct dwarf2_section_info *section,
4547 sect_offset offset)
4548{
a32a8923 4549 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4550 const gdb_byte *info_ptr;
ac298888 4551 unsigned int initial_length_size, offset_size;
f4dc4d17 4552 sect_offset abbrev_offset;
43988095 4553 uint16_t version;
f4dc4d17
DE
4554
4555 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4556 info_ptr = section->buffer + offset.sect_off;
ac298888 4557 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4558 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4559 info_ptr += initial_length_size;
4560
4561 version = read_2_bytes (abfd, info_ptr);
4562 info_ptr += 2;
4563 if (version >= 5)
4564 {
4565 /* Skip unit type and address size. */
4566 info_ptr += 2;
4567 }
4568
f4dc4d17
DE
4569 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4570 return abbrev_offset;
4571}
4572
aaa75496
JB
4573/* Allocate a new partial symtab for file named NAME and mark this new
4574 partial symtab as being an include of PST. */
4575
4576static void
d521ce57 4577dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4578 struct objfile *objfile)
4579{
4580 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4581
fbd9ab74
JK
4582 if (!IS_ABSOLUTE_PATH (subpst->filename))
4583 {
4584 /* It shares objfile->objfile_obstack. */
4585 subpst->dirname = pst->dirname;
4586 }
4587
aaa75496
JB
4588 subpst->textlow = 0;
4589 subpst->texthigh = 0;
4590
8d749320
SM
4591 subpst->dependencies
4592 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4593 subpst->dependencies[0] = pst;
4594 subpst->number_of_dependencies = 1;
4595
4596 subpst->globals_offset = 0;
4597 subpst->n_global_syms = 0;
4598 subpst->statics_offset = 0;
4599 subpst->n_static_syms = 0;
43f3e411 4600 subpst->compunit_symtab = NULL;
aaa75496
JB
4601 subpst->read_symtab = pst->read_symtab;
4602 subpst->readin = 0;
4603
4604 /* No private part is necessary for include psymtabs. This property
4605 can be used to differentiate between such include psymtabs and
10b3939b 4606 the regular ones. */
58a9656e 4607 subpst->read_symtab_private = NULL;
aaa75496
JB
4608}
4609
4610/* Read the Line Number Program data and extract the list of files
4611 included by the source file represented by PST. Build an include
d85a05f0 4612 partial symtab for each of these included files. */
aaa75496
JB
4613
4614static void
4615dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4616 struct die_info *die,
4617 struct partial_symtab *pst)
aaa75496 4618{
d85a05f0
DJ
4619 struct line_header *lh = NULL;
4620 struct attribute *attr;
aaa75496 4621
d85a05f0
DJ
4622 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4623 if (attr)
3019eac3 4624 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4625 if (lh == NULL)
4626 return; /* No linetable, so no includes. */
4627
c6da4cef 4628 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
527f3840 4629 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4630
4631 free_line_header (lh);
4632}
4633
348e048f 4634static hashval_t
52dc124a 4635hash_signatured_type (const void *item)
348e048f 4636{
9a3c8263
SM
4637 const struct signatured_type *sig_type
4638 = (const struct signatured_type *) item;
9a619af0 4639
348e048f 4640 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4641 return sig_type->signature;
348e048f
DE
4642}
4643
4644static int
52dc124a 4645eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4646{
9a3c8263
SM
4647 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4648 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4649
348e048f
DE
4650 return lhs->signature == rhs->signature;
4651}
4652
1fd400ff
TT
4653/* Allocate a hash table for signatured types. */
4654
4655static htab_t
673bfd45 4656allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4657{
4658 return htab_create_alloc_ex (41,
52dc124a
DE
4659 hash_signatured_type,
4660 eq_signatured_type,
1fd400ff
TT
4661 NULL,
4662 &objfile->objfile_obstack,
4663 hashtab_obstack_allocate,
4664 dummy_obstack_deallocate);
4665}
4666
d467dd73 4667/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4668
4669static int
d467dd73 4670add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4671{
9a3c8263
SM
4672 struct signatured_type *sigt = (struct signatured_type *) *slot;
4673 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4674
b4dd5633 4675 **datap = sigt;
1fd400ff
TT
4676 ++*datap;
4677
4678 return 1;
4679}
4680
78d4d2c5 4681/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4682 and fill them into TYPES_HTAB. It will process only type units,
4683 therefore DW_UT_type. */
c88ee1f0 4684
78d4d2c5
JK
4685static void
4686create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4687 dwarf2_section_info *section, htab_t &types_htab,
4688 rcuh_kind section_kind)
348e048f 4689{
3019eac3 4690 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4691 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4692 bfd *abfd;
4693 const gdb_byte *info_ptr, *end_ptr;
348e048f 4694
4bdcc0c1
DE
4695 abbrev_section = (dwo_file != NULL
4696 ? &dwo_file->sections.abbrev
4697 : &dwarf2_per_objfile->abbrev);
4698
b4f54984 4699 if (dwarf_read_debug)
43988095
JK
4700 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4701 get_section_name (section),
a32a8923 4702 get_section_file_name (abbrev_section));
09406207 4703
78d4d2c5
JK
4704 dwarf2_read_section (objfile, section);
4705 info_ptr = section->buffer;
348e048f 4706
78d4d2c5
JK
4707 if (info_ptr == NULL)
4708 return;
348e048f 4709
78d4d2c5
JK
4710 /* We can't set abfd until now because the section may be empty or
4711 not present, in which case the bfd is unknown. */
4712 abfd = get_section_bfd_owner (section);
348e048f 4713
78d4d2c5
JK
4714 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4715 because we don't need to read any dies: the signature is in the
4716 header. */
3019eac3 4717
78d4d2c5
JK
4718 end_ptr = info_ptr + section->size;
4719 while (info_ptr < end_ptr)
4720 {
4721 sect_offset offset;
78d4d2c5
JK
4722 struct signatured_type *sig_type;
4723 struct dwo_unit *dwo_tu;
4724 void **slot;
4725 const gdb_byte *ptr = info_ptr;
4726 struct comp_unit_head header;
4727 unsigned int length;
8b70b953 4728
78d4d2c5 4729 offset.sect_off = ptr - section->buffer;
348e048f 4730
a49dd8dd
JK
4731 /* Initialize it due to a false compiler warning. */
4732 header.signature = -1;
4733 header.type_offset_in_tu.cu_off = -1;
4734
78d4d2c5
JK
4735 /* We need to read the type's signature in order to build the hash
4736 table, but we don't need anything else just yet. */
348e048f 4737
43988095
JK
4738 ptr = read_and_check_comp_unit_head (&header, section,
4739 abbrev_section, ptr, section_kind);
348e048f 4740
78d4d2c5 4741 length = get_cu_length (&header);
6caca83c 4742
78d4d2c5
JK
4743 /* Skip dummy type units. */
4744 if (ptr >= info_ptr + length
43988095
JK
4745 || peek_abbrev_code (abfd, ptr) == 0
4746 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4747 {
4748 info_ptr += length;
4749 continue;
4750 }
dee91e82 4751
78d4d2c5
JK
4752 if (types_htab == NULL)
4753 {
4754 if (dwo_file)
4755 types_htab = allocate_dwo_unit_table (objfile);
4756 else
4757 types_htab = allocate_signatured_type_table (objfile);
4758 }
8b70b953 4759
78d4d2c5
JK
4760 if (dwo_file)
4761 {
4762 sig_type = NULL;
4763 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4764 struct dwo_unit);
4765 dwo_tu->dwo_file = dwo_file;
43988095
JK
4766 dwo_tu->signature = header.signature;
4767 dwo_tu->type_offset_in_tu = header.type_offset_in_tu;
78d4d2c5
JK
4768 dwo_tu->section = section;
4769 dwo_tu->offset = offset;
4770 dwo_tu->length = length;
4771 }
4772 else
4773 {
4774 /* N.B.: type_offset is not usable if this type uses a DWO file.
4775 The real type_offset is in the DWO file. */
4776 dwo_tu = NULL;
4777 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4778 struct signatured_type);
43988095
JK
4779 sig_type->signature = header.signature;
4780 sig_type->type_offset_in_tu = header.type_offset_in_tu;
78d4d2c5
JK
4781 sig_type->per_cu.objfile = objfile;
4782 sig_type->per_cu.is_debug_types = 1;
4783 sig_type->per_cu.section = section;
4784 sig_type->per_cu.offset = offset;
4785 sig_type->per_cu.length = length;
4786 }
4787
4788 slot = htab_find_slot (types_htab,
4789 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4790 INSERT);
4791 gdb_assert (slot != NULL);
4792 if (*slot != NULL)
4793 {
4794 sect_offset dup_offset;
0349ea22 4795
3019eac3
DE
4796 if (dwo_file)
4797 {
78d4d2c5
JK
4798 const struct dwo_unit *dup_tu
4799 = (const struct dwo_unit *) *slot;
4800
4801 dup_offset = dup_tu->offset;
3019eac3
DE
4802 }
4803 else
4804 {
78d4d2c5
JK
4805 const struct signatured_type *dup_tu
4806 = (const struct signatured_type *) *slot;
4807
4808 dup_offset = dup_tu->per_cu.offset;
3019eac3 4809 }
8b70b953 4810
78d4d2c5
JK
4811 complaint (&symfile_complaints,
4812 _("debug type entry at offset 0x%x is duplicate to"
4813 " the entry at offset 0x%x, signature %s"),
4814 offset.sect_off, dup_offset.sect_off,
43988095 4815 hex_string (header.signature));
78d4d2c5
JK
4816 }
4817 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4818
78d4d2c5
JK
4819 if (dwarf_read_debug > 1)
4820 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4821 offset.sect_off,
43988095 4822 hex_string (header.signature));
3019eac3 4823
78d4d2c5
JK
4824 info_ptr += length;
4825 }
4826}
3019eac3 4827
78d4d2c5
JK
4828/* Create the hash table of all entries in the .debug_types
4829 (or .debug_types.dwo) section(s).
4830 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4831 otherwise it is NULL.
b3c8eb43 4832
78d4d2c5 4833 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4834
78d4d2c5 4835 Note: This function processes DWO files only, not DWP files. */
348e048f 4836
78d4d2c5
JK
4837static void
4838create_debug_types_hash_table (struct dwo_file *dwo_file,
4839 VEC (dwarf2_section_info_def) *types,
4840 htab_t &types_htab)
4841{
4842 int ix;
4843 struct dwarf2_section_info *section;
4844
4845 if (VEC_empty (dwarf2_section_info_def, types))
4846 return;
348e048f 4847
78d4d2c5
JK
4848 for (ix = 0;
4849 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4850 ++ix)
43988095
JK
4851 create_debug_type_hash_table (dwo_file, section, types_htab,
4852 rcuh_kind::TYPE);
3019eac3
DE
4853}
4854
4855/* Create the hash table of all entries in the .debug_types section,
4856 and initialize all_type_units.
4857 The result is zero if there is an error (e.g. missing .debug_types section),
4858 otherwise non-zero. */
4859
4860static int
4861create_all_type_units (struct objfile *objfile)
4862{
78d4d2c5 4863 htab_t types_htab = NULL;
b4dd5633 4864 struct signatured_type **iter;
3019eac3 4865
43988095
JK
4866 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4867 rcuh_kind::COMPILE);
78d4d2c5 4868 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
4869 if (types_htab == NULL)
4870 {
4871 dwarf2_per_objfile->signatured_types = NULL;
4872 return 0;
4873 }
4874
348e048f
DE
4875 dwarf2_per_objfile->signatured_types = types_htab;
4876
6aa5f3a6
DE
4877 dwarf2_per_objfile->n_type_units
4878 = dwarf2_per_objfile->n_allocated_type_units
4879 = htab_elements (types_htab);
8d749320
SM
4880 dwarf2_per_objfile->all_type_units =
4881 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
4882 iter = &dwarf2_per_objfile->all_type_units[0];
4883 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4884 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4885 == dwarf2_per_objfile->n_type_units);
1fd400ff 4886
348e048f
DE
4887 return 1;
4888}
4889
6aa5f3a6
DE
4890/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4891 If SLOT is non-NULL, it is the entry to use in the hash table.
4892 Otherwise we find one. */
4893
4894static struct signatured_type *
4895add_type_unit (ULONGEST sig, void **slot)
4896{
4897 struct objfile *objfile = dwarf2_per_objfile->objfile;
4898 int n_type_units = dwarf2_per_objfile->n_type_units;
4899 struct signatured_type *sig_type;
4900
4901 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4902 ++n_type_units;
4903 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4904 {
4905 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4906 dwarf2_per_objfile->n_allocated_type_units = 1;
4907 dwarf2_per_objfile->n_allocated_type_units *= 2;
4908 dwarf2_per_objfile->all_type_units
224c3ddb
SM
4909 = XRESIZEVEC (struct signatured_type *,
4910 dwarf2_per_objfile->all_type_units,
4911 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
4912 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4913 }
4914 dwarf2_per_objfile->n_type_units = n_type_units;
4915
4916 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4917 struct signatured_type);
4918 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4919 sig_type->signature = sig;
4920 sig_type->per_cu.is_debug_types = 1;
4921 if (dwarf2_per_objfile->using_index)
4922 {
4923 sig_type->per_cu.v.quick =
4924 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4925 struct dwarf2_per_cu_quick_data);
4926 }
4927
4928 if (slot == NULL)
4929 {
4930 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4931 sig_type, INSERT);
4932 }
4933 gdb_assert (*slot == NULL);
4934 *slot = sig_type;
4935 /* The rest of sig_type must be filled in by the caller. */
4936 return sig_type;
4937}
4938
a2ce51a0
DE
4939/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4940 Fill in SIG_ENTRY with DWO_ENTRY. */
4941
4942static void
4943fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4944 struct signatured_type *sig_entry,
4945 struct dwo_unit *dwo_entry)
4946{
7ee85ab1 4947 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4948 gdb_assert (! sig_entry->per_cu.queued);
4949 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
4950 if (dwarf2_per_objfile->using_index)
4951 {
4952 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 4953 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
4954 }
4955 else
4956 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0
DE
4957 gdb_assert (sig_entry->signature == dwo_entry->signature);
4958 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4959 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4960 gdb_assert (sig_entry->dwo_unit == NULL);
4961
4962 sig_entry->per_cu.section = dwo_entry->section;
4963 sig_entry->per_cu.offset = dwo_entry->offset;
4964 sig_entry->per_cu.length = dwo_entry->length;
4965 sig_entry->per_cu.reading_dwo_directly = 1;
4966 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4967 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4968 sig_entry->dwo_unit = dwo_entry;
4969}
4970
4971/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4972 If we haven't read the TU yet, create the signatured_type data structure
4973 for a TU to be read in directly from a DWO file, bypassing the stub.
4974 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4975 using .gdb_index, then when reading a CU we want to stay in the DWO file
4976 containing that CU. Otherwise we could end up reading several other DWO
4977 files (due to comdat folding) to process the transitive closure of all the
4978 mentioned TUs, and that can be slow. The current DWO file will have every
4979 type signature that it needs.
a2ce51a0
DE
4980 We only do this for .gdb_index because in the psymtab case we already have
4981 to read all the DWOs to build the type unit groups. */
4982
4983static struct signatured_type *
4984lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4985{
4986 struct objfile *objfile = dwarf2_per_objfile->objfile;
4987 struct dwo_file *dwo_file;
4988 struct dwo_unit find_dwo_entry, *dwo_entry;
4989 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 4990 void **slot;
a2ce51a0
DE
4991
4992 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4993
6aa5f3a6
DE
4994 /* If TU skeletons have been removed then we may not have read in any
4995 TUs yet. */
4996 if (dwarf2_per_objfile->signatured_types == NULL)
4997 {
4998 dwarf2_per_objfile->signatured_types
4999 = allocate_signatured_type_table (objfile);
5000 }
a2ce51a0
DE
5001
5002 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5003 Use the global signatured_types array to do our own comdat-folding
5004 of types. If this is the first time we're reading this TU, and
5005 the TU has an entry in .gdb_index, replace the recorded data from
5006 .gdb_index with this TU. */
a2ce51a0 5007
a2ce51a0 5008 find_sig_entry.signature = sig;
6aa5f3a6
DE
5009 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5010 &find_sig_entry, INSERT);
9a3c8263 5011 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5012
5013 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5014 read. Don't reassign the global entry to point to this DWO if that's
5015 the case. Also note that if the TU is already being read, it may not
5016 have come from a DWO, the program may be a mix of Fission-compiled
5017 code and non-Fission-compiled code. */
5018
5019 /* Have we already tried to read this TU?
5020 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5021 needn't exist in the global table yet). */
5022 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5023 return sig_entry;
5024
6aa5f3a6
DE
5025 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5026 dwo_unit of the TU itself. */
5027 dwo_file = cu->dwo_unit->dwo_file;
5028
a2ce51a0
DE
5029 /* Ok, this is the first time we're reading this TU. */
5030 if (dwo_file->tus == NULL)
5031 return NULL;
5032 find_dwo_entry.signature = sig;
9a3c8263 5033 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5034 if (dwo_entry == NULL)
5035 return NULL;
5036
6aa5f3a6
DE
5037 /* If the global table doesn't have an entry for this TU, add one. */
5038 if (sig_entry == NULL)
5039 sig_entry = add_type_unit (sig, slot);
5040
a2ce51a0 5041 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5042 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5043 return sig_entry;
5044}
5045
a2ce51a0
DE
5046/* Subroutine of lookup_signatured_type.
5047 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5048 then try the DWP file. If the TU stub (skeleton) has been removed then
5049 it won't be in .gdb_index. */
a2ce51a0
DE
5050
5051static struct signatured_type *
5052lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5053{
5054 struct objfile *objfile = dwarf2_per_objfile->objfile;
5055 struct dwp_file *dwp_file = get_dwp_file ();
5056 struct dwo_unit *dwo_entry;
5057 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5058 void **slot;
a2ce51a0
DE
5059
5060 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5061 gdb_assert (dwp_file != NULL);
5062
6aa5f3a6
DE
5063 /* If TU skeletons have been removed then we may not have read in any
5064 TUs yet. */
5065 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5066 {
6aa5f3a6
DE
5067 dwarf2_per_objfile->signatured_types
5068 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5069 }
5070
6aa5f3a6
DE
5071 find_sig_entry.signature = sig;
5072 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5073 &find_sig_entry, INSERT);
9a3c8263 5074 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5075
5076 /* Have we already tried to read this TU?
5077 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5078 needn't exist in the global table yet). */
5079 if (sig_entry != NULL)
5080 return sig_entry;
5081
a2ce51a0
DE
5082 if (dwp_file->tus == NULL)
5083 return NULL;
57d63ce2
DE
5084 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5085 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5086 if (dwo_entry == NULL)
5087 return NULL;
5088
6aa5f3a6 5089 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5090 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5091
a2ce51a0
DE
5092 return sig_entry;
5093}
5094
380bca97 5095/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5096 Returns NULL if signature SIG is not present in the table.
5097 It is up to the caller to complain about this. */
348e048f
DE
5098
5099static struct signatured_type *
a2ce51a0 5100lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5101{
a2ce51a0
DE
5102 if (cu->dwo_unit
5103 && dwarf2_per_objfile->using_index)
5104 {
5105 /* We're in a DWO/DWP file, and we're using .gdb_index.
5106 These cases require special processing. */
5107 if (get_dwp_file () == NULL)
5108 return lookup_dwo_signatured_type (cu, sig);
5109 else
5110 return lookup_dwp_signatured_type (cu, sig);
5111 }
5112 else
5113 {
5114 struct signatured_type find_entry, *entry;
348e048f 5115
a2ce51a0
DE
5116 if (dwarf2_per_objfile->signatured_types == NULL)
5117 return NULL;
5118 find_entry.signature = sig;
9a3c8263
SM
5119 entry = ((struct signatured_type *)
5120 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5121 return entry;
5122 }
348e048f 5123}
42e7ad6c
DE
5124\f
5125/* Low level DIE reading support. */
348e048f 5126
d85a05f0
DJ
5127/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5128
5129static void
5130init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5131 struct dwarf2_cu *cu,
3019eac3
DE
5132 struct dwarf2_section_info *section,
5133 struct dwo_file *dwo_file)
d85a05f0 5134{
fceca515 5135 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5136 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5137 reader->cu = cu;
3019eac3 5138 reader->dwo_file = dwo_file;
dee91e82
DE
5139 reader->die_section = section;
5140 reader->buffer = section->buffer;
f664829e 5141 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5142 reader->comp_dir = NULL;
d85a05f0
DJ
5143}
5144
b0c7bfa9
DE
5145/* Subroutine of init_cutu_and_read_dies to simplify it.
5146 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5147 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5148 already.
5149
5150 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5151 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5152 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5153 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5154 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5155 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5156 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5157 are filled in with the info of the DIE from the DWO file.
5158 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5159 provided an abbrev table to use.
5160 The result is non-zero if a valid (non-dummy) DIE was found. */
5161
5162static int
5163read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5164 struct dwo_unit *dwo_unit,
5165 int abbrev_table_provided,
5166 struct die_info *stub_comp_unit_die,
a2ce51a0 5167 const char *stub_comp_dir,
b0c7bfa9 5168 struct die_reader_specs *result_reader,
d521ce57 5169 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5170 struct die_info **result_comp_unit_die,
5171 int *result_has_children)
5172{
5173 struct objfile *objfile = dwarf2_per_objfile->objfile;
5174 struct dwarf2_cu *cu = this_cu->cu;
5175 struct dwarf2_section_info *section;
5176 bfd *abfd;
d521ce57 5177 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5178 ULONGEST signature; /* Or dwo_id. */
5179 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5180 int i,num_extra_attrs;
5181 struct dwarf2_section_info *dwo_abbrev_section;
5182 struct attribute *attr;
5183 struct die_info *comp_unit_die;
5184
b0aeadb3
DE
5185 /* At most one of these may be provided. */
5186 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5187
b0c7bfa9
DE
5188 /* These attributes aren't processed until later:
5189 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5190 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5191 referenced later. However, these attributes are found in the stub
5192 which we won't have later. In order to not impose this complication
5193 on the rest of the code, we read them here and copy them to the
5194 DWO CU/TU die. */
b0c7bfa9
DE
5195
5196 stmt_list = NULL;
5197 low_pc = NULL;
5198 high_pc = NULL;
5199 ranges = NULL;
5200 comp_dir = NULL;
5201
5202 if (stub_comp_unit_die != NULL)
5203 {
5204 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5205 DWO file. */
5206 if (! this_cu->is_debug_types)
5207 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5208 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5209 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5210 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5211 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5212
5213 /* There should be a DW_AT_addr_base attribute here (if needed).
5214 We need the value before we can process DW_FORM_GNU_addr_index. */
5215 cu->addr_base = 0;
5216 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5217 if (attr)
5218 cu->addr_base = DW_UNSND (attr);
5219
5220 /* There should be a DW_AT_ranges_base attribute here (if needed).
5221 We need the value before we can process DW_AT_ranges. */
5222 cu->ranges_base = 0;
5223 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5224 if (attr)
5225 cu->ranges_base = DW_UNSND (attr);
5226 }
a2ce51a0
DE
5227 else if (stub_comp_dir != NULL)
5228 {
5229 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5230 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5231 comp_dir->name = DW_AT_comp_dir;
5232 comp_dir->form = DW_FORM_string;
5233 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5234 DW_STRING (comp_dir) = stub_comp_dir;
5235 }
b0c7bfa9
DE
5236
5237 /* Set up for reading the DWO CU/TU. */
5238 cu->dwo_unit = dwo_unit;
5239 section = dwo_unit->section;
5240 dwarf2_read_section (objfile, section);
a32a8923 5241 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
5242 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5243 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5244 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5245
5246 if (this_cu->is_debug_types)
5247 {
b0c7bfa9
DE
5248 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5249
43988095 5250 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5251 dwo_abbrev_section,
43988095 5252 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5253 /* This is not an assert because it can be caused by bad debug info. */
43988095 5254 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5255 {
5256 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5257 " TU at offset 0x%x [in module %s]"),
5258 hex_string (sig_type->signature),
43988095 5259 hex_string (cu->header.signature),
a2ce51a0
DE
5260 dwo_unit->offset.sect_off,
5261 bfd_get_filename (abfd));
5262 }
b0c7bfa9
DE
5263 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5264 /* For DWOs coming from DWP files, we don't know the CU length
5265 nor the type's offset in the TU until now. */
5266 dwo_unit->length = get_cu_length (&cu->header);
43988095 5267 dwo_unit->type_offset_in_tu = cu->header.type_offset_in_tu;
b0c7bfa9
DE
5268
5269 /* Establish the type offset that can be used to lookup the type.
5270 For DWO files, we don't know it until now. */
5271 sig_type->type_offset_in_section.sect_off =
5272 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5273 }
5274 else
5275 {
5276 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5277 dwo_abbrev_section,
43988095 5278 info_ptr, rcuh_kind::COMPILE);
b0c7bfa9
DE
5279 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5280 /* For DWOs coming from DWP files, we don't know the CU length
5281 until now. */
5282 dwo_unit->length = get_cu_length (&cu->header);
5283 }
5284
02142a6c
DE
5285 /* Replace the CU's original abbrev table with the DWO's.
5286 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5287 if (abbrev_table_provided)
5288 {
5289 /* Don't free the provided abbrev table, the caller of
5290 init_cutu_and_read_dies owns it. */
5291 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5292 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5293 make_cleanup (dwarf2_free_abbrev_table, cu);
5294 }
5295 else
5296 {
5297 dwarf2_free_abbrev_table (cu);
5298 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5299 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5300 }
5301
5302 /* Read in the die, but leave space to copy over the attributes
5303 from the stub. This has the benefit of simplifying the rest of
5304 the code - all the work to maintain the illusion of a single
5305 DW_TAG_{compile,type}_unit DIE is done here. */
5306 num_extra_attrs = ((stmt_list != NULL)
5307 + (low_pc != NULL)
5308 + (high_pc != NULL)
5309 + (ranges != NULL)
5310 + (comp_dir != NULL));
5311 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5312 result_has_children, num_extra_attrs);
5313
5314 /* Copy over the attributes from the stub to the DIE we just read in. */
5315 comp_unit_die = *result_comp_unit_die;
5316 i = comp_unit_die->num_attrs;
5317 if (stmt_list != NULL)
5318 comp_unit_die->attrs[i++] = *stmt_list;
5319 if (low_pc != NULL)
5320 comp_unit_die->attrs[i++] = *low_pc;
5321 if (high_pc != NULL)
5322 comp_unit_die->attrs[i++] = *high_pc;
5323 if (ranges != NULL)
5324 comp_unit_die->attrs[i++] = *ranges;
5325 if (comp_dir != NULL)
5326 comp_unit_die->attrs[i++] = *comp_dir;
5327 comp_unit_die->num_attrs += num_extra_attrs;
5328
b4f54984 5329 if (dwarf_die_debug)
bf6af496
DE
5330 {
5331 fprintf_unfiltered (gdb_stdlog,
5332 "Read die from %s@0x%x of %s:\n",
a32a8923 5333 get_section_name (section),
bf6af496
DE
5334 (unsigned) (begin_info_ptr - section->buffer),
5335 bfd_get_filename (abfd));
b4f54984 5336 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5337 }
5338
a2ce51a0
DE
5339 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5340 TUs by skipping the stub and going directly to the entry in the DWO file.
5341 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5342 to get it via circuitous means. Blech. */
5343 if (comp_dir != NULL)
5344 result_reader->comp_dir = DW_STRING (comp_dir);
5345
b0c7bfa9
DE
5346 /* Skip dummy compilation units. */
5347 if (info_ptr >= begin_info_ptr + dwo_unit->length
5348 || peek_abbrev_code (abfd, info_ptr) == 0)
5349 return 0;
5350
5351 *result_info_ptr = info_ptr;
5352 return 1;
5353}
5354
5355/* Subroutine of init_cutu_and_read_dies to simplify it.
5356 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5357 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5358
5359static struct dwo_unit *
5360lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5361 struct die_info *comp_unit_die)
5362{
5363 struct dwarf2_cu *cu = this_cu->cu;
5364 struct attribute *attr;
5365 ULONGEST signature;
5366 struct dwo_unit *dwo_unit;
5367 const char *comp_dir, *dwo_name;
5368
a2ce51a0
DE
5369 gdb_assert (cu != NULL);
5370
b0c7bfa9 5371 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5372 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5373 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5374
5375 if (this_cu->is_debug_types)
5376 {
5377 struct signatured_type *sig_type;
5378
5379 /* Since this_cu is the first member of struct signatured_type,
5380 we can go from a pointer to one to a pointer to the other. */
5381 sig_type = (struct signatured_type *) this_cu;
5382 signature = sig_type->signature;
5383 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5384 }
5385 else
5386 {
5387 struct attribute *attr;
5388
5389 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5390 if (! attr)
5391 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5392 " [in module %s]"),
4262abfb 5393 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5394 signature = DW_UNSND (attr);
5395 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5396 signature);
5397 }
5398
b0c7bfa9
DE
5399 return dwo_unit;
5400}
5401
a2ce51a0 5402/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5403 See it for a description of the parameters.
5404 Read a TU directly from a DWO file, bypassing the stub.
5405
5406 Note: This function could be a little bit simpler if we shared cleanups
5407 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5408 to do, so we keep this function self-contained. Or we could move this
5409 into our caller, but it's complex enough already. */
a2ce51a0
DE
5410
5411static void
6aa5f3a6
DE
5412init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5413 int use_existing_cu, int keep,
a2ce51a0
DE
5414 die_reader_func_ftype *die_reader_func,
5415 void *data)
5416{
5417 struct dwarf2_cu *cu;
5418 struct signatured_type *sig_type;
6aa5f3a6 5419 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5420 struct die_reader_specs reader;
5421 const gdb_byte *info_ptr;
5422 struct die_info *comp_unit_die;
5423 int has_children;
5424
5425 /* Verify we can do the following downcast, and that we have the
5426 data we need. */
5427 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5428 sig_type = (struct signatured_type *) this_cu;
5429 gdb_assert (sig_type->dwo_unit != NULL);
5430
5431 cleanups = make_cleanup (null_cleanup, NULL);
5432
6aa5f3a6
DE
5433 if (use_existing_cu && this_cu->cu != NULL)
5434 {
5435 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5436 cu = this_cu->cu;
5437 /* There's no need to do the rereading_dwo_cu handling that
5438 init_cutu_and_read_dies does since we don't read the stub. */
5439 }
5440 else
5441 {
5442 /* If !use_existing_cu, this_cu->cu must be NULL. */
5443 gdb_assert (this_cu->cu == NULL);
8d749320 5444 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5445 init_one_comp_unit (cu, this_cu);
5446 /* If an error occurs while loading, release our storage. */
5447 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5448 }
5449
5450 /* A future optimization, if needed, would be to use an existing
5451 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5452 could share abbrev tables. */
a2ce51a0
DE
5453
5454 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5455 0 /* abbrev_table_provided */,
5456 NULL /* stub_comp_unit_die */,
5457 sig_type->dwo_unit->dwo_file->comp_dir,
5458 &reader, &info_ptr,
5459 &comp_unit_die, &has_children) == 0)
5460 {
5461 /* Dummy die. */
5462 do_cleanups (cleanups);
5463 return;
5464 }
5465
5466 /* All the "real" work is done here. */
5467 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5468
6aa5f3a6 5469 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5470 but the alternative is making the latter more complex.
5471 This function is only for the special case of using DWO files directly:
5472 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5473 if (free_cu_cleanup != NULL)
a2ce51a0 5474 {
6aa5f3a6
DE
5475 if (keep)
5476 {
5477 /* We've successfully allocated this compilation unit. Let our
5478 caller clean it up when finished with it. */
5479 discard_cleanups (free_cu_cleanup);
a2ce51a0 5480
6aa5f3a6
DE
5481 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5482 So we have to manually free the abbrev table. */
5483 dwarf2_free_abbrev_table (cu);
a2ce51a0 5484
6aa5f3a6
DE
5485 /* Link this CU into read_in_chain. */
5486 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5487 dwarf2_per_objfile->read_in_chain = this_cu;
5488 }
5489 else
5490 do_cleanups (free_cu_cleanup);
a2ce51a0 5491 }
a2ce51a0
DE
5492
5493 do_cleanups (cleanups);
5494}
5495
fd820528 5496/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5497 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5498
f4dc4d17
DE
5499 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5500 Otherwise the table specified in the comp unit header is read in and used.
5501 This is an optimization for when we already have the abbrev table.
5502
dee91e82
DE
5503 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5504 Otherwise, a new CU is allocated with xmalloc.
5505
5506 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5507 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5508
5509 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5510 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5511
70221824 5512static void
fd820528 5513init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5514 struct abbrev_table *abbrev_table,
fd820528
DE
5515 int use_existing_cu, int keep,
5516 die_reader_func_ftype *die_reader_func,
5517 void *data)
c906108c 5518{
dee91e82 5519 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5520 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5521 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5522 struct dwarf2_cu *cu;
d521ce57 5523 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5524 struct die_reader_specs reader;
d85a05f0 5525 struct die_info *comp_unit_die;
dee91e82 5526 int has_children;
d85a05f0 5527 struct attribute *attr;
365156ad 5528 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5529 struct signatured_type *sig_type = NULL;
4bdcc0c1 5530 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5531 /* Non-zero if CU currently points to a DWO file and we need to
5532 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5533 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5534 int rereading_dwo_cu = 0;
c906108c 5535
b4f54984 5536 if (dwarf_die_debug)
09406207
DE
5537 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5538 this_cu->is_debug_types ? "type" : "comp",
5539 this_cu->offset.sect_off);
5540
dee91e82
DE
5541 if (use_existing_cu)
5542 gdb_assert (keep);
23745b47 5543
a2ce51a0
DE
5544 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5545 file (instead of going through the stub), short-circuit all of this. */
5546 if (this_cu->reading_dwo_directly)
5547 {
5548 /* Narrow down the scope of possibilities to have to understand. */
5549 gdb_assert (this_cu->is_debug_types);
5550 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5551 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5552 die_reader_func, data);
a2ce51a0
DE
5553 return;
5554 }
5555
dee91e82
DE
5556 cleanups = make_cleanup (null_cleanup, NULL);
5557
5558 /* This is cheap if the section is already read in. */
5559 dwarf2_read_section (objfile, section);
5560
5561 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5562
5563 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5564
5565 if (use_existing_cu && this_cu->cu != NULL)
5566 {
5567 cu = this_cu->cu;
42e7ad6c
DE
5568 /* If this CU is from a DWO file we need to start over, we need to
5569 refetch the attributes from the skeleton CU.
5570 This could be optimized by retrieving those attributes from when we
5571 were here the first time: the previous comp_unit_die was stored in
5572 comp_unit_obstack. But there's no data yet that we need this
5573 optimization. */
5574 if (cu->dwo_unit != NULL)
5575 rereading_dwo_cu = 1;
dee91e82
DE
5576 }
5577 else
5578 {
5579 /* If !use_existing_cu, this_cu->cu must be NULL. */
5580 gdb_assert (this_cu->cu == NULL);
8d749320 5581 cu = XNEW (struct dwarf2_cu);
dee91e82 5582 init_one_comp_unit (cu, this_cu);
dee91e82 5583 /* If an error occurs while loading, release our storage. */
365156ad 5584 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5585 }
dee91e82 5586
b0c7bfa9 5587 /* Get the header. */
42e7ad6c
DE
5588 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5589 {
5590 /* We already have the header, there's no need to read it in again. */
5591 info_ptr += cu->header.first_die_offset.cu_off;
5592 }
5593 else
5594 {
3019eac3 5595 if (this_cu->is_debug_types)
dee91e82 5596 {
43988095 5597 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5598 abbrev_section, info_ptr,
43988095 5599 rcuh_kind::TYPE);
dee91e82 5600
42e7ad6c
DE
5601 /* Since per_cu is the first member of struct signatured_type,
5602 we can go from a pointer to one to a pointer to the other. */
5603 sig_type = (struct signatured_type *) this_cu;
43988095 5604 gdb_assert (sig_type->signature == cu->header.signature);
42e7ad6c 5605 gdb_assert (sig_type->type_offset_in_tu.cu_off
43988095 5606 == cu->header.type_offset_in_tu.cu_off);
dee91e82
DE
5607 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5608
42e7ad6c
DE
5609 /* LENGTH has not been set yet for type units if we're
5610 using .gdb_index. */
1ce1cefd 5611 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5612
5613 /* Establish the type offset that can be used to lookup the type. */
5614 sig_type->type_offset_in_section.sect_off =
5615 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
43988095
JK
5616
5617 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5618 }
5619 else
5620 {
4bdcc0c1
DE
5621 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5622 abbrev_section,
43988095
JK
5623 info_ptr,
5624 rcuh_kind::COMPILE);
dee91e82
DE
5625
5626 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5627 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5628 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5629 }
5630 }
10b3939b 5631
6caca83c 5632 /* Skip dummy compilation units. */
dee91e82 5633 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5634 || peek_abbrev_code (abfd, info_ptr) == 0)
5635 {
dee91e82 5636 do_cleanups (cleanups);
21b2bd31 5637 return;
6caca83c
CC
5638 }
5639
433df2d4
DE
5640 /* If we don't have them yet, read the abbrevs for this compilation unit.
5641 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5642 done. Note that it's important that if the CU had an abbrev table
5643 on entry we don't free it when we're done: Somewhere up the call stack
5644 it may be in use. */
f4dc4d17
DE
5645 if (abbrev_table != NULL)
5646 {
5647 gdb_assert (cu->abbrev_table == NULL);
5648 gdb_assert (cu->header.abbrev_offset.sect_off
5649 == abbrev_table->offset.sect_off);
5650 cu->abbrev_table = abbrev_table;
5651 }
5652 else if (cu->abbrev_table == NULL)
dee91e82 5653 {
4bdcc0c1 5654 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5655 make_cleanup (dwarf2_free_abbrev_table, cu);
5656 }
42e7ad6c
DE
5657 else if (rereading_dwo_cu)
5658 {
5659 dwarf2_free_abbrev_table (cu);
5660 dwarf2_read_abbrevs (cu, abbrev_section);
5661 }
af703f96 5662
dee91e82 5663 /* Read the top level CU/TU die. */
3019eac3 5664 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5665 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5666
b0c7bfa9
DE
5667 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5668 from the DWO file.
5669 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5670 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5671 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5672 if (attr)
5673 {
3019eac3 5674 struct dwo_unit *dwo_unit;
b0c7bfa9 5675 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5676
5677 if (has_children)
6a506a2d
DE
5678 {
5679 complaint (&symfile_complaints,
5680 _("compilation unit with DW_AT_GNU_dwo_name"
5681 " has children (offset 0x%x) [in module %s]"),
5682 this_cu->offset.sect_off, bfd_get_filename (abfd));
5683 }
b0c7bfa9 5684 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5685 if (dwo_unit != NULL)
3019eac3 5686 {
6a506a2d
DE
5687 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5688 abbrev_table != NULL,
a2ce51a0 5689 comp_unit_die, NULL,
6a506a2d
DE
5690 &reader, &info_ptr,
5691 &dwo_comp_unit_die, &has_children) == 0)
5692 {
5693 /* Dummy die. */
5694 do_cleanups (cleanups);
5695 return;
5696 }
5697 comp_unit_die = dwo_comp_unit_die;
5698 }
5699 else
5700 {
5701 /* Yikes, we couldn't find the rest of the DIE, we only have
5702 the stub. A complaint has already been logged. There's
5703 not much more we can do except pass on the stub DIE to
5704 die_reader_func. We don't want to throw an error on bad
5705 debug info. */
3019eac3
DE
5706 }
5707 }
5708
b0c7bfa9 5709 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5710 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5711
b0c7bfa9 5712 /* Done, clean up. */
365156ad 5713 if (free_cu_cleanup != NULL)
348e048f 5714 {
365156ad
TT
5715 if (keep)
5716 {
5717 /* We've successfully allocated this compilation unit. Let our
5718 caller clean it up when finished with it. */
5719 discard_cleanups (free_cu_cleanup);
dee91e82 5720
365156ad
TT
5721 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5722 So we have to manually free the abbrev table. */
5723 dwarf2_free_abbrev_table (cu);
dee91e82 5724
365156ad
TT
5725 /* Link this CU into read_in_chain. */
5726 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5727 dwarf2_per_objfile->read_in_chain = this_cu;
5728 }
5729 else
5730 do_cleanups (free_cu_cleanup);
348e048f 5731 }
365156ad
TT
5732
5733 do_cleanups (cleanups);
dee91e82
DE
5734}
5735
33e80786
DE
5736/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5737 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5738 to have already done the lookup to find the DWO file).
dee91e82
DE
5739
5740 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5741 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5742
5743 We fill in THIS_CU->length.
5744
5745 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5746 linker) then DIE_READER_FUNC will not get called.
5747
5748 THIS_CU->cu is always freed when done.
3019eac3
DE
5749 This is done in order to not leave THIS_CU->cu in a state where we have
5750 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5751
5752static void
5753init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5754 struct dwo_file *dwo_file,
dee91e82
DE
5755 die_reader_func_ftype *die_reader_func,
5756 void *data)
5757{
5758 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5759 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5760 bfd *abfd = get_section_bfd_owner (section);
33e80786 5761 struct dwarf2_section_info *abbrev_section;
dee91e82 5762 struct dwarf2_cu cu;
d521ce57 5763 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5764 struct die_reader_specs reader;
5765 struct cleanup *cleanups;
5766 struct die_info *comp_unit_die;
5767 int has_children;
5768
b4f54984 5769 if (dwarf_die_debug)
09406207
DE
5770 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5771 this_cu->is_debug_types ? "type" : "comp",
5772 this_cu->offset.sect_off);
5773
dee91e82
DE
5774 gdb_assert (this_cu->cu == NULL);
5775
33e80786
DE
5776 abbrev_section = (dwo_file != NULL
5777 ? &dwo_file->sections.abbrev
5778 : get_abbrev_section_for_cu (this_cu));
5779
dee91e82
DE
5780 /* This is cheap if the section is already read in. */
5781 dwarf2_read_section (objfile, section);
5782
5783 init_one_comp_unit (&cu, this_cu);
5784
5785 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5786
5787 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5788 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5789 abbrev_section, info_ptr,
43988095
JK
5790 (this_cu->is_debug_types
5791 ? rcuh_kind::TYPE
5792 : rcuh_kind::COMPILE));
dee91e82 5793
1ce1cefd 5794 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5795
5796 /* Skip dummy compilation units. */
5797 if (info_ptr >= begin_info_ptr + this_cu->length
5798 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5799 {
dee91e82 5800 do_cleanups (cleanups);
21b2bd31 5801 return;
93311388 5802 }
72bf9492 5803
dee91e82
DE
5804 dwarf2_read_abbrevs (&cu, abbrev_section);
5805 make_cleanup (dwarf2_free_abbrev_table, &cu);
5806
3019eac3 5807 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5808 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5809
5810 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5811
5812 do_cleanups (cleanups);
5813}
5814
3019eac3
DE
5815/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5816 does not lookup the specified DWO file.
5817 This cannot be used to read DWO files.
dee91e82
DE
5818
5819 THIS_CU->cu is always freed when done.
3019eac3
DE
5820 This is done in order to not leave THIS_CU->cu in a state where we have
5821 to care whether it refers to the "main" CU or the DWO CU.
5822 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5823
5824static void
5825init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5826 die_reader_func_ftype *die_reader_func,
5827 void *data)
5828{
33e80786 5829 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5830}
0018ea6f
DE
5831\f
5832/* Type Unit Groups.
dee91e82 5833
0018ea6f
DE
5834 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5835 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5836 so that all types coming from the same compilation (.o file) are grouped
5837 together. A future step could be to put the types in the same symtab as
5838 the CU the types ultimately came from. */
ff013f42 5839
f4dc4d17
DE
5840static hashval_t
5841hash_type_unit_group (const void *item)
5842{
9a3c8263
SM
5843 const struct type_unit_group *tu_group
5844 = (const struct type_unit_group *) item;
f4dc4d17 5845
094b34ac 5846 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5847}
348e048f
DE
5848
5849static int
f4dc4d17 5850eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5851{
9a3c8263
SM
5852 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5853 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 5854
094b34ac 5855 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5856}
348e048f 5857
f4dc4d17
DE
5858/* Allocate a hash table for type unit groups. */
5859
5860static htab_t
5861allocate_type_unit_groups_table (void)
5862{
5863 return htab_create_alloc_ex (3,
5864 hash_type_unit_group,
5865 eq_type_unit_group,
5866 NULL,
5867 &dwarf2_per_objfile->objfile->objfile_obstack,
5868 hashtab_obstack_allocate,
5869 dummy_obstack_deallocate);
5870}
dee91e82 5871
f4dc4d17
DE
5872/* Type units that don't have DW_AT_stmt_list are grouped into their own
5873 partial symtabs. We combine several TUs per psymtab to not let the size
5874 of any one psymtab grow too big. */
5875#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5876#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5877
094b34ac 5878/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5879 Create the type_unit_group object used to hold one or more TUs. */
5880
5881static struct type_unit_group *
094b34ac 5882create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5883{
5884 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5885 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5886 struct type_unit_group *tu_group;
f4dc4d17
DE
5887
5888 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5889 struct type_unit_group);
094b34ac 5890 per_cu = &tu_group->per_cu;
f4dc4d17 5891 per_cu->objfile = objfile;
f4dc4d17 5892
094b34ac
DE
5893 if (dwarf2_per_objfile->using_index)
5894 {
5895 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5896 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5897 }
5898 else
5899 {
5900 unsigned int line_offset = line_offset_struct.sect_off;
5901 struct partial_symtab *pst;
5902 char *name;
5903
5904 /* Give the symtab a useful name for debug purposes. */
5905 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5906 name = xstrprintf ("<type_units_%d>",
5907 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5908 else
5909 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5910
5911 pst = create_partial_symtab (per_cu, name);
5912 pst->anonymous = 1;
f4dc4d17 5913
094b34ac
DE
5914 xfree (name);
5915 }
f4dc4d17 5916
094b34ac
DE
5917 tu_group->hash.dwo_unit = cu->dwo_unit;
5918 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5919
5920 return tu_group;
5921}
5922
094b34ac
DE
5923/* Look up the type_unit_group for type unit CU, and create it if necessary.
5924 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5925
5926static struct type_unit_group *
ff39bb5e 5927get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5928{
5929 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5930 struct type_unit_group *tu_group;
5931 void **slot;
5932 unsigned int line_offset;
5933 struct type_unit_group type_unit_group_for_lookup;
5934
5935 if (dwarf2_per_objfile->type_unit_groups == NULL)
5936 {
5937 dwarf2_per_objfile->type_unit_groups =
5938 allocate_type_unit_groups_table ();
5939 }
5940
5941 /* Do we need to create a new group, or can we use an existing one? */
5942
5943 if (stmt_list)
5944 {
5945 line_offset = DW_UNSND (stmt_list);
5946 ++tu_stats->nr_symtab_sharers;
5947 }
5948 else
5949 {
5950 /* Ugh, no stmt_list. Rare, but we have to handle it.
5951 We can do various things here like create one group per TU or
5952 spread them over multiple groups to split up the expansion work.
5953 To avoid worst case scenarios (too many groups or too large groups)
5954 we, umm, group them in bunches. */
5955 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5956 | (tu_stats->nr_stmt_less_type_units
5957 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5958 ++tu_stats->nr_stmt_less_type_units;
5959 }
5960
094b34ac
DE
5961 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5962 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5963 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5964 &type_unit_group_for_lookup, INSERT);
5965 if (*slot != NULL)
5966 {
9a3c8263 5967 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
5968 gdb_assert (tu_group != NULL);
5969 }
5970 else
5971 {
5972 sect_offset line_offset_struct;
5973
5974 line_offset_struct.sect_off = line_offset;
094b34ac 5975 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5976 *slot = tu_group;
5977 ++tu_stats->nr_symtabs;
5978 }
5979
5980 return tu_group;
5981}
0018ea6f
DE
5982\f
5983/* Partial symbol tables. */
5984
5985/* Create a psymtab named NAME and assign it to PER_CU.
5986
5987 The caller must fill in the following details:
5988 dirname, textlow, texthigh. */
5989
5990static struct partial_symtab *
5991create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5992{
5993 struct objfile *objfile = per_cu->objfile;
5994 struct partial_symtab *pst;
5995
18a94d75 5996 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
5997 objfile->global_psymbols.next,
5998 objfile->static_psymbols.next);
5999
6000 pst->psymtabs_addrmap_supported = 1;
6001
6002 /* This is the glue that links PST into GDB's symbol API. */
6003 pst->read_symtab_private = per_cu;
6004 pst->read_symtab = dwarf2_read_symtab;
6005 per_cu->v.psymtab = pst;
6006
6007 return pst;
6008}
6009
b93601f3
TT
6010/* The DATA object passed to process_psymtab_comp_unit_reader has this
6011 type. */
6012
6013struct process_psymtab_comp_unit_data
6014{
6015 /* True if we are reading a DW_TAG_partial_unit. */
6016
6017 int want_partial_unit;
6018
6019 /* The "pretend" language that is used if the CU doesn't declare a
6020 language. */
6021
6022 enum language pretend_language;
6023};
6024
0018ea6f
DE
6025/* die_reader_func for process_psymtab_comp_unit. */
6026
6027static void
6028process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6029 const gdb_byte *info_ptr,
0018ea6f
DE
6030 struct die_info *comp_unit_die,
6031 int has_children,
6032 void *data)
6033{
6034 struct dwarf2_cu *cu = reader->cu;
6035 struct objfile *objfile = cu->objfile;
3e29f34a 6036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6037 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6038 CORE_ADDR baseaddr;
6039 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6040 struct partial_symtab *pst;
3a2b436a 6041 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6042 const char *filename;
9a3c8263
SM
6043 struct process_psymtab_comp_unit_data *info
6044 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6045
b93601f3 6046 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6047 return;
6048
6049 gdb_assert (! per_cu->is_debug_types);
6050
b93601f3 6051 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6052
6053 cu->list_in_scope = &file_symbols;
6054
6055 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6056 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6057 if (filename == NULL)
0018ea6f 6058 filename = "";
0018ea6f
DE
6059
6060 pst = create_partial_symtab (per_cu, filename);
6061
6062 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6063 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6064
6065 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6066
6067 dwarf2_find_base_address (comp_unit_die, cu);
6068
6069 /* Possibly set the default values of LOWPC and HIGHPC from
6070 `DW_AT_ranges'. */
3a2b436a
JK
6071 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6072 &best_highpc, cu, pst);
6073 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6074 /* Store the contiguous range if it is not empty; it can be empty for
6075 CUs with no code. */
6076 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6077 gdbarch_adjust_dwarf2_addr (gdbarch,
6078 best_lowpc + baseaddr),
6079 gdbarch_adjust_dwarf2_addr (gdbarch,
6080 best_highpc + baseaddr) - 1,
6081 pst);
0018ea6f
DE
6082
6083 /* Check if comp unit has_children.
6084 If so, read the rest of the partial symbols from this comp unit.
6085 If not, there's no more debug_info for this comp unit. */
6086 if (has_children)
6087 {
6088 struct partial_die_info *first_die;
6089 CORE_ADDR lowpc, highpc;
6090
6091 lowpc = ((CORE_ADDR) -1);
6092 highpc = ((CORE_ADDR) 0);
6093
6094 first_die = load_partial_dies (reader, info_ptr, 1);
6095
6096 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6097 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6098
6099 /* If we didn't find a lowpc, set it to highpc to avoid
6100 complaints from `maint check'. */
6101 if (lowpc == ((CORE_ADDR) -1))
6102 lowpc = highpc;
6103
6104 /* If the compilation unit didn't have an explicit address range,
6105 then use the information extracted from its child dies. */
e385593e 6106 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6107 {
6108 best_lowpc = lowpc;
6109 best_highpc = highpc;
6110 }
6111 }
3e29f34a
MR
6112 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6113 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6114
8763cede 6115 end_psymtab_common (objfile, pst);
0018ea6f
DE
6116
6117 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6118 {
6119 int i;
6120 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6121 struct dwarf2_per_cu_data *iter;
6122
6123 /* Fill in 'dependencies' here; we fill in 'users' in a
6124 post-pass. */
6125 pst->number_of_dependencies = len;
8d749320
SM
6126 pst->dependencies =
6127 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6128 for (i = 0;
6129 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6130 i, iter);
6131 ++i)
6132 pst->dependencies[i] = iter->v.psymtab;
6133
6134 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6135 }
6136
6137 /* Get the list of files included in the current compilation unit,
6138 and build a psymtab for each of them. */
6139 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6140
b4f54984 6141 if (dwarf_read_debug)
0018ea6f
DE
6142 {
6143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6144
6145 fprintf_unfiltered (gdb_stdlog,
6146 "Psymtab for %s unit @0x%x: %s - %s"
6147 ", %d global, %d static syms\n",
6148 per_cu->is_debug_types ? "type" : "comp",
6149 per_cu->offset.sect_off,
6150 paddress (gdbarch, pst->textlow),
6151 paddress (gdbarch, pst->texthigh),
6152 pst->n_global_syms, pst->n_static_syms);
6153 }
6154}
6155
6156/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6157 Process compilation unit THIS_CU for a psymtab. */
6158
6159static void
6160process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6161 int want_partial_unit,
6162 enum language pretend_language)
0018ea6f 6163{
b93601f3
TT
6164 struct process_psymtab_comp_unit_data info;
6165
0018ea6f
DE
6166 /* If this compilation unit was already read in, free the
6167 cached copy in order to read it in again. This is
6168 necessary because we skipped some symbols when we first
6169 read in the compilation unit (see load_partial_dies).
6170 This problem could be avoided, but the benefit is unclear. */
6171 if (this_cu->cu != NULL)
6172 free_one_cached_comp_unit (this_cu);
6173
6174 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6175 info.want_partial_unit = want_partial_unit;
6176 info.pretend_language = pretend_language;
0018ea6f
DE
6177 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6178 process_psymtab_comp_unit_reader,
b93601f3 6179 &info);
0018ea6f
DE
6180
6181 /* Age out any secondary CUs. */
6182 age_cached_comp_units ();
6183}
f4dc4d17
DE
6184
6185/* Reader function for build_type_psymtabs. */
6186
6187static void
6188build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6189 const gdb_byte *info_ptr,
f4dc4d17
DE
6190 struct die_info *type_unit_die,
6191 int has_children,
6192 void *data)
6193{
6194 struct objfile *objfile = dwarf2_per_objfile->objfile;
6195 struct dwarf2_cu *cu = reader->cu;
6196 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6197 struct signatured_type *sig_type;
f4dc4d17
DE
6198 struct type_unit_group *tu_group;
6199 struct attribute *attr;
6200 struct partial_die_info *first_die;
6201 CORE_ADDR lowpc, highpc;
6202 struct partial_symtab *pst;
6203
6204 gdb_assert (data == NULL);
0186c6a7
DE
6205 gdb_assert (per_cu->is_debug_types);
6206 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6207
6208 if (! has_children)
6209 return;
6210
6211 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6212 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6213
0186c6a7 6214 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6215
6216 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6217 cu->list_in_scope = &file_symbols;
6218 pst = create_partial_symtab (per_cu, "");
6219 pst->anonymous = 1;
6220
6221 first_die = load_partial_dies (reader, info_ptr, 1);
6222
6223 lowpc = (CORE_ADDR) -1;
6224 highpc = (CORE_ADDR) 0;
6225 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6226
8763cede 6227 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6228}
6229
73051182
DE
6230/* Struct used to sort TUs by their abbreviation table offset. */
6231
6232struct tu_abbrev_offset
6233{
6234 struct signatured_type *sig_type;
6235 sect_offset abbrev_offset;
6236};
6237
6238/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6239
6240static int
6241sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6242{
9a3c8263
SM
6243 const struct tu_abbrev_offset * const *a
6244 = (const struct tu_abbrev_offset * const*) ap;
6245 const struct tu_abbrev_offset * const *b
6246 = (const struct tu_abbrev_offset * const*) bp;
73051182
DE
6247 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6248 unsigned int boff = (*b)->abbrev_offset.sect_off;
6249
6250 return (aoff > boff) - (aoff < boff);
6251}
6252
6253/* Efficiently read all the type units.
6254 This does the bulk of the work for build_type_psymtabs.
6255
6256 The efficiency is because we sort TUs by the abbrev table they use and
6257 only read each abbrev table once. In one program there are 200K TUs
6258 sharing 8K abbrev tables.
6259
6260 The main purpose of this function is to support building the
6261 dwarf2_per_objfile->type_unit_groups table.
6262 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6263 can collapse the search space by grouping them by stmt_list.
6264 The savings can be significant, in the same program from above the 200K TUs
6265 share 8K stmt_list tables.
6266
6267 FUNC is expected to call get_type_unit_group, which will create the
6268 struct type_unit_group if necessary and add it to
6269 dwarf2_per_objfile->type_unit_groups. */
6270
6271static void
6272build_type_psymtabs_1 (void)
6273{
73051182
DE
6274 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6275 struct cleanup *cleanups;
6276 struct abbrev_table *abbrev_table;
6277 sect_offset abbrev_offset;
6278 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6279 int i;
6280
6281 /* It's up to the caller to not call us multiple times. */
6282 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6283
6284 if (dwarf2_per_objfile->n_type_units == 0)
6285 return;
6286
6287 /* TUs typically share abbrev tables, and there can be way more TUs than
6288 abbrev tables. Sort by abbrev table to reduce the number of times we
6289 read each abbrev table in.
6290 Alternatives are to punt or to maintain a cache of abbrev tables.
6291 This is simpler and efficient enough for now.
6292
6293 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6294 symtab to use). Typically TUs with the same abbrev offset have the same
6295 stmt_list value too so in practice this should work well.
6296
6297 The basic algorithm here is:
6298
6299 sort TUs by abbrev table
6300 for each TU with same abbrev table:
6301 read abbrev table if first user
6302 read TU top level DIE
6303 [IWBN if DWO skeletons had DW_AT_stmt_list]
6304 call FUNC */
6305
b4f54984 6306 if (dwarf_read_debug)
73051182
DE
6307 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6308
6309 /* Sort in a separate table to maintain the order of all_type_units
6310 for .gdb_index: TU indices directly index all_type_units. */
6311 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6312 dwarf2_per_objfile->n_type_units);
6313 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6314 {
6315 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6316
6317 sorted_by_abbrev[i].sig_type = sig_type;
6318 sorted_by_abbrev[i].abbrev_offset =
6319 read_abbrev_offset (sig_type->per_cu.section,
6320 sig_type->per_cu.offset);
6321 }
6322 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6323 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6324 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6325
6326 abbrev_offset.sect_off = ~(unsigned) 0;
6327 abbrev_table = NULL;
6328 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6329
6330 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6331 {
6332 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6333
6334 /* Switch to the next abbrev table if necessary. */
6335 if (abbrev_table == NULL
6336 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6337 {
6338 if (abbrev_table != NULL)
6339 {
6340 abbrev_table_free (abbrev_table);
6341 /* Reset to NULL in case abbrev_table_read_table throws
6342 an error: abbrev_table_free_cleanup will get called. */
6343 abbrev_table = NULL;
6344 }
6345 abbrev_offset = tu->abbrev_offset;
6346 abbrev_table =
6347 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6348 abbrev_offset);
6349 ++tu_stats->nr_uniq_abbrev_tables;
6350 }
6351
6352 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6353 build_type_psymtabs_reader, NULL);
6354 }
6355
73051182 6356 do_cleanups (cleanups);
6aa5f3a6 6357}
73051182 6358
6aa5f3a6
DE
6359/* Print collected type unit statistics. */
6360
6361static void
6362print_tu_stats (void)
6363{
6364 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6365
6366 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6367 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6368 dwarf2_per_objfile->n_type_units);
6369 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6370 tu_stats->nr_uniq_abbrev_tables);
6371 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6372 tu_stats->nr_symtabs);
6373 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6374 tu_stats->nr_symtab_sharers);
6375 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6376 tu_stats->nr_stmt_less_type_units);
6377 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6378 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6379}
6380
f4dc4d17
DE
6381/* Traversal function for build_type_psymtabs. */
6382
6383static int
6384build_type_psymtab_dependencies (void **slot, void *info)
6385{
6386 struct objfile *objfile = dwarf2_per_objfile->objfile;
6387 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6388 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6389 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6390 int len = VEC_length (sig_type_ptr, tu_group->tus);
6391 struct signatured_type *iter;
f4dc4d17
DE
6392 int i;
6393
6394 gdb_assert (len > 0);
0186c6a7 6395 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6396
6397 pst->number_of_dependencies = len;
8d749320
SM
6398 pst->dependencies =
6399 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6400 for (i = 0;
0186c6a7 6401 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6402 ++i)
6403 {
0186c6a7
DE
6404 gdb_assert (iter->per_cu.is_debug_types);
6405 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6406 iter->type_unit_group = tu_group;
f4dc4d17
DE
6407 }
6408
0186c6a7 6409 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6410
6411 return 1;
6412}
6413
6414/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6415 Build partial symbol tables for the .debug_types comp-units. */
6416
6417static void
6418build_type_psymtabs (struct objfile *objfile)
6419{
0e50663e 6420 if (! create_all_type_units (objfile))
348e048f
DE
6421 return;
6422
73051182 6423 build_type_psymtabs_1 ();
6aa5f3a6 6424}
f4dc4d17 6425
6aa5f3a6
DE
6426/* Traversal function for process_skeletonless_type_unit.
6427 Read a TU in a DWO file and build partial symbols for it. */
6428
6429static int
6430process_skeletonless_type_unit (void **slot, void *info)
6431{
6432 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6433 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6434 struct signatured_type find_entry, *entry;
6435
6436 /* If this TU doesn't exist in the global table, add it and read it in. */
6437
6438 if (dwarf2_per_objfile->signatured_types == NULL)
6439 {
6440 dwarf2_per_objfile->signatured_types
6441 = allocate_signatured_type_table (objfile);
6442 }
6443
6444 find_entry.signature = dwo_unit->signature;
6445 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6446 INSERT);
6447 /* If we've already seen this type there's nothing to do. What's happening
6448 is we're doing our own version of comdat-folding here. */
6449 if (*slot != NULL)
6450 return 1;
6451
6452 /* This does the job that create_all_type_units would have done for
6453 this TU. */
6454 entry = add_type_unit (dwo_unit->signature, slot);
6455 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6456 *slot = entry;
6457
6458 /* This does the job that build_type_psymtabs_1 would have done. */
6459 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6460 build_type_psymtabs_reader, NULL);
6461
6462 return 1;
6463}
6464
6465/* Traversal function for process_skeletonless_type_units. */
6466
6467static int
6468process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6469{
6470 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6471
6472 if (dwo_file->tus != NULL)
6473 {
6474 htab_traverse_noresize (dwo_file->tus,
6475 process_skeletonless_type_unit, info);
6476 }
6477
6478 return 1;
6479}
6480
6481/* Scan all TUs of DWO files, verifying we've processed them.
6482 This is needed in case a TU was emitted without its skeleton.
6483 Note: This can't be done until we know what all the DWO files are. */
6484
6485static void
6486process_skeletonless_type_units (struct objfile *objfile)
6487{
6488 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6489 if (get_dwp_file () == NULL
6490 && dwarf2_per_objfile->dwo_files != NULL)
6491 {
6492 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6493 process_dwo_file_for_skeletonless_type_units,
6494 objfile);
6495 }
348e048f
DE
6496}
6497
60606b2c
TT
6498/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6499
6500static void
6501psymtabs_addrmap_cleanup (void *o)
6502{
9a3c8263 6503 struct objfile *objfile = (struct objfile *) o;
ec61707d 6504
60606b2c
TT
6505 objfile->psymtabs_addrmap = NULL;
6506}
6507
95554aad
TT
6508/* Compute the 'user' field for each psymtab in OBJFILE. */
6509
6510static void
6511set_partial_user (struct objfile *objfile)
6512{
6513 int i;
6514
6515 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6516 {
8832e7e3 6517 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6518 struct partial_symtab *pst = per_cu->v.psymtab;
6519 int j;
6520
36586728
TT
6521 if (pst == NULL)
6522 continue;
6523
95554aad
TT
6524 for (j = 0; j < pst->number_of_dependencies; ++j)
6525 {
6526 /* Set the 'user' field only if it is not already set. */
6527 if (pst->dependencies[j]->user == NULL)
6528 pst->dependencies[j]->user = pst;
6529 }
6530 }
6531}
6532
93311388
DE
6533/* Build the partial symbol table by doing a quick pass through the
6534 .debug_info and .debug_abbrev sections. */
72bf9492 6535
93311388 6536static void
c67a9c90 6537dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6538{
60606b2c
TT
6539 struct cleanup *back_to, *addrmap_cleanup;
6540 struct obstack temp_obstack;
21b2bd31 6541 int i;
93311388 6542
b4f54984 6543 if (dwarf_read_debug)
45cfd468
DE
6544 {
6545 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6546 objfile_name (objfile));
45cfd468
DE
6547 }
6548
98bfdba5
PA
6549 dwarf2_per_objfile->reading_partial_symbols = 1;
6550
be391dca 6551 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6552
93311388
DE
6553 /* Any cached compilation units will be linked by the per-objfile
6554 read_in_chain. Make sure to free them when we're done. */
6555 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6556
348e048f
DE
6557 build_type_psymtabs (objfile);
6558
93311388 6559 create_all_comp_units (objfile);
c906108c 6560
60606b2c
TT
6561 /* Create a temporary address map on a temporary obstack. We later
6562 copy this to the final obstack. */
6563 obstack_init (&temp_obstack);
6564 make_cleanup_obstack_free (&temp_obstack);
6565 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6566 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6567
21b2bd31 6568 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6569 {
8832e7e3 6570 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6571
b93601f3 6572 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6573 }
ff013f42 6574
6aa5f3a6
DE
6575 /* This has to wait until we read the CUs, we need the list of DWOs. */
6576 process_skeletonless_type_units (objfile);
6577
6578 /* Now that all TUs have been processed we can fill in the dependencies. */
6579 if (dwarf2_per_objfile->type_unit_groups != NULL)
6580 {
6581 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6582 build_type_psymtab_dependencies, NULL);
6583 }
6584
b4f54984 6585 if (dwarf_read_debug)
6aa5f3a6
DE
6586 print_tu_stats ();
6587
95554aad
TT
6588 set_partial_user (objfile);
6589
ff013f42
JK
6590 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6591 &objfile->objfile_obstack);
60606b2c 6592 discard_cleanups (addrmap_cleanup);
ff013f42 6593
ae038cb0 6594 do_cleanups (back_to);
45cfd468 6595
b4f54984 6596 if (dwarf_read_debug)
45cfd468 6597 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6598 objfile_name (objfile));
ae038cb0
DJ
6599}
6600
3019eac3 6601/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6602
6603static void
dee91e82 6604load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6605 const gdb_byte *info_ptr,
dee91e82
DE
6606 struct die_info *comp_unit_die,
6607 int has_children,
6608 void *data)
ae038cb0 6609{
dee91e82 6610 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6611
95554aad 6612 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6613
ae038cb0
DJ
6614 /* Check if comp unit has_children.
6615 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6616 If not, there's no more debug_info for this comp unit. */
d85a05f0 6617 if (has_children)
dee91e82
DE
6618 load_partial_dies (reader, info_ptr, 0);
6619}
98bfdba5 6620
dee91e82
DE
6621/* Load the partial DIEs for a secondary CU into memory.
6622 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6623
dee91e82
DE
6624static void
6625load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6626{
f4dc4d17
DE
6627 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6628 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6629}
6630
ae038cb0 6631static void
36586728
TT
6632read_comp_units_from_section (struct objfile *objfile,
6633 struct dwarf2_section_info *section,
6634 unsigned int is_dwz,
6635 int *n_allocated,
6636 int *n_comp_units,
6637 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6638{
d521ce57 6639 const gdb_byte *info_ptr;
a32a8923 6640 bfd *abfd = get_section_bfd_owner (section);
be391dca 6641
b4f54984 6642 if (dwarf_read_debug)
bf6af496 6643 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6644 get_section_name (section),
6645 get_section_file_name (section));
bf6af496 6646
36586728 6647 dwarf2_read_section (objfile, section);
ae038cb0 6648
36586728 6649 info_ptr = section->buffer;
6e70227d 6650
36586728 6651 while (info_ptr < section->buffer + section->size)
ae038cb0 6652 {
c764a876 6653 unsigned int length, initial_length_size;
ae038cb0 6654 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6655 sect_offset offset;
ae038cb0 6656
36586728 6657 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6658
6659 /* Read just enough information to find out where the next
6660 compilation unit is. */
36586728 6661 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6662
6663 /* Save the compilation unit for later lookup. */
8d749320 6664 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0
DJ
6665 memset (this_cu, 0, sizeof (*this_cu));
6666 this_cu->offset = offset;
c764a876 6667 this_cu->length = length + initial_length_size;
36586728 6668 this_cu->is_dwz = is_dwz;
9291a0cd 6669 this_cu->objfile = objfile;
8a0459fd 6670 this_cu->section = section;
ae038cb0 6671
36586728 6672 if (*n_comp_units == *n_allocated)
ae038cb0 6673 {
36586728 6674 *n_allocated *= 2;
224c3ddb
SM
6675 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6676 *all_comp_units, *n_allocated);
ae038cb0 6677 }
36586728
TT
6678 (*all_comp_units)[*n_comp_units] = this_cu;
6679 ++*n_comp_units;
ae038cb0
DJ
6680
6681 info_ptr = info_ptr + this_cu->length;
6682 }
36586728
TT
6683}
6684
6685/* Create a list of all compilation units in OBJFILE.
6686 This is only done for -readnow and building partial symtabs. */
6687
6688static void
6689create_all_comp_units (struct objfile *objfile)
6690{
6691 int n_allocated;
6692 int n_comp_units;
6693 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6694 struct dwz_file *dwz;
36586728
TT
6695
6696 n_comp_units = 0;
6697 n_allocated = 10;
8d749320 6698 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6699
6700 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6701 &n_allocated, &n_comp_units, &all_comp_units);
6702
4db1a1dc
TT
6703 dwz = dwarf2_get_dwz_file ();
6704 if (dwz != NULL)
6705 read_comp_units_from_section (objfile, &dwz->info, 1,
6706 &n_allocated, &n_comp_units,
6707 &all_comp_units);
ae038cb0 6708
8d749320
SM
6709 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6710 struct dwarf2_per_cu_data *,
6711 n_comp_units);
ae038cb0
DJ
6712 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6713 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6714 xfree (all_comp_units);
6715 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6716}
6717
5734ee8b 6718/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6719 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6720 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6721 DW_AT_ranges). See the comments of add_partial_subprogram on how
6722 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6723
72bf9492
DJ
6724static void
6725scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6726 CORE_ADDR *highpc, int set_addrmap,
6727 struct dwarf2_cu *cu)
c906108c 6728{
72bf9492 6729 struct partial_die_info *pdi;
c906108c 6730
91c24f0a
DC
6731 /* Now, march along the PDI's, descending into ones which have
6732 interesting children but skipping the children of the other ones,
6733 until we reach the end of the compilation unit. */
c906108c 6734
72bf9492 6735 pdi = first_die;
91c24f0a 6736
72bf9492
DJ
6737 while (pdi != NULL)
6738 {
6739 fixup_partial_die (pdi, cu);
c906108c 6740
f55ee35c 6741 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6742 children, so we need to look at them. Ditto for anonymous
6743 enums. */
933c6fe4 6744
72bf9492 6745 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6746 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6747 || pdi->tag == DW_TAG_imported_unit)
c906108c 6748 {
72bf9492 6749 switch (pdi->tag)
c906108c
SS
6750 {
6751 case DW_TAG_subprogram:
cdc07690 6752 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6753 break;
72929c62 6754 case DW_TAG_constant:
c906108c
SS
6755 case DW_TAG_variable:
6756 case DW_TAG_typedef:
91c24f0a 6757 case DW_TAG_union_type:
72bf9492 6758 if (!pdi->is_declaration)
63d06c5c 6759 {
72bf9492 6760 add_partial_symbol (pdi, cu);
63d06c5c
DC
6761 }
6762 break;
c906108c 6763 case DW_TAG_class_type:
680b30c7 6764 case DW_TAG_interface_type:
c906108c 6765 case DW_TAG_structure_type:
72bf9492 6766 if (!pdi->is_declaration)
c906108c 6767 {
72bf9492 6768 add_partial_symbol (pdi, cu);
c906108c 6769 }
e98c9e7c
TT
6770 if (cu->language == language_rust && pdi->has_children)
6771 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6772 set_addrmap, cu);
c906108c 6773 break;
91c24f0a 6774 case DW_TAG_enumeration_type:
72bf9492
DJ
6775 if (!pdi->is_declaration)
6776 add_partial_enumeration (pdi, cu);
c906108c
SS
6777 break;
6778 case DW_TAG_base_type:
a02abb62 6779 case DW_TAG_subrange_type:
c906108c 6780 /* File scope base type definitions are added to the partial
c5aa993b 6781 symbol table. */
72bf9492 6782 add_partial_symbol (pdi, cu);
c906108c 6783 break;
d9fa45fe 6784 case DW_TAG_namespace:
cdc07690 6785 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6786 break;
5d7cb8df 6787 case DW_TAG_module:
cdc07690 6788 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6789 break;
95554aad
TT
6790 case DW_TAG_imported_unit:
6791 {
6792 struct dwarf2_per_cu_data *per_cu;
6793
f4dc4d17
DE
6794 /* For now we don't handle imported units in type units. */
6795 if (cu->per_cu->is_debug_types)
6796 {
6797 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6798 " supported in type units [in module %s]"),
4262abfb 6799 objfile_name (cu->objfile));
f4dc4d17
DE
6800 }
6801
95554aad 6802 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6803 pdi->is_dwz,
95554aad
TT
6804 cu->objfile);
6805
6806 /* Go read the partial unit, if needed. */
6807 if (per_cu->v.psymtab == NULL)
b93601f3 6808 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6809
f4dc4d17 6810 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6811 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6812 }
6813 break;
74921315
KS
6814 case DW_TAG_imported_declaration:
6815 add_partial_symbol (pdi, cu);
6816 break;
c906108c
SS
6817 default:
6818 break;
6819 }
6820 }
6821
72bf9492
DJ
6822 /* If the die has a sibling, skip to the sibling. */
6823
6824 pdi = pdi->die_sibling;
6825 }
6826}
6827
6828/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6829
72bf9492 6830 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6831 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
6832 Enumerators are an exception; they use the scope of their parent
6833 enumeration type, i.e. the name of the enumeration type is not
6834 prepended to the enumerator.
91c24f0a 6835
72bf9492
DJ
6836 There are two complexities. One is DW_AT_specification; in this
6837 case "parent" means the parent of the target of the specification,
6838 instead of the direct parent of the DIE. The other is compilers
6839 which do not emit DW_TAG_namespace; in this case we try to guess
6840 the fully qualified name of structure types from their members'
6841 linkage names. This must be done using the DIE's children rather
6842 than the children of any DW_AT_specification target. We only need
6843 to do this for structures at the top level, i.e. if the target of
6844 any DW_AT_specification (if any; otherwise the DIE itself) does not
6845 have a parent. */
6846
6847/* Compute the scope prefix associated with PDI's parent, in
6848 compilation unit CU. The result will be allocated on CU's
6849 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6850 field. NULL is returned if no prefix is necessary. */
15d034d0 6851static const char *
72bf9492
DJ
6852partial_die_parent_scope (struct partial_die_info *pdi,
6853 struct dwarf2_cu *cu)
6854{
15d034d0 6855 const char *grandparent_scope;
72bf9492 6856 struct partial_die_info *parent, *real_pdi;
91c24f0a 6857
72bf9492
DJ
6858 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6859 then this means the parent of the specification DIE. */
6860
6861 real_pdi = pdi;
72bf9492 6862 while (real_pdi->has_specification)
36586728
TT
6863 real_pdi = find_partial_die (real_pdi->spec_offset,
6864 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6865
6866 parent = real_pdi->die_parent;
6867 if (parent == NULL)
6868 return NULL;
6869
6870 if (parent->scope_set)
6871 return parent->scope;
6872
6873 fixup_partial_die (parent, cu);
6874
10b3939b 6875 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6876
acebe513
UW
6877 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6878 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6879 Work around this problem here. */
6880 if (cu->language == language_cplus
6e70227d 6881 && parent->tag == DW_TAG_namespace
acebe513
UW
6882 && strcmp (parent->name, "::") == 0
6883 && grandparent_scope == NULL)
6884 {
6885 parent->scope = NULL;
6886 parent->scope_set = 1;
6887 return NULL;
6888 }
6889
9c6c53f7
SA
6890 if (pdi->tag == DW_TAG_enumerator)
6891 /* Enumerators should not get the name of the enumeration as a prefix. */
6892 parent->scope = grandparent_scope;
6893 else if (parent->tag == DW_TAG_namespace
f55ee35c 6894 || parent->tag == DW_TAG_module
72bf9492
DJ
6895 || parent->tag == DW_TAG_structure_type
6896 || parent->tag == DW_TAG_class_type
680b30c7 6897 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6898 || parent->tag == DW_TAG_union_type
6899 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6900 {
6901 if (grandparent_scope == NULL)
6902 parent->scope = parent->name;
6903 else
3e43a32a
MS
6904 parent->scope = typename_concat (&cu->comp_unit_obstack,
6905 grandparent_scope,
f55ee35c 6906 parent->name, 0, cu);
72bf9492 6907 }
72bf9492
DJ
6908 else
6909 {
6910 /* FIXME drow/2004-04-01: What should we be doing with
6911 function-local names? For partial symbols, we should probably be
6912 ignoring them. */
6913 complaint (&symfile_complaints,
e2e0b3e5 6914 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6915 parent->tag, pdi->offset.sect_off);
72bf9492 6916 parent->scope = grandparent_scope;
c906108c
SS
6917 }
6918
72bf9492
DJ
6919 parent->scope_set = 1;
6920 return parent->scope;
6921}
6922
6923/* Return the fully scoped name associated with PDI, from compilation unit
6924 CU. The result will be allocated with malloc. */
4568ecf9 6925
72bf9492
DJ
6926static char *
6927partial_die_full_name (struct partial_die_info *pdi,
6928 struct dwarf2_cu *cu)
6929{
15d034d0 6930 const char *parent_scope;
72bf9492 6931
98bfdba5
PA
6932 /* If this is a template instantiation, we can not work out the
6933 template arguments from partial DIEs. So, unfortunately, we have
6934 to go through the full DIEs. At least any work we do building
6935 types here will be reused if full symbols are loaded later. */
6936 if (pdi->has_template_arguments)
6937 {
6938 fixup_partial_die (pdi, cu);
6939
6940 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6941 {
6942 struct die_info *die;
6943 struct attribute attr;
6944 struct dwarf2_cu *ref_cu = cu;
6945
b64f50a1 6946 /* DW_FORM_ref_addr is using section offset. */
b4069958 6947 attr.name = (enum dwarf_attribute) 0;
98bfdba5 6948 attr.form = DW_FORM_ref_addr;
4568ecf9 6949 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6950 die = follow_die_ref (NULL, &attr, &ref_cu);
6951
6952 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6953 }
6954 }
6955
72bf9492
DJ
6956 parent_scope = partial_die_parent_scope (pdi, cu);
6957 if (parent_scope == NULL)
6958 return NULL;
6959 else
f55ee35c 6960 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6961}
6962
6963static void
72bf9492 6964add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6965{
e7c27a73 6966 struct objfile *objfile = cu->objfile;
3e29f34a 6967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 6968 CORE_ADDR addr = 0;
15d034d0 6969 const char *actual_name = NULL;
e142c38c 6970 CORE_ADDR baseaddr;
15d034d0 6971 char *built_actual_name;
e142c38c
DJ
6972
6973 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6974
15d034d0
TT
6975 built_actual_name = partial_die_full_name (pdi, cu);
6976 if (built_actual_name != NULL)
6977 actual_name = built_actual_name;
63d06c5c 6978
72bf9492
DJ
6979 if (actual_name == NULL)
6980 actual_name = pdi->name;
6981
c906108c
SS
6982 switch (pdi->tag)
6983 {
6984 case DW_TAG_subprogram:
3e29f34a 6985 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 6986 if (pdi->is_external || cu->language == language_ada)
c906108c 6987 {
2cfa0c8d
JB
6988 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6989 of the global scope. But in Ada, we want to be able to access
6990 nested procedures globally. So all Ada subprograms are stored
6991 in the global scope. */
f47fb265 6992 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6993 built_actual_name != NULL,
f47fb265
MS
6994 VAR_DOMAIN, LOC_BLOCK,
6995 &objfile->global_psymbols,
1762568f 6996 addr, cu->language, objfile);
c906108c
SS
6997 }
6998 else
6999 {
f47fb265 7000 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7001 built_actual_name != NULL,
f47fb265
MS
7002 VAR_DOMAIN, LOC_BLOCK,
7003 &objfile->static_psymbols,
1762568f 7004 addr, cu->language, objfile);
c906108c 7005 }
0c1b455e
TT
7006
7007 if (pdi->main_subprogram && actual_name != NULL)
7008 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7009 break;
72929c62
JB
7010 case DW_TAG_constant:
7011 {
7012 struct psymbol_allocation_list *list;
7013
7014 if (pdi->is_external)
7015 list = &objfile->global_psymbols;
7016 else
7017 list = &objfile->static_psymbols;
f47fb265 7018 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7019 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7020 list, 0, cu->language, objfile);
72929c62
JB
7021 }
7022 break;
c906108c 7023 case DW_TAG_variable:
95554aad
TT
7024 if (pdi->d.locdesc)
7025 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7026
95554aad 7027 if (pdi->d.locdesc
caac4577
JG
7028 && addr == 0
7029 && !dwarf2_per_objfile->has_section_at_zero)
7030 {
7031 /* A global or static variable may also have been stripped
7032 out by the linker if unused, in which case its address
7033 will be nullified; do not add such variables into partial
7034 symbol table then. */
7035 }
7036 else if (pdi->is_external)
c906108c
SS
7037 {
7038 /* Global Variable.
7039 Don't enter into the minimal symbol tables as there is
7040 a minimal symbol table entry from the ELF symbols already.
7041 Enter into partial symbol table if it has a location
7042 descriptor or a type.
7043 If the location descriptor is missing, new_symbol will create
7044 a LOC_UNRESOLVED symbol, the address of the variable will then
7045 be determined from the minimal symbol table whenever the variable
7046 is referenced.
7047 The address for the partial symbol table entry is not
7048 used by GDB, but it comes in handy for debugging partial symbol
7049 table building. */
7050
95554aad 7051 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7052 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7053 built_actual_name != NULL,
f47fb265
MS
7054 VAR_DOMAIN, LOC_STATIC,
7055 &objfile->global_psymbols,
1762568f 7056 addr + baseaddr,
f47fb265 7057 cu->language, objfile);
c906108c
SS
7058 }
7059 else
7060 {
ff908ebf
AW
7061 int has_loc = pdi->d.locdesc != NULL;
7062
7063 /* Static Variable. Skip symbols whose value we cannot know (those
7064 without location descriptors or constant values). */
7065 if (!has_loc && !pdi->has_const_value)
decbce07 7066 {
15d034d0 7067 xfree (built_actual_name);
decbce07
MS
7068 return;
7069 }
ff908ebf 7070
f47fb265 7071 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7072 built_actual_name != NULL,
f47fb265
MS
7073 VAR_DOMAIN, LOC_STATIC,
7074 &objfile->static_psymbols,
ff908ebf 7075 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7076 cu->language, objfile);
c906108c
SS
7077 }
7078 break;
7079 case DW_TAG_typedef:
7080 case DW_TAG_base_type:
a02abb62 7081 case DW_TAG_subrange_type:
38d518c9 7082 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7083 built_actual_name != NULL,
176620f1 7084 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7085 &objfile->static_psymbols,
1762568f 7086 0, cu->language, objfile);
c906108c 7087 break;
74921315 7088 case DW_TAG_imported_declaration:
72bf9492
DJ
7089 case DW_TAG_namespace:
7090 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7091 built_actual_name != NULL,
72bf9492
DJ
7092 VAR_DOMAIN, LOC_TYPEDEF,
7093 &objfile->global_psymbols,
1762568f 7094 0, cu->language, objfile);
72bf9492 7095 break;
530e8392
KB
7096 case DW_TAG_module:
7097 add_psymbol_to_list (actual_name, strlen (actual_name),
7098 built_actual_name != NULL,
7099 MODULE_DOMAIN, LOC_TYPEDEF,
7100 &objfile->global_psymbols,
1762568f 7101 0, cu->language, objfile);
530e8392 7102 break;
c906108c 7103 case DW_TAG_class_type:
680b30c7 7104 case DW_TAG_interface_type:
c906108c
SS
7105 case DW_TAG_structure_type:
7106 case DW_TAG_union_type:
7107 case DW_TAG_enumeration_type:
fa4028e9
JB
7108 /* Skip external references. The DWARF standard says in the section
7109 about "Structure, Union, and Class Type Entries": "An incomplete
7110 structure, union or class type is represented by a structure,
7111 union or class entry that does not have a byte size attribute
7112 and that has a DW_AT_declaration attribute." */
7113 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7114 {
15d034d0 7115 xfree (built_actual_name);
decbce07
MS
7116 return;
7117 }
fa4028e9 7118
63d06c5c
DC
7119 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7120 static vs. global. */
38d518c9 7121 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7122 built_actual_name != NULL,
176620f1 7123 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7124 cu->language == language_cplus
63d06c5c
DC
7125 ? &objfile->global_psymbols
7126 : &objfile->static_psymbols,
1762568f 7127 0, cu->language, objfile);
c906108c 7128
c906108c
SS
7129 break;
7130 case DW_TAG_enumerator:
38d518c9 7131 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7132 built_actual_name != NULL,
176620f1 7133 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7134 cu->language == language_cplus
f6fe98ef
DJ
7135 ? &objfile->global_psymbols
7136 : &objfile->static_psymbols,
1762568f 7137 0, cu->language, objfile);
c906108c
SS
7138 break;
7139 default:
7140 break;
7141 }
5c4e30ca 7142
15d034d0 7143 xfree (built_actual_name);
c906108c
SS
7144}
7145
5c4e30ca
DC
7146/* Read a partial die corresponding to a namespace; also, add a symbol
7147 corresponding to that namespace to the symbol table. NAMESPACE is
7148 the name of the enclosing namespace. */
91c24f0a 7149
72bf9492
DJ
7150static void
7151add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7152 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7153 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7154{
72bf9492 7155 /* Add a symbol for the namespace. */
e7c27a73 7156
72bf9492 7157 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7158
7159 /* Now scan partial symbols in that namespace. */
7160
91c24f0a 7161 if (pdi->has_children)
cdc07690 7162 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7163}
7164
5d7cb8df
JK
7165/* Read a partial die corresponding to a Fortran module. */
7166
7167static void
7168add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7169 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7170{
530e8392
KB
7171 /* Add a symbol for the namespace. */
7172
7173 add_partial_symbol (pdi, cu);
7174
f55ee35c 7175 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7176
7177 if (pdi->has_children)
cdc07690 7178 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7179}
7180
bc30ff58
JB
7181/* Read a partial die corresponding to a subprogram and create a partial
7182 symbol for that subprogram. When the CU language allows it, this
7183 routine also defines a partial symbol for each nested subprogram
cdc07690 7184 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7185 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7186 and highest PC values found in PDI.
6e70227d 7187
cdc07690
YQ
7188 PDI may also be a lexical block, in which case we simply search
7189 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7190 Again, this is only performed when the CU language allows this
7191 type of definitions. */
7192
7193static void
7194add_partial_subprogram (struct partial_die_info *pdi,
7195 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7196 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7197{
7198 if (pdi->tag == DW_TAG_subprogram)
7199 {
7200 if (pdi->has_pc_info)
7201 {
7202 if (pdi->lowpc < *lowpc)
7203 *lowpc = pdi->lowpc;
7204 if (pdi->highpc > *highpc)
7205 *highpc = pdi->highpc;
cdc07690 7206 if (set_addrmap)
5734ee8b 7207 {
5734ee8b 7208 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7209 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7210 CORE_ADDR baseaddr;
7211 CORE_ADDR highpc;
7212 CORE_ADDR lowpc;
5734ee8b
DJ
7213
7214 baseaddr = ANOFFSET (objfile->section_offsets,
7215 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7216 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7217 pdi->lowpc + baseaddr);
7218 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7219 pdi->highpc + baseaddr);
7220 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7221 cu->per_cu->v.psymtab);
5734ee8b 7222 }
481860b3
GB
7223 }
7224
7225 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7226 {
bc30ff58 7227 if (!pdi->is_declaration)
e8d05480
JB
7228 /* Ignore subprogram DIEs that do not have a name, they are
7229 illegal. Do not emit a complaint at this point, we will
7230 do so when we convert this psymtab into a symtab. */
7231 if (pdi->name)
7232 add_partial_symbol (pdi, cu);
bc30ff58
JB
7233 }
7234 }
6e70227d 7235
bc30ff58
JB
7236 if (! pdi->has_children)
7237 return;
7238
7239 if (cu->language == language_ada)
7240 {
7241 pdi = pdi->die_child;
7242 while (pdi != NULL)
7243 {
7244 fixup_partial_die (pdi, cu);
7245 if (pdi->tag == DW_TAG_subprogram
7246 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7247 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7248 pdi = pdi->die_sibling;
7249 }
7250 }
7251}
7252
91c24f0a
DC
7253/* Read a partial die corresponding to an enumeration type. */
7254
72bf9492
DJ
7255static void
7256add_partial_enumeration (struct partial_die_info *enum_pdi,
7257 struct dwarf2_cu *cu)
91c24f0a 7258{
72bf9492 7259 struct partial_die_info *pdi;
91c24f0a
DC
7260
7261 if (enum_pdi->name != NULL)
72bf9492
DJ
7262 add_partial_symbol (enum_pdi, cu);
7263
7264 pdi = enum_pdi->die_child;
7265 while (pdi)
91c24f0a 7266 {
72bf9492 7267 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7268 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7269 else
72bf9492
DJ
7270 add_partial_symbol (pdi, cu);
7271 pdi = pdi->die_sibling;
91c24f0a 7272 }
91c24f0a
DC
7273}
7274
6caca83c
CC
7275/* Return the initial uleb128 in the die at INFO_PTR. */
7276
7277static unsigned int
d521ce57 7278peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7279{
7280 unsigned int bytes_read;
7281
7282 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7283}
7284
4bb7a0a7
DJ
7285/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7286 Return the corresponding abbrev, or NULL if the number is zero (indicating
7287 an empty DIE). In either case *BYTES_READ will be set to the length of
7288 the initial number. */
7289
7290static struct abbrev_info *
d521ce57 7291peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7292 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7293{
7294 bfd *abfd = cu->objfile->obfd;
7295 unsigned int abbrev_number;
7296 struct abbrev_info *abbrev;
7297
7298 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7299
7300 if (abbrev_number == 0)
7301 return NULL;
7302
433df2d4 7303 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7304 if (!abbrev)
7305 {
422b9917
DE
7306 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7307 " at offset 0x%x [in module %s]"),
7308 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7309 cu->header.offset.sect_off, bfd_get_filename (abfd));
4bb7a0a7
DJ
7310 }
7311
7312 return abbrev;
7313}
7314
93311388
DE
7315/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7316 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7317 DIE. Any children of the skipped DIEs will also be skipped. */
7318
d521ce57
TT
7319static const gdb_byte *
7320skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7321{
dee91e82 7322 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7323 struct abbrev_info *abbrev;
7324 unsigned int bytes_read;
7325
7326 while (1)
7327 {
7328 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7329 if (abbrev == NULL)
7330 return info_ptr + bytes_read;
7331 else
dee91e82 7332 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7333 }
7334}
7335
93311388
DE
7336/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7337 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7338 abbrev corresponding to that skipped uleb128 should be passed in
7339 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7340 children. */
7341
d521ce57
TT
7342static const gdb_byte *
7343skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7344 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7345{
7346 unsigned int bytes_read;
7347 struct attribute attr;
dee91e82
DE
7348 bfd *abfd = reader->abfd;
7349 struct dwarf2_cu *cu = reader->cu;
d521ce57 7350 const gdb_byte *buffer = reader->buffer;
f664829e 7351 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7352 unsigned int form, i;
7353
7354 for (i = 0; i < abbrev->num_attrs; i++)
7355 {
7356 /* The only abbrev we care about is DW_AT_sibling. */
7357 if (abbrev->attrs[i].name == DW_AT_sibling)
7358 {
dee91e82 7359 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7360 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7361 complaint (&symfile_complaints,
7362 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7363 else
b9502d3f
WN
7364 {
7365 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7366 const gdb_byte *sibling_ptr = buffer + off;
7367
7368 if (sibling_ptr < info_ptr)
7369 complaint (&symfile_complaints,
7370 _("DW_AT_sibling points backwards"));
22869d73
KS
7371 else if (sibling_ptr > reader->buffer_end)
7372 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7373 else
7374 return sibling_ptr;
7375 }
4bb7a0a7
DJ
7376 }
7377
7378 /* If it isn't DW_AT_sibling, skip this attribute. */
7379 form = abbrev->attrs[i].form;
7380 skip_attribute:
7381 switch (form)
7382 {
4bb7a0a7 7383 case DW_FORM_ref_addr:
ae411497
TT
7384 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7385 and later it is offset sized. */
7386 if (cu->header.version == 2)
7387 info_ptr += cu->header.addr_size;
7388 else
7389 info_ptr += cu->header.offset_size;
7390 break;
36586728
TT
7391 case DW_FORM_GNU_ref_alt:
7392 info_ptr += cu->header.offset_size;
7393 break;
ae411497 7394 case DW_FORM_addr:
4bb7a0a7
DJ
7395 info_ptr += cu->header.addr_size;
7396 break;
7397 case DW_FORM_data1:
7398 case DW_FORM_ref1:
7399 case DW_FORM_flag:
7400 info_ptr += 1;
7401 break;
2dc7f7b3 7402 case DW_FORM_flag_present:
43988095 7403 case DW_FORM_implicit_const:
2dc7f7b3 7404 break;
4bb7a0a7
DJ
7405 case DW_FORM_data2:
7406 case DW_FORM_ref2:
7407 info_ptr += 2;
7408 break;
7409 case DW_FORM_data4:
7410 case DW_FORM_ref4:
7411 info_ptr += 4;
7412 break;
7413 case DW_FORM_data8:
7414 case DW_FORM_ref8:
55f1336d 7415 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7416 info_ptr += 8;
7417 break;
0224619f
JK
7418 case DW_FORM_data16:
7419 info_ptr += 16;
7420 break;
4bb7a0a7 7421 case DW_FORM_string:
9b1c24c8 7422 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7423 info_ptr += bytes_read;
7424 break;
2dc7f7b3 7425 case DW_FORM_sec_offset:
4bb7a0a7 7426 case DW_FORM_strp:
36586728 7427 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7428 info_ptr += cu->header.offset_size;
7429 break;
2dc7f7b3 7430 case DW_FORM_exprloc:
4bb7a0a7
DJ
7431 case DW_FORM_block:
7432 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7433 info_ptr += bytes_read;
7434 break;
7435 case DW_FORM_block1:
7436 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7437 break;
7438 case DW_FORM_block2:
7439 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7440 break;
7441 case DW_FORM_block4:
7442 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7443 break;
7444 case DW_FORM_sdata:
7445 case DW_FORM_udata:
7446 case DW_FORM_ref_udata:
3019eac3
DE
7447 case DW_FORM_GNU_addr_index:
7448 case DW_FORM_GNU_str_index:
d521ce57 7449 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7450 break;
7451 case DW_FORM_indirect:
7452 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7453 info_ptr += bytes_read;
7454 /* We need to continue parsing from here, so just go back to
7455 the top. */
7456 goto skip_attribute;
7457
7458 default:
3e43a32a
MS
7459 error (_("Dwarf Error: Cannot handle %s "
7460 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7461 dwarf_form_name (form),
7462 bfd_get_filename (abfd));
7463 }
7464 }
7465
7466 if (abbrev->has_children)
dee91e82 7467 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7468 else
7469 return info_ptr;
7470}
7471
93311388 7472/* Locate ORIG_PDI's sibling.
dee91e82 7473 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7474
d521ce57 7475static const gdb_byte *
dee91e82
DE
7476locate_pdi_sibling (const struct die_reader_specs *reader,
7477 struct partial_die_info *orig_pdi,
d521ce57 7478 const gdb_byte *info_ptr)
91c24f0a
DC
7479{
7480 /* Do we know the sibling already? */
72bf9492 7481
91c24f0a
DC
7482 if (orig_pdi->sibling)
7483 return orig_pdi->sibling;
7484
7485 /* Are there any children to deal with? */
7486
7487 if (!orig_pdi->has_children)
7488 return info_ptr;
7489
4bb7a0a7 7490 /* Skip the children the long way. */
91c24f0a 7491
dee91e82 7492 return skip_children (reader, info_ptr);
91c24f0a
DC
7493}
7494
257e7a09 7495/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7496 not NULL. */
c906108c
SS
7497
7498static void
257e7a09
YQ
7499dwarf2_read_symtab (struct partial_symtab *self,
7500 struct objfile *objfile)
c906108c 7501{
257e7a09 7502 if (self->readin)
c906108c 7503 {
442e4d9c 7504 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7505 self->filename);
442e4d9c
YQ
7506 }
7507 else
7508 {
7509 if (info_verbose)
c906108c 7510 {
442e4d9c 7511 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7512 self->filename);
442e4d9c 7513 gdb_flush (gdb_stdout);
c906108c 7514 }
c906108c 7515
442e4d9c 7516 /* Restore our global data. */
9a3c8263
SM
7517 dwarf2_per_objfile
7518 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7519 dwarf2_objfile_data_key);
10b3939b 7520
442e4d9c
YQ
7521 /* If this psymtab is constructed from a debug-only objfile, the
7522 has_section_at_zero flag will not necessarily be correct. We
7523 can get the correct value for this flag by looking at the data
7524 associated with the (presumably stripped) associated objfile. */
7525 if (objfile->separate_debug_objfile_backlink)
7526 {
7527 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7528 = ((struct dwarf2_per_objfile *)
7529 objfile_data (objfile->separate_debug_objfile_backlink,
7530 dwarf2_objfile_data_key));
9a619af0 7531
442e4d9c
YQ
7532 dwarf2_per_objfile->has_section_at_zero
7533 = dpo_backlink->has_section_at_zero;
7534 }
b2ab525c 7535
442e4d9c 7536 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7537
257e7a09 7538 psymtab_to_symtab_1 (self);
c906108c 7539
442e4d9c
YQ
7540 /* Finish up the debug error message. */
7541 if (info_verbose)
7542 printf_filtered (_("done.\n"));
c906108c 7543 }
95554aad
TT
7544
7545 process_cu_includes ();
c906108c 7546}
9cdd5dbd
DE
7547\f
7548/* Reading in full CUs. */
c906108c 7549
10b3939b
DJ
7550/* Add PER_CU to the queue. */
7551
7552static void
95554aad
TT
7553queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7554 enum language pretend_language)
10b3939b
DJ
7555{
7556 struct dwarf2_queue_item *item;
7557
7558 per_cu->queued = 1;
8d749320 7559 item = XNEW (struct dwarf2_queue_item);
10b3939b 7560 item->per_cu = per_cu;
95554aad 7561 item->pretend_language = pretend_language;
10b3939b
DJ
7562 item->next = NULL;
7563
7564 if (dwarf2_queue == NULL)
7565 dwarf2_queue = item;
7566 else
7567 dwarf2_queue_tail->next = item;
7568
7569 dwarf2_queue_tail = item;
7570}
7571
89e63ee4
DE
7572/* If PER_CU is not yet queued, add it to the queue.
7573 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7574 dependency.
0907af0c 7575 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7576 meaning either PER_CU is already queued or it is already loaded.
7577
7578 N.B. There is an invariant here that if a CU is queued then it is loaded.
7579 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7580
7581static int
89e63ee4 7582maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7583 struct dwarf2_per_cu_data *per_cu,
7584 enum language pretend_language)
7585{
7586 /* We may arrive here during partial symbol reading, if we need full
7587 DIEs to process an unusual case (e.g. template arguments). Do
7588 not queue PER_CU, just tell our caller to load its DIEs. */
7589 if (dwarf2_per_objfile->reading_partial_symbols)
7590 {
7591 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7592 return 1;
7593 return 0;
7594 }
7595
7596 /* Mark the dependence relation so that we don't flush PER_CU
7597 too early. */
89e63ee4
DE
7598 if (dependent_cu != NULL)
7599 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7600
7601 /* If it's already on the queue, we have nothing to do. */
7602 if (per_cu->queued)
7603 return 0;
7604
7605 /* If the compilation unit is already loaded, just mark it as
7606 used. */
7607 if (per_cu->cu != NULL)
7608 {
7609 per_cu->cu->last_used = 0;
7610 return 0;
7611 }
7612
7613 /* Add it to the queue. */
7614 queue_comp_unit (per_cu, pretend_language);
7615
7616 return 1;
7617}
7618
10b3939b
DJ
7619/* Process the queue. */
7620
7621static void
a0f42c21 7622process_queue (void)
10b3939b
DJ
7623{
7624 struct dwarf2_queue_item *item, *next_item;
7625
b4f54984 7626 if (dwarf_read_debug)
45cfd468
DE
7627 {
7628 fprintf_unfiltered (gdb_stdlog,
7629 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7630 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7631 }
7632
03dd20cc
DJ
7633 /* The queue starts out with one item, but following a DIE reference
7634 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7635 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7636 {
cc12ce38
DE
7637 if ((dwarf2_per_objfile->using_index
7638 ? !item->per_cu->v.quick->compunit_symtab
7639 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7640 /* Skip dummy CUs. */
7641 && item->per_cu->cu != NULL)
f4dc4d17
DE
7642 {
7643 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7644 unsigned int debug_print_threshold;
247f5c4f 7645 char buf[100];
f4dc4d17 7646
247f5c4f 7647 if (per_cu->is_debug_types)
f4dc4d17 7648 {
247f5c4f
DE
7649 struct signatured_type *sig_type =
7650 (struct signatured_type *) per_cu;
7651
7652 sprintf (buf, "TU %s at offset 0x%x",
73be47f5
DE
7653 hex_string (sig_type->signature),
7654 per_cu->offset.sect_off);
7655 /* There can be 100s of TUs.
7656 Only print them in verbose mode. */
7657 debug_print_threshold = 2;
f4dc4d17 7658 }
247f5c4f 7659 else
73be47f5
DE
7660 {
7661 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7662 debug_print_threshold = 1;
7663 }
247f5c4f 7664
b4f54984 7665 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7666 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7667
7668 if (per_cu->is_debug_types)
7669 process_full_type_unit (per_cu, item->pretend_language);
7670 else
7671 process_full_comp_unit (per_cu, item->pretend_language);
7672
b4f54984 7673 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7674 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7675 }
10b3939b
DJ
7676
7677 item->per_cu->queued = 0;
7678 next_item = item->next;
7679 xfree (item);
7680 }
7681
7682 dwarf2_queue_tail = NULL;
45cfd468 7683
b4f54984 7684 if (dwarf_read_debug)
45cfd468
DE
7685 {
7686 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7687 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7688 }
10b3939b
DJ
7689}
7690
7691/* Free all allocated queue entries. This function only releases anything if
7692 an error was thrown; if the queue was processed then it would have been
7693 freed as we went along. */
7694
7695static void
7696dwarf2_release_queue (void *dummy)
7697{
7698 struct dwarf2_queue_item *item, *last;
7699
7700 item = dwarf2_queue;
7701 while (item)
7702 {
7703 /* Anything still marked queued is likely to be in an
7704 inconsistent state, so discard it. */
7705 if (item->per_cu->queued)
7706 {
7707 if (item->per_cu->cu != NULL)
dee91e82 7708 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7709 item->per_cu->queued = 0;
7710 }
7711
7712 last = item;
7713 item = item->next;
7714 xfree (last);
7715 }
7716
7717 dwarf2_queue = dwarf2_queue_tail = NULL;
7718}
7719
7720/* Read in full symbols for PST, and anything it depends on. */
7721
c906108c 7722static void
fba45db2 7723psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7724{
10b3939b 7725 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7726 int i;
7727
95554aad
TT
7728 if (pst->readin)
7729 return;
7730
aaa75496 7731 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7732 if (!pst->dependencies[i]->readin
7733 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7734 {
7735 /* Inform about additional files that need to be read in. */
7736 if (info_verbose)
7737 {
a3f17187 7738 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7739 fputs_filtered (" ", gdb_stdout);
7740 wrap_here ("");
7741 fputs_filtered ("and ", gdb_stdout);
7742 wrap_here ("");
7743 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7744 wrap_here (""); /* Flush output. */
aaa75496
JB
7745 gdb_flush (gdb_stdout);
7746 }
7747 psymtab_to_symtab_1 (pst->dependencies[i]);
7748 }
7749
9a3c8263 7750 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7751
7752 if (per_cu == NULL)
aaa75496
JB
7753 {
7754 /* It's an include file, no symbols to read for it.
7755 Everything is in the parent symtab. */
7756 pst->readin = 1;
7757 return;
7758 }
c906108c 7759
a0f42c21 7760 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7761}
7762
dee91e82
DE
7763/* Trivial hash function for die_info: the hash value of a DIE
7764 is its offset in .debug_info for this objfile. */
10b3939b 7765
dee91e82
DE
7766static hashval_t
7767die_hash (const void *item)
10b3939b 7768{
9a3c8263 7769 const struct die_info *die = (const struct die_info *) item;
6502dd73 7770
dee91e82
DE
7771 return die->offset.sect_off;
7772}
63d06c5c 7773
dee91e82
DE
7774/* Trivial comparison function for die_info structures: two DIEs
7775 are equal if they have the same offset. */
98bfdba5 7776
dee91e82
DE
7777static int
7778die_eq (const void *item_lhs, const void *item_rhs)
7779{
9a3c8263
SM
7780 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7781 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7782
dee91e82
DE
7783 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7784}
c906108c 7785
dee91e82
DE
7786/* die_reader_func for load_full_comp_unit.
7787 This is identical to read_signatured_type_reader,
7788 but is kept separate for now. */
c906108c 7789
dee91e82
DE
7790static void
7791load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7792 const gdb_byte *info_ptr,
dee91e82
DE
7793 struct die_info *comp_unit_die,
7794 int has_children,
7795 void *data)
7796{
7797 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7798 enum language *language_ptr = (enum language *) data;
6caca83c 7799
dee91e82
DE
7800 gdb_assert (cu->die_hash == NULL);
7801 cu->die_hash =
7802 htab_create_alloc_ex (cu->header.length / 12,
7803 die_hash,
7804 die_eq,
7805 NULL,
7806 &cu->comp_unit_obstack,
7807 hashtab_obstack_allocate,
7808 dummy_obstack_deallocate);
e142c38c 7809
dee91e82
DE
7810 if (has_children)
7811 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7812 &info_ptr, comp_unit_die);
7813 cu->dies = comp_unit_die;
7814 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7815
7816 /* We try not to read any attributes in this function, because not
9cdd5dbd 7817 all CUs needed for references have been loaded yet, and symbol
10b3939b 7818 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7819 or we won't be able to build types correctly.
7820 Similarly, if we do not read the producer, we can not apply
7821 producer-specific interpretation. */
95554aad 7822 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7823}
10b3939b 7824
dee91e82 7825/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7826
dee91e82 7827static void
95554aad
TT
7828load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7829 enum language pretend_language)
dee91e82 7830{
3019eac3 7831 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7832
f4dc4d17
DE
7833 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7834 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7835}
7836
3da10d80
KS
7837/* Add a DIE to the delayed physname list. */
7838
7839static void
7840add_to_method_list (struct type *type, int fnfield_index, int index,
7841 const char *name, struct die_info *die,
7842 struct dwarf2_cu *cu)
7843{
7844 struct delayed_method_info mi;
7845 mi.type = type;
7846 mi.fnfield_index = fnfield_index;
7847 mi.index = index;
7848 mi.name = name;
7849 mi.die = die;
7850 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7851}
7852
7853/* A cleanup for freeing the delayed method list. */
7854
7855static void
7856free_delayed_list (void *ptr)
7857{
7858 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7859 if (cu->method_list != NULL)
7860 {
7861 VEC_free (delayed_method_info, cu->method_list);
7862 cu->method_list = NULL;
7863 }
7864}
7865
7866/* Compute the physnames of any methods on the CU's method list.
7867
7868 The computation of method physnames is delayed in order to avoid the
7869 (bad) condition that one of the method's formal parameters is of an as yet
7870 incomplete type. */
7871
7872static void
7873compute_delayed_physnames (struct dwarf2_cu *cu)
7874{
7875 int i;
7876 struct delayed_method_info *mi;
7877 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7878 {
1d06ead6 7879 const char *physname;
3da10d80
KS
7880 struct fn_fieldlist *fn_flp
7881 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7882 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7883 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7884 = physname ? physname : "";
3da10d80
KS
7885 }
7886}
7887
a766d390
DE
7888/* Go objects should be embedded in a DW_TAG_module DIE,
7889 and it's not clear if/how imported objects will appear.
7890 To keep Go support simple until that's worked out,
7891 go back through what we've read and create something usable.
7892 We could do this while processing each DIE, and feels kinda cleaner,
7893 but that way is more invasive.
7894 This is to, for example, allow the user to type "p var" or "b main"
7895 without having to specify the package name, and allow lookups
7896 of module.object to work in contexts that use the expression
7897 parser. */
7898
7899static void
7900fixup_go_packaging (struct dwarf2_cu *cu)
7901{
7902 char *package_name = NULL;
7903 struct pending *list;
7904 int i;
7905
7906 for (list = global_symbols; list != NULL; list = list->next)
7907 {
7908 for (i = 0; i < list->nsyms; ++i)
7909 {
7910 struct symbol *sym = list->symbol[i];
7911
7912 if (SYMBOL_LANGUAGE (sym) == language_go
7913 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7914 {
7915 char *this_package_name = go_symbol_package_name (sym);
7916
7917 if (this_package_name == NULL)
7918 continue;
7919 if (package_name == NULL)
7920 package_name = this_package_name;
7921 else
7922 {
7923 if (strcmp (package_name, this_package_name) != 0)
7924 complaint (&symfile_complaints,
7925 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
7926 (symbol_symtab (sym) != NULL
7927 ? symtab_to_filename_for_display
7928 (symbol_symtab (sym))
4262abfb 7929 : objfile_name (cu->objfile)),
a766d390
DE
7930 this_package_name, package_name);
7931 xfree (this_package_name);
7932 }
7933 }
7934 }
7935 }
7936
7937 if (package_name != NULL)
7938 {
7939 struct objfile *objfile = cu->objfile;
34a68019 7940 const char *saved_package_name
224c3ddb
SM
7941 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7942 package_name,
7943 strlen (package_name));
19f392bc
UW
7944 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7945 saved_package_name);
a766d390
DE
7946 struct symbol *sym;
7947
7948 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7949
e623cf5d 7950 sym = allocate_symbol (objfile);
f85f34ed 7951 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7952 SYMBOL_SET_NAMES (sym, saved_package_name,
7953 strlen (saved_package_name), 0, objfile);
a766d390
DE
7954 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7955 e.g., "main" finds the "main" module and not C's main(). */
7956 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7957 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7958 SYMBOL_TYPE (sym) = type;
7959
7960 add_symbol_to_list (sym, &global_symbols);
7961
7962 xfree (package_name);
7963 }
7964}
7965
95554aad
TT
7966/* Return the symtab for PER_CU. This works properly regardless of
7967 whether we're using the index or psymtabs. */
7968
43f3e411
DE
7969static struct compunit_symtab *
7970get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
7971{
7972 return (dwarf2_per_objfile->using_index
43f3e411
DE
7973 ? per_cu->v.quick->compunit_symtab
7974 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
7975}
7976
7977/* A helper function for computing the list of all symbol tables
7978 included by PER_CU. */
7979
7980static void
43f3e411 7981recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 7982 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 7983 struct dwarf2_per_cu_data *per_cu,
43f3e411 7984 struct compunit_symtab *immediate_parent)
95554aad
TT
7985{
7986 void **slot;
7987 int ix;
43f3e411 7988 struct compunit_symtab *cust;
95554aad
TT
7989 struct dwarf2_per_cu_data *iter;
7990
7991 slot = htab_find_slot (all_children, per_cu, INSERT);
7992 if (*slot != NULL)
7993 {
7994 /* This inclusion and its children have been processed. */
7995 return;
7996 }
7997
7998 *slot = per_cu;
7999 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8000 cust = get_compunit_symtab (per_cu);
8001 if (cust != NULL)
ec94af83
DE
8002 {
8003 /* If this is a type unit only add its symbol table if we haven't
8004 seen it yet (type unit per_cu's can share symtabs). */
8005 if (per_cu->is_debug_types)
8006 {
43f3e411 8007 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8008 if (*slot == NULL)
8009 {
43f3e411
DE
8010 *slot = cust;
8011 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8012 if (cust->user == NULL)
8013 cust->user = immediate_parent;
ec94af83
DE
8014 }
8015 }
8016 else
f9125b6c 8017 {
43f3e411
DE
8018 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8019 if (cust->user == NULL)
8020 cust->user = immediate_parent;
f9125b6c 8021 }
ec94af83 8022 }
95554aad
TT
8023
8024 for (ix = 0;
796a7ff8 8025 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8026 ++ix)
ec94af83
DE
8027 {
8028 recursively_compute_inclusions (result, all_children,
43f3e411 8029 all_type_symtabs, iter, cust);
ec94af83 8030 }
95554aad
TT
8031}
8032
43f3e411 8033/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8034 PER_CU. */
8035
8036static void
43f3e411 8037compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8038{
f4dc4d17
DE
8039 gdb_assert (! per_cu->is_debug_types);
8040
796a7ff8 8041 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8042 {
8043 int ix, len;
ec94af83 8044 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8045 struct compunit_symtab *compunit_symtab_iter;
8046 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8047 htab_t all_children, all_type_symtabs;
43f3e411 8048 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8049
8050 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8051 if (cust == NULL)
95554aad
TT
8052 return;
8053
8054 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8055 NULL, xcalloc, xfree);
ec94af83
DE
8056 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8057 NULL, xcalloc, xfree);
95554aad
TT
8058
8059 for (ix = 0;
796a7ff8 8060 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8061 ix, per_cu_iter);
95554aad 8062 ++ix)
ec94af83
DE
8063 {
8064 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8065 all_type_symtabs, per_cu_iter,
43f3e411 8066 cust);
ec94af83 8067 }
95554aad 8068
ec94af83 8069 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8070 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8071 cust->includes
8d749320
SM
8072 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8073 struct compunit_symtab *, len + 1);
95554aad 8074 for (ix = 0;
43f3e411
DE
8075 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8076 compunit_symtab_iter);
95554aad 8077 ++ix)
43f3e411
DE
8078 cust->includes[ix] = compunit_symtab_iter;
8079 cust->includes[len] = NULL;
95554aad 8080
43f3e411 8081 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8082 htab_delete (all_children);
ec94af83 8083 htab_delete (all_type_symtabs);
95554aad
TT
8084 }
8085}
8086
8087/* Compute the 'includes' field for the symtabs of all the CUs we just
8088 read. */
8089
8090static void
8091process_cu_includes (void)
8092{
8093 int ix;
8094 struct dwarf2_per_cu_data *iter;
8095
8096 for (ix = 0;
8097 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8098 ix, iter);
8099 ++ix)
f4dc4d17
DE
8100 {
8101 if (! iter->is_debug_types)
43f3e411 8102 compute_compunit_symtab_includes (iter);
f4dc4d17 8103 }
95554aad
TT
8104
8105 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8106}
8107
9cdd5dbd 8108/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8109 already been loaded into memory. */
8110
8111static void
95554aad
TT
8112process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8113 enum language pretend_language)
10b3939b 8114{
10b3939b 8115 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8116 struct objfile *objfile = per_cu->objfile;
3e29f34a 8117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8118 CORE_ADDR lowpc, highpc;
43f3e411 8119 struct compunit_symtab *cust;
3da10d80 8120 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8121 CORE_ADDR baseaddr;
4359dff1 8122 struct block *static_block;
3e29f34a 8123 CORE_ADDR addr;
10b3939b
DJ
8124
8125 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8126
10b3939b
DJ
8127 buildsym_init ();
8128 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8129 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8130
8131 cu->list_in_scope = &file_symbols;
c906108c 8132
95554aad
TT
8133 cu->language = pretend_language;
8134 cu->language_defn = language_def (cu->language);
8135
c906108c 8136 /* Do line number decoding in read_file_scope () */
10b3939b 8137 process_die (cu->dies, cu);
c906108c 8138
a766d390
DE
8139 /* For now fudge the Go package. */
8140 if (cu->language == language_go)
8141 fixup_go_packaging (cu);
8142
3da10d80
KS
8143 /* Now that we have processed all the DIEs in the CU, all the types
8144 should be complete, and it should now be safe to compute all of the
8145 physnames. */
8146 compute_delayed_physnames (cu);
8147 do_cleanups (delayed_list_cleanup);
8148
fae299cd
DC
8149 /* Some compilers don't define a DW_AT_high_pc attribute for the
8150 compilation unit. If the DW_AT_high_pc is missing, synthesize
8151 it, by scanning the DIE's below the compilation unit. */
10b3939b 8152 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8153
3e29f34a
MR
8154 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8155 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8156
8157 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8158 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8159 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8160 addrmap to help ensure it has an accurate map of pc values belonging to
8161 this comp unit. */
8162 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8163
43f3e411
DE
8164 cust = end_symtab_from_static_block (static_block,
8165 SECT_OFF_TEXT (objfile), 0);
c906108c 8166
43f3e411 8167 if (cust != NULL)
c906108c 8168 {
df15bd07 8169 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8170
8be455d7
JK
8171 /* Set symtab language to language from DW_AT_language. If the
8172 compilation is from a C file generated by language preprocessors, do
8173 not set the language if it was already deduced by start_subfile. */
43f3e411 8174 if (!(cu->language == language_c
40e3ad0e 8175 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8176 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8177
8178 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8179 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8180 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8181 there were bugs in prologue debug info, fixed later in GCC-4.5
8182 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8183
8184 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8185 needed, it would be wrong due to missing DW_AT_producer there.
8186
8187 Still one can confuse GDB by using non-standard GCC compilation
8188 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8189 */
ab260dad 8190 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8191 cust->locations_valid = 1;
e0d00bc7
JK
8192
8193 if (gcc_4_minor >= 5)
43f3e411 8194 cust->epilogue_unwind_valid = 1;
96408a79 8195
43f3e411 8196 cust->call_site_htab = cu->call_site_htab;
c906108c 8197 }
9291a0cd
TT
8198
8199 if (dwarf2_per_objfile->using_index)
43f3e411 8200 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8201 else
8202 {
8203 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8204 pst->compunit_symtab = cust;
9291a0cd
TT
8205 pst->readin = 1;
8206 }
c906108c 8207
95554aad
TT
8208 /* Push it for inclusion processing later. */
8209 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8210
c906108c 8211 do_cleanups (back_to);
f4dc4d17 8212}
45cfd468 8213
f4dc4d17
DE
8214/* Generate full symbol information for type unit PER_CU, whose DIEs have
8215 already been loaded into memory. */
8216
8217static void
8218process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8219 enum language pretend_language)
8220{
8221 struct dwarf2_cu *cu = per_cu->cu;
8222 struct objfile *objfile = per_cu->objfile;
43f3e411 8223 struct compunit_symtab *cust;
f4dc4d17 8224 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8225 struct signatured_type *sig_type;
8226
8227 gdb_assert (per_cu->is_debug_types);
8228 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8229
8230 buildsym_init ();
8231 back_to = make_cleanup (really_free_pendings, NULL);
8232 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8233
8234 cu->list_in_scope = &file_symbols;
8235
8236 cu->language = pretend_language;
8237 cu->language_defn = language_def (cu->language);
8238
8239 /* The symbol tables are set up in read_type_unit_scope. */
8240 process_die (cu->dies, cu);
8241
8242 /* For now fudge the Go package. */
8243 if (cu->language == language_go)
8244 fixup_go_packaging (cu);
8245
8246 /* Now that we have processed all the DIEs in the CU, all the types
8247 should be complete, and it should now be safe to compute all of the
8248 physnames. */
8249 compute_delayed_physnames (cu);
8250 do_cleanups (delayed_list_cleanup);
8251
8252 /* TUs share symbol tables.
8253 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8254 of it with end_expandable_symtab. Otherwise, complete the addition of
8255 this TU's symbols to the existing symtab. */
43f3e411 8256 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8257 {
43f3e411
DE
8258 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8259 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8260
43f3e411 8261 if (cust != NULL)
f4dc4d17
DE
8262 {
8263 /* Set symtab language to language from DW_AT_language. If the
8264 compilation is from a C file generated by language preprocessors,
8265 do not set the language if it was already deduced by
8266 start_subfile. */
43f3e411
DE
8267 if (!(cu->language == language_c
8268 && COMPUNIT_FILETABS (cust)->language != language_c))
8269 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8270 }
8271 }
8272 else
8273 {
0ab9ce85 8274 augment_type_symtab ();
43f3e411 8275 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8276 }
8277
8278 if (dwarf2_per_objfile->using_index)
43f3e411 8279 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8280 else
8281 {
8282 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8283 pst->compunit_symtab = cust;
f4dc4d17 8284 pst->readin = 1;
45cfd468 8285 }
f4dc4d17
DE
8286
8287 do_cleanups (back_to);
c906108c
SS
8288}
8289
95554aad
TT
8290/* Process an imported unit DIE. */
8291
8292static void
8293process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8294{
8295 struct attribute *attr;
8296
f4dc4d17
DE
8297 /* For now we don't handle imported units in type units. */
8298 if (cu->per_cu->is_debug_types)
8299 {
8300 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8301 " supported in type units [in module %s]"),
4262abfb 8302 objfile_name (cu->objfile));
f4dc4d17
DE
8303 }
8304
95554aad
TT
8305 attr = dwarf2_attr (die, DW_AT_import, cu);
8306 if (attr != NULL)
8307 {
8308 struct dwarf2_per_cu_data *per_cu;
95554aad 8309 sect_offset offset;
36586728 8310 int is_dwz;
95554aad
TT
8311
8312 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
8313 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8314 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 8315
69d751e3 8316 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8317 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8318 load_full_comp_unit (per_cu, cu->language);
8319
796a7ff8 8320 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8321 per_cu);
8322 }
8323}
8324
adde2bff
DE
8325/* Reset the in_process bit of a die. */
8326
8327static void
8328reset_die_in_process (void *arg)
8329{
9a3c8263 8330 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8331
adde2bff
DE
8332 die->in_process = 0;
8333}
8334
c906108c
SS
8335/* Process a die and its children. */
8336
8337static void
e7c27a73 8338process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8339{
adde2bff
DE
8340 struct cleanup *in_process;
8341
8342 /* We should only be processing those not already in process. */
8343 gdb_assert (!die->in_process);
8344
8345 die->in_process = 1;
8346 in_process = make_cleanup (reset_die_in_process,die);
8347
c906108c
SS
8348 switch (die->tag)
8349 {
8350 case DW_TAG_padding:
8351 break;
8352 case DW_TAG_compile_unit:
95554aad 8353 case DW_TAG_partial_unit:
e7c27a73 8354 read_file_scope (die, cu);
c906108c 8355 break;
348e048f
DE
8356 case DW_TAG_type_unit:
8357 read_type_unit_scope (die, cu);
8358 break;
c906108c 8359 case DW_TAG_subprogram:
c906108c 8360 case DW_TAG_inlined_subroutine:
edb3359d 8361 read_func_scope (die, cu);
c906108c
SS
8362 break;
8363 case DW_TAG_lexical_block:
14898363
L
8364 case DW_TAG_try_block:
8365 case DW_TAG_catch_block:
e7c27a73 8366 read_lexical_block_scope (die, cu);
c906108c 8367 break;
216f72a1 8368 case DW_TAG_call_site:
96408a79
SA
8369 case DW_TAG_GNU_call_site:
8370 read_call_site_scope (die, cu);
8371 break;
c906108c 8372 case DW_TAG_class_type:
680b30c7 8373 case DW_TAG_interface_type:
c906108c
SS
8374 case DW_TAG_structure_type:
8375 case DW_TAG_union_type:
134d01f1 8376 process_structure_scope (die, cu);
c906108c
SS
8377 break;
8378 case DW_TAG_enumeration_type:
134d01f1 8379 process_enumeration_scope (die, cu);
c906108c 8380 break;
134d01f1 8381
f792889a
DJ
8382 /* These dies have a type, but processing them does not create
8383 a symbol or recurse to process the children. Therefore we can
8384 read them on-demand through read_type_die. */
c906108c 8385 case DW_TAG_subroutine_type:
72019c9c 8386 case DW_TAG_set_type:
c906108c 8387 case DW_TAG_array_type:
c906108c 8388 case DW_TAG_pointer_type:
c906108c 8389 case DW_TAG_ptr_to_member_type:
c906108c 8390 case DW_TAG_reference_type:
c906108c 8391 case DW_TAG_string_type:
c906108c 8392 break;
134d01f1 8393
c906108c 8394 case DW_TAG_base_type:
a02abb62 8395 case DW_TAG_subrange_type:
cb249c71 8396 case DW_TAG_typedef:
134d01f1
DJ
8397 /* Add a typedef symbol for the type definition, if it has a
8398 DW_AT_name. */
f792889a 8399 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8400 break;
c906108c 8401 case DW_TAG_common_block:
e7c27a73 8402 read_common_block (die, cu);
c906108c
SS
8403 break;
8404 case DW_TAG_common_inclusion:
8405 break;
d9fa45fe 8406 case DW_TAG_namespace:
4d4ec4e5 8407 cu->processing_has_namespace_info = 1;
e7c27a73 8408 read_namespace (die, cu);
d9fa45fe 8409 break;
5d7cb8df 8410 case DW_TAG_module:
4d4ec4e5 8411 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8412 read_module (die, cu);
8413 break;
d9fa45fe 8414 case DW_TAG_imported_declaration:
74921315
KS
8415 cu->processing_has_namespace_info = 1;
8416 if (read_namespace_alias (die, cu))
8417 break;
8418 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8419 case DW_TAG_imported_module:
4d4ec4e5 8420 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8421 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8422 || cu->language != language_fortran))
8423 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8424 dwarf_tag_name (die->tag));
8425 read_import_statement (die, cu);
d9fa45fe 8426 break;
95554aad
TT
8427
8428 case DW_TAG_imported_unit:
8429 process_imported_unit_die (die, cu);
8430 break;
8431
c906108c 8432 default:
e7c27a73 8433 new_symbol (die, NULL, cu);
c906108c
SS
8434 break;
8435 }
adde2bff
DE
8436
8437 do_cleanups (in_process);
c906108c 8438}
ca69b9e6
DE
8439\f
8440/* DWARF name computation. */
c906108c 8441
94af9270
KS
8442/* A helper function for dwarf2_compute_name which determines whether DIE
8443 needs to have the name of the scope prepended to the name listed in the
8444 die. */
8445
8446static int
8447die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8448{
1c809c68
TT
8449 struct attribute *attr;
8450
94af9270
KS
8451 switch (die->tag)
8452 {
8453 case DW_TAG_namespace:
8454 case DW_TAG_typedef:
8455 case DW_TAG_class_type:
8456 case DW_TAG_interface_type:
8457 case DW_TAG_structure_type:
8458 case DW_TAG_union_type:
8459 case DW_TAG_enumeration_type:
8460 case DW_TAG_enumerator:
8461 case DW_TAG_subprogram:
08a76f8a 8462 case DW_TAG_inlined_subroutine:
94af9270 8463 case DW_TAG_member:
74921315 8464 case DW_TAG_imported_declaration:
94af9270
KS
8465 return 1;
8466
8467 case DW_TAG_variable:
c2b0a229 8468 case DW_TAG_constant:
94af9270
KS
8469 /* We only need to prefix "globally" visible variables. These include
8470 any variable marked with DW_AT_external or any variable that
8471 lives in a namespace. [Variables in anonymous namespaces
8472 require prefixing, but they are not DW_AT_external.] */
8473
8474 if (dwarf2_attr (die, DW_AT_specification, cu))
8475 {
8476 struct dwarf2_cu *spec_cu = cu;
9a619af0 8477
94af9270
KS
8478 return die_needs_namespace (die_specification (die, &spec_cu),
8479 spec_cu);
8480 }
8481
1c809c68 8482 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8483 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8484 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8485 return 0;
8486 /* A variable in a lexical block of some kind does not need a
8487 namespace, even though in C++ such variables may be external
8488 and have a mangled name. */
8489 if (die->parent->tag == DW_TAG_lexical_block
8490 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8491 || die->parent->tag == DW_TAG_catch_block
8492 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8493 return 0;
8494 return 1;
94af9270
KS
8495
8496 default:
8497 return 0;
8498 }
8499}
8500
8501/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8502 compute the physname for the object, which include a method's:
9c37b5ae 8503 - formal parameters (C++),
a766d390 8504 - receiver type (Go),
a766d390
DE
8505
8506 The term "physname" is a bit confusing.
8507 For C++, for example, it is the demangled name.
8508 For Go, for example, it's the mangled name.
94af9270 8509
af6b7be1
JB
8510 For Ada, return the DIE's linkage name rather than the fully qualified
8511 name. PHYSNAME is ignored..
8512
94af9270
KS
8513 The result is allocated on the objfile_obstack and canonicalized. */
8514
8515static const char *
15d034d0
TT
8516dwarf2_compute_name (const char *name,
8517 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8518 int physname)
8519{
bb5ed363
DE
8520 struct objfile *objfile = cu->objfile;
8521
94af9270
KS
8522 if (name == NULL)
8523 name = dwarf2_name (die, cu);
8524
2ee7123e
DE
8525 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8526 but otherwise compute it by typename_concat inside GDB.
8527 FIXME: Actually this is not really true, or at least not always true.
8528 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8529 Fortran names because there is no mangling standard. So new_symbol_full
8530 will set the demangled name to the result of dwarf2_full_name, and it is
8531 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8532 if (cu->language == language_ada
8533 || (cu->language == language_fortran && physname))
8534 {
8535 /* For Ada unit, we prefer the linkage name over the name, as
8536 the former contains the exported name, which the user expects
8537 to be able to reference. Ideally, we want the user to be able
8538 to reference this entity using either natural or linkage name,
8539 but we haven't started looking at this enhancement yet. */
2ee7123e 8540 const char *linkage_name;
f55ee35c 8541
2ee7123e
DE
8542 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8543 if (linkage_name == NULL)
8544 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8545 if (linkage_name != NULL)
8546 return linkage_name;
f55ee35c
JK
8547 }
8548
94af9270
KS
8549 /* These are the only languages we know how to qualify names in. */
8550 if (name != NULL
9c37b5ae 8551 && (cu->language == language_cplus
c44af4eb
TT
8552 || cu->language == language_fortran || cu->language == language_d
8553 || cu->language == language_rust))
94af9270
KS
8554 {
8555 if (die_needs_namespace (die, cu))
8556 {
8557 long length;
0d5cff50 8558 const char *prefix;
34a68019 8559 const char *canonical_name = NULL;
94af9270 8560
d7e74731
PA
8561 string_file buf;
8562
94af9270 8563 prefix = determine_prefix (die, cu);
94af9270
KS
8564 if (*prefix != '\0')
8565 {
f55ee35c
JK
8566 char *prefixed_name = typename_concat (NULL, prefix, name,
8567 physname, cu);
9a619af0 8568
d7e74731 8569 buf.puts (prefixed_name);
94af9270
KS
8570 xfree (prefixed_name);
8571 }
8572 else
d7e74731 8573 buf.puts (name);
94af9270 8574
98bfdba5
PA
8575 /* Template parameters may be specified in the DIE's DW_AT_name, or
8576 as children with DW_TAG_template_type_param or
8577 DW_TAG_value_type_param. If the latter, add them to the name
8578 here. If the name already has template parameters, then
8579 skip this step; some versions of GCC emit both, and
8580 it is more efficient to use the pre-computed name.
8581
8582 Something to keep in mind about this process: it is very
8583 unlikely, or in some cases downright impossible, to produce
8584 something that will match the mangled name of a function.
8585 If the definition of the function has the same debug info,
8586 we should be able to match up with it anyway. But fallbacks
8587 using the minimal symbol, for instance to find a method
8588 implemented in a stripped copy of libstdc++, will not work.
8589 If we do not have debug info for the definition, we will have to
8590 match them up some other way.
8591
8592 When we do name matching there is a related problem with function
8593 templates; two instantiated function templates are allowed to
8594 differ only by their return types, which we do not add here. */
8595
8596 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8597 {
8598 struct attribute *attr;
8599 struct die_info *child;
8600 int first = 1;
8601
8602 die->building_fullname = 1;
8603
8604 for (child = die->child; child != NULL; child = child->sibling)
8605 {
8606 struct type *type;
12df843f 8607 LONGEST value;
d521ce57 8608 const gdb_byte *bytes;
98bfdba5
PA
8609 struct dwarf2_locexpr_baton *baton;
8610 struct value *v;
8611
8612 if (child->tag != DW_TAG_template_type_param
8613 && child->tag != DW_TAG_template_value_param)
8614 continue;
8615
8616 if (first)
8617 {
d7e74731 8618 buf.puts ("<");
98bfdba5
PA
8619 first = 0;
8620 }
8621 else
d7e74731 8622 buf.puts (", ");
98bfdba5
PA
8623
8624 attr = dwarf2_attr (child, DW_AT_type, cu);
8625 if (attr == NULL)
8626 {
8627 complaint (&symfile_complaints,
8628 _("template parameter missing DW_AT_type"));
d7e74731 8629 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8630 continue;
8631 }
8632 type = die_type (child, cu);
8633
8634 if (child->tag == DW_TAG_template_type_param)
8635 {
d7e74731 8636 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8637 continue;
8638 }
8639
8640 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8641 if (attr == NULL)
8642 {
8643 complaint (&symfile_complaints,
3e43a32a
MS
8644 _("template parameter missing "
8645 "DW_AT_const_value"));
d7e74731 8646 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8647 continue;
8648 }
8649
8650 dwarf2_const_value_attr (attr, type, name,
8651 &cu->comp_unit_obstack, cu,
8652 &value, &bytes, &baton);
8653
8654 if (TYPE_NOSIGN (type))
8655 /* GDB prints characters as NUMBER 'CHAR'. If that's
8656 changed, this can use value_print instead. */
d7e74731 8657 c_printchar (value, type, &buf);
98bfdba5
PA
8658 else
8659 {
8660 struct value_print_options opts;
8661
8662 if (baton != NULL)
8663 v = dwarf2_evaluate_loc_desc (type, NULL,
8664 baton->data,
8665 baton->size,
8666 baton->per_cu);
8667 else if (bytes != NULL)
8668 {
8669 v = allocate_value (type);
8670 memcpy (value_contents_writeable (v), bytes,
8671 TYPE_LENGTH (type));
8672 }
8673 else
8674 v = value_from_longest (type, value);
8675
3e43a32a
MS
8676 /* Specify decimal so that we do not depend on
8677 the radix. */
98bfdba5
PA
8678 get_formatted_print_options (&opts, 'd');
8679 opts.raw = 1;
d7e74731 8680 value_print (v, &buf, &opts);
98bfdba5
PA
8681 release_value (v);
8682 value_free (v);
8683 }
8684 }
8685
8686 die->building_fullname = 0;
8687
8688 if (!first)
8689 {
8690 /* Close the argument list, with a space if necessary
8691 (nested templates). */
d7e74731
PA
8692 if (!buf.empty () && buf.string ().back () == '>')
8693 buf.puts (" >");
98bfdba5 8694 else
d7e74731 8695 buf.puts (">");
98bfdba5
PA
8696 }
8697 }
8698
9c37b5ae 8699 /* For C++ methods, append formal parameter type
94af9270 8700 information, if PHYSNAME. */
6e70227d 8701
94af9270 8702 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8703 && cu->language == language_cplus)
94af9270
KS
8704 {
8705 struct type *type = read_type_die (die, cu);
8706
d7e74731 8707 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8708 &type_print_raw_options);
94af9270 8709
9c37b5ae 8710 if (cu->language == language_cplus)
94af9270 8711 {
60430eff
DJ
8712 /* Assume that an artificial first parameter is
8713 "this", but do not crash if it is not. RealView
8714 marks unnamed (and thus unused) parameters as
8715 artificial; there is no way to differentiate
8716 the two cases. */
94af9270
KS
8717 if (TYPE_NFIELDS (type) > 0
8718 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8719 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8720 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8721 0))))
d7e74731 8722 buf.puts (" const");
94af9270
KS
8723 }
8724 }
8725
d7e74731 8726 const std::string &intermediate_name = buf.string ();
94af9270
KS
8727
8728 if (cu->language == language_cplus)
34a68019 8729 canonical_name
322a8516 8730 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8731 &objfile->per_bfd->storage_obstack);
8732
8733 /* If we only computed INTERMEDIATE_NAME, or if
8734 INTERMEDIATE_NAME is already canonical, then we need to
8735 copy it to the appropriate obstack. */
322a8516 8736 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8737 name = ((const char *)
8738 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8739 intermediate_name.c_str (),
8740 intermediate_name.length ()));
34a68019
TT
8741 else
8742 name = canonical_name;
94af9270
KS
8743 }
8744 }
8745
8746 return name;
8747}
8748
0114d602
DJ
8749/* Return the fully qualified name of DIE, based on its DW_AT_name.
8750 If scope qualifiers are appropriate they will be added. The result
34a68019 8751 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8752 not have a name. NAME may either be from a previous call to
8753 dwarf2_name or NULL.
8754
9c37b5ae 8755 The output string will be canonicalized (if C++). */
0114d602
DJ
8756
8757static const char *
15d034d0 8758dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8759{
94af9270
KS
8760 return dwarf2_compute_name (name, die, cu, 0);
8761}
0114d602 8762
94af9270
KS
8763/* Construct a physname for the given DIE in CU. NAME may either be
8764 from a previous call to dwarf2_name or NULL. The result will be
8765 allocated on the objfile_objstack or NULL if the DIE does not have a
8766 name.
0114d602 8767
9c37b5ae 8768 The output string will be canonicalized (if C++). */
0114d602 8769
94af9270 8770static const char *
15d034d0 8771dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8772{
bb5ed363 8773 struct objfile *objfile = cu->objfile;
900e11f9
JK
8774 const char *retval, *mangled = NULL, *canon = NULL;
8775 struct cleanup *back_to;
8776 int need_copy = 1;
8777
8778 /* In this case dwarf2_compute_name is just a shortcut not building anything
8779 on its own. */
8780 if (!die_needs_namespace (die, cu))
8781 return dwarf2_compute_name (name, die, cu, 1);
8782
8783 back_to = make_cleanup (null_cleanup, NULL);
8784
7d45c7c3
KB
8785 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8786 if (mangled == NULL)
8787 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 8788
e98c9e7c
TT
8789 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8790 See https://github.com/rust-lang/rust/issues/32925. */
8791 if (cu->language == language_rust && mangled != NULL
8792 && strchr (mangled, '{') != NULL)
8793 mangled = NULL;
8794
900e11f9
JK
8795 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8796 has computed. */
7d45c7c3 8797 if (mangled != NULL)
900e11f9
JK
8798 {
8799 char *demangled;
8800
900e11f9
JK
8801 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8802 type. It is easier for GDB users to search for such functions as
8803 `name(params)' than `long name(params)'. In such case the minimal
8804 symbol names do not match the full symbol names but for template
8805 functions there is never a need to look up their definition from their
8806 declaration so the only disadvantage remains the minimal symbol
8807 variant `long name(params)' does not have the proper inferior type.
8808 */
8809
a766d390
DE
8810 if (cu->language == language_go)
8811 {
8812 /* This is a lie, but we already lie to the caller new_symbol_full.
8813 new_symbol_full assumes we return the mangled name.
8814 This just undoes that lie until things are cleaned up. */
8815 demangled = NULL;
8816 }
8817 else
8818 {
8de20a37 8819 demangled = gdb_demangle (mangled,
9c37b5ae 8820 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 8821 }
900e11f9
JK
8822 if (demangled)
8823 {
8824 make_cleanup (xfree, demangled);
8825 canon = demangled;
8826 }
8827 else
8828 {
8829 canon = mangled;
8830 need_copy = 0;
8831 }
8832 }
8833
8834 if (canon == NULL || check_physname)
8835 {
8836 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8837
8838 if (canon != NULL && strcmp (physname, canon) != 0)
8839 {
8840 /* It may not mean a bug in GDB. The compiler could also
8841 compute DW_AT_linkage_name incorrectly. But in such case
8842 GDB would need to be bug-to-bug compatible. */
8843
8844 complaint (&symfile_complaints,
8845 _("Computed physname <%s> does not match demangled <%s> "
8846 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8847 physname, canon, mangled, die->offset.sect_off,
8848 objfile_name (objfile));
900e11f9
JK
8849
8850 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8851 is available here - over computed PHYSNAME. It is safer
8852 against both buggy GDB and buggy compilers. */
8853
8854 retval = canon;
8855 }
8856 else
8857 {
8858 retval = physname;
8859 need_copy = 0;
8860 }
8861 }
8862 else
8863 retval = canon;
8864
8865 if (need_copy)
224c3ddb
SM
8866 retval = ((const char *)
8867 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8868 retval, strlen (retval)));
900e11f9
JK
8869
8870 do_cleanups (back_to);
8871 return retval;
0114d602
DJ
8872}
8873
74921315
KS
8874/* Inspect DIE in CU for a namespace alias. If one exists, record
8875 a new symbol for it.
8876
8877 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8878
8879static int
8880read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8881{
8882 struct attribute *attr;
8883
8884 /* If the die does not have a name, this is not a namespace
8885 alias. */
8886 attr = dwarf2_attr (die, DW_AT_name, cu);
8887 if (attr != NULL)
8888 {
8889 int num;
8890 struct die_info *d = die;
8891 struct dwarf2_cu *imported_cu = cu;
8892
8893 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8894 keep inspecting DIEs until we hit the underlying import. */
8895#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8896 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8897 {
8898 attr = dwarf2_attr (d, DW_AT_import, cu);
8899 if (attr == NULL)
8900 break;
8901
8902 d = follow_die_ref (d, attr, &imported_cu);
8903 if (d->tag != DW_TAG_imported_declaration)
8904 break;
8905 }
8906
8907 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8908 {
8909 complaint (&symfile_complaints,
8910 _("DIE at 0x%x has too many recursively imported "
8911 "declarations"), d->offset.sect_off);
8912 return 0;
8913 }
8914
8915 if (attr != NULL)
8916 {
8917 struct type *type;
8918 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8919
8920 type = get_die_type_at_offset (offset, cu->per_cu);
8921 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8922 {
8923 /* This declaration is a global namespace alias. Add
8924 a symbol for it whose type is the aliased namespace. */
8925 new_symbol (die, type, cu);
8926 return 1;
8927 }
8928 }
8929 }
8930
8931 return 0;
8932}
8933
22cee43f
PMR
8934/* Return the using directives repository (global or local?) to use in the
8935 current context for LANGUAGE.
8936
8937 For Ada, imported declarations can materialize renamings, which *may* be
8938 global. However it is impossible (for now?) in DWARF to distinguish
8939 "external" imported declarations and "static" ones. As all imported
8940 declarations seem to be static in all other languages, make them all CU-wide
8941 global only in Ada. */
8942
8943static struct using_direct **
8944using_directives (enum language language)
8945{
8946 if (language == language_ada && context_stack_depth == 0)
8947 return &global_using_directives;
8948 else
8949 return &local_using_directives;
8950}
8951
27aa8d6a
SW
8952/* Read the import statement specified by the given die and record it. */
8953
8954static void
8955read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8956{
bb5ed363 8957 struct objfile *objfile = cu->objfile;
27aa8d6a 8958 struct attribute *import_attr;
32019081 8959 struct die_info *imported_die, *child_die;
de4affc9 8960 struct dwarf2_cu *imported_cu;
27aa8d6a 8961 const char *imported_name;
794684b6 8962 const char *imported_name_prefix;
13387711
SW
8963 const char *canonical_name;
8964 const char *import_alias;
8965 const char *imported_declaration = NULL;
794684b6 8966 const char *import_prefix;
32019081
JK
8967 VEC (const_char_ptr) *excludes = NULL;
8968 struct cleanup *cleanups;
13387711 8969
27aa8d6a
SW
8970 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8971 if (import_attr == NULL)
8972 {
8973 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8974 dwarf_tag_name (die->tag));
8975 return;
8976 }
8977
de4affc9
CC
8978 imported_cu = cu;
8979 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8980 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8981 if (imported_name == NULL)
8982 {
8983 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8984
8985 The import in the following code:
8986 namespace A
8987 {
8988 typedef int B;
8989 }
8990
8991 int main ()
8992 {
8993 using A::B;
8994 B b;
8995 return b;
8996 }
8997
8998 ...
8999 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9000 <52> DW_AT_decl_file : 1
9001 <53> DW_AT_decl_line : 6
9002 <54> DW_AT_import : <0x75>
9003 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9004 <59> DW_AT_name : B
9005 <5b> DW_AT_decl_file : 1
9006 <5c> DW_AT_decl_line : 2
9007 <5d> DW_AT_type : <0x6e>
9008 ...
9009 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9010 <76> DW_AT_byte_size : 4
9011 <77> DW_AT_encoding : 5 (signed)
9012
9013 imports the wrong die ( 0x75 instead of 0x58 ).
9014 This case will be ignored until the gcc bug is fixed. */
9015 return;
9016 }
9017
82856980
SW
9018 /* Figure out the local name after import. */
9019 import_alias = dwarf2_name (die, cu);
27aa8d6a 9020
794684b6
SW
9021 /* Figure out where the statement is being imported to. */
9022 import_prefix = determine_prefix (die, cu);
9023
9024 /* Figure out what the scope of the imported die is and prepend it
9025 to the name of the imported die. */
de4affc9 9026 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9027
f55ee35c
JK
9028 if (imported_die->tag != DW_TAG_namespace
9029 && imported_die->tag != DW_TAG_module)
794684b6 9030 {
13387711
SW
9031 imported_declaration = imported_name;
9032 canonical_name = imported_name_prefix;
794684b6 9033 }
13387711 9034 else if (strlen (imported_name_prefix) > 0)
12aaed36 9035 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9036 imported_name_prefix,
9037 (cu->language == language_d ? "." : "::"),
9038 imported_name, (char *) NULL);
13387711
SW
9039 else
9040 canonical_name = imported_name;
794684b6 9041
32019081
JK
9042 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9043
9044 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9045 for (child_die = die->child; child_die && child_die->tag;
9046 child_die = sibling_die (child_die))
9047 {
9048 /* DWARF-4: A Fortran use statement with a “rename list” may be
9049 represented by an imported module entry with an import attribute
9050 referring to the module and owned entries corresponding to those
9051 entities that are renamed as part of being imported. */
9052
9053 if (child_die->tag != DW_TAG_imported_declaration)
9054 {
9055 complaint (&symfile_complaints,
9056 _("child DW_TAG_imported_declaration expected "
9057 "- DIE at 0x%x [in module %s]"),
4262abfb 9058 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9059 continue;
9060 }
9061
9062 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9063 if (import_attr == NULL)
9064 {
9065 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9066 dwarf_tag_name (child_die->tag));
9067 continue;
9068 }
9069
9070 imported_cu = cu;
9071 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9072 &imported_cu);
9073 imported_name = dwarf2_name (imported_die, imported_cu);
9074 if (imported_name == NULL)
9075 {
9076 complaint (&symfile_complaints,
9077 _("child DW_TAG_imported_declaration has unknown "
9078 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 9079 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9080 continue;
9081 }
9082
9083 VEC_safe_push (const_char_ptr, excludes, imported_name);
9084
9085 process_die (child_die, cu);
9086 }
9087
22cee43f
PMR
9088 add_using_directive (using_directives (cu->language),
9089 import_prefix,
9090 canonical_name,
9091 import_alias,
9092 imported_declaration,
9093 excludes,
9094 0,
9095 &objfile->objfile_obstack);
32019081
JK
9096
9097 do_cleanups (cleanups);
27aa8d6a
SW
9098}
9099
f4dc4d17 9100/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 9101
cb1df416
DJ
9102static void
9103free_cu_line_header (void *arg)
9104{
9a3c8263 9105 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
cb1df416
DJ
9106
9107 free_line_header (cu->line_header);
9108 cu->line_header = NULL;
9109}
9110
1b80a9fa
JK
9111/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9112 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9113 this, it was first present in GCC release 4.3.0. */
9114
9115static int
9116producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9117{
9118 if (!cu->checked_producer)
9119 check_producer (cu);
9120
9121 return cu->producer_is_gcc_lt_4_3;
9122}
9123
9291a0cd
TT
9124static void
9125find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 9126 const char **name, const char **comp_dir)
9291a0cd 9127{
9291a0cd
TT
9128 /* Find the filename. Do not use dwarf2_name here, since the filename
9129 is not a source language identifier. */
7d45c7c3
KB
9130 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9131 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9132
7d45c7c3
KB
9133 if (*comp_dir == NULL
9134 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9135 && IS_ABSOLUTE_PATH (*name))
9291a0cd 9136 {
15d034d0
TT
9137 char *d = ldirname (*name);
9138
9139 *comp_dir = d;
9140 if (d != NULL)
9141 make_cleanup (xfree, d);
9291a0cd
TT
9142 }
9143 if (*comp_dir != NULL)
9144 {
9145 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9146 directory, get rid of it. */
e6a959d6 9147 const char *cp = strchr (*comp_dir, ':');
9291a0cd
TT
9148
9149 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9150 *comp_dir = cp + 1;
9151 }
9152
9153 if (*name == NULL)
9154 *name = "<unknown>";
9155}
9156
f4dc4d17
DE
9157/* Handle DW_AT_stmt_list for a compilation unit.
9158 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9159 COMP_DIR is the compilation directory. LOWPC is passed to
9160 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9161
9162static void
9163handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9164 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9165{
527f3840 9166 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9167 struct attribute *attr;
527f3840
JK
9168 unsigned int line_offset;
9169 struct line_header line_header_local;
9170 hashval_t line_header_local_hash;
9171 unsigned u;
9172 void **slot;
9173 int decode_mapping;
2ab95328 9174
f4dc4d17
DE
9175 gdb_assert (! cu->per_cu->is_debug_types);
9176
2ab95328 9177 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9178 if (attr == NULL)
9179 return;
9180
9181 line_offset = DW_UNSND (attr);
9182
9183 /* The line header hash table is only created if needed (it exists to
9184 prevent redundant reading of the line table for partial_units).
9185 If we're given a partial_unit, we'll need it. If we're given a
9186 compile_unit, then use the line header hash table if it's already
9187 created, but don't create one just yet. */
9188
9189 if (dwarf2_per_objfile->line_header_hash == NULL
9190 && die->tag == DW_TAG_partial_unit)
2ab95328 9191 {
527f3840
JK
9192 dwarf2_per_objfile->line_header_hash
9193 = htab_create_alloc_ex (127, line_header_hash_voidp,
9194 line_header_eq_voidp,
9195 free_line_header_voidp,
9196 &objfile->objfile_obstack,
9197 hashtab_obstack_allocate,
9198 dummy_obstack_deallocate);
9199 }
2ab95328 9200
527f3840
JK
9201 line_header_local.offset.sect_off = line_offset;
9202 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9203 line_header_local_hash = line_header_hash (&line_header_local);
9204 if (dwarf2_per_objfile->line_header_hash != NULL)
9205 {
9206 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9207 &line_header_local,
9208 line_header_local_hash, NO_INSERT);
9209
9210 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9211 is not present in *SLOT (since if there is something in *SLOT then
9212 it will be for a partial_unit). */
9213 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9214 {
527f3840 9215 gdb_assert (*slot != NULL);
9a3c8263 9216 cu->line_header = (struct line_header *) *slot;
527f3840 9217 return;
dee91e82 9218 }
2ab95328 9219 }
527f3840
JK
9220
9221 /* dwarf_decode_line_header does not yet provide sufficient information.
9222 We always have to call also dwarf_decode_lines for it. */
9223 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9224 if (cu->line_header == NULL)
9225 return;
9226
9227 if (dwarf2_per_objfile->line_header_hash == NULL)
9228 slot = NULL;
9229 else
9230 {
9231 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9232 &line_header_local,
9233 line_header_local_hash, INSERT);
9234 gdb_assert (slot != NULL);
9235 }
9236 if (slot != NULL && *slot == NULL)
9237 {
9238 /* This newly decoded line number information unit will be owned
9239 by line_header_hash hash table. */
9240 *slot = cu->line_header;
9241 }
9242 else
9243 {
9244 /* We cannot free any current entry in (*slot) as that struct line_header
9245 may be already used by multiple CUs. Create only temporary decoded
9246 line_header for this CU - it may happen at most once for each line
9247 number information unit. And if we're not using line_header_hash
9248 then this is what we want as well. */
9249 gdb_assert (die->tag != DW_TAG_partial_unit);
9250 make_cleanup (free_cu_line_header, cu);
9251 }
9252 decode_mapping = (die->tag != DW_TAG_partial_unit);
9253 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9254 decode_mapping);
2ab95328
TT
9255}
9256
95554aad 9257/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9258
c906108c 9259static void
e7c27a73 9260read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9261{
dee91e82 9262 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9263 struct gdbarch *gdbarch = get_objfile_arch (objfile);
debd256d 9264 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 9265 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9266 CORE_ADDR highpc = ((CORE_ADDR) 0);
9267 struct attribute *attr;
15d034d0
TT
9268 const char *name = NULL;
9269 const char *comp_dir = NULL;
c906108c 9270 struct die_info *child_die;
e142c38c 9271 CORE_ADDR baseaddr;
6e70227d 9272
e142c38c 9273 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9274
fae299cd 9275 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9276
9277 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9278 from finish_block. */
2acceee2 9279 if (lowpc == ((CORE_ADDR) -1))
c906108c 9280 lowpc = highpc;
3e29f34a 9281 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9282
9291a0cd 9283 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 9284
95554aad 9285 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9286
f4b8a18d
KW
9287 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9288 standardised yet. As a workaround for the language detection we fall
9289 back to the DW_AT_producer string. */
9290 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9291 cu->language = language_opencl;
9292
3019eac3
DE
9293 /* Similar hack for Go. */
9294 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9295 set_cu_language (DW_LANG_Go, cu);
9296
f4dc4d17 9297 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
9298
9299 /* Decode line number information if present. We do this before
9300 processing child DIEs, so that the line header table is available
9301 for DW_AT_decl_file. */
c3b7b696 9302 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
3019eac3
DE
9303
9304 /* Process all dies in compilation unit. */
9305 if (die->child != NULL)
9306 {
9307 child_die = die->child;
9308 while (child_die && child_die->tag)
9309 {
9310 process_die (child_die, cu);
9311 child_die = sibling_die (child_die);
9312 }
9313 }
9314
9315 /* Decode macro information, if present. Dwarf 2 macro information
9316 refers to information in the line number info statement program
9317 header, so we can only read it if we've read the header
9318 successfully. */
0af92d60
JK
9319 attr = dwarf2_attr (die, DW_AT_macros, cu);
9320 if (attr == NULL)
9321 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9322 if (attr && cu->line_header)
9323 {
9324 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9325 complaint (&symfile_complaints,
0af92d60 9326 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9327
43f3e411 9328 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9329 }
9330 else
9331 {
9332 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9333 if (attr && cu->line_header)
9334 {
9335 unsigned int macro_offset = DW_UNSND (attr);
9336
43f3e411 9337 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9338 }
9339 }
9340
9341 do_cleanups (back_to);
9342}
9343
f4dc4d17
DE
9344/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9345 Create the set of symtabs used by this TU, or if this TU is sharing
9346 symtabs with another TU and the symtabs have already been created
9347 then restore those symtabs in the line header.
9348 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9349
9350static void
f4dc4d17 9351setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9352{
f4dc4d17
DE
9353 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9354 struct type_unit_group *tu_group;
9355 int first_time;
9356 struct line_header *lh;
3019eac3 9357 struct attribute *attr;
f4dc4d17 9358 unsigned int i, line_offset;
0186c6a7 9359 struct signatured_type *sig_type;
3019eac3 9360
f4dc4d17 9361 gdb_assert (per_cu->is_debug_types);
0186c6a7 9362 sig_type = (struct signatured_type *) per_cu;
3019eac3 9363
f4dc4d17 9364 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9365
f4dc4d17 9366 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9367 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9368 if (sig_type->type_unit_group == NULL)
9369 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9370 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9371
9372 /* If we've already processed this stmt_list there's no real need to
9373 do it again, we could fake it and just recreate the part we need
9374 (file name,index -> symtab mapping). If data shows this optimization
9375 is useful we can do it then. */
43f3e411 9376 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9377
9378 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9379 debug info. */
9380 lh = NULL;
9381 if (attr != NULL)
3019eac3 9382 {
f4dc4d17
DE
9383 line_offset = DW_UNSND (attr);
9384 lh = dwarf_decode_line_header (line_offset, cu);
9385 }
9386 if (lh == NULL)
9387 {
9388 if (first_time)
9389 dwarf2_start_symtab (cu, "", NULL, 0);
9390 else
9391 {
9392 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9393 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9394 }
f4dc4d17 9395 return;
3019eac3
DE
9396 }
9397
f4dc4d17
DE
9398 cu->line_header = lh;
9399 make_cleanup (free_cu_line_header, cu);
3019eac3 9400
f4dc4d17
DE
9401 if (first_time)
9402 {
43f3e411 9403 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9404
1fd60fc0
DE
9405 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9406 still initializing it, and our caller (a few levels up)
9407 process_full_type_unit still needs to know if this is the first
9408 time. */
9409
f4dc4d17
DE
9410 tu_group->num_symtabs = lh->num_file_names;
9411 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 9412
f4dc4d17
DE
9413 for (i = 0; i < lh->num_file_names; ++i)
9414 {
d521ce57 9415 const char *dir = NULL;
f4dc4d17 9416 struct file_entry *fe = &lh->file_names[i];
3019eac3 9417
afa6c9ab 9418 if (fe->dir_index && lh->include_dirs != NULL)
f4dc4d17 9419 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 9420 dwarf2_start_subfile (fe->name, dir);
3019eac3 9421
f4dc4d17
DE
9422 if (current_subfile->symtab == NULL)
9423 {
9424 /* NOTE: start_subfile will recognize when it's been passed
9425 a file it has already seen. So we can't assume there's a
43f3e411 9426 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9427 lh->file_names may contain dups. */
43f3e411
DE
9428 current_subfile->symtab
9429 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9430 }
9431
9432 fe->symtab = current_subfile->symtab;
9433 tu_group->symtabs[i] = fe->symtab;
9434 }
9435 }
9436 else
3019eac3 9437 {
0ab9ce85 9438 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17
DE
9439
9440 for (i = 0; i < lh->num_file_names; ++i)
9441 {
9442 struct file_entry *fe = &lh->file_names[i];
9443
9444 fe->symtab = tu_group->symtabs[i];
9445 }
3019eac3
DE
9446 }
9447
f4dc4d17
DE
9448 /* The main symtab is allocated last. Type units don't have DW_AT_name
9449 so they don't have a "real" (so to speak) symtab anyway.
9450 There is later code that will assign the main symtab to all symbols
9451 that don't have one. We need to handle the case of a symbol with a
9452 missing symtab (DW_AT_decl_file) anyway. */
9453}
3019eac3 9454
f4dc4d17
DE
9455/* Process DW_TAG_type_unit.
9456 For TUs we want to skip the first top level sibling if it's not the
9457 actual type being defined by this TU. In this case the first top
9458 level sibling is there to provide context only. */
3019eac3 9459
f4dc4d17
DE
9460static void
9461read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9462{
9463 struct die_info *child_die;
3019eac3 9464
f4dc4d17
DE
9465 prepare_one_comp_unit (cu, die, language_minimal);
9466
9467 /* Initialize (or reinitialize) the machinery for building symtabs.
9468 We do this before processing child DIEs, so that the line header table
9469 is available for DW_AT_decl_file. */
9470 setup_type_unit_groups (die, cu);
9471
9472 if (die->child != NULL)
9473 {
9474 child_die = die->child;
9475 while (child_die && child_die->tag)
9476 {
9477 process_die (child_die, cu);
9478 child_die = sibling_die (child_die);
9479 }
9480 }
3019eac3
DE
9481}
9482\f
80626a55
DE
9483/* DWO/DWP files.
9484
9485 http://gcc.gnu.org/wiki/DebugFission
9486 http://gcc.gnu.org/wiki/DebugFissionDWP
9487
9488 To simplify handling of both DWO files ("object" files with the DWARF info)
9489 and DWP files (a file with the DWOs packaged up into one file), we treat
9490 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9491
9492static hashval_t
9493hash_dwo_file (const void *item)
9494{
9a3c8263 9495 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9496 hashval_t hash;
3019eac3 9497
a2ce51a0
DE
9498 hash = htab_hash_string (dwo_file->dwo_name);
9499 if (dwo_file->comp_dir != NULL)
9500 hash += htab_hash_string (dwo_file->comp_dir);
9501 return hash;
3019eac3
DE
9502}
9503
9504static int
9505eq_dwo_file (const void *item_lhs, const void *item_rhs)
9506{
9a3c8263
SM
9507 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9508 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9509
a2ce51a0
DE
9510 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9511 return 0;
9512 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9513 return lhs->comp_dir == rhs->comp_dir;
9514 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9515}
9516
9517/* Allocate a hash table for DWO files. */
9518
9519static htab_t
9520allocate_dwo_file_hash_table (void)
9521{
9522 struct objfile *objfile = dwarf2_per_objfile->objfile;
9523
9524 return htab_create_alloc_ex (41,
9525 hash_dwo_file,
9526 eq_dwo_file,
9527 NULL,
9528 &objfile->objfile_obstack,
9529 hashtab_obstack_allocate,
9530 dummy_obstack_deallocate);
9531}
9532
80626a55
DE
9533/* Lookup DWO file DWO_NAME. */
9534
9535static void **
0ac5b59e 9536lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9537{
9538 struct dwo_file find_entry;
9539 void **slot;
9540
9541 if (dwarf2_per_objfile->dwo_files == NULL)
9542 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9543
9544 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9545 find_entry.dwo_name = dwo_name;
9546 find_entry.comp_dir = comp_dir;
80626a55
DE
9547 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9548
9549 return slot;
9550}
9551
3019eac3
DE
9552static hashval_t
9553hash_dwo_unit (const void *item)
9554{
9a3c8263 9555 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9556
9557 /* This drops the top 32 bits of the id, but is ok for a hash. */
9558 return dwo_unit->signature;
9559}
9560
9561static int
9562eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9563{
9a3c8263
SM
9564 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9565 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9566
9567 /* The signature is assumed to be unique within the DWO file.
9568 So while object file CU dwo_id's always have the value zero,
9569 that's OK, assuming each object file DWO file has only one CU,
9570 and that's the rule for now. */
9571 return lhs->signature == rhs->signature;
9572}
9573
9574/* Allocate a hash table for DWO CUs,TUs.
9575 There is one of these tables for each of CUs,TUs for each DWO file. */
9576
9577static htab_t
9578allocate_dwo_unit_table (struct objfile *objfile)
9579{
9580 /* Start out with a pretty small number.
9581 Generally DWO files contain only one CU and maybe some TUs. */
9582 return htab_create_alloc_ex (3,
9583 hash_dwo_unit,
9584 eq_dwo_unit,
9585 NULL,
9586 &objfile->objfile_obstack,
9587 hashtab_obstack_allocate,
9588 dummy_obstack_deallocate);
9589}
9590
80626a55 9591/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9592
19c3d4c9 9593struct create_dwo_cu_data
3019eac3
DE
9594{
9595 struct dwo_file *dwo_file;
19c3d4c9 9596 struct dwo_unit dwo_unit;
3019eac3
DE
9597};
9598
19c3d4c9 9599/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9600
9601static void
19c3d4c9
DE
9602create_dwo_cu_reader (const struct die_reader_specs *reader,
9603 const gdb_byte *info_ptr,
9604 struct die_info *comp_unit_die,
9605 int has_children,
9606 void *datap)
3019eac3
DE
9607{
9608 struct dwarf2_cu *cu = reader->cu;
3019eac3 9609 sect_offset offset = cu->per_cu->offset;
8a0459fd 9610 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9611 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9612 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9613 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9614 struct attribute *attr;
3019eac3
DE
9615
9616 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9617 if (attr == NULL)
9618 {
19c3d4c9
DE
9619 complaint (&symfile_complaints,
9620 _("Dwarf Error: debug entry at offset 0x%x is missing"
9621 " its dwo_id [in module %s]"),
9622 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
9623 return;
9624 }
9625
3019eac3
DE
9626 dwo_unit->dwo_file = dwo_file;
9627 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9628 dwo_unit->section = section;
3019eac3
DE
9629 dwo_unit->offset = offset;
9630 dwo_unit->length = cu->per_cu->length;
9631
b4f54984 9632 if (dwarf_read_debug)
4031ecc5
DE
9633 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9634 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
9635}
9636
19c3d4c9
DE
9637/* Create the dwo_unit for the lone CU in DWO_FILE.
9638 Note: This function processes DWO files only, not DWP files. */
3019eac3 9639
19c3d4c9
DE
9640static struct dwo_unit *
9641create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9642{
9643 struct objfile *objfile = dwarf2_per_objfile->objfile;
9644 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9645 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9646 struct create_dwo_cu_data create_dwo_cu_data;
9647 struct dwo_unit *dwo_unit;
3019eac3
DE
9648
9649 dwarf2_read_section (objfile, section);
9650 info_ptr = section->buffer;
9651
9652 if (info_ptr == NULL)
9653 return NULL;
9654
b4f54984 9655 if (dwarf_read_debug)
19c3d4c9
DE
9656 {
9657 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9658 get_section_name (section),
9659 get_section_file_name (section));
19c3d4c9 9660 }
3019eac3 9661
19c3d4c9
DE
9662 create_dwo_cu_data.dwo_file = dwo_file;
9663 dwo_unit = NULL;
3019eac3
DE
9664
9665 end_ptr = info_ptr + section->size;
9666 while (info_ptr < end_ptr)
9667 {
9668 struct dwarf2_per_cu_data per_cu;
9669
19c3d4c9
DE
9670 memset (&create_dwo_cu_data.dwo_unit, 0,
9671 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9672 memset (&per_cu, 0, sizeof (per_cu));
9673 per_cu.objfile = objfile;
9674 per_cu.is_debug_types = 0;
9675 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 9676 per_cu.section = section;
3019eac3 9677
33e80786 9678 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9679 create_dwo_cu_reader,
9680 &create_dwo_cu_data);
9681
9682 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9683 {
9684 /* If we've already found one, complain. We only support one
9685 because having more than one requires hacking the dwo_name of
9686 each to match, which is highly unlikely to happen. */
9687 if (dwo_unit != NULL)
9688 {
9689 complaint (&symfile_complaints,
9690 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9691 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9692 break;
9693 }
9694
9695 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9696 *dwo_unit = create_dwo_cu_data.dwo_unit;
9697 }
3019eac3
DE
9698
9699 info_ptr += per_cu.length;
9700 }
9701
19c3d4c9 9702 return dwo_unit;
3019eac3
DE
9703}
9704
80626a55
DE
9705/* DWP file .debug_{cu,tu}_index section format:
9706 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9707
d2415c6c
DE
9708 DWP Version 1:
9709
80626a55
DE
9710 Both index sections have the same format, and serve to map a 64-bit
9711 signature to a set of section numbers. Each section begins with a header,
9712 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9713 indexes, and a pool of 32-bit section numbers. The index sections will be
9714 aligned at 8-byte boundaries in the file.
9715
d2415c6c
DE
9716 The index section header consists of:
9717
9718 V, 32 bit version number
9719 -, 32 bits unused
9720 N, 32 bit number of compilation units or type units in the index
9721 M, 32 bit number of slots in the hash table
80626a55 9722
d2415c6c 9723 Numbers are recorded using the byte order of the application binary.
80626a55 9724
d2415c6c
DE
9725 The hash table begins at offset 16 in the section, and consists of an array
9726 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9727 order of the application binary). Unused slots in the hash table are 0.
9728 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9729
d2415c6c
DE
9730 The parallel table begins immediately after the hash table
9731 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9732 array of 32-bit indexes (using the byte order of the application binary),
9733 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9734 table contains a 32-bit index into the pool of section numbers. For unused
9735 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9736
73869dc2
DE
9737 The pool of section numbers begins immediately following the hash table
9738 (at offset 16 + 12 * M from the beginning of the section). The pool of
9739 section numbers consists of an array of 32-bit words (using the byte order
9740 of the application binary). Each item in the array is indexed starting
9741 from 0. The hash table entry provides the index of the first section
9742 number in the set. Additional section numbers in the set follow, and the
9743 set is terminated by a 0 entry (section number 0 is not used in ELF).
9744
9745 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9746 section must be the first entry in the set, and the .debug_abbrev.dwo must
9747 be the second entry. Other members of the set may follow in any order.
9748
9749 ---
9750
9751 DWP Version 2:
9752
9753 DWP Version 2 combines all the .debug_info, etc. sections into one,
9754 and the entries in the index tables are now offsets into these sections.
9755 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9756 section.
9757
9758 Index Section Contents:
9759 Header
9760 Hash Table of Signatures dwp_hash_table.hash_table
9761 Parallel Table of Indices dwp_hash_table.unit_table
9762 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9763 Table of Section Sizes dwp_hash_table.v2.sizes
9764
9765 The index section header consists of:
9766
9767 V, 32 bit version number
9768 L, 32 bit number of columns in the table of section offsets
9769 N, 32 bit number of compilation units or type units in the index
9770 M, 32 bit number of slots in the hash table
9771
9772 Numbers are recorded using the byte order of the application binary.
9773
9774 The hash table has the same format as version 1.
9775 The parallel table of indices has the same format as version 1,
9776 except that the entries are origin-1 indices into the table of sections
9777 offsets and the table of section sizes.
9778
9779 The table of offsets begins immediately following the parallel table
9780 (at offset 16 + 12 * M from the beginning of the section). The table is
9781 a two-dimensional array of 32-bit words (using the byte order of the
9782 application binary), with L columns and N+1 rows, in row-major order.
9783 Each row in the array is indexed starting from 0. The first row provides
9784 a key to the remaining rows: each column in this row provides an identifier
9785 for a debug section, and the offsets in the same column of subsequent rows
9786 refer to that section. The section identifiers are:
9787
9788 DW_SECT_INFO 1 .debug_info.dwo
9789 DW_SECT_TYPES 2 .debug_types.dwo
9790 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9791 DW_SECT_LINE 4 .debug_line.dwo
9792 DW_SECT_LOC 5 .debug_loc.dwo
9793 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9794 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9795 DW_SECT_MACRO 8 .debug_macro.dwo
9796
9797 The offsets provided by the CU and TU index sections are the base offsets
9798 for the contributions made by each CU or TU to the corresponding section
9799 in the package file. Each CU and TU header contains an abbrev_offset
9800 field, used to find the abbreviations table for that CU or TU within the
9801 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9802 be interpreted as relative to the base offset given in the index section.
9803 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9804 should be interpreted as relative to the base offset for .debug_line.dwo,
9805 and offsets into other debug sections obtained from DWARF attributes should
9806 also be interpreted as relative to the corresponding base offset.
9807
9808 The table of sizes begins immediately following the table of offsets.
9809 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9810 with L columns and N rows, in row-major order. Each row in the array is
9811 indexed starting from 1 (row 0 is shared by the two tables).
9812
9813 ---
9814
9815 Hash table lookup is handled the same in version 1 and 2:
9816
9817 We assume that N and M will not exceed 2^32 - 1.
9818 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9819
d2415c6c
DE
9820 Given a 64-bit compilation unit signature or a type signature S, an entry
9821 in the hash table is located as follows:
80626a55 9822
d2415c6c
DE
9823 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9824 the low-order k bits all set to 1.
80626a55 9825
d2415c6c 9826 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9827
d2415c6c
DE
9828 3) If the hash table entry at index H matches the signature, use that
9829 entry. If the hash table entry at index H is unused (all zeroes),
9830 terminate the search: the signature is not present in the table.
80626a55 9831
d2415c6c 9832 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9833
d2415c6c 9834 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9835 to stop at an unused slot or find the match. */
80626a55
DE
9836
9837/* Create a hash table to map DWO IDs to their CU/TU entry in
9838 .debug_{info,types}.dwo in DWP_FILE.
9839 Returns NULL if there isn't one.
9840 Note: This function processes DWP files only, not DWO files. */
9841
9842static struct dwp_hash_table *
9843create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9844{
9845 struct objfile *objfile = dwarf2_per_objfile->objfile;
9846 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9847 const gdb_byte *index_ptr, *index_end;
80626a55 9848 struct dwarf2_section_info *index;
73869dc2 9849 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9850 struct dwp_hash_table *htab;
9851
9852 if (is_debug_types)
9853 index = &dwp_file->sections.tu_index;
9854 else
9855 index = &dwp_file->sections.cu_index;
9856
9857 if (dwarf2_section_empty_p (index))
9858 return NULL;
9859 dwarf2_read_section (objfile, index);
9860
9861 index_ptr = index->buffer;
9862 index_end = index_ptr + index->size;
9863
9864 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9865 index_ptr += 4;
9866 if (version == 2)
9867 nr_columns = read_4_bytes (dbfd, index_ptr);
9868 else
9869 nr_columns = 0;
9870 index_ptr += 4;
80626a55
DE
9871 nr_units = read_4_bytes (dbfd, index_ptr);
9872 index_ptr += 4;
9873 nr_slots = read_4_bytes (dbfd, index_ptr);
9874 index_ptr += 4;
9875
73869dc2 9876 if (version != 1 && version != 2)
80626a55 9877 {
21aa081e 9878 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9879 " [in module %s]"),
21aa081e 9880 pulongest (version), dwp_file->name);
80626a55
DE
9881 }
9882 if (nr_slots != (nr_slots & -nr_slots))
9883 {
21aa081e 9884 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9885 " is not power of 2 [in module %s]"),
21aa081e 9886 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9887 }
9888
9889 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9890 htab->version = version;
9891 htab->nr_columns = nr_columns;
80626a55
DE
9892 htab->nr_units = nr_units;
9893 htab->nr_slots = nr_slots;
9894 htab->hash_table = index_ptr;
9895 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9896
9897 /* Exit early if the table is empty. */
9898 if (nr_slots == 0 || nr_units == 0
9899 || (version == 2 && nr_columns == 0))
9900 {
9901 /* All must be zero. */
9902 if (nr_slots != 0 || nr_units != 0
9903 || (version == 2 && nr_columns != 0))
9904 {
9905 complaint (&symfile_complaints,
9906 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9907 " all zero [in modules %s]"),
9908 dwp_file->name);
9909 }
9910 return htab;
9911 }
9912
9913 if (version == 1)
9914 {
9915 htab->section_pool.v1.indices =
9916 htab->unit_table + sizeof (uint32_t) * nr_slots;
9917 /* It's harder to decide whether the section is too small in v1.
9918 V1 is deprecated anyway so we punt. */
9919 }
9920 else
9921 {
9922 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9923 int *ids = htab->section_pool.v2.section_ids;
9924 /* Reverse map for error checking. */
9925 int ids_seen[DW_SECT_MAX + 1];
9926 int i;
9927
9928 if (nr_columns < 2)
9929 {
9930 error (_("Dwarf Error: bad DWP hash table, too few columns"
9931 " in section table [in module %s]"),
9932 dwp_file->name);
9933 }
9934 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9935 {
9936 error (_("Dwarf Error: bad DWP hash table, too many columns"
9937 " in section table [in module %s]"),
9938 dwp_file->name);
9939 }
9940 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9941 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9942 for (i = 0; i < nr_columns; ++i)
9943 {
9944 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9945
9946 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9947 {
9948 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9949 " in section table [in module %s]"),
9950 id, dwp_file->name);
9951 }
9952 if (ids_seen[id] != -1)
9953 {
9954 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9955 " id %d in section table [in module %s]"),
9956 id, dwp_file->name);
9957 }
9958 ids_seen[id] = i;
9959 ids[i] = id;
9960 }
9961 /* Must have exactly one info or types section. */
9962 if (((ids_seen[DW_SECT_INFO] != -1)
9963 + (ids_seen[DW_SECT_TYPES] != -1))
9964 != 1)
9965 {
9966 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9967 " DWO info/types section [in module %s]"),
9968 dwp_file->name);
9969 }
9970 /* Must have an abbrev section. */
9971 if (ids_seen[DW_SECT_ABBREV] == -1)
9972 {
9973 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9974 " section [in module %s]"),
9975 dwp_file->name);
9976 }
9977 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9978 htab->section_pool.v2.sizes =
9979 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9980 * nr_units * nr_columns);
9981 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9982 * nr_units * nr_columns))
9983 > index_end)
9984 {
9985 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9986 " [in module %s]"),
9987 dwp_file->name);
9988 }
9989 }
80626a55
DE
9990
9991 return htab;
9992}
9993
9994/* Update SECTIONS with the data from SECTP.
9995
9996 This function is like the other "locate" section routines that are
9997 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 9998 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
9999
10000 The result is non-zero for success, or zero if an error was found. */
10001
10002static int
73869dc2
DE
10003locate_v1_virtual_dwo_sections (asection *sectp,
10004 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10005{
10006 const struct dwop_section_names *names = &dwop_section_names;
10007
10008 if (section_is_p (sectp->name, &names->abbrev_dwo))
10009 {
10010 /* There can be only one. */
049412e3 10011 if (sections->abbrev.s.section != NULL)
80626a55 10012 return 0;
049412e3 10013 sections->abbrev.s.section = sectp;
80626a55
DE
10014 sections->abbrev.size = bfd_get_section_size (sectp);
10015 }
10016 else if (section_is_p (sectp->name, &names->info_dwo)
10017 || section_is_p (sectp->name, &names->types_dwo))
10018 {
10019 /* There can be only one. */
049412e3 10020 if (sections->info_or_types.s.section != NULL)
80626a55 10021 return 0;
049412e3 10022 sections->info_or_types.s.section = sectp;
80626a55
DE
10023 sections->info_or_types.size = bfd_get_section_size (sectp);
10024 }
10025 else if (section_is_p (sectp->name, &names->line_dwo))
10026 {
10027 /* There can be only one. */
049412e3 10028 if (sections->line.s.section != NULL)
80626a55 10029 return 0;
049412e3 10030 sections->line.s.section = sectp;
80626a55
DE
10031 sections->line.size = bfd_get_section_size (sectp);
10032 }
10033 else if (section_is_p (sectp->name, &names->loc_dwo))
10034 {
10035 /* There can be only one. */
049412e3 10036 if (sections->loc.s.section != NULL)
80626a55 10037 return 0;
049412e3 10038 sections->loc.s.section = sectp;
80626a55
DE
10039 sections->loc.size = bfd_get_section_size (sectp);
10040 }
10041 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10042 {
10043 /* There can be only one. */
049412e3 10044 if (sections->macinfo.s.section != NULL)
80626a55 10045 return 0;
049412e3 10046 sections->macinfo.s.section = sectp;
80626a55
DE
10047 sections->macinfo.size = bfd_get_section_size (sectp);
10048 }
10049 else if (section_is_p (sectp->name, &names->macro_dwo))
10050 {
10051 /* There can be only one. */
049412e3 10052 if (sections->macro.s.section != NULL)
80626a55 10053 return 0;
049412e3 10054 sections->macro.s.section = sectp;
80626a55
DE
10055 sections->macro.size = bfd_get_section_size (sectp);
10056 }
10057 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10058 {
10059 /* There can be only one. */
049412e3 10060 if (sections->str_offsets.s.section != NULL)
80626a55 10061 return 0;
049412e3 10062 sections->str_offsets.s.section = sectp;
80626a55
DE
10063 sections->str_offsets.size = bfd_get_section_size (sectp);
10064 }
10065 else
10066 {
10067 /* No other kind of section is valid. */
10068 return 0;
10069 }
10070
10071 return 1;
10072}
10073
73869dc2
DE
10074/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10075 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10076 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10077 This is for DWP version 1 files. */
80626a55
DE
10078
10079static struct dwo_unit *
73869dc2
DE
10080create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10081 uint32_t unit_index,
10082 const char *comp_dir,
10083 ULONGEST signature, int is_debug_types)
80626a55
DE
10084{
10085 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10086 const struct dwp_hash_table *dwp_htab =
10087 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10088 bfd *dbfd = dwp_file->dbfd;
10089 const char *kind = is_debug_types ? "TU" : "CU";
10090 struct dwo_file *dwo_file;
10091 struct dwo_unit *dwo_unit;
73869dc2 10092 struct virtual_v1_dwo_sections sections;
80626a55
DE
10093 void **dwo_file_slot;
10094 char *virtual_dwo_name;
80626a55
DE
10095 struct cleanup *cleanups;
10096 int i;
10097
73869dc2
DE
10098 gdb_assert (dwp_file->version == 1);
10099
b4f54984 10100 if (dwarf_read_debug)
80626a55 10101 {
73869dc2 10102 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10103 kind,
73869dc2 10104 pulongest (unit_index), hex_string (signature),
80626a55
DE
10105 dwp_file->name);
10106 }
10107
19ac8c2e 10108 /* Fetch the sections of this DWO unit.
80626a55
DE
10109 Put a limit on the number of sections we look for so that bad data
10110 doesn't cause us to loop forever. */
10111
73869dc2 10112#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10113 (1 /* .debug_info or .debug_types */ \
10114 + 1 /* .debug_abbrev */ \
10115 + 1 /* .debug_line */ \
10116 + 1 /* .debug_loc */ \
10117 + 1 /* .debug_str_offsets */ \
19ac8c2e 10118 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10119 + 1 /* trailing zero */)
10120
10121 memset (&sections, 0, sizeof (sections));
10122 cleanups = make_cleanup (null_cleanup, 0);
10123
73869dc2 10124 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10125 {
10126 asection *sectp;
10127 uint32_t section_nr =
10128 read_4_bytes (dbfd,
73869dc2
DE
10129 dwp_htab->section_pool.v1.indices
10130 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10131
10132 if (section_nr == 0)
10133 break;
10134 if (section_nr >= dwp_file->num_sections)
10135 {
10136 error (_("Dwarf Error: bad DWP hash table, section number too large"
10137 " [in module %s]"),
10138 dwp_file->name);
10139 }
10140
10141 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10142 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10143 {
10144 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10145 " [in module %s]"),
10146 dwp_file->name);
10147 }
10148 }
10149
10150 if (i < 2
a32a8923
DE
10151 || dwarf2_section_empty_p (&sections.info_or_types)
10152 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10153 {
10154 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10155 " [in module %s]"),
10156 dwp_file->name);
10157 }
73869dc2 10158 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10159 {
10160 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10161 " [in module %s]"),
10162 dwp_file->name);
10163 }
10164
10165 /* It's easier for the rest of the code if we fake a struct dwo_file and
10166 have dwo_unit "live" in that. At least for now.
10167
10168 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10169 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10170 file, we can combine them back into a virtual DWO file to save space
10171 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10172 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10173
2792b94d
PM
10174 virtual_dwo_name =
10175 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10176 get_section_id (&sections.abbrev),
10177 get_section_id (&sections.line),
10178 get_section_id (&sections.loc),
10179 get_section_id (&sections.str_offsets));
80626a55
DE
10180 make_cleanup (xfree, virtual_dwo_name);
10181 /* Can we use an existing virtual DWO file? */
0ac5b59e 10182 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10183 /* Create one if necessary. */
10184 if (*dwo_file_slot == NULL)
10185 {
b4f54984 10186 if (dwarf_read_debug)
80626a55
DE
10187 {
10188 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10189 virtual_dwo_name);
10190 }
10191 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10192 dwo_file->dwo_name
10193 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10194 virtual_dwo_name,
10195 strlen (virtual_dwo_name));
0ac5b59e 10196 dwo_file->comp_dir = comp_dir;
80626a55
DE
10197 dwo_file->sections.abbrev = sections.abbrev;
10198 dwo_file->sections.line = sections.line;
10199 dwo_file->sections.loc = sections.loc;
10200 dwo_file->sections.macinfo = sections.macinfo;
10201 dwo_file->sections.macro = sections.macro;
10202 dwo_file->sections.str_offsets = sections.str_offsets;
10203 /* The "str" section is global to the entire DWP file. */
10204 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10205 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10206 there's no need to record it in dwo_file.
10207 Also, we can't simply record type sections in dwo_file because
10208 we record a pointer into the vector in dwo_unit. As we collect more
10209 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10210 for it, invalidating all copies of pointers into the previous
10211 contents. */
80626a55
DE
10212 *dwo_file_slot = dwo_file;
10213 }
10214 else
10215 {
b4f54984 10216 if (dwarf_read_debug)
80626a55
DE
10217 {
10218 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10219 virtual_dwo_name);
10220 }
9a3c8263 10221 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10222 }
10223 do_cleanups (cleanups);
10224
10225 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10226 dwo_unit->dwo_file = dwo_file;
10227 dwo_unit->signature = signature;
8d749320
SM
10228 dwo_unit->section =
10229 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10230 *dwo_unit->section = sections.info_or_types;
57d63ce2 10231 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10232
10233 return dwo_unit;
10234}
10235
73869dc2
DE
10236/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10237 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10238 piece within that section used by a TU/CU, return a virtual section
10239 of just that piece. */
10240
10241static struct dwarf2_section_info
10242create_dwp_v2_section (struct dwarf2_section_info *section,
10243 bfd_size_type offset, bfd_size_type size)
10244{
10245 struct dwarf2_section_info result;
10246 asection *sectp;
10247
10248 gdb_assert (section != NULL);
10249 gdb_assert (!section->is_virtual);
10250
10251 memset (&result, 0, sizeof (result));
10252 result.s.containing_section = section;
10253 result.is_virtual = 1;
10254
10255 if (size == 0)
10256 return result;
10257
10258 sectp = get_section_bfd_section (section);
10259
10260 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10261 bounds of the real section. This is a pretty-rare event, so just
10262 flag an error (easier) instead of a warning and trying to cope. */
10263 if (sectp == NULL
10264 || offset + size > bfd_get_section_size (sectp))
10265 {
10266 bfd *abfd = sectp->owner;
10267
10268 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10269 " in section %s [in module %s]"),
10270 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10271 objfile_name (dwarf2_per_objfile->objfile));
10272 }
10273
10274 result.virtual_offset = offset;
10275 result.size = size;
10276 return result;
10277}
10278
10279/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10280 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10281 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10282 This is for DWP version 2 files. */
10283
10284static struct dwo_unit *
10285create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10286 uint32_t unit_index,
10287 const char *comp_dir,
10288 ULONGEST signature, int is_debug_types)
10289{
10290 struct objfile *objfile = dwarf2_per_objfile->objfile;
10291 const struct dwp_hash_table *dwp_htab =
10292 is_debug_types ? dwp_file->tus : dwp_file->cus;
10293 bfd *dbfd = dwp_file->dbfd;
10294 const char *kind = is_debug_types ? "TU" : "CU";
10295 struct dwo_file *dwo_file;
10296 struct dwo_unit *dwo_unit;
10297 struct virtual_v2_dwo_sections sections;
10298 void **dwo_file_slot;
10299 char *virtual_dwo_name;
73869dc2
DE
10300 struct cleanup *cleanups;
10301 int i;
10302
10303 gdb_assert (dwp_file->version == 2);
10304
b4f54984 10305 if (dwarf_read_debug)
73869dc2
DE
10306 {
10307 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10308 kind,
10309 pulongest (unit_index), hex_string (signature),
10310 dwp_file->name);
10311 }
10312
10313 /* Fetch the section offsets of this DWO unit. */
10314
10315 memset (&sections, 0, sizeof (sections));
10316 cleanups = make_cleanup (null_cleanup, 0);
10317
10318 for (i = 0; i < dwp_htab->nr_columns; ++i)
10319 {
10320 uint32_t offset = read_4_bytes (dbfd,
10321 dwp_htab->section_pool.v2.offsets
10322 + (((unit_index - 1) * dwp_htab->nr_columns
10323 + i)
10324 * sizeof (uint32_t)));
10325 uint32_t size = read_4_bytes (dbfd,
10326 dwp_htab->section_pool.v2.sizes
10327 + (((unit_index - 1) * dwp_htab->nr_columns
10328 + i)
10329 * sizeof (uint32_t)));
10330
10331 switch (dwp_htab->section_pool.v2.section_ids[i])
10332 {
10333 case DW_SECT_INFO:
10334 case DW_SECT_TYPES:
10335 sections.info_or_types_offset = offset;
10336 sections.info_or_types_size = size;
10337 break;
10338 case DW_SECT_ABBREV:
10339 sections.abbrev_offset = offset;
10340 sections.abbrev_size = size;
10341 break;
10342 case DW_SECT_LINE:
10343 sections.line_offset = offset;
10344 sections.line_size = size;
10345 break;
10346 case DW_SECT_LOC:
10347 sections.loc_offset = offset;
10348 sections.loc_size = size;
10349 break;
10350 case DW_SECT_STR_OFFSETS:
10351 sections.str_offsets_offset = offset;
10352 sections.str_offsets_size = size;
10353 break;
10354 case DW_SECT_MACINFO:
10355 sections.macinfo_offset = offset;
10356 sections.macinfo_size = size;
10357 break;
10358 case DW_SECT_MACRO:
10359 sections.macro_offset = offset;
10360 sections.macro_size = size;
10361 break;
10362 }
10363 }
10364
10365 /* It's easier for the rest of the code if we fake a struct dwo_file and
10366 have dwo_unit "live" in that. At least for now.
10367
10368 The DWP file can be made up of a random collection of CUs and TUs.
10369 However, for each CU + set of TUs that came from the same original DWO
10370 file, we can combine them back into a virtual DWO file to save space
10371 (fewer struct dwo_file objects to allocate). Remember that for really
10372 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10373
10374 virtual_dwo_name =
10375 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10376 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10377 (long) (sections.line_size ? sections.line_offset : 0),
10378 (long) (sections.loc_size ? sections.loc_offset : 0),
10379 (long) (sections.str_offsets_size
10380 ? sections.str_offsets_offset : 0));
10381 make_cleanup (xfree, virtual_dwo_name);
10382 /* Can we use an existing virtual DWO file? */
10383 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10384 /* Create one if necessary. */
10385 if (*dwo_file_slot == NULL)
10386 {
b4f54984 10387 if (dwarf_read_debug)
73869dc2
DE
10388 {
10389 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10390 virtual_dwo_name);
10391 }
10392 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10393 dwo_file->dwo_name
10394 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10395 virtual_dwo_name,
10396 strlen (virtual_dwo_name));
73869dc2
DE
10397 dwo_file->comp_dir = comp_dir;
10398 dwo_file->sections.abbrev =
10399 create_dwp_v2_section (&dwp_file->sections.abbrev,
10400 sections.abbrev_offset, sections.abbrev_size);
10401 dwo_file->sections.line =
10402 create_dwp_v2_section (&dwp_file->sections.line,
10403 sections.line_offset, sections.line_size);
10404 dwo_file->sections.loc =
10405 create_dwp_v2_section (&dwp_file->sections.loc,
10406 sections.loc_offset, sections.loc_size);
10407 dwo_file->sections.macinfo =
10408 create_dwp_v2_section (&dwp_file->sections.macinfo,
10409 sections.macinfo_offset, sections.macinfo_size);
10410 dwo_file->sections.macro =
10411 create_dwp_v2_section (&dwp_file->sections.macro,
10412 sections.macro_offset, sections.macro_size);
10413 dwo_file->sections.str_offsets =
10414 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10415 sections.str_offsets_offset,
10416 sections.str_offsets_size);
10417 /* The "str" section is global to the entire DWP file. */
10418 dwo_file->sections.str = dwp_file->sections.str;
10419 /* The info or types section is assigned below to dwo_unit,
10420 there's no need to record it in dwo_file.
10421 Also, we can't simply record type sections in dwo_file because
10422 we record a pointer into the vector in dwo_unit. As we collect more
10423 types we'll grow the vector and eventually have to reallocate space
10424 for it, invalidating all copies of pointers into the previous
10425 contents. */
10426 *dwo_file_slot = dwo_file;
10427 }
10428 else
10429 {
b4f54984 10430 if (dwarf_read_debug)
73869dc2
DE
10431 {
10432 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10433 virtual_dwo_name);
10434 }
9a3c8263 10435 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10436 }
10437 do_cleanups (cleanups);
10438
10439 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10440 dwo_unit->dwo_file = dwo_file;
10441 dwo_unit->signature = signature;
8d749320
SM
10442 dwo_unit->section =
10443 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10444 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10445 ? &dwp_file->sections.types
10446 : &dwp_file->sections.info,
10447 sections.info_or_types_offset,
10448 sections.info_or_types_size);
10449 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10450
10451 return dwo_unit;
10452}
10453
57d63ce2
DE
10454/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10455 Returns NULL if the signature isn't found. */
80626a55
DE
10456
10457static struct dwo_unit *
57d63ce2
DE
10458lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10459 ULONGEST signature, int is_debug_types)
80626a55 10460{
57d63ce2
DE
10461 const struct dwp_hash_table *dwp_htab =
10462 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10463 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10464 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10465 uint32_t hash = signature & mask;
10466 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10467 unsigned int i;
10468 void **slot;
870f88f7 10469 struct dwo_unit find_dwo_cu;
80626a55
DE
10470
10471 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10472 find_dwo_cu.signature = signature;
19ac8c2e
DE
10473 slot = htab_find_slot (is_debug_types
10474 ? dwp_file->loaded_tus
10475 : dwp_file->loaded_cus,
10476 &find_dwo_cu, INSERT);
80626a55
DE
10477
10478 if (*slot != NULL)
9a3c8263 10479 return (struct dwo_unit *) *slot;
80626a55
DE
10480
10481 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10482 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10483 {
10484 ULONGEST signature_in_table;
10485
10486 signature_in_table =
57d63ce2 10487 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10488 if (signature_in_table == signature)
10489 {
57d63ce2
DE
10490 uint32_t unit_index =
10491 read_4_bytes (dbfd,
10492 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10493
73869dc2
DE
10494 if (dwp_file->version == 1)
10495 {
10496 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10497 comp_dir, signature,
10498 is_debug_types);
10499 }
10500 else
10501 {
10502 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10503 comp_dir, signature,
10504 is_debug_types);
10505 }
9a3c8263 10506 return (struct dwo_unit *) *slot;
80626a55
DE
10507 }
10508 if (signature_in_table == 0)
10509 return NULL;
10510 hash = (hash + hash2) & mask;
10511 }
10512
10513 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10514 " [in module %s]"),
10515 dwp_file->name);
10516}
10517
ab5088bf 10518/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10519 Open the file specified by FILE_NAME and hand it off to BFD for
10520 preliminary analysis. Return a newly initialized bfd *, which
10521 includes a canonicalized copy of FILE_NAME.
80626a55 10522 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10523 SEARCH_CWD is true if the current directory is to be searched.
10524 It will be searched before debug-file-directory.
13aaf454
DE
10525 If successful, the file is added to the bfd include table of the
10526 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10527 If unable to find/open the file, return NULL.
3019eac3
DE
10528 NOTE: This function is derived from symfile_bfd_open. */
10529
192b62ce 10530static gdb_bfd_ref_ptr
6ac97d4c 10531try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10532{
80626a55 10533 int desc, flags;
3019eac3 10534 char *absolute_name;
9c02c129
DE
10535 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10536 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10537 to debug_file_directory. */
10538 char *search_path;
10539 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10540
6ac97d4c
DE
10541 if (search_cwd)
10542 {
10543 if (*debug_file_directory != '\0')
10544 search_path = concat (".", dirname_separator_string,
b36cec19 10545 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10546 else
10547 search_path = xstrdup (".");
10548 }
9c02c129 10549 else
6ac97d4c 10550 search_path = xstrdup (debug_file_directory);
3019eac3 10551
492c0ab7 10552 flags = OPF_RETURN_REALPATH;
80626a55
DE
10553 if (is_dwp)
10554 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10555 desc = openp (search_path, flags, file_name,
3019eac3 10556 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10557 xfree (search_path);
3019eac3
DE
10558 if (desc < 0)
10559 return NULL;
10560
192b62ce 10561 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10562 xfree (absolute_name);
9c02c129
DE
10563 if (sym_bfd == NULL)
10564 return NULL;
192b62ce 10565 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10566
192b62ce
TT
10567 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10568 return NULL;
3019eac3 10569
13aaf454
DE
10570 /* Success. Record the bfd as having been included by the objfile's bfd.
10571 This is important because things like demangled_names_hash lives in the
10572 objfile's per_bfd space and may have references to things like symbol
10573 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10574 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10575
3019eac3
DE
10576 return sym_bfd;
10577}
10578
ab5088bf 10579/* Try to open DWO file FILE_NAME.
3019eac3
DE
10580 COMP_DIR is the DW_AT_comp_dir attribute.
10581 The result is the bfd handle of the file.
10582 If there is a problem finding or opening the file, return NULL.
10583 Upon success, the canonicalized path of the file is stored in the bfd,
10584 same as symfile_bfd_open. */
10585
192b62ce 10586static gdb_bfd_ref_ptr
ab5088bf 10587open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10588{
80626a55 10589 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10590 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10591
10592 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10593
10594 if (comp_dir != NULL)
10595 {
b36cec19
PA
10596 char *path_to_try = concat (comp_dir, SLASH_STRING,
10597 file_name, (char *) NULL);
3019eac3
DE
10598
10599 /* NOTE: If comp_dir is a relative path, this will also try the
10600 search path, which seems useful. */
192b62ce
TT
10601 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10602 1 /*search_cwd*/));
3019eac3
DE
10603 xfree (path_to_try);
10604 if (abfd != NULL)
10605 return abfd;
10606 }
10607
10608 /* That didn't work, try debug-file-directory, which, despite its name,
10609 is a list of paths. */
10610
10611 if (*debug_file_directory == '\0')
10612 return NULL;
10613
6ac97d4c 10614 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10615}
10616
80626a55
DE
10617/* This function is mapped across the sections and remembers the offset and
10618 size of each of the DWO debugging sections we are interested in. */
10619
10620static void
10621dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10622{
9a3c8263 10623 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10624 const struct dwop_section_names *names = &dwop_section_names;
10625
10626 if (section_is_p (sectp->name, &names->abbrev_dwo))
10627 {
049412e3 10628 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10629 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10630 }
10631 else if (section_is_p (sectp->name, &names->info_dwo))
10632 {
049412e3 10633 dwo_sections->info.s.section = sectp;
80626a55
DE
10634 dwo_sections->info.size = bfd_get_section_size (sectp);
10635 }
10636 else if (section_is_p (sectp->name, &names->line_dwo))
10637 {
049412e3 10638 dwo_sections->line.s.section = sectp;
80626a55
DE
10639 dwo_sections->line.size = bfd_get_section_size (sectp);
10640 }
10641 else if (section_is_p (sectp->name, &names->loc_dwo))
10642 {
049412e3 10643 dwo_sections->loc.s.section = sectp;
80626a55
DE
10644 dwo_sections->loc.size = bfd_get_section_size (sectp);
10645 }
10646 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10647 {
049412e3 10648 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10649 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10650 }
10651 else if (section_is_p (sectp->name, &names->macro_dwo))
10652 {
049412e3 10653 dwo_sections->macro.s.section = sectp;
80626a55
DE
10654 dwo_sections->macro.size = bfd_get_section_size (sectp);
10655 }
10656 else if (section_is_p (sectp->name, &names->str_dwo))
10657 {
049412e3 10658 dwo_sections->str.s.section = sectp;
80626a55
DE
10659 dwo_sections->str.size = bfd_get_section_size (sectp);
10660 }
10661 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10662 {
049412e3 10663 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10664 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10665 }
10666 else if (section_is_p (sectp->name, &names->types_dwo))
10667 {
10668 struct dwarf2_section_info type_section;
10669
10670 memset (&type_section, 0, sizeof (type_section));
049412e3 10671 type_section.s.section = sectp;
80626a55
DE
10672 type_section.size = bfd_get_section_size (sectp);
10673 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10674 &type_section);
10675 }
10676}
10677
ab5088bf 10678/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10679 by PER_CU. This is for the non-DWP case.
80626a55 10680 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10681
10682static struct dwo_file *
0ac5b59e
DE
10683open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10684 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10685{
10686 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10687 struct dwo_file *dwo_file;
3019eac3
DE
10688 struct cleanup *cleanups;
10689
192b62ce 10690 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10691 if (dbfd == NULL)
10692 {
b4f54984 10693 if (dwarf_read_debug)
80626a55
DE
10694 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10695 return NULL;
10696 }
10697 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10698 dwo_file->dwo_name = dwo_name;
10699 dwo_file->comp_dir = comp_dir;
192b62ce 10700 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10701
10702 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10703
192b62ce
TT
10704 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10705 &dwo_file->sections);
3019eac3 10706
19c3d4c9 10707 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3 10708
78d4d2c5
JK
10709 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10710 dwo_file->tus);
3019eac3
DE
10711
10712 discard_cleanups (cleanups);
10713
b4f54984 10714 if (dwarf_read_debug)
80626a55
DE
10715 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10716
3019eac3
DE
10717 return dwo_file;
10718}
10719
80626a55 10720/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10721 size of each of the DWP debugging sections common to version 1 and 2 that
10722 we are interested in. */
3019eac3 10723
80626a55 10724static void
73869dc2
DE
10725dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10726 void *dwp_file_ptr)
3019eac3 10727{
9a3c8263 10728 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10729 const struct dwop_section_names *names = &dwop_section_names;
10730 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10731
80626a55 10732 /* Record the ELF section number for later lookup: this is what the
73869dc2 10733 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10734 gdb_assert (elf_section_nr < dwp_file->num_sections);
10735 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10736
80626a55
DE
10737 /* Look for specific sections that we need. */
10738 if (section_is_p (sectp->name, &names->str_dwo))
10739 {
049412e3 10740 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10741 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10742 }
10743 else if (section_is_p (sectp->name, &names->cu_index))
10744 {
049412e3 10745 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10746 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10747 }
10748 else if (section_is_p (sectp->name, &names->tu_index))
10749 {
049412e3 10750 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10751 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10752 }
10753}
3019eac3 10754
73869dc2
DE
10755/* This function is mapped across the sections and remembers the offset and
10756 size of each of the DWP version 2 debugging sections that we are interested
10757 in. This is split into a separate function because we don't know if we
10758 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10759
10760static void
10761dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10762{
9a3c8263 10763 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10764 const struct dwop_section_names *names = &dwop_section_names;
10765 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10766
10767 /* Record the ELF section number for later lookup: this is what the
10768 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10769 gdb_assert (elf_section_nr < dwp_file->num_sections);
10770 dwp_file->elf_sections[elf_section_nr] = sectp;
10771
10772 /* Look for specific sections that we need. */
10773 if (section_is_p (sectp->name, &names->abbrev_dwo))
10774 {
049412e3 10775 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10776 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10777 }
10778 else if (section_is_p (sectp->name, &names->info_dwo))
10779 {
049412e3 10780 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10781 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10782 }
10783 else if (section_is_p (sectp->name, &names->line_dwo))
10784 {
049412e3 10785 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10786 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10787 }
10788 else if (section_is_p (sectp->name, &names->loc_dwo))
10789 {
049412e3 10790 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10791 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10792 }
10793 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10794 {
049412e3 10795 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10796 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10797 }
10798 else if (section_is_p (sectp->name, &names->macro_dwo))
10799 {
049412e3 10800 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
10801 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10802 }
10803 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10804 {
049412e3 10805 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
10806 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10807 }
10808 else if (section_is_p (sectp->name, &names->types_dwo))
10809 {
049412e3 10810 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10811 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10812 }
10813}
10814
80626a55 10815/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10816
80626a55
DE
10817static hashval_t
10818hash_dwp_loaded_cutus (const void *item)
10819{
9a3c8263 10820 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10821
80626a55
DE
10822 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10823 return dwo_unit->signature;
3019eac3
DE
10824}
10825
80626a55 10826/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10827
80626a55
DE
10828static int
10829eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10830{
9a3c8263
SM
10831 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10832 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10833
80626a55
DE
10834 return dua->signature == dub->signature;
10835}
3019eac3 10836
80626a55 10837/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10838
80626a55
DE
10839static htab_t
10840allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10841{
10842 return htab_create_alloc_ex (3,
10843 hash_dwp_loaded_cutus,
10844 eq_dwp_loaded_cutus,
10845 NULL,
10846 &objfile->objfile_obstack,
10847 hashtab_obstack_allocate,
10848 dummy_obstack_deallocate);
10849}
3019eac3 10850
ab5088bf
DE
10851/* Try to open DWP file FILE_NAME.
10852 The result is the bfd handle of the file.
10853 If there is a problem finding or opening the file, return NULL.
10854 Upon success, the canonicalized path of the file is stored in the bfd,
10855 same as symfile_bfd_open. */
10856
192b62ce 10857static gdb_bfd_ref_ptr
ab5088bf
DE
10858open_dwp_file (const char *file_name)
10859{
192b62ce
TT
10860 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10861 1 /*search_cwd*/));
6ac97d4c
DE
10862 if (abfd != NULL)
10863 return abfd;
10864
10865 /* Work around upstream bug 15652.
10866 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10867 [Whether that's a "bug" is debatable, but it is getting in our way.]
10868 We have no real idea where the dwp file is, because gdb's realpath-ing
10869 of the executable's path may have discarded the needed info.
10870 [IWBN if the dwp file name was recorded in the executable, akin to
10871 .gnu_debuglink, but that doesn't exist yet.]
10872 Strip the directory from FILE_NAME and search again. */
10873 if (*debug_file_directory != '\0')
10874 {
10875 /* Don't implicitly search the current directory here.
10876 If the user wants to search "." to handle this case,
10877 it must be added to debug-file-directory. */
10878 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10879 0 /*search_cwd*/);
10880 }
10881
10882 return NULL;
ab5088bf
DE
10883}
10884
80626a55
DE
10885/* Initialize the use of the DWP file for the current objfile.
10886 By convention the name of the DWP file is ${objfile}.dwp.
10887 The result is NULL if it can't be found. */
a766d390 10888
80626a55 10889static struct dwp_file *
ab5088bf 10890open_and_init_dwp_file (void)
80626a55
DE
10891{
10892 struct objfile *objfile = dwarf2_per_objfile->objfile;
10893 struct dwp_file *dwp_file;
10894 char *dwp_name;
6c447423 10895 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
80626a55 10896
82bf32bc
JK
10897 /* Try to find first .dwp for the binary file before any symbolic links
10898 resolving. */
6c447423
DE
10899
10900 /* If the objfile is a debug file, find the name of the real binary
10901 file and get the name of dwp file from there. */
10902 if (objfile->separate_debug_objfile_backlink != NULL)
10903 {
10904 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10905 const char *backlink_basename = lbasename (backlink->original_name);
10906 char *debug_dirname = ldirname (objfile->original_name);
10907
10908 make_cleanup (xfree, debug_dirname);
10909 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10910 SLASH_STRING, backlink_basename);
10911 }
10912 else
10913 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10914 make_cleanup (xfree, dwp_name);
80626a55 10915
192b62ce 10916 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
82bf32bc
JK
10917 if (dbfd == NULL
10918 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10919 {
10920 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10921 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10922 make_cleanup (xfree, dwp_name);
10923 dbfd = open_dwp_file (dwp_name);
10924 }
10925
80626a55
DE
10926 if (dbfd == NULL)
10927 {
b4f54984 10928 if (dwarf_read_debug)
80626a55
DE
10929 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10930 do_cleanups (cleanups);
10931 return NULL;
3019eac3 10932 }
80626a55 10933 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
10934 dwp_file->name = bfd_get_filename (dbfd.get ());
10935 dwp_file->dbfd = dbfd.release ();
80626a55 10936 do_cleanups (cleanups);
c906108c 10937
80626a55 10938 /* +1: section 0 is unused */
192b62ce 10939 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
10940 dwp_file->elf_sections =
10941 OBSTACK_CALLOC (&objfile->objfile_obstack,
10942 dwp_file->num_sections, asection *);
10943
192b62ce
TT
10944 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10945 dwp_file);
80626a55
DE
10946
10947 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10948
10949 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10950
73869dc2
DE
10951 /* The DWP file version is stored in the hash table. Oh well. */
10952 if (dwp_file->cus->version != dwp_file->tus->version)
10953 {
10954 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 10955 pretty bizarre. We use pulongest here because that's the established
4d65956b 10956 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
10957 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10958 " TU version %s [in DWP file %s]"),
10959 pulongest (dwp_file->cus->version),
10960 pulongest (dwp_file->tus->version), dwp_name);
73869dc2
DE
10961 }
10962 dwp_file->version = dwp_file->cus->version;
10963
10964 if (dwp_file->version == 2)
192b62ce
TT
10965 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10966 dwp_file);
73869dc2 10967
19ac8c2e
DE
10968 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10969 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 10970
b4f54984 10971 if (dwarf_read_debug)
80626a55
DE
10972 {
10973 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10974 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
10975 " %s CUs, %s TUs\n",
10976 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10977 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
10978 }
10979
10980 return dwp_file;
3019eac3 10981}
c906108c 10982
ab5088bf
DE
10983/* Wrapper around open_and_init_dwp_file, only open it once. */
10984
10985static struct dwp_file *
10986get_dwp_file (void)
10987{
10988 if (! dwarf2_per_objfile->dwp_checked)
10989 {
10990 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10991 dwarf2_per_objfile->dwp_checked = 1;
10992 }
10993 return dwarf2_per_objfile->dwp_file;
10994}
10995
80626a55
DE
10996/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10997 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10998 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 10999 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11000 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11001
11002 This is called, for example, when wanting to read a variable with a
11003 complex location. Therefore we don't want to do file i/o for every call.
11004 Therefore we don't want to look for a DWO file on every call.
11005 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11006 then we check if we've already seen DWO_NAME, and only THEN do we check
11007 for a DWO file.
11008
1c658ad5 11009 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11010 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11011
3019eac3 11012static struct dwo_unit *
80626a55
DE
11013lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11014 const char *dwo_name, const char *comp_dir,
11015 ULONGEST signature, int is_debug_types)
3019eac3
DE
11016{
11017 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11018 const char *kind = is_debug_types ? "TU" : "CU";
11019 void **dwo_file_slot;
3019eac3 11020 struct dwo_file *dwo_file;
80626a55 11021 struct dwp_file *dwp_file;
cb1df416 11022
6a506a2d
DE
11023 /* First see if there's a DWP file.
11024 If we have a DWP file but didn't find the DWO inside it, don't
11025 look for the original DWO file. It makes gdb behave differently
11026 depending on whether one is debugging in the build tree. */
cf2c3c16 11027
ab5088bf 11028 dwp_file = get_dwp_file ();
80626a55 11029 if (dwp_file != NULL)
cf2c3c16 11030 {
80626a55
DE
11031 const struct dwp_hash_table *dwp_htab =
11032 is_debug_types ? dwp_file->tus : dwp_file->cus;
11033
11034 if (dwp_htab != NULL)
11035 {
11036 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11037 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11038 signature, is_debug_types);
80626a55
DE
11039
11040 if (dwo_cutu != NULL)
11041 {
b4f54984 11042 if (dwarf_read_debug)
80626a55
DE
11043 {
11044 fprintf_unfiltered (gdb_stdlog,
11045 "Virtual DWO %s %s found: @%s\n",
11046 kind, hex_string (signature),
11047 host_address_to_string (dwo_cutu));
11048 }
11049 return dwo_cutu;
11050 }
11051 }
11052 }
6a506a2d 11053 else
80626a55 11054 {
6a506a2d 11055 /* No DWP file, look for the DWO file. */
80626a55 11056
6a506a2d
DE
11057 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11058 if (*dwo_file_slot == NULL)
80626a55 11059 {
6a506a2d
DE
11060 /* Read in the file and build a table of the CUs/TUs it contains. */
11061 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11062 }
6a506a2d 11063 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11064 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11065
6a506a2d 11066 if (dwo_file != NULL)
19c3d4c9 11067 {
6a506a2d
DE
11068 struct dwo_unit *dwo_cutu = NULL;
11069
11070 if (is_debug_types && dwo_file->tus)
11071 {
11072 struct dwo_unit find_dwo_cutu;
11073
11074 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11075 find_dwo_cutu.signature = signature;
9a3c8263
SM
11076 dwo_cutu
11077 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
11078 }
11079 else if (!is_debug_types && dwo_file->cu)
80626a55 11080 {
6a506a2d
DE
11081 if (signature == dwo_file->cu->signature)
11082 dwo_cutu = dwo_file->cu;
11083 }
11084
11085 if (dwo_cutu != NULL)
11086 {
b4f54984 11087 if (dwarf_read_debug)
6a506a2d
DE
11088 {
11089 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11090 kind, dwo_name, hex_string (signature),
11091 host_address_to_string (dwo_cutu));
11092 }
11093 return dwo_cutu;
80626a55
DE
11094 }
11095 }
2e276125 11096 }
9cdd5dbd 11097
80626a55
DE
11098 /* We didn't find it. This could mean a dwo_id mismatch, or
11099 someone deleted the DWO/DWP file, or the search path isn't set up
11100 correctly to find the file. */
11101
b4f54984 11102 if (dwarf_read_debug)
80626a55
DE
11103 {
11104 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11105 kind, dwo_name, hex_string (signature));
11106 }
3019eac3 11107
6656a72d
DE
11108 /* This is a warning and not a complaint because it can be caused by
11109 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11110 {
11111 /* Print the name of the DWP file if we looked there, helps the user
11112 better diagnose the problem. */
11113 char *dwp_text = NULL;
11114 struct cleanup *cleanups;
11115
11116 if (dwp_file != NULL)
11117 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11118 cleanups = make_cleanup (xfree, dwp_text);
11119
11120 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11121 " [in module %s]"),
11122 kind, dwo_name, hex_string (signature),
11123 dwp_text != NULL ? dwp_text : "",
11124 this_unit->is_debug_types ? "TU" : "CU",
11125 this_unit->offset.sect_off, objfile_name (objfile));
11126
11127 do_cleanups (cleanups);
11128 }
3019eac3 11129 return NULL;
5fb290d7
DJ
11130}
11131
80626a55
DE
11132/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11133 See lookup_dwo_cutu_unit for details. */
11134
11135static struct dwo_unit *
11136lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11137 const char *dwo_name, const char *comp_dir,
11138 ULONGEST signature)
11139{
11140 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11141}
11142
11143/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11144 See lookup_dwo_cutu_unit for details. */
11145
11146static struct dwo_unit *
11147lookup_dwo_type_unit (struct signatured_type *this_tu,
11148 const char *dwo_name, const char *comp_dir)
11149{
11150 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11151}
11152
89e63ee4
DE
11153/* Traversal function for queue_and_load_all_dwo_tus. */
11154
11155static int
11156queue_and_load_dwo_tu (void **slot, void *info)
11157{
11158 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11159 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11160 ULONGEST signature = dwo_unit->signature;
11161 struct signatured_type *sig_type =
11162 lookup_dwo_signatured_type (per_cu->cu, signature);
11163
11164 if (sig_type != NULL)
11165 {
11166 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11167
11168 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11169 a real dependency of PER_CU on SIG_TYPE. That is detected later
11170 while processing PER_CU. */
11171 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11172 load_full_type_unit (sig_cu);
11173 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11174 }
11175
11176 return 1;
11177}
11178
11179/* Queue all TUs contained in the DWO of PER_CU to be read in.
11180 The DWO may have the only definition of the type, though it may not be
11181 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11182 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11183
11184static void
11185queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11186{
11187 struct dwo_unit *dwo_unit;
11188 struct dwo_file *dwo_file;
11189
11190 gdb_assert (!per_cu->is_debug_types);
11191 gdb_assert (get_dwp_file () == NULL);
11192 gdb_assert (per_cu->cu != NULL);
11193
11194 dwo_unit = per_cu->cu->dwo_unit;
11195 gdb_assert (dwo_unit != NULL);
11196
11197 dwo_file = dwo_unit->dwo_file;
11198 if (dwo_file->tus != NULL)
11199 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11200}
11201
3019eac3
DE
11202/* Free all resources associated with DWO_FILE.
11203 Close the DWO file and munmap the sections.
11204 All memory should be on the objfile obstack. */
348e048f
DE
11205
11206static void
3019eac3 11207free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11208{
348e048f 11209
5c6fa7ab 11210 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11211 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11212
3019eac3
DE
11213 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11214}
348e048f 11215
3019eac3 11216/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11217
3019eac3
DE
11218static void
11219free_dwo_file_cleanup (void *arg)
11220{
11221 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11222 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11223
3019eac3
DE
11224 free_dwo_file (dwo_file, objfile);
11225}
348e048f 11226
3019eac3 11227/* Traversal function for free_dwo_files. */
2ab95328 11228
3019eac3
DE
11229static int
11230free_dwo_file_from_slot (void **slot, void *info)
11231{
11232 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11233 struct objfile *objfile = (struct objfile *) info;
348e048f 11234
3019eac3 11235 free_dwo_file (dwo_file, objfile);
348e048f 11236
3019eac3
DE
11237 return 1;
11238}
348e048f 11239
3019eac3 11240/* Free all resources associated with DWO_FILES. */
348e048f 11241
3019eac3
DE
11242static void
11243free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11244{
11245 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11246}
3019eac3
DE
11247\f
11248/* Read in various DIEs. */
348e048f 11249
d389af10
JK
11250/* qsort helper for inherit_abstract_dies. */
11251
11252static int
11253unsigned_int_compar (const void *ap, const void *bp)
11254{
11255 unsigned int a = *(unsigned int *) ap;
11256 unsigned int b = *(unsigned int *) bp;
11257
11258 return (a > b) - (b > a);
11259}
11260
11261/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11262 Inherit only the children of the DW_AT_abstract_origin DIE not being
11263 already referenced by DW_AT_abstract_origin from the children of the
11264 current DIE. */
d389af10
JK
11265
11266static void
11267inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11268{
11269 struct die_info *child_die;
11270 unsigned die_children_count;
11271 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11272 sect_offset *offsets;
11273 sect_offset *offsets_end, *offsetp;
d389af10
JK
11274 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11275 struct die_info *origin_die;
11276 /* Iterator of the ORIGIN_DIE children. */
11277 struct die_info *origin_child_die;
11278 struct cleanup *cleanups;
11279 struct attribute *attr;
cd02d79d
PA
11280 struct dwarf2_cu *origin_cu;
11281 struct pending **origin_previous_list_in_scope;
d389af10
JK
11282
11283 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11284 if (!attr)
11285 return;
11286
cd02d79d
PA
11287 /* Note that following die references may follow to a die in a
11288 different cu. */
11289
11290 origin_cu = cu;
11291 origin_die = follow_die_ref (die, attr, &origin_cu);
11292
11293 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11294 symbols in. */
11295 origin_previous_list_in_scope = origin_cu->list_in_scope;
11296 origin_cu->list_in_scope = cu->list_in_scope;
11297
edb3359d
DJ
11298 if (die->tag != origin_die->tag
11299 && !(die->tag == DW_TAG_inlined_subroutine
11300 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11301 complaint (&symfile_complaints,
11302 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 11303 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
11304
11305 child_die = die->child;
11306 die_children_count = 0;
11307 while (child_die && child_die->tag)
11308 {
11309 child_die = sibling_die (child_die);
11310 die_children_count++;
11311 }
8d749320 11312 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11313 cleanups = make_cleanup (xfree, offsets);
11314
11315 offsets_end = offsets;
3ea89b92
PMR
11316 for (child_die = die->child;
11317 child_die && child_die->tag;
11318 child_die = sibling_die (child_die))
11319 {
11320 struct die_info *child_origin_die;
11321 struct dwarf2_cu *child_origin_cu;
11322
11323 /* We are trying to process concrete instance entries:
216f72a1 11324 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11325 it's not relevant to our analysis here. i.e. detecting DIEs that are
11326 present in the abstract instance but not referenced in the concrete
11327 one. */
216f72a1
JK
11328 if (child_die->tag == DW_TAG_call_site
11329 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11330 continue;
11331
c38f313d
DJ
11332 /* For each CHILD_DIE, find the corresponding child of
11333 ORIGIN_DIE. If there is more than one layer of
11334 DW_AT_abstract_origin, follow them all; there shouldn't be,
11335 but GCC versions at least through 4.4 generate this (GCC PR
11336 40573). */
3ea89b92
PMR
11337 child_origin_die = child_die;
11338 child_origin_cu = cu;
c38f313d
DJ
11339 while (1)
11340 {
cd02d79d
PA
11341 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11342 child_origin_cu);
c38f313d
DJ
11343 if (attr == NULL)
11344 break;
cd02d79d
PA
11345 child_origin_die = follow_die_ref (child_origin_die, attr,
11346 &child_origin_cu);
c38f313d
DJ
11347 }
11348
d389af10
JK
11349 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11350 counterpart may exist. */
c38f313d 11351 if (child_origin_die != child_die)
d389af10 11352 {
edb3359d
DJ
11353 if (child_die->tag != child_origin_die->tag
11354 && !(child_die->tag == DW_TAG_inlined_subroutine
11355 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11356 complaint (&symfile_complaints,
11357 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11358 "different tags"), child_die->offset.sect_off,
11359 child_origin_die->offset.sect_off);
c38f313d
DJ
11360 if (child_origin_die->parent != origin_die)
11361 complaint (&symfile_complaints,
11362 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11363 "different parents"), child_die->offset.sect_off,
11364 child_origin_die->offset.sect_off);
c38f313d
DJ
11365 else
11366 *offsets_end++ = child_origin_die->offset;
d389af10 11367 }
d389af10
JK
11368 }
11369 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11370 unsigned_int_compar);
11371 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 11372 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
11373 complaint (&symfile_complaints,
11374 _("Multiple children of DIE 0x%x refer "
11375 "to DIE 0x%x as their abstract origin"),
b64f50a1 11376 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
11377
11378 offsetp = offsets;
11379 origin_child_die = origin_die->child;
11380 while (origin_child_die && origin_child_die->tag)
11381 {
11382 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
11383 while (offsetp < offsets_end
11384 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 11385 offsetp++;
b64f50a1
JK
11386 if (offsetp >= offsets_end
11387 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10 11388 {
adde2bff
DE
11389 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11390 Check whether we're already processing ORIGIN_CHILD_DIE.
11391 This can happen with mutually referenced abstract_origins.
11392 PR 16581. */
11393 if (!origin_child_die->in_process)
11394 process_die (origin_child_die, origin_cu);
d389af10
JK
11395 }
11396 origin_child_die = sibling_die (origin_child_die);
11397 }
cd02d79d 11398 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11399
11400 do_cleanups (cleanups);
11401}
11402
c906108c 11403static void
e7c27a73 11404read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11405{
e7c27a73 11406 struct objfile *objfile = cu->objfile;
3e29f34a 11407 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11408 struct context_stack *newobj;
c906108c
SS
11409 CORE_ADDR lowpc;
11410 CORE_ADDR highpc;
11411 struct die_info *child_die;
edb3359d 11412 struct attribute *attr, *call_line, *call_file;
15d034d0 11413 const char *name;
e142c38c 11414 CORE_ADDR baseaddr;
801e3a5b 11415 struct block *block;
edb3359d 11416 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11417 VEC (symbolp) *template_args = NULL;
11418 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11419
11420 if (inlined_func)
11421 {
11422 /* If we do not have call site information, we can't show the
11423 caller of this inlined function. That's too confusing, so
11424 only use the scope for local variables. */
11425 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11426 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11427 if (call_line == NULL || call_file == NULL)
11428 {
11429 read_lexical_block_scope (die, cu);
11430 return;
11431 }
11432 }
c906108c 11433
e142c38c
DJ
11434 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11435
94af9270 11436 name = dwarf2_name (die, cu);
c906108c 11437
e8d05480
JB
11438 /* Ignore functions with missing or empty names. These are actually
11439 illegal according to the DWARF standard. */
11440 if (name == NULL)
11441 {
11442 complaint (&symfile_complaints,
b64f50a1
JK
11443 _("missing name for subprogram DIE at %d"),
11444 die->offset.sect_off);
e8d05480
JB
11445 return;
11446 }
11447
11448 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11449 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11450 <= PC_BOUNDS_INVALID)
e8d05480 11451 {
ae4d0c03
PM
11452 attr = dwarf2_attr (die, DW_AT_external, cu);
11453 if (!attr || !DW_UNSND (attr))
11454 complaint (&symfile_complaints,
3e43a32a
MS
11455 _("cannot get low and high bounds "
11456 "for subprogram DIE at %d"),
b64f50a1 11457 die->offset.sect_off);
e8d05480
JB
11458 return;
11459 }
c906108c 11460
3e29f34a
MR
11461 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11462 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11463
34eaf542
TT
11464 /* If we have any template arguments, then we must allocate a
11465 different sort of symbol. */
11466 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11467 {
11468 if (child_die->tag == DW_TAG_template_type_param
11469 || child_die->tag == DW_TAG_template_value_param)
11470 {
e623cf5d 11471 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11472 templ_func->base.is_cplus_template_function = 1;
11473 break;
11474 }
11475 }
11476
fe978cb0
PA
11477 newobj = push_context (0, lowpc);
11478 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11479 (struct symbol *) templ_func);
4c2df51b 11480
4cecd739
DJ
11481 /* If there is a location expression for DW_AT_frame_base, record
11482 it. */
e142c38c 11483 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11484 if (attr)
fe978cb0 11485 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11486
63e43d3a
PMR
11487 /* If there is a location for the static link, record it. */
11488 newobj->static_link = NULL;
11489 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11490 if (attr)
11491 {
224c3ddb
SM
11492 newobj->static_link
11493 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11494 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11495 }
11496
e142c38c 11497 cu->list_in_scope = &local_symbols;
c906108c 11498
639d11d3 11499 if (die->child != NULL)
c906108c 11500 {
639d11d3 11501 child_die = die->child;
c906108c
SS
11502 while (child_die && child_die->tag)
11503 {
34eaf542
TT
11504 if (child_die->tag == DW_TAG_template_type_param
11505 || child_die->tag == DW_TAG_template_value_param)
11506 {
11507 struct symbol *arg = new_symbol (child_die, NULL, cu);
11508
f1078f66
DJ
11509 if (arg != NULL)
11510 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11511 }
11512 else
11513 process_die (child_die, cu);
c906108c
SS
11514 child_die = sibling_die (child_die);
11515 }
11516 }
11517
d389af10
JK
11518 inherit_abstract_dies (die, cu);
11519
4a811a97
UW
11520 /* If we have a DW_AT_specification, we might need to import using
11521 directives from the context of the specification DIE. See the
11522 comment in determine_prefix. */
11523 if (cu->language == language_cplus
11524 && dwarf2_attr (die, DW_AT_specification, cu))
11525 {
11526 struct dwarf2_cu *spec_cu = cu;
11527 struct die_info *spec_die = die_specification (die, &spec_cu);
11528
11529 while (spec_die)
11530 {
11531 child_die = spec_die->child;
11532 while (child_die && child_die->tag)
11533 {
11534 if (child_die->tag == DW_TAG_imported_module)
11535 process_die (child_die, spec_cu);
11536 child_die = sibling_die (child_die);
11537 }
11538
11539 /* In some cases, GCC generates specification DIEs that
11540 themselves contain DW_AT_specification attributes. */
11541 spec_die = die_specification (spec_die, &spec_cu);
11542 }
11543 }
11544
fe978cb0 11545 newobj = pop_context ();
c906108c 11546 /* Make a block for the local symbols within. */
fe978cb0 11547 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11548 newobj->static_link, lowpc, highpc);
801e3a5b 11549
df8a16a1 11550 /* For C++, set the block's scope. */
45280282
IB
11551 if ((cu->language == language_cplus
11552 || cu->language == language_fortran
c44af4eb
TT
11553 || cu->language == language_d
11554 || cu->language == language_rust)
4d4ec4e5 11555 && cu->processing_has_namespace_info)
195a3f6c
TT
11556 block_set_scope (block, determine_prefix (die, cu),
11557 &objfile->objfile_obstack);
df8a16a1 11558
801e3a5b
JB
11559 /* If we have address ranges, record them. */
11560 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11561
fe978cb0 11562 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11563
34eaf542
TT
11564 /* Attach template arguments to function. */
11565 if (! VEC_empty (symbolp, template_args))
11566 {
11567 gdb_assert (templ_func != NULL);
11568
11569 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11570 templ_func->template_arguments
8d749320
SM
11571 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11572 templ_func->n_template_arguments);
34eaf542
TT
11573 memcpy (templ_func->template_arguments,
11574 VEC_address (symbolp, template_args),
11575 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11576 VEC_free (symbolp, template_args);
11577 }
11578
208d8187
JB
11579 /* In C++, we can have functions nested inside functions (e.g., when
11580 a function declares a class that has methods). This means that
11581 when we finish processing a function scope, we may need to go
11582 back to building a containing block's symbol lists. */
fe978cb0 11583 local_symbols = newobj->locals;
22cee43f 11584 local_using_directives = newobj->local_using_directives;
208d8187 11585
921e78cf
JB
11586 /* If we've finished processing a top-level function, subsequent
11587 symbols go in the file symbol list. */
11588 if (outermost_context_p ())
e142c38c 11589 cu->list_in_scope = &file_symbols;
c906108c
SS
11590}
11591
11592/* Process all the DIES contained within a lexical block scope. Start
11593 a new scope, process the dies, and then close the scope. */
11594
11595static void
e7c27a73 11596read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11597{
e7c27a73 11598 struct objfile *objfile = cu->objfile;
3e29f34a 11599 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11600 struct context_stack *newobj;
c906108c
SS
11601 CORE_ADDR lowpc, highpc;
11602 struct die_info *child_die;
e142c38c
DJ
11603 CORE_ADDR baseaddr;
11604
11605 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11606
11607 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11608 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11609 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11610 be nasty. Might be easier to properly extend generic blocks to
af34e669 11611 describe ranges. */
e385593e
JK
11612 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11613 {
11614 case PC_BOUNDS_NOT_PRESENT:
11615 /* DW_TAG_lexical_block has no attributes, process its children as if
11616 there was no wrapping by that DW_TAG_lexical_block.
11617 GCC does no longer produces such DWARF since GCC r224161. */
11618 for (child_die = die->child;
11619 child_die != NULL && child_die->tag;
11620 child_die = sibling_die (child_die))
11621 process_die (child_die, cu);
11622 return;
11623 case PC_BOUNDS_INVALID:
11624 return;
11625 }
3e29f34a
MR
11626 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11627 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11628
11629 push_context (0, lowpc);
639d11d3 11630 if (die->child != NULL)
c906108c 11631 {
639d11d3 11632 child_die = die->child;
c906108c
SS
11633 while (child_die && child_die->tag)
11634 {
e7c27a73 11635 process_die (child_die, cu);
c906108c
SS
11636 child_die = sibling_die (child_die);
11637 }
11638 }
3ea89b92 11639 inherit_abstract_dies (die, cu);
fe978cb0 11640 newobj = pop_context ();
c906108c 11641
22cee43f 11642 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11643 {
801e3a5b 11644 struct block *block
63e43d3a 11645 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11646 newobj->start_addr, highpc);
801e3a5b
JB
11647
11648 /* Note that recording ranges after traversing children, as we
11649 do here, means that recording a parent's ranges entails
11650 walking across all its children's ranges as they appear in
11651 the address map, which is quadratic behavior.
11652
11653 It would be nicer to record the parent's ranges before
11654 traversing its children, simply overriding whatever you find
11655 there. But since we don't even decide whether to create a
11656 block until after we've traversed its children, that's hard
11657 to do. */
11658 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11659 }
fe978cb0 11660 local_symbols = newobj->locals;
22cee43f 11661 local_using_directives = newobj->local_using_directives;
c906108c
SS
11662}
11663
216f72a1 11664/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11665
11666static void
11667read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11668{
11669 struct objfile *objfile = cu->objfile;
11670 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11671 CORE_ADDR pc, baseaddr;
11672 struct attribute *attr;
11673 struct call_site *call_site, call_site_local;
11674 void **slot;
11675 int nparams;
11676 struct die_info *child_die;
11677
11678 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11679
216f72a1
JK
11680 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11681 if (attr == NULL)
11682 {
11683 /* This was a pre-DWARF-5 GNU extension alias
11684 for DW_AT_call_return_pc. */
11685 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11686 }
96408a79
SA
11687 if (!attr)
11688 {
11689 complaint (&symfile_complaints,
216f72a1 11690 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11691 "DIE 0x%x [in module %s]"),
4262abfb 11692 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11693 return;
11694 }
31aa7e4e 11695 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11696 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11697
11698 if (cu->call_site_htab == NULL)
11699 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11700 NULL, &objfile->objfile_obstack,
11701 hashtab_obstack_allocate, NULL);
11702 call_site_local.pc = pc;
11703 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11704 if (*slot != NULL)
11705 {
11706 complaint (&symfile_complaints,
216f72a1 11707 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11708 "DIE 0x%x [in module %s]"),
4262abfb
JK
11709 paddress (gdbarch, pc), die->offset.sect_off,
11710 objfile_name (objfile));
96408a79
SA
11711 return;
11712 }
11713
11714 /* Count parameters at the caller. */
11715
11716 nparams = 0;
11717 for (child_die = die->child; child_die && child_die->tag;
11718 child_die = sibling_die (child_die))
11719 {
216f72a1
JK
11720 if (child_die->tag != DW_TAG_call_site_parameter
11721 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11722 {
11723 complaint (&symfile_complaints,
216f72a1
JK
11724 _("Tag %d is not DW_TAG_call_site_parameter in "
11725 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
11726 child_die->tag, child_die->offset.sect_off,
11727 objfile_name (objfile));
96408a79
SA
11728 continue;
11729 }
11730
11731 nparams++;
11732 }
11733
224c3ddb
SM
11734 call_site
11735 = ((struct call_site *)
11736 obstack_alloc (&objfile->objfile_obstack,
11737 sizeof (*call_site)
11738 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11739 *slot = call_site;
11740 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11741 call_site->pc = pc;
11742
216f72a1
JK
11743 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11744 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11745 {
11746 struct die_info *func_die;
11747
11748 /* Skip also over DW_TAG_inlined_subroutine. */
11749 for (func_die = die->parent;
11750 func_die && func_die->tag != DW_TAG_subprogram
11751 && func_die->tag != DW_TAG_subroutine_type;
11752 func_die = func_die->parent);
11753
216f72a1
JK
11754 /* DW_AT_call_all_calls is a superset
11755 of DW_AT_call_all_tail_calls. */
96408a79 11756 if (func_die
216f72a1 11757 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11758 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11759 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11760 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11761 {
11762 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11763 not complete. But keep CALL_SITE for look ups via call_site_htab,
11764 both the initial caller containing the real return address PC and
11765 the final callee containing the current PC of a chain of tail
11766 calls do not need to have the tail call list complete. But any
11767 function candidate for a virtual tail call frame searched via
11768 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11769 determined unambiguously. */
11770 }
11771 else
11772 {
11773 struct type *func_type = NULL;
11774
11775 if (func_die)
11776 func_type = get_die_type (func_die, cu);
11777 if (func_type != NULL)
11778 {
11779 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11780
11781 /* Enlist this call site to the function. */
11782 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11783 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11784 }
11785 else
11786 complaint (&symfile_complaints,
216f72a1 11787 _("Cannot find function owning DW_TAG_call_site "
96408a79 11788 "DIE 0x%x [in module %s]"),
4262abfb 11789 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11790 }
11791 }
11792
216f72a1
JK
11793 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11794 if (attr == NULL)
11795 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11796 if (attr == NULL)
11797 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 11798 if (attr == NULL)
216f72a1
JK
11799 {
11800 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11801 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11802 }
96408a79
SA
11803 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11804 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11805 /* Keep NULL DWARF_BLOCK. */;
11806 else if (attr_form_is_block (attr))
11807 {
11808 struct dwarf2_locexpr_baton *dlbaton;
11809
8d749320 11810 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
11811 dlbaton->data = DW_BLOCK (attr)->data;
11812 dlbaton->size = DW_BLOCK (attr)->size;
11813 dlbaton->per_cu = cu->per_cu;
11814
11815 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11816 }
7771576e 11817 else if (attr_form_is_ref (attr))
96408a79 11818 {
96408a79
SA
11819 struct dwarf2_cu *target_cu = cu;
11820 struct die_info *target_die;
11821
ac9ec31b 11822 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11823 gdb_assert (target_cu->objfile == objfile);
11824 if (die_is_declaration (target_die, target_cu))
11825 {
7d45c7c3 11826 const char *target_physname;
9112db09
JK
11827
11828 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11829 target_physname = dwarf2_string_attr (target_die,
11830 DW_AT_linkage_name,
11831 target_cu);
11832 if (target_physname == NULL)
11833 target_physname = dwarf2_string_attr (target_die,
11834 DW_AT_MIPS_linkage_name,
11835 target_cu);
11836 if (target_physname == NULL)
9112db09 11837 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11838 if (target_physname == NULL)
11839 complaint (&symfile_complaints,
216f72a1 11840 _("DW_AT_call_target target DIE has invalid "
96408a79 11841 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 11842 die->offset.sect_off, objfile_name (objfile));
96408a79 11843 else
7d455152 11844 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11845 }
11846 else
11847 {
11848 CORE_ADDR lowpc;
11849
11850 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11851 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11852 <= PC_BOUNDS_INVALID)
96408a79 11853 complaint (&symfile_complaints,
216f72a1 11854 _("DW_AT_call_target target DIE has invalid "
96408a79 11855 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 11856 die->offset.sect_off, objfile_name (objfile));
96408a79 11857 else
3e29f34a
MR
11858 {
11859 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11860 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11861 }
96408a79
SA
11862 }
11863 }
11864 else
11865 complaint (&symfile_complaints,
216f72a1 11866 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 11867 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 11868 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11869
11870 call_site->per_cu = cu->per_cu;
11871
11872 for (child_die = die->child;
11873 child_die && child_die->tag;
11874 child_die = sibling_die (child_die))
11875 {
96408a79 11876 struct call_site_parameter *parameter;
1788b2d3 11877 struct attribute *loc, *origin;
96408a79 11878
216f72a1
JK
11879 if (child_die->tag != DW_TAG_call_site_parameter
11880 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11881 {
11882 /* Already printed the complaint above. */
11883 continue;
11884 }
11885
11886 gdb_assert (call_site->parameter_count < nparams);
11887 parameter = &call_site->parameter[call_site->parameter_count];
11888
1788b2d3
JK
11889 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11890 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 11891 register is contained in DW_AT_call_value. */
96408a79 11892
24c5c679 11893 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
11894 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11895 if (origin == NULL)
11896 {
11897 /* This was a pre-DWARF-5 GNU extension alias
11898 for DW_AT_call_parameter. */
11899 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11900 }
7771576e 11901 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
11902 {
11903 sect_offset offset;
11904
11905 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11906 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
11907 if (!offset_in_cu_p (&cu->header, offset))
11908 {
11909 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11910 binding can be done only inside one CU. Such referenced DIE
11911 therefore cannot be even moved to DW_TAG_partial_unit. */
11912 complaint (&symfile_complaints,
216f72a1
JK
11913 _("DW_AT_call_parameter offset is not in CU for "
11914 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11915 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
11916 continue;
11917 }
1788b2d3
JK
11918 parameter->u.param_offset.cu_off = (offset.sect_off
11919 - cu->header.offset.sect_off);
11920 }
11921 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11922 {
11923 complaint (&symfile_complaints,
11924 _("No DW_FORM_block* DW_AT_location for "
216f72a1 11925 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11926 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11927 continue;
11928 }
24c5c679 11929 else
96408a79 11930 {
24c5c679
JK
11931 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11932 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11933 if (parameter->u.dwarf_reg != -1)
11934 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11935 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11936 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11937 &parameter->u.fb_offset))
11938 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11939 else
11940 {
11941 complaint (&symfile_complaints,
11942 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11943 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 11944 "DW_TAG_call_site child DIE 0x%x "
24c5c679 11945 "[in module %s]"),
4262abfb 11946 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
11947 continue;
11948 }
96408a79
SA
11949 }
11950
216f72a1
JK
11951 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
11952 if (attr == NULL)
11953 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
11954 if (!attr_form_is_block (attr))
11955 {
11956 complaint (&symfile_complaints,
216f72a1
JK
11957 _("No DW_FORM_block* DW_AT_call_value for "
11958 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11959 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11960 continue;
11961 }
11962 parameter->value = DW_BLOCK (attr)->data;
11963 parameter->value_size = DW_BLOCK (attr)->size;
11964
11965 /* Parameters are not pre-cleared by memset above. */
11966 parameter->data_value = NULL;
11967 parameter->data_value_size = 0;
11968 call_site->parameter_count++;
11969
216f72a1
JK
11970 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
11971 if (attr == NULL)
11972 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
11973 if (attr)
11974 {
11975 if (!attr_form_is_block (attr))
11976 complaint (&symfile_complaints,
216f72a1
JK
11977 _("No DW_FORM_block* DW_AT_call_data_value for "
11978 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11979 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11980 else
11981 {
11982 parameter->data_value = DW_BLOCK (attr)->data;
11983 parameter->data_value_size = DW_BLOCK (attr)->size;
11984 }
11985 }
11986 }
11987}
11988
43988095
JK
11989/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
11990 reading .debug_rnglists.
11991 Callback's type should be:
11992 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
11993 Return true if the attributes are present and valid, otherwise,
11994 return false. */
11995
11996template <typename Callback>
11997static bool
11998dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
11999 Callback &&callback)
12000{
12001 struct objfile *objfile = cu->objfile;
12002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12003 struct comp_unit_head *cu_header = &cu->header;
12004 bfd *obfd = objfile->obfd;
12005 unsigned int addr_size = cu_header->addr_size;
12006 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12007 /* Base address selection entry. */
12008 CORE_ADDR base;
12009 int found_base;
12010 unsigned int dummy;
12011 const gdb_byte *buffer;
12012 CORE_ADDR low = 0;
12013 CORE_ADDR high = 0;
12014 CORE_ADDR baseaddr;
12015 bool overflow = false;
12016
12017 found_base = cu->base_known;
12018 base = cu->base_address;
12019
12020 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12021 if (offset >= dwarf2_per_objfile->rnglists.size)
12022 {
12023 complaint (&symfile_complaints,
12024 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12025 offset);
12026 return false;
12027 }
12028 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12029
12030 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12031
12032 while (1)
12033 {
7814882a
JK
12034 /* Initialize it due to a false compiler warning. */
12035 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12036 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12037 + dwarf2_per_objfile->rnglists.size);
12038 unsigned int bytes_read;
12039
12040 if (buffer == buf_end)
12041 {
12042 overflow = true;
12043 break;
12044 }
12045 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12046 switch (rlet)
12047 {
12048 case DW_RLE_end_of_list:
12049 break;
12050 case DW_RLE_base_address:
12051 if (buffer + cu->header.addr_size > buf_end)
12052 {
12053 overflow = true;
12054 break;
12055 }
12056 base = read_address (obfd, buffer, cu, &bytes_read);
12057 found_base = 1;
12058 buffer += bytes_read;
12059 break;
12060 case DW_RLE_start_length:
12061 if (buffer + cu->header.addr_size > buf_end)
12062 {
12063 overflow = true;
12064 break;
12065 }
12066 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12067 buffer += bytes_read;
12068 range_end = (range_beginning
12069 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12070 buffer += bytes_read;
12071 if (buffer > buf_end)
12072 {
12073 overflow = true;
12074 break;
12075 }
12076 break;
12077 case DW_RLE_offset_pair:
12078 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12079 buffer += bytes_read;
12080 if (buffer > buf_end)
12081 {
12082 overflow = true;
12083 break;
12084 }
12085 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12086 buffer += bytes_read;
12087 if (buffer > buf_end)
12088 {
12089 overflow = true;
12090 break;
12091 }
12092 break;
12093 case DW_RLE_start_end:
12094 if (buffer + 2 * cu->header.addr_size > buf_end)
12095 {
12096 overflow = true;
12097 break;
12098 }
12099 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12100 buffer += bytes_read;
12101 range_end = read_address (obfd, buffer, cu, &bytes_read);
12102 buffer += bytes_read;
12103 break;
12104 default:
12105 complaint (&symfile_complaints,
12106 _("Invalid .debug_rnglists data (no base address)"));
12107 return false;
12108 }
12109 if (rlet == DW_RLE_end_of_list || overflow)
12110 break;
12111 if (rlet == DW_RLE_base_address)
12112 continue;
12113
12114 if (!found_base)
12115 {
12116 /* We have no valid base address for the ranges
12117 data. */
12118 complaint (&symfile_complaints,
12119 _("Invalid .debug_rnglists data (no base address)"));
12120 return false;
12121 }
12122
12123 if (range_beginning > range_end)
12124 {
12125 /* Inverted range entries are invalid. */
12126 complaint (&symfile_complaints,
12127 _("Invalid .debug_rnglists data (inverted range)"));
12128 return false;
12129 }
12130
12131 /* Empty range entries have no effect. */
12132 if (range_beginning == range_end)
12133 continue;
12134
12135 range_beginning += base;
12136 range_end += base;
12137
12138 /* A not-uncommon case of bad debug info.
12139 Don't pollute the addrmap with bad data. */
12140 if (range_beginning + baseaddr == 0
12141 && !dwarf2_per_objfile->has_section_at_zero)
12142 {
12143 complaint (&symfile_complaints,
12144 _(".debug_rnglists entry has start address of zero"
12145 " [in module %s]"), objfile_name (objfile));
12146 continue;
12147 }
12148
12149 callback (range_beginning, range_end);
12150 }
12151
12152 if (overflow)
12153 {
12154 complaint (&symfile_complaints,
12155 _("Offset %d is not terminated "
12156 "for DW_AT_ranges attribute"),
12157 offset);
12158 return false;
12159 }
12160
12161 return true;
12162}
12163
12164/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12165 Callback's type should be:
12166 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12167 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12168
43988095 12169template <typename Callback>
43039443 12170static int
5f46c5a5 12171dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12172 Callback &&callback)
43039443
JK
12173{
12174 struct objfile *objfile = cu->objfile;
3e29f34a 12175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12176 struct comp_unit_head *cu_header = &cu->header;
12177 bfd *obfd = objfile->obfd;
12178 unsigned int addr_size = cu_header->addr_size;
12179 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12180 /* Base address selection entry. */
12181 CORE_ADDR base;
12182 int found_base;
12183 unsigned int dummy;
d521ce57 12184 const gdb_byte *buffer;
ff013f42 12185 CORE_ADDR baseaddr;
43039443 12186
43988095
JK
12187 if (cu_header->version >= 5)
12188 return dwarf2_rnglists_process (offset, cu, callback);
12189
d00adf39
DE
12190 found_base = cu->base_known;
12191 base = cu->base_address;
43039443 12192
be391dca 12193 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12194 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12195 {
12196 complaint (&symfile_complaints,
12197 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12198 offset);
12199 return 0;
12200 }
dce234bc 12201 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12202
e7030f15 12203 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12204
43039443
JK
12205 while (1)
12206 {
12207 CORE_ADDR range_beginning, range_end;
12208
12209 range_beginning = read_address (obfd, buffer, cu, &dummy);
12210 buffer += addr_size;
12211 range_end = read_address (obfd, buffer, cu, &dummy);
12212 buffer += addr_size;
12213 offset += 2 * addr_size;
12214
12215 /* An end of list marker is a pair of zero addresses. */
12216 if (range_beginning == 0 && range_end == 0)
12217 /* Found the end of list entry. */
12218 break;
12219
12220 /* Each base address selection entry is a pair of 2 values.
12221 The first is the largest possible address, the second is
12222 the base address. Check for a base address here. */
12223 if ((range_beginning & mask) == mask)
12224 {
28d2bfb9
AB
12225 /* If we found the largest possible address, then we already
12226 have the base address in range_end. */
12227 base = range_end;
43039443
JK
12228 found_base = 1;
12229 continue;
12230 }
12231
12232 if (!found_base)
12233 {
12234 /* We have no valid base address for the ranges
12235 data. */
12236 complaint (&symfile_complaints,
12237 _("Invalid .debug_ranges data (no base address)"));
12238 return 0;
12239 }
12240
9277c30c
UW
12241 if (range_beginning > range_end)
12242 {
12243 /* Inverted range entries are invalid. */
12244 complaint (&symfile_complaints,
12245 _("Invalid .debug_ranges data (inverted range)"));
12246 return 0;
12247 }
12248
12249 /* Empty range entries have no effect. */
12250 if (range_beginning == range_end)
12251 continue;
12252
43039443
JK
12253 range_beginning += base;
12254 range_end += base;
12255
01093045
DE
12256 /* A not-uncommon case of bad debug info.
12257 Don't pollute the addrmap with bad data. */
12258 if (range_beginning + baseaddr == 0
12259 && !dwarf2_per_objfile->has_section_at_zero)
12260 {
12261 complaint (&symfile_complaints,
12262 _(".debug_ranges entry has start address of zero"
4262abfb 12263 " [in module %s]"), objfile_name (objfile));
01093045
DE
12264 continue;
12265 }
12266
5f46c5a5
JK
12267 callback (range_beginning, range_end);
12268 }
12269
12270 return 1;
12271}
12272
12273/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12274 Return 1 if the attributes are present and valid, otherwise, return 0.
12275 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12276
12277static int
12278dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12279 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12280 struct partial_symtab *ranges_pst)
12281{
12282 struct objfile *objfile = cu->objfile;
12283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12284 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12285 SECT_OFF_TEXT (objfile));
12286 int low_set = 0;
12287 CORE_ADDR low = 0;
12288 CORE_ADDR high = 0;
12289 int retval;
12290
12291 retval = dwarf2_ranges_process (offset, cu,
12292 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12293 {
9277c30c 12294 if (ranges_pst != NULL)
3e29f34a
MR
12295 {
12296 CORE_ADDR lowpc;
12297 CORE_ADDR highpc;
12298
12299 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12300 range_beginning + baseaddr);
12301 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12302 range_end + baseaddr);
12303 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12304 ranges_pst);
12305 }
ff013f42 12306
43039443
JK
12307 /* FIXME: This is recording everything as a low-high
12308 segment of consecutive addresses. We should have a
12309 data structure for discontiguous block ranges
12310 instead. */
12311 if (! low_set)
12312 {
12313 low = range_beginning;
12314 high = range_end;
12315 low_set = 1;
12316 }
12317 else
12318 {
12319 if (range_beginning < low)
12320 low = range_beginning;
12321 if (range_end > high)
12322 high = range_end;
12323 }
5f46c5a5
JK
12324 });
12325 if (!retval)
12326 return 0;
43039443
JK
12327
12328 if (! low_set)
12329 /* If the first entry is an end-of-list marker, the range
12330 describes an empty scope, i.e. no instructions. */
12331 return 0;
12332
12333 if (low_return)
12334 *low_return = low;
12335 if (high_return)
12336 *high_return = high;
12337 return 1;
12338}
12339
3a2b436a
JK
12340/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12341 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12342 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12343
3a2b436a 12344static enum pc_bounds_kind
af34e669 12345dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12346 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12347 struct partial_symtab *pst)
c906108c
SS
12348{
12349 struct attribute *attr;
91da1414 12350 struct attribute *attr_high;
af34e669
DJ
12351 CORE_ADDR low = 0;
12352 CORE_ADDR high = 0;
e385593e 12353 enum pc_bounds_kind ret;
c906108c 12354
91da1414
MW
12355 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12356 if (attr_high)
af34e669 12357 {
e142c38c 12358 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12359 if (attr)
91da1414 12360 {
31aa7e4e
JB
12361 low = attr_value_as_address (attr);
12362 high = attr_value_as_address (attr_high);
12363 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12364 high += low;
91da1414 12365 }
af34e669
DJ
12366 else
12367 /* Found high w/o low attribute. */
e385593e 12368 return PC_BOUNDS_INVALID;
af34e669
DJ
12369
12370 /* Found consecutive range of addresses. */
3a2b436a 12371 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12372 }
c906108c 12373 else
af34e669 12374 {
e142c38c 12375 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12376 if (attr != NULL)
12377 {
ab435259
DE
12378 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12379 We take advantage of the fact that DW_AT_ranges does not appear
12380 in DW_TAG_compile_unit of DWO files. */
12381 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12382 unsigned int ranges_offset = (DW_UNSND (attr)
12383 + (need_ranges_base
12384 ? cu->ranges_base
12385 : 0));
2e3cf129 12386
af34e669 12387 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12388 .debug_ranges section. */
2e3cf129 12389 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12390 return PC_BOUNDS_INVALID;
43039443 12391 /* Found discontinuous range of addresses. */
3a2b436a 12392 ret = PC_BOUNDS_RANGES;
af34e669 12393 }
e385593e
JK
12394 else
12395 return PC_BOUNDS_NOT_PRESENT;
af34e669 12396 }
c906108c 12397
9373cf26
JK
12398 /* read_partial_die has also the strict LOW < HIGH requirement. */
12399 if (high <= low)
e385593e 12400 return PC_BOUNDS_INVALID;
c906108c
SS
12401
12402 /* When using the GNU linker, .gnu.linkonce. sections are used to
12403 eliminate duplicate copies of functions and vtables and such.
12404 The linker will arbitrarily choose one and discard the others.
12405 The AT_*_pc values for such functions refer to local labels in
12406 these sections. If the section from that file was discarded, the
12407 labels are not in the output, so the relocs get a value of 0.
12408 If this is a discarded function, mark the pc bounds as invalid,
12409 so that GDB will ignore it. */
72dca2f5 12410 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12411 return PC_BOUNDS_INVALID;
c906108c
SS
12412
12413 *lowpc = low;
96408a79
SA
12414 if (highpc)
12415 *highpc = high;
af34e669 12416 return ret;
c906108c
SS
12417}
12418
b084d499
JB
12419/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12420 its low and high PC addresses. Do nothing if these addresses could not
12421 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12422 and HIGHPC to the high address if greater than HIGHPC. */
12423
12424static void
12425dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12426 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12427 struct dwarf2_cu *cu)
12428{
12429 CORE_ADDR low, high;
12430 struct die_info *child = die->child;
12431
e385593e 12432 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12433 {
325fac50
PA
12434 *lowpc = std::min (*lowpc, low);
12435 *highpc = std::max (*highpc, high);
b084d499
JB
12436 }
12437
12438 /* If the language does not allow nested subprograms (either inside
12439 subprograms or lexical blocks), we're done. */
12440 if (cu->language != language_ada)
12441 return;
6e70227d 12442
b084d499
JB
12443 /* Check all the children of the given DIE. If it contains nested
12444 subprograms, then check their pc bounds. Likewise, we need to
12445 check lexical blocks as well, as they may also contain subprogram
12446 definitions. */
12447 while (child && child->tag)
12448 {
12449 if (child->tag == DW_TAG_subprogram
12450 || child->tag == DW_TAG_lexical_block)
12451 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12452 child = sibling_die (child);
12453 }
12454}
12455
fae299cd
DC
12456/* Get the low and high pc's represented by the scope DIE, and store
12457 them in *LOWPC and *HIGHPC. If the correct values can't be
12458 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12459
12460static void
12461get_scope_pc_bounds (struct die_info *die,
12462 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12463 struct dwarf2_cu *cu)
12464{
12465 CORE_ADDR best_low = (CORE_ADDR) -1;
12466 CORE_ADDR best_high = (CORE_ADDR) 0;
12467 CORE_ADDR current_low, current_high;
12468
3a2b436a 12469 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12470 >= PC_BOUNDS_RANGES)
fae299cd
DC
12471 {
12472 best_low = current_low;
12473 best_high = current_high;
12474 }
12475 else
12476 {
12477 struct die_info *child = die->child;
12478
12479 while (child && child->tag)
12480 {
12481 switch (child->tag) {
12482 case DW_TAG_subprogram:
b084d499 12483 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12484 break;
12485 case DW_TAG_namespace:
f55ee35c 12486 case DW_TAG_module:
fae299cd
DC
12487 /* FIXME: carlton/2004-01-16: Should we do this for
12488 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12489 that current GCC's always emit the DIEs corresponding
12490 to definitions of methods of classes as children of a
12491 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12492 the DIEs giving the declarations, which could be
12493 anywhere). But I don't see any reason why the
12494 standards says that they have to be there. */
12495 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12496
12497 if (current_low != ((CORE_ADDR) -1))
12498 {
325fac50
PA
12499 best_low = std::min (best_low, current_low);
12500 best_high = std::max (best_high, current_high);
fae299cd
DC
12501 }
12502 break;
12503 default:
0963b4bd 12504 /* Ignore. */
fae299cd
DC
12505 break;
12506 }
12507
12508 child = sibling_die (child);
12509 }
12510 }
12511
12512 *lowpc = best_low;
12513 *highpc = best_high;
12514}
12515
801e3a5b
JB
12516/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12517 in DIE. */
380bca97 12518
801e3a5b
JB
12519static void
12520dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12521 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12522{
bb5ed363 12523 struct objfile *objfile = cu->objfile;
3e29f34a 12524 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12525 struct attribute *attr;
91da1414 12526 struct attribute *attr_high;
801e3a5b 12527
91da1414
MW
12528 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12529 if (attr_high)
801e3a5b 12530 {
801e3a5b
JB
12531 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12532 if (attr)
12533 {
31aa7e4e
JB
12534 CORE_ADDR low = attr_value_as_address (attr);
12535 CORE_ADDR high = attr_value_as_address (attr_high);
12536
12537 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12538 high += low;
9a619af0 12539
3e29f34a
MR
12540 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12541 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12542 record_block_range (block, low, high - 1);
801e3a5b
JB
12543 }
12544 }
12545
12546 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12547 if (attr)
12548 {
bb5ed363 12549 bfd *obfd = objfile->obfd;
ab435259
DE
12550 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12551 We take advantage of the fact that DW_AT_ranges does not appear
12552 in DW_TAG_compile_unit of DWO files. */
12553 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12554
12555 /* The value of the DW_AT_ranges attribute is the offset of the
12556 address range list in the .debug_ranges section. */
ab435259
DE
12557 unsigned long offset = (DW_UNSND (attr)
12558 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12559 const gdb_byte *buffer;
801e3a5b
JB
12560
12561 /* For some target architectures, but not others, the
12562 read_address function sign-extends the addresses it returns.
12563 To recognize base address selection entries, we need a
12564 mask. */
12565 unsigned int addr_size = cu->header.addr_size;
12566 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12567
12568 /* The base address, to which the next pair is relative. Note
12569 that this 'base' is a DWARF concept: most entries in a range
12570 list are relative, to reduce the number of relocs against the
12571 debugging information. This is separate from this function's
12572 'baseaddr' argument, which GDB uses to relocate debugging
12573 information from a shared library based on the address at
12574 which the library was loaded. */
d00adf39
DE
12575 CORE_ADDR base = cu->base_address;
12576 int base_known = cu->base_known;
801e3a5b 12577
5f46c5a5
JK
12578 dwarf2_ranges_process (offset, cu,
12579 [&] (CORE_ADDR start, CORE_ADDR end)
12580 {
58fdfd2c
JK
12581 start += baseaddr;
12582 end += baseaddr;
5f46c5a5
JK
12583 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12584 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12585 record_block_range (block, start, end - 1);
12586 });
801e3a5b
JB
12587 }
12588}
12589
685b1105
JK
12590/* Check whether the producer field indicates either of GCC < 4.6, or the
12591 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12592
685b1105
JK
12593static void
12594check_producer (struct dwarf2_cu *cu)
60d5a603 12595{
38360086 12596 int major, minor;
60d5a603
JK
12597
12598 if (cu->producer == NULL)
12599 {
12600 /* For unknown compilers expect their behavior is DWARF version
12601 compliant.
12602
12603 GCC started to support .debug_types sections by -gdwarf-4 since
12604 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12605 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12606 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12607 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12608 }
b1ffba5a 12609 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12610 {
38360086
MW
12611 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12612 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12613 }
61012eef 12614 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12615 cu->producer_is_icc = 1;
12616 else
12617 {
12618 /* For other non-GCC compilers, expect their behavior is DWARF version
12619 compliant. */
60d5a603
JK
12620 }
12621
ba919b58 12622 cu->checked_producer = 1;
685b1105 12623}
ba919b58 12624
685b1105
JK
12625/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12626 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12627 during 4.6.0 experimental. */
12628
12629static int
12630producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12631{
12632 if (!cu->checked_producer)
12633 check_producer (cu);
12634
12635 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12636}
12637
12638/* Return the default accessibility type if it is not overriden by
12639 DW_AT_accessibility. */
12640
12641static enum dwarf_access_attribute
12642dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12643{
12644 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12645 {
12646 /* The default DWARF 2 accessibility for members is public, the default
12647 accessibility for inheritance is private. */
12648
12649 if (die->tag != DW_TAG_inheritance)
12650 return DW_ACCESS_public;
12651 else
12652 return DW_ACCESS_private;
12653 }
12654 else
12655 {
12656 /* DWARF 3+ defines the default accessibility a different way. The same
12657 rules apply now for DW_TAG_inheritance as for the members and it only
12658 depends on the container kind. */
12659
12660 if (die->parent->tag == DW_TAG_class_type)
12661 return DW_ACCESS_private;
12662 else
12663 return DW_ACCESS_public;
12664 }
12665}
12666
74ac6d43
TT
12667/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12668 offset. If the attribute was not found return 0, otherwise return
12669 1. If it was found but could not properly be handled, set *OFFSET
12670 to 0. */
12671
12672static int
12673handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12674 LONGEST *offset)
12675{
12676 struct attribute *attr;
12677
12678 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12679 if (attr != NULL)
12680 {
12681 *offset = 0;
12682
12683 /* Note that we do not check for a section offset first here.
12684 This is because DW_AT_data_member_location is new in DWARF 4,
12685 so if we see it, we can assume that a constant form is really
12686 a constant and not a section offset. */
12687 if (attr_form_is_constant (attr))
12688 *offset = dwarf2_get_attr_constant_value (attr, 0);
12689 else if (attr_form_is_section_offset (attr))
12690 dwarf2_complex_location_expr_complaint ();
12691 else if (attr_form_is_block (attr))
12692 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12693 else
12694 dwarf2_complex_location_expr_complaint ();
12695
12696 return 1;
12697 }
12698
12699 return 0;
12700}
12701
c906108c
SS
12702/* Add an aggregate field to the field list. */
12703
12704static void
107d2387 12705dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12706 struct dwarf2_cu *cu)
6e70227d 12707{
e7c27a73 12708 struct objfile *objfile = cu->objfile;
5e2b427d 12709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12710 struct nextfield *new_field;
12711 struct attribute *attr;
12712 struct field *fp;
15d034d0 12713 const char *fieldname = "";
c906108c
SS
12714
12715 /* Allocate a new field list entry and link it in. */
8d749320 12716 new_field = XNEW (struct nextfield);
b8c9b27d 12717 make_cleanup (xfree, new_field);
c906108c 12718 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12719
12720 if (die->tag == DW_TAG_inheritance)
12721 {
12722 new_field->next = fip->baseclasses;
12723 fip->baseclasses = new_field;
12724 }
12725 else
12726 {
12727 new_field->next = fip->fields;
12728 fip->fields = new_field;
12729 }
c906108c
SS
12730 fip->nfields++;
12731
e142c38c 12732 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12733 if (attr)
12734 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12735 else
12736 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12737 if (new_field->accessibility != DW_ACCESS_public)
12738 fip->non_public_fields = 1;
60d5a603 12739
e142c38c 12740 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12741 if (attr)
12742 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12743 else
12744 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12745
12746 fp = &new_field->field;
a9a9bd0f 12747
e142c38c 12748 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12749 {
74ac6d43
TT
12750 LONGEST offset;
12751
a9a9bd0f 12752 /* Data member other than a C++ static data member. */
6e70227d 12753
c906108c 12754 /* Get type of field. */
e7c27a73 12755 fp->type = die_type (die, cu);
c906108c 12756
d6a843b5 12757 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12758
c906108c 12759 /* Get bit size of field (zero if none). */
e142c38c 12760 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12761 if (attr)
12762 {
12763 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12764 }
12765 else
12766 {
12767 FIELD_BITSIZE (*fp) = 0;
12768 }
12769
12770 /* Get bit offset of field. */
74ac6d43
TT
12771 if (handle_data_member_location (die, cu, &offset))
12772 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12773 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12774 if (attr)
12775 {
5e2b427d 12776 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12777 {
12778 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12779 additional bit offset from the MSB of the containing
12780 anonymous object to the MSB of the field. We don't
12781 have to do anything special since we don't need to
12782 know the size of the anonymous object. */
f41f5e61 12783 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12784 }
12785 else
12786 {
12787 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12788 MSB of the anonymous object, subtract off the number of
12789 bits from the MSB of the field to the MSB of the
12790 object, and then subtract off the number of bits of
12791 the field itself. The result is the bit offset of
12792 the LSB of the field. */
c906108c
SS
12793 int anonymous_size;
12794 int bit_offset = DW_UNSND (attr);
12795
e142c38c 12796 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12797 if (attr)
12798 {
12799 /* The size of the anonymous object containing
12800 the bit field is explicit, so use the
12801 indicated size (in bytes). */
12802 anonymous_size = DW_UNSND (attr);
12803 }
12804 else
12805 {
12806 /* The size of the anonymous object containing
12807 the bit field must be inferred from the type
12808 attribute of the data member containing the
12809 bit field. */
12810 anonymous_size = TYPE_LENGTH (fp->type);
12811 }
f41f5e61
PA
12812 SET_FIELD_BITPOS (*fp,
12813 (FIELD_BITPOS (*fp)
12814 + anonymous_size * bits_per_byte
12815 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12816 }
12817 }
da5b30da
AA
12818 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12819 if (attr != NULL)
12820 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12821 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
12822
12823 /* Get name of field. */
39cbfefa
DJ
12824 fieldname = dwarf2_name (die, cu);
12825 if (fieldname == NULL)
12826 fieldname = "";
d8151005
DJ
12827
12828 /* The name is already allocated along with this objfile, so we don't
12829 need to duplicate it for the type. */
12830 fp->name = fieldname;
c906108c
SS
12831
12832 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12833 pointer or virtual base class pointer) to private. */
e142c38c 12834 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12835 {
d48cc9dd 12836 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12837 new_field->accessibility = DW_ACCESS_private;
12838 fip->non_public_fields = 1;
12839 }
12840 }
a9a9bd0f 12841 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12842 {
a9a9bd0f
DC
12843 /* C++ static member. */
12844
12845 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12846 is a declaration, but all versions of G++ as of this writing
12847 (so through at least 3.2.1) incorrectly generate
12848 DW_TAG_variable tags. */
6e70227d 12849
ff355380 12850 const char *physname;
c906108c 12851
a9a9bd0f 12852 /* Get name of field. */
39cbfefa
DJ
12853 fieldname = dwarf2_name (die, cu);
12854 if (fieldname == NULL)
c906108c
SS
12855 return;
12856
254e6b9e 12857 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12858 if (attr
12859 /* Only create a symbol if this is an external value.
12860 new_symbol checks this and puts the value in the global symbol
12861 table, which we want. If it is not external, new_symbol
12862 will try to put the value in cu->list_in_scope which is wrong. */
12863 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12864 {
12865 /* A static const member, not much different than an enum as far as
12866 we're concerned, except that we can support more types. */
12867 new_symbol (die, NULL, cu);
12868 }
12869
2df3850c 12870 /* Get physical name. */
ff355380 12871 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12872
d8151005
DJ
12873 /* The name is already allocated along with this objfile, so we don't
12874 need to duplicate it for the type. */
12875 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12876 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12877 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12878 }
12879 else if (die->tag == DW_TAG_inheritance)
12880 {
74ac6d43 12881 LONGEST offset;
d4b96c9a 12882
74ac6d43
TT
12883 /* C++ base class field. */
12884 if (handle_data_member_location (die, cu, &offset))
12885 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12886 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12887 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12888 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12889 fip->nbaseclasses++;
12890 }
12891}
12892
98751a41
JK
12893/* Add a typedef defined in the scope of the FIP's class. */
12894
12895static void
12896dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12897 struct dwarf2_cu *cu)
6e70227d 12898{
98751a41 12899 struct typedef_field_list *new_field;
98751a41 12900 struct typedef_field *fp;
98751a41
JK
12901
12902 /* Allocate a new field list entry and link it in. */
8d749320 12903 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
12904 make_cleanup (xfree, new_field);
12905
12906 gdb_assert (die->tag == DW_TAG_typedef);
12907
12908 fp = &new_field->field;
12909
12910 /* Get name of field. */
12911 fp->name = dwarf2_name (die, cu);
12912 if (fp->name == NULL)
12913 return;
12914
12915 fp->type = read_type_die (die, cu);
12916
12917 new_field->next = fip->typedef_field_list;
12918 fip->typedef_field_list = new_field;
12919 fip->typedef_field_list_count++;
12920}
12921
c906108c
SS
12922/* Create the vector of fields, and attach it to the type. */
12923
12924static void
fba45db2 12925dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12926 struct dwarf2_cu *cu)
c906108c
SS
12927{
12928 int nfields = fip->nfields;
12929
12930 /* Record the field count, allocate space for the array of fields,
12931 and create blank accessibility bitfields if necessary. */
12932 TYPE_NFIELDS (type) = nfields;
12933 TYPE_FIELDS (type) = (struct field *)
12934 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12935 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12936
b4ba55a1 12937 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
12938 {
12939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12940
12941 TYPE_FIELD_PRIVATE_BITS (type) =
12942 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12943 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12944
12945 TYPE_FIELD_PROTECTED_BITS (type) =
12946 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12947 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12948
774b6a14
TT
12949 TYPE_FIELD_IGNORE_BITS (type) =
12950 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12951 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
12952 }
12953
12954 /* If the type has baseclasses, allocate and clear a bit vector for
12955 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 12956 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
12957 {
12958 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 12959 unsigned char *pointer;
c906108c
SS
12960
12961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 12962 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 12963 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
12964 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12965 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12966 }
12967
3e43a32a
MS
12968 /* Copy the saved-up fields into the field vector. Start from the head of
12969 the list, adding to the tail of the field array, so that they end up in
12970 the same order in the array in which they were added to the list. */
c906108c
SS
12971 while (nfields-- > 0)
12972 {
7d0ccb61
DJ
12973 struct nextfield *fieldp;
12974
12975 if (fip->fields)
12976 {
12977 fieldp = fip->fields;
12978 fip->fields = fieldp->next;
12979 }
12980 else
12981 {
12982 fieldp = fip->baseclasses;
12983 fip->baseclasses = fieldp->next;
12984 }
12985
12986 TYPE_FIELD (type, nfields) = fieldp->field;
12987 switch (fieldp->accessibility)
c906108c 12988 {
c5aa993b 12989 case DW_ACCESS_private:
b4ba55a1
JB
12990 if (cu->language != language_ada)
12991 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 12992 break;
c906108c 12993
c5aa993b 12994 case DW_ACCESS_protected:
b4ba55a1
JB
12995 if (cu->language != language_ada)
12996 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 12997 break;
c906108c 12998
c5aa993b
JM
12999 case DW_ACCESS_public:
13000 break;
c906108c 13001
c5aa993b
JM
13002 default:
13003 /* Unknown accessibility. Complain and treat it as public. */
13004 {
e2e0b3e5 13005 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13006 fieldp->accessibility);
c5aa993b
JM
13007 }
13008 break;
c906108c
SS
13009 }
13010 if (nfields < fip->nbaseclasses)
13011 {
7d0ccb61 13012 switch (fieldp->virtuality)
c906108c 13013 {
c5aa993b
JM
13014 case DW_VIRTUALITY_virtual:
13015 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13016 if (cu->language == language_ada)
a73c6dcd 13017 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13018 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13019 break;
c906108c
SS
13020 }
13021 }
c906108c
SS
13022 }
13023}
13024
7d27a96d
TT
13025/* Return true if this member function is a constructor, false
13026 otherwise. */
13027
13028static int
13029dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13030{
13031 const char *fieldname;
fe978cb0 13032 const char *type_name;
7d27a96d
TT
13033 int len;
13034
13035 if (die->parent == NULL)
13036 return 0;
13037
13038 if (die->parent->tag != DW_TAG_structure_type
13039 && die->parent->tag != DW_TAG_union_type
13040 && die->parent->tag != DW_TAG_class_type)
13041 return 0;
13042
13043 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13044 type_name = dwarf2_name (die->parent, cu);
13045 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13046 return 0;
13047
13048 len = strlen (fieldname);
fe978cb0
PA
13049 return (strncmp (fieldname, type_name, len) == 0
13050 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13051}
13052
c906108c
SS
13053/* Add a member function to the proper fieldlist. */
13054
13055static void
107d2387 13056dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13057 struct type *type, struct dwarf2_cu *cu)
c906108c 13058{
e7c27a73 13059 struct objfile *objfile = cu->objfile;
c906108c
SS
13060 struct attribute *attr;
13061 struct fnfieldlist *flp;
13062 int i;
13063 struct fn_field *fnp;
15d034d0 13064 const char *fieldname;
c906108c 13065 struct nextfnfield *new_fnfield;
f792889a 13066 struct type *this_type;
60d5a603 13067 enum dwarf_access_attribute accessibility;
c906108c 13068
b4ba55a1 13069 if (cu->language == language_ada)
a73c6dcd 13070 error (_("unexpected member function in Ada type"));
b4ba55a1 13071
2df3850c 13072 /* Get name of member function. */
39cbfefa
DJ
13073 fieldname = dwarf2_name (die, cu);
13074 if (fieldname == NULL)
2df3850c 13075 return;
c906108c 13076
c906108c
SS
13077 /* Look up member function name in fieldlist. */
13078 for (i = 0; i < fip->nfnfields; i++)
13079 {
27bfe10e 13080 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13081 break;
13082 }
13083
13084 /* Create new list element if necessary. */
13085 if (i < fip->nfnfields)
13086 flp = &fip->fnfieldlists[i];
13087 else
13088 {
13089 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13090 {
13091 fip->fnfieldlists = (struct fnfieldlist *)
13092 xrealloc (fip->fnfieldlists,
13093 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13094 * sizeof (struct fnfieldlist));
c906108c 13095 if (fip->nfnfields == 0)
c13c43fd 13096 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13097 }
13098 flp = &fip->fnfieldlists[fip->nfnfields];
13099 flp->name = fieldname;
13100 flp->length = 0;
13101 flp->head = NULL;
3da10d80 13102 i = fip->nfnfields++;
c906108c
SS
13103 }
13104
13105 /* Create a new member function field and chain it to the field list
0963b4bd 13106 entry. */
8d749320 13107 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13108 make_cleanup (xfree, new_fnfield);
c906108c
SS
13109 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13110 new_fnfield->next = flp->head;
13111 flp->head = new_fnfield;
13112 flp->length++;
13113
13114 /* Fill in the member function field info. */
13115 fnp = &new_fnfield->fnfield;
3da10d80
KS
13116
13117 /* Delay processing of the physname until later. */
9c37b5ae 13118 if (cu->language == language_cplus)
3da10d80
KS
13119 {
13120 add_to_method_list (type, i, flp->length - 1, fieldname,
13121 die, cu);
13122 }
13123 else
13124 {
1d06ead6 13125 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13126 fnp->physname = physname ? physname : "";
13127 }
13128
c906108c 13129 fnp->type = alloc_type (objfile);
f792889a
DJ
13130 this_type = read_type_die (die, cu);
13131 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13132 {
f792889a 13133 int nparams = TYPE_NFIELDS (this_type);
c906108c 13134
f792889a 13135 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13136 of the method itself (TYPE_CODE_METHOD). */
13137 smash_to_method_type (fnp->type, type,
f792889a
DJ
13138 TYPE_TARGET_TYPE (this_type),
13139 TYPE_FIELDS (this_type),
13140 TYPE_NFIELDS (this_type),
13141 TYPE_VARARGS (this_type));
c906108c
SS
13142
13143 /* Handle static member functions.
c5aa993b 13144 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13145 member functions. G++ helps GDB by marking the first
13146 parameter for non-static member functions (which is the this
13147 pointer) as artificial. We obtain this information from
13148 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13149 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13150 fnp->voffset = VOFFSET_STATIC;
13151 }
13152 else
e2e0b3e5 13153 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13154 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13155
13156 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13157 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13158 fnp->fcontext = die_containing_type (die, cu);
c906108c 13159
3e43a32a
MS
13160 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13161 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13162
13163 /* Get accessibility. */
e142c38c 13164 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13165 if (attr)
aead7601 13166 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13167 else
13168 accessibility = dwarf2_default_access_attribute (die, cu);
13169 switch (accessibility)
c906108c 13170 {
60d5a603
JK
13171 case DW_ACCESS_private:
13172 fnp->is_private = 1;
13173 break;
13174 case DW_ACCESS_protected:
13175 fnp->is_protected = 1;
13176 break;
c906108c
SS
13177 }
13178
b02dede2 13179 /* Check for artificial methods. */
e142c38c 13180 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13181 if (attr && DW_UNSND (attr) != 0)
13182 fnp->is_artificial = 1;
13183
7d27a96d
TT
13184 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13185
0d564a31 13186 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13187 function. For older versions of GCC, this is an offset in the
13188 appropriate virtual table, as specified by DW_AT_containing_type.
13189 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13190 to the object address. */
13191
e142c38c 13192 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13193 if (attr)
8e19ed76 13194 {
aec5aa8b 13195 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13196 {
aec5aa8b
TT
13197 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13198 {
13199 /* Old-style GCC. */
13200 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13201 }
13202 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13203 || (DW_BLOCK (attr)->size > 1
13204 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13205 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13206 {
aec5aa8b
TT
13207 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13208 if ((fnp->voffset % cu->header.addr_size) != 0)
13209 dwarf2_complex_location_expr_complaint ();
13210 else
13211 fnp->voffset /= cu->header.addr_size;
13212 fnp->voffset += 2;
13213 }
13214 else
13215 dwarf2_complex_location_expr_complaint ();
13216
13217 if (!fnp->fcontext)
7e993ebf
KS
13218 {
13219 /* If there is no `this' field and no DW_AT_containing_type,
13220 we cannot actually find a base class context for the
13221 vtable! */
13222 if (TYPE_NFIELDS (this_type) == 0
13223 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13224 {
13225 complaint (&symfile_complaints,
13226 _("cannot determine context for virtual member "
13227 "function \"%s\" (offset %d)"),
13228 fieldname, die->offset.sect_off);
13229 }
13230 else
13231 {
13232 fnp->fcontext
13233 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13234 }
13235 }
aec5aa8b 13236 }
3690dd37 13237 else if (attr_form_is_section_offset (attr))
8e19ed76 13238 {
4d3c2250 13239 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13240 }
13241 else
13242 {
4d3c2250
KB
13243 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13244 fieldname);
8e19ed76 13245 }
0d564a31 13246 }
d48cc9dd
DJ
13247 else
13248 {
13249 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13250 if (attr && DW_UNSND (attr))
13251 {
13252 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13253 complaint (&symfile_complaints,
3e43a32a
MS
13254 _("Member function \"%s\" (offset %d) is virtual "
13255 "but the vtable offset is not specified"),
b64f50a1 13256 fieldname, die->offset.sect_off);
9655fd1a 13257 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13258 TYPE_CPLUS_DYNAMIC (type) = 1;
13259 }
13260 }
c906108c
SS
13261}
13262
13263/* Create the vector of member function fields, and attach it to the type. */
13264
13265static void
fba45db2 13266dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13267 struct dwarf2_cu *cu)
c906108c
SS
13268{
13269 struct fnfieldlist *flp;
c906108c
SS
13270 int i;
13271
b4ba55a1 13272 if (cu->language == language_ada)
a73c6dcd 13273 error (_("unexpected member functions in Ada type"));
b4ba55a1 13274
c906108c
SS
13275 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13276 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13277 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13278
13279 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13280 {
13281 struct nextfnfield *nfp = flp->head;
13282 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13283 int k;
13284
13285 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13286 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13287 fn_flp->fn_fields = (struct fn_field *)
13288 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13289 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13290 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13291 }
13292
13293 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13294}
13295
1168df01
JB
13296/* Returns non-zero if NAME is the name of a vtable member in CU's
13297 language, zero otherwise. */
13298static int
13299is_vtable_name (const char *name, struct dwarf2_cu *cu)
13300{
13301 static const char vptr[] = "_vptr";
987504bb 13302 static const char vtable[] = "vtable";
1168df01 13303
9c37b5ae
TT
13304 /* Look for the C++ form of the vtable. */
13305 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13306 return 1;
13307
13308 return 0;
13309}
13310
c0dd20ea 13311/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13312 functions, with the ABI-specified layout. If TYPE describes
13313 such a structure, smash it into a member function type.
61049d3b
DJ
13314
13315 GCC shouldn't do this; it should just output pointer to member DIEs.
13316 This is GCC PR debug/28767. */
c0dd20ea 13317
0b92b5bb
TT
13318static void
13319quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13320{
09e2d7c7 13321 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13322
13323 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13324 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13325 return;
c0dd20ea
DJ
13326
13327 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13328 if (TYPE_FIELD_NAME (type, 0) == NULL
13329 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13330 || TYPE_FIELD_NAME (type, 1) == NULL
13331 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13332 return;
c0dd20ea
DJ
13333
13334 /* Find the type of the method. */
0b92b5bb 13335 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13336 if (pfn_type == NULL
13337 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13338 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13339 return;
c0dd20ea
DJ
13340
13341 /* Look for the "this" argument. */
13342 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13343 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13344 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13345 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13346 return;
c0dd20ea 13347
09e2d7c7 13348 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13349 new_type = alloc_type (objfile);
09e2d7c7 13350 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13351 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13352 TYPE_VARARGS (pfn_type));
0b92b5bb 13353 smash_to_methodptr_type (type, new_type);
c0dd20ea 13354}
1168df01 13355
685b1105
JK
13356/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13357 (icc). */
13358
13359static int
13360producer_is_icc (struct dwarf2_cu *cu)
13361{
13362 if (!cu->checked_producer)
13363 check_producer (cu);
13364
13365 return cu->producer_is_icc;
13366}
13367
c906108c 13368/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13369 (definition) to create a type for the structure or union. Fill in
13370 the type's name and general properties; the members will not be
83655187
DE
13371 processed until process_structure_scope. A symbol table entry for
13372 the type will also not be done until process_structure_scope (assuming
13373 the type has a name).
c906108c 13374
c767944b
DJ
13375 NOTE: we need to call these functions regardless of whether or not the
13376 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13377 structure or union. This gets the type entered into our set of
83655187 13378 user defined types. */
c906108c 13379
f792889a 13380static struct type *
134d01f1 13381read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13382{
e7c27a73 13383 struct objfile *objfile = cu->objfile;
c906108c
SS
13384 struct type *type;
13385 struct attribute *attr;
15d034d0 13386 const char *name;
c906108c 13387
348e048f
DE
13388 /* If the definition of this type lives in .debug_types, read that type.
13389 Don't follow DW_AT_specification though, that will take us back up
13390 the chain and we want to go down. */
45e58e77 13391 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13392 if (attr)
13393 {
ac9ec31b 13394 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13395
ac9ec31b 13396 /* The type's CU may not be the same as CU.
02142a6c 13397 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13398 return set_die_type (die, type, cu);
13399 }
13400
c0dd20ea 13401 type = alloc_type (objfile);
c906108c 13402 INIT_CPLUS_SPECIFIC (type);
93311388 13403
39cbfefa
DJ
13404 name = dwarf2_name (die, cu);
13405 if (name != NULL)
c906108c 13406 {
987504bb 13407 if (cu->language == language_cplus
c44af4eb
TT
13408 || cu->language == language_d
13409 || cu->language == language_rust)
63d06c5c 13410 {
15d034d0 13411 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13412
13413 /* dwarf2_full_name might have already finished building the DIE's
13414 type. If so, there is no need to continue. */
13415 if (get_die_type (die, cu) != NULL)
13416 return get_die_type (die, cu);
13417
13418 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13419 if (die->tag == DW_TAG_structure_type
13420 || die->tag == DW_TAG_class_type)
13421 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13422 }
13423 else
13424 {
d8151005
DJ
13425 /* The name is already allocated along with this objfile, so
13426 we don't need to duplicate it for the type. */
7d455152 13427 TYPE_TAG_NAME (type) = name;
94af9270
KS
13428 if (die->tag == DW_TAG_class_type)
13429 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13430 }
c906108c
SS
13431 }
13432
13433 if (die->tag == DW_TAG_structure_type)
13434 {
13435 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13436 }
13437 else if (die->tag == DW_TAG_union_type)
13438 {
13439 TYPE_CODE (type) = TYPE_CODE_UNION;
13440 }
13441 else
13442 {
4753d33b 13443 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13444 }
13445
0cc2414c
TT
13446 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13447 TYPE_DECLARED_CLASS (type) = 1;
13448
e142c38c 13449 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13450 if (attr)
13451 {
155bfbd3
JB
13452 if (attr_form_is_constant (attr))
13453 TYPE_LENGTH (type) = DW_UNSND (attr);
13454 else
13455 {
13456 /* For the moment, dynamic type sizes are not supported
13457 by GDB's struct type. The actual size is determined
13458 on-demand when resolving the type of a given object,
13459 so set the type's length to zero for now. Otherwise,
13460 we record an expression as the length, and that expression
13461 could lead to a very large value, which could eventually
13462 lead to us trying to allocate that much memory when creating
13463 a value of that type. */
13464 TYPE_LENGTH (type) = 0;
13465 }
c906108c
SS
13466 }
13467 else
13468 {
13469 TYPE_LENGTH (type) = 0;
13470 }
13471
422b1cb0 13472 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13473 {
13474 /* ICC does not output the required DW_AT_declaration
13475 on incomplete types, but gives them a size of zero. */
422b1cb0 13476 TYPE_STUB (type) = 1;
685b1105
JK
13477 }
13478 else
13479 TYPE_STUB_SUPPORTED (type) = 1;
13480
dc718098 13481 if (die_is_declaration (die, cu))
876cecd0 13482 TYPE_STUB (type) = 1;
a6c727b2
DJ
13483 else if (attr == NULL && die->child == NULL
13484 && producer_is_realview (cu->producer))
13485 /* RealView does not output the required DW_AT_declaration
13486 on incomplete types. */
13487 TYPE_STUB (type) = 1;
dc718098 13488
c906108c
SS
13489 /* We need to add the type field to the die immediately so we don't
13490 infinitely recurse when dealing with pointers to the structure
0963b4bd 13491 type within the structure itself. */
1c379e20 13492 set_die_type (die, type, cu);
c906108c 13493
7e314c57
JK
13494 /* set_die_type should be already done. */
13495 set_descriptive_type (type, die, cu);
13496
c767944b
DJ
13497 return type;
13498}
13499
13500/* Finish creating a structure or union type, including filling in
13501 its members and creating a symbol for it. */
13502
13503static void
13504process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13505{
13506 struct objfile *objfile = cu->objfile;
ca040673 13507 struct die_info *child_die;
c767944b
DJ
13508 struct type *type;
13509
13510 type = get_die_type (die, cu);
13511 if (type == NULL)
13512 type = read_structure_type (die, cu);
13513
e142c38c 13514 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13515 {
13516 struct field_info fi;
34eaf542 13517 VEC (symbolp) *template_args = NULL;
c767944b 13518 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13519
13520 memset (&fi, 0, sizeof (struct field_info));
13521
639d11d3 13522 child_die = die->child;
c906108c
SS
13523
13524 while (child_die && child_die->tag)
13525 {
a9a9bd0f
DC
13526 if (child_die->tag == DW_TAG_member
13527 || child_die->tag == DW_TAG_variable)
c906108c 13528 {
a9a9bd0f
DC
13529 /* NOTE: carlton/2002-11-05: A C++ static data member
13530 should be a DW_TAG_member that is a declaration, but
13531 all versions of G++ as of this writing (so through at
13532 least 3.2.1) incorrectly generate DW_TAG_variable
13533 tags for them instead. */
e7c27a73 13534 dwarf2_add_field (&fi, child_die, cu);
c906108c 13535 }
8713b1b1 13536 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13537 {
e98c9e7c
TT
13538 /* Rust doesn't have member functions in the C++ sense.
13539 However, it does emit ordinary functions as children
13540 of a struct DIE. */
13541 if (cu->language == language_rust)
13542 read_func_scope (child_die, cu);
13543 else
13544 {
13545 /* C++ member function. */
13546 dwarf2_add_member_fn (&fi, child_die, type, cu);
13547 }
c906108c
SS
13548 }
13549 else if (child_die->tag == DW_TAG_inheritance)
13550 {
13551 /* C++ base class field. */
e7c27a73 13552 dwarf2_add_field (&fi, child_die, cu);
c906108c 13553 }
98751a41
JK
13554 else if (child_die->tag == DW_TAG_typedef)
13555 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13556 else if (child_die->tag == DW_TAG_template_type_param
13557 || child_die->tag == DW_TAG_template_value_param)
13558 {
13559 struct symbol *arg = new_symbol (child_die, NULL, cu);
13560
f1078f66
DJ
13561 if (arg != NULL)
13562 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13563 }
13564
c906108c
SS
13565 child_die = sibling_die (child_die);
13566 }
13567
34eaf542
TT
13568 /* Attach template arguments to type. */
13569 if (! VEC_empty (symbolp, template_args))
13570 {
13571 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13572 TYPE_N_TEMPLATE_ARGUMENTS (type)
13573 = VEC_length (symbolp, template_args);
13574 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13575 = XOBNEWVEC (&objfile->objfile_obstack,
13576 struct symbol *,
13577 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13578 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13579 VEC_address (symbolp, template_args),
13580 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13581 * sizeof (struct symbol *)));
13582 VEC_free (symbolp, template_args);
13583 }
13584
c906108c
SS
13585 /* Attach fields and member functions to the type. */
13586 if (fi.nfields)
e7c27a73 13587 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13588 if (fi.nfnfields)
13589 {
e7c27a73 13590 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13591
c5aa993b 13592 /* Get the type which refers to the base class (possibly this
c906108c 13593 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13594 class from the DW_AT_containing_type attribute. This use of
13595 DW_AT_containing_type is a GNU extension. */
c906108c 13596
e142c38c 13597 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13598 {
e7c27a73 13599 struct type *t = die_containing_type (die, cu);
c906108c 13600
ae6ae975 13601 set_type_vptr_basetype (type, t);
c906108c
SS
13602 if (type == t)
13603 {
c906108c
SS
13604 int i;
13605
13606 /* Our own class provides vtbl ptr. */
13607 for (i = TYPE_NFIELDS (t) - 1;
13608 i >= TYPE_N_BASECLASSES (t);
13609 --i)
13610 {
0d5cff50 13611 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13612
1168df01 13613 if (is_vtable_name (fieldname, cu))
c906108c 13614 {
ae6ae975 13615 set_type_vptr_fieldno (type, i);
c906108c
SS
13616 break;
13617 }
13618 }
13619
13620 /* Complain if virtual function table field not found. */
13621 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13622 complaint (&symfile_complaints,
3e43a32a
MS
13623 _("virtual function table pointer "
13624 "not found when defining class '%s'"),
4d3c2250
KB
13625 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13626 "");
c906108c
SS
13627 }
13628 else
13629 {
ae6ae975 13630 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13631 }
13632 }
f6235d4c 13633 else if (cu->producer
61012eef 13634 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13635 {
13636 /* The IBM XLC compiler does not provide direct indication
13637 of the containing type, but the vtable pointer is
13638 always named __vfp. */
13639
13640 int i;
13641
13642 for (i = TYPE_NFIELDS (type) - 1;
13643 i >= TYPE_N_BASECLASSES (type);
13644 --i)
13645 {
13646 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13647 {
ae6ae975
DE
13648 set_type_vptr_fieldno (type, i);
13649 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13650 break;
13651 }
13652 }
13653 }
c906108c 13654 }
98751a41
JK
13655
13656 /* Copy fi.typedef_field_list linked list elements content into the
13657 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13658 if (fi.typedef_field_list)
13659 {
13660 int i = fi.typedef_field_list_count;
13661
a0d7a4ff 13662 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13663 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13664 = ((struct typedef_field *)
13665 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13666 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13667
13668 /* Reverse the list order to keep the debug info elements order. */
13669 while (--i >= 0)
13670 {
13671 struct typedef_field *dest, *src;
6e70227d 13672
98751a41
JK
13673 dest = &TYPE_TYPEDEF_FIELD (type, i);
13674 src = &fi.typedef_field_list->field;
13675 fi.typedef_field_list = fi.typedef_field_list->next;
13676 *dest = *src;
13677 }
13678 }
c767944b
DJ
13679
13680 do_cleanups (back_to);
c906108c 13681 }
63d06c5c 13682
bb5ed363 13683 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13684
90aeadfc
DC
13685 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13686 snapshots) has been known to create a die giving a declaration
13687 for a class that has, as a child, a die giving a definition for a
13688 nested class. So we have to process our children even if the
13689 current die is a declaration. Normally, of course, a declaration
13690 won't have any children at all. */
134d01f1 13691
ca040673
DE
13692 child_die = die->child;
13693
90aeadfc
DC
13694 while (child_die != NULL && child_die->tag)
13695 {
13696 if (child_die->tag == DW_TAG_member
13697 || child_die->tag == DW_TAG_variable
34eaf542
TT
13698 || child_die->tag == DW_TAG_inheritance
13699 || child_die->tag == DW_TAG_template_value_param
13700 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13701 {
90aeadfc 13702 /* Do nothing. */
134d01f1 13703 }
90aeadfc
DC
13704 else
13705 process_die (child_die, cu);
134d01f1 13706
90aeadfc 13707 child_die = sibling_die (child_die);
134d01f1
DJ
13708 }
13709
fa4028e9
JB
13710 /* Do not consider external references. According to the DWARF standard,
13711 these DIEs are identified by the fact that they have no byte_size
13712 attribute, and a declaration attribute. */
13713 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13714 || !die_is_declaration (die, cu))
c767944b 13715 new_symbol (die, type, cu);
134d01f1
DJ
13716}
13717
55426c9d
JB
13718/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13719 update TYPE using some information only available in DIE's children. */
13720
13721static void
13722update_enumeration_type_from_children (struct die_info *die,
13723 struct type *type,
13724 struct dwarf2_cu *cu)
13725{
13726 struct obstack obstack;
60f7655a 13727 struct die_info *child_die;
55426c9d
JB
13728 int unsigned_enum = 1;
13729 int flag_enum = 1;
13730 ULONGEST mask = 0;
13731 struct cleanup *old_chain;
13732
13733 obstack_init (&obstack);
13734 old_chain = make_cleanup_obstack_free (&obstack);
13735
60f7655a
DE
13736 for (child_die = die->child;
13737 child_die != NULL && child_die->tag;
13738 child_die = sibling_die (child_die))
55426c9d
JB
13739 {
13740 struct attribute *attr;
13741 LONGEST value;
13742 const gdb_byte *bytes;
13743 struct dwarf2_locexpr_baton *baton;
13744 const char *name;
60f7655a 13745
55426c9d
JB
13746 if (child_die->tag != DW_TAG_enumerator)
13747 continue;
13748
13749 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13750 if (attr == NULL)
13751 continue;
13752
13753 name = dwarf2_name (child_die, cu);
13754 if (name == NULL)
13755 name = "<anonymous enumerator>";
13756
13757 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13758 &value, &bytes, &baton);
13759 if (value < 0)
13760 {
13761 unsigned_enum = 0;
13762 flag_enum = 0;
13763 }
13764 else if ((mask & value) != 0)
13765 flag_enum = 0;
13766 else
13767 mask |= value;
13768
13769 /* If we already know that the enum type is neither unsigned, nor
13770 a flag type, no need to look at the rest of the enumerates. */
13771 if (!unsigned_enum && !flag_enum)
13772 break;
55426c9d
JB
13773 }
13774
13775 if (unsigned_enum)
13776 TYPE_UNSIGNED (type) = 1;
13777 if (flag_enum)
13778 TYPE_FLAG_ENUM (type) = 1;
13779
13780 do_cleanups (old_chain);
13781}
13782
134d01f1
DJ
13783/* Given a DW_AT_enumeration_type die, set its type. We do not
13784 complete the type's fields yet, or create any symbols. */
c906108c 13785
f792889a 13786static struct type *
134d01f1 13787read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13788{
e7c27a73 13789 struct objfile *objfile = cu->objfile;
c906108c 13790 struct type *type;
c906108c 13791 struct attribute *attr;
0114d602 13792 const char *name;
134d01f1 13793
348e048f
DE
13794 /* If the definition of this type lives in .debug_types, read that type.
13795 Don't follow DW_AT_specification though, that will take us back up
13796 the chain and we want to go down. */
45e58e77 13797 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13798 if (attr)
13799 {
ac9ec31b 13800 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13801
ac9ec31b 13802 /* The type's CU may not be the same as CU.
02142a6c 13803 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13804 return set_die_type (die, type, cu);
13805 }
13806
c906108c
SS
13807 type = alloc_type (objfile);
13808
13809 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13810 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13811 if (name != NULL)
7d455152 13812 TYPE_TAG_NAME (type) = name;
c906108c 13813
0626fc76
TT
13814 attr = dwarf2_attr (die, DW_AT_type, cu);
13815 if (attr != NULL)
13816 {
13817 struct type *underlying_type = die_type (die, cu);
13818
13819 TYPE_TARGET_TYPE (type) = underlying_type;
13820 }
13821
e142c38c 13822 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13823 if (attr)
13824 {
13825 TYPE_LENGTH (type) = DW_UNSND (attr);
13826 }
13827 else
13828 {
13829 TYPE_LENGTH (type) = 0;
13830 }
13831
137033e9
JB
13832 /* The enumeration DIE can be incomplete. In Ada, any type can be
13833 declared as private in the package spec, and then defined only
13834 inside the package body. Such types are known as Taft Amendment
13835 Types. When another package uses such a type, an incomplete DIE
13836 may be generated by the compiler. */
02eb380e 13837 if (die_is_declaration (die, cu))
876cecd0 13838 TYPE_STUB (type) = 1;
02eb380e 13839
0626fc76
TT
13840 /* Finish the creation of this type by using the enum's children.
13841 We must call this even when the underlying type has been provided
13842 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13843 update_enumeration_type_from_children (die, type, cu);
13844
0626fc76
TT
13845 /* If this type has an underlying type that is not a stub, then we
13846 may use its attributes. We always use the "unsigned" attribute
13847 in this situation, because ordinarily we guess whether the type
13848 is unsigned -- but the guess can be wrong and the underlying type
13849 can tell us the reality. However, we defer to a local size
13850 attribute if one exists, because this lets the compiler override
13851 the underlying type if needed. */
13852 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13853 {
13854 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13855 if (TYPE_LENGTH (type) == 0)
13856 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13857 }
13858
3d567982
TT
13859 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13860
f792889a 13861 return set_die_type (die, type, cu);
134d01f1
DJ
13862}
13863
13864/* Given a pointer to a die which begins an enumeration, process all
13865 the dies that define the members of the enumeration, and create the
13866 symbol for the enumeration type.
13867
13868 NOTE: We reverse the order of the element list. */
13869
13870static void
13871process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13872{
f792889a 13873 struct type *this_type;
134d01f1 13874
f792889a
DJ
13875 this_type = get_die_type (die, cu);
13876 if (this_type == NULL)
13877 this_type = read_enumeration_type (die, cu);
9dc481d3 13878
639d11d3 13879 if (die->child != NULL)
c906108c 13880 {
9dc481d3
DE
13881 struct die_info *child_die;
13882 struct symbol *sym;
13883 struct field *fields = NULL;
13884 int num_fields = 0;
15d034d0 13885 const char *name;
9dc481d3 13886
639d11d3 13887 child_die = die->child;
c906108c
SS
13888 while (child_die && child_die->tag)
13889 {
13890 if (child_die->tag != DW_TAG_enumerator)
13891 {
e7c27a73 13892 process_die (child_die, cu);
c906108c
SS
13893 }
13894 else
13895 {
39cbfefa
DJ
13896 name = dwarf2_name (child_die, cu);
13897 if (name)
c906108c 13898 {
f792889a 13899 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13900
13901 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13902 {
13903 fields = (struct field *)
13904 xrealloc (fields,
13905 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13906 * sizeof (struct field));
c906108c
SS
13907 }
13908
3567439c 13909 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13910 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13911 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13912 FIELD_BITSIZE (fields[num_fields]) = 0;
13913
13914 num_fields++;
13915 }
13916 }
13917
13918 child_die = sibling_die (child_die);
13919 }
13920
13921 if (num_fields)
13922 {
f792889a
DJ
13923 TYPE_NFIELDS (this_type) = num_fields;
13924 TYPE_FIELDS (this_type) = (struct field *)
13925 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13926 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13927 sizeof (struct field) * num_fields);
b8c9b27d 13928 xfree (fields);
c906108c 13929 }
c906108c 13930 }
134d01f1 13931
6c83ed52
TT
13932 /* If we are reading an enum from a .debug_types unit, and the enum
13933 is a declaration, and the enum is not the signatured type in the
13934 unit, then we do not want to add a symbol for it. Adding a
13935 symbol would in some cases obscure the true definition of the
13936 enum, giving users an incomplete type when the definition is
13937 actually available. Note that we do not want to do this for all
13938 enums which are just declarations, because C++0x allows forward
13939 enum declarations. */
3019eac3 13940 if (cu->per_cu->is_debug_types
6c83ed52
TT
13941 && die_is_declaration (die, cu))
13942 {
52dc124a 13943 struct signatured_type *sig_type;
6c83ed52 13944
c0f78cd4 13945 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
13946 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13947 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
13948 return;
13949 }
13950
f792889a 13951 new_symbol (die, this_type, cu);
c906108c
SS
13952}
13953
13954/* Extract all information from a DW_TAG_array_type DIE and put it in
13955 the DIE's type field. For now, this only handles one dimensional
13956 arrays. */
13957
f792889a 13958static struct type *
e7c27a73 13959read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13960{
e7c27a73 13961 struct objfile *objfile = cu->objfile;
c906108c 13962 struct die_info *child_die;
7e314c57 13963 struct type *type;
c906108c
SS
13964 struct type *element_type, *range_type, *index_type;
13965 struct type **range_types = NULL;
13966 struct attribute *attr;
13967 int ndim = 0;
13968 struct cleanup *back_to;
15d034d0 13969 const char *name;
dc53a7ad 13970 unsigned int bit_stride = 0;
c906108c 13971
e7c27a73 13972 element_type = die_type (die, cu);
c906108c 13973
7e314c57
JK
13974 /* The die_type call above may have already set the type for this DIE. */
13975 type = get_die_type (die, cu);
13976 if (type)
13977 return type;
13978
dc53a7ad
JB
13979 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13980 if (attr != NULL)
13981 bit_stride = DW_UNSND (attr) * 8;
13982
13983 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13984 if (attr != NULL)
13985 bit_stride = DW_UNSND (attr);
13986
c906108c
SS
13987 /* Irix 6.2 native cc creates array types without children for
13988 arrays with unspecified length. */
639d11d3 13989 if (die->child == NULL)
c906108c 13990 {
46bf5051 13991 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 13992 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
13993 type = create_array_type_with_stride (NULL, element_type, range_type,
13994 bit_stride);
f792889a 13995 return set_die_type (die, type, cu);
c906108c
SS
13996 }
13997
13998 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 13999 child_die = die->child;
c906108c
SS
14000 while (child_die && child_die->tag)
14001 {
14002 if (child_die->tag == DW_TAG_subrange_type)
14003 {
f792889a 14004 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14005
f792889a 14006 if (child_type != NULL)
a02abb62 14007 {
0963b4bd
MS
14008 /* The range type was succesfully read. Save it for the
14009 array type creation. */
a02abb62
JB
14010 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14011 {
14012 range_types = (struct type **)
14013 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14014 * sizeof (struct type *));
14015 if (ndim == 0)
14016 make_cleanup (free_current_contents, &range_types);
14017 }
f792889a 14018 range_types[ndim++] = child_type;
a02abb62 14019 }
c906108c
SS
14020 }
14021 child_die = sibling_die (child_die);
14022 }
14023
14024 /* Dwarf2 dimensions are output from left to right, create the
14025 necessary array types in backwards order. */
7ca2d3a3 14026
c906108c 14027 type = element_type;
7ca2d3a3
DL
14028
14029 if (read_array_order (die, cu) == DW_ORD_col_major)
14030 {
14031 int i = 0;
9a619af0 14032
7ca2d3a3 14033 while (i < ndim)
dc53a7ad
JB
14034 type = create_array_type_with_stride (NULL, type, range_types[i++],
14035 bit_stride);
7ca2d3a3
DL
14036 }
14037 else
14038 {
14039 while (ndim-- > 0)
dc53a7ad
JB
14040 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14041 bit_stride);
7ca2d3a3 14042 }
c906108c 14043
f5f8a009
EZ
14044 /* Understand Dwarf2 support for vector types (like they occur on
14045 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14046 array type. This is not part of the Dwarf2/3 standard yet, but a
14047 custom vendor extension. The main difference between a regular
14048 array and the vector variant is that vectors are passed by value
14049 to functions. */
e142c38c 14050 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14051 if (attr)
ea37ba09 14052 make_vector_type (type);
f5f8a009 14053
dbc98a8b
KW
14054 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14055 implementation may choose to implement triple vectors using this
14056 attribute. */
14057 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14058 if (attr)
14059 {
14060 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14061 TYPE_LENGTH (type) = DW_UNSND (attr);
14062 else
3e43a32a
MS
14063 complaint (&symfile_complaints,
14064 _("DW_AT_byte_size for array type smaller "
14065 "than the total size of elements"));
dbc98a8b
KW
14066 }
14067
39cbfefa
DJ
14068 name = dwarf2_name (die, cu);
14069 if (name)
14070 TYPE_NAME (type) = name;
6e70227d 14071
0963b4bd 14072 /* Install the type in the die. */
7e314c57
JK
14073 set_die_type (die, type, cu);
14074
14075 /* set_die_type should be already done. */
b4ba55a1
JB
14076 set_descriptive_type (type, die, cu);
14077
c906108c
SS
14078 do_cleanups (back_to);
14079
7e314c57 14080 return type;
c906108c
SS
14081}
14082
7ca2d3a3 14083static enum dwarf_array_dim_ordering
6e70227d 14084read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14085{
14086 struct attribute *attr;
14087
14088 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14089
aead7601
SM
14090 if (attr)
14091 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14092
0963b4bd
MS
14093 /* GNU F77 is a special case, as at 08/2004 array type info is the
14094 opposite order to the dwarf2 specification, but data is still
14095 laid out as per normal fortran.
7ca2d3a3 14096
0963b4bd
MS
14097 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14098 version checking. */
7ca2d3a3 14099
905e0470
PM
14100 if (cu->language == language_fortran
14101 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14102 {
14103 return DW_ORD_row_major;
14104 }
14105
6e70227d 14106 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14107 {
14108 case array_column_major:
14109 return DW_ORD_col_major;
14110 case array_row_major:
14111 default:
14112 return DW_ORD_row_major;
14113 };
14114}
14115
72019c9c 14116/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14117 the DIE's type field. */
72019c9c 14118
f792889a 14119static struct type *
72019c9c
GM
14120read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14121{
7e314c57
JK
14122 struct type *domain_type, *set_type;
14123 struct attribute *attr;
f792889a 14124
7e314c57
JK
14125 domain_type = die_type (die, cu);
14126
14127 /* The die_type call above may have already set the type for this DIE. */
14128 set_type = get_die_type (die, cu);
14129 if (set_type)
14130 return set_type;
14131
14132 set_type = create_set_type (NULL, domain_type);
14133
14134 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14135 if (attr)
14136 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14137
f792889a 14138 return set_die_type (die, set_type, cu);
72019c9c 14139}
7ca2d3a3 14140
0971de02
TT
14141/* A helper for read_common_block that creates a locexpr baton.
14142 SYM is the symbol which we are marking as computed.
14143 COMMON_DIE is the DIE for the common block.
14144 COMMON_LOC is the location expression attribute for the common
14145 block itself.
14146 MEMBER_LOC is the location expression attribute for the particular
14147 member of the common block that we are processing.
14148 CU is the CU from which the above come. */
14149
14150static void
14151mark_common_block_symbol_computed (struct symbol *sym,
14152 struct die_info *common_die,
14153 struct attribute *common_loc,
14154 struct attribute *member_loc,
14155 struct dwarf2_cu *cu)
14156{
14157 struct objfile *objfile = dwarf2_per_objfile->objfile;
14158 struct dwarf2_locexpr_baton *baton;
14159 gdb_byte *ptr;
14160 unsigned int cu_off;
14161 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14162 LONGEST offset = 0;
14163
14164 gdb_assert (common_loc && member_loc);
14165 gdb_assert (attr_form_is_block (common_loc));
14166 gdb_assert (attr_form_is_block (member_loc)
14167 || attr_form_is_constant (member_loc));
14168
8d749320 14169 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14170 baton->per_cu = cu->per_cu;
14171 gdb_assert (baton->per_cu);
14172
14173 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14174
14175 if (attr_form_is_constant (member_loc))
14176 {
14177 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14178 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14179 }
14180 else
14181 baton->size += DW_BLOCK (member_loc)->size;
14182
224c3ddb 14183 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14184 baton->data = ptr;
14185
14186 *ptr++ = DW_OP_call4;
14187 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14188 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14189 ptr += 4;
14190
14191 if (attr_form_is_constant (member_loc))
14192 {
14193 *ptr++ = DW_OP_addr;
14194 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14195 ptr += cu->header.addr_size;
14196 }
14197 else
14198 {
14199 /* We have to copy the data here, because DW_OP_call4 will only
14200 use a DW_AT_location attribute. */
14201 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14202 ptr += DW_BLOCK (member_loc)->size;
14203 }
14204
14205 *ptr++ = DW_OP_plus;
14206 gdb_assert (ptr - baton->data == baton->size);
14207
0971de02 14208 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14209 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14210}
14211
4357ac6c
TT
14212/* Create appropriate locally-scoped variables for all the
14213 DW_TAG_common_block entries. Also create a struct common_block
14214 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14215 is used to sepate the common blocks name namespace from regular
14216 variable names. */
c906108c
SS
14217
14218static void
e7c27a73 14219read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14220{
0971de02
TT
14221 struct attribute *attr;
14222
14223 attr = dwarf2_attr (die, DW_AT_location, cu);
14224 if (attr)
14225 {
14226 /* Support the .debug_loc offsets. */
14227 if (attr_form_is_block (attr))
14228 {
14229 /* Ok. */
14230 }
14231 else if (attr_form_is_section_offset (attr))
14232 {
14233 dwarf2_complex_location_expr_complaint ();
14234 attr = NULL;
14235 }
14236 else
14237 {
14238 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14239 "common block member");
14240 attr = NULL;
14241 }
14242 }
14243
639d11d3 14244 if (die->child != NULL)
c906108c 14245 {
4357ac6c
TT
14246 struct objfile *objfile = cu->objfile;
14247 struct die_info *child_die;
14248 size_t n_entries = 0, size;
14249 struct common_block *common_block;
14250 struct symbol *sym;
74ac6d43 14251
4357ac6c
TT
14252 for (child_die = die->child;
14253 child_die && child_die->tag;
14254 child_die = sibling_die (child_die))
14255 ++n_entries;
14256
14257 size = (sizeof (struct common_block)
14258 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14259 common_block
14260 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14261 size);
4357ac6c
TT
14262 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14263 common_block->n_entries = 0;
14264
14265 for (child_die = die->child;
14266 child_die && child_die->tag;
14267 child_die = sibling_die (child_die))
14268 {
14269 /* Create the symbol in the DW_TAG_common_block block in the current
14270 symbol scope. */
e7c27a73 14271 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14272 if (sym != NULL)
14273 {
14274 struct attribute *member_loc;
14275
14276 common_block->contents[common_block->n_entries++] = sym;
14277
14278 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14279 cu);
14280 if (member_loc)
14281 {
14282 /* GDB has handled this for a long time, but it is
14283 not specified by DWARF. It seems to have been
14284 emitted by gfortran at least as recently as:
14285 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14286 complaint (&symfile_complaints,
14287 _("Variable in common block has "
14288 "DW_AT_data_member_location "
14289 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
14290 child_die->offset.sect_off,
14291 objfile_name (cu->objfile));
0971de02
TT
14292
14293 if (attr_form_is_section_offset (member_loc))
14294 dwarf2_complex_location_expr_complaint ();
14295 else if (attr_form_is_constant (member_loc)
14296 || attr_form_is_block (member_loc))
14297 {
14298 if (attr)
14299 mark_common_block_symbol_computed (sym, die, attr,
14300 member_loc, cu);
14301 }
14302 else
14303 dwarf2_complex_location_expr_complaint ();
14304 }
14305 }
c906108c 14306 }
4357ac6c
TT
14307
14308 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14309 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14310 }
14311}
14312
0114d602 14313/* Create a type for a C++ namespace. */
d9fa45fe 14314
0114d602
DJ
14315static struct type *
14316read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14317{
e7c27a73 14318 struct objfile *objfile = cu->objfile;
0114d602 14319 const char *previous_prefix, *name;
9219021c 14320 int is_anonymous;
0114d602
DJ
14321 struct type *type;
14322
14323 /* For extensions, reuse the type of the original namespace. */
14324 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14325 {
14326 struct die_info *ext_die;
14327 struct dwarf2_cu *ext_cu = cu;
9a619af0 14328
0114d602
DJ
14329 ext_die = dwarf2_extension (die, &ext_cu);
14330 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14331
14332 /* EXT_CU may not be the same as CU.
02142a6c 14333 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14334 return set_die_type (die, type, cu);
14335 }
9219021c 14336
e142c38c 14337 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14338
14339 /* Now build the name of the current namespace. */
14340
0114d602
DJ
14341 previous_prefix = determine_prefix (die, cu);
14342 if (previous_prefix[0] != '\0')
14343 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14344 previous_prefix, name, 0, cu);
0114d602
DJ
14345
14346 /* Create the type. */
19f392bc 14347 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14348 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14349
60531b24 14350 return set_die_type (die, type, cu);
0114d602
DJ
14351}
14352
22cee43f 14353/* Read a namespace scope. */
0114d602
DJ
14354
14355static void
14356read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14357{
14358 struct objfile *objfile = cu->objfile;
0114d602 14359 int is_anonymous;
9219021c 14360
5c4e30ca
DC
14361 /* Add a symbol associated to this if we haven't seen the namespace
14362 before. Also, add a using directive if it's an anonymous
14363 namespace. */
9219021c 14364
f2f0e013 14365 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14366 {
14367 struct type *type;
14368
0114d602 14369 type = read_type_die (die, cu);
e7c27a73 14370 new_symbol (die, type, cu);
5c4e30ca 14371
e8e80198 14372 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14373 if (is_anonymous)
0114d602
DJ
14374 {
14375 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14376
22cee43f
PMR
14377 add_using_directive (using_directives (cu->language),
14378 previous_prefix, TYPE_NAME (type), NULL,
14379 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14380 }
5c4e30ca 14381 }
9219021c 14382
639d11d3 14383 if (die->child != NULL)
d9fa45fe 14384 {
639d11d3 14385 struct die_info *child_die = die->child;
6e70227d 14386
d9fa45fe
DC
14387 while (child_die && child_die->tag)
14388 {
e7c27a73 14389 process_die (child_die, cu);
d9fa45fe
DC
14390 child_die = sibling_die (child_die);
14391 }
14392 }
38d518c9
EZ
14393}
14394
f55ee35c
JK
14395/* Read a Fortran module as type. This DIE can be only a declaration used for
14396 imported module. Still we need that type as local Fortran "use ... only"
14397 declaration imports depend on the created type in determine_prefix. */
14398
14399static struct type *
14400read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14401{
14402 struct objfile *objfile = cu->objfile;
15d034d0 14403 const char *module_name;
f55ee35c
JK
14404 struct type *type;
14405
14406 module_name = dwarf2_name (die, cu);
14407 if (!module_name)
3e43a32a
MS
14408 complaint (&symfile_complaints,
14409 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 14410 die->offset.sect_off);
19f392bc 14411 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14412
14413 /* determine_prefix uses TYPE_TAG_NAME. */
14414 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14415
14416 return set_die_type (die, type, cu);
14417}
14418
5d7cb8df
JK
14419/* Read a Fortran module. */
14420
14421static void
14422read_module (struct die_info *die, struct dwarf2_cu *cu)
14423{
14424 struct die_info *child_die = die->child;
530e8392
KB
14425 struct type *type;
14426
14427 type = read_type_die (die, cu);
14428 new_symbol (die, type, cu);
5d7cb8df 14429
5d7cb8df
JK
14430 while (child_die && child_die->tag)
14431 {
14432 process_die (child_die, cu);
14433 child_die = sibling_die (child_die);
14434 }
14435}
14436
38d518c9
EZ
14437/* Return the name of the namespace represented by DIE. Set
14438 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14439 namespace. */
14440
14441static const char *
e142c38c 14442namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14443{
14444 struct die_info *current_die;
14445 const char *name = NULL;
14446
14447 /* Loop through the extensions until we find a name. */
14448
14449 for (current_die = die;
14450 current_die != NULL;
f2f0e013 14451 current_die = dwarf2_extension (die, &cu))
38d518c9 14452 {
96553a0c
DE
14453 /* We don't use dwarf2_name here so that we can detect the absence
14454 of a name -> anonymous namespace. */
7d45c7c3 14455 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14456
38d518c9
EZ
14457 if (name != NULL)
14458 break;
14459 }
14460
14461 /* Is it an anonymous namespace? */
14462
14463 *is_anonymous = (name == NULL);
14464 if (*is_anonymous)
2b1dbab0 14465 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14466
14467 return name;
d9fa45fe
DC
14468}
14469
c906108c
SS
14470/* Extract all information from a DW_TAG_pointer_type DIE and add to
14471 the user defined type vector. */
14472
f792889a 14473static struct type *
e7c27a73 14474read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14475{
5e2b427d 14476 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14477 struct comp_unit_head *cu_header = &cu->header;
c906108c 14478 struct type *type;
8b2dbe47
KB
14479 struct attribute *attr_byte_size;
14480 struct attribute *attr_address_class;
14481 int byte_size, addr_class;
7e314c57
JK
14482 struct type *target_type;
14483
14484 target_type = die_type (die, cu);
c906108c 14485
7e314c57
JK
14486 /* The die_type call above may have already set the type for this DIE. */
14487 type = get_die_type (die, cu);
14488 if (type)
14489 return type;
14490
14491 type = lookup_pointer_type (target_type);
8b2dbe47 14492
e142c38c 14493 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14494 if (attr_byte_size)
14495 byte_size = DW_UNSND (attr_byte_size);
c906108c 14496 else
8b2dbe47
KB
14497 byte_size = cu_header->addr_size;
14498
e142c38c 14499 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14500 if (attr_address_class)
14501 addr_class = DW_UNSND (attr_address_class);
14502 else
14503 addr_class = DW_ADDR_none;
14504
14505 /* If the pointer size or address class is different than the
14506 default, create a type variant marked as such and set the
14507 length accordingly. */
14508 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14509 {
5e2b427d 14510 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14511 {
14512 int type_flags;
14513
849957d9 14514 type_flags = gdbarch_address_class_type_flags
5e2b427d 14515 (gdbarch, byte_size, addr_class);
876cecd0
TT
14516 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14517 == 0);
8b2dbe47
KB
14518 type = make_type_with_address_space (type, type_flags);
14519 }
14520 else if (TYPE_LENGTH (type) != byte_size)
14521 {
3e43a32a
MS
14522 complaint (&symfile_complaints,
14523 _("invalid pointer size %d"), byte_size);
8b2dbe47 14524 }
6e70227d 14525 else
9a619af0
MS
14526 {
14527 /* Should we also complain about unhandled address classes? */
14528 }
c906108c 14529 }
8b2dbe47
KB
14530
14531 TYPE_LENGTH (type) = byte_size;
f792889a 14532 return set_die_type (die, type, cu);
c906108c
SS
14533}
14534
14535/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14536 the user defined type vector. */
14537
f792889a 14538static struct type *
e7c27a73 14539read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14540{
14541 struct type *type;
14542 struct type *to_type;
14543 struct type *domain;
14544
e7c27a73
DJ
14545 to_type = die_type (die, cu);
14546 domain = die_containing_type (die, cu);
0d5de010 14547
7e314c57
JK
14548 /* The calls above may have already set the type for this DIE. */
14549 type = get_die_type (die, cu);
14550 if (type)
14551 return type;
14552
0d5de010
DJ
14553 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14554 type = lookup_methodptr_type (to_type);
7078baeb
TT
14555 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14556 {
14557 struct type *new_type = alloc_type (cu->objfile);
14558
14559 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14560 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14561 TYPE_VARARGS (to_type));
14562 type = lookup_methodptr_type (new_type);
14563 }
0d5de010
DJ
14564 else
14565 type = lookup_memberptr_type (to_type, domain);
c906108c 14566
f792889a 14567 return set_die_type (die, type, cu);
c906108c
SS
14568}
14569
14570/* Extract all information from a DW_TAG_reference_type DIE and add to
14571 the user defined type vector. */
14572
f792889a 14573static struct type *
e7c27a73 14574read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14575{
e7c27a73 14576 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14577 struct type *type, *target_type;
c906108c
SS
14578 struct attribute *attr;
14579
7e314c57
JK
14580 target_type = die_type (die, cu);
14581
14582 /* The die_type call above may have already set the type for this DIE. */
14583 type = get_die_type (die, cu);
14584 if (type)
14585 return type;
14586
3b224330 14587 type = lookup_lvalue_reference_type (target_type);
e142c38c 14588 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14589 if (attr)
14590 {
14591 TYPE_LENGTH (type) = DW_UNSND (attr);
14592 }
14593 else
14594 {
107d2387 14595 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14596 }
f792889a 14597 return set_die_type (die, type, cu);
c906108c
SS
14598}
14599
cf363f18
MW
14600/* Add the given cv-qualifiers to the element type of the array. GCC
14601 outputs DWARF type qualifiers that apply to an array, not the
14602 element type. But GDB relies on the array element type to carry
14603 the cv-qualifiers. This mimics section 6.7.3 of the C99
14604 specification. */
14605
14606static struct type *
14607add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14608 struct type *base_type, int cnst, int voltl)
14609{
14610 struct type *el_type, *inner_array;
14611
14612 base_type = copy_type (base_type);
14613 inner_array = base_type;
14614
14615 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14616 {
14617 TYPE_TARGET_TYPE (inner_array) =
14618 copy_type (TYPE_TARGET_TYPE (inner_array));
14619 inner_array = TYPE_TARGET_TYPE (inner_array);
14620 }
14621
14622 el_type = TYPE_TARGET_TYPE (inner_array);
14623 cnst |= TYPE_CONST (el_type);
14624 voltl |= TYPE_VOLATILE (el_type);
14625 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14626
14627 return set_die_type (die, base_type, cu);
14628}
14629
f792889a 14630static struct type *
e7c27a73 14631read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14632{
f792889a 14633 struct type *base_type, *cv_type;
c906108c 14634
e7c27a73 14635 base_type = die_type (die, cu);
7e314c57
JK
14636
14637 /* The die_type call above may have already set the type for this DIE. */
14638 cv_type = get_die_type (die, cu);
14639 if (cv_type)
14640 return cv_type;
14641
2f608a3a
KW
14642 /* In case the const qualifier is applied to an array type, the element type
14643 is so qualified, not the array type (section 6.7.3 of C99). */
14644 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14645 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14646
f792889a
DJ
14647 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14648 return set_die_type (die, cv_type, cu);
c906108c
SS
14649}
14650
f792889a 14651static struct type *
e7c27a73 14652read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14653{
f792889a 14654 struct type *base_type, *cv_type;
c906108c 14655
e7c27a73 14656 base_type = die_type (die, cu);
7e314c57
JK
14657
14658 /* The die_type call above may have already set the type for this DIE. */
14659 cv_type = get_die_type (die, cu);
14660 if (cv_type)
14661 return cv_type;
14662
cf363f18
MW
14663 /* In case the volatile qualifier is applied to an array type, the
14664 element type is so qualified, not the array type (section 6.7.3
14665 of C99). */
14666 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14667 return add_array_cv_type (die, cu, base_type, 0, 1);
14668
f792889a
DJ
14669 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14670 return set_die_type (die, cv_type, cu);
c906108c
SS
14671}
14672
06d66ee9
TT
14673/* Handle DW_TAG_restrict_type. */
14674
14675static struct type *
14676read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14677{
14678 struct type *base_type, *cv_type;
14679
14680 base_type = die_type (die, cu);
14681
14682 /* The die_type call above may have already set the type for this DIE. */
14683 cv_type = get_die_type (die, cu);
14684 if (cv_type)
14685 return cv_type;
14686
14687 cv_type = make_restrict_type (base_type);
14688 return set_die_type (die, cv_type, cu);
14689}
14690
a2c2acaf
MW
14691/* Handle DW_TAG_atomic_type. */
14692
14693static struct type *
14694read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14695{
14696 struct type *base_type, *cv_type;
14697
14698 base_type = die_type (die, cu);
14699
14700 /* The die_type call above may have already set the type for this DIE. */
14701 cv_type = get_die_type (die, cu);
14702 if (cv_type)
14703 return cv_type;
14704
14705 cv_type = make_atomic_type (base_type);
14706 return set_die_type (die, cv_type, cu);
14707}
14708
c906108c
SS
14709/* Extract all information from a DW_TAG_string_type DIE and add to
14710 the user defined type vector. It isn't really a user defined type,
14711 but it behaves like one, with other DIE's using an AT_user_def_type
14712 attribute to reference it. */
14713
f792889a 14714static struct type *
e7c27a73 14715read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14716{
e7c27a73 14717 struct objfile *objfile = cu->objfile;
3b7538c0 14718 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14719 struct type *type, *range_type, *index_type, *char_type;
14720 struct attribute *attr;
14721 unsigned int length;
14722
e142c38c 14723 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14724 if (attr)
14725 {
14726 length = DW_UNSND (attr);
14727 }
14728 else
14729 {
0963b4bd 14730 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14731 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14732 if (attr)
14733 {
14734 length = DW_UNSND (attr);
14735 }
14736 else
14737 {
14738 length = 1;
14739 }
c906108c 14740 }
6ccb9162 14741
46bf5051 14742 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14743 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14744 char_type = language_string_char_type (cu->language_defn, gdbarch);
14745 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14746
f792889a 14747 return set_die_type (die, type, cu);
c906108c
SS
14748}
14749
4d804846
JB
14750/* Assuming that DIE corresponds to a function, returns nonzero
14751 if the function is prototyped. */
14752
14753static int
14754prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14755{
14756 struct attribute *attr;
14757
14758 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14759 if (attr && (DW_UNSND (attr) != 0))
14760 return 1;
14761
14762 /* The DWARF standard implies that the DW_AT_prototyped attribute
14763 is only meaninful for C, but the concept also extends to other
14764 languages that allow unprototyped functions (Eg: Objective C).
14765 For all other languages, assume that functions are always
14766 prototyped. */
14767 if (cu->language != language_c
14768 && cu->language != language_objc
14769 && cu->language != language_opencl)
14770 return 1;
14771
14772 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14773 prototyped and unprototyped functions; default to prototyped,
14774 since that is more common in modern code (and RealView warns
14775 about unprototyped functions). */
14776 if (producer_is_realview (cu->producer))
14777 return 1;
14778
14779 return 0;
14780}
14781
c906108c
SS
14782/* Handle DIES due to C code like:
14783
14784 struct foo
c5aa993b
JM
14785 {
14786 int (*funcp)(int a, long l);
14787 int b;
14788 };
c906108c 14789
0963b4bd 14790 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14791
f792889a 14792static struct type *
e7c27a73 14793read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14794{
bb5ed363 14795 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14796 struct type *type; /* Type that this function returns. */
14797 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14798 struct attribute *attr;
14799
e7c27a73 14800 type = die_type (die, cu);
7e314c57
JK
14801
14802 /* The die_type call above may have already set the type for this DIE. */
14803 ftype = get_die_type (die, cu);
14804 if (ftype)
14805 return ftype;
14806
0c8b41f1 14807 ftype = lookup_function_type (type);
c906108c 14808
4d804846 14809 if (prototyped_function_p (die, cu))
a6c727b2 14810 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14811
c055b101
CV
14812 /* Store the calling convention in the type if it's available in
14813 the subroutine die. Otherwise set the calling convention to
14814 the default value DW_CC_normal. */
14815 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14816 if (attr)
14817 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14818 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14819 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14820 else
14821 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14822
743649fd
MW
14823 /* Record whether the function returns normally to its caller or not
14824 if the DWARF producer set that information. */
14825 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14826 if (attr && (DW_UNSND (attr) != 0))
14827 TYPE_NO_RETURN (ftype) = 1;
14828
76c10ea2
GM
14829 /* We need to add the subroutine type to the die immediately so
14830 we don't infinitely recurse when dealing with parameters
0963b4bd 14831 declared as the same subroutine type. */
76c10ea2 14832 set_die_type (die, ftype, cu);
6e70227d 14833
639d11d3 14834 if (die->child != NULL)
c906108c 14835 {
bb5ed363 14836 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14837 struct die_info *child_die;
8072405b 14838 int nparams, iparams;
c906108c
SS
14839
14840 /* Count the number of parameters.
14841 FIXME: GDB currently ignores vararg functions, but knows about
14842 vararg member functions. */
8072405b 14843 nparams = 0;
639d11d3 14844 child_die = die->child;
c906108c
SS
14845 while (child_die && child_die->tag)
14846 {
14847 if (child_die->tag == DW_TAG_formal_parameter)
14848 nparams++;
14849 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14850 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14851 child_die = sibling_die (child_die);
14852 }
14853
14854 /* Allocate storage for parameters and fill them in. */
14855 TYPE_NFIELDS (ftype) = nparams;
14856 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14857 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14858
8072405b
JK
14859 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14860 even if we error out during the parameters reading below. */
14861 for (iparams = 0; iparams < nparams; iparams++)
14862 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14863
14864 iparams = 0;
639d11d3 14865 child_die = die->child;
c906108c
SS
14866 while (child_die && child_die->tag)
14867 {
14868 if (child_die->tag == DW_TAG_formal_parameter)
14869 {
3ce3b1ba
PA
14870 struct type *arg_type;
14871
14872 /* DWARF version 2 has no clean way to discern C++
14873 static and non-static member functions. G++ helps
14874 GDB by marking the first parameter for non-static
14875 member functions (which is the this pointer) as
14876 artificial. We pass this information to
14877 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14878
14879 DWARF version 3 added DW_AT_object_pointer, which GCC
14880 4.5 does not yet generate. */
e142c38c 14881 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14882 if (attr)
14883 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14884 else
9c37b5ae 14885 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
14886 arg_type = die_type (child_die, cu);
14887
14888 /* RealView does not mark THIS as const, which the testsuite
14889 expects. GCC marks THIS as const in method definitions,
14890 but not in the class specifications (GCC PR 43053). */
14891 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14892 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14893 {
14894 int is_this = 0;
14895 struct dwarf2_cu *arg_cu = cu;
14896 const char *name = dwarf2_name (child_die, cu);
14897
14898 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14899 if (attr)
14900 {
14901 /* If the compiler emits this, use it. */
14902 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14903 is_this = 1;
14904 }
14905 else if (name && strcmp (name, "this") == 0)
14906 /* Function definitions will have the argument names. */
14907 is_this = 1;
14908 else if (name == NULL && iparams == 0)
14909 /* Declarations may not have the names, so like
14910 elsewhere in GDB, assume an artificial first
14911 argument is "this". */
14912 is_this = 1;
14913
14914 if (is_this)
14915 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14916 arg_type, 0);
14917 }
14918
14919 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14920 iparams++;
14921 }
14922 child_die = sibling_die (child_die);
14923 }
14924 }
14925
76c10ea2 14926 return ftype;
c906108c
SS
14927}
14928
f792889a 14929static struct type *
e7c27a73 14930read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14931{
e7c27a73 14932 struct objfile *objfile = cu->objfile;
0114d602 14933 const char *name = NULL;
3c8e0968 14934 struct type *this_type, *target_type;
c906108c 14935
94af9270 14936 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
14937 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14938 TYPE_TARGET_STUB (this_type) = 1;
f792889a 14939 set_die_type (die, this_type, cu);
3c8e0968
DE
14940 target_type = die_type (die, cu);
14941 if (target_type != this_type)
14942 TYPE_TARGET_TYPE (this_type) = target_type;
14943 else
14944 {
14945 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14946 spec and cause infinite loops in GDB. */
14947 complaint (&symfile_complaints,
14948 _("Self-referential DW_TAG_typedef "
14949 "- DIE at 0x%x [in module %s]"),
4262abfb 14950 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
14951 TYPE_TARGET_TYPE (this_type) = NULL;
14952 }
f792889a 14953 return this_type;
c906108c
SS
14954}
14955
9b790ce7
UW
14956/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14957 (which may be different from NAME) to the architecture back-end to allow
14958 it to guess the correct format if necessary. */
14959
14960static struct type *
14961dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14962 const char *name_hint)
14963{
14964 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14965 const struct floatformat **format;
14966 struct type *type;
14967
14968 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14969 if (format)
14970 type = init_float_type (objfile, bits, name, format);
14971 else
14972 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14973
14974 return type;
14975}
14976
c906108c
SS
14977/* Find a representation of a given base type and install
14978 it in the TYPE field of the die. */
14979
f792889a 14980static struct type *
e7c27a73 14981read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14982{
e7c27a73 14983 struct objfile *objfile = cu->objfile;
c906108c
SS
14984 struct type *type;
14985 struct attribute *attr;
19f392bc 14986 int encoding = 0, bits = 0;
15d034d0 14987 const char *name;
c906108c 14988
e142c38c 14989 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
14990 if (attr)
14991 {
14992 encoding = DW_UNSND (attr);
14993 }
e142c38c 14994 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14995 if (attr)
14996 {
19f392bc 14997 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 14998 }
39cbfefa 14999 name = dwarf2_name (die, cu);
6ccb9162 15000 if (!name)
c906108c 15001 {
6ccb9162
UW
15002 complaint (&symfile_complaints,
15003 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15004 }
6ccb9162
UW
15005
15006 switch (encoding)
c906108c 15007 {
6ccb9162
UW
15008 case DW_ATE_address:
15009 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15010 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15011 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15012 break;
15013 case DW_ATE_boolean:
19f392bc 15014 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15015 break;
15016 case DW_ATE_complex_float:
9b790ce7 15017 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15018 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15019 break;
15020 case DW_ATE_decimal_float:
19f392bc 15021 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15022 break;
15023 case DW_ATE_float:
9b790ce7 15024 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15025 break;
15026 case DW_ATE_signed:
19f392bc 15027 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15028 break;
15029 case DW_ATE_unsigned:
3b2b8fea
TT
15030 if (cu->language == language_fortran
15031 && name
61012eef 15032 && startswith (name, "character("))
19f392bc
UW
15033 type = init_character_type (objfile, bits, 1, name);
15034 else
15035 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15036 break;
15037 case DW_ATE_signed_char:
6e70227d 15038 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15039 || cu->language == language_pascal
15040 || cu->language == language_fortran)
19f392bc
UW
15041 type = init_character_type (objfile, bits, 0, name);
15042 else
15043 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15044 break;
15045 case DW_ATE_unsigned_char:
868a0084 15046 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15047 || cu->language == language_pascal
c44af4eb
TT
15048 || cu->language == language_fortran
15049 || cu->language == language_rust)
19f392bc
UW
15050 type = init_character_type (objfile, bits, 1, name);
15051 else
15052 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15053 break;
75079b2b
TT
15054 case DW_ATE_UTF:
15055 /* We just treat this as an integer and then recognize the
15056 type by name elsewhere. */
19f392bc 15057 type = init_integer_type (objfile, bits, 0, name);
75079b2b
TT
15058 break;
15059
6ccb9162
UW
15060 default:
15061 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15062 dwarf_type_encoding_name (encoding));
19f392bc
UW
15063 type = init_type (objfile, TYPE_CODE_ERROR,
15064 bits / TARGET_CHAR_BIT, name);
6ccb9162 15065 break;
c906108c 15066 }
6ccb9162 15067
0114d602 15068 if (name && strcmp (name, "char") == 0)
876cecd0 15069 TYPE_NOSIGN (type) = 1;
0114d602 15070
f792889a 15071 return set_die_type (die, type, cu);
c906108c
SS
15072}
15073
80180f79
SA
15074/* Parse dwarf attribute if it's a block, reference or constant and put the
15075 resulting value of the attribute into struct bound_prop.
15076 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15077
15078static int
15079attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15080 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15081{
15082 struct dwarf2_property_baton *baton;
15083 struct obstack *obstack = &cu->objfile->objfile_obstack;
15084
15085 if (attr == NULL || prop == NULL)
15086 return 0;
15087
15088 if (attr_form_is_block (attr))
15089 {
8d749320 15090 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15091 baton->referenced_type = NULL;
15092 baton->locexpr.per_cu = cu->per_cu;
15093 baton->locexpr.size = DW_BLOCK (attr)->size;
15094 baton->locexpr.data = DW_BLOCK (attr)->data;
15095 prop->data.baton = baton;
15096 prop->kind = PROP_LOCEXPR;
15097 gdb_assert (prop->data.baton != NULL);
15098 }
15099 else if (attr_form_is_ref (attr))
15100 {
15101 struct dwarf2_cu *target_cu = cu;
15102 struct die_info *target_die;
15103 struct attribute *target_attr;
15104
15105 target_die = follow_die_ref (die, attr, &target_cu);
15106 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15107 if (target_attr == NULL)
15108 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15109 target_cu);
80180f79
SA
15110 if (target_attr == NULL)
15111 return 0;
15112
df25ebbd 15113 switch (target_attr->name)
80180f79 15114 {
df25ebbd
JB
15115 case DW_AT_location:
15116 if (attr_form_is_section_offset (target_attr))
15117 {
8d749320 15118 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15119 baton->referenced_type = die_type (target_die, target_cu);
15120 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15121 prop->data.baton = baton;
15122 prop->kind = PROP_LOCLIST;
15123 gdb_assert (prop->data.baton != NULL);
15124 }
15125 else if (attr_form_is_block (target_attr))
15126 {
8d749320 15127 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15128 baton->referenced_type = die_type (target_die, target_cu);
15129 baton->locexpr.per_cu = cu->per_cu;
15130 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15131 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15132 prop->data.baton = baton;
15133 prop->kind = PROP_LOCEXPR;
15134 gdb_assert (prop->data.baton != NULL);
15135 }
15136 else
15137 {
15138 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15139 "dynamic property");
15140 return 0;
15141 }
15142 break;
15143 case DW_AT_data_member_location:
15144 {
15145 LONGEST offset;
15146
15147 if (!handle_data_member_location (target_die, target_cu,
15148 &offset))
15149 return 0;
15150
8d749320 15151 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15152 baton->referenced_type = read_type_die (target_die->parent,
15153 target_cu);
df25ebbd
JB
15154 baton->offset_info.offset = offset;
15155 baton->offset_info.type = die_type (target_die, target_cu);
15156 prop->data.baton = baton;
15157 prop->kind = PROP_ADDR_OFFSET;
15158 break;
15159 }
80180f79
SA
15160 }
15161 }
15162 else if (attr_form_is_constant (attr))
15163 {
15164 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15165 prop->kind = PROP_CONST;
15166 }
15167 else
15168 {
15169 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15170 dwarf2_name (die, cu));
15171 return 0;
15172 }
15173
15174 return 1;
15175}
15176
a02abb62
JB
15177/* Read the given DW_AT_subrange DIE. */
15178
f792889a 15179static struct type *
a02abb62
JB
15180read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15181{
4c9ad8c2 15182 struct type *base_type, *orig_base_type;
a02abb62
JB
15183 struct type *range_type;
15184 struct attribute *attr;
729efb13 15185 struct dynamic_prop low, high;
4fae6e18 15186 int low_default_is_valid;
c451ebe5 15187 int high_bound_is_count = 0;
15d034d0 15188 const char *name;
43bbcdc2 15189 LONGEST negative_mask;
e77813c8 15190
4c9ad8c2
TT
15191 orig_base_type = die_type (die, cu);
15192 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15193 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15194 creating the range type, but we use the result of check_typedef
15195 when examining properties of the type. */
15196 base_type = check_typedef (orig_base_type);
a02abb62 15197
7e314c57
JK
15198 /* The die_type call above may have already set the type for this DIE. */
15199 range_type = get_die_type (die, cu);
15200 if (range_type)
15201 return range_type;
15202
729efb13
SA
15203 low.kind = PROP_CONST;
15204 high.kind = PROP_CONST;
15205 high.data.const_val = 0;
15206
4fae6e18
JK
15207 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15208 omitting DW_AT_lower_bound. */
15209 switch (cu->language)
6e70227d 15210 {
4fae6e18
JK
15211 case language_c:
15212 case language_cplus:
729efb13 15213 low.data.const_val = 0;
4fae6e18
JK
15214 low_default_is_valid = 1;
15215 break;
15216 case language_fortran:
729efb13 15217 low.data.const_val = 1;
4fae6e18
JK
15218 low_default_is_valid = 1;
15219 break;
15220 case language_d:
4fae6e18 15221 case language_objc:
c44af4eb 15222 case language_rust:
729efb13 15223 low.data.const_val = 0;
4fae6e18
JK
15224 low_default_is_valid = (cu->header.version >= 4);
15225 break;
15226 case language_ada:
15227 case language_m2:
15228 case language_pascal:
729efb13 15229 low.data.const_val = 1;
4fae6e18
JK
15230 low_default_is_valid = (cu->header.version >= 4);
15231 break;
15232 default:
729efb13 15233 low.data.const_val = 0;
4fae6e18
JK
15234 low_default_is_valid = 0;
15235 break;
a02abb62
JB
15236 }
15237
e142c38c 15238 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15239 if (attr)
11c1ba78 15240 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15241 else if (!low_default_is_valid)
15242 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15243 "- DIE at 0x%x [in module %s]"),
4262abfb 15244 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 15245
e142c38c 15246 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15247 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15248 {
15249 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15250 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15251 {
c451ebe5
SA
15252 /* If bounds are constant do the final calculation here. */
15253 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15254 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15255 else
15256 high_bound_is_count = 1;
c2ff108b 15257 }
e77813c8
PM
15258 }
15259
15260 /* Dwarf-2 specifications explicitly allows to create subrange types
15261 without specifying a base type.
15262 In that case, the base type must be set to the type of
15263 the lower bound, upper bound or count, in that order, if any of these
15264 three attributes references an object that has a type.
15265 If no base type is found, the Dwarf-2 specifications say that
15266 a signed integer type of size equal to the size of an address should
15267 be used.
15268 For the following C code: `extern char gdb_int [];'
15269 GCC produces an empty range DIE.
15270 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15271 high bound or count are not yet handled by this code. */
e77813c8
PM
15272 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15273 {
15274 struct objfile *objfile = cu->objfile;
15275 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15276 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15277 struct type *int_type = objfile_type (objfile)->builtin_int;
15278
15279 /* Test "int", "long int", and "long long int" objfile types,
15280 and select the first one having a size above or equal to the
15281 architecture address size. */
15282 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15283 base_type = int_type;
15284 else
15285 {
15286 int_type = objfile_type (objfile)->builtin_long;
15287 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15288 base_type = int_type;
15289 else
15290 {
15291 int_type = objfile_type (objfile)->builtin_long_long;
15292 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15293 base_type = int_type;
15294 }
15295 }
15296 }
a02abb62 15297
dbb9c2b1
JB
15298 /* Normally, the DWARF producers are expected to use a signed
15299 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15300 But this is unfortunately not always the case, as witnessed
15301 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15302 is used instead. To work around that ambiguity, we treat
15303 the bounds as signed, and thus sign-extend their values, when
15304 the base type is signed. */
6e70227d 15305 negative_mask =
66c6502d 15306 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15307 if (low.kind == PROP_CONST
15308 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15309 low.data.const_val |= negative_mask;
15310 if (high.kind == PROP_CONST
15311 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15312 high.data.const_val |= negative_mask;
43bbcdc2 15313
729efb13 15314 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15315
c451ebe5
SA
15316 if (high_bound_is_count)
15317 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15318
c2ff108b
JK
15319 /* Ada expects an empty array on no boundary attributes. */
15320 if (attr == NULL && cu->language != language_ada)
729efb13 15321 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15322
39cbfefa
DJ
15323 name = dwarf2_name (die, cu);
15324 if (name)
15325 TYPE_NAME (range_type) = name;
6e70227d 15326
e142c38c 15327 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15328 if (attr)
15329 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15330
7e314c57
JK
15331 set_die_type (die, range_type, cu);
15332
15333 /* set_die_type should be already done. */
b4ba55a1
JB
15334 set_descriptive_type (range_type, die, cu);
15335
7e314c57 15336 return range_type;
a02abb62 15337}
6e70227d 15338
f792889a 15339static struct type *
81a17f79
JB
15340read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15341{
15342 struct type *type;
81a17f79 15343
81a17f79
JB
15344 /* For now, we only support the C meaning of an unspecified type: void. */
15345
19f392bc 15346 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15347 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15348
f792889a 15349 return set_die_type (die, type, cu);
81a17f79 15350}
a02abb62 15351
639d11d3
DC
15352/* Read a single die and all its descendents. Set the die's sibling
15353 field to NULL; set other fields in the die correctly, and set all
15354 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15355 location of the info_ptr after reading all of those dies. PARENT
15356 is the parent of the die in question. */
15357
15358static struct die_info *
dee91e82 15359read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15360 const gdb_byte *info_ptr,
15361 const gdb_byte **new_info_ptr,
dee91e82 15362 struct die_info *parent)
639d11d3
DC
15363{
15364 struct die_info *die;
d521ce57 15365 const gdb_byte *cur_ptr;
639d11d3
DC
15366 int has_children;
15367
bf6af496 15368 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15369 if (die == NULL)
15370 {
15371 *new_info_ptr = cur_ptr;
15372 return NULL;
15373 }
93311388 15374 store_in_ref_table (die, reader->cu);
639d11d3
DC
15375
15376 if (has_children)
bf6af496 15377 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15378 else
15379 {
15380 die->child = NULL;
15381 *new_info_ptr = cur_ptr;
15382 }
15383
15384 die->sibling = NULL;
15385 die->parent = parent;
15386 return die;
15387}
15388
15389/* Read a die, all of its descendents, and all of its siblings; set
15390 all of the fields of all of the dies correctly. Arguments are as
15391 in read_die_and_children. */
15392
15393static struct die_info *
bf6af496 15394read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15395 const gdb_byte *info_ptr,
15396 const gdb_byte **new_info_ptr,
bf6af496 15397 struct die_info *parent)
639d11d3
DC
15398{
15399 struct die_info *first_die, *last_sibling;
d521ce57 15400 const gdb_byte *cur_ptr;
639d11d3 15401
c906108c 15402 cur_ptr = info_ptr;
639d11d3
DC
15403 first_die = last_sibling = NULL;
15404
15405 while (1)
c906108c 15406 {
639d11d3 15407 struct die_info *die
dee91e82 15408 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15409
1d325ec1 15410 if (die == NULL)
c906108c 15411 {
639d11d3
DC
15412 *new_info_ptr = cur_ptr;
15413 return first_die;
c906108c 15414 }
1d325ec1
DJ
15415
15416 if (!first_die)
15417 first_die = die;
c906108c 15418 else
1d325ec1
DJ
15419 last_sibling->sibling = die;
15420
15421 last_sibling = die;
c906108c 15422 }
c906108c
SS
15423}
15424
bf6af496
DE
15425/* Read a die, all of its descendents, and all of its siblings; set
15426 all of the fields of all of the dies correctly. Arguments are as
15427 in read_die_and_children.
15428 This the main entry point for reading a DIE and all its children. */
15429
15430static struct die_info *
15431read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15432 const gdb_byte *info_ptr,
15433 const gdb_byte **new_info_ptr,
bf6af496
DE
15434 struct die_info *parent)
15435{
15436 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15437 new_info_ptr, parent);
15438
b4f54984 15439 if (dwarf_die_debug)
bf6af496
DE
15440 {
15441 fprintf_unfiltered (gdb_stdlog,
15442 "Read die from %s@0x%x of %s:\n",
a32a8923 15443 get_section_name (reader->die_section),
bf6af496
DE
15444 (unsigned) (info_ptr - reader->die_section->buffer),
15445 bfd_get_filename (reader->abfd));
b4f54984 15446 dump_die (die, dwarf_die_debug);
bf6af496
DE
15447 }
15448
15449 return die;
15450}
15451
3019eac3
DE
15452/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15453 attributes.
15454 The caller is responsible for filling in the extra attributes
15455 and updating (*DIEP)->num_attrs.
15456 Set DIEP to point to a newly allocated die with its information,
15457 except for its child, sibling, and parent fields.
15458 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15459
d521ce57 15460static const gdb_byte *
3019eac3 15461read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15462 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15463 int *has_children, int num_extra_attrs)
93311388 15464{
b64f50a1
JK
15465 unsigned int abbrev_number, bytes_read, i;
15466 sect_offset offset;
93311388
DE
15467 struct abbrev_info *abbrev;
15468 struct die_info *die;
15469 struct dwarf2_cu *cu = reader->cu;
15470 bfd *abfd = reader->abfd;
15471
b64f50a1 15472 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
15473 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15474 info_ptr += bytes_read;
15475 if (!abbrev_number)
15476 {
15477 *diep = NULL;
15478 *has_children = 0;
15479 return info_ptr;
15480 }
15481
433df2d4 15482 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15483 if (!abbrev)
348e048f
DE
15484 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15485 abbrev_number,
15486 bfd_get_filename (abfd));
15487
3019eac3 15488 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
15489 die->offset = offset;
15490 die->tag = abbrev->tag;
15491 die->abbrev = abbrev_number;
15492
3019eac3
DE
15493 /* Make the result usable.
15494 The caller needs to update num_attrs after adding the extra
15495 attributes. */
93311388
DE
15496 die->num_attrs = abbrev->num_attrs;
15497
15498 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15499 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15500 info_ptr);
93311388
DE
15501
15502 *diep = die;
15503 *has_children = abbrev->has_children;
15504 return info_ptr;
15505}
15506
3019eac3
DE
15507/* Read a die and all its attributes.
15508 Set DIEP to point to a newly allocated die with its information,
15509 except for its child, sibling, and parent fields.
15510 Set HAS_CHILDREN to tell whether the die has children or not. */
15511
d521ce57 15512static const gdb_byte *
3019eac3 15513read_full_die (const struct die_reader_specs *reader,
d521ce57 15514 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15515 int *has_children)
15516{
d521ce57 15517 const gdb_byte *result;
bf6af496
DE
15518
15519 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15520
b4f54984 15521 if (dwarf_die_debug)
bf6af496
DE
15522 {
15523 fprintf_unfiltered (gdb_stdlog,
15524 "Read die from %s@0x%x of %s:\n",
a32a8923 15525 get_section_name (reader->die_section),
bf6af496
DE
15526 (unsigned) (info_ptr - reader->die_section->buffer),
15527 bfd_get_filename (reader->abfd));
b4f54984 15528 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15529 }
15530
15531 return result;
3019eac3 15532}
433df2d4
DE
15533\f
15534/* Abbreviation tables.
3019eac3 15535
433df2d4 15536 In DWARF version 2, the description of the debugging information is
c906108c
SS
15537 stored in a separate .debug_abbrev section. Before we read any
15538 dies from a section we read in all abbreviations and install them
433df2d4
DE
15539 in a hash table. */
15540
15541/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15542
15543static struct abbrev_info *
15544abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15545{
15546 struct abbrev_info *abbrev;
15547
8d749320 15548 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15549 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15550
433df2d4
DE
15551 return abbrev;
15552}
15553
15554/* Add an abbreviation to the table. */
c906108c
SS
15555
15556static void
433df2d4
DE
15557abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15558 unsigned int abbrev_number,
15559 struct abbrev_info *abbrev)
15560{
15561 unsigned int hash_number;
15562
15563 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15564 abbrev->next = abbrev_table->abbrevs[hash_number];
15565 abbrev_table->abbrevs[hash_number] = abbrev;
15566}
dee91e82 15567
433df2d4
DE
15568/* Look up an abbrev in the table.
15569 Returns NULL if the abbrev is not found. */
15570
15571static struct abbrev_info *
15572abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15573 unsigned int abbrev_number)
c906108c 15574{
433df2d4
DE
15575 unsigned int hash_number;
15576 struct abbrev_info *abbrev;
15577
15578 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15579 abbrev = abbrev_table->abbrevs[hash_number];
15580
15581 while (abbrev)
15582 {
15583 if (abbrev->number == abbrev_number)
15584 return abbrev;
15585 abbrev = abbrev->next;
15586 }
15587 return NULL;
15588}
15589
15590/* Read in an abbrev table. */
15591
15592static struct abbrev_table *
15593abbrev_table_read_table (struct dwarf2_section_info *section,
15594 sect_offset offset)
15595{
15596 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15597 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15598 struct abbrev_table *abbrev_table;
d521ce57 15599 const gdb_byte *abbrev_ptr;
c906108c
SS
15600 struct abbrev_info *cur_abbrev;
15601 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15602 unsigned int abbrev_form;
f3dd6933
DJ
15603 struct attr_abbrev *cur_attrs;
15604 unsigned int allocated_attrs;
c906108c 15605
70ba0933 15606 abbrev_table = XNEW (struct abbrev_table);
f4dc4d17 15607 abbrev_table->offset = offset;
433df2d4 15608 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15609 abbrev_table->abbrevs =
15610 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15611 ABBREV_HASH_SIZE);
433df2d4
DE
15612 memset (abbrev_table->abbrevs, 0,
15613 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15614
433df2d4
DE
15615 dwarf2_read_section (objfile, section);
15616 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
15617 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15618 abbrev_ptr += bytes_read;
15619
f3dd6933 15620 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15621 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15622
0963b4bd 15623 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15624 while (abbrev_number)
15625 {
433df2d4 15626 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15627
15628 /* read in abbrev header */
15629 cur_abbrev->number = abbrev_number;
aead7601
SM
15630 cur_abbrev->tag
15631 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15632 abbrev_ptr += bytes_read;
15633 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15634 abbrev_ptr += 1;
15635
15636 /* now read in declarations */
22d2f3ab 15637 for (;;)
c906108c 15638 {
43988095
JK
15639 LONGEST implicit_const;
15640
22d2f3ab
JK
15641 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15642 abbrev_ptr += bytes_read;
15643 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15644 abbrev_ptr += bytes_read;
43988095
JK
15645 if (abbrev_form == DW_FORM_implicit_const)
15646 {
15647 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15648 &bytes_read);
15649 abbrev_ptr += bytes_read;
15650 }
15651 else
15652 {
15653 /* Initialize it due to a false compiler warning. */
15654 implicit_const = -1;
15655 }
22d2f3ab
JK
15656
15657 if (abbrev_name == 0)
15658 break;
15659
f3dd6933 15660 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15661 {
f3dd6933
DJ
15662 allocated_attrs += ATTR_ALLOC_CHUNK;
15663 cur_attrs
224c3ddb 15664 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15665 }
ae038cb0 15666
aead7601
SM
15667 cur_attrs[cur_abbrev->num_attrs].name
15668 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15669 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15670 = (enum dwarf_form) abbrev_form;
43988095 15671 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15672 ++cur_abbrev->num_attrs;
c906108c
SS
15673 }
15674
8d749320
SM
15675 cur_abbrev->attrs =
15676 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15677 cur_abbrev->num_attrs);
f3dd6933
DJ
15678 memcpy (cur_abbrev->attrs, cur_attrs,
15679 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15680
433df2d4 15681 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15682
15683 /* Get next abbreviation.
15684 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15685 always properly terminated with an abbrev number of 0.
15686 Exit loop if we encounter an abbreviation which we have
15687 already read (which means we are about to read the abbreviations
15688 for the next compile unit) or if the end of the abbreviation
15689 table is reached. */
433df2d4 15690 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15691 break;
15692 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15693 abbrev_ptr += bytes_read;
433df2d4 15694 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15695 break;
15696 }
f3dd6933
DJ
15697
15698 xfree (cur_attrs);
433df2d4 15699 return abbrev_table;
c906108c
SS
15700}
15701
433df2d4 15702/* Free the resources held by ABBREV_TABLE. */
c906108c 15703
c906108c 15704static void
433df2d4 15705abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15706{
433df2d4
DE
15707 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15708 xfree (abbrev_table);
c906108c
SS
15709}
15710
f4dc4d17
DE
15711/* Same as abbrev_table_free but as a cleanup.
15712 We pass in a pointer to the pointer to the table so that we can
15713 set the pointer to NULL when we're done. It also simplifies
73051182 15714 build_type_psymtabs_1. */
f4dc4d17
DE
15715
15716static void
15717abbrev_table_free_cleanup (void *table_ptr)
15718{
9a3c8263 15719 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15720
15721 if (*abbrev_table_ptr != NULL)
15722 abbrev_table_free (*abbrev_table_ptr);
15723 *abbrev_table_ptr = NULL;
15724}
15725
433df2d4
DE
15726/* Read the abbrev table for CU from ABBREV_SECTION. */
15727
15728static void
15729dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15730 struct dwarf2_section_info *abbrev_section)
c906108c 15731{
433df2d4
DE
15732 cu->abbrev_table =
15733 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15734}
c906108c 15735
433df2d4 15736/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15737
433df2d4
DE
15738static void
15739dwarf2_free_abbrev_table (void *ptr_to_cu)
15740{
9a3c8263 15741 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15742
a2ce51a0
DE
15743 if (cu->abbrev_table != NULL)
15744 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15745 /* Set this to NULL so that we SEGV if we try to read it later,
15746 and also because free_comp_unit verifies this is NULL. */
15747 cu->abbrev_table = NULL;
15748}
15749\f
72bf9492
DJ
15750/* Returns nonzero if TAG represents a type that we might generate a partial
15751 symbol for. */
15752
15753static int
15754is_type_tag_for_partial (int tag)
15755{
15756 switch (tag)
15757 {
15758#if 0
15759 /* Some types that would be reasonable to generate partial symbols for,
15760 that we don't at present. */
15761 case DW_TAG_array_type:
15762 case DW_TAG_file_type:
15763 case DW_TAG_ptr_to_member_type:
15764 case DW_TAG_set_type:
15765 case DW_TAG_string_type:
15766 case DW_TAG_subroutine_type:
15767#endif
15768 case DW_TAG_base_type:
15769 case DW_TAG_class_type:
680b30c7 15770 case DW_TAG_interface_type:
72bf9492
DJ
15771 case DW_TAG_enumeration_type:
15772 case DW_TAG_structure_type:
15773 case DW_TAG_subrange_type:
15774 case DW_TAG_typedef:
15775 case DW_TAG_union_type:
15776 return 1;
15777 default:
15778 return 0;
15779 }
15780}
15781
15782/* Load all DIEs that are interesting for partial symbols into memory. */
15783
15784static struct partial_die_info *
dee91e82 15785load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15786 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15787{
dee91e82 15788 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15789 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15790 struct partial_die_info *part_die;
15791 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15792 struct abbrev_info *abbrev;
15793 unsigned int bytes_read;
5afb4e99 15794 unsigned int load_all = 0;
72bf9492
DJ
15795 int nesting_level = 1;
15796
15797 parent_die = NULL;
15798 last_die = NULL;
15799
7adf1e79
DE
15800 gdb_assert (cu->per_cu != NULL);
15801 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15802 load_all = 1;
15803
72bf9492
DJ
15804 cu->partial_dies
15805 = htab_create_alloc_ex (cu->header.length / 12,
15806 partial_die_hash,
15807 partial_die_eq,
15808 NULL,
15809 &cu->comp_unit_obstack,
15810 hashtab_obstack_allocate,
15811 dummy_obstack_deallocate);
15812
8d749320 15813 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15814
15815 while (1)
15816 {
15817 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15818
15819 /* A NULL abbrev means the end of a series of children. */
15820 if (abbrev == NULL)
15821 {
15822 if (--nesting_level == 0)
15823 {
15824 /* PART_DIE was probably the last thing allocated on the
15825 comp_unit_obstack, so we could call obstack_free
15826 here. We don't do that because the waste is small,
15827 and will be cleaned up when we're done with this
15828 compilation unit. This way, we're also more robust
15829 against other users of the comp_unit_obstack. */
15830 return first_die;
15831 }
15832 info_ptr += bytes_read;
15833 last_die = parent_die;
15834 parent_die = parent_die->die_parent;
15835 continue;
15836 }
15837
98bfdba5
PA
15838 /* Check for template arguments. We never save these; if
15839 they're seen, we just mark the parent, and go on our way. */
15840 if (parent_die != NULL
15841 && cu->language == language_cplus
15842 && (abbrev->tag == DW_TAG_template_type_param
15843 || abbrev->tag == DW_TAG_template_value_param))
15844 {
15845 parent_die->has_template_arguments = 1;
15846
15847 if (!load_all)
15848 {
15849 /* We don't need a partial DIE for the template argument. */
dee91e82 15850 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15851 continue;
15852 }
15853 }
15854
0d99eb77 15855 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15856 Skip their other children. */
15857 if (!load_all
15858 && cu->language == language_cplus
15859 && parent_die != NULL
15860 && parent_die->tag == DW_TAG_subprogram)
15861 {
dee91e82 15862 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15863 continue;
15864 }
15865
5afb4e99
DJ
15866 /* Check whether this DIE is interesting enough to save. Normally
15867 we would not be interested in members here, but there may be
15868 later variables referencing them via DW_AT_specification (for
15869 static members). */
15870 if (!load_all
15871 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15872 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15873 && abbrev->tag != DW_TAG_enumerator
15874 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15875 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15876 && abbrev->tag != DW_TAG_variable
5afb4e99 15877 && abbrev->tag != DW_TAG_namespace
f55ee35c 15878 && abbrev->tag != DW_TAG_module
95554aad 15879 && abbrev->tag != DW_TAG_member
74921315
KS
15880 && abbrev->tag != DW_TAG_imported_unit
15881 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15882 {
15883 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15884 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15885 continue;
15886 }
15887
dee91e82
DE
15888 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15889 info_ptr);
72bf9492
DJ
15890
15891 /* This two-pass algorithm for processing partial symbols has a
15892 high cost in cache pressure. Thus, handle some simple cases
15893 here which cover the majority of C partial symbols. DIEs
15894 which neither have specification tags in them, nor could have
15895 specification tags elsewhere pointing at them, can simply be
15896 processed and discarded.
15897
15898 This segment is also optional; scan_partial_symbols and
15899 add_partial_symbol will handle these DIEs if we chain
15900 them in normally. When compilers which do not emit large
15901 quantities of duplicate debug information are more common,
15902 this code can probably be removed. */
15903
15904 /* Any complete simple types at the top level (pretty much all
15905 of them, for a language without namespaces), can be processed
15906 directly. */
15907 if (parent_die == NULL
15908 && part_die->has_specification == 0
15909 && part_die->is_declaration == 0
d8228535 15910 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15911 || part_die->tag == DW_TAG_base_type
15912 || part_die->tag == DW_TAG_subrange_type))
15913 {
15914 if (building_psymtab && part_die->name != NULL)
04a679b8 15915 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15916 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15917 &objfile->static_psymbols,
1762568f 15918 0, cu->language, objfile);
dee91e82 15919 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15920 continue;
15921 }
15922
d8228535
JK
15923 /* The exception for DW_TAG_typedef with has_children above is
15924 a workaround of GCC PR debug/47510. In the case of this complaint
15925 type_name_no_tag_or_error will error on such types later.
15926
15927 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15928 it could not find the child DIEs referenced later, this is checked
15929 above. In correct DWARF DW_TAG_typedef should have no children. */
15930
15931 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15932 complaint (&symfile_complaints,
15933 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15934 "- DIE at 0x%x [in module %s]"),
4262abfb 15935 part_die->offset.sect_off, objfile_name (objfile));
d8228535 15936
72bf9492
DJ
15937 /* If we're at the second level, and we're an enumerator, and
15938 our parent has no specification (meaning possibly lives in a
15939 namespace elsewhere), then we can add the partial symbol now
15940 instead of queueing it. */
15941 if (part_die->tag == DW_TAG_enumerator
15942 && parent_die != NULL
15943 && parent_die->die_parent == NULL
15944 && parent_die->tag == DW_TAG_enumeration_type
15945 && parent_die->has_specification == 0)
15946 {
15947 if (part_die->name == NULL)
3e43a32a
MS
15948 complaint (&symfile_complaints,
15949 _("malformed enumerator DIE ignored"));
72bf9492 15950 else if (building_psymtab)
04a679b8 15951 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15952 VAR_DOMAIN, LOC_CONST,
9c37b5ae 15953 cu->language == language_cplus
bb5ed363
DE
15954 ? &objfile->global_psymbols
15955 : &objfile->static_psymbols,
1762568f 15956 0, cu->language, objfile);
72bf9492 15957
dee91e82 15958 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15959 continue;
15960 }
15961
15962 /* We'll save this DIE so link it in. */
15963 part_die->die_parent = parent_die;
15964 part_die->die_sibling = NULL;
15965 part_die->die_child = NULL;
15966
15967 if (last_die && last_die == parent_die)
15968 last_die->die_child = part_die;
15969 else if (last_die)
15970 last_die->die_sibling = part_die;
15971
15972 last_die = part_die;
15973
15974 if (first_die == NULL)
15975 first_die = part_die;
15976
15977 /* Maybe add the DIE to the hash table. Not all DIEs that we
15978 find interesting need to be in the hash table, because we
15979 also have the parent/sibling/child chains; only those that we
15980 might refer to by offset later during partial symbol reading.
15981
15982 For now this means things that might have be the target of a
15983 DW_AT_specification, DW_AT_abstract_origin, or
15984 DW_AT_extension. DW_AT_extension will refer only to
15985 namespaces; DW_AT_abstract_origin refers to functions (and
15986 many things under the function DIE, but we do not recurse
15987 into function DIEs during partial symbol reading) and
15988 possibly variables as well; DW_AT_specification refers to
15989 declarations. Declarations ought to have the DW_AT_declaration
15990 flag. It happens that GCC forgets to put it in sometimes, but
15991 only for functions, not for types.
15992
15993 Adding more things than necessary to the hash table is harmless
15994 except for the performance cost. Adding too few will result in
5afb4e99
DJ
15995 wasted time in find_partial_die, when we reread the compilation
15996 unit with load_all_dies set. */
72bf9492 15997
5afb4e99 15998 if (load_all
72929c62 15999 || abbrev->tag == DW_TAG_constant
5afb4e99 16000 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16001 || abbrev->tag == DW_TAG_variable
16002 || abbrev->tag == DW_TAG_namespace
16003 || part_die->is_declaration)
16004 {
16005 void **slot;
16006
16007 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 16008 part_die->offset.sect_off, INSERT);
72bf9492
DJ
16009 *slot = part_die;
16010 }
16011
8d749320 16012 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16013
16014 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16015 we have no reason to follow the children of structures; for other
98bfdba5
PA
16016 languages we have to, so that we can get at method physnames
16017 to infer fully qualified class names, for DW_AT_specification,
16018 and for C++ template arguments. For C++, we also look one level
16019 inside functions to find template arguments (if the name of the
16020 function does not already contain the template arguments).
bc30ff58
JB
16021
16022 For Ada, we need to scan the children of subprograms and lexical
16023 blocks as well because Ada allows the definition of nested
16024 entities that could be interesting for the debugger, such as
16025 nested subprograms for instance. */
72bf9492 16026 if (last_die->has_children
5afb4e99
DJ
16027 && (load_all
16028 || last_die->tag == DW_TAG_namespace
f55ee35c 16029 || last_die->tag == DW_TAG_module
72bf9492 16030 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16031 || (cu->language == language_cplus
16032 && last_die->tag == DW_TAG_subprogram
16033 && (last_die->name == NULL
16034 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16035 || (cu->language != language_c
16036 && (last_die->tag == DW_TAG_class_type
680b30c7 16037 || last_die->tag == DW_TAG_interface_type
72bf9492 16038 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16039 || last_die->tag == DW_TAG_union_type))
16040 || (cu->language == language_ada
16041 && (last_die->tag == DW_TAG_subprogram
16042 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16043 {
16044 nesting_level++;
16045 parent_die = last_die;
16046 continue;
16047 }
16048
16049 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16050 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16051
16052 /* Back to the top, do it again. */
16053 }
16054}
16055
c906108c
SS
16056/* Read a minimal amount of information into the minimal die structure. */
16057
d521ce57 16058static const gdb_byte *
dee91e82
DE
16059read_partial_die (const struct die_reader_specs *reader,
16060 struct partial_die_info *part_die,
16061 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16062 const gdb_byte *info_ptr)
c906108c 16063{
dee91e82 16064 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16065 struct objfile *objfile = cu->objfile;
d521ce57 16066 const gdb_byte *buffer = reader->buffer;
fa238c03 16067 unsigned int i;
c906108c 16068 struct attribute attr;
c5aa993b 16069 int has_low_pc_attr = 0;
c906108c 16070 int has_high_pc_attr = 0;
91da1414 16071 int high_pc_relative = 0;
c906108c 16072
72bf9492 16073 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16074
b64f50a1 16075 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
16076
16077 info_ptr += abbrev_len;
16078
16079 if (abbrev == NULL)
16080 return info_ptr;
16081
c906108c
SS
16082 part_die->tag = abbrev->tag;
16083 part_die->has_children = abbrev->has_children;
c906108c
SS
16084
16085 for (i = 0; i < abbrev->num_attrs; ++i)
16086 {
dee91e82 16087 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16088
16089 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16090 partial symbol table. */
c906108c
SS
16091 switch (attr.name)
16092 {
16093 case DW_AT_name:
71c25dea
TT
16094 switch (part_die->tag)
16095 {
16096 case DW_TAG_compile_unit:
95554aad 16097 case DW_TAG_partial_unit:
348e048f 16098 case DW_TAG_type_unit:
71c25dea
TT
16099 /* Compilation units have a DW_AT_name that is a filename, not
16100 a source language identifier. */
16101 case DW_TAG_enumeration_type:
16102 case DW_TAG_enumerator:
16103 /* These tags always have simple identifiers already; no need
16104 to canonicalize them. */
16105 part_die->name = DW_STRING (&attr);
16106 break;
16107 default:
16108 part_die->name
16109 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16110 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16111 break;
16112 }
c906108c 16113 break;
31ef98ae 16114 case DW_AT_linkage_name:
c906108c 16115 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16116 /* Note that both forms of linkage name might appear. We
16117 assume they will be the same, and we only store the last
16118 one we see. */
94af9270
KS
16119 if (cu->language == language_ada)
16120 part_die->name = DW_STRING (&attr);
abc72ce4 16121 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16122 break;
16123 case DW_AT_low_pc:
16124 has_low_pc_attr = 1;
31aa7e4e 16125 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16126 break;
16127 case DW_AT_high_pc:
16128 has_high_pc_attr = 1;
31aa7e4e
JB
16129 part_die->highpc = attr_value_as_address (&attr);
16130 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16131 high_pc_relative = 1;
c906108c
SS
16132 break;
16133 case DW_AT_location:
0963b4bd 16134 /* Support the .debug_loc offsets. */
8e19ed76
PS
16135 if (attr_form_is_block (&attr))
16136 {
95554aad 16137 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16138 }
3690dd37 16139 else if (attr_form_is_section_offset (&attr))
8e19ed76 16140 {
4d3c2250 16141 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16142 }
16143 else
16144 {
4d3c2250
KB
16145 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16146 "partial symbol information");
8e19ed76 16147 }
c906108c 16148 break;
c906108c
SS
16149 case DW_AT_external:
16150 part_die->is_external = DW_UNSND (&attr);
16151 break;
16152 case DW_AT_declaration:
16153 part_die->is_declaration = DW_UNSND (&attr);
16154 break;
16155 case DW_AT_type:
16156 part_die->has_type = 1;
16157 break;
16158 case DW_AT_abstract_origin:
16159 case DW_AT_specification:
72bf9492
DJ
16160 case DW_AT_extension:
16161 part_die->has_specification = 1;
c764a876 16162 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16163 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16164 || cu->per_cu->is_dwz);
c906108c
SS
16165 break;
16166 case DW_AT_sibling:
16167 /* Ignore absolute siblings, they might point outside of
16168 the current compile unit. */
16169 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16170 complaint (&symfile_complaints,
16171 _("ignoring absolute DW_AT_sibling"));
c906108c 16172 else
b9502d3f
WN
16173 {
16174 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16175 const gdb_byte *sibling_ptr = buffer + off;
16176
16177 if (sibling_ptr < info_ptr)
16178 complaint (&symfile_complaints,
16179 _("DW_AT_sibling points backwards"));
22869d73
KS
16180 else if (sibling_ptr > reader->buffer_end)
16181 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16182 else
16183 part_die->sibling = sibling_ptr;
16184 }
c906108c 16185 break;
fa4028e9
JB
16186 case DW_AT_byte_size:
16187 part_die->has_byte_size = 1;
16188 break;
ff908ebf
AW
16189 case DW_AT_const_value:
16190 part_die->has_const_value = 1;
16191 break;
68511cec
CES
16192 case DW_AT_calling_convention:
16193 /* DWARF doesn't provide a way to identify a program's source-level
16194 entry point. DW_AT_calling_convention attributes are only meant
16195 to describe functions' calling conventions.
16196
16197 However, because it's a necessary piece of information in
0c1b455e
TT
16198 Fortran, and before DWARF 4 DW_CC_program was the only
16199 piece of debugging information whose definition refers to
16200 a 'main program' at all, several compilers marked Fortran
16201 main programs with DW_CC_program --- even when those
16202 functions use the standard calling conventions.
16203
16204 Although DWARF now specifies a way to provide this
16205 information, we support this practice for backward
16206 compatibility. */
68511cec 16207 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16208 && cu->language == language_fortran)
16209 part_die->main_subprogram = 1;
68511cec 16210 break;
481860b3
GB
16211 case DW_AT_inline:
16212 if (DW_UNSND (&attr) == DW_INL_inlined
16213 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16214 part_die->may_be_inlined = 1;
16215 break;
95554aad
TT
16216
16217 case DW_AT_import:
16218 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
16219 {
16220 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16221 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16222 || cu->per_cu->is_dwz);
16223 }
95554aad
TT
16224 break;
16225
0c1b455e
TT
16226 case DW_AT_main_subprogram:
16227 part_die->main_subprogram = DW_UNSND (&attr);
16228 break;
16229
c906108c
SS
16230 default:
16231 break;
16232 }
16233 }
16234
91da1414
MW
16235 if (high_pc_relative)
16236 part_die->highpc += part_die->lowpc;
16237
9373cf26
JK
16238 if (has_low_pc_attr && has_high_pc_attr)
16239 {
16240 /* When using the GNU linker, .gnu.linkonce. sections are used to
16241 eliminate duplicate copies of functions and vtables and such.
16242 The linker will arbitrarily choose one and discard the others.
16243 The AT_*_pc values for such functions refer to local labels in
16244 these sections. If the section from that file was discarded, the
16245 labels are not in the output, so the relocs get a value of 0.
16246 If this is a discarded function, mark the pc bounds as invalid,
16247 so that GDB will ignore it. */
16248 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16249 {
bb5ed363 16250 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16251
16252 complaint (&symfile_complaints,
16253 _("DW_AT_low_pc %s is zero "
16254 "for DIE at 0x%x [in module %s]"),
16255 paddress (gdbarch, part_die->lowpc),
4262abfb 16256 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16257 }
16258 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16259 else if (part_die->lowpc >= part_die->highpc)
16260 {
bb5ed363 16261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16262
16263 complaint (&symfile_complaints,
16264 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16265 "for DIE at 0x%x [in module %s]"),
16266 paddress (gdbarch, part_die->lowpc),
16267 paddress (gdbarch, part_die->highpc),
4262abfb 16268 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16269 }
16270 else
16271 part_die->has_pc_info = 1;
16272 }
85cbf3d3 16273
c906108c
SS
16274 return info_ptr;
16275}
16276
72bf9492
DJ
16277/* Find a cached partial DIE at OFFSET in CU. */
16278
16279static struct partial_die_info *
b64f50a1 16280find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
16281{
16282 struct partial_die_info *lookup_die = NULL;
16283 struct partial_die_info part_die;
16284
16285 part_die.offset = offset;
9a3c8263
SM
16286 lookup_die = ((struct partial_die_info *)
16287 htab_find_with_hash (cu->partial_dies, &part_die,
16288 offset.sect_off));
72bf9492 16289
72bf9492
DJ
16290 return lookup_die;
16291}
16292
348e048f
DE
16293/* Find a partial DIE at OFFSET, which may or may not be in CU,
16294 except in the case of .debug_types DIEs which do not reference
16295 outside their CU (they do however referencing other types via
55f1336d 16296 DW_FORM_ref_sig8). */
72bf9492
DJ
16297
16298static struct partial_die_info *
36586728 16299find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16300{
bb5ed363 16301 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16302 struct dwarf2_per_cu_data *per_cu = NULL;
16303 struct partial_die_info *pd = NULL;
72bf9492 16304
36586728
TT
16305 if (offset_in_dwz == cu->per_cu->is_dwz
16306 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
16307 {
16308 pd = find_partial_die_in_comp_unit (offset, cu);
16309 if (pd != NULL)
16310 return pd;
0d99eb77
DE
16311 /* We missed recording what we needed.
16312 Load all dies and try again. */
16313 per_cu = cu->per_cu;
5afb4e99 16314 }
0d99eb77
DE
16315 else
16316 {
16317 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16318 if (cu->per_cu->is_debug_types)
0d99eb77
DE
16319 {
16320 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16321 " external reference to offset 0x%lx [in module %s].\n"),
16322 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16323 bfd_get_filename (objfile->obfd));
16324 }
36586728
TT
16325 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16326 objfile);
72bf9492 16327
0d99eb77
DE
16328 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16329 load_partial_comp_unit (per_cu);
ae038cb0 16330
0d99eb77
DE
16331 per_cu->cu->last_used = 0;
16332 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16333 }
5afb4e99 16334
dee91e82
DE
16335 /* If we didn't find it, and not all dies have been loaded,
16336 load them all and try again. */
16337
5afb4e99
DJ
16338 if (pd == NULL && per_cu->load_all_dies == 0)
16339 {
5afb4e99 16340 per_cu->load_all_dies = 1;
fd820528
DE
16341
16342 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16343 THIS_CU->cu may already be in use. So we can't just free it and
16344 replace its DIEs with the ones we read in. Instead, we leave those
16345 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16346 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16347 set. */
dee91e82 16348 load_partial_comp_unit (per_cu);
5afb4e99
DJ
16349
16350 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16351 }
16352
16353 if (pd == NULL)
16354 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16355 _("could not find partial DIE 0x%x "
16356 "in cache [from module %s]\n"),
b64f50a1 16357 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 16358 return pd;
72bf9492
DJ
16359}
16360
abc72ce4
DE
16361/* See if we can figure out if the class lives in a namespace. We do
16362 this by looking for a member function; its demangled name will
16363 contain namespace info, if there is any. */
16364
16365static void
16366guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16367 struct dwarf2_cu *cu)
16368{
16369 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16370 what template types look like, because the demangler
16371 frequently doesn't give the same name as the debug info. We
16372 could fix this by only using the demangled name to get the
16373 prefix (but see comment in read_structure_type). */
16374
16375 struct partial_die_info *real_pdi;
16376 struct partial_die_info *child_pdi;
16377
16378 /* If this DIE (this DIE's specification, if any) has a parent, then
16379 we should not do this. We'll prepend the parent's fully qualified
16380 name when we create the partial symbol. */
16381
16382 real_pdi = struct_pdi;
16383 while (real_pdi->has_specification)
36586728
TT
16384 real_pdi = find_partial_die (real_pdi->spec_offset,
16385 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16386
16387 if (real_pdi->die_parent != NULL)
16388 return;
16389
16390 for (child_pdi = struct_pdi->die_child;
16391 child_pdi != NULL;
16392 child_pdi = child_pdi->die_sibling)
16393 {
16394 if (child_pdi->tag == DW_TAG_subprogram
16395 && child_pdi->linkage_name != NULL)
16396 {
16397 char *actual_class_name
16398 = language_class_name_from_physname (cu->language_defn,
16399 child_pdi->linkage_name);
16400 if (actual_class_name != NULL)
16401 {
16402 struct_pdi->name
224c3ddb
SM
16403 = ((const char *)
16404 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16405 actual_class_name,
16406 strlen (actual_class_name)));
abc72ce4
DE
16407 xfree (actual_class_name);
16408 }
16409 break;
16410 }
16411 }
16412}
16413
72bf9492
DJ
16414/* Adjust PART_DIE before generating a symbol for it. This function
16415 may set the is_external flag or change the DIE's name. */
16416
16417static void
16418fixup_partial_die (struct partial_die_info *part_die,
16419 struct dwarf2_cu *cu)
16420{
abc72ce4
DE
16421 /* Once we've fixed up a die, there's no point in doing so again.
16422 This also avoids a memory leak if we were to call
16423 guess_partial_die_structure_name multiple times. */
16424 if (part_die->fixup_called)
16425 return;
16426
72bf9492
DJ
16427 /* If we found a reference attribute and the DIE has no name, try
16428 to find a name in the referred to DIE. */
16429
16430 if (part_die->name == NULL && part_die->has_specification)
16431 {
16432 struct partial_die_info *spec_die;
72bf9492 16433
36586728
TT
16434 spec_die = find_partial_die (part_die->spec_offset,
16435 part_die->spec_is_dwz, cu);
72bf9492 16436
10b3939b 16437 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16438
16439 if (spec_die->name)
16440 {
16441 part_die->name = spec_die->name;
16442
16443 /* Copy DW_AT_external attribute if it is set. */
16444 if (spec_die->is_external)
16445 part_die->is_external = spec_die->is_external;
16446 }
16447 }
16448
16449 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16450
16451 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16452 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16453
abc72ce4
DE
16454 /* If there is no parent die to provide a namespace, and there are
16455 children, see if we can determine the namespace from their linkage
122d1940 16456 name. */
abc72ce4 16457 if (cu->language == language_cplus
8b70b953 16458 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16459 && part_die->die_parent == NULL
16460 && part_die->has_children
16461 && (part_die->tag == DW_TAG_class_type
16462 || part_die->tag == DW_TAG_structure_type
16463 || part_die->tag == DW_TAG_union_type))
16464 guess_partial_die_structure_name (part_die, cu);
16465
53832f31
TT
16466 /* GCC might emit a nameless struct or union that has a linkage
16467 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16468 if (part_die->name == NULL
96408a79
SA
16469 && (part_die->tag == DW_TAG_class_type
16470 || part_die->tag == DW_TAG_interface_type
16471 || part_die->tag == DW_TAG_structure_type
16472 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16473 && part_die->linkage_name != NULL)
16474 {
16475 char *demangled;
16476
8de20a37 16477 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16478 if (demangled)
16479 {
96408a79
SA
16480 const char *base;
16481
16482 /* Strip any leading namespaces/classes, keep only the base name.
16483 DW_AT_name for named DIEs does not contain the prefixes. */
16484 base = strrchr (demangled, ':');
16485 if (base && base > demangled && base[-1] == ':')
16486 base++;
16487 else
16488 base = demangled;
16489
34a68019 16490 part_die->name
224c3ddb
SM
16491 = ((const char *)
16492 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16493 base, strlen (base)));
53832f31
TT
16494 xfree (demangled);
16495 }
16496 }
16497
abc72ce4 16498 part_die->fixup_called = 1;
72bf9492
DJ
16499}
16500
a8329558 16501/* Read an attribute value described by an attribute form. */
c906108c 16502
d521ce57 16503static const gdb_byte *
dee91e82
DE
16504read_attribute_value (const struct die_reader_specs *reader,
16505 struct attribute *attr, unsigned form,
43988095 16506 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16507{
dee91e82 16508 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16509 struct objfile *objfile = cu->objfile;
16510 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16511 bfd *abfd = reader->abfd;
e7c27a73 16512 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16513 unsigned int bytes_read;
16514 struct dwarf_block *blk;
16515
aead7601 16516 attr->form = (enum dwarf_form) form;
a8329558 16517 switch (form)
c906108c 16518 {
c906108c 16519 case DW_FORM_ref_addr:
ae411497 16520 if (cu->header.version == 2)
4568ecf9 16521 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16522 else
4568ecf9
DE
16523 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16524 &cu->header, &bytes_read);
ae411497
TT
16525 info_ptr += bytes_read;
16526 break;
36586728
TT
16527 case DW_FORM_GNU_ref_alt:
16528 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16529 info_ptr += bytes_read;
16530 break;
ae411497 16531 case DW_FORM_addr:
e7c27a73 16532 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16533 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16534 info_ptr += bytes_read;
c906108c
SS
16535 break;
16536 case DW_FORM_block2:
7b5a2f43 16537 blk = dwarf_alloc_block (cu);
c906108c
SS
16538 blk->size = read_2_bytes (abfd, info_ptr);
16539 info_ptr += 2;
16540 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16541 info_ptr += blk->size;
16542 DW_BLOCK (attr) = blk;
16543 break;
16544 case DW_FORM_block4:
7b5a2f43 16545 blk = dwarf_alloc_block (cu);
c906108c
SS
16546 blk->size = read_4_bytes (abfd, info_ptr);
16547 info_ptr += 4;
16548 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16549 info_ptr += blk->size;
16550 DW_BLOCK (attr) = blk;
16551 break;
16552 case DW_FORM_data2:
16553 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16554 info_ptr += 2;
16555 break;
16556 case DW_FORM_data4:
16557 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16558 info_ptr += 4;
16559 break;
16560 case DW_FORM_data8:
16561 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16562 info_ptr += 8;
16563 break;
0224619f
JK
16564 case DW_FORM_data16:
16565 blk = dwarf_alloc_block (cu);
16566 blk->size = 16;
16567 blk->data = read_n_bytes (abfd, info_ptr, 16);
16568 info_ptr += 16;
16569 DW_BLOCK (attr) = blk;
16570 break;
2dc7f7b3
TT
16571 case DW_FORM_sec_offset:
16572 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16573 info_ptr += bytes_read;
16574 break;
c906108c 16575 case DW_FORM_string:
9b1c24c8 16576 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16577 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16578 info_ptr += bytes_read;
16579 break;
4bdf3d34 16580 case DW_FORM_strp:
36586728
TT
16581 if (!cu->per_cu->is_dwz)
16582 {
16583 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16584 &bytes_read);
16585 DW_STRING_IS_CANONICAL (attr) = 0;
16586 info_ptr += bytes_read;
16587 break;
16588 }
16589 /* FALLTHROUGH */
43988095
JK
16590 case DW_FORM_line_strp:
16591 if (!cu->per_cu->is_dwz)
16592 {
16593 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16594 cu_header, &bytes_read);
16595 DW_STRING_IS_CANONICAL (attr) = 0;
16596 info_ptr += bytes_read;
16597 break;
16598 }
16599 /* FALLTHROUGH */
36586728
TT
16600 case DW_FORM_GNU_strp_alt:
16601 {
16602 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16603 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16604 &bytes_read);
16605
16606 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16607 DW_STRING_IS_CANONICAL (attr) = 0;
16608 info_ptr += bytes_read;
16609 }
4bdf3d34 16610 break;
2dc7f7b3 16611 case DW_FORM_exprloc:
c906108c 16612 case DW_FORM_block:
7b5a2f43 16613 blk = dwarf_alloc_block (cu);
c906108c
SS
16614 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16615 info_ptr += bytes_read;
16616 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16617 info_ptr += blk->size;
16618 DW_BLOCK (attr) = blk;
16619 break;
16620 case DW_FORM_block1:
7b5a2f43 16621 blk = dwarf_alloc_block (cu);
c906108c
SS
16622 blk->size = read_1_byte (abfd, info_ptr);
16623 info_ptr += 1;
16624 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16625 info_ptr += blk->size;
16626 DW_BLOCK (attr) = blk;
16627 break;
16628 case DW_FORM_data1:
16629 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16630 info_ptr += 1;
16631 break;
16632 case DW_FORM_flag:
16633 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16634 info_ptr += 1;
16635 break;
2dc7f7b3
TT
16636 case DW_FORM_flag_present:
16637 DW_UNSND (attr) = 1;
16638 break;
c906108c
SS
16639 case DW_FORM_sdata:
16640 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16641 info_ptr += bytes_read;
16642 break;
16643 case DW_FORM_udata:
16644 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16645 info_ptr += bytes_read;
16646 break;
16647 case DW_FORM_ref1:
4568ecf9
DE
16648 DW_UNSND (attr) = (cu->header.offset.sect_off
16649 + read_1_byte (abfd, info_ptr));
c906108c
SS
16650 info_ptr += 1;
16651 break;
16652 case DW_FORM_ref2:
4568ecf9
DE
16653 DW_UNSND (attr) = (cu->header.offset.sect_off
16654 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16655 info_ptr += 2;
16656 break;
16657 case DW_FORM_ref4:
4568ecf9
DE
16658 DW_UNSND (attr) = (cu->header.offset.sect_off
16659 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16660 info_ptr += 4;
16661 break;
613e1657 16662 case DW_FORM_ref8:
4568ecf9
DE
16663 DW_UNSND (attr) = (cu->header.offset.sect_off
16664 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16665 info_ptr += 8;
16666 break;
55f1336d 16667 case DW_FORM_ref_sig8:
ac9ec31b 16668 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16669 info_ptr += 8;
16670 break;
c906108c 16671 case DW_FORM_ref_udata:
4568ecf9
DE
16672 DW_UNSND (attr) = (cu->header.offset.sect_off
16673 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16674 info_ptr += bytes_read;
16675 break;
c906108c 16676 case DW_FORM_indirect:
a8329558
KW
16677 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16678 info_ptr += bytes_read;
43988095
JK
16679 if (form == DW_FORM_implicit_const)
16680 {
16681 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16682 info_ptr += bytes_read;
16683 }
16684 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16685 info_ptr);
16686 break;
16687 case DW_FORM_implicit_const:
16688 DW_SND (attr) = implicit_const;
a8329558 16689 break;
3019eac3
DE
16690 case DW_FORM_GNU_addr_index:
16691 if (reader->dwo_file == NULL)
16692 {
16693 /* For now flag a hard error.
16694 Later we can turn this into a complaint. */
16695 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16696 dwarf_form_name (form),
16697 bfd_get_filename (abfd));
16698 }
16699 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16700 info_ptr += bytes_read;
16701 break;
16702 case DW_FORM_GNU_str_index:
16703 if (reader->dwo_file == NULL)
16704 {
16705 /* For now flag a hard error.
16706 Later we can turn this into a complaint if warranted. */
16707 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16708 dwarf_form_name (form),
16709 bfd_get_filename (abfd));
16710 }
16711 {
16712 ULONGEST str_index =
16713 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16714
342587c4 16715 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16716 DW_STRING_IS_CANONICAL (attr) = 0;
16717 info_ptr += bytes_read;
16718 }
16719 break;
c906108c 16720 default:
8a3fe4f8 16721 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16722 dwarf_form_name (form),
16723 bfd_get_filename (abfd));
c906108c 16724 }
28e94949 16725
36586728 16726 /* Super hack. */
7771576e 16727 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16728 attr->form = DW_FORM_GNU_ref_alt;
16729
28e94949
JB
16730 /* We have seen instances where the compiler tried to emit a byte
16731 size attribute of -1 which ended up being encoded as an unsigned
16732 0xffffffff. Although 0xffffffff is technically a valid size value,
16733 an object of this size seems pretty unlikely so we can relatively
16734 safely treat these cases as if the size attribute was invalid and
16735 treat them as zero by default. */
16736 if (attr->name == DW_AT_byte_size
16737 && form == DW_FORM_data4
16738 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16739 {
16740 complaint
16741 (&symfile_complaints,
43bbcdc2
PH
16742 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16743 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16744 DW_UNSND (attr) = 0;
16745 }
28e94949 16746
c906108c
SS
16747 return info_ptr;
16748}
16749
a8329558
KW
16750/* Read an attribute described by an abbreviated attribute. */
16751
d521ce57 16752static const gdb_byte *
dee91e82
DE
16753read_attribute (const struct die_reader_specs *reader,
16754 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16755 const gdb_byte *info_ptr)
a8329558
KW
16756{
16757 attr->name = abbrev->name;
43988095
JK
16758 return read_attribute_value (reader, attr, abbrev->form,
16759 abbrev->implicit_const, info_ptr);
a8329558
KW
16760}
16761
0963b4bd 16762/* Read dwarf information from a buffer. */
c906108c
SS
16763
16764static unsigned int
a1855c1d 16765read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16766{
fe1b8b76 16767 return bfd_get_8 (abfd, buf);
c906108c
SS
16768}
16769
16770static int
a1855c1d 16771read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16772{
fe1b8b76 16773 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16774}
16775
16776static unsigned int
a1855c1d 16777read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16778{
fe1b8b76 16779 return bfd_get_16 (abfd, buf);
c906108c
SS
16780}
16781
21ae7a4d 16782static int
a1855c1d 16783read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16784{
16785 return bfd_get_signed_16 (abfd, buf);
16786}
16787
c906108c 16788static unsigned int
a1855c1d 16789read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16790{
fe1b8b76 16791 return bfd_get_32 (abfd, buf);
c906108c
SS
16792}
16793
21ae7a4d 16794static int
a1855c1d 16795read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16796{
16797 return bfd_get_signed_32 (abfd, buf);
16798}
16799
93311388 16800static ULONGEST
a1855c1d 16801read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16802{
fe1b8b76 16803 return bfd_get_64 (abfd, buf);
c906108c
SS
16804}
16805
16806static CORE_ADDR
d521ce57 16807read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16808 unsigned int *bytes_read)
c906108c 16809{
e7c27a73 16810 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16811 CORE_ADDR retval = 0;
16812
107d2387 16813 if (cu_header->signed_addr_p)
c906108c 16814 {
107d2387
AC
16815 switch (cu_header->addr_size)
16816 {
16817 case 2:
fe1b8b76 16818 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16819 break;
16820 case 4:
fe1b8b76 16821 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16822 break;
16823 case 8:
fe1b8b76 16824 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16825 break;
16826 default:
8e65ff28 16827 internal_error (__FILE__, __LINE__,
e2e0b3e5 16828 _("read_address: bad switch, signed [in module %s]"),
659b0389 16829 bfd_get_filename (abfd));
107d2387
AC
16830 }
16831 }
16832 else
16833 {
16834 switch (cu_header->addr_size)
16835 {
16836 case 2:
fe1b8b76 16837 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16838 break;
16839 case 4:
fe1b8b76 16840 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16841 break;
16842 case 8:
fe1b8b76 16843 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16844 break;
16845 default:
8e65ff28 16846 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16847 _("read_address: bad switch, "
16848 "unsigned [in module %s]"),
659b0389 16849 bfd_get_filename (abfd));
107d2387 16850 }
c906108c 16851 }
64367e0a 16852
107d2387
AC
16853 *bytes_read = cu_header->addr_size;
16854 return retval;
c906108c
SS
16855}
16856
f7ef9339 16857/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16858 specification allows the initial length to take up either 4 bytes
16859 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16860 bytes describe the length and all offsets will be 8 bytes in length
16861 instead of 4.
16862
f7ef9339
KB
16863 An older, non-standard 64-bit format is also handled by this
16864 function. The older format in question stores the initial length
16865 as an 8-byte quantity without an escape value. Lengths greater
16866 than 2^32 aren't very common which means that the initial 4 bytes
16867 is almost always zero. Since a length value of zero doesn't make
16868 sense for the 32-bit format, this initial zero can be considered to
16869 be an escape value which indicates the presence of the older 64-bit
16870 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16871 greater than 4GB. If it becomes necessary to handle lengths
16872 somewhat larger than 4GB, we could allow other small values (such
16873 as the non-sensical values of 1, 2, and 3) to also be used as
16874 escape values indicating the presence of the old format.
f7ef9339 16875
917c78fc
MK
16876 The value returned via bytes_read should be used to increment the
16877 relevant pointer after calling read_initial_length().
c764a876 16878
613e1657
KB
16879 [ Note: read_initial_length() and read_offset() are based on the
16880 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16881 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16882 from:
16883
f7ef9339 16884 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16885
613e1657
KB
16886 This document is only a draft and is subject to change. (So beware.)
16887
f7ef9339 16888 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16889 determined empirically by examining 64-bit ELF files produced by
16890 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16891
16892 - Kevin, July 16, 2002
613e1657
KB
16893 ] */
16894
16895static LONGEST
d521ce57 16896read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16897{
fe1b8b76 16898 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16899
dd373385 16900 if (length == 0xffffffff)
613e1657 16901 {
fe1b8b76 16902 length = bfd_get_64 (abfd, buf + 4);
613e1657 16903 *bytes_read = 12;
613e1657 16904 }
dd373385 16905 else if (length == 0)
f7ef9339 16906 {
dd373385 16907 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16908 length = bfd_get_64 (abfd, buf);
f7ef9339 16909 *bytes_read = 8;
f7ef9339 16910 }
613e1657
KB
16911 else
16912 {
16913 *bytes_read = 4;
613e1657
KB
16914 }
16915
c764a876
DE
16916 return length;
16917}
dd373385 16918
c764a876
DE
16919/* Cover function for read_initial_length.
16920 Returns the length of the object at BUF, and stores the size of the
16921 initial length in *BYTES_READ and stores the size that offsets will be in
16922 *OFFSET_SIZE.
16923 If the initial length size is not equivalent to that specified in
16924 CU_HEADER then issue a complaint.
16925 This is useful when reading non-comp-unit headers. */
dd373385 16926
c764a876 16927static LONGEST
d521ce57 16928read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
16929 const struct comp_unit_head *cu_header,
16930 unsigned int *bytes_read,
16931 unsigned int *offset_size)
16932{
16933 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16934
16935 gdb_assert (cu_header->initial_length_size == 4
16936 || cu_header->initial_length_size == 8
16937 || cu_header->initial_length_size == 12);
16938
16939 if (cu_header->initial_length_size != *bytes_read)
16940 complaint (&symfile_complaints,
16941 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 16942
c764a876 16943 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 16944 return length;
613e1657
KB
16945}
16946
16947/* Read an offset from the data stream. The size of the offset is
917c78fc 16948 given by cu_header->offset_size. */
613e1657
KB
16949
16950static LONGEST
d521ce57
TT
16951read_offset (bfd *abfd, const gdb_byte *buf,
16952 const struct comp_unit_head *cu_header,
891d2f0b 16953 unsigned int *bytes_read)
c764a876
DE
16954{
16955 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 16956
c764a876
DE
16957 *bytes_read = cu_header->offset_size;
16958 return offset;
16959}
16960
16961/* Read an offset from the data stream. */
16962
16963static LONGEST
d521ce57 16964read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
16965{
16966 LONGEST retval = 0;
16967
c764a876 16968 switch (offset_size)
613e1657
KB
16969 {
16970 case 4:
fe1b8b76 16971 retval = bfd_get_32 (abfd, buf);
613e1657
KB
16972 break;
16973 case 8:
fe1b8b76 16974 retval = bfd_get_64 (abfd, buf);
613e1657
KB
16975 break;
16976 default:
8e65ff28 16977 internal_error (__FILE__, __LINE__,
c764a876 16978 _("read_offset_1: bad switch [in module %s]"),
659b0389 16979 bfd_get_filename (abfd));
613e1657
KB
16980 }
16981
917c78fc 16982 return retval;
613e1657
KB
16983}
16984
d521ce57
TT
16985static const gdb_byte *
16986read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
16987{
16988 /* If the size of a host char is 8 bits, we can return a pointer
16989 to the buffer, otherwise we have to copy the data to a buffer
16990 allocated on the temporary obstack. */
4bdf3d34 16991 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 16992 return buf;
c906108c
SS
16993}
16994
d521ce57
TT
16995static const char *
16996read_direct_string (bfd *abfd, const gdb_byte *buf,
16997 unsigned int *bytes_read_ptr)
c906108c
SS
16998{
16999 /* If the size of a host char is 8 bits, we can return a pointer
17000 to the string, otherwise we have to copy the string to a buffer
17001 allocated on the temporary obstack. */
4bdf3d34 17002 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17003 if (*buf == '\0')
17004 {
17005 *bytes_read_ptr = 1;
17006 return NULL;
17007 }
d521ce57
TT
17008 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17009 return (const char *) buf;
4bdf3d34
JJ
17010}
17011
43988095
JK
17012/* Return pointer to string at section SECT offset STR_OFFSET with error
17013 reporting strings FORM_NAME and SECT_NAME. */
17014
d521ce57 17015static const char *
43988095
JK
17016read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17017 struct dwarf2_section_info *sect,
17018 const char *form_name,
17019 const char *sect_name)
17020{
17021 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17022 if (sect->buffer == NULL)
17023 error (_("%s used without %s section [in module %s]"),
17024 form_name, sect_name, bfd_get_filename (abfd));
17025 if (str_offset >= sect->size)
17026 error (_("%s pointing outside of %s section [in module %s]"),
17027 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17028 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17029 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17030 return NULL;
43988095
JK
17031 return (const char *) (sect->buffer + str_offset);
17032}
17033
17034/* Return pointer to string at .debug_str offset STR_OFFSET. */
17035
17036static const char *
17037read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17038{
17039 return read_indirect_string_at_offset_from (abfd, str_offset,
17040 &dwarf2_per_objfile->str,
17041 "DW_FORM_strp", ".debug_str");
17042}
17043
17044/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17045
17046static const char *
17047read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17048{
17049 return read_indirect_string_at_offset_from (abfd, str_offset,
17050 &dwarf2_per_objfile->line_str,
17051 "DW_FORM_line_strp",
17052 ".debug_line_str");
c906108c
SS
17053}
17054
36586728
TT
17055/* Read a string at offset STR_OFFSET in the .debug_str section from
17056 the .dwz file DWZ. Throw an error if the offset is too large. If
17057 the string consists of a single NUL byte, return NULL; otherwise
17058 return a pointer to the string. */
17059
d521ce57 17060static const char *
36586728
TT
17061read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17062{
17063 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17064
17065 if (dwz->str.buffer == NULL)
17066 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17067 "section [in module %s]"),
17068 bfd_get_filename (dwz->dwz_bfd));
17069 if (str_offset >= dwz->str.size)
17070 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17071 ".debug_str section [in module %s]"),
17072 bfd_get_filename (dwz->dwz_bfd));
17073 gdb_assert (HOST_CHAR_BIT == 8);
17074 if (dwz->str.buffer[str_offset] == '\0')
17075 return NULL;
d521ce57 17076 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17077}
17078
43988095
JK
17079/* Return pointer to string at .debug_str offset as read from BUF.
17080 BUF is assumed to be in a compilation unit described by CU_HEADER.
17081 Return *BYTES_READ_PTR count of bytes read from BUF. */
17082
d521ce57
TT
17083static const char *
17084read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17085 const struct comp_unit_head *cu_header,
17086 unsigned int *bytes_read_ptr)
17087{
17088 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17089
17090 return read_indirect_string_at_offset (abfd, str_offset);
17091}
17092
43988095
JK
17093/* Return pointer to string at .debug_line_str offset as read from BUF.
17094 BUF is assumed to be in a compilation unit described by CU_HEADER.
17095 Return *BYTES_READ_PTR count of bytes read from BUF. */
17096
17097static const char *
17098read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17099 const struct comp_unit_head *cu_header,
17100 unsigned int *bytes_read_ptr)
17101{
17102 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17103
17104 return read_indirect_line_string_at_offset (abfd, str_offset);
17105}
17106
17107ULONGEST
d521ce57 17108read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17109 unsigned int *bytes_read_ptr)
c906108c 17110{
12df843f 17111 ULONGEST result;
ce5d95e1 17112 unsigned int num_read;
870f88f7 17113 int shift;
c906108c
SS
17114 unsigned char byte;
17115
17116 result = 0;
17117 shift = 0;
17118 num_read = 0;
c906108c
SS
17119 while (1)
17120 {
fe1b8b76 17121 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17122 buf++;
17123 num_read++;
12df843f 17124 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17125 if ((byte & 128) == 0)
17126 {
17127 break;
17128 }
17129 shift += 7;
17130 }
17131 *bytes_read_ptr = num_read;
17132 return result;
17133}
17134
12df843f 17135static LONGEST
d521ce57
TT
17136read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17137 unsigned int *bytes_read_ptr)
c906108c 17138{
12df843f 17139 LONGEST result;
870f88f7 17140 int shift, num_read;
c906108c
SS
17141 unsigned char byte;
17142
17143 result = 0;
17144 shift = 0;
c906108c 17145 num_read = 0;
c906108c
SS
17146 while (1)
17147 {
fe1b8b76 17148 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17149 buf++;
17150 num_read++;
12df843f 17151 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17152 shift += 7;
17153 if ((byte & 128) == 0)
17154 {
17155 break;
17156 }
17157 }
77e0b926 17158 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17159 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17160 *bytes_read_ptr = num_read;
17161 return result;
17162}
17163
3019eac3
DE
17164/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17165 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17166 ADDR_SIZE is the size of addresses from the CU header. */
17167
17168static CORE_ADDR
17169read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17170{
17171 struct objfile *objfile = dwarf2_per_objfile->objfile;
17172 bfd *abfd = objfile->obfd;
17173 const gdb_byte *info_ptr;
17174
17175 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17176 if (dwarf2_per_objfile->addr.buffer == NULL)
17177 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17178 objfile_name (objfile));
3019eac3
DE
17179 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17180 error (_("DW_FORM_addr_index pointing outside of "
17181 ".debug_addr section [in module %s]"),
4262abfb 17182 objfile_name (objfile));
3019eac3
DE
17183 info_ptr = (dwarf2_per_objfile->addr.buffer
17184 + addr_base + addr_index * addr_size);
17185 if (addr_size == 4)
17186 return bfd_get_32 (abfd, info_ptr);
17187 else
17188 return bfd_get_64 (abfd, info_ptr);
17189}
17190
17191/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17192
17193static CORE_ADDR
17194read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17195{
17196 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17197}
17198
17199/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17200
17201static CORE_ADDR
d521ce57 17202read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17203 unsigned int *bytes_read)
17204{
17205 bfd *abfd = cu->objfile->obfd;
17206 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17207
17208 return read_addr_index (cu, addr_index);
17209}
17210
17211/* Data structure to pass results from dwarf2_read_addr_index_reader
17212 back to dwarf2_read_addr_index. */
17213
17214struct dwarf2_read_addr_index_data
17215{
17216 ULONGEST addr_base;
17217 int addr_size;
17218};
17219
17220/* die_reader_func for dwarf2_read_addr_index. */
17221
17222static void
17223dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17224 const gdb_byte *info_ptr,
3019eac3
DE
17225 struct die_info *comp_unit_die,
17226 int has_children,
17227 void *data)
17228{
17229 struct dwarf2_cu *cu = reader->cu;
17230 struct dwarf2_read_addr_index_data *aidata =
17231 (struct dwarf2_read_addr_index_data *) data;
17232
17233 aidata->addr_base = cu->addr_base;
17234 aidata->addr_size = cu->header.addr_size;
17235}
17236
17237/* Given an index in .debug_addr, fetch the value.
17238 NOTE: This can be called during dwarf expression evaluation,
17239 long after the debug information has been read, and thus per_cu->cu
17240 may no longer exist. */
17241
17242CORE_ADDR
17243dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17244 unsigned int addr_index)
17245{
17246 struct objfile *objfile = per_cu->objfile;
17247 struct dwarf2_cu *cu = per_cu->cu;
17248 ULONGEST addr_base;
17249 int addr_size;
17250
17251 /* This is intended to be called from outside this file. */
17252 dw2_setup (objfile);
17253
17254 /* We need addr_base and addr_size.
17255 If we don't have PER_CU->cu, we have to get it.
17256 Nasty, but the alternative is storing the needed info in PER_CU,
17257 which at this point doesn't seem justified: it's not clear how frequently
17258 it would get used and it would increase the size of every PER_CU.
17259 Entry points like dwarf2_per_cu_addr_size do a similar thing
17260 so we're not in uncharted territory here.
17261 Alas we need to be a bit more complicated as addr_base is contained
17262 in the DIE.
17263
17264 We don't need to read the entire CU(/TU).
17265 We just need the header and top level die.
a1b64ce1 17266
3019eac3 17267 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17268 For now we skip this optimization. */
3019eac3
DE
17269
17270 if (cu != NULL)
17271 {
17272 addr_base = cu->addr_base;
17273 addr_size = cu->header.addr_size;
17274 }
17275 else
17276 {
17277 struct dwarf2_read_addr_index_data aidata;
17278
a1b64ce1
DE
17279 /* Note: We can't use init_cutu_and_read_dies_simple here,
17280 we need addr_base. */
17281 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17282 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17283 addr_base = aidata.addr_base;
17284 addr_size = aidata.addr_size;
17285 }
17286
17287 return read_addr_index_1 (addr_index, addr_base, addr_size);
17288}
17289
57d63ce2
DE
17290/* Given a DW_FORM_GNU_str_index, fetch the string.
17291 This is only used by the Fission support. */
3019eac3 17292
d521ce57 17293static const char *
342587c4 17294read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17295{
17296 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17297 const char *objf_name = objfile_name (objfile);
3019eac3 17298 bfd *abfd = objfile->obfd;
342587c4 17299 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17300 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17301 struct dwarf2_section_info *str_offsets_section =
17302 &reader->dwo_file->sections.str_offsets;
d521ce57 17303 const gdb_byte *info_ptr;
3019eac3 17304 ULONGEST str_offset;
57d63ce2 17305 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17306
73869dc2
DE
17307 dwarf2_read_section (objfile, str_section);
17308 dwarf2_read_section (objfile, str_offsets_section);
17309 if (str_section->buffer == NULL)
57d63ce2 17310 error (_("%s used without .debug_str.dwo section"
3019eac3 17311 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17312 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17313 if (str_offsets_section->buffer == NULL)
57d63ce2 17314 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 17315 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17316 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17317 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17318 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 17319 " section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17320 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17321 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17322 + str_index * cu->header.offset_size);
17323 if (cu->header.offset_size == 4)
17324 str_offset = bfd_get_32 (abfd, info_ptr);
17325 else
17326 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17327 if (str_offset >= str_section->size)
57d63ce2 17328 error (_("Offset from %s pointing outside of"
3019eac3 17329 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17330 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17331 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17332}
17333
3019eac3
DE
17334/* Return the length of an LEB128 number in BUF. */
17335
17336static int
17337leb128_size (const gdb_byte *buf)
17338{
17339 const gdb_byte *begin = buf;
17340 gdb_byte byte;
17341
17342 while (1)
17343 {
17344 byte = *buf++;
17345 if ((byte & 128) == 0)
17346 return buf - begin;
17347 }
17348}
17349
c906108c 17350static void
e142c38c 17351set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17352{
17353 switch (lang)
17354 {
17355 case DW_LANG_C89:
76bee0cc 17356 case DW_LANG_C99:
0cfd832f 17357 case DW_LANG_C11:
c906108c 17358 case DW_LANG_C:
d1be3247 17359 case DW_LANG_UPC:
e142c38c 17360 cu->language = language_c;
c906108c 17361 break;
9c37b5ae 17362 case DW_LANG_Java:
c906108c 17363 case DW_LANG_C_plus_plus:
0cfd832f
MW
17364 case DW_LANG_C_plus_plus_11:
17365 case DW_LANG_C_plus_plus_14:
e142c38c 17366 cu->language = language_cplus;
c906108c 17367 break;
6aecb9c2
JB
17368 case DW_LANG_D:
17369 cu->language = language_d;
17370 break;
c906108c
SS
17371 case DW_LANG_Fortran77:
17372 case DW_LANG_Fortran90:
b21b22e0 17373 case DW_LANG_Fortran95:
f7de9aab
MW
17374 case DW_LANG_Fortran03:
17375 case DW_LANG_Fortran08:
e142c38c 17376 cu->language = language_fortran;
c906108c 17377 break;
a766d390
DE
17378 case DW_LANG_Go:
17379 cu->language = language_go;
17380 break;
c906108c 17381 case DW_LANG_Mips_Assembler:
e142c38c 17382 cu->language = language_asm;
c906108c
SS
17383 break;
17384 case DW_LANG_Ada83:
8aaf0b47 17385 case DW_LANG_Ada95:
bc5f45f8
JB
17386 cu->language = language_ada;
17387 break;
72019c9c
GM
17388 case DW_LANG_Modula2:
17389 cu->language = language_m2;
17390 break;
fe8e67fd
PM
17391 case DW_LANG_Pascal83:
17392 cu->language = language_pascal;
17393 break;
22566fbd
DJ
17394 case DW_LANG_ObjC:
17395 cu->language = language_objc;
17396 break;
c44af4eb
TT
17397 case DW_LANG_Rust:
17398 case DW_LANG_Rust_old:
17399 cu->language = language_rust;
17400 break;
c906108c
SS
17401 case DW_LANG_Cobol74:
17402 case DW_LANG_Cobol85:
c906108c 17403 default:
e142c38c 17404 cu->language = language_minimal;
c906108c
SS
17405 break;
17406 }
e142c38c 17407 cu->language_defn = language_def (cu->language);
c906108c
SS
17408}
17409
17410/* Return the named attribute or NULL if not there. */
17411
17412static struct attribute *
e142c38c 17413dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17414{
a48e046c 17415 for (;;)
c906108c 17416 {
a48e046c
TT
17417 unsigned int i;
17418 struct attribute *spec = NULL;
17419
17420 for (i = 0; i < die->num_attrs; ++i)
17421 {
17422 if (die->attrs[i].name == name)
17423 return &die->attrs[i];
17424 if (die->attrs[i].name == DW_AT_specification
17425 || die->attrs[i].name == DW_AT_abstract_origin)
17426 spec = &die->attrs[i];
17427 }
17428
17429 if (!spec)
17430 break;
c906108c 17431
f2f0e013 17432 die = follow_die_ref (die, spec, &cu);
f2f0e013 17433 }
c5aa993b 17434
c906108c
SS
17435 return NULL;
17436}
17437
348e048f
DE
17438/* Return the named attribute or NULL if not there,
17439 but do not follow DW_AT_specification, etc.
17440 This is for use in contexts where we're reading .debug_types dies.
17441 Following DW_AT_specification, DW_AT_abstract_origin will take us
17442 back up the chain, and we want to go down. */
17443
17444static struct attribute *
45e58e77 17445dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17446{
17447 unsigned int i;
17448
17449 for (i = 0; i < die->num_attrs; ++i)
17450 if (die->attrs[i].name == name)
17451 return &die->attrs[i];
17452
17453 return NULL;
17454}
17455
7d45c7c3
KB
17456/* Return the string associated with a string-typed attribute, or NULL if it
17457 is either not found or is of an incorrect type. */
17458
17459static const char *
17460dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17461{
17462 struct attribute *attr;
17463 const char *str = NULL;
17464
17465 attr = dwarf2_attr (die, name, cu);
17466
17467 if (attr != NULL)
17468 {
43988095
JK
17469 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17470 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17471 str = DW_STRING (attr);
17472 else
17473 complaint (&symfile_complaints,
17474 _("string type expected for attribute %s for "
17475 "DIE at 0x%x in module %s"),
17476 dwarf_attr_name (name), die->offset.sect_off,
17477 objfile_name (cu->objfile));
17478 }
17479
17480 return str;
17481}
17482
05cf31d1
JB
17483/* Return non-zero iff the attribute NAME is defined for the given DIE,
17484 and holds a non-zero value. This function should only be used for
2dc7f7b3 17485 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17486
17487static int
17488dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17489{
17490 struct attribute *attr = dwarf2_attr (die, name, cu);
17491
17492 return (attr && DW_UNSND (attr));
17493}
17494
3ca72b44 17495static int
e142c38c 17496die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17497{
05cf31d1
JB
17498 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17499 which value is non-zero. However, we have to be careful with
17500 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17501 (via dwarf2_flag_true_p) follows this attribute. So we may
17502 end up accidently finding a declaration attribute that belongs
17503 to a different DIE referenced by the specification attribute,
17504 even though the given DIE does not have a declaration attribute. */
17505 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17506 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17507}
17508
63d06c5c 17509/* Return the die giving the specification for DIE, if there is
f2f0e013 17510 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17511 containing the return value on output. If there is no
17512 specification, but there is an abstract origin, that is
17513 returned. */
63d06c5c
DC
17514
17515static struct die_info *
f2f0e013 17516die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17517{
f2f0e013
DJ
17518 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17519 *spec_cu);
63d06c5c 17520
edb3359d
DJ
17521 if (spec_attr == NULL)
17522 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17523
63d06c5c
DC
17524 if (spec_attr == NULL)
17525 return NULL;
17526 else
f2f0e013 17527 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17528}
c906108c 17529
debd256d 17530/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
17531 refers to.
17532 NOTE: This is also used as a "cleanup" function. */
17533
debd256d
JB
17534static void
17535free_line_header (struct line_header *lh)
17536{
17537 if (lh->standard_opcode_lengths)
a8bc7b56 17538 xfree (lh->standard_opcode_lengths);
debd256d
JB
17539
17540 /* Remember that all the lh->file_names[i].name pointers are
17541 pointers into debug_line_buffer, and don't need to be freed. */
17542 if (lh->file_names)
a8bc7b56 17543 xfree (lh->file_names);
debd256d
JB
17544
17545 /* Similarly for the include directory names. */
17546 if (lh->include_dirs)
a8bc7b56 17547 xfree (lh->include_dirs);
debd256d 17548
a8bc7b56 17549 xfree (lh);
debd256d
JB
17550}
17551
527f3840
JK
17552/* Stub for free_line_header to match void * callback types. */
17553
17554static void
17555free_line_header_voidp (void *arg)
17556{
9a3c8263 17557 struct line_header *lh = (struct line_header *) arg;
527f3840
JK
17558
17559 free_line_header (lh);
17560}
17561
debd256d 17562/* Add an entry to LH's include directory table. */
ae2de4f8 17563
debd256d 17564static void
d521ce57 17565add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 17566{
27e0867f
DE
17567 if (dwarf_line_debug >= 2)
17568 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17569 lh->num_include_dirs + 1, include_dir);
17570
debd256d
JB
17571 /* Grow the array if necessary. */
17572 if (lh->include_dirs_size == 0)
c5aa993b 17573 {
debd256d 17574 lh->include_dirs_size = 1; /* for testing */
8d749320 17575 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
debd256d
JB
17576 }
17577 else if (lh->num_include_dirs >= lh->include_dirs_size)
17578 {
17579 lh->include_dirs_size *= 2;
8d749320
SM
17580 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17581 lh->include_dirs_size);
c5aa993b 17582 }
c906108c 17583
debd256d
JB
17584 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17585}
6e70227d 17586
debd256d 17587/* Add an entry to LH's file name table. */
ae2de4f8 17588
debd256d
JB
17589static void
17590add_file_name (struct line_header *lh,
d521ce57 17591 const char *name,
debd256d
JB
17592 unsigned int dir_index,
17593 unsigned int mod_time,
17594 unsigned int length)
17595{
17596 struct file_entry *fe;
17597
27e0867f
DE
17598 if (dwarf_line_debug >= 2)
17599 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17600 lh->num_file_names + 1, name);
17601
debd256d
JB
17602 /* Grow the array if necessary. */
17603 if (lh->file_names_size == 0)
17604 {
17605 lh->file_names_size = 1; /* for testing */
8d749320 17606 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
debd256d
JB
17607 }
17608 else if (lh->num_file_names >= lh->file_names_size)
17609 {
17610 lh->file_names_size *= 2;
224c3ddb
SM
17611 lh->file_names
17612 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
debd256d
JB
17613 }
17614
17615 fe = &lh->file_names[lh->num_file_names++];
17616 fe->name = name;
17617 fe->dir_index = dir_index;
17618 fe->mod_time = mod_time;
17619 fe->length = length;
aaa75496 17620 fe->included_p = 0;
cb1df416 17621 fe->symtab = NULL;
debd256d 17622}
6e70227d 17623
83769d0b 17624/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17625
17626static struct dwarf2_section_info *
17627get_debug_line_section (struct dwarf2_cu *cu)
17628{
17629 struct dwarf2_section_info *section;
17630
17631 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17632 DWO file. */
17633 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17634 section = &cu->dwo_unit->dwo_file->sections.line;
17635 else if (cu->per_cu->is_dwz)
17636 {
17637 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17638
17639 section = &dwz->line;
17640 }
17641 else
17642 section = &dwarf2_per_objfile->line;
17643
17644 return section;
17645}
17646
43988095
JK
17647/* Forwarding function for read_formatted_entries. */
17648
17649static void
17650add_include_dir_stub (struct line_header *lh, const char *name,
17651 unsigned int dir_index, unsigned int mod_time,
17652 unsigned int length)
17653{
17654 add_include_dir (lh, name);
17655}
17656
17657/* Read directory or file name entry format, starting with byte of
17658 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17659 entries count and the entries themselves in the described entry
17660 format. */
17661
17662static void
17663read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17664 struct line_header *lh,
17665 const struct comp_unit_head *cu_header,
17666 void (*callback) (struct line_header *lh,
17667 const char *name,
17668 unsigned int dir_index,
17669 unsigned int mod_time,
17670 unsigned int length))
17671{
17672 gdb_byte format_count, formati;
17673 ULONGEST data_count, datai;
17674 const gdb_byte *buf = *bufp;
17675 const gdb_byte *format_header_data;
17676 int i;
17677 unsigned int bytes_read;
17678
17679 format_count = read_1_byte (abfd, buf);
17680 buf += 1;
17681 format_header_data = buf;
17682 for (formati = 0; formati < format_count; formati++)
17683 {
17684 read_unsigned_leb128 (abfd, buf, &bytes_read);
17685 buf += bytes_read;
17686 read_unsigned_leb128 (abfd, buf, &bytes_read);
17687 buf += bytes_read;
17688 }
17689
17690 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17691 buf += bytes_read;
17692 for (datai = 0; datai < data_count; datai++)
17693 {
17694 const gdb_byte *format = format_header_data;
17695 struct file_entry fe;
17696
17697 memset (&fe, 0, sizeof (fe));
17698
17699 for (formati = 0; formati < format_count; formati++)
17700 {
17701 ULONGEST content_type, form;
17702 const char *string_trash;
17703 const char **stringp = &string_trash;
17704 unsigned int uint_trash, *uintp = &uint_trash;
17705
17706 content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17707 format += bytes_read;
17708 switch (content_type)
17709 {
17710 case DW_LNCT_path:
17711 stringp = &fe.name;
17712 break;
17713 case DW_LNCT_directory_index:
17714 uintp = &fe.dir_index;
17715 break;
17716 case DW_LNCT_timestamp:
17717 uintp = &fe.mod_time;
17718 break;
17719 case DW_LNCT_size:
17720 uintp = &fe.length;
17721 break;
17722 case DW_LNCT_MD5:
17723 break;
17724 default:
17725 complaint (&symfile_complaints,
17726 _("Unknown format content type %s"),
17727 pulongest (content_type));
17728 }
17729
17730 form = read_unsigned_leb128 (abfd, format, &bytes_read);
17731 format += bytes_read;
17732 switch (form)
17733 {
17734 case DW_FORM_string:
17735 *stringp = read_direct_string (abfd, buf, &bytes_read);
17736 buf += bytes_read;
17737 break;
17738
17739 case DW_FORM_line_strp:
17740 *stringp = read_indirect_line_string (abfd, buf, cu_header, &bytes_read);
17741 buf += bytes_read;
17742 break;
17743
17744 case DW_FORM_data1:
17745 *uintp = read_1_byte (abfd, buf);
17746 buf += 1;
17747 break;
17748
17749 case DW_FORM_data2:
17750 *uintp = read_2_bytes (abfd, buf);
17751 buf += 2;
17752 break;
17753
17754 case DW_FORM_data4:
17755 *uintp = read_4_bytes (abfd, buf);
17756 buf += 4;
17757 break;
17758
17759 case DW_FORM_data8:
17760 *uintp = read_8_bytes (abfd, buf);
17761 buf += 8;
17762 break;
17763
17764 case DW_FORM_udata:
17765 *uintp = read_unsigned_leb128 (abfd, buf, &bytes_read);
17766 buf += bytes_read;
17767 break;
17768
17769 case DW_FORM_block:
17770 /* It is valid only for DW_LNCT_timestamp which is ignored by
17771 current GDB. */
17772 break;
17773 }
17774 }
17775
17776 callback (lh, fe.name, fe.dir_index, fe.mod_time, fe.length);
17777 }
17778
17779 *bufp = buf;
17780}
17781
debd256d 17782/* Read the statement program header starting at OFFSET in
3019eac3 17783 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17784 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17785 Returns NULL if there is a problem reading the header, e.g., if it
17786 has a version we don't understand.
debd256d
JB
17787
17788 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17789 the returned object point into the dwarf line section buffer,
17790 and must not be freed. */
ae2de4f8 17791
debd256d 17792static struct line_header *
3019eac3 17793dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
17794{
17795 struct cleanup *back_to;
17796 struct line_header *lh;
d521ce57 17797 const gdb_byte *line_ptr;
c764a876 17798 unsigned int bytes_read, offset_size;
debd256d 17799 int i;
d521ce57 17800 const char *cur_dir, *cur_file;
3019eac3
DE
17801 struct dwarf2_section_info *section;
17802 bfd *abfd;
17803
36586728 17804 section = get_debug_line_section (cu);
3019eac3
DE
17805 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17806 if (section->buffer == NULL)
debd256d 17807 {
3019eac3
DE
17808 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17809 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17810 else
17811 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17812 return 0;
17813 }
17814
fceca515
DE
17815 /* We can't do this until we know the section is non-empty.
17816 Only then do we know we have such a section. */
a32a8923 17817 abfd = get_section_bfd_owner (section);
fceca515 17818
a738430d
MK
17819 /* Make sure that at least there's room for the total_length field.
17820 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 17821 if (offset + 4 >= section->size)
debd256d 17822 {
4d3c2250 17823 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17824 return 0;
17825 }
17826
8d749320 17827 lh = XNEW (struct line_header);
debd256d
JB
17828 memset (lh, 0, sizeof (*lh));
17829 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17830 (void *) lh);
17831
527f3840
JK
17832 lh->offset.sect_off = offset;
17833 lh->offset_in_dwz = cu->per_cu->is_dwz;
17834
3019eac3 17835 line_ptr = section->buffer + offset;
debd256d 17836
a738430d 17837 /* Read in the header. */
6e70227d 17838 lh->total_length =
c764a876
DE
17839 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17840 &bytes_read, &offset_size);
debd256d 17841 line_ptr += bytes_read;
3019eac3 17842 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17843 {
4d3c2250 17844 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 17845 do_cleanups (back_to);
debd256d
JB
17846 return 0;
17847 }
17848 lh->statement_program_end = line_ptr + lh->total_length;
17849 lh->version = read_2_bytes (abfd, line_ptr);
17850 line_ptr += 2;
43988095 17851 if (lh->version > 5)
cd366ee8
DE
17852 {
17853 /* This is a version we don't understand. The format could have
17854 changed in ways we don't handle properly so just punt. */
17855 complaint (&symfile_complaints,
17856 _("unsupported version in .debug_line section"));
17857 return NULL;
17858 }
43988095
JK
17859 if (lh->version >= 5)
17860 {
17861 gdb_byte segment_selector_size;
17862
17863 /* Skip address size. */
17864 read_1_byte (abfd, line_ptr);
17865 line_ptr += 1;
17866
17867 segment_selector_size = read_1_byte (abfd, line_ptr);
17868 line_ptr += 1;
17869 if (segment_selector_size != 0)
17870 {
17871 complaint (&symfile_complaints,
17872 _("unsupported segment selector size %u "
17873 "in .debug_line section"),
17874 segment_selector_size);
17875 return NULL;
17876 }
17877 }
c764a876
DE
17878 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17879 line_ptr += offset_size;
debd256d
JB
17880 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17881 line_ptr += 1;
2dc7f7b3
TT
17882 if (lh->version >= 4)
17883 {
17884 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17885 line_ptr += 1;
17886 }
17887 else
17888 lh->maximum_ops_per_instruction = 1;
17889
17890 if (lh->maximum_ops_per_instruction == 0)
17891 {
17892 lh->maximum_ops_per_instruction = 1;
17893 complaint (&symfile_complaints,
3e43a32a
MS
17894 _("invalid maximum_ops_per_instruction "
17895 "in `.debug_line' section"));
2dc7f7b3
TT
17896 }
17897
debd256d
JB
17898 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17899 line_ptr += 1;
17900 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17901 line_ptr += 1;
17902 lh->line_range = read_1_byte (abfd, line_ptr);
17903 line_ptr += 1;
17904 lh->opcode_base = read_1_byte (abfd, line_ptr);
17905 line_ptr += 1;
8d749320 17906 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
debd256d
JB
17907
17908 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17909 for (i = 1; i < lh->opcode_base; ++i)
17910 {
17911 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17912 line_ptr += 1;
17913 }
17914
43988095 17915 if (lh->version >= 5)
debd256d 17916 {
43988095
JK
17917 /* Read directory table. */
17918 read_formatted_entries (abfd, &line_ptr, lh, &cu->header,
17919 add_include_dir_stub);
debd256d 17920
43988095
JK
17921 /* Read file name table. */
17922 read_formatted_entries (abfd, &line_ptr, lh, &cu->header, add_file_name);
17923 }
17924 else
debd256d 17925 {
43988095
JK
17926 /* Read directory table. */
17927 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17928 {
17929 line_ptr += bytes_read;
17930 add_include_dir (lh, cur_dir);
17931 }
debd256d
JB
17932 line_ptr += bytes_read;
17933
43988095
JK
17934 /* Read file name table. */
17935 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17936 {
17937 unsigned int dir_index, mod_time, length;
17938
17939 line_ptr += bytes_read;
17940 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17941 line_ptr += bytes_read;
17942 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17943 line_ptr += bytes_read;
17944 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17945 line_ptr += bytes_read;
17946
17947 add_file_name (lh, cur_file, dir_index, mod_time, length);
17948 }
17949 line_ptr += bytes_read;
debd256d 17950 }
6e70227d 17951 lh->statement_program_start = line_ptr;
debd256d 17952
3019eac3 17953 if (line_ptr > (section->buffer + section->size))
4d3c2250 17954 complaint (&symfile_complaints,
3e43a32a
MS
17955 _("line number info header doesn't "
17956 "fit in `.debug_line' section"));
debd256d
JB
17957
17958 discard_cleanups (back_to);
17959 return lh;
17960}
c906108c 17961
c6da4cef
DE
17962/* Subroutine of dwarf_decode_lines to simplify it.
17963 Return the file name of the psymtab for included file FILE_INDEX
17964 in line header LH of PST.
17965 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17966 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17967 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17968
17969 The function creates dangling cleanup registration. */
c6da4cef 17970
d521ce57 17971static const char *
c6da4cef
DE
17972psymtab_include_file_name (const struct line_header *lh, int file_index,
17973 const struct partial_symtab *pst,
17974 const char *comp_dir)
17975{
17976 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
17977 const char *include_name = fe.name;
17978 const char *include_name_to_compare = include_name;
17979 const char *dir_name = NULL;
72b9f47f
TT
17980 const char *pst_filename;
17981 char *copied_name = NULL;
c6da4cef
DE
17982 int file_is_pst;
17983
afa6c9ab 17984 if (fe.dir_index && lh->include_dirs != NULL)
c6da4cef
DE
17985 dir_name = lh->include_dirs[fe.dir_index - 1];
17986
17987 if (!IS_ABSOLUTE_PATH (include_name)
17988 && (dir_name != NULL || comp_dir != NULL))
17989 {
17990 /* Avoid creating a duplicate psymtab for PST.
17991 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17992 Before we do the comparison, however, we need to account
17993 for DIR_NAME and COMP_DIR.
17994 First prepend dir_name (if non-NULL). If we still don't
17995 have an absolute path prepend comp_dir (if non-NULL).
17996 However, the directory we record in the include-file's
17997 psymtab does not contain COMP_DIR (to match the
17998 corresponding symtab(s)).
17999
18000 Example:
18001
18002 bash$ cd /tmp
18003 bash$ gcc -g ./hello.c
18004 include_name = "hello.c"
18005 dir_name = "."
18006 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18007 DW_AT_name = "./hello.c"
18008
18009 */
c6da4cef
DE
18010
18011 if (dir_name != NULL)
18012 {
d521ce57
TT
18013 char *tem = concat (dir_name, SLASH_STRING,
18014 include_name, (char *)NULL);
18015
18016 make_cleanup (xfree, tem);
18017 include_name = tem;
c6da4cef 18018 include_name_to_compare = include_name;
c6da4cef
DE
18019 }
18020 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18021 {
d521ce57
TT
18022 char *tem = concat (comp_dir, SLASH_STRING,
18023 include_name, (char *)NULL);
18024
18025 make_cleanup (xfree, tem);
18026 include_name_to_compare = tem;
c6da4cef
DE
18027 }
18028 }
18029
18030 pst_filename = pst->filename;
18031 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18032 {
72b9f47f
TT
18033 copied_name = concat (pst->dirname, SLASH_STRING,
18034 pst_filename, (char *)NULL);
18035 pst_filename = copied_name;
c6da4cef
DE
18036 }
18037
1e3fad37 18038 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18039
72b9f47f
TT
18040 if (copied_name != NULL)
18041 xfree (copied_name);
c6da4cef
DE
18042
18043 if (file_is_pst)
18044 return NULL;
18045 return include_name;
18046}
18047
d9b3de22
DE
18048/* State machine to track the state of the line number program. */
18049
18050typedef struct
18051{
18052 /* These are part of the standard DWARF line number state machine. */
18053
18054 unsigned char op_index;
18055 unsigned int file;
18056 unsigned int line;
18057 CORE_ADDR address;
18058 int is_stmt;
18059 unsigned int discriminator;
18060
18061 /* Additional bits of state we need to track. */
18062
18063 /* The last file that we called dwarf2_start_subfile for.
18064 This is only used for TLLs. */
18065 unsigned int last_file;
18066 /* The last file a line number was recorded for. */
18067 struct subfile *last_subfile;
18068
18069 /* The function to call to record a line. */
18070 record_line_ftype *record_line;
18071
18072 /* The last line number that was recorded, used to coalesce
18073 consecutive entries for the same line. This can happen, for
18074 example, when discriminators are present. PR 17276. */
18075 unsigned int last_line;
18076 int line_has_non_zero_discriminator;
18077} lnp_state_machine;
18078
18079/* There's a lot of static state to pass to dwarf_record_line.
18080 This keeps it all together. */
18081
18082typedef struct
18083{
18084 /* The gdbarch. */
18085 struct gdbarch *gdbarch;
18086
18087 /* The line number header. */
18088 struct line_header *line_header;
18089
18090 /* Non-zero if we're recording lines.
18091 Otherwise we're building partial symtabs and are just interested in
18092 finding include files mentioned by the line number program. */
18093 int record_lines_p;
18094} lnp_reader_state;
18095
c91513d8
PP
18096/* Ignore this record_line request. */
18097
18098static void
18099noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18100{
18101 return;
18102}
18103
a05a36a5
DE
18104/* Return non-zero if we should add LINE to the line number table.
18105 LINE is the line to add, LAST_LINE is the last line that was added,
18106 LAST_SUBFILE is the subfile for LAST_LINE.
18107 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18108 had a non-zero discriminator.
18109
18110 We have to be careful in the presence of discriminators.
18111 E.g., for this line:
18112
18113 for (i = 0; i < 100000; i++);
18114
18115 clang can emit four line number entries for that one line,
18116 each with a different discriminator.
18117 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18118
18119 However, we want gdb to coalesce all four entries into one.
18120 Otherwise the user could stepi into the middle of the line and
18121 gdb would get confused about whether the pc really was in the
18122 middle of the line.
18123
18124 Things are further complicated by the fact that two consecutive
18125 line number entries for the same line is a heuristic used by gcc
18126 to denote the end of the prologue. So we can't just discard duplicate
18127 entries, we have to be selective about it. The heuristic we use is
18128 that we only collapse consecutive entries for the same line if at least
18129 one of those entries has a non-zero discriminator. PR 17276.
18130
18131 Note: Addresses in the line number state machine can never go backwards
18132 within one sequence, thus this coalescing is ok. */
18133
18134static int
18135dwarf_record_line_p (unsigned int line, unsigned int last_line,
18136 int line_has_non_zero_discriminator,
18137 struct subfile *last_subfile)
18138{
18139 if (current_subfile != last_subfile)
18140 return 1;
18141 if (line != last_line)
18142 return 1;
18143 /* Same line for the same file that we've seen already.
18144 As a last check, for pr 17276, only record the line if the line
18145 has never had a non-zero discriminator. */
18146 if (!line_has_non_zero_discriminator)
18147 return 1;
18148 return 0;
18149}
18150
252a6764
DE
18151/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18152 in the line table of subfile SUBFILE. */
18153
18154static void
d9b3de22
DE
18155dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18156 unsigned int line, CORE_ADDR address,
18157 record_line_ftype p_record_line)
252a6764
DE
18158{
18159 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18160
27e0867f
DE
18161 if (dwarf_line_debug)
18162 {
18163 fprintf_unfiltered (gdb_stdlog,
18164 "Recording line %u, file %s, address %s\n",
18165 line, lbasename (subfile->name),
18166 paddress (gdbarch, address));
18167 }
18168
d5962de5 18169 (*p_record_line) (subfile, line, addr);
252a6764
DE
18170}
18171
18172/* Subroutine of dwarf_decode_lines_1 to simplify it.
18173 Mark the end of a set of line number records.
d9b3de22 18174 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18175 If SUBFILE is NULL the request is ignored. */
18176
18177static void
18178dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18179 CORE_ADDR address, record_line_ftype p_record_line)
18180{
27e0867f
DE
18181 if (subfile == NULL)
18182 return;
18183
18184 if (dwarf_line_debug)
18185 {
18186 fprintf_unfiltered (gdb_stdlog,
18187 "Finishing current line, file %s, address %s\n",
18188 lbasename (subfile->name),
18189 paddress (gdbarch, address));
18190 }
18191
d9b3de22
DE
18192 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18193}
18194
18195/* Record the line in STATE.
18196 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18197
18198static void
18199dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18200 int end_sequence)
18201{
18202 const struct line_header *lh = reader->line_header;
18203 unsigned int file, line, discriminator;
18204 int is_stmt;
18205
18206 file = state->file;
18207 line = state->line;
18208 is_stmt = state->is_stmt;
18209 discriminator = state->discriminator;
18210
18211 if (dwarf_line_debug)
18212 {
18213 fprintf_unfiltered (gdb_stdlog,
18214 "Processing actual line %u: file %u,"
18215 " address %s, is_stmt %u, discrim %u\n",
18216 line, file,
18217 paddress (reader->gdbarch, state->address),
18218 is_stmt, discriminator);
18219 }
18220
18221 if (file == 0 || file - 1 >= lh->num_file_names)
18222 dwarf2_debug_line_missing_file_complaint ();
18223 /* For now we ignore lines not starting on an instruction boundary.
18224 But not when processing end_sequence for compatibility with the
18225 previous version of the code. */
18226 else if (state->op_index == 0 || end_sequence)
18227 {
18228 lh->file_names[file - 1].included_p = 1;
18229 if (reader->record_lines_p && is_stmt)
18230 {
e815d2d2 18231 if (state->last_subfile != current_subfile || end_sequence)
d9b3de22
DE
18232 {
18233 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18234 state->address, state->record_line);
18235 }
18236
18237 if (!end_sequence)
18238 {
18239 if (dwarf_record_line_p (line, state->last_line,
18240 state->line_has_non_zero_discriminator,
18241 state->last_subfile))
18242 {
18243 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18244 line, state->address,
18245 state->record_line);
18246 }
18247 state->last_subfile = current_subfile;
18248 state->last_line = line;
18249 }
18250 }
18251 }
18252}
18253
18254/* Initialize STATE for the start of a line number program. */
18255
18256static void
18257init_lnp_state_machine (lnp_state_machine *state,
18258 const lnp_reader_state *reader)
18259{
18260 memset (state, 0, sizeof (*state));
18261
18262 /* Just starting, there is no "last file". */
18263 state->last_file = 0;
18264 state->last_subfile = NULL;
18265
18266 state->record_line = record_line;
18267
18268 state->last_line = 0;
18269 state->line_has_non_zero_discriminator = 0;
18270
18271 /* Initialize these according to the DWARF spec. */
18272 state->op_index = 0;
18273 state->file = 1;
18274 state->line = 1;
18275 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18276 was a line entry for it so that the backend has a chance to adjust it
18277 and also record it in case it needs it. This is currently used by MIPS
18278 code, cf. `mips_adjust_dwarf2_line'. */
18279 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18280 state->is_stmt = reader->line_header->default_is_stmt;
18281 state->discriminator = 0;
252a6764
DE
18282}
18283
924c2928
DE
18284/* Check address and if invalid nop-out the rest of the lines in this
18285 sequence. */
18286
18287static void
d9b3de22 18288check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
924c2928
DE
18289 const gdb_byte *line_ptr,
18290 CORE_ADDR lowpc, CORE_ADDR address)
18291{
18292 /* If address < lowpc then it's not a usable value, it's outside the
18293 pc range of the CU. However, we restrict the test to only address
18294 values of zero to preserve GDB's previous behaviour which is to
18295 handle the specific case of a function being GC'd by the linker. */
18296
18297 if (address == 0 && address < lowpc)
18298 {
18299 /* This line table is for a function which has been
18300 GCd by the linker. Ignore it. PR gdb/12528 */
18301
18302 struct objfile *objfile = cu->objfile;
18303 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18304
18305 complaint (&symfile_complaints,
18306 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18307 line_offset, objfile_name (objfile));
d9b3de22
DE
18308 state->record_line = noop_record_line;
18309 /* Note: sm.record_line is left as noop_record_line
924c2928
DE
18310 until we see DW_LNE_end_sequence. */
18311 }
18312}
18313
f3f5162e 18314/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18315 Process the line number information in LH.
18316 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18317 program in order to set included_p for every referenced header. */
debd256d 18318
c906108c 18319static void
43f3e411
DE
18320dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18321 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18322{
d521ce57
TT
18323 const gdb_byte *line_ptr, *extended_end;
18324 const gdb_byte *line_end;
a8c50c1f 18325 unsigned int bytes_read, extended_len;
699ca60a 18326 unsigned char op_code, extended_op;
e142c38c
DJ
18327 CORE_ADDR baseaddr;
18328 struct objfile *objfile = cu->objfile;
f3f5162e 18329 bfd *abfd = objfile->obfd;
fbf65064 18330 struct gdbarch *gdbarch = get_objfile_arch (objfile);
d9b3de22
DE
18331 /* Non-zero if we're recording line info (as opposed to building partial
18332 symtabs). */
18333 int record_lines_p = !decode_for_pst_p;
18334 /* A collection of things we need to pass to dwarf_record_line. */
18335 lnp_reader_state reader_state;
e142c38c
DJ
18336
18337 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18338
debd256d
JB
18339 line_ptr = lh->statement_program_start;
18340 line_end = lh->statement_program_end;
c906108c 18341
d9b3de22
DE
18342 reader_state.gdbarch = gdbarch;
18343 reader_state.line_header = lh;
18344 reader_state.record_lines_p = record_lines_p;
18345
c906108c
SS
18346 /* Read the statement sequences until there's nothing left. */
18347 while (line_ptr < line_end)
18348 {
d9b3de22
DE
18349 /* The DWARF line number program state machine. */
18350 lnp_state_machine state_machine;
c906108c 18351 int end_sequence = 0;
d9b3de22
DE
18352
18353 /* Reset the state machine at the start of each sequence. */
18354 init_lnp_state_machine (&state_machine, &reader_state);
18355
18356 if (record_lines_p && lh->num_file_names >= state_machine.file)
c906108c 18357 {
aaa75496 18358 /* Start a subfile for the current file of the state machine. */
debd256d
JB
18359 /* lh->include_dirs and lh->file_names are 0-based, but the
18360 directory and file name numbers in the statement program
18361 are 1-based. */
d9b3de22 18362 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
d521ce57 18363 const char *dir = NULL;
a738430d 18364
afa6c9ab 18365 if (fe->dir_index && lh->include_dirs != NULL)
debd256d 18366 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb 18367
4d663531 18368 dwarf2_start_subfile (fe->name, dir);
c906108c
SS
18369 }
18370
a738430d 18371 /* Decode the table. */
d9b3de22 18372 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18373 {
18374 op_code = read_1_byte (abfd, line_ptr);
18375 line_ptr += 1;
9aa1fe7e 18376
debd256d 18377 if (op_code >= lh->opcode_base)
6e70227d 18378 {
8e07a239 18379 /* Special opcode. */
699ca60a 18380 unsigned char adj_opcode;
3e29f34a 18381 CORE_ADDR addr_adj;
a05a36a5 18382 int line_delta;
8e07a239 18383
debd256d 18384 adj_opcode = op_code - lh->opcode_base;
d9b3de22
DE
18385 addr_adj = (((state_machine.op_index
18386 + (adj_opcode / lh->line_range))
2dc7f7b3
TT
18387 / lh->maximum_ops_per_instruction)
18388 * lh->minimum_instruction_length);
d9b3de22
DE
18389 state_machine.address
18390 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18391 state_machine.op_index = ((state_machine.op_index
18392 + (adj_opcode / lh->line_range))
18393 % lh->maximum_ops_per_instruction);
a05a36a5 18394 line_delta = lh->line_base + (adj_opcode % lh->line_range);
d9b3de22 18395 state_machine.line += line_delta;
a05a36a5 18396 if (line_delta != 0)
d9b3de22
DE
18397 state_machine.line_has_non_zero_discriminator
18398 = state_machine.discriminator != 0;
18399
18400 dwarf_record_line (&reader_state, &state_machine, 0);
18401 state_machine.discriminator = 0;
9aa1fe7e
GK
18402 }
18403 else switch (op_code)
c906108c
SS
18404 {
18405 case DW_LNS_extended_op:
3e43a32a
MS
18406 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18407 &bytes_read);
473b7be6 18408 line_ptr += bytes_read;
a8c50c1f 18409 extended_end = line_ptr + extended_len;
c906108c
SS
18410 extended_op = read_1_byte (abfd, line_ptr);
18411 line_ptr += 1;
18412 switch (extended_op)
18413 {
18414 case DW_LNE_end_sequence:
d9b3de22 18415 state_machine.record_line = record_line;
c906108c 18416 end_sequence = 1;
c906108c
SS
18417 break;
18418 case DW_LNE_set_address:
d9b3de22
DE
18419 {
18420 CORE_ADDR address
18421 = read_address (abfd, line_ptr, cu, &bytes_read);
18422
18423 line_ptr += bytes_read;
18424 check_line_address (cu, &state_machine, line_ptr,
18425 lowpc, address);
18426 state_machine.op_index = 0;
18427 address += baseaddr;
18428 state_machine.address
18429 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18430 }
c906108c
SS
18431 break;
18432 case DW_LNE_define_file:
debd256d 18433 {
d521ce57 18434 const char *cur_file;
debd256d 18435 unsigned int dir_index, mod_time, length;
6e70227d 18436
3e43a32a
MS
18437 cur_file = read_direct_string (abfd, line_ptr,
18438 &bytes_read);
debd256d
JB
18439 line_ptr += bytes_read;
18440 dir_index =
18441 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18442 line_ptr += bytes_read;
18443 mod_time =
18444 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18445 line_ptr += bytes_read;
18446 length =
18447 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18448 line_ptr += bytes_read;
18449 add_file_name (lh, cur_file, dir_index, mod_time, length);
18450 }
c906108c 18451 break;
d0c6ba3d
CC
18452 case DW_LNE_set_discriminator:
18453 /* The discriminator is not interesting to the debugger;
a05a36a5
DE
18454 just ignore it. We still need to check its value though:
18455 if there are consecutive entries for the same
18456 (non-prologue) line we want to coalesce them.
18457 PR 17276. */
d9b3de22
DE
18458 state_machine.discriminator
18459 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18460 state_machine.line_has_non_zero_discriminator
18461 |= state_machine.discriminator != 0;
a05a36a5 18462 line_ptr += bytes_read;
d0c6ba3d 18463 break;
c906108c 18464 default:
4d3c2250 18465 complaint (&symfile_complaints,
e2e0b3e5 18466 _("mangled .debug_line section"));
debd256d 18467 return;
c906108c 18468 }
a8c50c1f
DJ
18469 /* Make sure that we parsed the extended op correctly. If e.g.
18470 we expected a different address size than the producer used,
18471 we may have read the wrong number of bytes. */
18472 if (line_ptr != extended_end)
18473 {
18474 complaint (&symfile_complaints,
18475 _("mangled .debug_line section"));
18476 return;
18477 }
c906108c
SS
18478 break;
18479 case DW_LNS_copy:
d9b3de22
DE
18480 dwarf_record_line (&reader_state, &state_machine, 0);
18481 state_machine.discriminator = 0;
c906108c
SS
18482 break;
18483 case DW_LNS_advance_pc:
2dc7f7b3
TT
18484 {
18485 CORE_ADDR adjust
18486 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3e29f34a 18487 CORE_ADDR addr_adj;
2dc7f7b3 18488
d9b3de22 18489 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18490 / lh->maximum_ops_per_instruction)
18491 * lh->minimum_instruction_length);
d9b3de22
DE
18492 state_machine.address
18493 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18494 state_machine.op_index = ((state_machine.op_index + adjust)
18495 % lh->maximum_ops_per_instruction);
2dc7f7b3
TT
18496 line_ptr += bytes_read;
18497 }
c906108c
SS
18498 break;
18499 case DW_LNS_advance_line:
a05a36a5
DE
18500 {
18501 int line_delta
18502 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18503
d9b3de22 18504 state_machine.line += line_delta;
a05a36a5 18505 if (line_delta != 0)
d9b3de22
DE
18506 state_machine.line_has_non_zero_discriminator
18507 = state_machine.discriminator != 0;
a05a36a5
DE
18508 line_ptr += bytes_read;
18509 }
c906108c
SS
18510 break;
18511 case DW_LNS_set_file:
d9b3de22
DE
18512 {
18513 /* The arrays lh->include_dirs and lh->file_names are
18514 0-based, but the directory and file name numbers in
18515 the statement program are 1-based. */
18516 struct file_entry *fe;
18517 const char *dir = NULL;
18518
18519 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18520 &bytes_read);
18521 line_ptr += bytes_read;
18522 if (state_machine.file == 0
18523 || state_machine.file - 1 >= lh->num_file_names)
18524 dwarf2_debug_line_missing_file_complaint ();
18525 else
18526 {
18527 fe = &lh->file_names[state_machine.file - 1];
18528 if (fe->dir_index && lh->include_dirs != NULL)
18529 dir = lh->include_dirs[fe->dir_index - 1];
18530 if (record_lines_p)
18531 {
18532 state_machine.last_subfile = current_subfile;
18533 state_machine.line_has_non_zero_discriminator
18534 = state_machine.discriminator != 0;
18535 dwarf2_start_subfile (fe->name, dir);
18536 }
18537 }
18538 }
c906108c
SS
18539 break;
18540 case DW_LNS_set_column:
0ad93d4f 18541 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18542 line_ptr += bytes_read;
18543 break;
18544 case DW_LNS_negate_stmt:
d9b3de22 18545 state_machine.is_stmt = (!state_machine.is_stmt);
c906108c
SS
18546 break;
18547 case DW_LNS_set_basic_block:
c906108c 18548 break;
c2c6d25f
JM
18549 /* Add to the address register of the state machine the
18550 address increment value corresponding to special opcode
a738430d
MK
18551 255. I.e., this value is scaled by the minimum
18552 instruction length since special opcode 255 would have
b021a221 18553 scaled the increment. */
c906108c 18554 case DW_LNS_const_add_pc:
2dc7f7b3
TT
18555 {
18556 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
3e29f34a 18557 CORE_ADDR addr_adj;
2dc7f7b3 18558
d9b3de22 18559 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18560 / lh->maximum_ops_per_instruction)
18561 * lh->minimum_instruction_length);
d9b3de22
DE
18562 state_machine.address
18563 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18564 state_machine.op_index = ((state_machine.op_index + adjust)
18565 % lh->maximum_ops_per_instruction);
2dc7f7b3 18566 }
c906108c
SS
18567 break;
18568 case DW_LNS_fixed_advance_pc:
3e29f34a
MR
18569 {
18570 CORE_ADDR addr_adj;
18571
18572 addr_adj = read_2_bytes (abfd, line_ptr);
d9b3de22
DE
18573 state_machine.address
18574 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18575 state_machine.op_index = 0;
3e29f34a
MR
18576 line_ptr += 2;
18577 }
c906108c 18578 break;
9aa1fe7e 18579 default:
a738430d
MK
18580 {
18581 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18582 int i;
a738430d 18583
debd256d 18584 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18585 {
18586 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18587 line_ptr += bytes_read;
18588 }
18589 }
c906108c
SS
18590 }
18591 }
d9b3de22
DE
18592
18593 if (!end_sequence)
18594 dwarf2_debug_line_missing_end_sequence_complaint ();
18595
18596 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18597 in which case we still finish recording the last line). */
18598 dwarf_record_line (&reader_state, &state_machine, 1);
c906108c 18599 }
f3f5162e
DE
18600}
18601
18602/* Decode the Line Number Program (LNP) for the given line_header
18603 structure and CU. The actual information extracted and the type
18604 of structures created from the LNP depends on the value of PST.
18605
18606 1. If PST is NULL, then this procedure uses the data from the program
18607 to create all necessary symbol tables, and their linetables.
18608
18609 2. If PST is not NULL, this procedure reads the program to determine
18610 the list of files included by the unit represented by PST, and
18611 builds all the associated partial symbol tables.
18612
18613 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18614 It is used for relative paths in the line table.
18615 NOTE: When processing partial symtabs (pst != NULL),
18616 comp_dir == pst->dirname.
18617
18618 NOTE: It is important that psymtabs have the same file name (via strcmp)
18619 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18620 symtab we don't use it in the name of the psymtabs we create.
18621 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18622 A good testcase for this is mb-inline.exp.
18623
527f3840
JK
18624 LOWPC is the lowest address in CU (or 0 if not known).
18625
18626 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18627 for its PC<->lines mapping information. Otherwise only the filename
18628 table is read in. */
f3f5162e
DE
18629
18630static void
18631dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18632 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18633 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18634{
18635 struct objfile *objfile = cu->objfile;
18636 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18637
527f3840
JK
18638 if (decode_mapping)
18639 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18640
18641 if (decode_for_pst_p)
18642 {
18643 int file_index;
18644
18645 /* Now that we're done scanning the Line Header Program, we can
18646 create the psymtab of each included file. */
18647 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18648 if (lh->file_names[file_index].included_p == 1)
18649 {
d521ce57 18650 const char *include_name =
c6da4cef
DE
18651 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18652 if (include_name != NULL)
aaa75496
JB
18653 dwarf2_create_include_psymtab (include_name, pst, objfile);
18654 }
18655 }
cb1df416
DJ
18656 else
18657 {
18658 /* Make sure a symtab is created for every file, even files
18659 which contain only variables (i.e. no code with associated
18660 line numbers). */
43f3e411 18661 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18662 int i;
cb1df416
DJ
18663
18664 for (i = 0; i < lh->num_file_names; i++)
18665 {
d521ce57 18666 const char *dir = NULL;
f3f5162e 18667 struct file_entry *fe;
9a619af0 18668
cb1df416 18669 fe = &lh->file_names[i];
afa6c9ab 18670 if (fe->dir_index && lh->include_dirs != NULL)
cb1df416 18671 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 18672 dwarf2_start_subfile (fe->name, dir);
cb1df416 18673
cb1df416 18674 if (current_subfile->symtab == NULL)
43f3e411
DE
18675 {
18676 current_subfile->symtab
18677 = allocate_symtab (cust, current_subfile->name);
18678 }
cb1df416
DJ
18679 fe->symtab = current_subfile->symtab;
18680 }
18681 }
c906108c
SS
18682}
18683
18684/* Start a subfile for DWARF. FILENAME is the name of the file and
18685 DIRNAME the name of the source directory which contains FILENAME
4d663531 18686 or NULL if not known.
c906108c
SS
18687 This routine tries to keep line numbers from identical absolute and
18688 relative file names in a common subfile.
18689
18690 Using the `list' example from the GDB testsuite, which resides in
18691 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18692 of /srcdir/list0.c yields the following debugging information for list0.c:
18693
c5aa993b 18694 DW_AT_name: /srcdir/list0.c
4d663531 18695 DW_AT_comp_dir: /compdir
357e46e7 18696 files.files[0].name: list0.h
c5aa993b 18697 files.files[0].dir: /srcdir
357e46e7 18698 files.files[1].name: list0.c
c5aa993b 18699 files.files[1].dir: /srcdir
c906108c
SS
18700
18701 The line number information for list0.c has to end up in a single
4f1520fb
FR
18702 subfile, so that `break /srcdir/list0.c:1' works as expected.
18703 start_subfile will ensure that this happens provided that we pass the
18704 concatenation of files.files[1].dir and files.files[1].name as the
18705 subfile's name. */
c906108c
SS
18706
18707static void
4d663531 18708dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18709{
d521ce57 18710 char *copy = NULL;
4f1520fb 18711
4d663531 18712 /* In order not to lose the line information directory,
4f1520fb
FR
18713 we concatenate it to the filename when it makes sense.
18714 Note that the Dwarf3 standard says (speaking of filenames in line
18715 information): ``The directory index is ignored for file names
18716 that represent full path names''. Thus ignoring dirname in the
18717 `else' branch below isn't an issue. */
c906108c 18718
d5166ae1 18719 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18720 {
18721 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18722 filename = copy;
18723 }
c906108c 18724
4d663531 18725 start_subfile (filename);
4f1520fb 18726
d521ce57
TT
18727 if (copy != NULL)
18728 xfree (copy);
c906108c
SS
18729}
18730
f4dc4d17
DE
18731/* Start a symtab for DWARF.
18732 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18733
43f3e411 18734static struct compunit_symtab *
f4dc4d17 18735dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18736 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18737{
43f3e411
DE
18738 struct compunit_symtab *cust
18739 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18740
f4dc4d17
DE
18741 record_debugformat ("DWARF 2");
18742 record_producer (cu->producer);
18743
18744 /* We assume that we're processing GCC output. */
18745 processing_gcc_compilation = 2;
18746
4d4ec4e5 18747 cu->processing_has_namespace_info = 0;
43f3e411
DE
18748
18749 return cust;
f4dc4d17
DE
18750}
18751
4c2df51b
DJ
18752static void
18753var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18754 struct dwarf2_cu *cu)
4c2df51b 18755{
e7c27a73
DJ
18756 struct objfile *objfile = cu->objfile;
18757 struct comp_unit_head *cu_header = &cu->header;
18758
4c2df51b
DJ
18759 /* NOTE drow/2003-01-30: There used to be a comment and some special
18760 code here to turn a symbol with DW_AT_external and a
18761 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18762 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18763 with some versions of binutils) where shared libraries could have
18764 relocations against symbols in their debug information - the
18765 minimal symbol would have the right address, but the debug info
18766 would not. It's no longer necessary, because we will explicitly
18767 apply relocations when we read in the debug information now. */
18768
18769 /* A DW_AT_location attribute with no contents indicates that a
18770 variable has been optimized away. */
18771 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18772 {
f1e6e072 18773 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18774 return;
18775 }
18776
18777 /* Handle one degenerate form of location expression specially, to
18778 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18779 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18780 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18781
18782 if (attr_form_is_block (attr)
3019eac3
DE
18783 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18784 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18785 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18786 && (DW_BLOCK (attr)->size
18787 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18788 {
891d2f0b 18789 unsigned int dummy;
4c2df51b 18790
3019eac3
DE
18791 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18792 SYMBOL_VALUE_ADDRESS (sym) =
18793 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18794 else
18795 SYMBOL_VALUE_ADDRESS (sym) =
18796 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18797 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18798 fixup_symbol_section (sym, objfile);
18799 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18800 SYMBOL_SECTION (sym));
4c2df51b
DJ
18801 return;
18802 }
18803
18804 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18805 expression evaluator, and use LOC_COMPUTED only when necessary
18806 (i.e. when the value of a register or memory location is
18807 referenced, or a thread-local block, etc.). Then again, it might
18808 not be worthwhile. I'm assuming that it isn't unless performance
18809 or memory numbers show me otherwise. */
18810
f1e6e072 18811 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18812
f1e6e072 18813 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18814 cu->has_loclist = 1;
4c2df51b
DJ
18815}
18816
c906108c
SS
18817/* Given a pointer to a DWARF information entry, figure out if we need
18818 to make a symbol table entry for it, and if so, create a new entry
18819 and return a pointer to it.
18820 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18821 used the passed type.
18822 If SPACE is not NULL, use it to hold the new symbol. If it is
18823 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18824
18825static struct symbol *
34eaf542
TT
18826new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18827 struct symbol *space)
c906108c 18828{
e7c27a73 18829 struct objfile *objfile = cu->objfile;
3e29f34a 18830 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18831 struct symbol *sym = NULL;
15d034d0 18832 const char *name;
c906108c
SS
18833 struct attribute *attr = NULL;
18834 struct attribute *attr2 = NULL;
e142c38c 18835 CORE_ADDR baseaddr;
e37fd15a
SW
18836 struct pending **list_to_add = NULL;
18837
edb3359d 18838 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18839
18840 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18841
94af9270 18842 name = dwarf2_name (die, cu);
c906108c
SS
18843 if (name)
18844 {
94af9270 18845 const char *linkagename;
34eaf542 18846 int suppress_add = 0;
94af9270 18847
34eaf542
TT
18848 if (space)
18849 sym = space;
18850 else
e623cf5d 18851 sym = allocate_symbol (objfile);
c906108c 18852 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18853
18854 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18855 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18856 linkagename = dwarf2_physname (name, die, cu);
18857 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18858
f55ee35c
JK
18859 /* Fortran does not have mangling standard and the mangling does differ
18860 between gfortran, iFort etc. */
18861 if (cu->language == language_fortran
b250c185 18862 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18863 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18864 dwarf2_full_name (name, die, cu),
29df156d 18865 NULL);
f55ee35c 18866
c906108c 18867 /* Default assumptions.
c5aa993b 18868 Use the passed type or decode it from the die. */
176620f1 18869 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18870 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18871 if (type != NULL)
18872 SYMBOL_TYPE (sym) = type;
18873 else
e7c27a73 18874 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18875 attr = dwarf2_attr (die,
18876 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18877 cu);
c906108c
SS
18878 if (attr)
18879 {
18880 SYMBOL_LINE (sym) = DW_UNSND (attr);
18881 }
cb1df416 18882
edb3359d
DJ
18883 attr = dwarf2_attr (die,
18884 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18885 cu);
cb1df416
DJ
18886 if (attr)
18887 {
18888 int file_index = DW_UNSND (attr);
9a619af0 18889
cb1df416
DJ
18890 if (cu->line_header == NULL
18891 || file_index > cu->line_header->num_file_names)
18892 complaint (&symfile_complaints,
18893 _("file index out of range"));
1c3d648d 18894 else if (file_index > 0)
cb1df416
DJ
18895 {
18896 struct file_entry *fe;
9a619af0 18897
cb1df416 18898 fe = &cu->line_header->file_names[file_index - 1];
08be3fe3 18899 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18900 }
18901 }
18902
c906108c
SS
18903 switch (die->tag)
18904 {
18905 case DW_TAG_label:
e142c38c 18906 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18907 if (attr)
3e29f34a
MR
18908 {
18909 CORE_ADDR addr;
18910
18911 addr = attr_value_as_address (attr);
18912 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18913 SYMBOL_VALUE_ADDRESS (sym) = addr;
18914 }
0f5238ed
TT
18915 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18916 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18917 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18918 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18919 break;
18920 case DW_TAG_subprogram:
18921 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18922 finish_block. */
f1e6e072 18923 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18924 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18925 if ((attr2 && (DW_UNSND (attr2) != 0))
18926 || cu->language == language_ada)
c906108c 18927 {
2cfa0c8d
JB
18928 /* Subprograms marked external are stored as a global symbol.
18929 Ada subprograms, whether marked external or not, are always
18930 stored as a global symbol, because we want to be able to
18931 access them globally. For instance, we want to be able
18932 to break on a nested subprogram without having to
18933 specify the context. */
e37fd15a 18934 list_to_add = &global_symbols;
c906108c
SS
18935 }
18936 else
18937 {
e37fd15a 18938 list_to_add = cu->list_in_scope;
c906108c
SS
18939 }
18940 break;
edb3359d
DJ
18941 case DW_TAG_inlined_subroutine:
18942 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18943 finish_block. */
f1e6e072 18944 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 18945 SYMBOL_INLINED (sym) = 1;
481860b3 18946 list_to_add = cu->list_in_scope;
edb3359d 18947 break;
34eaf542
TT
18948 case DW_TAG_template_value_param:
18949 suppress_add = 1;
18950 /* Fall through. */
72929c62 18951 case DW_TAG_constant:
c906108c 18952 case DW_TAG_variable:
254e6b9e 18953 case DW_TAG_member:
0963b4bd
MS
18954 /* Compilation with minimal debug info may result in
18955 variables with missing type entries. Change the
18956 misleading `void' type to something sensible. */
c906108c 18957 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 18958 SYMBOL_TYPE (sym)
46bf5051 18959 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 18960
e142c38c 18961 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
18962 /* In the case of DW_TAG_member, we should only be called for
18963 static const members. */
18964 if (die->tag == DW_TAG_member)
18965 {
3863f96c
DE
18966 /* dwarf2_add_field uses die_is_declaration,
18967 so we do the same. */
254e6b9e
DE
18968 gdb_assert (die_is_declaration (die, cu));
18969 gdb_assert (attr);
18970 }
c906108c
SS
18971 if (attr)
18972 {
e7c27a73 18973 dwarf2_const_value (attr, sym, cu);
e142c38c 18974 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 18975 if (!suppress_add)
34eaf542
TT
18976 {
18977 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 18978 list_to_add = &global_symbols;
34eaf542 18979 else
e37fd15a 18980 list_to_add = cu->list_in_scope;
34eaf542 18981 }
c906108c
SS
18982 break;
18983 }
e142c38c 18984 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18985 if (attr)
18986 {
e7c27a73 18987 var_decode_location (attr, sym, cu);
e142c38c 18988 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
18989
18990 /* Fortran explicitly imports any global symbols to the local
18991 scope by DW_TAG_common_block. */
18992 if (cu->language == language_fortran && die->parent
18993 && die->parent->tag == DW_TAG_common_block)
18994 attr2 = NULL;
18995
caac4577
JG
18996 if (SYMBOL_CLASS (sym) == LOC_STATIC
18997 && SYMBOL_VALUE_ADDRESS (sym) == 0
18998 && !dwarf2_per_objfile->has_section_at_zero)
18999 {
19000 /* When a static variable is eliminated by the linker,
19001 the corresponding debug information is not stripped
19002 out, but the variable address is set to null;
19003 do not add such variables into symbol table. */
19004 }
19005 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19006 {
f55ee35c
JK
19007 /* Workaround gfortran PR debug/40040 - it uses
19008 DW_AT_location for variables in -fPIC libraries which may
19009 get overriden by other libraries/executable and get
19010 a different address. Resolve it by the minimal symbol
19011 which may come from inferior's executable using copy
19012 relocation. Make this workaround only for gfortran as for
19013 other compilers GDB cannot guess the minimal symbol
19014 Fortran mangling kind. */
19015 if (cu->language == language_fortran && die->parent
19016 && die->parent->tag == DW_TAG_module
19017 && cu->producer
28586665 19018 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19019 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19020
1c809c68
TT
19021 /* A variable with DW_AT_external is never static,
19022 but it may be block-scoped. */
19023 list_to_add = (cu->list_in_scope == &file_symbols
19024 ? &global_symbols : cu->list_in_scope);
1c809c68 19025 }
c906108c 19026 else
e37fd15a 19027 list_to_add = cu->list_in_scope;
c906108c
SS
19028 }
19029 else
19030 {
19031 /* We do not know the address of this symbol.
c5aa993b
JM
19032 If it is an external symbol and we have type information
19033 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19034 The address of the variable will then be determined from
19035 the minimal symbol table whenever the variable is
19036 referenced. */
e142c38c 19037 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19038
19039 /* Fortran explicitly imports any global symbols to the local
19040 scope by DW_TAG_common_block. */
19041 if (cu->language == language_fortran && die->parent
19042 && die->parent->tag == DW_TAG_common_block)
19043 {
19044 /* SYMBOL_CLASS doesn't matter here because
19045 read_common_block is going to reset it. */
19046 if (!suppress_add)
19047 list_to_add = cu->list_in_scope;
19048 }
19049 else if (attr2 && (DW_UNSND (attr2) != 0)
19050 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19051 {
0fe7935b
DJ
19052 /* A variable with DW_AT_external is never static, but it
19053 may be block-scoped. */
19054 list_to_add = (cu->list_in_scope == &file_symbols
19055 ? &global_symbols : cu->list_in_scope);
19056
f1e6e072 19057 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19058 }
442ddf59
JK
19059 else if (!die_is_declaration (die, cu))
19060 {
19061 /* Use the default LOC_OPTIMIZED_OUT class. */
19062 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19063 if (!suppress_add)
19064 list_to_add = cu->list_in_scope;
442ddf59 19065 }
c906108c
SS
19066 }
19067 break;
19068 case DW_TAG_formal_parameter:
edb3359d
DJ
19069 /* If we are inside a function, mark this as an argument. If
19070 not, we might be looking at an argument to an inlined function
19071 when we do not have enough information to show inlined frames;
19072 pretend it's a local variable in that case so that the user can
19073 still see it. */
19074 if (context_stack_depth > 0
19075 && context_stack[context_stack_depth - 1].name != NULL)
19076 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19077 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19078 if (attr)
19079 {
e7c27a73 19080 var_decode_location (attr, sym, cu);
c906108c 19081 }
e142c38c 19082 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19083 if (attr)
19084 {
e7c27a73 19085 dwarf2_const_value (attr, sym, cu);
c906108c 19086 }
f346a30d 19087
e37fd15a 19088 list_to_add = cu->list_in_scope;
c906108c
SS
19089 break;
19090 case DW_TAG_unspecified_parameters:
19091 /* From varargs functions; gdb doesn't seem to have any
19092 interest in this information, so just ignore it for now.
19093 (FIXME?) */
19094 break;
34eaf542
TT
19095 case DW_TAG_template_type_param:
19096 suppress_add = 1;
19097 /* Fall through. */
c906108c 19098 case DW_TAG_class_type:
680b30c7 19099 case DW_TAG_interface_type:
c906108c
SS
19100 case DW_TAG_structure_type:
19101 case DW_TAG_union_type:
72019c9c 19102 case DW_TAG_set_type:
c906108c 19103 case DW_TAG_enumeration_type:
f1e6e072 19104 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19105 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19106
63d06c5c 19107 {
9c37b5ae 19108 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19109 really ever be static objects: otherwise, if you try
19110 to, say, break of a class's method and you're in a file
19111 which doesn't mention that class, it won't work unless
19112 the check for all static symbols in lookup_symbol_aux
19113 saves you. See the OtherFileClass tests in
19114 gdb.c++/namespace.exp. */
19115
e37fd15a 19116 if (!suppress_add)
34eaf542 19117 {
34eaf542 19118 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19119 && cu->language == language_cplus
34eaf542 19120 ? &global_symbols : cu->list_in_scope);
63d06c5c 19121
64382290 19122 /* The semantics of C++ state that "struct foo {
9c37b5ae 19123 ... }" also defines a typedef for "foo". */
64382290 19124 if (cu->language == language_cplus
45280282 19125 || cu->language == language_ada
c44af4eb
TT
19126 || cu->language == language_d
19127 || cu->language == language_rust)
64382290
TT
19128 {
19129 /* The symbol's name is already allocated along
19130 with this objfile, so we don't need to
19131 duplicate it for the type. */
19132 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19133 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19134 }
63d06c5c
DC
19135 }
19136 }
c906108c
SS
19137 break;
19138 case DW_TAG_typedef:
f1e6e072 19139 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19140 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19141 list_to_add = cu->list_in_scope;
63d06c5c 19142 break;
c906108c 19143 case DW_TAG_base_type:
a02abb62 19144 case DW_TAG_subrange_type:
f1e6e072 19145 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19146 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19147 list_to_add = cu->list_in_scope;
c906108c
SS
19148 break;
19149 case DW_TAG_enumerator:
e142c38c 19150 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19151 if (attr)
19152 {
e7c27a73 19153 dwarf2_const_value (attr, sym, cu);
c906108c 19154 }
63d06c5c
DC
19155 {
19156 /* NOTE: carlton/2003-11-10: See comment above in the
19157 DW_TAG_class_type, etc. block. */
19158
e142c38c 19159 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19160 && cu->language == language_cplus
e142c38c 19161 ? &global_symbols : cu->list_in_scope);
63d06c5c 19162 }
c906108c 19163 break;
74921315 19164 case DW_TAG_imported_declaration:
5c4e30ca 19165 case DW_TAG_namespace:
f1e6e072 19166 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19167 list_to_add = &global_symbols;
5c4e30ca 19168 break;
530e8392
KB
19169 case DW_TAG_module:
19170 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19171 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19172 list_to_add = &global_symbols;
19173 break;
4357ac6c 19174 case DW_TAG_common_block:
f1e6e072 19175 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19176 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19177 add_symbol_to_list (sym, cu->list_in_scope);
19178 break;
c906108c
SS
19179 default:
19180 /* Not a tag we recognize. Hopefully we aren't processing
19181 trash data, but since we must specifically ignore things
19182 we don't recognize, there is nothing else we should do at
0963b4bd 19183 this point. */
e2e0b3e5 19184 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19185 dwarf_tag_name (die->tag));
c906108c
SS
19186 break;
19187 }
df8a16a1 19188
e37fd15a
SW
19189 if (suppress_add)
19190 {
19191 sym->hash_next = objfile->template_symbols;
19192 objfile->template_symbols = sym;
19193 list_to_add = NULL;
19194 }
19195
19196 if (list_to_add != NULL)
19197 add_symbol_to_list (sym, list_to_add);
19198
df8a16a1
DJ
19199 /* For the benefit of old versions of GCC, check for anonymous
19200 namespaces based on the demangled name. */
4d4ec4e5 19201 if (!cu->processing_has_namespace_info
94af9270 19202 && cu->language == language_cplus)
a10964d1 19203 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19204 }
19205 return (sym);
19206}
19207
34eaf542
TT
19208/* A wrapper for new_symbol_full that always allocates a new symbol. */
19209
19210static struct symbol *
19211new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19212{
19213 return new_symbol_full (die, type, cu, NULL);
19214}
19215
98bfdba5
PA
19216/* Given an attr with a DW_FORM_dataN value in host byte order,
19217 zero-extend it as appropriate for the symbol's type. The DWARF
19218 standard (v4) is not entirely clear about the meaning of using
19219 DW_FORM_dataN for a constant with a signed type, where the type is
19220 wider than the data. The conclusion of a discussion on the DWARF
19221 list was that this is unspecified. We choose to always zero-extend
19222 because that is the interpretation long in use by GCC. */
c906108c 19223
98bfdba5 19224static gdb_byte *
ff39bb5e 19225dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19226 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19227{
e7c27a73 19228 struct objfile *objfile = cu->objfile;
e17a4113
UW
19229 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19230 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19231 LONGEST l = DW_UNSND (attr);
19232
19233 if (bits < sizeof (*value) * 8)
19234 {
19235 l &= ((LONGEST) 1 << bits) - 1;
19236 *value = l;
19237 }
19238 else if (bits == sizeof (*value) * 8)
19239 *value = l;
19240 else
19241 {
224c3ddb 19242 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19243 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19244 return bytes;
19245 }
19246
19247 return NULL;
19248}
19249
19250/* Read a constant value from an attribute. Either set *VALUE, or if
19251 the value does not fit in *VALUE, set *BYTES - either already
19252 allocated on the objfile obstack, or newly allocated on OBSTACK,
19253 or, set *BATON, if we translated the constant to a location
19254 expression. */
19255
19256static void
ff39bb5e 19257dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19258 const char *name, struct obstack *obstack,
19259 struct dwarf2_cu *cu,
d521ce57 19260 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19261 struct dwarf2_locexpr_baton **baton)
19262{
19263 struct objfile *objfile = cu->objfile;
19264 struct comp_unit_head *cu_header = &cu->header;
c906108c 19265 struct dwarf_block *blk;
98bfdba5
PA
19266 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19267 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19268
19269 *value = 0;
19270 *bytes = NULL;
19271 *baton = NULL;
c906108c
SS
19272
19273 switch (attr->form)
19274 {
19275 case DW_FORM_addr:
3019eac3 19276 case DW_FORM_GNU_addr_index:
ac56253d 19277 {
ac56253d
TT
19278 gdb_byte *data;
19279
98bfdba5
PA
19280 if (TYPE_LENGTH (type) != cu_header->addr_size)
19281 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19282 cu_header->addr_size,
98bfdba5 19283 TYPE_LENGTH (type));
ac56253d
TT
19284 /* Symbols of this form are reasonably rare, so we just
19285 piggyback on the existing location code rather than writing
19286 a new implementation of symbol_computed_ops. */
8d749320 19287 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19288 (*baton)->per_cu = cu->per_cu;
19289 gdb_assert ((*baton)->per_cu);
ac56253d 19290
98bfdba5 19291 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19292 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19293 (*baton)->data = data;
ac56253d
TT
19294
19295 data[0] = DW_OP_addr;
19296 store_unsigned_integer (&data[1], cu_header->addr_size,
19297 byte_order, DW_ADDR (attr));
19298 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19299 }
c906108c 19300 break;
4ac36638 19301 case DW_FORM_string:
93b5768b 19302 case DW_FORM_strp:
3019eac3 19303 case DW_FORM_GNU_str_index:
36586728 19304 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19305 /* DW_STRING is already allocated on the objfile obstack, point
19306 directly to it. */
d521ce57 19307 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19308 break;
c906108c
SS
19309 case DW_FORM_block1:
19310 case DW_FORM_block2:
19311 case DW_FORM_block4:
19312 case DW_FORM_block:
2dc7f7b3 19313 case DW_FORM_exprloc:
0224619f 19314 case DW_FORM_data16:
c906108c 19315 blk = DW_BLOCK (attr);
98bfdba5
PA
19316 if (TYPE_LENGTH (type) != blk->size)
19317 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19318 TYPE_LENGTH (type));
19319 *bytes = blk->data;
c906108c 19320 break;
2df3850c
JM
19321
19322 /* The DW_AT_const_value attributes are supposed to carry the
19323 symbol's value "represented as it would be on the target
19324 architecture." By the time we get here, it's already been
19325 converted to host endianness, so we just need to sign- or
19326 zero-extend it as appropriate. */
19327 case DW_FORM_data1:
3aef2284 19328 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19329 break;
c906108c 19330 case DW_FORM_data2:
3aef2284 19331 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19332 break;
c906108c 19333 case DW_FORM_data4:
3aef2284 19334 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19335 break;
c906108c 19336 case DW_FORM_data8:
3aef2284 19337 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19338 break;
19339
c906108c 19340 case DW_FORM_sdata:
98bfdba5 19341 *value = DW_SND (attr);
2df3850c
JM
19342 break;
19343
c906108c 19344 case DW_FORM_udata:
98bfdba5 19345 *value = DW_UNSND (attr);
c906108c 19346 break;
2df3850c 19347
c906108c 19348 default:
4d3c2250 19349 complaint (&symfile_complaints,
e2e0b3e5 19350 _("unsupported const value attribute form: '%s'"),
4d3c2250 19351 dwarf_form_name (attr->form));
98bfdba5 19352 *value = 0;
c906108c
SS
19353 break;
19354 }
19355}
19356
2df3850c 19357
98bfdba5
PA
19358/* Copy constant value from an attribute to a symbol. */
19359
2df3850c 19360static void
ff39bb5e 19361dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19362 struct dwarf2_cu *cu)
2df3850c 19363{
98bfdba5 19364 struct objfile *objfile = cu->objfile;
12df843f 19365 LONGEST value;
d521ce57 19366 const gdb_byte *bytes;
98bfdba5 19367 struct dwarf2_locexpr_baton *baton;
2df3850c 19368
98bfdba5
PA
19369 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19370 SYMBOL_PRINT_NAME (sym),
19371 &objfile->objfile_obstack, cu,
19372 &value, &bytes, &baton);
2df3850c 19373
98bfdba5
PA
19374 if (baton != NULL)
19375 {
98bfdba5 19376 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19377 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19378 }
19379 else if (bytes != NULL)
19380 {
19381 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19382 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19383 }
19384 else
19385 {
19386 SYMBOL_VALUE (sym) = value;
f1e6e072 19387 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19388 }
2df3850c
JM
19389}
19390
c906108c
SS
19391/* Return the type of the die in question using its DW_AT_type attribute. */
19392
19393static struct type *
e7c27a73 19394die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19395{
c906108c 19396 struct attribute *type_attr;
c906108c 19397
e142c38c 19398 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19399 if (!type_attr)
19400 {
19401 /* A missing DW_AT_type represents a void type. */
46bf5051 19402 return objfile_type (cu->objfile)->builtin_void;
c906108c 19403 }
348e048f 19404
673bfd45 19405 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19406}
19407
b4ba55a1
JB
19408/* True iff CU's producer generates GNAT Ada auxiliary information
19409 that allows to find parallel types through that information instead
19410 of having to do expensive parallel lookups by type name. */
19411
19412static int
19413need_gnat_info (struct dwarf2_cu *cu)
19414{
19415 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19416 of GNAT produces this auxiliary information, without any indication
19417 that it is produced. Part of enhancing the FSF version of GNAT
19418 to produce that information will be to put in place an indicator
19419 that we can use in order to determine whether the descriptive type
19420 info is available or not. One suggestion that has been made is
19421 to use a new attribute, attached to the CU die. For now, assume
19422 that the descriptive type info is not available. */
19423 return 0;
19424}
19425
b4ba55a1
JB
19426/* Return the auxiliary type of the die in question using its
19427 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19428 attribute is not present. */
19429
19430static struct type *
19431die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19432{
b4ba55a1 19433 struct attribute *type_attr;
b4ba55a1
JB
19434
19435 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19436 if (!type_attr)
19437 return NULL;
19438
673bfd45 19439 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19440}
19441
19442/* If DIE has a descriptive_type attribute, then set the TYPE's
19443 descriptive type accordingly. */
19444
19445static void
19446set_descriptive_type (struct type *type, struct die_info *die,
19447 struct dwarf2_cu *cu)
19448{
19449 struct type *descriptive_type = die_descriptive_type (die, cu);
19450
19451 if (descriptive_type)
19452 {
19453 ALLOCATE_GNAT_AUX_TYPE (type);
19454 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19455 }
19456}
19457
c906108c
SS
19458/* Return the containing type of the die in question using its
19459 DW_AT_containing_type attribute. */
19460
19461static struct type *
e7c27a73 19462die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19463{
c906108c 19464 struct attribute *type_attr;
c906108c 19465
e142c38c 19466 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19467 if (!type_attr)
19468 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19469 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19470
673bfd45 19471 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19472}
19473
ac9ec31b
DE
19474/* Return an error marker type to use for the ill formed type in DIE/CU. */
19475
19476static struct type *
19477build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19478{
19479 struct objfile *objfile = dwarf2_per_objfile->objfile;
19480 char *message, *saved;
19481
19482 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19483 objfile_name (objfile),
ac9ec31b
DE
19484 cu->header.offset.sect_off,
19485 die->offset.sect_off);
224c3ddb
SM
19486 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19487 message, strlen (message));
ac9ec31b
DE
19488 xfree (message);
19489
19f392bc 19490 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19491}
19492
673bfd45 19493/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19494 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19495 DW_AT_containing_type.
673bfd45
DE
19496 If there is no type substitute an error marker. */
19497
c906108c 19498static struct type *
ff39bb5e 19499lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19500 struct dwarf2_cu *cu)
c906108c 19501{
bb5ed363 19502 struct objfile *objfile = cu->objfile;
f792889a
DJ
19503 struct type *this_type;
19504
ac9ec31b
DE
19505 gdb_assert (attr->name == DW_AT_type
19506 || attr->name == DW_AT_GNAT_descriptive_type
19507 || attr->name == DW_AT_containing_type);
19508
673bfd45
DE
19509 /* First see if we have it cached. */
19510
36586728
TT
19511 if (attr->form == DW_FORM_GNU_ref_alt)
19512 {
19513 struct dwarf2_per_cu_data *per_cu;
19514 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19515
19516 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19517 this_type = get_die_type_at_offset (offset, per_cu);
19518 }
7771576e 19519 else if (attr_form_is_ref (attr))
673bfd45 19520 {
b64f50a1 19521 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
19522
19523 this_type = get_die_type_at_offset (offset, cu->per_cu);
19524 }
55f1336d 19525 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19526 {
ac9ec31b 19527 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19528
ac9ec31b 19529 return get_signatured_type (die, signature, cu);
673bfd45
DE
19530 }
19531 else
19532 {
ac9ec31b
DE
19533 complaint (&symfile_complaints,
19534 _("Dwarf Error: Bad type attribute %s in DIE"
19535 " at 0x%x [in module %s]"),
19536 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 19537 objfile_name (objfile));
ac9ec31b 19538 return build_error_marker_type (cu, die);
673bfd45
DE
19539 }
19540
19541 /* If not cached we need to read it in. */
19542
19543 if (this_type == NULL)
19544 {
ac9ec31b 19545 struct die_info *type_die = NULL;
673bfd45
DE
19546 struct dwarf2_cu *type_cu = cu;
19547
7771576e 19548 if (attr_form_is_ref (attr))
ac9ec31b
DE
19549 type_die = follow_die_ref (die, attr, &type_cu);
19550 if (type_die == NULL)
19551 return build_error_marker_type (cu, die);
19552 /* If we find the type now, it's probably because the type came
3019eac3
DE
19553 from an inter-CU reference and the type's CU got expanded before
19554 ours. */
ac9ec31b 19555 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19556 }
19557
19558 /* If we still don't have a type use an error marker. */
19559
19560 if (this_type == NULL)
ac9ec31b 19561 return build_error_marker_type (cu, die);
673bfd45 19562
f792889a 19563 return this_type;
c906108c
SS
19564}
19565
673bfd45
DE
19566/* Return the type in DIE, CU.
19567 Returns NULL for invalid types.
19568
02142a6c 19569 This first does a lookup in die_type_hash,
673bfd45
DE
19570 and only reads the die in if necessary.
19571
19572 NOTE: This can be called when reading in partial or full symbols. */
19573
f792889a 19574static struct type *
e7c27a73 19575read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19576{
f792889a
DJ
19577 struct type *this_type;
19578
19579 this_type = get_die_type (die, cu);
19580 if (this_type)
19581 return this_type;
19582
673bfd45
DE
19583 return read_type_die_1 (die, cu);
19584}
19585
19586/* Read the type in DIE, CU.
19587 Returns NULL for invalid types. */
19588
19589static struct type *
19590read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19591{
19592 struct type *this_type = NULL;
19593
c906108c
SS
19594 switch (die->tag)
19595 {
19596 case DW_TAG_class_type:
680b30c7 19597 case DW_TAG_interface_type:
c906108c
SS
19598 case DW_TAG_structure_type:
19599 case DW_TAG_union_type:
f792889a 19600 this_type = read_structure_type (die, cu);
c906108c
SS
19601 break;
19602 case DW_TAG_enumeration_type:
f792889a 19603 this_type = read_enumeration_type (die, cu);
c906108c
SS
19604 break;
19605 case DW_TAG_subprogram:
19606 case DW_TAG_subroutine_type:
edb3359d 19607 case DW_TAG_inlined_subroutine:
f792889a 19608 this_type = read_subroutine_type (die, cu);
c906108c
SS
19609 break;
19610 case DW_TAG_array_type:
f792889a 19611 this_type = read_array_type (die, cu);
c906108c 19612 break;
72019c9c 19613 case DW_TAG_set_type:
f792889a 19614 this_type = read_set_type (die, cu);
72019c9c 19615 break;
c906108c 19616 case DW_TAG_pointer_type:
f792889a 19617 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19618 break;
19619 case DW_TAG_ptr_to_member_type:
f792889a 19620 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19621 break;
19622 case DW_TAG_reference_type:
f792889a 19623 this_type = read_tag_reference_type (die, cu);
c906108c
SS
19624 break;
19625 case DW_TAG_const_type:
f792889a 19626 this_type = read_tag_const_type (die, cu);
c906108c
SS
19627 break;
19628 case DW_TAG_volatile_type:
f792889a 19629 this_type = read_tag_volatile_type (die, cu);
c906108c 19630 break;
06d66ee9
TT
19631 case DW_TAG_restrict_type:
19632 this_type = read_tag_restrict_type (die, cu);
19633 break;
c906108c 19634 case DW_TAG_string_type:
f792889a 19635 this_type = read_tag_string_type (die, cu);
c906108c
SS
19636 break;
19637 case DW_TAG_typedef:
f792889a 19638 this_type = read_typedef (die, cu);
c906108c 19639 break;
a02abb62 19640 case DW_TAG_subrange_type:
f792889a 19641 this_type = read_subrange_type (die, cu);
a02abb62 19642 break;
c906108c 19643 case DW_TAG_base_type:
f792889a 19644 this_type = read_base_type (die, cu);
c906108c 19645 break;
81a17f79 19646 case DW_TAG_unspecified_type:
f792889a 19647 this_type = read_unspecified_type (die, cu);
81a17f79 19648 break;
0114d602
DJ
19649 case DW_TAG_namespace:
19650 this_type = read_namespace_type (die, cu);
19651 break;
f55ee35c
JK
19652 case DW_TAG_module:
19653 this_type = read_module_type (die, cu);
19654 break;
a2c2acaf
MW
19655 case DW_TAG_atomic_type:
19656 this_type = read_tag_atomic_type (die, cu);
19657 break;
c906108c 19658 default:
3e43a32a
MS
19659 complaint (&symfile_complaints,
19660 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19661 dwarf_tag_name (die->tag));
c906108c
SS
19662 break;
19663 }
63d06c5c 19664
f792889a 19665 return this_type;
63d06c5c
DC
19666}
19667
abc72ce4
DE
19668/* See if we can figure out if the class lives in a namespace. We do
19669 this by looking for a member function; its demangled name will
19670 contain namespace info, if there is any.
19671 Return the computed name or NULL.
19672 Space for the result is allocated on the objfile's obstack.
19673 This is the full-die version of guess_partial_die_structure_name.
19674 In this case we know DIE has no useful parent. */
19675
19676static char *
19677guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19678{
19679 struct die_info *spec_die;
19680 struct dwarf2_cu *spec_cu;
19681 struct die_info *child;
19682
19683 spec_cu = cu;
19684 spec_die = die_specification (die, &spec_cu);
19685 if (spec_die != NULL)
19686 {
19687 die = spec_die;
19688 cu = spec_cu;
19689 }
19690
19691 for (child = die->child;
19692 child != NULL;
19693 child = child->sibling)
19694 {
19695 if (child->tag == DW_TAG_subprogram)
19696 {
7d45c7c3 19697 const char *linkage_name;
abc72ce4 19698
7d45c7c3
KB
19699 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19700 if (linkage_name == NULL)
19701 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19702 cu);
19703 if (linkage_name != NULL)
abc72ce4
DE
19704 {
19705 char *actual_name
19706 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19707 linkage_name);
abc72ce4
DE
19708 char *name = NULL;
19709
19710 if (actual_name != NULL)
19711 {
15d034d0 19712 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19713
19714 if (die_name != NULL
19715 && strcmp (die_name, actual_name) != 0)
19716 {
19717 /* Strip off the class name from the full name.
19718 We want the prefix. */
19719 int die_name_len = strlen (die_name);
19720 int actual_name_len = strlen (actual_name);
19721
19722 /* Test for '::' as a sanity check. */
19723 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19724 && actual_name[actual_name_len
19725 - die_name_len - 1] == ':')
224c3ddb
SM
19726 name = (char *) obstack_copy0 (
19727 &cu->objfile->per_bfd->storage_obstack,
19728 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19729 }
19730 }
19731 xfree (actual_name);
19732 return name;
19733 }
19734 }
19735 }
19736
19737 return NULL;
19738}
19739
96408a79
SA
19740/* GCC might emit a nameless typedef that has a linkage name. Determine the
19741 prefix part in such case. See
19742 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19743
19744static char *
19745anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19746{
19747 struct attribute *attr;
e6a959d6 19748 const char *base;
96408a79
SA
19749
19750 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19751 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19752 return NULL;
19753
7d45c7c3 19754 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19755 return NULL;
19756
19757 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19758 if (attr == NULL)
19759 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19760 if (attr == NULL || DW_STRING (attr) == NULL)
19761 return NULL;
19762
19763 /* dwarf2_name had to be already called. */
19764 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19765
19766 /* Strip the base name, keep any leading namespaces/classes. */
19767 base = strrchr (DW_STRING (attr), ':');
19768 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19769 return "";
19770
224c3ddb
SM
19771 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19772 DW_STRING (attr),
19773 &base[-1] - DW_STRING (attr));
96408a79
SA
19774}
19775
fdde2d81 19776/* Return the name of the namespace/class that DIE is defined within,
0114d602 19777 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19778
0114d602
DJ
19779 For example, if we're within the method foo() in the following
19780 code:
19781
19782 namespace N {
19783 class C {
19784 void foo () {
19785 }
19786 };
19787 }
19788
19789 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19790
0d5cff50 19791static const char *
e142c38c 19792determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19793{
0114d602
DJ
19794 struct die_info *parent, *spec_die;
19795 struct dwarf2_cu *spec_cu;
19796 struct type *parent_type;
96408a79 19797 char *retval;
63d06c5c 19798
9c37b5ae 19799 if (cu->language != language_cplus
c44af4eb
TT
19800 && cu->language != language_fortran && cu->language != language_d
19801 && cu->language != language_rust)
0114d602
DJ
19802 return "";
19803
96408a79
SA
19804 retval = anonymous_struct_prefix (die, cu);
19805 if (retval)
19806 return retval;
19807
0114d602
DJ
19808 /* We have to be careful in the presence of DW_AT_specification.
19809 For example, with GCC 3.4, given the code
19810
19811 namespace N {
19812 void foo() {
19813 // Definition of N::foo.
19814 }
19815 }
19816
19817 then we'll have a tree of DIEs like this:
19818
19819 1: DW_TAG_compile_unit
19820 2: DW_TAG_namespace // N
19821 3: DW_TAG_subprogram // declaration of N::foo
19822 4: DW_TAG_subprogram // definition of N::foo
19823 DW_AT_specification // refers to die #3
19824
19825 Thus, when processing die #4, we have to pretend that we're in
19826 the context of its DW_AT_specification, namely the contex of die
19827 #3. */
19828 spec_cu = cu;
19829 spec_die = die_specification (die, &spec_cu);
19830 if (spec_die == NULL)
19831 parent = die->parent;
19832 else
63d06c5c 19833 {
0114d602
DJ
19834 parent = spec_die->parent;
19835 cu = spec_cu;
63d06c5c 19836 }
0114d602
DJ
19837
19838 if (parent == NULL)
19839 return "";
98bfdba5
PA
19840 else if (parent->building_fullname)
19841 {
19842 const char *name;
19843 const char *parent_name;
19844
19845 /* It has been seen on RealView 2.2 built binaries,
19846 DW_TAG_template_type_param types actually _defined_ as
19847 children of the parent class:
19848
19849 enum E {};
19850 template class <class Enum> Class{};
19851 Class<enum E> class_e;
19852
19853 1: DW_TAG_class_type (Class)
19854 2: DW_TAG_enumeration_type (E)
19855 3: DW_TAG_enumerator (enum1:0)
19856 3: DW_TAG_enumerator (enum2:1)
19857 ...
19858 2: DW_TAG_template_type_param
19859 DW_AT_type DW_FORM_ref_udata (E)
19860
19861 Besides being broken debug info, it can put GDB into an
19862 infinite loop. Consider:
19863
19864 When we're building the full name for Class<E>, we'll start
19865 at Class, and go look over its template type parameters,
19866 finding E. We'll then try to build the full name of E, and
19867 reach here. We're now trying to build the full name of E,
19868 and look over the parent DIE for containing scope. In the
19869 broken case, if we followed the parent DIE of E, we'd again
19870 find Class, and once again go look at its template type
19871 arguments, etc., etc. Simply don't consider such parent die
19872 as source-level parent of this die (it can't be, the language
19873 doesn't allow it), and break the loop here. */
19874 name = dwarf2_name (die, cu);
19875 parent_name = dwarf2_name (parent, cu);
19876 complaint (&symfile_complaints,
19877 _("template param type '%s' defined within parent '%s'"),
19878 name ? name : "<unknown>",
19879 parent_name ? parent_name : "<unknown>");
19880 return "";
19881 }
63d06c5c 19882 else
0114d602
DJ
19883 switch (parent->tag)
19884 {
63d06c5c 19885 case DW_TAG_namespace:
0114d602 19886 parent_type = read_type_die (parent, cu);
acebe513
UW
19887 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19888 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19889 Work around this problem here. */
19890 if (cu->language == language_cplus
19891 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19892 return "";
0114d602
DJ
19893 /* We give a name to even anonymous namespaces. */
19894 return TYPE_TAG_NAME (parent_type);
63d06c5c 19895 case DW_TAG_class_type:
680b30c7 19896 case DW_TAG_interface_type:
63d06c5c 19897 case DW_TAG_structure_type:
0114d602 19898 case DW_TAG_union_type:
f55ee35c 19899 case DW_TAG_module:
0114d602
DJ
19900 parent_type = read_type_die (parent, cu);
19901 if (TYPE_TAG_NAME (parent_type) != NULL)
19902 return TYPE_TAG_NAME (parent_type);
19903 else
19904 /* An anonymous structure is only allowed non-static data
19905 members; no typedefs, no member functions, et cetera.
19906 So it does not need a prefix. */
19907 return "";
abc72ce4 19908 case DW_TAG_compile_unit:
95554aad 19909 case DW_TAG_partial_unit:
abc72ce4
DE
19910 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19911 if (cu->language == language_cplus
8b70b953 19912 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19913 && die->child != NULL
19914 && (die->tag == DW_TAG_class_type
19915 || die->tag == DW_TAG_structure_type
19916 || die->tag == DW_TAG_union_type))
19917 {
19918 char *name = guess_full_die_structure_name (die, cu);
19919 if (name != NULL)
19920 return name;
19921 }
19922 return "";
3d567982
TT
19923 case DW_TAG_enumeration_type:
19924 parent_type = read_type_die (parent, cu);
19925 if (TYPE_DECLARED_CLASS (parent_type))
19926 {
19927 if (TYPE_TAG_NAME (parent_type) != NULL)
19928 return TYPE_TAG_NAME (parent_type);
19929 return "";
19930 }
19931 /* Fall through. */
63d06c5c 19932 default:
8176b9b8 19933 return determine_prefix (parent, cu);
63d06c5c 19934 }
63d06c5c
DC
19935}
19936
3e43a32a
MS
19937/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19938 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19939 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19940 an obconcat, otherwise allocate storage for the result. The CU argument is
19941 used to determine the language and hence, the appropriate separator. */
987504bb 19942
f55ee35c 19943#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
19944
19945static char *
f55ee35c
JK
19946typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19947 int physname, struct dwarf2_cu *cu)
63d06c5c 19948{
f55ee35c 19949 const char *lead = "";
5c315b68 19950 const char *sep;
63d06c5c 19951
3e43a32a
MS
19952 if (suffix == NULL || suffix[0] == '\0'
19953 || prefix == NULL || prefix[0] == '\0')
987504bb 19954 sep = "";
45280282
IB
19955 else if (cu->language == language_d)
19956 {
19957 /* For D, the 'main' function could be defined in any module, but it
19958 should never be prefixed. */
19959 if (strcmp (suffix, "D main") == 0)
19960 {
19961 prefix = "";
19962 sep = "";
19963 }
19964 else
19965 sep = ".";
19966 }
f55ee35c
JK
19967 else if (cu->language == language_fortran && physname)
19968 {
19969 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19970 DW_AT_MIPS_linkage_name is preferred and used instead. */
19971
19972 lead = "__";
19973 sep = "_MOD_";
19974 }
987504bb
JJ
19975 else
19976 sep = "::";
63d06c5c 19977
6dd47d34
DE
19978 if (prefix == NULL)
19979 prefix = "";
19980 if (suffix == NULL)
19981 suffix = "";
19982
987504bb
JJ
19983 if (obs == NULL)
19984 {
3e43a32a 19985 char *retval
224c3ddb
SM
19986 = ((char *)
19987 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 19988
f55ee35c
JK
19989 strcpy (retval, lead);
19990 strcat (retval, prefix);
6dd47d34
DE
19991 strcat (retval, sep);
19992 strcat (retval, suffix);
63d06c5c
DC
19993 return retval;
19994 }
987504bb
JJ
19995 else
19996 {
19997 /* We have an obstack. */
f55ee35c 19998 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 19999 }
63d06c5c
DC
20000}
20001
c906108c
SS
20002/* Return sibling of die, NULL if no sibling. */
20003
f9aca02d 20004static struct die_info *
fba45db2 20005sibling_die (struct die_info *die)
c906108c 20006{
639d11d3 20007 return die->sibling;
c906108c
SS
20008}
20009
71c25dea
TT
20010/* Get name of a die, return NULL if not found. */
20011
15d034d0
TT
20012static const char *
20013dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20014 struct obstack *obstack)
20015{
20016 if (name && cu->language == language_cplus)
20017 {
2f408ecb 20018 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20019
2f408ecb 20020 if (!canon_name.empty ())
71c25dea 20021 {
2f408ecb
PA
20022 if (canon_name != name)
20023 name = (const char *) obstack_copy0 (obstack,
20024 canon_name.c_str (),
20025 canon_name.length ());
71c25dea
TT
20026 }
20027 }
20028
20029 return name;
c906108c
SS
20030}
20031
96553a0c
DE
20032/* Get name of a die, return NULL if not found.
20033 Anonymous namespaces are converted to their magic string. */
9219021c 20034
15d034d0 20035static const char *
e142c38c 20036dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20037{
20038 struct attribute *attr;
20039
e142c38c 20040 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20041 if ((!attr || !DW_STRING (attr))
96553a0c 20042 && die->tag != DW_TAG_namespace
53832f31
TT
20043 && die->tag != DW_TAG_class_type
20044 && die->tag != DW_TAG_interface_type
20045 && die->tag != DW_TAG_structure_type
20046 && die->tag != DW_TAG_union_type)
71c25dea
TT
20047 return NULL;
20048
20049 switch (die->tag)
20050 {
20051 case DW_TAG_compile_unit:
95554aad 20052 case DW_TAG_partial_unit:
71c25dea
TT
20053 /* Compilation units have a DW_AT_name that is a filename, not
20054 a source language identifier. */
20055 case DW_TAG_enumeration_type:
20056 case DW_TAG_enumerator:
20057 /* These tags always have simple identifiers already; no need
20058 to canonicalize them. */
20059 return DW_STRING (attr);
907af001 20060
96553a0c
DE
20061 case DW_TAG_namespace:
20062 if (attr != NULL && DW_STRING (attr) != NULL)
20063 return DW_STRING (attr);
20064 return CP_ANONYMOUS_NAMESPACE_STR;
20065
907af001
UW
20066 case DW_TAG_class_type:
20067 case DW_TAG_interface_type:
20068 case DW_TAG_structure_type:
20069 case DW_TAG_union_type:
20070 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20071 structures or unions. These were of the form "._%d" in GCC 4.1,
20072 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20073 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20074 if (attr && DW_STRING (attr)
61012eef
GB
20075 && (startswith (DW_STRING (attr), "._")
20076 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20077 return NULL;
53832f31
TT
20078
20079 /* GCC might emit a nameless typedef that has a linkage name. See
20080 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20081 if (!attr || DW_STRING (attr) == NULL)
20082 {
df5c6c50 20083 char *demangled = NULL;
53832f31
TT
20084
20085 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20086 if (attr == NULL)
20087 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20088
20089 if (attr == NULL || DW_STRING (attr) == NULL)
20090 return NULL;
20091
df5c6c50
JK
20092 /* Avoid demangling DW_STRING (attr) the second time on a second
20093 call for the same DIE. */
20094 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20095 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20096
20097 if (demangled)
20098 {
e6a959d6 20099 const char *base;
96408a79 20100
53832f31 20101 /* FIXME: we already did this for the partial symbol... */
34a68019 20102 DW_STRING (attr)
224c3ddb
SM
20103 = ((const char *)
20104 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20105 demangled, strlen (demangled)));
53832f31
TT
20106 DW_STRING_IS_CANONICAL (attr) = 1;
20107 xfree (demangled);
96408a79
SA
20108
20109 /* Strip any leading namespaces/classes, keep only the base name.
20110 DW_AT_name for named DIEs does not contain the prefixes. */
20111 base = strrchr (DW_STRING (attr), ':');
20112 if (base && base > DW_STRING (attr) && base[-1] == ':')
20113 return &base[1];
20114 else
20115 return DW_STRING (attr);
53832f31
TT
20116 }
20117 }
907af001
UW
20118 break;
20119
71c25dea 20120 default:
907af001
UW
20121 break;
20122 }
20123
20124 if (!DW_STRING_IS_CANONICAL (attr))
20125 {
20126 DW_STRING (attr)
20127 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20128 &cu->objfile->per_bfd->storage_obstack);
907af001 20129 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20130 }
907af001 20131 return DW_STRING (attr);
9219021c
DC
20132}
20133
20134/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20135 is none. *EXT_CU is the CU containing DIE on input, and the CU
20136 containing the return value on output. */
9219021c
DC
20137
20138static struct die_info *
f2f0e013 20139dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20140{
20141 struct attribute *attr;
9219021c 20142
f2f0e013 20143 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20144 if (attr == NULL)
20145 return NULL;
20146
f2f0e013 20147 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20148}
20149
c906108c
SS
20150/* Convert a DIE tag into its string name. */
20151
f39c6ffd 20152static const char *
aa1ee363 20153dwarf_tag_name (unsigned tag)
c906108c 20154{
f39c6ffd
TT
20155 const char *name = get_DW_TAG_name (tag);
20156
20157 if (name == NULL)
20158 return "DW_TAG_<unknown>";
20159
20160 return name;
c906108c
SS
20161}
20162
20163/* Convert a DWARF attribute code into its string name. */
20164
f39c6ffd 20165static const char *
aa1ee363 20166dwarf_attr_name (unsigned attr)
c906108c 20167{
f39c6ffd
TT
20168 const char *name;
20169
c764a876 20170#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20171 if (attr == DW_AT_MIPS_fde)
20172 return "DW_AT_MIPS_fde";
20173#else
20174 if (attr == DW_AT_HP_block_index)
20175 return "DW_AT_HP_block_index";
c764a876 20176#endif
f39c6ffd
TT
20177
20178 name = get_DW_AT_name (attr);
20179
20180 if (name == NULL)
20181 return "DW_AT_<unknown>";
20182
20183 return name;
c906108c
SS
20184}
20185
20186/* Convert a DWARF value form code into its string name. */
20187
f39c6ffd 20188static const char *
aa1ee363 20189dwarf_form_name (unsigned form)
c906108c 20190{
f39c6ffd
TT
20191 const char *name = get_DW_FORM_name (form);
20192
20193 if (name == NULL)
20194 return "DW_FORM_<unknown>";
20195
20196 return name;
c906108c
SS
20197}
20198
20199static char *
fba45db2 20200dwarf_bool_name (unsigned mybool)
c906108c
SS
20201{
20202 if (mybool)
20203 return "TRUE";
20204 else
20205 return "FALSE";
20206}
20207
20208/* Convert a DWARF type code into its string name. */
20209
f39c6ffd 20210static const char *
aa1ee363 20211dwarf_type_encoding_name (unsigned enc)
c906108c 20212{
f39c6ffd 20213 const char *name = get_DW_ATE_name (enc);
c906108c 20214
f39c6ffd
TT
20215 if (name == NULL)
20216 return "DW_ATE_<unknown>";
c906108c 20217
f39c6ffd 20218 return name;
c906108c 20219}
c906108c 20220
f9aca02d 20221static void
d97bc12b 20222dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20223{
20224 unsigned int i;
20225
d97bc12b
DE
20226 print_spaces (indent, f);
20227 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 20228 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
20229
20230 if (die->parent != NULL)
20231 {
20232 print_spaces (indent, f);
20233 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 20234 die->parent->offset.sect_off);
d97bc12b
DE
20235 }
20236
20237 print_spaces (indent, f);
20238 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20239 dwarf_bool_name (die->child != NULL));
c906108c 20240
d97bc12b
DE
20241 print_spaces (indent, f);
20242 fprintf_unfiltered (f, " attributes:\n");
20243
c906108c
SS
20244 for (i = 0; i < die->num_attrs; ++i)
20245 {
d97bc12b
DE
20246 print_spaces (indent, f);
20247 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20248 dwarf_attr_name (die->attrs[i].name),
20249 dwarf_form_name (die->attrs[i].form));
d97bc12b 20250
c906108c
SS
20251 switch (die->attrs[i].form)
20252 {
c906108c 20253 case DW_FORM_addr:
3019eac3 20254 case DW_FORM_GNU_addr_index:
d97bc12b 20255 fprintf_unfiltered (f, "address: ");
5af949e3 20256 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20257 break;
20258 case DW_FORM_block2:
20259 case DW_FORM_block4:
20260 case DW_FORM_block:
20261 case DW_FORM_block1:
56eb65bd
SP
20262 fprintf_unfiltered (f, "block: size %s",
20263 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20264 break;
2dc7f7b3 20265 case DW_FORM_exprloc:
56eb65bd
SP
20266 fprintf_unfiltered (f, "expression: size %s",
20267 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20268 break;
0224619f
JK
20269 case DW_FORM_data16:
20270 fprintf_unfiltered (f, "constant of 16 bytes");
20271 break;
4568ecf9
DE
20272 case DW_FORM_ref_addr:
20273 fprintf_unfiltered (f, "ref address: ");
20274 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20275 break;
36586728
TT
20276 case DW_FORM_GNU_ref_alt:
20277 fprintf_unfiltered (f, "alt ref address: ");
20278 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20279 break;
10b3939b
DJ
20280 case DW_FORM_ref1:
20281 case DW_FORM_ref2:
20282 case DW_FORM_ref4:
4568ecf9
DE
20283 case DW_FORM_ref8:
20284 case DW_FORM_ref_udata:
d97bc12b 20285 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20286 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20287 break;
c906108c
SS
20288 case DW_FORM_data1:
20289 case DW_FORM_data2:
20290 case DW_FORM_data4:
ce5d95e1 20291 case DW_FORM_data8:
c906108c
SS
20292 case DW_FORM_udata:
20293 case DW_FORM_sdata:
43bbcdc2
PH
20294 fprintf_unfiltered (f, "constant: %s",
20295 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20296 break;
2dc7f7b3
TT
20297 case DW_FORM_sec_offset:
20298 fprintf_unfiltered (f, "section offset: %s",
20299 pulongest (DW_UNSND (&die->attrs[i])));
20300 break;
55f1336d 20301 case DW_FORM_ref_sig8:
ac9ec31b
DE
20302 fprintf_unfiltered (f, "signature: %s",
20303 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20304 break;
c906108c 20305 case DW_FORM_string:
4bdf3d34 20306 case DW_FORM_strp:
43988095 20307 case DW_FORM_line_strp:
3019eac3 20308 case DW_FORM_GNU_str_index:
36586728 20309 case DW_FORM_GNU_strp_alt:
8285870a 20310 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20311 DW_STRING (&die->attrs[i])
8285870a
JK
20312 ? DW_STRING (&die->attrs[i]) : "",
20313 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20314 break;
20315 case DW_FORM_flag:
20316 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20317 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20318 else
d97bc12b 20319 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20320 break;
2dc7f7b3
TT
20321 case DW_FORM_flag_present:
20322 fprintf_unfiltered (f, "flag: TRUE");
20323 break;
a8329558 20324 case DW_FORM_indirect:
0963b4bd
MS
20325 /* The reader will have reduced the indirect form to
20326 the "base form" so this form should not occur. */
3e43a32a
MS
20327 fprintf_unfiltered (f,
20328 "unexpected attribute form: DW_FORM_indirect");
a8329558 20329 break;
c906108c 20330 default:
d97bc12b 20331 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20332 die->attrs[i].form);
d97bc12b 20333 break;
c906108c 20334 }
d97bc12b 20335 fprintf_unfiltered (f, "\n");
c906108c
SS
20336 }
20337}
20338
f9aca02d 20339static void
d97bc12b 20340dump_die_for_error (struct die_info *die)
c906108c 20341{
d97bc12b
DE
20342 dump_die_shallow (gdb_stderr, 0, die);
20343}
20344
20345static void
20346dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20347{
20348 int indent = level * 4;
20349
20350 gdb_assert (die != NULL);
20351
20352 if (level >= max_level)
20353 return;
20354
20355 dump_die_shallow (f, indent, die);
20356
20357 if (die->child != NULL)
c906108c 20358 {
d97bc12b
DE
20359 print_spaces (indent, f);
20360 fprintf_unfiltered (f, " Children:");
20361 if (level + 1 < max_level)
20362 {
20363 fprintf_unfiltered (f, "\n");
20364 dump_die_1 (f, level + 1, max_level, die->child);
20365 }
20366 else
20367 {
3e43a32a
MS
20368 fprintf_unfiltered (f,
20369 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20370 }
20371 }
20372
20373 if (die->sibling != NULL && level > 0)
20374 {
20375 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20376 }
20377}
20378
d97bc12b
DE
20379/* This is called from the pdie macro in gdbinit.in.
20380 It's not static so gcc will keep a copy callable from gdb. */
20381
20382void
20383dump_die (struct die_info *die, int max_level)
20384{
20385 dump_die_1 (gdb_stdlog, 0, max_level, die);
20386}
20387
f9aca02d 20388static void
51545339 20389store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20390{
51545339 20391 void **slot;
c906108c 20392
b64f50a1
JK
20393 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20394 INSERT);
51545339
DJ
20395
20396 *slot = die;
c906108c
SS
20397}
20398
b64f50a1
JK
20399/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20400 required kind. */
20401
20402static sect_offset
ff39bb5e 20403dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20404{
4568ecf9 20405 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 20406
7771576e 20407 if (attr_form_is_ref (attr))
b64f50a1 20408 return retval;
93311388 20409
b64f50a1 20410 retval.sect_off = 0;
93311388
DE
20411 complaint (&symfile_complaints,
20412 _("unsupported die ref attribute form: '%s'"),
20413 dwarf_form_name (attr->form));
b64f50a1 20414 return retval;
c906108c
SS
20415}
20416
43bbcdc2
PH
20417/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20418 * the value held by the attribute is not constant. */
a02abb62 20419
43bbcdc2 20420static LONGEST
ff39bb5e 20421dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20422{
20423 if (attr->form == DW_FORM_sdata)
20424 return DW_SND (attr);
20425 else if (attr->form == DW_FORM_udata
20426 || attr->form == DW_FORM_data1
20427 || attr->form == DW_FORM_data2
20428 || attr->form == DW_FORM_data4
20429 || attr->form == DW_FORM_data8)
20430 return DW_UNSND (attr);
20431 else
20432 {
0224619f 20433 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20434 complaint (&symfile_complaints,
20435 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20436 dwarf_form_name (attr->form));
20437 return default_value;
20438 }
20439}
20440
348e048f
DE
20441/* Follow reference or signature attribute ATTR of SRC_DIE.
20442 On entry *REF_CU is the CU of SRC_DIE.
20443 On exit *REF_CU is the CU of the result. */
20444
20445static struct die_info *
ff39bb5e 20446follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20447 struct dwarf2_cu **ref_cu)
20448{
20449 struct die_info *die;
20450
7771576e 20451 if (attr_form_is_ref (attr))
348e048f 20452 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20453 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20454 die = follow_die_sig (src_die, attr, ref_cu);
20455 else
20456 {
20457 dump_die_for_error (src_die);
20458 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20459 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20460 }
20461
20462 return die;
03dd20cc
DJ
20463}
20464
5c631832 20465/* Follow reference OFFSET.
673bfd45
DE
20466 On entry *REF_CU is the CU of the source die referencing OFFSET.
20467 On exit *REF_CU is the CU of the result.
20468 Returns NULL if OFFSET is invalid. */
f504f079 20469
f9aca02d 20470static struct die_info *
36586728
TT
20471follow_die_offset (sect_offset offset, int offset_in_dwz,
20472 struct dwarf2_cu **ref_cu)
c906108c 20473{
10b3939b 20474 struct die_info temp_die;
f2f0e013 20475 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20476
348e048f
DE
20477 gdb_assert (cu->per_cu != NULL);
20478
98bfdba5
PA
20479 target_cu = cu;
20480
3019eac3 20481 if (cu->per_cu->is_debug_types)
348e048f
DE
20482 {
20483 /* .debug_types CUs cannot reference anything outside their CU.
20484 If they need to, they have to reference a signatured type via
55f1336d 20485 DW_FORM_ref_sig8. */
348e048f 20486 if (! offset_in_cu_p (&cu->header, offset))
5c631832 20487 return NULL;
348e048f 20488 }
36586728
TT
20489 else if (offset_in_dwz != cu->per_cu->is_dwz
20490 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
20491 {
20492 struct dwarf2_per_cu_data *per_cu;
9a619af0 20493
36586728
TT
20494 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20495 cu->objfile);
03dd20cc
DJ
20496
20497 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20498 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20499 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20500
10b3939b
DJ
20501 target_cu = per_cu->cu;
20502 }
98bfdba5
PA
20503 else if (cu->dies == NULL)
20504 {
20505 /* We're loading full DIEs during partial symbol reading. */
20506 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20507 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20508 }
c906108c 20509
f2f0e013 20510 *ref_cu = target_cu;
51545339 20511 temp_die.offset = offset;
9a3c8263
SM
20512 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20513 &temp_die, offset.sect_off);
5c631832 20514}
10b3939b 20515
5c631832
JK
20516/* Follow reference attribute ATTR of SRC_DIE.
20517 On entry *REF_CU is the CU of SRC_DIE.
20518 On exit *REF_CU is the CU of the result. */
20519
20520static struct die_info *
ff39bb5e 20521follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20522 struct dwarf2_cu **ref_cu)
20523{
b64f50a1 20524 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20525 struct dwarf2_cu *cu = *ref_cu;
20526 struct die_info *die;
20527
36586728
TT
20528 die = follow_die_offset (offset,
20529 (attr->form == DW_FORM_GNU_ref_alt
20530 || cu->per_cu->is_dwz),
20531 ref_cu);
5c631832
JK
20532 if (!die)
20533 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20534 "at 0x%x [in module %s]"),
4262abfb
JK
20535 offset.sect_off, src_die->offset.sect_off,
20536 objfile_name (cu->objfile));
348e048f 20537
5c631832
JK
20538 return die;
20539}
20540
d83e736b
JK
20541/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20542 Returned value is intended for DW_OP_call*. Returned
20543 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20544
20545struct dwarf2_locexpr_baton
8b9737bf
TT
20546dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20547 struct dwarf2_per_cu_data *per_cu,
20548 CORE_ADDR (*get_frame_pc) (void *baton),
20549 void *baton)
5c631832 20550{
918dd910 20551 struct dwarf2_cu *cu;
5c631832
JK
20552 struct die_info *die;
20553 struct attribute *attr;
20554 struct dwarf2_locexpr_baton retval;
20555
8cf6f0b1
TT
20556 dw2_setup (per_cu->objfile);
20557
918dd910
JK
20558 if (per_cu->cu == NULL)
20559 load_cu (per_cu);
20560 cu = per_cu->cu;
cc12ce38
DE
20561 if (cu == NULL)
20562 {
20563 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20564 Instead just throw an error, not much else we can do. */
20565 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20566 offset.sect_off, objfile_name (per_cu->objfile));
20567 }
918dd910 20568
36586728 20569 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
20570 if (!die)
20571 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20572 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20573
20574 attr = dwarf2_attr (die, DW_AT_location, cu);
20575 if (!attr)
20576 {
e103e986
JK
20577 /* DWARF: "If there is no such attribute, then there is no effect.".
20578 DATA is ignored if SIZE is 0. */
5c631832 20579
e103e986 20580 retval.data = NULL;
5c631832
JK
20581 retval.size = 0;
20582 }
8cf6f0b1
TT
20583 else if (attr_form_is_section_offset (attr))
20584 {
20585 struct dwarf2_loclist_baton loclist_baton;
20586 CORE_ADDR pc = (*get_frame_pc) (baton);
20587 size_t size;
20588
20589 fill_in_loclist_baton (cu, &loclist_baton, attr);
20590
20591 retval.data = dwarf2_find_location_expression (&loclist_baton,
20592 &size, pc);
20593 retval.size = size;
20594 }
5c631832
JK
20595 else
20596 {
20597 if (!attr_form_is_block (attr))
20598 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20599 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 20600 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20601
20602 retval.data = DW_BLOCK (attr)->data;
20603 retval.size = DW_BLOCK (attr)->size;
20604 }
20605 retval.per_cu = cu->per_cu;
918dd910 20606
918dd910
JK
20607 age_cached_comp_units ();
20608
5c631832 20609 return retval;
348e048f
DE
20610}
20611
8b9737bf
TT
20612/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20613 offset. */
20614
20615struct dwarf2_locexpr_baton
20616dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20617 struct dwarf2_per_cu_data *per_cu,
20618 CORE_ADDR (*get_frame_pc) (void *baton),
20619 void *baton)
20620{
20621 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20622
20623 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20624}
20625
b6807d98
TT
20626/* Write a constant of a given type as target-ordered bytes into
20627 OBSTACK. */
20628
20629static const gdb_byte *
20630write_constant_as_bytes (struct obstack *obstack,
20631 enum bfd_endian byte_order,
20632 struct type *type,
20633 ULONGEST value,
20634 LONGEST *len)
20635{
20636 gdb_byte *result;
20637
20638 *len = TYPE_LENGTH (type);
224c3ddb 20639 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20640 store_unsigned_integer (result, *len, byte_order, value);
20641
20642 return result;
20643}
20644
20645/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20646 pointer to the constant bytes and set LEN to the length of the
20647 data. If memory is needed, allocate it on OBSTACK. If the DIE
20648 does not have a DW_AT_const_value, return NULL. */
20649
20650const gdb_byte *
20651dwarf2_fetch_constant_bytes (sect_offset offset,
20652 struct dwarf2_per_cu_data *per_cu,
20653 struct obstack *obstack,
20654 LONGEST *len)
20655{
20656 struct dwarf2_cu *cu;
20657 struct die_info *die;
20658 struct attribute *attr;
20659 const gdb_byte *result = NULL;
20660 struct type *type;
20661 LONGEST value;
20662 enum bfd_endian byte_order;
20663
20664 dw2_setup (per_cu->objfile);
20665
20666 if (per_cu->cu == NULL)
20667 load_cu (per_cu);
20668 cu = per_cu->cu;
cc12ce38
DE
20669 if (cu == NULL)
20670 {
20671 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20672 Instead just throw an error, not much else we can do. */
20673 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20674 offset.sect_off, objfile_name (per_cu->objfile));
20675 }
b6807d98
TT
20676
20677 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20678 if (!die)
20679 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20680 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
20681
20682
20683 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20684 if (attr == NULL)
20685 return NULL;
20686
20687 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20688 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20689
20690 switch (attr->form)
20691 {
20692 case DW_FORM_addr:
20693 case DW_FORM_GNU_addr_index:
20694 {
20695 gdb_byte *tem;
20696
20697 *len = cu->header.addr_size;
224c3ddb 20698 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20699 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20700 result = tem;
20701 }
20702 break;
20703 case DW_FORM_string:
20704 case DW_FORM_strp:
20705 case DW_FORM_GNU_str_index:
20706 case DW_FORM_GNU_strp_alt:
20707 /* DW_STRING is already allocated on the objfile obstack, point
20708 directly to it. */
20709 result = (const gdb_byte *) DW_STRING (attr);
20710 *len = strlen (DW_STRING (attr));
20711 break;
20712 case DW_FORM_block1:
20713 case DW_FORM_block2:
20714 case DW_FORM_block4:
20715 case DW_FORM_block:
20716 case DW_FORM_exprloc:
0224619f 20717 case DW_FORM_data16:
b6807d98
TT
20718 result = DW_BLOCK (attr)->data;
20719 *len = DW_BLOCK (attr)->size;
20720 break;
20721
20722 /* The DW_AT_const_value attributes are supposed to carry the
20723 symbol's value "represented as it would be on the target
20724 architecture." By the time we get here, it's already been
20725 converted to host endianness, so we just need to sign- or
20726 zero-extend it as appropriate. */
20727 case DW_FORM_data1:
20728 type = die_type (die, cu);
20729 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20730 if (result == NULL)
20731 result = write_constant_as_bytes (obstack, byte_order,
20732 type, value, len);
20733 break;
20734 case DW_FORM_data2:
20735 type = die_type (die, cu);
20736 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20737 if (result == NULL)
20738 result = write_constant_as_bytes (obstack, byte_order,
20739 type, value, len);
20740 break;
20741 case DW_FORM_data4:
20742 type = die_type (die, cu);
20743 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20744 if (result == NULL)
20745 result = write_constant_as_bytes (obstack, byte_order,
20746 type, value, len);
20747 break;
20748 case DW_FORM_data8:
20749 type = die_type (die, cu);
20750 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20751 if (result == NULL)
20752 result = write_constant_as_bytes (obstack, byte_order,
20753 type, value, len);
20754 break;
20755
20756 case DW_FORM_sdata:
20757 type = die_type (die, cu);
20758 result = write_constant_as_bytes (obstack, byte_order,
20759 type, DW_SND (attr), len);
20760 break;
20761
20762 case DW_FORM_udata:
20763 type = die_type (die, cu);
20764 result = write_constant_as_bytes (obstack, byte_order,
20765 type, DW_UNSND (attr), len);
20766 break;
20767
20768 default:
20769 complaint (&symfile_complaints,
20770 _("unsupported const value attribute form: '%s'"),
20771 dwarf_form_name (attr->form));
20772 break;
20773 }
20774
20775 return result;
20776}
20777
7942e96e
AA
20778/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20779 valid type for this die is found. */
20780
20781struct type *
20782dwarf2_fetch_die_type_sect_off (sect_offset offset,
20783 struct dwarf2_per_cu_data *per_cu)
20784{
20785 struct dwarf2_cu *cu;
20786 struct die_info *die;
20787
20788 dw2_setup (per_cu->objfile);
20789
20790 if (per_cu->cu == NULL)
20791 load_cu (per_cu);
20792 cu = per_cu->cu;
20793 if (!cu)
20794 return NULL;
20795
20796 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20797 if (!die)
20798 return NULL;
20799
20800 return die_type (die, cu);
20801}
20802
8a9b8146
TT
20803/* Return the type of the DIE at DIE_OFFSET in the CU named by
20804 PER_CU. */
20805
20806struct type *
b64f50a1 20807dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20808 struct dwarf2_per_cu_data *per_cu)
20809{
b64f50a1
JK
20810 sect_offset die_offset_sect;
20811
8a9b8146 20812 dw2_setup (per_cu->objfile);
b64f50a1
JK
20813
20814 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20815 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20816}
20817
ac9ec31b 20818/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20819 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20820 On exit *REF_CU is the CU of the result.
20821 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20822
20823static struct die_info *
ac9ec31b
DE
20824follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20825 struct dwarf2_cu **ref_cu)
348e048f 20826{
348e048f 20827 struct die_info temp_die;
348e048f
DE
20828 struct dwarf2_cu *sig_cu;
20829 struct die_info *die;
20830
ac9ec31b
DE
20831 /* While it might be nice to assert sig_type->type == NULL here,
20832 we can get here for DW_AT_imported_declaration where we need
20833 the DIE not the type. */
348e048f
DE
20834
20835 /* If necessary, add it to the queue and load its DIEs. */
20836
95554aad 20837 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20838 read_signatured_type (sig_type);
348e048f 20839
348e048f 20840 sig_cu = sig_type->per_cu.cu;
69d751e3 20841 gdb_assert (sig_cu != NULL);
3019eac3
DE
20842 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20843 temp_die.offset = sig_type->type_offset_in_section;
9a3c8263
SM
20844 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20845 temp_die.offset.sect_off);
348e048f
DE
20846 if (die)
20847 {
796a7ff8
DE
20848 /* For .gdb_index version 7 keep track of included TUs.
20849 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20850 if (dwarf2_per_objfile->index_table != NULL
20851 && dwarf2_per_objfile->index_table->version <= 7)
20852 {
20853 VEC_safe_push (dwarf2_per_cu_ptr,
20854 (*ref_cu)->per_cu->imported_symtabs,
20855 sig_cu->per_cu);
20856 }
20857
348e048f
DE
20858 *ref_cu = sig_cu;
20859 return die;
20860 }
20861
ac9ec31b
DE
20862 return NULL;
20863}
20864
20865/* Follow signatured type referenced by ATTR in SRC_DIE.
20866 On entry *REF_CU is the CU of SRC_DIE.
20867 On exit *REF_CU is the CU of the result.
20868 The result is the DIE of the type.
20869 If the referenced type cannot be found an error is thrown. */
20870
20871static struct die_info *
ff39bb5e 20872follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20873 struct dwarf2_cu **ref_cu)
20874{
20875 ULONGEST signature = DW_SIGNATURE (attr);
20876 struct signatured_type *sig_type;
20877 struct die_info *die;
20878
20879 gdb_assert (attr->form == DW_FORM_ref_sig8);
20880
a2ce51a0 20881 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20882 /* sig_type will be NULL if the signatured type is missing from
20883 the debug info. */
20884 if (sig_type == NULL)
20885 {
20886 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20887 " from DIE at 0x%x [in module %s]"),
20888 hex_string (signature), src_die->offset.sect_off,
4262abfb 20889 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20890 }
20891
20892 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20893 if (die == NULL)
20894 {
20895 dump_die_for_error (src_die);
20896 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20897 " from DIE at 0x%x [in module %s]"),
20898 hex_string (signature), src_die->offset.sect_off,
4262abfb 20899 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20900 }
20901
20902 return die;
20903}
20904
20905/* Get the type specified by SIGNATURE referenced in DIE/CU,
20906 reading in and processing the type unit if necessary. */
20907
20908static struct type *
20909get_signatured_type (struct die_info *die, ULONGEST signature,
20910 struct dwarf2_cu *cu)
20911{
20912 struct signatured_type *sig_type;
20913 struct dwarf2_cu *type_cu;
20914 struct die_info *type_die;
20915 struct type *type;
20916
a2ce51a0 20917 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20918 /* sig_type will be NULL if the signatured type is missing from
20919 the debug info. */
20920 if (sig_type == NULL)
20921 {
20922 complaint (&symfile_complaints,
20923 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20924 " from DIE at 0x%x [in module %s]"),
20925 hex_string (signature), die->offset.sect_off,
4262abfb 20926 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20927 return build_error_marker_type (cu, die);
20928 }
20929
20930 /* If we already know the type we're done. */
20931 if (sig_type->type != NULL)
20932 return sig_type->type;
20933
20934 type_cu = cu;
20935 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20936 if (type_die != NULL)
20937 {
20938 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20939 is created. This is important, for example, because for c++ classes
20940 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20941 type = read_type_die (type_die, type_cu);
20942 if (type == NULL)
20943 {
20944 complaint (&symfile_complaints,
20945 _("Dwarf Error: Cannot build signatured type %s"
20946 " referenced from DIE at 0x%x [in module %s]"),
20947 hex_string (signature), die->offset.sect_off,
4262abfb 20948 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20949 type = build_error_marker_type (cu, die);
20950 }
20951 }
20952 else
20953 {
20954 complaint (&symfile_complaints,
20955 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20956 " from DIE at 0x%x [in module %s]"),
20957 hex_string (signature), die->offset.sect_off,
4262abfb 20958 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20959 type = build_error_marker_type (cu, die);
20960 }
20961 sig_type->type = type;
20962
20963 return type;
20964}
20965
20966/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20967 reading in and processing the type unit if necessary. */
20968
20969static struct type *
ff39bb5e 20970get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 20971 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
20972{
20973 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 20974 if (attr_form_is_ref (attr))
ac9ec31b
DE
20975 {
20976 struct dwarf2_cu *type_cu = cu;
20977 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20978
20979 return read_type_die (type_die, type_cu);
20980 }
20981 else if (attr->form == DW_FORM_ref_sig8)
20982 {
20983 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20984 }
20985 else
20986 {
20987 complaint (&symfile_complaints,
20988 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20989 " at 0x%x [in module %s]"),
20990 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 20991 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20992 return build_error_marker_type (cu, die);
20993 }
348e048f
DE
20994}
20995
e5fe5e75 20996/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
20997
20998static void
e5fe5e75 20999load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21000{
52dc124a 21001 struct signatured_type *sig_type;
348e048f 21002
f4dc4d17
DE
21003 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21004 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21005
6721b2ec
DE
21006 /* We have the per_cu, but we need the signatured_type.
21007 Fortunately this is an easy translation. */
21008 gdb_assert (per_cu->is_debug_types);
21009 sig_type = (struct signatured_type *) per_cu;
348e048f 21010
6721b2ec 21011 gdb_assert (per_cu->cu == NULL);
348e048f 21012
52dc124a 21013 read_signatured_type (sig_type);
348e048f 21014
6721b2ec 21015 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21016}
21017
dee91e82
DE
21018/* die_reader_func for read_signatured_type.
21019 This is identical to load_full_comp_unit_reader,
21020 but is kept separate for now. */
348e048f
DE
21021
21022static void
dee91e82 21023read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21024 const gdb_byte *info_ptr,
dee91e82
DE
21025 struct die_info *comp_unit_die,
21026 int has_children,
21027 void *data)
348e048f 21028{
dee91e82 21029 struct dwarf2_cu *cu = reader->cu;
348e048f 21030
dee91e82
DE
21031 gdb_assert (cu->die_hash == NULL);
21032 cu->die_hash =
21033 htab_create_alloc_ex (cu->header.length / 12,
21034 die_hash,
21035 die_eq,
21036 NULL,
21037 &cu->comp_unit_obstack,
21038 hashtab_obstack_allocate,
21039 dummy_obstack_deallocate);
348e048f 21040
dee91e82
DE
21041 if (has_children)
21042 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21043 &info_ptr, comp_unit_die);
21044 cu->dies = comp_unit_die;
21045 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21046
21047 /* We try not to read any attributes in this function, because not
9cdd5dbd 21048 all CUs needed for references have been loaded yet, and symbol
348e048f 21049 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21050 or we won't be able to build types correctly.
21051 Similarly, if we do not read the producer, we can not apply
21052 producer-specific interpretation. */
95554aad 21053 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21054}
348e048f 21055
3019eac3
DE
21056/* Read in a signatured type and build its CU and DIEs.
21057 If the type is a stub for the real type in a DWO file,
21058 read in the real type from the DWO file as well. */
dee91e82
DE
21059
21060static void
21061read_signatured_type (struct signatured_type *sig_type)
21062{
21063 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21064
3019eac3 21065 gdb_assert (per_cu->is_debug_types);
dee91e82 21066 gdb_assert (per_cu->cu == NULL);
348e048f 21067
f4dc4d17
DE
21068 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21069 read_signatured_type_reader, NULL);
7ee85ab1 21070 sig_type->per_cu.tu_read = 1;
c906108c
SS
21071}
21072
c906108c
SS
21073/* Decode simple location descriptions.
21074 Given a pointer to a dwarf block that defines a location, compute
21075 the location and return the value.
21076
4cecd739
DJ
21077 NOTE drow/2003-11-18: This function is called in two situations
21078 now: for the address of static or global variables (partial symbols
21079 only) and for offsets into structures which are expected to be
21080 (more or less) constant. The partial symbol case should go away,
21081 and only the constant case should remain. That will let this
21082 function complain more accurately. A few special modes are allowed
21083 without complaint for global variables (for instance, global
21084 register values and thread-local values).
c906108c
SS
21085
21086 A location description containing no operations indicates that the
4cecd739 21087 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21088 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21089 callers will only want a very basic result and this can become a
21ae7a4d
JK
21090 complaint.
21091
21092 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21093
21094static CORE_ADDR
e7c27a73 21095decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21096{
e7c27a73 21097 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21098 size_t i;
21099 size_t size = blk->size;
d521ce57 21100 const gdb_byte *data = blk->data;
21ae7a4d
JK
21101 CORE_ADDR stack[64];
21102 int stacki;
21103 unsigned int bytes_read, unsnd;
21104 gdb_byte op;
c906108c 21105
21ae7a4d
JK
21106 i = 0;
21107 stacki = 0;
21108 stack[stacki] = 0;
21109 stack[++stacki] = 0;
21110
21111 while (i < size)
21112 {
21113 op = data[i++];
21114 switch (op)
21115 {
21116 case DW_OP_lit0:
21117 case DW_OP_lit1:
21118 case DW_OP_lit2:
21119 case DW_OP_lit3:
21120 case DW_OP_lit4:
21121 case DW_OP_lit5:
21122 case DW_OP_lit6:
21123 case DW_OP_lit7:
21124 case DW_OP_lit8:
21125 case DW_OP_lit9:
21126 case DW_OP_lit10:
21127 case DW_OP_lit11:
21128 case DW_OP_lit12:
21129 case DW_OP_lit13:
21130 case DW_OP_lit14:
21131 case DW_OP_lit15:
21132 case DW_OP_lit16:
21133 case DW_OP_lit17:
21134 case DW_OP_lit18:
21135 case DW_OP_lit19:
21136 case DW_OP_lit20:
21137 case DW_OP_lit21:
21138 case DW_OP_lit22:
21139 case DW_OP_lit23:
21140 case DW_OP_lit24:
21141 case DW_OP_lit25:
21142 case DW_OP_lit26:
21143 case DW_OP_lit27:
21144 case DW_OP_lit28:
21145 case DW_OP_lit29:
21146 case DW_OP_lit30:
21147 case DW_OP_lit31:
21148 stack[++stacki] = op - DW_OP_lit0;
21149 break;
f1bea926 21150
21ae7a4d
JK
21151 case DW_OP_reg0:
21152 case DW_OP_reg1:
21153 case DW_OP_reg2:
21154 case DW_OP_reg3:
21155 case DW_OP_reg4:
21156 case DW_OP_reg5:
21157 case DW_OP_reg6:
21158 case DW_OP_reg7:
21159 case DW_OP_reg8:
21160 case DW_OP_reg9:
21161 case DW_OP_reg10:
21162 case DW_OP_reg11:
21163 case DW_OP_reg12:
21164 case DW_OP_reg13:
21165 case DW_OP_reg14:
21166 case DW_OP_reg15:
21167 case DW_OP_reg16:
21168 case DW_OP_reg17:
21169 case DW_OP_reg18:
21170 case DW_OP_reg19:
21171 case DW_OP_reg20:
21172 case DW_OP_reg21:
21173 case DW_OP_reg22:
21174 case DW_OP_reg23:
21175 case DW_OP_reg24:
21176 case DW_OP_reg25:
21177 case DW_OP_reg26:
21178 case DW_OP_reg27:
21179 case DW_OP_reg28:
21180 case DW_OP_reg29:
21181 case DW_OP_reg30:
21182 case DW_OP_reg31:
21183 stack[++stacki] = op - DW_OP_reg0;
21184 if (i < size)
21185 dwarf2_complex_location_expr_complaint ();
21186 break;
c906108c 21187
21ae7a4d
JK
21188 case DW_OP_regx:
21189 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21190 i += bytes_read;
21191 stack[++stacki] = unsnd;
21192 if (i < size)
21193 dwarf2_complex_location_expr_complaint ();
21194 break;
c906108c 21195
21ae7a4d
JK
21196 case DW_OP_addr:
21197 stack[++stacki] = read_address (objfile->obfd, &data[i],
21198 cu, &bytes_read);
21199 i += bytes_read;
21200 break;
d53d4ac5 21201
21ae7a4d
JK
21202 case DW_OP_const1u:
21203 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21204 i += 1;
21205 break;
21206
21207 case DW_OP_const1s:
21208 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21209 i += 1;
21210 break;
21211
21212 case DW_OP_const2u:
21213 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21214 i += 2;
21215 break;
21216
21217 case DW_OP_const2s:
21218 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21219 i += 2;
21220 break;
d53d4ac5 21221
21ae7a4d
JK
21222 case DW_OP_const4u:
21223 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21224 i += 4;
21225 break;
21226
21227 case DW_OP_const4s:
21228 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21229 i += 4;
21230 break;
21231
585861ea
JK
21232 case DW_OP_const8u:
21233 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21234 i += 8;
21235 break;
21236
21ae7a4d
JK
21237 case DW_OP_constu:
21238 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21239 &bytes_read);
21240 i += bytes_read;
21241 break;
21242
21243 case DW_OP_consts:
21244 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21245 i += bytes_read;
21246 break;
21247
21248 case DW_OP_dup:
21249 stack[stacki + 1] = stack[stacki];
21250 stacki++;
21251 break;
21252
21253 case DW_OP_plus:
21254 stack[stacki - 1] += stack[stacki];
21255 stacki--;
21256 break;
21257
21258 case DW_OP_plus_uconst:
21259 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21260 &bytes_read);
21261 i += bytes_read;
21262 break;
21263
21264 case DW_OP_minus:
21265 stack[stacki - 1] -= stack[stacki];
21266 stacki--;
21267 break;
21268
21269 case DW_OP_deref:
21270 /* If we're not the last op, then we definitely can't encode
21271 this using GDB's address_class enum. This is valid for partial
21272 global symbols, although the variable's address will be bogus
21273 in the psymtab. */
21274 if (i < size)
21275 dwarf2_complex_location_expr_complaint ();
21276 break;
21277
21278 case DW_OP_GNU_push_tls_address:
4aa4e28b 21279 case DW_OP_form_tls_address:
21ae7a4d
JK
21280 /* The top of the stack has the offset from the beginning
21281 of the thread control block at which the variable is located. */
21282 /* Nothing should follow this operator, so the top of stack would
21283 be returned. */
21284 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21285 address will be bogus in the psymtab. Make it always at least
21286 non-zero to not look as a variable garbage collected by linker
21287 which have DW_OP_addr 0. */
21ae7a4d
JK
21288 if (i < size)
21289 dwarf2_complex_location_expr_complaint ();
585861ea 21290 stack[stacki]++;
21ae7a4d
JK
21291 break;
21292
21293 case DW_OP_GNU_uninit:
21294 break;
21295
3019eac3 21296 case DW_OP_GNU_addr_index:
49f6c839 21297 case DW_OP_GNU_const_index:
3019eac3
DE
21298 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21299 &bytes_read);
21300 i += bytes_read;
21301 break;
21302
21ae7a4d
JK
21303 default:
21304 {
f39c6ffd 21305 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21306
21307 if (name)
21308 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21309 name);
21310 else
21311 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21312 op);
21313 }
21314
21315 return (stack[stacki]);
d53d4ac5 21316 }
3c6e0cb3 21317
21ae7a4d
JK
21318 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21319 outside of the allocated space. Also enforce minimum>0. */
21320 if (stacki >= ARRAY_SIZE (stack) - 1)
21321 {
21322 complaint (&symfile_complaints,
21323 _("location description stack overflow"));
21324 return 0;
21325 }
21326
21327 if (stacki <= 0)
21328 {
21329 complaint (&symfile_complaints,
21330 _("location description stack underflow"));
21331 return 0;
21332 }
21333 }
21334 return (stack[stacki]);
c906108c
SS
21335}
21336
21337/* memory allocation interface */
21338
c906108c 21339static struct dwarf_block *
7b5a2f43 21340dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21341{
8d749320 21342 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21343}
21344
c906108c 21345static struct die_info *
b60c80d6 21346dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21347{
21348 struct die_info *die;
b60c80d6
DJ
21349 size_t size = sizeof (struct die_info);
21350
21351 if (num_attrs > 1)
21352 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21353
b60c80d6 21354 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21355 memset (die, 0, sizeof (struct die_info));
21356 return (die);
21357}
2e276125
JB
21358
21359\f
21360/* Macro support. */
21361
233d95b5
JK
21362/* Return file name relative to the compilation directory of file number I in
21363 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21364 responsible for freeing it. */
233d95b5 21365
2e276125 21366static char *
233d95b5 21367file_file_name (int file, struct line_header *lh)
2e276125 21368{
6a83a1e6
EZ
21369 /* Is the file number a valid index into the line header's file name
21370 table? Remember that file numbers start with one, not zero. */
21371 if (1 <= file && file <= lh->num_file_names)
21372 {
21373 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 21374
afa6c9ab
SL
21375 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
21376 || lh->include_dirs == NULL)
6a83a1e6 21377 return xstrdup (fe->name);
233d95b5 21378 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
b36cec19 21379 fe->name, (char *) NULL);
6a83a1e6 21380 }
2e276125
JB
21381 else
21382 {
6a83a1e6
EZ
21383 /* The compiler produced a bogus file number. We can at least
21384 record the macro definitions made in the file, even if we
21385 won't be able to find the file by name. */
21386 char fake_name[80];
9a619af0 21387
8c042590
PM
21388 xsnprintf (fake_name, sizeof (fake_name),
21389 "<bad macro file number %d>", file);
2e276125 21390
6e70227d 21391 complaint (&symfile_complaints,
6a83a1e6
EZ
21392 _("bad file number in macro information (%d)"),
21393 file);
2e276125 21394
6a83a1e6 21395 return xstrdup (fake_name);
2e276125
JB
21396 }
21397}
21398
233d95b5
JK
21399/* Return the full name of file number I in *LH's file name table.
21400 Use COMP_DIR as the name of the current directory of the
21401 compilation. The result is allocated using xmalloc; the caller is
21402 responsible for freeing it. */
21403static char *
21404file_full_name (int file, struct line_header *lh, const char *comp_dir)
21405{
21406 /* Is the file number a valid index into the line header's file name
21407 table? Remember that file numbers start with one, not zero. */
21408 if (1 <= file && file <= lh->num_file_names)
21409 {
21410 char *relative = file_file_name (file, lh);
21411
21412 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21413 return relative;
b36cec19
PA
21414 return reconcat (relative, comp_dir, SLASH_STRING,
21415 relative, (char *) NULL);
233d95b5
JK
21416 }
21417 else
21418 return file_file_name (file, lh);
21419}
21420
2e276125
JB
21421
21422static struct macro_source_file *
21423macro_start_file (int file, int line,
21424 struct macro_source_file *current_file,
43f3e411 21425 struct line_header *lh)
2e276125 21426{
233d95b5
JK
21427 /* File name relative to the compilation directory of this source file. */
21428 char *file_name = file_file_name (file, lh);
2e276125 21429
2e276125 21430 if (! current_file)
abc9d0dc 21431 {
fc474241
DE
21432 /* Note: We don't create a macro table for this compilation unit
21433 at all until we actually get a filename. */
43f3e411 21434 struct macro_table *macro_table = get_macro_table ();
fc474241 21435
abc9d0dc
TT
21436 /* If we have no current file, then this must be the start_file
21437 directive for the compilation unit's main source file. */
fc474241
DE
21438 current_file = macro_set_main (macro_table, file_name);
21439 macro_define_special (macro_table);
abc9d0dc 21440 }
2e276125 21441 else
233d95b5 21442 current_file = macro_include (current_file, line, file_name);
2e276125 21443
233d95b5 21444 xfree (file_name);
6e70227d 21445
2e276125
JB
21446 return current_file;
21447}
21448
21449
21450/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21451 followed by a null byte. */
21452static char *
21453copy_string (const char *buf, int len)
21454{
224c3ddb 21455 char *s = (char *) xmalloc (len + 1);
9a619af0 21456
2e276125
JB
21457 memcpy (s, buf, len);
21458 s[len] = '\0';
2e276125
JB
21459 return s;
21460}
21461
21462
21463static const char *
21464consume_improper_spaces (const char *p, const char *body)
21465{
21466 if (*p == ' ')
21467 {
4d3c2250 21468 complaint (&symfile_complaints,
3e43a32a
MS
21469 _("macro definition contains spaces "
21470 "in formal argument list:\n`%s'"),
4d3c2250 21471 body);
2e276125
JB
21472
21473 while (*p == ' ')
21474 p++;
21475 }
21476
21477 return p;
21478}
21479
21480
21481static void
21482parse_macro_definition (struct macro_source_file *file, int line,
21483 const char *body)
21484{
21485 const char *p;
21486
21487 /* The body string takes one of two forms. For object-like macro
21488 definitions, it should be:
21489
21490 <macro name> " " <definition>
21491
21492 For function-like macro definitions, it should be:
21493
21494 <macro name> "() " <definition>
21495 or
21496 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21497
21498 Spaces may appear only where explicitly indicated, and in the
21499 <definition>.
21500
21501 The Dwarf 2 spec says that an object-like macro's name is always
21502 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21503 the space when the macro's definition is the empty string.
2e276125
JB
21504
21505 The Dwarf 2 spec says that there should be no spaces between the
21506 formal arguments in a function-like macro's formal argument list,
21507 but versions of GCC around March 2002 include spaces after the
21508 commas. */
21509
21510
21511 /* Find the extent of the macro name. The macro name is terminated
21512 by either a space or null character (for an object-like macro) or
21513 an opening paren (for a function-like macro). */
21514 for (p = body; *p; p++)
21515 if (*p == ' ' || *p == '(')
21516 break;
21517
21518 if (*p == ' ' || *p == '\0')
21519 {
21520 /* It's an object-like macro. */
21521 int name_len = p - body;
21522 char *name = copy_string (body, name_len);
21523 const char *replacement;
21524
21525 if (*p == ' ')
21526 replacement = body + name_len + 1;
21527 else
21528 {
4d3c2250 21529 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21530 replacement = body + name_len;
21531 }
6e70227d 21532
2e276125
JB
21533 macro_define_object (file, line, name, replacement);
21534
21535 xfree (name);
21536 }
21537 else if (*p == '(')
21538 {
21539 /* It's a function-like macro. */
21540 char *name = copy_string (body, p - body);
21541 int argc = 0;
21542 int argv_size = 1;
8d749320 21543 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21544
21545 p++;
21546
21547 p = consume_improper_spaces (p, body);
21548
21549 /* Parse the formal argument list. */
21550 while (*p && *p != ')')
21551 {
21552 /* Find the extent of the current argument name. */
21553 const char *arg_start = p;
21554
21555 while (*p && *p != ',' && *p != ')' && *p != ' ')
21556 p++;
21557
21558 if (! *p || p == arg_start)
4d3c2250 21559 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21560 else
21561 {
21562 /* Make sure argv has room for the new argument. */
21563 if (argc >= argv_size)
21564 {
21565 argv_size *= 2;
224c3ddb 21566 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21567 }
21568
21569 argv[argc++] = copy_string (arg_start, p - arg_start);
21570 }
21571
21572 p = consume_improper_spaces (p, body);
21573
21574 /* Consume the comma, if present. */
21575 if (*p == ',')
21576 {
21577 p++;
21578
21579 p = consume_improper_spaces (p, body);
21580 }
21581 }
21582
21583 if (*p == ')')
21584 {
21585 p++;
21586
21587 if (*p == ' ')
21588 /* Perfectly formed definition, no complaints. */
21589 macro_define_function (file, line, name,
6e70227d 21590 argc, (const char **) argv,
2e276125
JB
21591 p + 1);
21592 else if (*p == '\0')
21593 {
21594 /* Complain, but do define it. */
4d3c2250 21595 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21596 macro_define_function (file, line, name,
6e70227d 21597 argc, (const char **) argv,
2e276125
JB
21598 p);
21599 }
21600 else
21601 /* Just complain. */
4d3c2250 21602 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21603 }
21604 else
21605 /* Just complain. */
4d3c2250 21606 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21607
21608 xfree (name);
21609 {
21610 int i;
21611
21612 for (i = 0; i < argc; i++)
21613 xfree (argv[i]);
21614 }
21615 xfree (argv);
21616 }
21617 else
4d3c2250 21618 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21619}
21620
cf2c3c16
TT
21621/* Skip some bytes from BYTES according to the form given in FORM.
21622 Returns the new pointer. */
2e276125 21623
d521ce57
TT
21624static const gdb_byte *
21625skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21626 enum dwarf_form form,
21627 unsigned int offset_size,
21628 struct dwarf2_section_info *section)
2e276125 21629{
cf2c3c16 21630 unsigned int bytes_read;
2e276125 21631
cf2c3c16 21632 switch (form)
2e276125 21633 {
cf2c3c16
TT
21634 case DW_FORM_data1:
21635 case DW_FORM_flag:
21636 ++bytes;
21637 break;
21638
21639 case DW_FORM_data2:
21640 bytes += 2;
21641 break;
21642
21643 case DW_FORM_data4:
21644 bytes += 4;
21645 break;
21646
21647 case DW_FORM_data8:
21648 bytes += 8;
21649 break;
21650
0224619f
JK
21651 case DW_FORM_data16:
21652 bytes += 16;
21653 break;
21654
cf2c3c16
TT
21655 case DW_FORM_string:
21656 read_direct_string (abfd, bytes, &bytes_read);
21657 bytes += bytes_read;
21658 break;
21659
21660 case DW_FORM_sec_offset:
21661 case DW_FORM_strp:
36586728 21662 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21663 bytes += offset_size;
21664 break;
21665
21666 case DW_FORM_block:
21667 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21668 bytes += bytes_read;
21669 break;
21670
21671 case DW_FORM_block1:
21672 bytes += 1 + read_1_byte (abfd, bytes);
21673 break;
21674 case DW_FORM_block2:
21675 bytes += 2 + read_2_bytes (abfd, bytes);
21676 break;
21677 case DW_FORM_block4:
21678 bytes += 4 + read_4_bytes (abfd, bytes);
21679 break;
21680
21681 case DW_FORM_sdata:
21682 case DW_FORM_udata:
3019eac3
DE
21683 case DW_FORM_GNU_addr_index:
21684 case DW_FORM_GNU_str_index:
d521ce57 21685 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21686 if (bytes == NULL)
21687 {
21688 dwarf2_section_buffer_overflow_complaint (section);
21689 return NULL;
21690 }
cf2c3c16
TT
21691 break;
21692
21693 default:
21694 {
21695 complain:
21696 complaint (&symfile_complaints,
21697 _("invalid form 0x%x in `%s'"),
a32a8923 21698 form, get_section_name (section));
cf2c3c16
TT
21699 return NULL;
21700 }
2e276125
JB
21701 }
21702
cf2c3c16
TT
21703 return bytes;
21704}
757a13d0 21705
cf2c3c16
TT
21706/* A helper for dwarf_decode_macros that handles skipping an unknown
21707 opcode. Returns an updated pointer to the macro data buffer; or,
21708 on error, issues a complaint and returns NULL. */
757a13d0 21709
d521ce57 21710static const gdb_byte *
cf2c3c16 21711skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21712 const gdb_byte **opcode_definitions,
21713 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21714 bfd *abfd,
21715 unsigned int offset_size,
21716 struct dwarf2_section_info *section)
21717{
21718 unsigned int bytes_read, i;
21719 unsigned long arg;
d521ce57 21720 const gdb_byte *defn;
2e276125 21721
cf2c3c16 21722 if (opcode_definitions[opcode] == NULL)
2e276125 21723 {
cf2c3c16
TT
21724 complaint (&symfile_complaints,
21725 _("unrecognized DW_MACFINO opcode 0x%x"),
21726 opcode);
21727 return NULL;
21728 }
2e276125 21729
cf2c3c16
TT
21730 defn = opcode_definitions[opcode];
21731 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21732 defn += bytes_read;
2e276125 21733
cf2c3c16
TT
21734 for (i = 0; i < arg; ++i)
21735 {
aead7601
SM
21736 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21737 (enum dwarf_form) defn[i], offset_size,
f664829e 21738 section);
cf2c3c16
TT
21739 if (mac_ptr == NULL)
21740 {
21741 /* skip_form_bytes already issued the complaint. */
21742 return NULL;
21743 }
21744 }
757a13d0 21745
cf2c3c16
TT
21746 return mac_ptr;
21747}
757a13d0 21748
cf2c3c16
TT
21749/* A helper function which parses the header of a macro section.
21750 If the macro section is the extended (for now called "GNU") type,
21751 then this updates *OFFSET_SIZE. Returns a pointer to just after
21752 the header, or issues a complaint and returns NULL on error. */
757a13d0 21753
d521ce57
TT
21754static const gdb_byte *
21755dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21756 bfd *abfd,
d521ce57 21757 const gdb_byte *mac_ptr,
cf2c3c16
TT
21758 unsigned int *offset_size,
21759 int section_is_gnu)
21760{
21761 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21762
cf2c3c16
TT
21763 if (section_is_gnu)
21764 {
21765 unsigned int version, flags;
757a13d0 21766
cf2c3c16 21767 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21768 if (version != 4 && version != 5)
cf2c3c16
TT
21769 {
21770 complaint (&symfile_complaints,
21771 _("unrecognized version `%d' in .debug_macro section"),
21772 version);
21773 return NULL;
21774 }
21775 mac_ptr += 2;
757a13d0 21776
cf2c3c16
TT
21777 flags = read_1_byte (abfd, mac_ptr);
21778 ++mac_ptr;
21779 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21780
cf2c3c16
TT
21781 if ((flags & 2) != 0)
21782 /* We don't need the line table offset. */
21783 mac_ptr += *offset_size;
757a13d0 21784
cf2c3c16
TT
21785 /* Vendor opcode descriptions. */
21786 if ((flags & 4) != 0)
21787 {
21788 unsigned int i, count;
757a13d0 21789
cf2c3c16
TT
21790 count = read_1_byte (abfd, mac_ptr);
21791 ++mac_ptr;
21792 for (i = 0; i < count; ++i)
21793 {
21794 unsigned int opcode, bytes_read;
21795 unsigned long arg;
21796
21797 opcode = read_1_byte (abfd, mac_ptr);
21798 ++mac_ptr;
21799 opcode_definitions[opcode] = mac_ptr;
21800 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21801 mac_ptr += bytes_read;
21802 mac_ptr += arg;
21803 }
757a13d0 21804 }
cf2c3c16 21805 }
757a13d0 21806
cf2c3c16
TT
21807 return mac_ptr;
21808}
757a13d0 21809
cf2c3c16 21810/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21811 including DW_MACRO_import. */
cf2c3c16
TT
21812
21813static void
d521ce57
TT
21814dwarf_decode_macro_bytes (bfd *abfd,
21815 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21816 struct macro_source_file *current_file,
43f3e411 21817 struct line_header *lh,
cf2c3c16 21818 struct dwarf2_section_info *section,
36586728 21819 int section_is_gnu, int section_is_dwz,
cf2c3c16 21820 unsigned int offset_size,
8fc3fc34 21821 htab_t include_hash)
cf2c3c16 21822{
4d663531 21823 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21824 enum dwarf_macro_record_type macinfo_type;
21825 int at_commandline;
d521ce57 21826 const gdb_byte *opcode_definitions[256];
757a13d0 21827
cf2c3c16
TT
21828 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21829 &offset_size, section_is_gnu);
21830 if (mac_ptr == NULL)
21831 {
21832 /* We already issued a complaint. */
21833 return;
21834 }
757a13d0
JK
21835
21836 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21837 GDB is still reading the definitions from command line. First
21838 DW_MACINFO_start_file will need to be ignored as it was already executed
21839 to create CURRENT_FILE for the main source holding also the command line
21840 definitions. On first met DW_MACINFO_start_file this flag is reset to
21841 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21842
21843 at_commandline = 1;
21844
21845 do
21846 {
21847 /* Do we at least have room for a macinfo type byte? */
21848 if (mac_ptr >= mac_end)
21849 {
f664829e 21850 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21851 break;
21852 }
21853
aead7601 21854 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21855 mac_ptr++;
21856
cf2c3c16
TT
21857 /* Note that we rely on the fact that the corresponding GNU and
21858 DWARF constants are the same. */
757a13d0
JK
21859 switch (macinfo_type)
21860 {
21861 /* A zero macinfo type indicates the end of the macro
21862 information. */
21863 case 0:
21864 break;
2e276125 21865
0af92d60
JK
21866 case DW_MACRO_define:
21867 case DW_MACRO_undef:
21868 case DW_MACRO_define_strp:
21869 case DW_MACRO_undef_strp:
21870 case DW_MACRO_define_sup:
21871 case DW_MACRO_undef_sup:
2e276125 21872 {
891d2f0b 21873 unsigned int bytes_read;
2e276125 21874 int line;
d521ce57 21875 const char *body;
cf2c3c16 21876 int is_define;
2e276125 21877
cf2c3c16
TT
21878 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21879 mac_ptr += bytes_read;
21880
0af92d60
JK
21881 if (macinfo_type == DW_MACRO_define
21882 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
21883 {
21884 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21885 mac_ptr += bytes_read;
21886 }
21887 else
21888 {
21889 LONGEST str_offset;
21890
21891 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21892 mac_ptr += offset_size;
2e276125 21893
0af92d60
JK
21894 if (macinfo_type == DW_MACRO_define_sup
21895 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 21896 || section_is_dwz)
36586728
TT
21897 {
21898 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21899
21900 body = read_indirect_string_from_dwz (dwz, str_offset);
21901 }
21902 else
21903 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21904 }
21905
0af92d60
JK
21906 is_define = (macinfo_type == DW_MACRO_define
21907 || macinfo_type == DW_MACRO_define_strp
21908 || macinfo_type == DW_MACRO_define_sup);
2e276125 21909 if (! current_file)
757a13d0
JK
21910 {
21911 /* DWARF violation as no main source is present. */
21912 complaint (&symfile_complaints,
21913 _("debug info with no main source gives macro %s "
21914 "on line %d: %s"),
cf2c3c16
TT
21915 is_define ? _("definition") : _("undefinition"),
21916 line, body);
757a13d0
JK
21917 break;
21918 }
3e43a32a
MS
21919 if ((line == 0 && !at_commandline)
21920 || (line != 0 && at_commandline))
4d3c2250 21921 complaint (&symfile_complaints,
757a13d0
JK
21922 _("debug info gives %s macro %s with %s line %d: %s"),
21923 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21924 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21925 line == 0 ? _("zero") : _("non-zero"), line, body);
21926
cf2c3c16 21927 if (is_define)
757a13d0 21928 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21929 else
21930 {
0af92d60
JK
21931 gdb_assert (macinfo_type == DW_MACRO_undef
21932 || macinfo_type == DW_MACRO_undef_strp
21933 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
21934 macro_undef (current_file, line, body);
21935 }
2e276125
JB
21936 }
21937 break;
21938
0af92d60 21939 case DW_MACRO_start_file:
2e276125 21940 {
891d2f0b 21941 unsigned int bytes_read;
2e276125
JB
21942 int line, file;
21943
21944 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21945 mac_ptr += bytes_read;
21946 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21947 mac_ptr += bytes_read;
21948
3e43a32a
MS
21949 if ((line == 0 && !at_commandline)
21950 || (line != 0 && at_commandline))
757a13d0
JK
21951 complaint (&symfile_complaints,
21952 _("debug info gives source %d included "
21953 "from %s at %s line %d"),
21954 file, at_commandline ? _("command-line") : _("file"),
21955 line == 0 ? _("zero") : _("non-zero"), line);
21956
21957 if (at_commandline)
21958 {
0af92d60 21959 /* This DW_MACRO_start_file was executed in the
cf2c3c16 21960 pass one. */
757a13d0
JK
21961 at_commandline = 0;
21962 }
21963 else
43f3e411 21964 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
21965 }
21966 break;
21967
0af92d60 21968 case DW_MACRO_end_file:
2e276125 21969 if (! current_file)
4d3c2250 21970 complaint (&symfile_complaints,
3e43a32a
MS
21971 _("macro debug info has an unmatched "
21972 "`close_file' directive"));
2e276125
JB
21973 else
21974 {
21975 current_file = current_file->included_by;
21976 if (! current_file)
21977 {
cf2c3c16 21978 enum dwarf_macro_record_type next_type;
2e276125
JB
21979
21980 /* GCC circa March 2002 doesn't produce the zero
21981 type byte marking the end of the compilation
21982 unit. Complain if it's not there, but exit no
21983 matter what. */
21984
21985 /* Do we at least have room for a macinfo type byte? */
21986 if (mac_ptr >= mac_end)
21987 {
f664829e 21988 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
21989 return;
21990 }
21991
21992 /* We don't increment mac_ptr here, so this is just
21993 a look-ahead. */
aead7601
SM
21994 next_type
21995 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21996 mac_ptr);
2e276125 21997 if (next_type != 0)
4d3c2250 21998 complaint (&symfile_complaints,
3e43a32a
MS
21999 _("no terminating 0-type entry for "
22000 "macros in `.debug_macinfo' section"));
2e276125
JB
22001
22002 return;
22003 }
22004 }
22005 break;
22006
0af92d60
JK
22007 case DW_MACRO_import:
22008 case DW_MACRO_import_sup:
cf2c3c16
TT
22009 {
22010 LONGEST offset;
8fc3fc34 22011 void **slot;
a036ba48
TT
22012 bfd *include_bfd = abfd;
22013 struct dwarf2_section_info *include_section = section;
d521ce57 22014 const gdb_byte *include_mac_end = mac_end;
a036ba48 22015 int is_dwz = section_is_dwz;
d521ce57 22016 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22017
22018 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22019 mac_ptr += offset_size;
22020
0af92d60 22021 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22022 {
22023 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22024
4d663531 22025 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22026
a036ba48 22027 include_section = &dwz->macro;
a32a8923 22028 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22029 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22030 is_dwz = 1;
22031 }
22032
22033 new_mac_ptr = include_section->buffer + offset;
22034 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22035
8fc3fc34
TT
22036 if (*slot != NULL)
22037 {
22038 /* This has actually happened; see
22039 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22040 complaint (&symfile_complaints,
0af92d60 22041 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22042 ".debug_macro section"));
22043 }
22044 else
22045 {
d521ce57 22046 *slot = (void *) new_mac_ptr;
36586728 22047
a036ba48 22048 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22049 include_mac_end, current_file, lh,
36586728 22050 section, section_is_gnu, is_dwz,
4d663531 22051 offset_size, include_hash);
8fc3fc34 22052
d521ce57 22053 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22054 }
cf2c3c16
TT
22055 }
22056 break;
22057
2e276125 22058 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22059 if (!section_is_gnu)
22060 {
22061 unsigned int bytes_read;
2e276125 22062
ac298888
TT
22063 /* This reads the constant, but since we don't recognize
22064 any vendor extensions, we ignore it. */
22065 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22066 mac_ptr += bytes_read;
22067 read_direct_string (abfd, mac_ptr, &bytes_read);
22068 mac_ptr += bytes_read;
2e276125 22069
cf2c3c16
TT
22070 /* We don't recognize any vendor extensions. */
22071 break;
22072 }
22073 /* FALLTHROUGH */
22074
22075 default:
22076 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22077 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22078 section);
22079 if (mac_ptr == NULL)
22080 return;
22081 break;
2e276125 22082 }
757a13d0 22083 } while (macinfo_type != 0);
2e276125 22084}
8e19ed76 22085
cf2c3c16 22086static void
09262596 22087dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22088 int section_is_gnu)
cf2c3c16 22089{
bb5ed363 22090 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22091 struct line_header *lh = cu->line_header;
22092 bfd *abfd;
d521ce57 22093 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22094 struct macro_source_file *current_file = 0;
22095 enum dwarf_macro_record_type macinfo_type;
22096 unsigned int offset_size = cu->header.offset_size;
d521ce57 22097 const gdb_byte *opcode_definitions[256];
8fc3fc34 22098 struct cleanup *cleanup;
8fc3fc34 22099 void **slot;
09262596
DE
22100 struct dwarf2_section_info *section;
22101 const char *section_name;
22102
22103 if (cu->dwo_unit != NULL)
22104 {
22105 if (section_is_gnu)
22106 {
22107 section = &cu->dwo_unit->dwo_file->sections.macro;
22108 section_name = ".debug_macro.dwo";
22109 }
22110 else
22111 {
22112 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22113 section_name = ".debug_macinfo.dwo";
22114 }
22115 }
22116 else
22117 {
22118 if (section_is_gnu)
22119 {
22120 section = &dwarf2_per_objfile->macro;
22121 section_name = ".debug_macro";
22122 }
22123 else
22124 {
22125 section = &dwarf2_per_objfile->macinfo;
22126 section_name = ".debug_macinfo";
22127 }
22128 }
cf2c3c16 22129
bb5ed363 22130 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22131 if (section->buffer == NULL)
22132 {
fceca515 22133 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22134 return;
22135 }
a32a8923 22136 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22137
22138 /* First pass: Find the name of the base filename.
22139 This filename is needed in order to process all macros whose definition
22140 (or undefinition) comes from the command line. These macros are defined
22141 before the first DW_MACINFO_start_file entry, and yet still need to be
22142 associated to the base file.
22143
22144 To determine the base file name, we scan the macro definitions until we
22145 reach the first DW_MACINFO_start_file entry. We then initialize
22146 CURRENT_FILE accordingly so that any macro definition found before the
22147 first DW_MACINFO_start_file can still be associated to the base file. */
22148
22149 mac_ptr = section->buffer + offset;
22150 mac_end = section->buffer + section->size;
22151
22152 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22153 &offset_size, section_is_gnu);
22154 if (mac_ptr == NULL)
22155 {
22156 /* We already issued a complaint. */
22157 return;
22158 }
22159
22160 do
22161 {
22162 /* Do we at least have room for a macinfo type byte? */
22163 if (mac_ptr >= mac_end)
22164 {
22165 /* Complaint is printed during the second pass as GDB will probably
22166 stop the first pass earlier upon finding
22167 DW_MACINFO_start_file. */
22168 break;
22169 }
22170
aead7601 22171 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22172 mac_ptr++;
22173
22174 /* Note that we rely on the fact that the corresponding GNU and
22175 DWARF constants are the same. */
22176 switch (macinfo_type)
22177 {
22178 /* A zero macinfo type indicates the end of the macro
22179 information. */
22180 case 0:
22181 break;
22182
0af92d60
JK
22183 case DW_MACRO_define:
22184 case DW_MACRO_undef:
cf2c3c16
TT
22185 /* Only skip the data by MAC_PTR. */
22186 {
22187 unsigned int bytes_read;
22188
22189 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22190 mac_ptr += bytes_read;
22191 read_direct_string (abfd, mac_ptr, &bytes_read);
22192 mac_ptr += bytes_read;
22193 }
22194 break;
22195
0af92d60 22196 case DW_MACRO_start_file:
cf2c3c16
TT
22197 {
22198 unsigned int bytes_read;
22199 int line, file;
22200
22201 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22202 mac_ptr += bytes_read;
22203 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22204 mac_ptr += bytes_read;
22205
43f3e411 22206 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22207 }
22208 break;
22209
0af92d60 22210 case DW_MACRO_end_file:
cf2c3c16
TT
22211 /* No data to skip by MAC_PTR. */
22212 break;
22213
0af92d60
JK
22214 case DW_MACRO_define_strp:
22215 case DW_MACRO_undef_strp:
22216 case DW_MACRO_define_sup:
22217 case DW_MACRO_undef_sup:
cf2c3c16
TT
22218 {
22219 unsigned int bytes_read;
22220
22221 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22222 mac_ptr += bytes_read;
22223 mac_ptr += offset_size;
22224 }
22225 break;
22226
0af92d60
JK
22227 case DW_MACRO_import:
22228 case DW_MACRO_import_sup:
cf2c3c16 22229 /* Note that, according to the spec, a transparent include
0af92d60 22230 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22231 skip this opcode. */
22232 mac_ptr += offset_size;
22233 break;
22234
22235 case DW_MACINFO_vendor_ext:
22236 /* Only skip the data by MAC_PTR. */
22237 if (!section_is_gnu)
22238 {
22239 unsigned int bytes_read;
22240
22241 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22242 mac_ptr += bytes_read;
22243 read_direct_string (abfd, mac_ptr, &bytes_read);
22244 mac_ptr += bytes_read;
22245 }
22246 /* FALLTHROUGH */
22247
22248 default:
22249 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22250 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22251 section);
22252 if (mac_ptr == NULL)
22253 return;
22254 break;
22255 }
22256 } while (macinfo_type != 0 && current_file == NULL);
22257
22258 /* Second pass: Process all entries.
22259
22260 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22261 command-line macro definitions/undefinitions. This flag is unset when we
22262 reach the first DW_MACINFO_start_file entry. */
22263
fc4007c9
TT
22264 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22265 htab_eq_pointer,
22266 NULL, xcalloc, xfree));
8fc3fc34 22267 mac_ptr = section->buffer + offset;
fc4007c9 22268 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22269 *slot = (void *) mac_ptr;
8fc3fc34 22270 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22271 current_file, lh, section,
fc4007c9
TT
22272 section_is_gnu, 0, offset_size,
22273 include_hash.get ());
cf2c3c16
TT
22274}
22275
8e19ed76 22276/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22277 if so return true else false. */
380bca97 22278
8e19ed76 22279static int
6e5a29e1 22280attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22281{
22282 return (attr == NULL ? 0 :
22283 attr->form == DW_FORM_block1
22284 || attr->form == DW_FORM_block2
22285 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22286 || attr->form == DW_FORM_block
22287 || attr->form == DW_FORM_exprloc);
8e19ed76 22288}
4c2df51b 22289
c6a0999f
JB
22290/* Return non-zero if ATTR's value is a section offset --- classes
22291 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22292 You may use DW_UNSND (attr) to retrieve such offsets.
22293
22294 Section 7.5.4, "Attribute Encodings", explains that no attribute
22295 may have a value that belongs to more than one of these classes; it
22296 would be ambiguous if we did, because we use the same forms for all
22297 of them. */
380bca97 22298
3690dd37 22299static int
6e5a29e1 22300attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22301{
22302 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22303 || attr->form == DW_FORM_data8
22304 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22305}
22306
3690dd37
JB
22307/* Return non-zero if ATTR's value falls in the 'constant' class, or
22308 zero otherwise. When this function returns true, you can apply
22309 dwarf2_get_attr_constant_value to it.
22310
22311 However, note that for some attributes you must check
22312 attr_form_is_section_offset before using this test. DW_FORM_data4
22313 and DW_FORM_data8 are members of both the constant class, and of
22314 the classes that contain offsets into other debug sections
22315 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22316 that, if an attribute's can be either a constant or one of the
22317 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22318 taken as section offsets, not constants.
22319
22320 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22321 cannot handle that. */
380bca97 22322
3690dd37 22323static int
6e5a29e1 22324attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22325{
22326 switch (attr->form)
22327 {
22328 case DW_FORM_sdata:
22329 case DW_FORM_udata:
22330 case DW_FORM_data1:
22331 case DW_FORM_data2:
22332 case DW_FORM_data4:
22333 case DW_FORM_data8:
22334 return 1;
22335 default:
22336 return 0;
22337 }
22338}
22339
7771576e
SA
22340
22341/* DW_ADDR is always stored already as sect_offset; despite for the forms
22342 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22343
22344static int
6e5a29e1 22345attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22346{
22347 switch (attr->form)
22348 {
22349 case DW_FORM_ref_addr:
22350 case DW_FORM_ref1:
22351 case DW_FORM_ref2:
22352 case DW_FORM_ref4:
22353 case DW_FORM_ref8:
22354 case DW_FORM_ref_udata:
22355 case DW_FORM_GNU_ref_alt:
22356 return 1;
22357 default:
22358 return 0;
22359 }
22360}
22361
3019eac3
DE
22362/* Return the .debug_loc section to use for CU.
22363 For DWO files use .debug_loc.dwo. */
22364
22365static struct dwarf2_section_info *
22366cu_debug_loc_section (struct dwarf2_cu *cu)
22367{
22368 if (cu->dwo_unit)
43988095
JK
22369 {
22370 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22371
22372 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22373 }
22374 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22375 : &dwarf2_per_objfile->loc);
3019eac3
DE
22376}
22377
8cf6f0b1
TT
22378/* A helper function that fills in a dwarf2_loclist_baton. */
22379
22380static void
22381fill_in_loclist_baton (struct dwarf2_cu *cu,
22382 struct dwarf2_loclist_baton *baton,
ff39bb5e 22383 const struct attribute *attr)
8cf6f0b1 22384{
3019eac3
DE
22385 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22386
22387 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22388
22389 baton->per_cu = cu->per_cu;
22390 gdb_assert (baton->per_cu);
22391 /* We don't know how long the location list is, but make sure we
22392 don't run off the edge of the section. */
3019eac3
DE
22393 baton->size = section->size - DW_UNSND (attr);
22394 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22395 baton->base_address = cu->base_address;
f664829e 22396 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22397}
22398
4c2df51b 22399static void
ff39bb5e 22400dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22401 struct dwarf2_cu *cu, int is_block)
4c2df51b 22402{
bb5ed363 22403 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22404 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22405
3690dd37 22406 if (attr_form_is_section_offset (attr)
3019eac3 22407 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22408 the section. If so, fall through to the complaint in the
22409 other branch. */
3019eac3 22410 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22411 {
0d53c4c4 22412 struct dwarf2_loclist_baton *baton;
4c2df51b 22413
8d749320 22414 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22415
8cf6f0b1 22416 fill_in_loclist_baton (cu, baton, attr);
be391dca 22417
d00adf39 22418 if (cu->base_known == 0)
0d53c4c4 22419 complaint (&symfile_complaints,
3e43a32a
MS
22420 _("Location list used without "
22421 "specifying the CU base address."));
4c2df51b 22422
f1e6e072
TT
22423 SYMBOL_ACLASS_INDEX (sym) = (is_block
22424 ? dwarf2_loclist_block_index
22425 : dwarf2_loclist_index);
0d53c4c4
DJ
22426 SYMBOL_LOCATION_BATON (sym) = baton;
22427 }
22428 else
22429 {
22430 struct dwarf2_locexpr_baton *baton;
22431
8d749320 22432 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22433 baton->per_cu = cu->per_cu;
22434 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22435
22436 if (attr_form_is_block (attr))
22437 {
22438 /* Note that we're just copying the block's data pointer
22439 here, not the actual data. We're still pointing into the
6502dd73
DJ
22440 info_buffer for SYM's objfile; right now we never release
22441 that buffer, but when we do clean up properly this may
22442 need to change. */
0d53c4c4
DJ
22443 baton->size = DW_BLOCK (attr)->size;
22444 baton->data = DW_BLOCK (attr)->data;
22445 }
22446 else
22447 {
22448 dwarf2_invalid_attrib_class_complaint ("location description",
22449 SYMBOL_NATURAL_NAME (sym));
22450 baton->size = 0;
0d53c4c4 22451 }
6e70227d 22452
f1e6e072
TT
22453 SYMBOL_ACLASS_INDEX (sym) = (is_block
22454 ? dwarf2_locexpr_block_index
22455 : dwarf2_locexpr_index);
0d53c4c4
DJ
22456 SYMBOL_LOCATION_BATON (sym) = baton;
22457 }
4c2df51b 22458}
6502dd73 22459
9aa1f1e3
TT
22460/* Return the OBJFILE associated with the compilation unit CU. If CU
22461 came from a separate debuginfo file, then the master objfile is
22462 returned. */
ae0d2f24
UW
22463
22464struct objfile *
22465dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22466{
9291a0cd 22467 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22468
22469 /* Return the master objfile, so that we can report and look up the
22470 correct file containing this variable. */
22471 if (objfile->separate_debug_objfile_backlink)
22472 objfile = objfile->separate_debug_objfile_backlink;
22473
22474 return objfile;
22475}
22476
96408a79
SA
22477/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22478 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22479 CU_HEADERP first. */
22480
22481static const struct comp_unit_head *
22482per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22483 struct dwarf2_per_cu_data *per_cu)
22484{
d521ce57 22485 const gdb_byte *info_ptr;
96408a79
SA
22486
22487 if (per_cu->cu)
22488 return &per_cu->cu->header;
22489
8a0459fd 22490 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
22491
22492 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22493 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22494 rcuh_kind::COMPILE);
96408a79
SA
22495
22496 return cu_headerp;
22497}
22498
ae0d2f24
UW
22499/* Return the address size given in the compilation unit header for CU. */
22500
98714339 22501int
ae0d2f24
UW
22502dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22503{
96408a79
SA
22504 struct comp_unit_head cu_header_local;
22505 const struct comp_unit_head *cu_headerp;
c471e790 22506
96408a79
SA
22507 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22508
22509 return cu_headerp->addr_size;
ae0d2f24
UW
22510}
22511
9eae7c52
TT
22512/* Return the offset size given in the compilation unit header for CU. */
22513
22514int
22515dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22516{
96408a79
SA
22517 struct comp_unit_head cu_header_local;
22518 const struct comp_unit_head *cu_headerp;
9c6c53f7 22519
96408a79
SA
22520 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22521
22522 return cu_headerp->offset_size;
22523}
22524
22525/* See its dwarf2loc.h declaration. */
22526
22527int
22528dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22529{
22530 struct comp_unit_head cu_header_local;
22531 const struct comp_unit_head *cu_headerp;
22532
22533 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22534
22535 if (cu_headerp->version == 2)
22536 return cu_headerp->addr_size;
22537 else
22538 return cu_headerp->offset_size;
181cebd4
JK
22539}
22540
9aa1f1e3
TT
22541/* Return the text offset of the CU. The returned offset comes from
22542 this CU's objfile. If this objfile came from a separate debuginfo
22543 file, then the offset may be different from the corresponding
22544 offset in the parent objfile. */
22545
22546CORE_ADDR
22547dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22548{
bb3fa9d0 22549 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22550
22551 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22552}
22553
43988095
JK
22554/* Return DWARF version number of PER_CU. */
22555
22556short
22557dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22558{
22559 return per_cu->dwarf_version;
22560}
22561
348e048f
DE
22562/* Locate the .debug_info compilation unit from CU's objfile which contains
22563 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22564
22565static struct dwarf2_per_cu_data *
b64f50a1 22566dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 22567 unsigned int offset_in_dwz,
ae038cb0
DJ
22568 struct objfile *objfile)
22569{
22570 struct dwarf2_per_cu_data *this_cu;
22571 int low, high;
36586728 22572 const sect_offset *cu_off;
ae038cb0 22573
ae038cb0
DJ
22574 low = 0;
22575 high = dwarf2_per_objfile->n_comp_units - 1;
22576 while (high > low)
22577 {
36586728 22578 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22579 int mid = low + (high - low) / 2;
9a619af0 22580
36586728
TT
22581 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22582 cu_off = &mid_cu->offset;
22583 if (mid_cu->is_dwz > offset_in_dwz
22584 || (mid_cu->is_dwz == offset_in_dwz
22585 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
22586 high = mid;
22587 else
22588 low = mid + 1;
22589 }
22590 gdb_assert (low == high);
36586728
TT
22591 this_cu = dwarf2_per_objfile->all_comp_units[low];
22592 cu_off = &this_cu->offset;
22593 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 22594 {
36586728 22595 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
22596 error (_("Dwarf Error: could not find partial DIE containing "
22597 "offset 0x%lx [in module %s]"),
b64f50a1 22598 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 22599
b64f50a1
JK
22600 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22601 <= offset.sect_off);
ae038cb0
DJ
22602 return dwarf2_per_objfile->all_comp_units[low-1];
22603 }
22604 else
22605 {
22606 this_cu = dwarf2_per_objfile->all_comp_units[low];
22607 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
22608 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22609 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22610 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
22611 return this_cu;
22612 }
22613}
22614
23745b47 22615/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22616
9816fde3 22617static void
23745b47 22618init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22619{
9816fde3 22620 memset (cu, 0, sizeof (*cu));
23745b47
DE
22621 per_cu->cu = cu;
22622 cu->per_cu = per_cu;
22623 cu->objfile = per_cu->objfile;
93311388 22624 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22625}
22626
22627/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22628
22629static void
95554aad
TT
22630prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22631 enum language pretend_language)
9816fde3
JK
22632{
22633 struct attribute *attr;
22634
22635 /* Set the language we're debugging. */
22636 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22637 if (attr)
22638 set_cu_language (DW_UNSND (attr), cu);
22639 else
9cded63f 22640 {
95554aad 22641 cu->language = pretend_language;
9cded63f
TT
22642 cu->language_defn = language_def (cu->language);
22643 }
dee91e82 22644
7d45c7c3 22645 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22646}
22647
ae038cb0
DJ
22648/* Release one cached compilation unit, CU. We unlink it from the tree
22649 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22650 the caller is responsible for that.
22651 NOTE: DATA is a void * because this function is also used as a
22652 cleanup routine. */
ae038cb0
DJ
22653
22654static void
68dc6402 22655free_heap_comp_unit (void *data)
ae038cb0 22656{
9a3c8263 22657 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22658
23745b47
DE
22659 gdb_assert (cu->per_cu != NULL);
22660 cu->per_cu->cu = NULL;
ae038cb0
DJ
22661 cu->per_cu = NULL;
22662
22663 obstack_free (&cu->comp_unit_obstack, NULL);
22664
22665 xfree (cu);
22666}
22667
72bf9492 22668/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22669 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22670 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22671
22672static void
22673free_stack_comp_unit (void *data)
22674{
9a3c8263 22675 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22676
23745b47
DE
22677 gdb_assert (cu->per_cu != NULL);
22678 cu->per_cu->cu = NULL;
22679 cu->per_cu = NULL;
22680
72bf9492
DJ
22681 obstack_free (&cu->comp_unit_obstack, NULL);
22682 cu->partial_dies = NULL;
ae038cb0
DJ
22683}
22684
22685/* Free all cached compilation units. */
22686
22687static void
22688free_cached_comp_units (void *data)
22689{
22690 struct dwarf2_per_cu_data *per_cu, **last_chain;
22691
22692 per_cu = dwarf2_per_objfile->read_in_chain;
22693 last_chain = &dwarf2_per_objfile->read_in_chain;
22694 while (per_cu != NULL)
22695 {
22696 struct dwarf2_per_cu_data *next_cu;
22697
22698 next_cu = per_cu->cu->read_in_chain;
22699
68dc6402 22700 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22701 *last_chain = next_cu;
22702
22703 per_cu = next_cu;
22704 }
22705}
22706
22707/* Increase the age counter on each cached compilation unit, and free
22708 any that are too old. */
22709
22710static void
22711age_cached_comp_units (void)
22712{
22713 struct dwarf2_per_cu_data *per_cu, **last_chain;
22714
22715 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22716 per_cu = dwarf2_per_objfile->read_in_chain;
22717 while (per_cu != NULL)
22718 {
22719 per_cu->cu->last_used ++;
b4f54984 22720 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22721 dwarf2_mark (per_cu->cu);
22722 per_cu = per_cu->cu->read_in_chain;
22723 }
22724
22725 per_cu = dwarf2_per_objfile->read_in_chain;
22726 last_chain = &dwarf2_per_objfile->read_in_chain;
22727 while (per_cu != NULL)
22728 {
22729 struct dwarf2_per_cu_data *next_cu;
22730
22731 next_cu = per_cu->cu->read_in_chain;
22732
22733 if (!per_cu->cu->mark)
22734 {
68dc6402 22735 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22736 *last_chain = next_cu;
22737 }
22738 else
22739 last_chain = &per_cu->cu->read_in_chain;
22740
22741 per_cu = next_cu;
22742 }
22743}
22744
22745/* Remove a single compilation unit from the cache. */
22746
22747static void
dee91e82 22748free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22749{
22750 struct dwarf2_per_cu_data *per_cu, **last_chain;
22751
22752 per_cu = dwarf2_per_objfile->read_in_chain;
22753 last_chain = &dwarf2_per_objfile->read_in_chain;
22754 while (per_cu != NULL)
22755 {
22756 struct dwarf2_per_cu_data *next_cu;
22757
22758 next_cu = per_cu->cu->read_in_chain;
22759
dee91e82 22760 if (per_cu == target_per_cu)
ae038cb0 22761 {
68dc6402 22762 free_heap_comp_unit (per_cu->cu);
dee91e82 22763 per_cu->cu = NULL;
ae038cb0
DJ
22764 *last_chain = next_cu;
22765 break;
22766 }
22767 else
22768 last_chain = &per_cu->cu->read_in_chain;
22769
22770 per_cu = next_cu;
22771 }
22772}
22773
fe3e1990
DJ
22774/* Release all extra memory associated with OBJFILE. */
22775
22776void
22777dwarf2_free_objfile (struct objfile *objfile)
22778{
9a3c8263
SM
22779 dwarf2_per_objfile
22780 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22781 dwarf2_objfile_data_key);
fe3e1990
DJ
22782
22783 if (dwarf2_per_objfile == NULL)
22784 return;
22785
22786 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22787 free_cached_comp_units (NULL);
22788
7b9f3c50
DE
22789 if (dwarf2_per_objfile->quick_file_names_table)
22790 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22791
527f3840
JK
22792 if (dwarf2_per_objfile->line_header_hash)
22793 htab_delete (dwarf2_per_objfile->line_header_hash);
22794
fe3e1990
DJ
22795 /* Everything else should be on the objfile obstack. */
22796}
22797
dee91e82
DE
22798/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22799 We store these in a hash table separate from the DIEs, and preserve them
22800 when the DIEs are flushed out of cache.
22801
22802 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22803 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22804 or the type may come from a DWO file. Furthermore, while it's more logical
22805 to use per_cu->section+offset, with Fission the section with the data is in
22806 the DWO file but we don't know that section at the point we need it.
22807 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22808 because we can enter the lookup routine, get_die_type_at_offset, from
22809 outside this file, and thus won't necessarily have PER_CU->cu.
22810 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22811
dee91e82 22812struct dwarf2_per_cu_offset_and_type
1c379e20 22813{
dee91e82 22814 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 22815 sect_offset offset;
1c379e20
DJ
22816 struct type *type;
22817};
22818
dee91e82 22819/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22820
22821static hashval_t
dee91e82 22822per_cu_offset_and_type_hash (const void *item)
1c379e20 22823{
9a3c8263
SM
22824 const struct dwarf2_per_cu_offset_and_type *ofs
22825 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22826
dee91e82 22827 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
22828}
22829
dee91e82 22830/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22831
22832static int
dee91e82 22833per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22834{
9a3c8263
SM
22835 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22836 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22837 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22838 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22839
dee91e82
DE
22840 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22841 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
22842}
22843
22844/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22845 table if necessary. For convenience, return TYPE.
22846
22847 The DIEs reading must have careful ordering to:
22848 * Not cause infite loops trying to read in DIEs as a prerequisite for
22849 reading current DIE.
22850 * Not trying to dereference contents of still incompletely read in types
22851 while reading in other DIEs.
22852 * Enable referencing still incompletely read in types just by a pointer to
22853 the type without accessing its fields.
22854
22855 Therefore caller should follow these rules:
22856 * Try to fetch any prerequisite types we may need to build this DIE type
22857 before building the type and calling set_die_type.
e71ec853 22858 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22859 possible before fetching more types to complete the current type.
22860 * Make the type as complete as possible before fetching more types. */
1c379e20 22861
f792889a 22862static struct type *
1c379e20
DJ
22863set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22864{
dee91e82 22865 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22866 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22867 struct attribute *attr;
22868 struct dynamic_prop prop;
1c379e20 22869
b4ba55a1
JB
22870 /* For Ada types, make sure that the gnat-specific data is always
22871 initialized (if not already set). There are a few types where
22872 we should not be doing so, because the type-specific area is
22873 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22874 where the type-specific area is used to store the floatformat).
22875 But this is not a problem, because the gnat-specific information
22876 is actually not needed for these types. */
22877 if (need_gnat_info (cu)
22878 && TYPE_CODE (type) != TYPE_CODE_FUNC
22879 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22880 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22881 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22882 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22883 && !HAVE_GNAT_AUX_INFO (type))
22884 INIT_GNAT_SPECIFIC (type);
22885
3f2f83dd
KB
22886 /* Read DW_AT_allocated and set in type. */
22887 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22888 if (attr_form_is_block (attr))
22889 {
22890 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22891 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22892 }
22893 else if (attr != NULL)
22894 {
22895 complaint (&symfile_complaints,
22896 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22897 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22898 die->offset.sect_off);
22899 }
22900
22901 /* Read DW_AT_associated and set in type. */
22902 attr = dwarf2_attr (die, DW_AT_associated, cu);
22903 if (attr_form_is_block (attr))
22904 {
22905 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22906 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22907 }
22908 else if (attr != NULL)
22909 {
22910 complaint (&symfile_complaints,
22911 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22912 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22913 die->offset.sect_off);
22914 }
22915
3cdcd0ce
JB
22916 /* Read DW_AT_data_location and set in type. */
22917 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22918 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22919 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22920
dee91e82 22921 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22922 {
dee91e82
DE
22923 dwarf2_per_objfile->die_type_hash =
22924 htab_create_alloc_ex (127,
22925 per_cu_offset_and_type_hash,
22926 per_cu_offset_and_type_eq,
22927 NULL,
22928 &objfile->objfile_obstack,
22929 hashtab_obstack_allocate,
22930 dummy_obstack_deallocate);
f792889a 22931 }
1c379e20 22932
dee91e82 22933 ofs.per_cu = cu->per_cu;
1c379e20
DJ
22934 ofs.offset = die->offset;
22935 ofs.type = type;
dee91e82
DE
22936 slot = (struct dwarf2_per_cu_offset_and_type **)
22937 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
22938 if (*slot)
22939 complaint (&symfile_complaints,
22940 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 22941 die->offset.sect_off);
8d749320
SM
22942 *slot = XOBNEW (&objfile->objfile_obstack,
22943 struct dwarf2_per_cu_offset_and_type);
1c379e20 22944 **slot = ofs;
f792889a 22945 return type;
1c379e20
DJ
22946}
22947
02142a6c
DE
22948/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22949 or return NULL if the die does not have a saved type. */
1c379e20
DJ
22950
22951static struct type *
b64f50a1 22952get_die_type_at_offset (sect_offset offset,
673bfd45 22953 struct dwarf2_per_cu_data *per_cu)
1c379e20 22954{
dee91e82 22955 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 22956
dee91e82 22957 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22958 return NULL;
1c379e20 22959
dee91e82 22960 ofs.per_cu = per_cu;
673bfd45 22961 ofs.offset = offset;
9a3c8263
SM
22962 slot = ((struct dwarf2_per_cu_offset_and_type *)
22963 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
22964 if (slot)
22965 return slot->type;
22966 else
22967 return NULL;
22968}
22969
02142a6c 22970/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
22971 or return NULL if DIE does not have a saved type. */
22972
22973static struct type *
22974get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22975{
22976 return get_die_type_at_offset (die->offset, cu->per_cu);
22977}
22978
10b3939b
DJ
22979/* Add a dependence relationship from CU to REF_PER_CU. */
22980
22981static void
22982dwarf2_add_dependence (struct dwarf2_cu *cu,
22983 struct dwarf2_per_cu_data *ref_per_cu)
22984{
22985 void **slot;
22986
22987 if (cu->dependencies == NULL)
22988 cu->dependencies
22989 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22990 NULL, &cu->comp_unit_obstack,
22991 hashtab_obstack_allocate,
22992 dummy_obstack_deallocate);
22993
22994 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22995 if (*slot == NULL)
22996 *slot = ref_per_cu;
22997}
1c379e20 22998
f504f079
DE
22999/* Subroutine of dwarf2_mark to pass to htab_traverse.
23000 Set the mark field in every compilation unit in the
ae038cb0
DJ
23001 cache that we must keep because we are keeping CU. */
23002
10b3939b
DJ
23003static int
23004dwarf2_mark_helper (void **slot, void *data)
23005{
23006 struct dwarf2_per_cu_data *per_cu;
23007
23008 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23009
23010 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23011 reading of the chain. As such dependencies remain valid it is not much
23012 useful to track and undo them during QUIT cleanups. */
23013 if (per_cu->cu == NULL)
23014 return 1;
23015
10b3939b
DJ
23016 if (per_cu->cu->mark)
23017 return 1;
23018 per_cu->cu->mark = 1;
23019
23020 if (per_cu->cu->dependencies != NULL)
23021 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23022
23023 return 1;
23024}
23025
f504f079
DE
23026/* Set the mark field in CU and in every other compilation unit in the
23027 cache that we must keep because we are keeping CU. */
23028
ae038cb0
DJ
23029static void
23030dwarf2_mark (struct dwarf2_cu *cu)
23031{
23032 if (cu->mark)
23033 return;
23034 cu->mark = 1;
10b3939b
DJ
23035 if (cu->dependencies != NULL)
23036 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23037}
23038
23039static void
23040dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23041{
23042 while (per_cu)
23043 {
23044 per_cu->cu->mark = 0;
23045 per_cu = per_cu->cu->read_in_chain;
23046 }
72bf9492
DJ
23047}
23048
72bf9492
DJ
23049/* Trivial hash function for partial_die_info: the hash value of a DIE
23050 is its offset in .debug_info for this objfile. */
23051
23052static hashval_t
23053partial_die_hash (const void *item)
23054{
9a3c8263
SM
23055 const struct partial_die_info *part_die
23056 = (const struct partial_die_info *) item;
9a619af0 23057
b64f50a1 23058 return part_die->offset.sect_off;
72bf9492
DJ
23059}
23060
23061/* Trivial comparison function for partial_die_info structures: two DIEs
23062 are equal if they have the same offset. */
23063
23064static int
23065partial_die_eq (const void *item_lhs, const void *item_rhs)
23066{
9a3c8263
SM
23067 const struct partial_die_info *part_die_lhs
23068 = (const struct partial_die_info *) item_lhs;
23069 const struct partial_die_info *part_die_rhs
23070 = (const struct partial_die_info *) item_rhs;
9a619af0 23071
b64f50a1 23072 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
23073}
23074
b4f54984
DE
23075static struct cmd_list_element *set_dwarf_cmdlist;
23076static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23077
23078static void
b4f54984 23079set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23080{
b4f54984 23081 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23082 gdb_stdout);
ae038cb0
DJ
23083}
23084
23085static void
b4f54984 23086show_dwarf_cmd (char *args, int from_tty)
6e70227d 23087{
b4f54984 23088 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23089}
23090
4bf44c1c 23091/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23092
23093static void
c1bd65d0 23094dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23095{
9a3c8263 23096 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23097 int ix;
8b70b953 23098
626f2d1c
TT
23099 /* Make sure we don't accidentally use dwarf2_per_objfile while
23100 cleaning up. */
23101 dwarf2_per_objfile = NULL;
23102
59b0c7c1
JB
23103 for (ix = 0; ix < data->n_comp_units; ++ix)
23104 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23105
59b0c7c1 23106 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23107 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23108 data->all_type_units[ix]->per_cu.imported_symtabs);
23109 xfree (data->all_type_units);
95554aad 23110
8b70b953 23111 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23112
23113 if (data->dwo_files)
23114 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23115 if (data->dwp_file)
23116 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23117
23118 if (data->dwz_file && data->dwz_file->dwz_bfd)
23119 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23120}
23121
23122\f
ae2de4f8 23123/* The "save gdb-index" command. */
9291a0cd
TT
23124
23125/* The contents of the hash table we create when building the string
23126 table. */
23127struct strtab_entry
23128{
23129 offset_type offset;
23130 const char *str;
23131};
23132
559a7a62
JK
23133/* Hash function for a strtab_entry.
23134
23135 Function is used only during write_hash_table so no index format backward
23136 compatibility is needed. */
b89be57b 23137
9291a0cd
TT
23138static hashval_t
23139hash_strtab_entry (const void *e)
23140{
9a3c8263 23141 const struct strtab_entry *entry = (const struct strtab_entry *) e;
559a7a62 23142 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
23143}
23144
23145/* Equality function for a strtab_entry. */
b89be57b 23146
9291a0cd
TT
23147static int
23148eq_strtab_entry (const void *a, const void *b)
23149{
9a3c8263
SM
23150 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23151 const struct strtab_entry *eb = (const struct strtab_entry *) b;
9291a0cd
TT
23152 return !strcmp (ea->str, eb->str);
23153}
23154
23155/* Create a strtab_entry hash table. */
b89be57b 23156
9291a0cd
TT
23157static htab_t
23158create_strtab (void)
23159{
23160 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23161 xfree, xcalloc, xfree);
23162}
23163
23164/* Add a string to the constant pool. Return the string's offset in
23165 host order. */
b89be57b 23166
9291a0cd
TT
23167static offset_type
23168add_string (htab_t table, struct obstack *cpool, const char *str)
23169{
23170 void **slot;
23171 struct strtab_entry entry;
23172 struct strtab_entry *result;
23173
23174 entry.str = str;
23175 slot = htab_find_slot (table, &entry, INSERT);
23176 if (*slot)
9a3c8263 23177 result = (struct strtab_entry *) *slot;
9291a0cd
TT
23178 else
23179 {
23180 result = XNEW (struct strtab_entry);
23181 result->offset = obstack_object_size (cpool);
23182 result->str = str;
23183 obstack_grow_str0 (cpool, str);
23184 *slot = result;
23185 }
23186 return result->offset;
23187}
23188
23189/* An entry in the symbol table. */
23190struct symtab_index_entry
23191{
23192 /* The name of the symbol. */
23193 const char *name;
23194 /* The offset of the name in the constant pool. */
23195 offset_type index_offset;
23196 /* A sorted vector of the indices of all the CUs that hold an object
23197 of this name. */
23198 VEC (offset_type) *cu_indices;
23199};
23200
23201/* The symbol table. This is a power-of-2-sized hash table. */
23202struct mapped_symtab
23203{
23204 offset_type n_elements;
23205 offset_type size;
23206 struct symtab_index_entry **data;
23207};
23208
23209/* Hash function for a symtab_index_entry. */
b89be57b 23210
9291a0cd
TT
23211static hashval_t
23212hash_symtab_entry (const void *e)
23213{
9a3c8263
SM
23214 const struct symtab_index_entry *entry
23215 = (const struct symtab_index_entry *) e;
9291a0cd
TT
23216 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23217 sizeof (offset_type) * VEC_length (offset_type,
23218 entry->cu_indices),
23219 0);
23220}
23221
23222/* Equality function for a symtab_index_entry. */
b89be57b 23223
9291a0cd
TT
23224static int
23225eq_symtab_entry (const void *a, const void *b)
23226{
9a3c8263
SM
23227 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23228 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
9291a0cd
TT
23229 int len = VEC_length (offset_type, ea->cu_indices);
23230 if (len != VEC_length (offset_type, eb->cu_indices))
23231 return 0;
23232 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23233 VEC_address (offset_type, eb->cu_indices),
23234 sizeof (offset_type) * len);
23235}
23236
23237/* Destroy a symtab_index_entry. */
b89be57b 23238
9291a0cd
TT
23239static void
23240delete_symtab_entry (void *p)
23241{
9a3c8263 23242 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
9291a0cd
TT
23243 VEC_free (offset_type, entry->cu_indices);
23244 xfree (entry);
23245}
23246
23247/* Create a hash table holding symtab_index_entry objects. */
b89be57b 23248
9291a0cd 23249static htab_t
3876f04e 23250create_symbol_hash_table (void)
9291a0cd
TT
23251{
23252 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23253 delete_symtab_entry, xcalloc, xfree);
23254}
23255
23256/* Create a new mapped symtab object. */
b89be57b 23257
9291a0cd
TT
23258static struct mapped_symtab *
23259create_mapped_symtab (void)
23260{
23261 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23262 symtab->n_elements = 0;
23263 symtab->size = 1024;
23264 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23265 return symtab;
23266}
23267
23268/* Destroy a mapped_symtab. */
b89be57b 23269
9291a0cd
TT
23270static void
23271cleanup_mapped_symtab (void *p)
23272{
9a3c8263 23273 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
9291a0cd
TT
23274 /* The contents of the array are freed when the other hash table is
23275 destroyed. */
23276 xfree (symtab->data);
23277 xfree (symtab);
23278}
23279
23280/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
23281 the slot.
23282
23283 Function is used only during write_hash_table so no index format backward
23284 compatibility is needed. */
b89be57b 23285
9291a0cd
TT
23286static struct symtab_index_entry **
23287find_slot (struct mapped_symtab *symtab, const char *name)
23288{
559a7a62 23289 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
23290
23291 index = hash & (symtab->size - 1);
23292 step = ((hash * 17) & (symtab->size - 1)) | 1;
23293
23294 for (;;)
23295 {
23296 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23297 return &symtab->data[index];
23298 index = (index + step) & (symtab->size - 1);
23299 }
23300}
23301
23302/* Expand SYMTAB's hash table. */
b89be57b 23303
9291a0cd
TT
23304static void
23305hash_expand (struct mapped_symtab *symtab)
23306{
23307 offset_type old_size = symtab->size;
23308 offset_type i;
23309 struct symtab_index_entry **old_entries = symtab->data;
23310
23311 symtab->size *= 2;
23312 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23313
23314 for (i = 0; i < old_size; ++i)
23315 {
23316 if (old_entries[i])
23317 {
23318 struct symtab_index_entry **slot = find_slot (symtab,
23319 old_entries[i]->name);
23320 *slot = old_entries[i];
23321 }
23322 }
23323
23324 xfree (old_entries);
23325}
23326
156942c7
DE
23327/* Add an entry to SYMTAB. NAME is the name of the symbol.
23328 CU_INDEX is the index of the CU in which the symbol appears.
23329 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23330
9291a0cd
TT
23331static void
23332add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23333 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23334 offset_type cu_index)
23335{
23336 struct symtab_index_entry **slot;
156942c7 23337 offset_type cu_index_and_attrs;
9291a0cd
TT
23338
23339 ++symtab->n_elements;
23340 if (4 * symtab->n_elements / 3 >= symtab->size)
23341 hash_expand (symtab);
23342
23343 slot = find_slot (symtab, name);
23344 if (!*slot)
23345 {
23346 *slot = XNEW (struct symtab_index_entry);
23347 (*slot)->name = name;
156942c7 23348 /* index_offset is set later. */
9291a0cd
TT
23349 (*slot)->cu_indices = NULL;
23350 }
156942c7
DE
23351
23352 cu_index_and_attrs = 0;
23353 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23354 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23355 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23356
23357 /* We don't want to record an index value twice as we want to avoid the
23358 duplication.
23359 We process all global symbols and then all static symbols
23360 (which would allow us to avoid the duplication by only having to check
23361 the last entry pushed), but a symbol could have multiple kinds in one CU.
23362 To keep things simple we don't worry about the duplication here and
23363 sort and uniqufy the list after we've processed all symbols. */
23364 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23365}
23366
23367/* qsort helper routine for uniquify_cu_indices. */
23368
23369static int
23370offset_type_compare (const void *ap, const void *bp)
23371{
23372 offset_type a = *(offset_type *) ap;
23373 offset_type b = *(offset_type *) bp;
23374
23375 return (a > b) - (b > a);
23376}
23377
23378/* Sort and remove duplicates of all symbols' cu_indices lists. */
23379
23380static void
23381uniquify_cu_indices (struct mapped_symtab *symtab)
23382{
23383 int i;
23384
23385 for (i = 0; i < symtab->size; ++i)
23386 {
23387 struct symtab_index_entry *entry = symtab->data[i];
23388
23389 if (entry
23390 && entry->cu_indices != NULL)
23391 {
23392 unsigned int next_to_insert, next_to_check;
23393 offset_type last_value;
23394
23395 qsort (VEC_address (offset_type, entry->cu_indices),
23396 VEC_length (offset_type, entry->cu_indices),
23397 sizeof (offset_type), offset_type_compare);
23398
23399 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23400 next_to_insert = 1;
23401 for (next_to_check = 1;
23402 next_to_check < VEC_length (offset_type, entry->cu_indices);
23403 ++next_to_check)
23404 {
23405 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23406 != last_value)
23407 {
23408 last_value = VEC_index (offset_type, entry->cu_indices,
23409 next_to_check);
23410 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23411 last_value);
23412 ++next_to_insert;
23413 }
23414 }
23415 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23416 }
23417 }
9291a0cd
TT
23418}
23419
23420/* Add a vector of indices to the constant pool. */
b89be57b 23421
9291a0cd 23422static offset_type
3876f04e 23423add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
23424 struct symtab_index_entry *entry)
23425{
23426 void **slot;
23427
3876f04e 23428 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
23429 if (!*slot)
23430 {
23431 offset_type len = VEC_length (offset_type, entry->cu_indices);
23432 offset_type val = MAYBE_SWAP (len);
23433 offset_type iter;
23434 int i;
23435
23436 *slot = entry;
23437 entry->index_offset = obstack_object_size (cpool);
23438
23439 obstack_grow (cpool, &val, sizeof (val));
23440 for (i = 0;
23441 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23442 ++i)
23443 {
23444 val = MAYBE_SWAP (iter);
23445 obstack_grow (cpool, &val, sizeof (val));
23446 }
23447 }
23448 else
23449 {
9a3c8263
SM
23450 struct symtab_index_entry *old_entry
23451 = (struct symtab_index_entry *) *slot;
9291a0cd
TT
23452 entry->index_offset = old_entry->index_offset;
23453 entry = old_entry;
23454 }
23455 return entry->index_offset;
23456}
23457
23458/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23459 constant pool entries going into the obstack CPOOL. */
b89be57b 23460
9291a0cd
TT
23461static void
23462write_hash_table (struct mapped_symtab *symtab,
23463 struct obstack *output, struct obstack *cpool)
23464{
23465 offset_type i;
3876f04e 23466 htab_t symbol_hash_table;
9291a0cd
TT
23467 htab_t str_table;
23468
3876f04e 23469 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 23470 str_table = create_strtab ();
3876f04e 23471
9291a0cd
TT
23472 /* We add all the index vectors to the constant pool first, to
23473 ensure alignment is ok. */
23474 for (i = 0; i < symtab->size; ++i)
23475 {
23476 if (symtab->data[i])
3876f04e 23477 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
23478 }
23479
23480 /* Now write out the hash table. */
23481 for (i = 0; i < symtab->size; ++i)
23482 {
23483 offset_type str_off, vec_off;
23484
23485 if (symtab->data[i])
23486 {
23487 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23488 vec_off = symtab->data[i]->index_offset;
23489 }
23490 else
23491 {
23492 /* While 0 is a valid constant pool index, it is not valid
23493 to have 0 for both offsets. */
23494 str_off = 0;
23495 vec_off = 0;
23496 }
23497
23498 str_off = MAYBE_SWAP (str_off);
23499 vec_off = MAYBE_SWAP (vec_off);
23500
23501 obstack_grow (output, &str_off, sizeof (str_off));
23502 obstack_grow (output, &vec_off, sizeof (vec_off));
23503 }
23504
23505 htab_delete (str_table);
3876f04e 23506 htab_delete (symbol_hash_table);
9291a0cd
TT
23507}
23508
0a5429f6
DE
23509/* Struct to map psymtab to CU index in the index file. */
23510struct psymtab_cu_index_map
23511{
23512 struct partial_symtab *psymtab;
23513 unsigned int cu_index;
23514};
23515
23516static hashval_t
23517hash_psymtab_cu_index (const void *item)
23518{
9a3c8263
SM
23519 const struct psymtab_cu_index_map *map
23520 = (const struct psymtab_cu_index_map *) item;
0a5429f6
DE
23521
23522 return htab_hash_pointer (map->psymtab);
23523}
23524
23525static int
23526eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23527{
9a3c8263
SM
23528 const struct psymtab_cu_index_map *lhs
23529 = (const struct psymtab_cu_index_map *) item_lhs;
23530 const struct psymtab_cu_index_map *rhs
23531 = (const struct psymtab_cu_index_map *) item_rhs;
0a5429f6
DE
23532
23533 return lhs->psymtab == rhs->psymtab;
23534}
23535
23536/* Helper struct for building the address table. */
23537struct addrmap_index_data
23538{
23539 struct objfile *objfile;
23540 struct obstack *addr_obstack;
23541 htab_t cu_index_htab;
23542
23543 /* Non-zero if the previous_* fields are valid.
23544 We can't write an entry until we see the next entry (since it is only then
23545 that we know the end of the entry). */
23546 int previous_valid;
23547 /* Index of the CU in the table of all CUs in the index file. */
23548 unsigned int previous_cu_index;
0963b4bd 23549 /* Start address of the CU. */
0a5429f6
DE
23550 CORE_ADDR previous_cu_start;
23551};
23552
23553/* Write an address entry to OBSTACK. */
b89be57b 23554
9291a0cd 23555static void
0a5429f6
DE
23556add_address_entry (struct objfile *objfile, struct obstack *obstack,
23557 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23558{
0a5429f6 23559 offset_type cu_index_to_write;
948f8e3d 23560 gdb_byte addr[8];
9291a0cd
TT
23561 CORE_ADDR baseaddr;
23562
23563 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23564
0a5429f6
DE
23565 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23566 obstack_grow (obstack, addr, 8);
23567 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23568 obstack_grow (obstack, addr, 8);
23569 cu_index_to_write = MAYBE_SWAP (cu_index);
23570 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23571}
23572
23573/* Worker function for traversing an addrmap to build the address table. */
23574
23575static int
23576add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23577{
9a3c8263
SM
23578 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23579 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23580
23581 if (data->previous_valid)
23582 add_address_entry (data->objfile, data->addr_obstack,
23583 data->previous_cu_start, start_addr,
23584 data->previous_cu_index);
23585
23586 data->previous_cu_start = start_addr;
23587 if (pst != NULL)
23588 {
23589 struct psymtab_cu_index_map find_map, *map;
23590 find_map.psymtab = pst;
9a3c8263
SM
23591 map = ((struct psymtab_cu_index_map *)
23592 htab_find (data->cu_index_htab, &find_map));
0a5429f6
DE
23593 gdb_assert (map != NULL);
23594 data->previous_cu_index = map->cu_index;
23595 data->previous_valid = 1;
23596 }
23597 else
23598 data->previous_valid = 0;
23599
23600 return 0;
23601}
23602
23603/* Write OBJFILE's address map to OBSTACK.
23604 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23605 in the index file. */
23606
23607static void
23608write_address_map (struct objfile *objfile, struct obstack *obstack,
23609 htab_t cu_index_htab)
23610{
23611 struct addrmap_index_data addrmap_index_data;
23612
23613 /* When writing the address table, we have to cope with the fact that
23614 the addrmap iterator only provides the start of a region; we have to
23615 wait until the next invocation to get the start of the next region. */
23616
23617 addrmap_index_data.objfile = objfile;
23618 addrmap_index_data.addr_obstack = obstack;
23619 addrmap_index_data.cu_index_htab = cu_index_htab;
23620 addrmap_index_data.previous_valid = 0;
23621
23622 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23623 &addrmap_index_data);
23624
23625 /* It's highly unlikely the last entry (end address = 0xff...ff)
23626 is valid, but we should still handle it.
23627 The end address is recorded as the start of the next region, but that
23628 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23629 anyway. */
23630 if (addrmap_index_data.previous_valid)
23631 add_address_entry (objfile, obstack,
23632 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23633 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23634}
23635
156942c7
DE
23636/* Return the symbol kind of PSYM. */
23637
23638static gdb_index_symbol_kind
23639symbol_kind (struct partial_symbol *psym)
23640{
23641 domain_enum domain = PSYMBOL_DOMAIN (psym);
23642 enum address_class aclass = PSYMBOL_CLASS (psym);
23643
23644 switch (domain)
23645 {
23646 case VAR_DOMAIN:
23647 switch (aclass)
23648 {
23649 case LOC_BLOCK:
23650 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23651 case LOC_TYPEDEF:
23652 return GDB_INDEX_SYMBOL_KIND_TYPE;
23653 case LOC_COMPUTED:
23654 case LOC_CONST_BYTES:
23655 case LOC_OPTIMIZED_OUT:
23656 case LOC_STATIC:
23657 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23658 case LOC_CONST:
23659 /* Note: It's currently impossible to recognize psyms as enum values
23660 short of reading the type info. For now punt. */
23661 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23662 default:
23663 /* There are other LOC_FOO values that one might want to classify
23664 as variables, but dwarf2read.c doesn't currently use them. */
23665 return GDB_INDEX_SYMBOL_KIND_OTHER;
23666 }
23667 case STRUCT_DOMAIN:
23668 return GDB_INDEX_SYMBOL_KIND_TYPE;
23669 default:
23670 return GDB_INDEX_SYMBOL_KIND_OTHER;
23671 }
23672}
23673
9291a0cd 23674/* Add a list of partial symbols to SYMTAB. */
b89be57b 23675
9291a0cd
TT
23676static void
23677write_psymbols (struct mapped_symtab *symtab,
987d643c 23678 htab_t psyms_seen,
9291a0cd
TT
23679 struct partial_symbol **psymp,
23680 int count,
987d643c
TT
23681 offset_type cu_index,
23682 int is_static)
9291a0cd
TT
23683{
23684 for (; count-- > 0; ++psymp)
23685 {
156942c7
DE
23686 struct partial_symbol *psym = *psymp;
23687 void **slot;
987d643c 23688
156942c7 23689 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23690 error (_("Ada is not currently supported by the index"));
987d643c 23691
987d643c 23692 /* Only add a given psymbol once. */
156942c7 23693 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23694 if (!*slot)
23695 {
156942c7
DE
23696 gdb_index_symbol_kind kind = symbol_kind (psym);
23697
23698 *slot = psym;
23699 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23700 is_static, kind, cu_index);
987d643c 23701 }
9291a0cd
TT
23702 }
23703}
23704
23705/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23706 exception if there is an error. */
b89be57b 23707
9291a0cd
TT
23708static void
23709write_obstack (FILE *file, struct obstack *obstack)
23710{
23711 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23712 file)
23713 != obstack_object_size (obstack))
23714 error (_("couldn't data write to file"));
23715}
23716
1fd400ff
TT
23717/* A helper struct used when iterating over debug_types. */
23718struct signatured_type_index_data
23719{
23720 struct objfile *objfile;
23721 struct mapped_symtab *symtab;
23722 struct obstack *types_list;
987d643c 23723 htab_t psyms_seen;
1fd400ff
TT
23724 int cu_index;
23725};
23726
23727/* A helper function that writes a single signatured_type to an
23728 obstack. */
b89be57b 23729
1fd400ff
TT
23730static int
23731write_one_signatured_type (void **slot, void *d)
23732{
9a3c8263
SM
23733 struct signatured_type_index_data *info
23734 = (struct signatured_type_index_data *) d;
1fd400ff 23735 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23736 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23737 gdb_byte val[8];
23738
23739 write_psymbols (info->symtab,
987d643c 23740 info->psyms_seen,
3e43a32a
MS
23741 info->objfile->global_psymbols.list
23742 + psymtab->globals_offset,
987d643c
TT
23743 psymtab->n_global_syms, info->cu_index,
23744 0);
1fd400ff 23745 write_psymbols (info->symtab,
987d643c 23746 info->psyms_seen,
3e43a32a
MS
23747 info->objfile->static_psymbols.list
23748 + psymtab->statics_offset,
987d643c
TT
23749 psymtab->n_static_syms, info->cu_index,
23750 1);
1fd400ff 23751
b64f50a1
JK
23752 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23753 entry->per_cu.offset.sect_off);
1fd400ff 23754 obstack_grow (info->types_list, val, 8);
3019eac3
DE
23755 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23756 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
23757 obstack_grow (info->types_list, val, 8);
23758 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23759 obstack_grow (info->types_list, val, 8);
23760
23761 ++info->cu_index;
23762
23763 return 1;
23764}
23765
95554aad
TT
23766/* Recurse into all "included" dependencies and write their symbols as
23767 if they appeared in this psymtab. */
23768
23769static void
23770recursively_write_psymbols (struct objfile *objfile,
23771 struct partial_symtab *psymtab,
23772 struct mapped_symtab *symtab,
23773 htab_t psyms_seen,
23774 offset_type cu_index)
23775{
23776 int i;
23777
23778 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23779 if (psymtab->dependencies[i]->user != NULL)
23780 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23781 symtab, psyms_seen, cu_index);
23782
23783 write_psymbols (symtab,
23784 psyms_seen,
23785 objfile->global_psymbols.list + psymtab->globals_offset,
23786 psymtab->n_global_syms, cu_index,
23787 0);
23788 write_psymbols (symtab,
23789 psyms_seen,
23790 objfile->static_psymbols.list + psymtab->statics_offset,
23791 psymtab->n_static_syms, cu_index,
23792 1);
23793}
23794
9291a0cd 23795/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23796
9291a0cd
TT
23797static void
23798write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23799{
23800 struct cleanup *cleanup;
bef155c3 23801 char *filename;
1fd400ff
TT
23802 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23803 struct obstack cu_list, types_cu_list;
9291a0cd
TT
23804 int i;
23805 FILE *out_file;
23806 struct mapped_symtab *symtab;
23807 offset_type val, size_of_contents, total_len;
23808 struct stat st;
0a5429f6 23809 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23810
9291a0cd
TT
23811 if (dwarf2_per_objfile->using_index)
23812 error (_("Cannot use an index to create the index"));
23813
8b70b953
TT
23814 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23815 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23816
260b681b
DE
23817 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23818 return;
23819
4262abfb
JK
23820 if (stat (objfile_name (objfile), &st) < 0)
23821 perror_with_name (objfile_name (objfile));
9291a0cd 23822
4262abfb 23823 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23824 INDEX_SUFFIX, (char *) NULL);
23825 cleanup = make_cleanup (xfree, filename);
23826
614c279d 23827 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23828 if (!out_file)
23829 error (_("Can't open `%s' for writing"), filename);
23830
bef155c3 23831 gdb::unlinker unlink_file (filename);
9291a0cd
TT
23832
23833 symtab = create_mapped_symtab ();
23834 make_cleanup (cleanup_mapped_symtab, symtab);
23835
23836 obstack_init (&addr_obstack);
23837 make_cleanup_obstack_free (&addr_obstack);
23838
23839 obstack_init (&cu_list);
23840 make_cleanup_obstack_free (&cu_list);
23841
1fd400ff
TT
23842 obstack_init (&types_cu_list);
23843 make_cleanup_obstack_free (&types_cu_list);
23844
fc4007c9
TT
23845 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23846 htab_eq_pointer,
23847 NULL, xcalloc, xfree));
987d643c 23848
0a5429f6
DE
23849 /* While we're scanning CU's create a table that maps a psymtab pointer
23850 (which is what addrmap records) to its index (which is what is recorded
23851 in the index file). This will later be needed to write the address
23852 table. */
fc4007c9
TT
23853 htab_up cu_index_htab (htab_create_alloc (100,
23854 hash_psymtab_cu_index,
23855 eq_psymtab_cu_index,
23856 NULL, xcalloc, xfree));
8d749320
SM
23857 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23858 dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23859 make_cleanup (xfree, psymtab_cu_index_map);
23860
23861 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23862 work here. Also, the debug_types entries do not appear in
23863 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23864 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23865 {
3e43a32a
MS
23866 struct dwarf2_per_cu_data *per_cu
23867 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23868 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23869 gdb_byte val[8];
0a5429f6
DE
23870 struct psymtab_cu_index_map *map;
23871 void **slot;
9291a0cd 23872
92fac807
JK
23873 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23874 It may be referenced from a local scope but in such case it does not
23875 need to be present in .gdb_index. */
23876 if (psymtab == NULL)
23877 continue;
23878
95554aad 23879 if (psymtab->user == NULL)
fc4007c9
TT
23880 recursively_write_psymbols (objfile, psymtab, symtab,
23881 psyms_seen.get (), i);
9291a0cd 23882
0a5429f6
DE
23883 map = &psymtab_cu_index_map[i];
23884 map->psymtab = psymtab;
23885 map->cu_index = i;
fc4007c9 23886 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
0a5429f6
DE
23887 gdb_assert (slot != NULL);
23888 gdb_assert (*slot == NULL);
23889 *slot = map;
9291a0cd 23890
b64f50a1
JK
23891 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23892 per_cu->offset.sect_off);
9291a0cd 23893 obstack_grow (&cu_list, val, 8);
e254ef6a 23894 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23895 obstack_grow (&cu_list, val, 8);
23896 }
23897
0a5429f6 23898 /* Dump the address map. */
fc4007c9 23899 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
0a5429f6 23900
1fd400ff
TT
23901 /* Write out the .debug_type entries, if any. */
23902 if (dwarf2_per_objfile->signatured_types)
23903 {
23904 struct signatured_type_index_data sig_data;
23905
23906 sig_data.objfile = objfile;
23907 sig_data.symtab = symtab;
23908 sig_data.types_list = &types_cu_list;
fc4007c9 23909 sig_data.psyms_seen = psyms_seen.get ();
1fd400ff
TT
23910 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23911 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23912 write_one_signatured_type, &sig_data);
23913 }
23914
156942c7
DE
23915 /* Now that we've processed all symbols we can shrink their cu_indices
23916 lists. */
23917 uniquify_cu_indices (symtab);
23918
9291a0cd
TT
23919 obstack_init (&constant_pool);
23920 make_cleanup_obstack_free (&constant_pool);
23921 obstack_init (&symtab_obstack);
23922 make_cleanup_obstack_free (&symtab_obstack);
23923 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23924
23925 obstack_init (&contents);
23926 make_cleanup_obstack_free (&contents);
1fd400ff 23927 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23928 total_len = size_of_contents;
23929
23930 /* The version number. */
796a7ff8 23931 val = MAYBE_SWAP (8);
9291a0cd
TT
23932 obstack_grow (&contents, &val, sizeof (val));
23933
23934 /* The offset of the CU list from the start of the file. */
23935 val = MAYBE_SWAP (total_len);
23936 obstack_grow (&contents, &val, sizeof (val));
23937 total_len += obstack_object_size (&cu_list);
23938
1fd400ff
TT
23939 /* The offset of the types CU list from the start of the file. */
23940 val = MAYBE_SWAP (total_len);
23941 obstack_grow (&contents, &val, sizeof (val));
23942 total_len += obstack_object_size (&types_cu_list);
23943
9291a0cd
TT
23944 /* The offset of the address table from the start of the file. */
23945 val = MAYBE_SWAP (total_len);
23946 obstack_grow (&contents, &val, sizeof (val));
23947 total_len += obstack_object_size (&addr_obstack);
23948
23949 /* The offset of the symbol table from the start of the file. */
23950 val = MAYBE_SWAP (total_len);
23951 obstack_grow (&contents, &val, sizeof (val));
23952 total_len += obstack_object_size (&symtab_obstack);
23953
23954 /* The offset of the constant pool from the start of the file. */
23955 val = MAYBE_SWAP (total_len);
23956 obstack_grow (&contents, &val, sizeof (val));
23957 total_len += obstack_object_size (&constant_pool);
23958
23959 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23960
23961 write_obstack (out_file, &contents);
23962 write_obstack (out_file, &cu_list);
1fd400ff 23963 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
23964 write_obstack (out_file, &addr_obstack);
23965 write_obstack (out_file, &symtab_obstack);
23966 write_obstack (out_file, &constant_pool);
23967
23968 fclose (out_file);
23969
bef155c3
TT
23970 /* We want to keep the file. */
23971 unlink_file.keep ();
9291a0cd
TT
23972
23973 do_cleanups (cleanup);
23974}
23975
90476074
TT
23976/* Implementation of the `save gdb-index' command.
23977
23978 Note that the file format used by this command is documented in the
23979 GDB manual. Any changes here must be documented there. */
11570e71 23980
9291a0cd
TT
23981static void
23982save_gdb_index_command (char *arg, int from_tty)
23983{
23984 struct objfile *objfile;
23985
23986 if (!arg || !*arg)
96d19272 23987 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23988
23989 ALL_OBJFILES (objfile)
23990 {
23991 struct stat st;
23992
23993 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23994 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23995 continue;
23996
9a3c8263
SM
23997 dwarf2_per_objfile
23998 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23999 dwarf2_objfile_data_key);
9291a0cd
TT
24000 if (dwarf2_per_objfile)
24001 {
9291a0cd 24002
492d29ea 24003 TRY
9291a0cd
TT
24004 {
24005 write_psymtabs_to_index (objfile, arg);
24006 }
492d29ea
PA
24007 CATCH (except, RETURN_MASK_ERROR)
24008 {
24009 exception_fprintf (gdb_stderr, except,
24010 _("Error while writing index for `%s': "),
24011 objfile_name (objfile));
24012 }
24013 END_CATCH
9291a0cd
TT
24014 }
24015 }
dce234bc
PP
24016}
24017
9291a0cd
TT
24018\f
24019
b4f54984 24020int dwarf_always_disassemble;
9eae7c52
TT
24021
24022static void
b4f54984
DE
24023show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24024 struct cmd_list_element *c, const char *value)
9eae7c52 24025{
3e43a32a
MS
24026 fprintf_filtered (file,
24027 _("Whether to always disassemble "
24028 "DWARF expressions is %s.\n"),
9eae7c52
TT
24029 value);
24030}
24031
900e11f9
JK
24032static void
24033show_check_physname (struct ui_file *file, int from_tty,
24034 struct cmd_list_element *c, const char *value)
24035{
24036 fprintf_filtered (file,
24037 _("Whether to check \"physname\" is %s.\n"),
24038 value);
24039}
24040
6502dd73
DJ
24041void _initialize_dwarf2_read (void);
24042
24043void
24044_initialize_dwarf2_read (void)
24045{
96d19272
JK
24046 struct cmd_list_element *c;
24047
dce234bc 24048 dwarf2_objfile_data_key
c1bd65d0 24049 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24050
b4f54984
DE
24051 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24052Set DWARF specific variables.\n\
24053Configure DWARF variables such as the cache size"),
24054 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24055 0/*allow-unknown*/, &maintenance_set_cmdlist);
24056
b4f54984
DE
24057 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24058Show DWARF specific variables\n\
24059Show DWARF variables such as the cache size"),
24060 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24061 0/*allow-unknown*/, &maintenance_show_cmdlist);
24062
24063 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24064 &dwarf_max_cache_age, _("\
24065Set the upper bound on the age of cached DWARF compilation units."), _("\
24066Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24067A higher limit means that cached compilation units will be stored\n\
24068in memory longer, and more total memory will be used. Zero disables\n\
24069caching, which can slow down startup."),
2c5b56ce 24070 NULL,
b4f54984
DE
24071 show_dwarf_max_cache_age,
24072 &set_dwarf_cmdlist,
24073 &show_dwarf_cmdlist);
d97bc12b 24074
9eae7c52 24075 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24076 &dwarf_always_disassemble, _("\
9eae7c52
TT
24077Set whether `info address' always disassembles DWARF expressions."), _("\
24078Show whether `info address' always disassembles DWARF expressions."), _("\
24079When enabled, DWARF expressions are always printed in an assembly-like\n\
24080syntax. When disabled, expressions will be printed in a more\n\
24081conversational style, when possible."),
24082 NULL,
b4f54984
DE
24083 show_dwarf_always_disassemble,
24084 &set_dwarf_cmdlist,
24085 &show_dwarf_cmdlist);
24086
24087 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24088Set debugging of the DWARF reader."), _("\
24089Show debugging of the DWARF reader."), _("\
24090When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24091reading and symtab expansion. A value of 1 (one) provides basic\n\
24092information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24093 NULL,
24094 NULL,
24095 &setdebuglist, &showdebuglist);
24096
b4f54984
DE
24097 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24098Set debugging of the DWARF DIE reader."), _("\
24099Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24100When enabled (non-zero), DIEs are dumped after they are read in.\n\
24101The value is the maximum depth to print."),
ccce17b0
YQ
24102 NULL,
24103 NULL,
24104 &setdebuglist, &showdebuglist);
9291a0cd 24105
27e0867f
DE
24106 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24107Set debugging of the dwarf line reader."), _("\
24108Show debugging of the dwarf line reader."), _("\
24109When enabled (non-zero), line number entries are dumped as they are read in.\n\
24110A value of 1 (one) provides basic information.\n\
24111A value greater than 1 provides more verbose information."),
24112 NULL,
24113 NULL,
24114 &setdebuglist, &showdebuglist);
24115
900e11f9
JK
24116 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24117Set cross-checking of \"physname\" code against demangler."), _("\
24118Show cross-checking of \"physname\" code against demangler."), _("\
24119When enabled, GDB's internal \"physname\" code is checked against\n\
24120the demangler."),
24121 NULL, show_check_physname,
24122 &setdebuglist, &showdebuglist);
24123
e615022a
DE
24124 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24125 no_class, &use_deprecated_index_sections, _("\
24126Set whether to use deprecated gdb_index sections."), _("\
24127Show whether to use deprecated gdb_index sections."), _("\
24128When enabled, deprecated .gdb_index sections are used anyway.\n\
24129Normally they are ignored either because of a missing feature or\n\
24130performance issue.\n\
24131Warning: This option must be enabled before gdb reads the file."),
24132 NULL,
24133 NULL,
24134 &setlist, &showlist);
24135
96d19272 24136 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24137 _("\
fc1a9d6e 24138Save a gdb-index file.\n\
11570e71 24139Usage: save gdb-index DIRECTORY"),
96d19272
JK
24140 &save_cmdlist);
24141 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24142
24143 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24144 &dwarf2_locexpr_funcs);
24145 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24146 &dwarf2_loclist_funcs);
24147
24148 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24149 &dwarf2_block_frame_base_locexpr_funcs);
24150 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24151 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24152}
This page took 3.770042 seconds and 4 git commands to generate.