DWARF-5: Macros
[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"
4c2df51b 73
c906108c 74#include <fcntl.h>
c906108c 75#include <sys/types.h>
325fac50 76#include <algorithm>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
900e11f9
JK
92/* When non-zero, cross-check physname against demangler. */
93static int check_physname = 0;
94
481860b3 95/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 96static int use_deprecated_index_sections = 0;
481860b3 97
6502dd73
DJ
98static const struct objfile_data *dwarf2_objfile_data_key;
99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
73869dc2
DE
107/* A descriptor for dwarf sections.
108
109 S.ASECTION, SIZE are typically initialized when the objfile is first
110 scanned. BUFFER, READIN are filled in later when the section is read.
111 If the section contained compressed data then SIZE is updated to record
112 the uncompressed size of the section.
113
114 DWP file format V2 introduces a wrinkle that is easiest to handle by
115 creating the concept of virtual sections contained within a real section.
116 In DWP V2 the sections of the input DWO files are concatenated together
117 into one section, but section offsets are kept relative to the original
118 input section.
119 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
120 the real section this "virtual" section is contained in, and BUFFER,SIZE
121 describe the virtual section. */
122
dce234bc
PP
123struct dwarf2_section_info
124{
73869dc2
DE
125 union
126 {
e5aa3347 127 /* If this is a real section, the bfd section. */
049412e3 128 asection *section;
73869dc2 129 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 130 section. */
73869dc2
DE
131 struct dwarf2_section_info *containing_section;
132 } s;
19ac8c2e 133 /* Pointer to section data, only valid if readin. */
d521ce57 134 const gdb_byte *buffer;
73869dc2 135 /* The size of the section, real or virtual. */
dce234bc 136 bfd_size_type size;
73869dc2
DE
137 /* If this is a virtual section, the offset in the real section.
138 Only valid if is_virtual. */
139 bfd_size_type virtual_offset;
be391dca 140 /* True if we have tried to read this section. */
73869dc2
DE
141 char readin;
142 /* True if this is a virtual section, False otherwise.
049412e3 143 This specifies which of s.section and s.containing_section to use. */
73869dc2 144 char is_virtual;
dce234bc
PP
145};
146
8b70b953
TT
147typedef struct dwarf2_section_info dwarf2_section_info_def;
148DEF_VEC_O (dwarf2_section_info_def);
149
9291a0cd
TT
150/* All offsets in the index are of this type. It must be
151 architecture-independent. */
152typedef uint32_t offset_type;
153
154DEF_VEC_I (offset_type);
155
156942c7
DE
156/* Ensure only legit values are used. */
157#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
158 do { \
159 gdb_assert ((unsigned int) (value) <= 1); \
160 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
161 } while (0)
162
163/* Ensure only legit values are used. */
164#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
165 do { \
166 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
167 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
168 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
169 } while (0)
170
171/* Ensure we don't use more than the alloted nuber of bits for the CU. */
172#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
173 do { \
174 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
175 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
176 } while (0)
177
9291a0cd
TT
178/* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180struct mapped_index
181{
559a7a62
JK
182 /* Index data format version. */
183 int version;
184
9291a0cd
TT
185 /* The total length of the buffer. */
186 off_t total_size;
b11b1f88 187
9291a0cd
TT
188 /* A pointer to the address table data. */
189 const gdb_byte *address_table;
b11b1f88 190
9291a0cd
TT
191 /* Size of the address table data in bytes. */
192 offset_type address_table_size;
b11b1f88 193
3876f04e
DE
194 /* The symbol table, implemented as a hash table. */
195 const offset_type *symbol_table;
b11b1f88 196
9291a0cd 197 /* Size in slots, each slot is 2 offset_types. */
3876f04e 198 offset_type symbol_table_slots;
b11b1f88 199
9291a0cd
TT
200 /* A pointer to the constant pool. */
201 const char *constant_pool;
202};
203
95554aad
TT
204typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
205DEF_VEC_P (dwarf2_per_cu_ptr);
206
52059ffd
TT
207struct tu_stats
208{
209 int nr_uniq_abbrev_tables;
210 int nr_symtabs;
211 int nr_symtab_sharers;
212 int nr_stmt_less_type_units;
213 int nr_all_type_units_reallocs;
214};
215
9cdd5dbd
DE
216/* Collection of data recorded per objfile.
217 This hangs off of dwarf2_objfile_data_key. */
218
6502dd73
DJ
219struct dwarf2_per_objfile
220{
dce234bc
PP
221 struct dwarf2_section_info info;
222 struct dwarf2_section_info abbrev;
223 struct dwarf2_section_info line;
dce234bc 224 struct dwarf2_section_info loc;
43988095 225 struct dwarf2_section_info loclists;
dce234bc 226 struct dwarf2_section_info macinfo;
cf2c3c16 227 struct dwarf2_section_info macro;
dce234bc 228 struct dwarf2_section_info str;
43988095 229 struct dwarf2_section_info line_str;
dce234bc 230 struct dwarf2_section_info ranges;
43988095 231 struct dwarf2_section_info rnglists;
3019eac3 232 struct dwarf2_section_info addr;
dce234bc
PP
233 struct dwarf2_section_info frame;
234 struct dwarf2_section_info eh_frame;
9291a0cd 235 struct dwarf2_section_info gdb_index;
ae038cb0 236
8b70b953
TT
237 VEC (dwarf2_section_info_def) *types;
238
be391dca
TT
239 /* Back link. */
240 struct objfile *objfile;
241
d467dd73 242 /* Table of all the compilation units. This is used to locate
10b3939b 243 the target compilation unit of a particular reference. */
ae038cb0
DJ
244 struct dwarf2_per_cu_data **all_comp_units;
245
246 /* The number of compilation units in ALL_COMP_UNITS. */
247 int n_comp_units;
248
1fd400ff 249 /* The number of .debug_types-related CUs. */
d467dd73 250 int n_type_units;
1fd400ff 251
6aa5f3a6
DE
252 /* The number of elements allocated in all_type_units.
253 If there are skeleton-less TUs, we add them to all_type_units lazily. */
254 int n_allocated_type_units;
255
a2ce51a0
DE
256 /* The .debug_types-related CUs (TUs).
257 This is stored in malloc space because we may realloc it. */
b4dd5633 258 struct signatured_type **all_type_units;
1fd400ff 259
f4dc4d17
DE
260 /* Table of struct type_unit_group objects.
261 The hash key is the DW_AT_stmt_list value. */
262 htab_t type_unit_groups;
72dca2f5 263
348e048f
DE
264 /* A table mapping .debug_types signatures to its signatured_type entry.
265 This is NULL if the .debug_types section hasn't been read in yet. */
266 htab_t signatured_types;
267
f4dc4d17
DE
268 /* Type unit statistics, to see how well the scaling improvements
269 are doing. */
52059ffd 270 struct tu_stats tu_stats;
f4dc4d17
DE
271
272 /* A chain of compilation units that are currently read in, so that
273 they can be freed later. */
274 struct dwarf2_per_cu_data *read_in_chain;
275
3019eac3
DE
276 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
277 This is NULL if the table hasn't been allocated yet. */
278 htab_t dwo_files;
279
80626a55
DE
280 /* Non-zero if we've check for whether there is a DWP file. */
281 int dwp_checked;
282
283 /* The DWP file if there is one, or NULL. */
284 struct dwp_file *dwp_file;
285
36586728
TT
286 /* The shared '.dwz' file, if one exists. This is used when the
287 original data was compressed using 'dwz -m'. */
288 struct dwz_file *dwz_file;
289
72dca2f5
FR
290 /* A flag indicating wether this objfile has a section loaded at a
291 VMA of 0. */
292 int has_section_at_zero;
9291a0cd 293
ae2de4f8
DE
294 /* True if we are using the mapped index,
295 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
296 unsigned char using_index;
297
ae2de4f8 298 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 299 struct mapped_index *index_table;
98bfdba5 300
7b9f3c50 301 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
302 TUs typically share line table entries with a CU, so we maintain a
303 separate table of all line table entries to support the sharing.
304 Note that while there can be way more TUs than CUs, we've already
305 sorted all the TUs into "type unit groups", grouped by their
306 DW_AT_stmt_list value. Therefore the only sharing done here is with a
307 CU and its associated TU group if there is one. */
7b9f3c50
DE
308 htab_t quick_file_names_table;
309
98bfdba5
PA
310 /* Set during partial symbol reading, to prevent queueing of full
311 symbols. */
312 int reading_partial_symbols;
673bfd45 313
dee91e82 314 /* Table mapping type DIEs to their struct type *.
673bfd45 315 This is NULL if not allocated yet.
02142a6c 316 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 317 htab_t die_type_hash;
95554aad
TT
318
319 /* The CUs we recently read. */
320 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
321
322 /* Table containing line_header indexed by offset and offset_in_dwz. */
323 htab_t line_header_hash;
6502dd73
DJ
324};
325
326static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 327
251d32d9 328/* Default names of the debugging sections. */
c906108c 329
233a11ab
CS
330/* Note that if the debugging section has been compressed, it might
331 have a name like .zdebug_info. */
332
9cdd5dbd
DE
333static const struct dwarf2_debug_sections dwarf2_elf_names =
334{
251d32d9
TG
335 { ".debug_info", ".zdebug_info" },
336 { ".debug_abbrev", ".zdebug_abbrev" },
337 { ".debug_line", ".zdebug_line" },
338 { ".debug_loc", ".zdebug_loc" },
43988095 339 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 340 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 341 { ".debug_macro", ".zdebug_macro" },
251d32d9 342 { ".debug_str", ".zdebug_str" },
43988095 343 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 344 { ".debug_ranges", ".zdebug_ranges" },
43988095 345 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 346 { ".debug_types", ".zdebug_types" },
3019eac3 347 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
348 { ".debug_frame", ".zdebug_frame" },
349 { ".eh_frame", NULL },
24d3216f
TT
350 { ".gdb_index", ".zgdb_index" },
351 23
251d32d9 352};
c906108c 353
80626a55 354/* List of DWO/DWP sections. */
3019eac3 355
80626a55 356static const struct dwop_section_names
3019eac3
DE
357{
358 struct dwarf2_section_names abbrev_dwo;
359 struct dwarf2_section_names info_dwo;
360 struct dwarf2_section_names line_dwo;
361 struct dwarf2_section_names loc_dwo;
43988095 362 struct dwarf2_section_names loclists_dwo;
09262596
DE
363 struct dwarf2_section_names macinfo_dwo;
364 struct dwarf2_section_names macro_dwo;
3019eac3
DE
365 struct dwarf2_section_names str_dwo;
366 struct dwarf2_section_names str_offsets_dwo;
367 struct dwarf2_section_names types_dwo;
80626a55
DE
368 struct dwarf2_section_names cu_index;
369 struct dwarf2_section_names tu_index;
3019eac3 370}
80626a55 371dwop_section_names =
3019eac3
DE
372{
373 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
374 { ".debug_info.dwo", ".zdebug_info.dwo" },
375 { ".debug_line.dwo", ".zdebug_line.dwo" },
376 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 377 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
378 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
379 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
380 { ".debug_str.dwo", ".zdebug_str.dwo" },
381 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
382 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
383 { ".debug_cu_index", ".zdebug_cu_index" },
384 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
385};
386
c906108c
SS
387/* local data types */
388
107d2387
AC
389/* The data in a compilation unit header, after target2host
390 translation, looks like this. */
c906108c 391struct comp_unit_head
a738430d 392{
c764a876 393 unsigned int length;
a738430d 394 short version;
a738430d
MK
395 unsigned char addr_size;
396 unsigned char signed_addr_p;
b64f50a1 397 sect_offset abbrev_offset;
57349743 398
a738430d
MK
399 /* Size of file offsets; either 4 or 8. */
400 unsigned int offset_size;
57349743 401
a738430d
MK
402 /* Size of the length field; either 4 or 12. */
403 unsigned int initial_length_size;
57349743 404
43988095
JK
405 enum dwarf_unit_type unit_type;
406
a738430d
MK
407 /* Offset to the first byte of this compilation unit header in the
408 .debug_info section, for resolving relative reference dies. */
b64f50a1 409 sect_offset offset;
57349743 410
d00adf39
DE
411 /* Offset to first die in this cu from the start of the cu.
412 This will be the first byte following the compilation unit header. */
b64f50a1 413 cu_offset first_die_offset;
43988095
JK
414
415 /* 64-bit signature of this type unit - it is valid only for
416 UNIT_TYPE DW_UT_type. */
417 ULONGEST signature;
418
419 /* For types, offset in the type's DIE of the type defined by this TU. */
420 cu_offset type_offset_in_tu;
a738430d 421};
c906108c 422
3da10d80
KS
423/* Type used for delaying computation of method physnames.
424 See comments for compute_delayed_physnames. */
425struct delayed_method_info
426{
427 /* The type to which the method is attached, i.e., its parent class. */
428 struct type *type;
429
430 /* The index of the method in the type's function fieldlists. */
431 int fnfield_index;
432
433 /* The index of the method in the fieldlist. */
434 int index;
435
436 /* The name of the DIE. */
437 const char *name;
438
439 /* The DIE associated with this method. */
440 struct die_info *die;
441};
442
443typedef struct delayed_method_info delayed_method_info;
444DEF_VEC_O (delayed_method_info);
445
e7c27a73
DJ
446/* Internal state when decoding a particular compilation unit. */
447struct dwarf2_cu
448{
449 /* The objfile containing this compilation unit. */
450 struct objfile *objfile;
451
d00adf39 452 /* The header of the compilation unit. */
e7c27a73 453 struct comp_unit_head header;
e142c38c 454
d00adf39
DE
455 /* Base address of this compilation unit. */
456 CORE_ADDR base_address;
457
458 /* Non-zero if base_address has been set. */
459 int base_known;
460
e142c38c
DJ
461 /* The language we are debugging. */
462 enum language language;
463 const struct language_defn *language_defn;
464
b0f35d58
DL
465 const char *producer;
466
e142c38c
DJ
467 /* The generic symbol table building routines have separate lists for
468 file scope symbols and all all other scopes (local scopes). So
469 we need to select the right one to pass to add_symbol_to_list().
470 We do it by keeping a pointer to the correct list in list_in_scope.
471
472 FIXME: The original dwarf code just treated the file scope as the
473 first local scope, and all other local scopes as nested local
474 scopes, and worked fine. Check to see if we really need to
475 distinguish these in buildsym.c. */
476 struct pending **list_in_scope;
477
433df2d4
DE
478 /* The abbrev table for this CU.
479 Normally this points to the abbrev table in the objfile.
480 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
481 struct abbrev_table *abbrev_table;
72bf9492 482
b64f50a1
JK
483 /* Hash table holding all the loaded partial DIEs
484 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
485 htab_t partial_dies;
486
487 /* Storage for things with the same lifetime as this read-in compilation
488 unit, including partial DIEs. */
489 struct obstack comp_unit_obstack;
490
ae038cb0
DJ
491 /* When multiple dwarf2_cu structures are living in memory, this field
492 chains them all together, so that they can be released efficiently.
493 We will probably also want a generation counter so that most-recently-used
494 compilation units are cached... */
495 struct dwarf2_per_cu_data *read_in_chain;
496
69d751e3 497 /* Backlink to our per_cu entry. */
ae038cb0
DJ
498 struct dwarf2_per_cu_data *per_cu;
499
500 /* How many compilation units ago was this CU last referenced? */
501 int last_used;
502
b64f50a1
JK
503 /* A hash table of DIE cu_offset for following references with
504 die_info->offset.sect_off as hash. */
51545339 505 htab_t die_hash;
10b3939b
DJ
506
507 /* Full DIEs if read in. */
508 struct die_info *dies;
509
510 /* A set of pointers to dwarf2_per_cu_data objects for compilation
511 units referenced by this one. Only set during full symbol processing;
512 partial symbol tables do not have dependencies. */
513 htab_t dependencies;
514
cb1df416
DJ
515 /* Header data from the line table, during full symbol processing. */
516 struct line_header *line_header;
517
3da10d80
KS
518 /* A list of methods which need to have physnames computed
519 after all type information has been read. */
520 VEC (delayed_method_info) *method_list;
521
96408a79
SA
522 /* To be copied to symtab->call_site_htab. */
523 htab_t call_site_htab;
524
034e5797
DE
525 /* Non-NULL if this CU came from a DWO file.
526 There is an invariant here that is important to remember:
527 Except for attributes copied from the top level DIE in the "main"
528 (or "stub") file in preparation for reading the DWO file
529 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
530 Either there isn't a DWO file (in which case this is NULL and the point
531 is moot), or there is and either we're not going to read it (in which
532 case this is NULL) or there is and we are reading it (in which case this
533 is non-NULL). */
3019eac3
DE
534 struct dwo_unit *dwo_unit;
535
536 /* The DW_AT_addr_base attribute if present, zero otherwise
537 (zero is a valid value though).
1dbab08b 538 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
539 ULONGEST addr_base;
540
2e3cf129
DE
541 /* The DW_AT_ranges_base attribute if present, zero otherwise
542 (zero is a valid value though).
1dbab08b 543 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 544 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
545 be used without needing to know whether DWO files are in use or not.
546 N.B. This does not apply to DW_AT_ranges appearing in
547 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
548 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
549 DW_AT_ranges_base *would* have to be applied, and we'd have to care
550 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
551 ULONGEST ranges_base;
552
ae038cb0
DJ
553 /* Mark used when releasing cached dies. */
554 unsigned int mark : 1;
555
8be455d7
JK
556 /* This CU references .debug_loc. See the symtab->locations_valid field.
557 This test is imperfect as there may exist optimized debug code not using
558 any location list and still facing inlining issues if handled as
559 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 560 unsigned int has_loclist : 1;
ba919b58 561
1b80a9fa
JK
562 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
563 if all the producer_is_* fields are valid. This information is cached
564 because profiling CU expansion showed excessive time spent in
565 producer_is_gxx_lt_4_6. */
ba919b58
TT
566 unsigned int checked_producer : 1;
567 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 568 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 569 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
570
571 /* When set, the file that we're processing is known to have
572 debugging info for C++ namespaces. GCC 3.3.x did not produce
573 this information, but later versions do. */
574
575 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
576};
577
10b3939b
DJ
578/* Persistent data held for a compilation unit, even when not
579 processing it. We put a pointer to this structure in the
28dee7f5 580 read_symtab_private field of the psymtab. */
10b3939b 581
ae038cb0
DJ
582struct dwarf2_per_cu_data
583{
36586728 584 /* The start offset and length of this compilation unit.
45452591 585 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
586 initial_length_size.
587 If the DIE refers to a DWO file, this is always of the original die,
588 not the DWO file. */
b64f50a1 589 sect_offset offset;
36586728 590 unsigned int length;
ae038cb0 591
43988095
JK
592 /* DWARF standard version this data has been read from (such as 4 or 5). */
593 short dwarf_version;
594
ae038cb0
DJ
595 /* Flag indicating this compilation unit will be read in before
596 any of the current compilation units are processed. */
c764a876 597 unsigned int queued : 1;
ae038cb0 598
0d99eb77
DE
599 /* This flag will be set when reading partial DIEs if we need to load
600 absolutely all DIEs for this compilation unit, instead of just the ones
601 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
602 hash table and don't find it. */
603 unsigned int load_all_dies : 1;
604
0186c6a7
DE
605 /* Non-zero if this CU is from .debug_types.
606 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
607 this is non-zero. */
3019eac3
DE
608 unsigned int is_debug_types : 1;
609
36586728
TT
610 /* Non-zero if this CU is from the .dwz file. */
611 unsigned int is_dwz : 1;
612
a2ce51a0
DE
613 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
614 This flag is only valid if is_debug_types is true.
615 We can't read a CU directly from a DWO file: There are required
616 attributes in the stub. */
617 unsigned int reading_dwo_directly : 1;
618
7ee85ab1
DE
619 /* Non-zero if the TU has been read.
620 This is used to assist the "Stay in DWO Optimization" for Fission:
621 When reading a DWO, it's faster to read TUs from the DWO instead of
622 fetching them from random other DWOs (due to comdat folding).
623 If the TU has already been read, the optimization is unnecessary
624 (and unwise - we don't want to change where gdb thinks the TU lives
625 "midflight").
626 This flag is only valid if is_debug_types is true. */
627 unsigned int tu_read : 1;
628
3019eac3
DE
629 /* The section this CU/TU lives in.
630 If the DIE refers to a DWO file, this is always the original die,
631 not the DWO file. */
8a0459fd 632 struct dwarf2_section_info *section;
348e048f 633
17ea53c3 634 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
635 of the CU cache it gets reset to NULL again. This is left as NULL for
636 dummy CUs (a CU header, but nothing else). */
ae038cb0 637 struct dwarf2_cu *cu;
1c379e20 638
9cdd5dbd
DE
639 /* The corresponding objfile.
640 Normally we can get the objfile from dwarf2_per_objfile.
641 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
642 struct objfile *objfile;
643
fffbe6a8
YQ
644 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
645 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
646 union
647 {
648 /* The partial symbol table associated with this compilation unit,
95554aad 649 or NULL for unread partial units. */
9291a0cd
TT
650 struct partial_symtab *psymtab;
651
652 /* Data needed by the "quick" functions. */
653 struct dwarf2_per_cu_quick_data *quick;
654 } v;
95554aad 655
796a7ff8
DE
656 /* The CUs we import using DW_TAG_imported_unit. This is filled in
657 while reading psymtabs, used to compute the psymtab dependencies,
658 and then cleared. Then it is filled in again while reading full
659 symbols, and only deleted when the objfile is destroyed.
660
661 This is also used to work around a difference between the way gold
662 generates .gdb_index version <=7 and the way gdb does. Arguably this
663 is a gold bug. For symbols coming from TUs, gold records in the index
664 the CU that includes the TU instead of the TU itself. This breaks
665 dw2_lookup_symbol: It assumes that if the index says symbol X lives
666 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
667 will find X. Alas TUs live in their own symtab, so after expanding CU Y
668 we need to look in TU Z to find X. Fortunately, this is akin to
669 DW_TAG_imported_unit, so we just use the same mechanism: For
670 .gdb_index version <=7 this also records the TUs that the CU referred
671 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
672 indices so we only pay a price for gold generated indices.
673 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 674 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
675};
676
348e048f
DE
677/* Entry in the signatured_types hash table. */
678
679struct signatured_type
680{
42e7ad6c 681 /* The "per_cu" object of this type.
ac9ec31b 682 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
683 N.B.: This is the first member so that it's easy to convert pointers
684 between them. */
685 struct dwarf2_per_cu_data per_cu;
686
3019eac3 687 /* The type's signature. */
348e048f
DE
688 ULONGEST signature;
689
3019eac3 690 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
691 If this TU is a DWO stub and the definition lives in a DWO file
692 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
693 cu_offset type_offset_in_tu;
694
695 /* Offset in the section of the type's DIE.
696 If the definition lives in a DWO file, this is the offset in the
697 .debug_types.dwo section.
698 The value is zero until the actual value is known.
699 Zero is otherwise not a valid section offset. */
700 sect_offset type_offset_in_section;
0186c6a7
DE
701
702 /* Type units are grouped by their DW_AT_stmt_list entry so that they
703 can share them. This points to the containing symtab. */
704 struct type_unit_group *type_unit_group;
ac9ec31b
DE
705
706 /* The type.
707 The first time we encounter this type we fully read it in and install it
708 in the symbol tables. Subsequent times we only need the type. */
709 struct type *type;
a2ce51a0
DE
710
711 /* Containing DWO unit.
712 This field is valid iff per_cu.reading_dwo_directly. */
713 struct dwo_unit *dwo_unit;
348e048f
DE
714};
715
0186c6a7
DE
716typedef struct signatured_type *sig_type_ptr;
717DEF_VEC_P (sig_type_ptr);
718
094b34ac
DE
719/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
720 This includes type_unit_group and quick_file_names. */
721
722struct stmt_list_hash
723{
724 /* The DWO unit this table is from or NULL if there is none. */
725 struct dwo_unit *dwo_unit;
726
727 /* Offset in .debug_line or .debug_line.dwo. */
728 sect_offset line_offset;
729};
730
f4dc4d17
DE
731/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
732 an object of this type. */
733
734struct type_unit_group
735{
0186c6a7 736 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
737 To simplify things we create an artificial CU that "includes" all the
738 type units using this stmt_list so that the rest of the code still has
739 a "per_cu" handle on the symtab.
740 This PER_CU is recognized by having no section. */
8a0459fd 741#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
742 struct dwarf2_per_cu_data per_cu;
743
0186c6a7
DE
744 /* The TUs that share this DW_AT_stmt_list entry.
745 This is added to while parsing type units to build partial symtabs,
746 and is deleted afterwards and not used again. */
747 VEC (sig_type_ptr) *tus;
f4dc4d17 748
43f3e411 749 /* The compunit symtab.
094b34ac 750 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
751 so we create an essentially anonymous symtab as the compunit symtab. */
752 struct compunit_symtab *compunit_symtab;
f4dc4d17 753
094b34ac
DE
754 /* The data used to construct the hash key. */
755 struct stmt_list_hash hash;
f4dc4d17
DE
756
757 /* The number of symtabs from the line header.
758 The value here must match line_header.num_file_names. */
759 unsigned int num_symtabs;
760
761 /* The symbol tables for this TU (obtained from the files listed in
762 DW_AT_stmt_list).
763 WARNING: The order of entries here must match the order of entries
764 in the line header. After the first TU using this type_unit_group, the
765 line header for the subsequent TUs is recreated from this. This is done
766 because we need to use the same symtabs for each TU using the same
767 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
768 there's no guarantee the line header doesn't have duplicate entries. */
769 struct symtab **symtabs;
770};
771
73869dc2 772/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
773
774struct dwo_sections
775{
776 struct dwarf2_section_info abbrev;
3019eac3
DE
777 struct dwarf2_section_info line;
778 struct dwarf2_section_info loc;
43988095 779 struct dwarf2_section_info loclists;
09262596
DE
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
3019eac3
DE
782 struct dwarf2_section_info str;
783 struct dwarf2_section_info str_offsets;
80626a55
DE
784 /* In the case of a virtual DWO file, these two are unused. */
785 struct dwarf2_section_info info;
3019eac3
DE
786 VEC (dwarf2_section_info_def) *types;
787};
788
c88ee1f0 789/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
790
791struct dwo_unit
792{
793 /* Backlink to the containing struct dwo_file. */
794 struct dwo_file *dwo_file;
795
796 /* The "id" that distinguishes this CU/TU.
797 .debug_info calls this "dwo_id", .debug_types calls this "signature".
798 Since signatures came first, we stick with it for consistency. */
799 ULONGEST signature;
800
801 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 802 struct dwarf2_section_info *section;
3019eac3 803
19ac8c2e 804 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
3019eac3
DE
805 sect_offset offset;
806 unsigned int length;
807
808 /* For types, offset in the type's DIE of the type defined by this TU. */
809 cu_offset type_offset_in_tu;
810};
811
73869dc2
DE
812/* include/dwarf2.h defines the DWP section codes.
813 It defines a max value but it doesn't define a min value, which we
814 use for error checking, so provide one. */
815
816enum dwp_v2_section_ids
817{
818 DW_SECT_MIN = 1
819};
820
80626a55 821/* Data for one DWO file.
57d63ce2
DE
822
823 This includes virtual DWO files (a virtual DWO file is a DWO file as it
824 appears in a DWP file). DWP files don't really have DWO files per se -
825 comdat folding of types "loses" the DWO file they came from, and from
826 a high level view DWP files appear to contain a mass of random types.
827 However, to maintain consistency with the non-DWP case we pretend DWP
828 files contain virtual DWO files, and we assign each TU with one virtual
829 DWO file (generally based on the line and abbrev section offsets -
830 a heuristic that seems to work in practice). */
3019eac3
DE
831
832struct dwo_file
833{
0ac5b59e 834 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
835 For virtual DWO files the name is constructed from the section offsets
836 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
837 from related CU+TUs. */
0ac5b59e
DE
838 const char *dwo_name;
839
840 /* The DW_AT_comp_dir attribute. */
841 const char *comp_dir;
3019eac3 842
80626a55
DE
843 /* The bfd, when the file is open. Otherwise this is NULL.
844 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
845 bfd *dbfd;
3019eac3 846
73869dc2
DE
847 /* The sections that make up this DWO file.
848 Remember that for virtual DWO files in DWP V2, these are virtual
849 sections (for lack of a better name). */
3019eac3
DE
850 struct dwo_sections sections;
851
19c3d4c9
DE
852 /* The CU in the file.
853 We only support one because having more than one requires hacking the
854 dwo_name of each to match, which is highly unlikely to happen.
855 Doing this means all TUs can share comp_dir: We also assume that
856 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
857 struct dwo_unit *cu;
3019eac3
DE
858
859 /* Table of TUs in the file.
860 Each element is a struct dwo_unit. */
861 htab_t tus;
862};
863
80626a55
DE
864/* These sections are what may appear in a DWP file. */
865
866struct dwp_sections
867{
73869dc2 868 /* These are used by both DWP version 1 and 2. */
80626a55
DE
869 struct dwarf2_section_info str;
870 struct dwarf2_section_info cu_index;
871 struct dwarf2_section_info tu_index;
73869dc2
DE
872
873 /* These are only used by DWP version 2 files.
874 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
875 sections are referenced by section number, and are not recorded here.
876 In DWP version 2 there is at most one copy of all these sections, each
877 section being (effectively) comprised of the concatenation of all of the
878 individual sections that exist in the version 1 format.
879 To keep the code simple we treat each of these concatenated pieces as a
880 section itself (a virtual section?). */
881 struct dwarf2_section_info abbrev;
882 struct dwarf2_section_info info;
883 struct dwarf2_section_info line;
884 struct dwarf2_section_info loc;
885 struct dwarf2_section_info macinfo;
886 struct dwarf2_section_info macro;
887 struct dwarf2_section_info str_offsets;
888 struct dwarf2_section_info types;
80626a55
DE
889};
890
73869dc2
DE
891/* These sections are what may appear in a virtual DWO file in DWP version 1.
892 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 893
73869dc2 894struct virtual_v1_dwo_sections
80626a55
DE
895{
896 struct dwarf2_section_info abbrev;
897 struct dwarf2_section_info line;
898 struct dwarf2_section_info loc;
899 struct dwarf2_section_info macinfo;
900 struct dwarf2_section_info macro;
901 struct dwarf2_section_info str_offsets;
902 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 903 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
904 struct dwarf2_section_info info_or_types;
905};
906
73869dc2
DE
907/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
908 In version 2, the sections of the DWO files are concatenated together
909 and stored in one section of that name. Thus each ELF section contains
910 several "virtual" sections. */
911
912struct virtual_v2_dwo_sections
913{
914 bfd_size_type abbrev_offset;
915 bfd_size_type abbrev_size;
916
917 bfd_size_type line_offset;
918 bfd_size_type line_size;
919
920 bfd_size_type loc_offset;
921 bfd_size_type loc_size;
922
923 bfd_size_type macinfo_offset;
924 bfd_size_type macinfo_size;
925
926 bfd_size_type macro_offset;
927 bfd_size_type macro_size;
928
929 bfd_size_type str_offsets_offset;
930 bfd_size_type str_offsets_size;
931
932 /* Each DWP hash table entry records one CU or one TU.
933 That is recorded here, and copied to dwo_unit.section. */
934 bfd_size_type info_or_types_offset;
935 bfd_size_type info_or_types_size;
936};
937
80626a55
DE
938/* Contents of DWP hash tables. */
939
940struct dwp_hash_table
941{
73869dc2 942 uint32_t version, nr_columns;
80626a55 943 uint32_t nr_units, nr_slots;
73869dc2
DE
944 const gdb_byte *hash_table, *unit_table;
945 union
946 {
947 struct
948 {
949 const gdb_byte *indices;
950 } v1;
951 struct
952 {
953 /* This is indexed by column number and gives the id of the section
954 in that column. */
955#define MAX_NR_V2_DWO_SECTIONS \
956 (1 /* .debug_info or .debug_types */ \
957 + 1 /* .debug_abbrev */ \
958 + 1 /* .debug_line */ \
959 + 1 /* .debug_loc */ \
960 + 1 /* .debug_str_offsets */ \
961 + 1 /* .debug_macro or .debug_macinfo */)
962 int section_ids[MAX_NR_V2_DWO_SECTIONS];
963 const gdb_byte *offsets;
964 const gdb_byte *sizes;
965 } v2;
966 } section_pool;
80626a55
DE
967};
968
969/* Data for one DWP file. */
970
971struct dwp_file
972{
973 /* Name of the file. */
974 const char *name;
975
73869dc2
DE
976 /* File format version. */
977 int version;
978
93417882 979 /* The bfd. */
80626a55
DE
980 bfd *dbfd;
981
982 /* Section info for this file. */
983 struct dwp_sections sections;
984
57d63ce2 985 /* Table of CUs in the file. */
80626a55
DE
986 const struct dwp_hash_table *cus;
987
988 /* Table of TUs in the file. */
989 const struct dwp_hash_table *tus;
990
19ac8c2e
DE
991 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
992 htab_t loaded_cus;
993 htab_t loaded_tus;
80626a55 994
73869dc2
DE
995 /* Table to map ELF section numbers to their sections.
996 This is only needed for the DWP V1 file format. */
80626a55
DE
997 unsigned int num_sections;
998 asection **elf_sections;
999};
1000
36586728
TT
1001/* This represents a '.dwz' file. */
1002
1003struct dwz_file
1004{
1005 /* A dwz file can only contain a few sections. */
1006 struct dwarf2_section_info abbrev;
1007 struct dwarf2_section_info info;
1008 struct dwarf2_section_info str;
1009 struct dwarf2_section_info line;
1010 struct dwarf2_section_info macro;
2ec9a5e0 1011 struct dwarf2_section_info gdb_index;
36586728
TT
1012
1013 /* The dwz's BFD. */
1014 bfd *dwz_bfd;
1015};
1016
0963b4bd
MS
1017/* Struct used to pass misc. parameters to read_die_and_children, et
1018 al. which are used for both .debug_info and .debug_types dies.
1019 All parameters here are unchanging for the life of the call. This
dee91e82 1020 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1021
1022struct die_reader_specs
1023{
a32a8923 1024 /* The bfd of die_section. */
93311388
DE
1025 bfd* abfd;
1026
1027 /* The CU of the DIE we are parsing. */
1028 struct dwarf2_cu *cu;
1029
80626a55 1030 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1031 struct dwo_file *dwo_file;
1032
dee91e82 1033 /* The section the die comes from.
3019eac3 1034 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1035 struct dwarf2_section_info *die_section;
1036
1037 /* die_section->buffer. */
d521ce57 1038 const gdb_byte *buffer;
f664829e
DE
1039
1040 /* The end of the buffer. */
1041 const gdb_byte *buffer_end;
a2ce51a0
DE
1042
1043 /* The value of the DW_AT_comp_dir attribute. */
1044 const char *comp_dir;
93311388
DE
1045};
1046
fd820528 1047/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1048typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1049 const gdb_byte *info_ptr,
dee91e82
DE
1050 struct die_info *comp_unit_die,
1051 int has_children,
1052 void *data);
1053
52059ffd
TT
1054struct file_entry
1055{
1056 const char *name;
1057 unsigned int dir_index;
1058 unsigned int mod_time;
1059 unsigned int length;
83769d0b
DE
1060 /* Non-zero if referenced by the Line Number Program. */
1061 int included_p;
1062 /* The associated symbol table, if any. */
1063 struct symtab *symtab;
52059ffd
TT
1064};
1065
debd256d
JB
1066/* The line number information for a compilation unit (found in the
1067 .debug_line section) begins with a "statement program header",
1068 which contains the following information. */
1069struct line_header
1070{
527f3840
JK
1071 /* Offset of line number information in .debug_line section. */
1072 sect_offset offset;
1073
1074 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1075 unsigned offset_in_dwz : 1;
1076
debd256d
JB
1077 unsigned int total_length;
1078 unsigned short version;
1079 unsigned int header_length;
1080 unsigned char minimum_instruction_length;
2dc7f7b3 1081 unsigned char maximum_ops_per_instruction;
debd256d
JB
1082 unsigned char default_is_stmt;
1083 int line_base;
1084 unsigned char line_range;
1085 unsigned char opcode_base;
1086
1087 /* standard_opcode_lengths[i] is the number of operands for the
1088 standard opcode whose value is i. This means that
1089 standard_opcode_lengths[0] is unused, and the last meaningful
1090 element is standard_opcode_lengths[opcode_base - 1]. */
1091 unsigned char *standard_opcode_lengths;
1092
1093 /* The include_directories table. NOTE! These strings are not
1094 allocated with xmalloc; instead, they are pointers into
1095 debug_line_buffer. If you try to free them, `free' will get
1096 indigestion. */
1097 unsigned int num_include_dirs, include_dirs_size;
d521ce57 1098 const char **include_dirs;
debd256d
JB
1099
1100 /* The file_names table. NOTE! These strings are not allocated
1101 with xmalloc; instead, they are pointers into debug_line_buffer.
1102 Don't try to free them directly. */
1103 unsigned int num_file_names, file_names_size;
52059ffd 1104 struct file_entry *file_names;
debd256d
JB
1105
1106 /* The start and end of the statement program following this
6502dd73 1107 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 1108 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 1109};
c906108c
SS
1110
1111/* When we construct a partial symbol table entry we only
0963b4bd 1112 need this much information. */
c906108c
SS
1113struct partial_die_info
1114 {
72bf9492 1115 /* Offset of this DIE. */
b64f50a1 1116 sect_offset offset;
72bf9492
DJ
1117
1118 /* DWARF-2 tag for this DIE. */
1119 ENUM_BITFIELD(dwarf_tag) tag : 16;
1120
72bf9492
DJ
1121 /* Assorted flags describing the data found in this DIE. */
1122 unsigned int has_children : 1;
1123 unsigned int is_external : 1;
1124 unsigned int is_declaration : 1;
1125 unsigned int has_type : 1;
1126 unsigned int has_specification : 1;
1127 unsigned int has_pc_info : 1;
481860b3 1128 unsigned int may_be_inlined : 1;
72bf9492 1129
0c1b455e
TT
1130 /* This DIE has been marked DW_AT_main_subprogram. */
1131 unsigned int main_subprogram : 1;
1132
72bf9492
DJ
1133 /* Flag set if the SCOPE field of this structure has been
1134 computed. */
1135 unsigned int scope_set : 1;
1136
fa4028e9
JB
1137 /* Flag set if the DIE has a byte_size attribute. */
1138 unsigned int has_byte_size : 1;
1139
ff908ebf
AW
1140 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1141 unsigned int has_const_value : 1;
1142
98bfdba5
PA
1143 /* Flag set if any of the DIE's children are template arguments. */
1144 unsigned int has_template_arguments : 1;
1145
abc72ce4
DE
1146 /* Flag set if fixup_partial_die has been called on this die. */
1147 unsigned int fixup_called : 1;
1148
36586728
TT
1149 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1150 unsigned int is_dwz : 1;
1151
1152 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1153 unsigned int spec_is_dwz : 1;
1154
72bf9492 1155 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1156 sometimes a default name for unnamed DIEs. */
15d034d0 1157 const char *name;
72bf9492 1158
abc72ce4
DE
1159 /* The linkage name, if present. */
1160 const char *linkage_name;
1161
72bf9492
DJ
1162 /* The scope to prepend to our children. This is generally
1163 allocated on the comp_unit_obstack, so will disappear
1164 when this compilation unit leaves the cache. */
15d034d0 1165 const char *scope;
72bf9492 1166
95554aad
TT
1167 /* Some data associated with the partial DIE. The tag determines
1168 which field is live. */
1169 union
1170 {
1171 /* The location description associated with this DIE, if any. */
1172 struct dwarf_block *locdesc;
1173 /* The offset of an import, for DW_TAG_imported_unit. */
1174 sect_offset offset;
1175 } d;
72bf9492
DJ
1176
1177 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1178 CORE_ADDR lowpc;
1179 CORE_ADDR highpc;
72bf9492 1180
93311388 1181 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1182 DW_AT_sibling, if any. */
abc72ce4
DE
1183 /* NOTE: This member isn't strictly necessary, read_partial_die could
1184 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1185 const gdb_byte *sibling;
72bf9492
DJ
1186
1187 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1188 DW_AT_specification (or DW_AT_abstract_origin or
1189 DW_AT_extension). */
b64f50a1 1190 sect_offset spec_offset;
72bf9492
DJ
1191
1192 /* Pointers to this DIE's parent, first child, and next sibling,
1193 if any. */
1194 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1195 };
1196
0963b4bd 1197/* This data structure holds the information of an abbrev. */
c906108c
SS
1198struct abbrev_info
1199 {
1200 unsigned int number; /* number identifying abbrev */
1201 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1202 unsigned short has_children; /* boolean */
1203 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1204 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1205 struct abbrev_info *next; /* next in chain */
1206 };
1207
1208struct attr_abbrev
1209 {
9d25dd43
DE
1210 ENUM_BITFIELD(dwarf_attribute) name : 16;
1211 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1212
1213 /* It is valid only if FORM is DW_FORM_implicit_const. */
1214 LONGEST implicit_const;
c906108c
SS
1215 };
1216
433df2d4
DE
1217/* Size of abbrev_table.abbrev_hash_table. */
1218#define ABBREV_HASH_SIZE 121
1219
1220/* Top level data structure to contain an abbreviation table. */
1221
1222struct abbrev_table
1223{
f4dc4d17
DE
1224 /* Where the abbrev table came from.
1225 This is used as a sanity check when the table is used. */
433df2d4
DE
1226 sect_offset offset;
1227
1228 /* Storage for the abbrev table. */
1229 struct obstack abbrev_obstack;
1230
1231 /* Hash table of abbrevs.
1232 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1233 It could be statically allocated, but the previous code didn't so we
1234 don't either. */
1235 struct abbrev_info **abbrevs;
1236};
1237
0963b4bd 1238/* Attributes have a name and a value. */
b60c80d6
DJ
1239struct attribute
1240 {
9d25dd43 1241 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1242 ENUM_BITFIELD(dwarf_form) form : 15;
1243
1244 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1245 field should be in u.str (existing only for DW_STRING) but it is kept
1246 here for better struct attribute alignment. */
1247 unsigned int string_is_canonical : 1;
1248
b60c80d6
DJ
1249 union
1250 {
15d034d0 1251 const char *str;
b60c80d6 1252 struct dwarf_block *blk;
43bbcdc2
PH
1253 ULONGEST unsnd;
1254 LONGEST snd;
b60c80d6 1255 CORE_ADDR addr;
ac9ec31b 1256 ULONGEST signature;
b60c80d6
DJ
1257 }
1258 u;
1259 };
1260
0963b4bd 1261/* This data structure holds a complete die structure. */
c906108c
SS
1262struct die_info
1263 {
76815b17
DE
1264 /* DWARF-2 tag for this DIE. */
1265 ENUM_BITFIELD(dwarf_tag) tag : 16;
1266
1267 /* Number of attributes */
98bfdba5
PA
1268 unsigned char num_attrs;
1269
1270 /* True if we're presently building the full type name for the
1271 type derived from this DIE. */
1272 unsigned char building_fullname : 1;
76815b17 1273
adde2bff
DE
1274 /* True if this die is in process. PR 16581. */
1275 unsigned char in_process : 1;
1276
76815b17
DE
1277 /* Abbrev number */
1278 unsigned int abbrev;
1279
93311388 1280 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1281 sect_offset offset;
78ba4af6
JB
1282
1283 /* The dies in a compilation unit form an n-ary tree. PARENT
1284 points to this die's parent; CHILD points to the first child of
1285 this node; and all the children of a given node are chained
4950bc1c 1286 together via their SIBLING fields. */
639d11d3
DC
1287 struct die_info *child; /* Its first child, if any. */
1288 struct die_info *sibling; /* Its next sibling, if any. */
1289 struct die_info *parent; /* Its parent, if any. */
c906108c 1290
b60c80d6
DJ
1291 /* An array of attributes, with NUM_ATTRS elements. There may be
1292 zero, but it's not common and zero-sized arrays are not
1293 sufficiently portable C. */
1294 struct attribute attrs[1];
c906108c
SS
1295 };
1296
0963b4bd 1297/* Get at parts of an attribute structure. */
c906108c
SS
1298
1299#define DW_STRING(attr) ((attr)->u.str)
8285870a 1300#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1301#define DW_UNSND(attr) ((attr)->u.unsnd)
1302#define DW_BLOCK(attr) ((attr)->u.blk)
1303#define DW_SND(attr) ((attr)->u.snd)
1304#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1305#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1306
0963b4bd 1307/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1308struct dwarf_block
1309 {
56eb65bd 1310 size_t size;
1d6edc3c
JK
1311
1312 /* Valid only if SIZE is not zero. */
d521ce57 1313 const gdb_byte *data;
c906108c
SS
1314 };
1315
c906108c
SS
1316#ifndef ATTR_ALLOC_CHUNK
1317#define ATTR_ALLOC_CHUNK 4
1318#endif
1319
c906108c
SS
1320/* Allocate fields for structs, unions and enums in this size. */
1321#ifndef DW_FIELD_ALLOC_CHUNK
1322#define DW_FIELD_ALLOC_CHUNK 4
1323#endif
1324
c906108c
SS
1325/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1326 but this would require a corresponding change in unpack_field_as_long
1327 and friends. */
1328static int bits_per_byte = 8;
1329
52059ffd
TT
1330struct nextfield
1331{
1332 struct nextfield *next;
1333 int accessibility;
1334 int virtuality;
1335 struct field field;
1336};
1337
1338struct nextfnfield
1339{
1340 struct nextfnfield *next;
1341 struct fn_field fnfield;
1342};
1343
1344struct fnfieldlist
1345{
1346 const char *name;
1347 int length;
1348 struct nextfnfield *head;
1349};
1350
1351struct typedef_field_list
1352{
1353 struct typedef_field field;
1354 struct typedef_field_list *next;
1355};
1356
c906108c
SS
1357/* The routines that read and process dies for a C struct or C++ class
1358 pass lists of data member fields and lists of member function fields
1359 in an instance of a field_info structure, as defined below. */
1360struct field_info
c5aa993b 1361 {
0963b4bd 1362 /* List of data member and baseclasses fields. */
52059ffd 1363 struct nextfield *fields, *baseclasses;
c906108c 1364
7d0ccb61 1365 /* Number of fields (including baseclasses). */
c5aa993b 1366 int nfields;
c906108c 1367
c5aa993b
JM
1368 /* Number of baseclasses. */
1369 int nbaseclasses;
c906108c 1370
c5aa993b
JM
1371 /* Set if the accesibility of one of the fields is not public. */
1372 int non_public_fields;
c906108c 1373
c5aa993b
JM
1374 /* Member function fields array, entries are allocated in the order they
1375 are encountered in the object file. */
52059ffd 1376 struct nextfnfield *fnfields;
c906108c 1377
c5aa993b
JM
1378 /* Member function fieldlist array, contains name of possibly overloaded
1379 member function, number of overloaded member functions and a pointer
1380 to the head of the member function field chain. */
52059ffd 1381 struct fnfieldlist *fnfieldlists;
c906108c 1382
c5aa993b
JM
1383 /* Number of entries in the fnfieldlists array. */
1384 int nfnfields;
98751a41
JK
1385
1386 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1387 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1388 struct typedef_field_list *typedef_field_list;
98751a41 1389 unsigned typedef_field_list_count;
c5aa993b 1390 };
c906108c 1391
10b3939b
DJ
1392/* One item on the queue of compilation units to read in full symbols
1393 for. */
1394struct dwarf2_queue_item
1395{
1396 struct dwarf2_per_cu_data *per_cu;
95554aad 1397 enum language pretend_language;
10b3939b
DJ
1398 struct dwarf2_queue_item *next;
1399};
1400
1401/* The current queue. */
1402static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1403
ae038cb0
DJ
1404/* Loaded secondary compilation units are kept in memory until they
1405 have not been referenced for the processing of this many
1406 compilation units. Set this to zero to disable caching. Cache
1407 sizes of up to at least twenty will improve startup time for
1408 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1409static int dwarf_max_cache_age = 5;
920d2a44 1410static void
b4f54984
DE
1411show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1412 struct cmd_list_element *c, const char *value)
920d2a44 1413{
3e43a32a 1414 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1415 "DWARF compilation units is %s.\n"),
920d2a44
AC
1416 value);
1417}
4390d890 1418\f
c906108c
SS
1419/* local function prototypes */
1420
a32a8923
DE
1421static const char *get_section_name (const struct dwarf2_section_info *);
1422
1423static const char *get_section_file_name (const struct dwarf2_section_info *);
1424
4efb68b1 1425static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1426
918dd910
JK
1427static void dwarf2_find_base_address (struct die_info *die,
1428 struct dwarf2_cu *cu);
1429
0018ea6f
DE
1430static struct partial_symtab *create_partial_symtab
1431 (struct dwarf2_per_cu_data *per_cu, const char *name);
1432
c67a9c90 1433static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1434
72bf9492
DJ
1435static void scan_partial_symbols (struct partial_die_info *,
1436 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1437 int, struct dwarf2_cu *);
c906108c 1438
72bf9492
DJ
1439static void add_partial_symbol (struct partial_die_info *,
1440 struct dwarf2_cu *);
63d06c5c 1441
72bf9492
DJ
1442static void add_partial_namespace (struct partial_die_info *pdi,
1443 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1444 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1445
5d7cb8df 1446static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1447 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1448 struct dwarf2_cu *cu);
1449
72bf9492
DJ
1450static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1451 struct dwarf2_cu *cu);
91c24f0a 1452
bc30ff58
JB
1453static void add_partial_subprogram (struct partial_die_info *pdi,
1454 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1455 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1456
257e7a09
YQ
1457static void dwarf2_read_symtab (struct partial_symtab *,
1458 struct objfile *);
c906108c 1459
a14ed312 1460static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1461
433df2d4
DE
1462static struct abbrev_info *abbrev_table_lookup_abbrev
1463 (const struct abbrev_table *, unsigned int);
1464
1465static struct abbrev_table *abbrev_table_read_table
1466 (struct dwarf2_section_info *, sect_offset);
1467
1468static void abbrev_table_free (struct abbrev_table *);
1469
f4dc4d17
DE
1470static void abbrev_table_free_cleanup (void *);
1471
dee91e82
DE
1472static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1473 struct dwarf2_section_info *);
c906108c 1474
f3dd6933 1475static void dwarf2_free_abbrev_table (void *);
c906108c 1476
d521ce57 1477static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1478
dee91e82 1479static struct partial_die_info *load_partial_dies
d521ce57 1480 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1481
d521ce57
TT
1482static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1483 struct partial_die_info *,
1484 struct abbrev_info *,
1485 unsigned int,
1486 const gdb_byte *);
c906108c 1487
36586728 1488static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1489 struct dwarf2_cu *);
72bf9492
DJ
1490
1491static void fixup_partial_die (struct partial_die_info *,
1492 struct dwarf2_cu *);
1493
d521ce57
TT
1494static const gdb_byte *read_attribute (const struct die_reader_specs *,
1495 struct attribute *, struct attr_abbrev *,
1496 const gdb_byte *);
a8329558 1497
a1855c1d 1498static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1499
a1855c1d 1500static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1501
a1855c1d 1502static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1503
a1855c1d 1504static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1505
a1855c1d 1506static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1507
d521ce57 1508static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1509 unsigned int *);
c906108c 1510
d521ce57 1511static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1512
1513static LONGEST read_checked_initial_length_and_offset
d521ce57 1514 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1515 unsigned int *, unsigned int *);
613e1657 1516
d521ce57
TT
1517static LONGEST read_offset (bfd *, const gdb_byte *,
1518 const struct comp_unit_head *,
c764a876
DE
1519 unsigned int *);
1520
d521ce57 1521static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1522
f4dc4d17
DE
1523static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1524 sect_offset);
1525
d521ce57 1526static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1527
d521ce57 1528static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1529
d521ce57
TT
1530static const char *read_indirect_string (bfd *, const gdb_byte *,
1531 const struct comp_unit_head *,
1532 unsigned int *);
4bdf3d34 1533
43988095
JK
1534static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1535 const struct comp_unit_head *,
1536 unsigned int *);
36586728 1537
43988095 1538static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1539
d521ce57 1540static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1541
d521ce57
TT
1542static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1543 const gdb_byte *,
3019eac3
DE
1544 unsigned int *);
1545
d521ce57 1546static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1547 ULONGEST str_index);
3019eac3 1548
e142c38c 1549static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1550
e142c38c
DJ
1551static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1552 struct dwarf2_cu *);
c906108c 1553
348e048f 1554static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1555 unsigned int);
348e048f 1556
7d45c7c3
KB
1557static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1558 struct dwarf2_cu *cu);
1559
05cf31d1
JB
1560static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1561 struct dwarf2_cu *cu);
1562
e142c38c 1563static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1564
e142c38c 1565static struct die_info *die_specification (struct die_info *die,
f2f0e013 1566 struct dwarf2_cu **);
63d06c5c 1567
debd256d
JB
1568static void free_line_header (struct line_header *lh);
1569
3019eac3
DE
1570static struct line_header *dwarf_decode_line_header (unsigned int offset,
1571 struct dwarf2_cu *cu);
debd256d 1572
f3f5162e 1573static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1574 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1575 CORE_ADDR, int decode_mapping);
c906108c 1576
4d663531 1577static void dwarf2_start_subfile (const char *, const char *);
c906108c 1578
43f3e411
DE
1579static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1580 const char *, const char *,
1581 CORE_ADDR);
f4dc4d17 1582
a14ed312 1583static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1584 struct dwarf2_cu *);
c906108c 1585
34eaf542
TT
1586static struct symbol *new_symbol_full (struct die_info *, struct type *,
1587 struct dwarf2_cu *, struct symbol *);
1588
ff39bb5e 1589static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1590 struct dwarf2_cu *);
c906108c 1591
ff39bb5e 1592static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1593 struct type *type,
1594 const char *name,
1595 struct obstack *obstack,
12df843f 1596 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1597 const gdb_byte **bytes,
98bfdba5 1598 struct dwarf2_locexpr_baton **baton);
2df3850c 1599
e7c27a73 1600static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1601
b4ba55a1
JB
1602static int need_gnat_info (struct dwarf2_cu *);
1603
3e43a32a
MS
1604static struct type *die_descriptive_type (struct die_info *,
1605 struct dwarf2_cu *);
b4ba55a1
JB
1606
1607static void set_descriptive_type (struct type *, struct die_info *,
1608 struct dwarf2_cu *);
1609
e7c27a73
DJ
1610static struct type *die_containing_type (struct die_info *,
1611 struct dwarf2_cu *);
c906108c 1612
ff39bb5e 1613static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1614 struct dwarf2_cu *);
c906108c 1615
f792889a 1616static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1617
673bfd45
DE
1618static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1619
0d5cff50 1620static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1621
6e70227d 1622static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1623 const char *suffix, int physname,
1624 struct dwarf2_cu *cu);
63d06c5c 1625
e7c27a73 1626static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1627
348e048f
DE
1628static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1629
e7c27a73 1630static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1631
e7c27a73 1632static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1633
96408a79
SA
1634static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1635
ff013f42
JK
1636static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1637 struct dwarf2_cu *, struct partial_symtab *);
1638
3a2b436a 1639/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1640 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1641enum pc_bounds_kind
1642{
e385593e 1643 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1644 PC_BOUNDS_NOT_PRESENT,
1645
e385593e
JK
1646 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1647 were present but they do not form a valid range of PC addresses. */
1648 PC_BOUNDS_INVALID,
1649
3a2b436a
JK
1650 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1651 PC_BOUNDS_RANGES,
1652
1653 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1654 PC_BOUNDS_HIGH_LOW,
1655};
1656
1657static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *,
1660 struct partial_symtab *);
c906108c 1661
fae299cd
DC
1662static void get_scope_pc_bounds (struct die_info *,
1663 CORE_ADDR *, CORE_ADDR *,
1664 struct dwarf2_cu *);
1665
801e3a5b
JB
1666static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1667 CORE_ADDR, struct dwarf2_cu *);
1668
a14ed312 1669static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1670 struct dwarf2_cu *);
c906108c 1671
a14ed312 1672static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1673 struct type *, struct dwarf2_cu *);
c906108c 1674
a14ed312 1675static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1676 struct die_info *, struct type *,
e7c27a73 1677 struct dwarf2_cu *);
c906108c 1678
a14ed312 1679static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1680 struct type *,
1681 struct dwarf2_cu *);
c906108c 1682
134d01f1 1683static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1684
e7c27a73 1685static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1686
e7c27a73 1687static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1688
5d7cb8df
JK
1689static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1690
22cee43f
PMR
1691static struct using_direct **using_directives (enum language);
1692
27aa8d6a
SW
1693static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1694
74921315
KS
1695static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1696
f55ee35c
JK
1697static struct type *read_module_type (struct die_info *die,
1698 struct dwarf2_cu *cu);
1699
38d518c9 1700static const char *namespace_name (struct die_info *die,
e142c38c 1701 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1702
134d01f1 1703static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1704
e7c27a73 1705static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1706
6e70227d 1707static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1708 struct dwarf2_cu *);
1709
bf6af496 1710static struct die_info *read_die_and_siblings_1
d521ce57 1711 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1712 struct die_info *);
639d11d3 1713
dee91e82 1714static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1715 const gdb_byte *info_ptr,
1716 const gdb_byte **new_info_ptr,
639d11d3
DC
1717 struct die_info *parent);
1718
d521ce57
TT
1719static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1720 struct die_info **, const gdb_byte *,
1721 int *, int);
3019eac3 1722
d521ce57
TT
1723static const gdb_byte *read_full_die (const struct die_reader_specs *,
1724 struct die_info **, const gdb_byte *,
1725 int *);
93311388 1726
e7c27a73 1727static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1728
15d034d0
TT
1729static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1730 struct obstack *);
71c25dea 1731
15d034d0 1732static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1733
15d034d0 1734static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1735 struct die_info *die,
1736 struct dwarf2_cu *cu);
1737
ca69b9e6
DE
1738static const char *dwarf2_physname (const char *name, struct die_info *die,
1739 struct dwarf2_cu *cu);
1740
e142c38c 1741static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1742 struct dwarf2_cu **);
9219021c 1743
f39c6ffd 1744static const char *dwarf_tag_name (unsigned int);
c906108c 1745
f39c6ffd 1746static const char *dwarf_attr_name (unsigned int);
c906108c 1747
f39c6ffd 1748static const char *dwarf_form_name (unsigned int);
c906108c 1749
a14ed312 1750static char *dwarf_bool_name (unsigned int);
c906108c 1751
f39c6ffd 1752static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1753
f9aca02d 1754static struct die_info *sibling_die (struct die_info *);
c906108c 1755
d97bc12b
DE
1756static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1757
1758static void dump_die_for_error (struct die_info *);
1759
1760static void dump_die_1 (struct ui_file *, int level, int max_level,
1761 struct die_info *);
c906108c 1762
d97bc12b 1763/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1764
51545339 1765static void store_in_ref_table (struct die_info *,
10b3939b 1766 struct dwarf2_cu *);
c906108c 1767
ff39bb5e 1768static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1769
ff39bb5e 1770static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1771
348e048f 1772static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1773 const struct attribute *,
348e048f
DE
1774 struct dwarf2_cu **);
1775
10b3939b 1776static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1777 const struct attribute *,
f2f0e013 1778 struct dwarf2_cu **);
c906108c 1779
348e048f 1780static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1781 const struct attribute *,
348e048f
DE
1782 struct dwarf2_cu **);
1783
ac9ec31b
DE
1784static struct type *get_signatured_type (struct die_info *, ULONGEST,
1785 struct dwarf2_cu *);
1786
1787static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1788 const struct attribute *,
ac9ec31b
DE
1789 struct dwarf2_cu *);
1790
e5fe5e75 1791static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1792
52dc124a 1793static void read_signatured_type (struct signatured_type *);
348e048f 1794
63e43d3a
PMR
1795static int attr_to_dynamic_prop (const struct attribute *attr,
1796 struct die_info *die, struct dwarf2_cu *cu,
1797 struct dynamic_prop *prop);
1798
c906108c
SS
1799/* memory allocation interface */
1800
7b5a2f43 1801static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1802
b60c80d6 1803static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1804
43f3e411 1805static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1806
6e5a29e1 1807static int attr_form_is_block (const struct attribute *);
8e19ed76 1808
6e5a29e1 1809static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1810
6e5a29e1 1811static int attr_form_is_constant (const struct attribute *);
3690dd37 1812
6e5a29e1 1813static int attr_form_is_ref (const struct attribute *);
7771576e 1814
8cf6f0b1
TT
1815static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1816 struct dwarf2_loclist_baton *baton,
ff39bb5e 1817 const struct attribute *attr);
8cf6f0b1 1818
ff39bb5e 1819static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1820 struct symbol *sym,
f1e6e072
TT
1821 struct dwarf2_cu *cu,
1822 int is_block);
4c2df51b 1823
d521ce57
TT
1824static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1825 const gdb_byte *info_ptr,
1826 struct abbrev_info *abbrev);
4bb7a0a7 1827
72bf9492
DJ
1828static void free_stack_comp_unit (void *);
1829
72bf9492
DJ
1830static hashval_t partial_die_hash (const void *item);
1831
1832static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1833
ae038cb0 1834static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1835 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1836
9816fde3 1837static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1838 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1839
1840static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1841 struct die_info *comp_unit_die,
1842 enum language pretend_language);
93311388 1843
68dc6402 1844static void free_heap_comp_unit (void *);
ae038cb0
DJ
1845
1846static void free_cached_comp_units (void *);
1847
1848static void age_cached_comp_units (void);
1849
dee91e82 1850static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1851
f792889a
DJ
1852static struct type *set_die_type (struct die_info *, struct type *,
1853 struct dwarf2_cu *);
1c379e20 1854
ae038cb0
DJ
1855static void create_all_comp_units (struct objfile *);
1856
0e50663e 1857static int create_all_type_units (struct objfile *);
1fd400ff 1858
95554aad
TT
1859static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1860 enum language);
10b3939b 1861
95554aad
TT
1862static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1863 enum language);
10b3939b 1864
f4dc4d17
DE
1865static void process_full_type_unit (struct dwarf2_per_cu_data *,
1866 enum language);
1867
10b3939b
DJ
1868static void dwarf2_add_dependence (struct dwarf2_cu *,
1869 struct dwarf2_per_cu_data *);
1870
ae038cb0
DJ
1871static void dwarf2_mark (struct dwarf2_cu *);
1872
1873static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1874
b64f50a1 1875static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1876 struct dwarf2_per_cu_data *);
673bfd45 1877
f792889a 1878static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1879
9291a0cd
TT
1880static void dwarf2_release_queue (void *dummy);
1881
95554aad
TT
1882static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1883 enum language pretend_language);
1884
a0f42c21 1885static void process_queue (void);
9291a0cd
TT
1886
1887static void find_file_and_directory (struct die_info *die,
1888 struct dwarf2_cu *cu,
15d034d0 1889 const char **name, const char **comp_dir);
9291a0cd
TT
1890
1891static char *file_full_name (int file, struct line_header *lh,
1892 const char *comp_dir);
1893
43988095
JK
1894/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1895enum class rcuh_kind { COMPILE, TYPE };
1896
d521ce57 1897static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1898 (struct comp_unit_head *header,
1899 struct dwarf2_section_info *section,
d521ce57 1900 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1901 rcuh_kind section_kind);
36586728 1902
fd820528 1903static void init_cutu_and_read_dies
f4dc4d17
DE
1904 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1905 int use_existing_cu, int keep,
3019eac3
DE
1906 die_reader_func_ftype *die_reader_func, void *data);
1907
dee91e82
DE
1908static void init_cutu_and_read_dies_simple
1909 (struct dwarf2_per_cu_data *this_cu,
1910 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1911
673bfd45 1912static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1913
3019eac3
DE
1914static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1915
57d63ce2
DE
1916static struct dwo_unit *lookup_dwo_unit_in_dwp
1917 (struct dwp_file *dwp_file, const char *comp_dir,
1918 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
1919
1920static struct dwp_file *get_dwp_file (void);
1921
3019eac3 1922static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1923 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1924
1925static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1926 (struct signatured_type *, const char *, const char *);
3019eac3 1927
89e63ee4
DE
1928static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1929
3019eac3
DE
1930static void free_dwo_file_cleanup (void *);
1931
95554aad
TT
1932static void process_cu_includes (void);
1933
1b80a9fa 1934static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1935
1936static void free_line_header_voidp (void *arg);
4390d890
DE
1937\f
1938/* Various complaints about symbol reading that don't abort the process. */
1939
1940static void
1941dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1942{
1943 complaint (&symfile_complaints,
1944 _("statement list doesn't fit in .debug_line section"));
1945}
1946
1947static void
1948dwarf2_debug_line_missing_file_complaint (void)
1949{
1950 complaint (&symfile_complaints,
1951 _(".debug_line section has line data without a file"));
1952}
1953
1954static void
1955dwarf2_debug_line_missing_end_sequence_complaint (void)
1956{
1957 complaint (&symfile_complaints,
1958 _(".debug_line section has line "
1959 "program sequence without an end"));
1960}
1961
1962static void
1963dwarf2_complex_location_expr_complaint (void)
1964{
1965 complaint (&symfile_complaints, _("location expression too complex"));
1966}
1967
1968static void
1969dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1970 int arg3)
1971{
1972 complaint (&symfile_complaints,
1973 _("const value length mismatch for '%s', got %d, expected %d"),
1974 arg1, arg2, arg3);
1975}
1976
1977static void
1978dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1979{
1980 complaint (&symfile_complaints,
1981 _("debug info runs off end of %s section"
1982 " [in module %s]"),
a32a8923
DE
1983 get_section_name (section),
1984 get_section_file_name (section));
4390d890 1985}
1b80a9fa 1986
4390d890
DE
1987static void
1988dwarf2_macro_malformed_definition_complaint (const char *arg1)
1989{
1990 complaint (&symfile_complaints,
1991 _("macro debug info contains a "
1992 "malformed macro definition:\n`%s'"),
1993 arg1);
1994}
1995
1996static void
1997dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1998{
1999 complaint (&symfile_complaints,
2000 _("invalid attribute class or form for '%s' in '%s'"),
2001 arg1, arg2);
2002}
527f3840
JK
2003
2004/* Hash function for line_header_hash. */
2005
2006static hashval_t
2007line_header_hash (const struct line_header *ofs)
2008{
2009 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
2010}
2011
2012/* Hash function for htab_create_alloc_ex for line_header_hash. */
2013
2014static hashval_t
2015line_header_hash_voidp (const void *item)
2016{
9a3c8263 2017 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2018
2019 return line_header_hash (ofs);
2020}
2021
2022/* Equality function for line_header_hash. */
2023
2024static int
2025line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2026{
9a3c8263
SM
2027 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2028 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840
JK
2029
2030 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
2031 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2032}
2033
4390d890 2034\f
9291a0cd
TT
2035#if WORDS_BIGENDIAN
2036
2037/* Convert VALUE between big- and little-endian. */
2038static offset_type
2039byte_swap (offset_type value)
2040{
2041 offset_type result;
2042
2043 result = (value & 0xff) << 24;
2044 result |= (value & 0xff00) << 8;
2045 result |= (value & 0xff0000) >> 8;
2046 result |= (value & 0xff000000) >> 24;
2047 return result;
2048}
2049
2050#define MAYBE_SWAP(V) byte_swap (V)
2051
2052#else
2053#define MAYBE_SWAP(V) (V)
2054#endif /* WORDS_BIGENDIAN */
2055
31aa7e4e
JB
2056/* Read the given attribute value as an address, taking the attribute's
2057 form into account. */
2058
2059static CORE_ADDR
2060attr_value_as_address (struct attribute *attr)
2061{
2062 CORE_ADDR addr;
2063
2064 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2065 {
2066 /* Aside from a few clearly defined exceptions, attributes that
2067 contain an address must always be in DW_FORM_addr form.
2068 Unfortunately, some compilers happen to be violating this
2069 requirement by encoding addresses using other forms, such
2070 as DW_FORM_data4 for example. For those broken compilers,
2071 we try to do our best, without any guarantee of success,
2072 to interpret the address correctly. It would also be nice
2073 to generate a complaint, but that would require us to maintain
2074 a list of legitimate cases where a non-address form is allowed,
2075 as well as update callers to pass in at least the CU's DWARF
2076 version. This is more overhead than what we're willing to
2077 expand for a pretty rare case. */
2078 addr = DW_UNSND (attr);
2079 }
2080 else
2081 addr = DW_ADDR (attr);
2082
2083 return addr;
2084}
2085
9291a0cd
TT
2086/* The suffix for an index file. */
2087#define INDEX_SUFFIX ".gdb-index"
2088
c906108c 2089/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2090 information and return true if we have enough to do something.
2091 NAMES points to the dwarf2 section names, or is NULL if the standard
2092 ELF names are used. */
c906108c
SS
2093
2094int
251d32d9
TG
2095dwarf2_has_info (struct objfile *objfile,
2096 const struct dwarf2_debug_sections *names)
c906108c 2097{
9a3c8263
SM
2098 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2099 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2100 if (!dwarf2_per_objfile)
2101 {
2102 /* Initialize per-objfile state. */
2103 struct dwarf2_per_objfile *data
8d749320 2104 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2105
be391dca
TT
2106 memset (data, 0, sizeof (*data));
2107 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2108 dwarf2_per_objfile = data;
6502dd73 2109
251d32d9
TG
2110 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2111 (void *) names);
be391dca
TT
2112 dwarf2_per_objfile->objfile = objfile;
2113 }
73869dc2 2114 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2115 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2116 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2117 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2118}
2119
2120/* Return the containing section of virtual section SECTION. */
2121
2122static struct dwarf2_section_info *
2123get_containing_section (const struct dwarf2_section_info *section)
2124{
2125 gdb_assert (section->is_virtual);
2126 return section->s.containing_section;
c906108c
SS
2127}
2128
a32a8923
DE
2129/* Return the bfd owner of SECTION. */
2130
2131static struct bfd *
2132get_section_bfd_owner (const struct dwarf2_section_info *section)
2133{
73869dc2
DE
2134 if (section->is_virtual)
2135 {
2136 section = get_containing_section (section);
2137 gdb_assert (!section->is_virtual);
2138 }
049412e3 2139 return section->s.section->owner;
a32a8923
DE
2140}
2141
2142/* Return the bfd section of SECTION.
2143 Returns NULL if the section is not present. */
2144
2145static asection *
2146get_section_bfd_section (const struct dwarf2_section_info *section)
2147{
73869dc2
DE
2148 if (section->is_virtual)
2149 {
2150 section = get_containing_section (section);
2151 gdb_assert (!section->is_virtual);
2152 }
049412e3 2153 return section->s.section;
a32a8923
DE
2154}
2155
2156/* Return the name of SECTION. */
2157
2158static const char *
2159get_section_name (const struct dwarf2_section_info *section)
2160{
2161 asection *sectp = get_section_bfd_section (section);
2162
2163 gdb_assert (sectp != NULL);
2164 return bfd_section_name (get_section_bfd_owner (section), sectp);
2165}
2166
2167/* Return the name of the file SECTION is in. */
2168
2169static const char *
2170get_section_file_name (const struct dwarf2_section_info *section)
2171{
2172 bfd *abfd = get_section_bfd_owner (section);
2173
2174 return bfd_get_filename (abfd);
2175}
2176
2177/* Return the id of SECTION.
2178 Returns 0 if SECTION doesn't exist. */
2179
2180static int
2181get_section_id (const struct dwarf2_section_info *section)
2182{
2183 asection *sectp = get_section_bfd_section (section);
2184
2185 if (sectp == NULL)
2186 return 0;
2187 return sectp->id;
2188}
2189
2190/* Return the flags of SECTION.
73869dc2 2191 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2192
2193static int
2194get_section_flags (const struct dwarf2_section_info *section)
2195{
2196 asection *sectp = get_section_bfd_section (section);
2197
2198 gdb_assert (sectp != NULL);
2199 return bfd_get_section_flags (sectp->owner, sectp);
2200}
2201
251d32d9
TG
2202/* When loading sections, we look either for uncompressed section or for
2203 compressed section names. */
233a11ab
CS
2204
2205static int
251d32d9
TG
2206section_is_p (const char *section_name,
2207 const struct dwarf2_section_names *names)
233a11ab 2208{
251d32d9
TG
2209 if (names->normal != NULL
2210 && strcmp (section_name, names->normal) == 0)
2211 return 1;
2212 if (names->compressed != NULL
2213 && strcmp (section_name, names->compressed) == 0)
2214 return 1;
2215 return 0;
233a11ab
CS
2216}
2217
c906108c
SS
2218/* This function is mapped across the sections and remembers the
2219 offset and size of each of the debugging sections we are interested
2220 in. */
2221
2222static void
251d32d9 2223dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2224{
251d32d9 2225 const struct dwarf2_debug_sections *names;
dc7650b8 2226 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2227
2228 if (vnames == NULL)
2229 names = &dwarf2_elf_names;
2230 else
2231 names = (const struct dwarf2_debug_sections *) vnames;
2232
dc7650b8
JK
2233 if ((aflag & SEC_HAS_CONTENTS) == 0)
2234 {
2235 }
2236 else if (section_is_p (sectp->name, &names->info))
c906108c 2237 {
049412e3 2238 dwarf2_per_objfile->info.s.section = sectp;
dce234bc 2239 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2240 }
251d32d9 2241 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2242 {
049412e3 2243 dwarf2_per_objfile->abbrev.s.section = sectp;
dce234bc 2244 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2245 }
251d32d9 2246 else if (section_is_p (sectp->name, &names->line))
c906108c 2247 {
049412e3 2248 dwarf2_per_objfile->line.s.section = sectp;
dce234bc 2249 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2250 }
251d32d9 2251 else if (section_is_p (sectp->name, &names->loc))
c906108c 2252 {
049412e3 2253 dwarf2_per_objfile->loc.s.section = sectp;
dce234bc 2254 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2255 }
43988095
JK
2256 else if (section_is_p (sectp->name, &names->loclists))
2257 {
2258 dwarf2_per_objfile->loclists.s.section = sectp;
2259 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2260 }
251d32d9 2261 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2262 {
049412e3 2263 dwarf2_per_objfile->macinfo.s.section = sectp;
dce234bc 2264 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2265 }
cf2c3c16
TT
2266 else if (section_is_p (sectp->name, &names->macro))
2267 {
049412e3 2268 dwarf2_per_objfile->macro.s.section = sectp;
cf2c3c16
TT
2269 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2270 }
251d32d9 2271 else if (section_is_p (sectp->name, &names->str))
c906108c 2272 {
049412e3 2273 dwarf2_per_objfile->str.s.section = sectp;
dce234bc 2274 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2275 }
43988095
JK
2276 else if (section_is_p (sectp->name, &names->line_str))
2277 {
2278 dwarf2_per_objfile->line_str.s.section = sectp;
2279 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2280 }
3019eac3
DE
2281 else if (section_is_p (sectp->name, &names->addr))
2282 {
049412e3 2283 dwarf2_per_objfile->addr.s.section = sectp;
3019eac3
DE
2284 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2285 }
251d32d9 2286 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2287 {
049412e3 2288 dwarf2_per_objfile->frame.s.section = sectp;
dce234bc 2289 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2290 }
251d32d9 2291 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2292 {
049412e3 2293 dwarf2_per_objfile->eh_frame.s.section = sectp;
dc7650b8 2294 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2295 }
251d32d9 2296 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2297 {
049412e3 2298 dwarf2_per_objfile->ranges.s.section = sectp;
dce234bc 2299 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2300 }
43988095
JK
2301 else if (section_is_p (sectp->name, &names->rnglists))
2302 {
2303 dwarf2_per_objfile->rnglists.s.section = sectp;
2304 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2305 }
251d32d9 2306 else if (section_is_p (sectp->name, &names->types))
348e048f 2307 {
8b70b953
TT
2308 struct dwarf2_section_info type_section;
2309
2310 memset (&type_section, 0, sizeof (type_section));
049412e3 2311 type_section.s.section = sectp;
8b70b953
TT
2312 type_section.size = bfd_get_section_size (sectp);
2313
2314 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2315 &type_section);
348e048f 2316 }
251d32d9 2317 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2318 {
049412e3 2319 dwarf2_per_objfile->gdb_index.s.section = sectp;
9291a0cd
TT
2320 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2321 }
dce234bc 2322
b4e1fd61 2323 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2324 && bfd_section_vma (abfd, sectp) == 0)
2325 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2326}
2327
fceca515
DE
2328/* A helper function that decides whether a section is empty,
2329 or not present. */
9e0ac564
TT
2330
2331static int
19ac8c2e 2332dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2333{
73869dc2
DE
2334 if (section->is_virtual)
2335 return section->size == 0;
049412e3 2336 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2337}
2338
3019eac3
DE
2339/* Read the contents of the section INFO.
2340 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2341 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2342 of the DWO file.
dce234bc 2343 If the section is compressed, uncompress it before returning. */
c906108c 2344
dce234bc
PP
2345static void
2346dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2347{
a32a8923 2348 asection *sectp;
3019eac3 2349 bfd *abfd;
dce234bc 2350 gdb_byte *buf, *retbuf;
c906108c 2351
be391dca
TT
2352 if (info->readin)
2353 return;
dce234bc 2354 info->buffer = NULL;
be391dca 2355 info->readin = 1;
188dd5d6 2356
9e0ac564 2357 if (dwarf2_section_empty_p (info))
dce234bc 2358 return;
c906108c 2359
a32a8923 2360 sectp = get_section_bfd_section (info);
3019eac3 2361
73869dc2
DE
2362 /* If this is a virtual section we need to read in the real one first. */
2363 if (info->is_virtual)
2364 {
2365 struct dwarf2_section_info *containing_section =
2366 get_containing_section (info);
2367
2368 gdb_assert (sectp != NULL);
2369 if ((sectp->flags & SEC_RELOC) != 0)
2370 {
2371 error (_("Dwarf Error: DWP format V2 with relocations is not"
2372 " supported in section %s [in module %s]"),
2373 get_section_name (info), get_section_file_name (info));
2374 }
2375 dwarf2_read_section (objfile, containing_section);
2376 /* Other code should have already caught virtual sections that don't
2377 fit. */
2378 gdb_assert (info->virtual_offset + info->size
2379 <= containing_section->size);
2380 /* If the real section is empty or there was a problem reading the
2381 section we shouldn't get here. */
2382 gdb_assert (containing_section->buffer != NULL);
2383 info->buffer = containing_section->buffer + info->virtual_offset;
2384 return;
2385 }
2386
4bf44c1c
TT
2387 /* If the section has relocations, we must read it ourselves.
2388 Otherwise we attach it to the BFD. */
2389 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2390 {
d521ce57 2391 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2392 return;
dce234bc 2393 }
dce234bc 2394
224c3ddb 2395 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2396 info->buffer = buf;
dce234bc
PP
2397
2398 /* When debugging .o files, we may need to apply relocations; see
2399 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2400 We never compress sections in .o files, so we only need to
2401 try this when the section is not compressed. */
ac8035ab 2402 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2403 if (retbuf != NULL)
2404 {
2405 info->buffer = retbuf;
2406 return;
2407 }
2408
a32a8923
DE
2409 abfd = get_section_bfd_owner (info);
2410 gdb_assert (abfd != NULL);
2411
dce234bc
PP
2412 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2413 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2414 {
2415 error (_("Dwarf Error: Can't read DWARF data"
2416 " in section %s [in module %s]"),
2417 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2418 }
dce234bc
PP
2419}
2420
9e0ac564
TT
2421/* A helper function that returns the size of a section in a safe way.
2422 If you are positive that the section has been read before using the
2423 size, then it is safe to refer to the dwarf2_section_info object's
2424 "size" field directly. In other cases, you must call this
2425 function, because for compressed sections the size field is not set
2426 correctly until the section has been read. */
2427
2428static bfd_size_type
2429dwarf2_section_size (struct objfile *objfile,
2430 struct dwarf2_section_info *info)
2431{
2432 if (!info->readin)
2433 dwarf2_read_section (objfile, info);
2434 return info->size;
2435}
2436
dce234bc 2437/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2438 SECTION_NAME. */
af34e669 2439
dce234bc 2440void
3017a003
TG
2441dwarf2_get_section_info (struct objfile *objfile,
2442 enum dwarf2_section_enum sect,
d521ce57 2443 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2444 bfd_size_type *sizep)
2445{
2446 struct dwarf2_per_objfile *data
9a3c8263
SM
2447 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2448 dwarf2_objfile_data_key);
dce234bc 2449 struct dwarf2_section_info *info;
a3b2a86b
TT
2450
2451 /* We may see an objfile without any DWARF, in which case we just
2452 return nothing. */
2453 if (data == NULL)
2454 {
2455 *sectp = NULL;
2456 *bufp = NULL;
2457 *sizep = 0;
2458 return;
2459 }
3017a003
TG
2460 switch (sect)
2461 {
2462 case DWARF2_DEBUG_FRAME:
2463 info = &data->frame;
2464 break;
2465 case DWARF2_EH_FRAME:
2466 info = &data->eh_frame;
2467 break;
2468 default:
2469 gdb_assert_not_reached ("unexpected section");
2470 }
dce234bc 2471
9e0ac564 2472 dwarf2_read_section (objfile, info);
dce234bc 2473
a32a8923 2474 *sectp = get_section_bfd_section (info);
dce234bc
PP
2475 *bufp = info->buffer;
2476 *sizep = info->size;
2477}
2478
36586728
TT
2479/* A helper function to find the sections for a .dwz file. */
2480
2481static void
2482locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2483{
9a3c8263 2484 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2485
2486 /* Note that we only support the standard ELF names, because .dwz
2487 is ELF-only (at the time of writing). */
2488 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2489 {
049412e3 2490 dwz_file->abbrev.s.section = sectp;
36586728
TT
2491 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2492 }
2493 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2494 {
049412e3 2495 dwz_file->info.s.section = sectp;
36586728
TT
2496 dwz_file->info.size = bfd_get_section_size (sectp);
2497 }
2498 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2499 {
049412e3 2500 dwz_file->str.s.section = sectp;
36586728
TT
2501 dwz_file->str.size = bfd_get_section_size (sectp);
2502 }
2503 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2504 {
049412e3 2505 dwz_file->line.s.section = sectp;
36586728
TT
2506 dwz_file->line.size = bfd_get_section_size (sectp);
2507 }
2508 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2509 {
049412e3 2510 dwz_file->macro.s.section = sectp;
36586728
TT
2511 dwz_file->macro.size = bfd_get_section_size (sectp);
2512 }
2ec9a5e0
TT
2513 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2514 {
049412e3 2515 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2516 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2517 }
36586728
TT
2518}
2519
4db1a1dc
TT
2520/* Open the separate '.dwz' debug file, if needed. Return NULL if
2521 there is no .gnu_debugaltlink section in the file. Error if there
2522 is such a section but the file cannot be found. */
36586728
TT
2523
2524static struct dwz_file *
2525dwarf2_get_dwz_file (void)
2526{
4db1a1dc 2527 char *data;
36586728
TT
2528 struct cleanup *cleanup;
2529 const char *filename;
2530 struct dwz_file *result;
acd13123 2531 bfd_size_type buildid_len_arg;
dc294be5
TT
2532 size_t buildid_len;
2533 bfd_byte *buildid;
36586728
TT
2534
2535 if (dwarf2_per_objfile->dwz_file != NULL)
2536 return dwarf2_per_objfile->dwz_file;
2537
4db1a1dc
TT
2538 bfd_set_error (bfd_error_no_error);
2539 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2540 &buildid_len_arg, &buildid);
4db1a1dc
TT
2541 if (data == NULL)
2542 {
2543 if (bfd_get_error () == bfd_error_no_error)
2544 return NULL;
2545 error (_("could not read '.gnu_debugaltlink' section: %s"),
2546 bfd_errmsg (bfd_get_error ()));
2547 }
36586728 2548 cleanup = make_cleanup (xfree, data);
dc294be5 2549 make_cleanup (xfree, buildid);
36586728 2550
acd13123
TT
2551 buildid_len = (size_t) buildid_len_arg;
2552
f9d83a0b 2553 filename = (const char *) data;
36586728
TT
2554 if (!IS_ABSOLUTE_PATH (filename))
2555 {
4262abfb 2556 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2557 char *rel;
2558
2559 make_cleanup (xfree, abs);
2560 abs = ldirname (abs);
2561 make_cleanup (xfree, abs);
2562
2563 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2564 make_cleanup (xfree, rel);
2565 filename = rel;
2566 }
2567
dc294be5
TT
2568 /* First try the file name given in the section. If that doesn't
2569 work, try to use the build-id instead. */
192b62ce 2570 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2571 if (dwz_bfd != NULL)
36586728 2572 {
192b62ce
TT
2573 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2574 dwz_bfd.release ();
36586728
TT
2575 }
2576
dc294be5
TT
2577 if (dwz_bfd == NULL)
2578 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2579
2580 if (dwz_bfd == NULL)
2581 error (_("could not find '.gnu_debugaltlink' file for %s"),
2582 objfile_name (dwarf2_per_objfile->objfile));
2583
36586728
TT
2584 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2585 struct dwz_file);
192b62ce 2586 result->dwz_bfd = dwz_bfd.release ();
36586728 2587
192b62ce 2588 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2589
2590 do_cleanups (cleanup);
2591
192b62ce 2592 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2593 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2594 return result;
2595}
9291a0cd 2596\f
7b9f3c50
DE
2597/* DWARF quick_symbols_functions support. */
2598
2599/* TUs can share .debug_line entries, and there can be a lot more TUs than
2600 unique line tables, so we maintain a separate table of all .debug_line
2601 derived entries to support the sharing.
2602 All the quick functions need is the list of file names. We discard the
2603 line_header when we're done and don't need to record it here. */
2604struct quick_file_names
2605{
094b34ac
DE
2606 /* The data used to construct the hash key. */
2607 struct stmt_list_hash hash;
7b9f3c50
DE
2608
2609 /* The number of entries in file_names, real_names. */
2610 unsigned int num_file_names;
2611
2612 /* The file names from the line table, after being run through
2613 file_full_name. */
2614 const char **file_names;
2615
2616 /* The file names from the line table after being run through
2617 gdb_realpath. These are computed lazily. */
2618 const char **real_names;
2619};
2620
2621/* When using the index (and thus not using psymtabs), each CU has an
2622 object of this type. This is used to hold information needed by
2623 the various "quick" methods. */
2624struct dwarf2_per_cu_quick_data
2625{
2626 /* The file table. This can be NULL if there was no file table
2627 or it's currently not read in.
2628 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2629 struct quick_file_names *file_names;
2630
2631 /* The corresponding symbol table. This is NULL if symbols for this
2632 CU have not yet been read. */
43f3e411 2633 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2634
2635 /* A temporary mark bit used when iterating over all CUs in
2636 expand_symtabs_matching. */
2637 unsigned int mark : 1;
2638
2639 /* True if we've tried to read the file table and found there isn't one.
2640 There will be no point in trying to read it again next time. */
2641 unsigned int no_file_data : 1;
2642};
2643
094b34ac
DE
2644/* Utility hash function for a stmt_list_hash. */
2645
2646static hashval_t
2647hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2648{
2649 hashval_t v = 0;
2650
2651 if (stmt_list_hash->dwo_unit != NULL)
2652 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2653 v += stmt_list_hash->line_offset.sect_off;
2654 return v;
2655}
2656
2657/* Utility equality function for a stmt_list_hash. */
2658
2659static int
2660eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2661 const struct stmt_list_hash *rhs)
2662{
2663 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2664 return 0;
2665 if (lhs->dwo_unit != NULL
2666 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2667 return 0;
2668
2669 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2670}
2671
7b9f3c50
DE
2672/* Hash function for a quick_file_names. */
2673
2674static hashval_t
2675hash_file_name_entry (const void *e)
2676{
9a3c8263
SM
2677 const struct quick_file_names *file_data
2678 = (const struct quick_file_names *) e;
7b9f3c50 2679
094b34ac 2680 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2681}
2682
2683/* Equality function for a quick_file_names. */
2684
2685static int
2686eq_file_name_entry (const void *a, const void *b)
2687{
9a3c8263
SM
2688 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2689 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2690
094b34ac 2691 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2692}
2693
2694/* Delete function for a quick_file_names. */
2695
2696static void
2697delete_file_name_entry (void *e)
2698{
9a3c8263 2699 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2700 int i;
2701
2702 for (i = 0; i < file_data->num_file_names; ++i)
2703 {
2704 xfree ((void*) file_data->file_names[i]);
2705 if (file_data->real_names)
2706 xfree ((void*) file_data->real_names[i]);
2707 }
2708
2709 /* The space for the struct itself lives on objfile_obstack,
2710 so we don't free it here. */
2711}
2712
2713/* Create a quick_file_names hash table. */
2714
2715static htab_t
2716create_quick_file_names_table (unsigned int nr_initial_entries)
2717{
2718 return htab_create_alloc (nr_initial_entries,
2719 hash_file_name_entry, eq_file_name_entry,
2720 delete_file_name_entry, xcalloc, xfree);
2721}
9291a0cd 2722
918dd910
JK
2723/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2724 have to be created afterwards. You should call age_cached_comp_units after
2725 processing PER_CU->CU. dw2_setup must have been already called. */
2726
2727static void
2728load_cu (struct dwarf2_per_cu_data *per_cu)
2729{
3019eac3 2730 if (per_cu->is_debug_types)
e5fe5e75 2731 load_full_type_unit (per_cu);
918dd910 2732 else
95554aad 2733 load_full_comp_unit (per_cu, language_minimal);
918dd910 2734
cc12ce38
DE
2735 if (per_cu->cu == NULL)
2736 return; /* Dummy CU. */
2dc860c0
DE
2737
2738 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2739}
2740
a0f42c21 2741/* Read in the symbols for PER_CU. */
2fdf6df6 2742
9291a0cd 2743static void
a0f42c21 2744dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2745{
2746 struct cleanup *back_to;
2747
f4dc4d17
DE
2748 /* Skip type_unit_groups, reading the type units they contain
2749 is handled elsewhere. */
2750 if (IS_TYPE_UNIT_GROUP (per_cu))
2751 return;
2752
9291a0cd
TT
2753 back_to = make_cleanup (dwarf2_release_queue, NULL);
2754
95554aad 2755 if (dwarf2_per_objfile->using_index
43f3e411 2756 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2757 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2758 {
2759 queue_comp_unit (per_cu, language_minimal);
2760 load_cu (per_cu);
89e63ee4
DE
2761
2762 /* If we just loaded a CU from a DWO, and we're working with an index
2763 that may badly handle TUs, load all the TUs in that DWO as well.
2764 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2765 if (!per_cu->is_debug_types
cc12ce38 2766 && per_cu->cu != NULL
89e63ee4
DE
2767 && per_cu->cu->dwo_unit != NULL
2768 && dwarf2_per_objfile->index_table != NULL
2769 && dwarf2_per_objfile->index_table->version <= 7
2770 /* DWP files aren't supported yet. */
2771 && get_dwp_file () == NULL)
2772 queue_and_load_all_dwo_tus (per_cu);
95554aad 2773 }
9291a0cd 2774
a0f42c21 2775 process_queue ();
9291a0cd
TT
2776
2777 /* Age the cache, releasing compilation units that have not
2778 been used recently. */
2779 age_cached_comp_units ();
2780
2781 do_cleanups (back_to);
2782}
2783
2784/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2785 the objfile from which this CU came. Returns the resulting symbol
2786 table. */
2fdf6df6 2787
43f3e411 2788static struct compunit_symtab *
a0f42c21 2789dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2790{
95554aad 2791 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2792 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2793 {
2794 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2795 increment_reading_symtab ();
a0f42c21 2796 dw2_do_instantiate_symtab (per_cu);
95554aad 2797 process_cu_includes ();
9291a0cd
TT
2798 do_cleanups (back_to);
2799 }
f194fefb 2800
43f3e411 2801 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2802}
2803
8832e7e3 2804/* Return the CU/TU given its index.
f4dc4d17
DE
2805
2806 This is intended for loops like:
2807
2808 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2809 + dwarf2_per_objfile->n_type_units); ++i)
2810 {
8832e7e3 2811 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2812
2813 ...;
2814 }
2815*/
2fdf6df6 2816
1fd400ff 2817static struct dwarf2_per_cu_data *
8832e7e3 2818dw2_get_cutu (int index)
1fd400ff
TT
2819{
2820 if (index >= dwarf2_per_objfile->n_comp_units)
2821 {
f4dc4d17 2822 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2823 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2824 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2825 }
2826
2827 return dwarf2_per_objfile->all_comp_units[index];
2828}
2829
8832e7e3
DE
2830/* Return the CU given its index.
2831 This differs from dw2_get_cutu in that it's for when you know INDEX
2832 refers to a CU. */
f4dc4d17
DE
2833
2834static struct dwarf2_per_cu_data *
8832e7e3 2835dw2_get_cu (int index)
f4dc4d17 2836{
8832e7e3 2837 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2838
1fd400ff
TT
2839 return dwarf2_per_objfile->all_comp_units[index];
2840}
2841
2ec9a5e0
TT
2842/* A helper for create_cus_from_index that handles a given list of
2843 CUs. */
2fdf6df6 2844
74a0d9f6 2845static void
2ec9a5e0
TT
2846create_cus_from_index_list (struct objfile *objfile,
2847 const gdb_byte *cu_list, offset_type n_elements,
2848 struct dwarf2_section_info *section,
2849 int is_dwz,
2850 int base_offset)
9291a0cd
TT
2851{
2852 offset_type i;
9291a0cd 2853
2ec9a5e0 2854 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2855 {
2856 struct dwarf2_per_cu_data *the_cu;
2857 ULONGEST offset, length;
2858
74a0d9f6
JK
2859 gdb_static_assert (sizeof (ULONGEST) >= 8);
2860 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2861 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2862 cu_list += 2 * 8;
2863
2864 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2865 struct dwarf2_per_cu_data);
b64f50a1 2866 the_cu->offset.sect_off = offset;
9291a0cd
TT
2867 the_cu->length = length;
2868 the_cu->objfile = objfile;
8a0459fd 2869 the_cu->section = section;
9291a0cd
TT
2870 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2871 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2872 the_cu->is_dwz = is_dwz;
2873 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2874 }
9291a0cd
TT
2875}
2876
2ec9a5e0 2877/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2878 the CU objects for this objfile. */
2ec9a5e0 2879
74a0d9f6 2880static void
2ec9a5e0
TT
2881create_cus_from_index (struct objfile *objfile,
2882 const gdb_byte *cu_list, offset_type cu_list_elements,
2883 const gdb_byte *dwz_list, offset_type dwz_elements)
2884{
2885 struct dwz_file *dwz;
2886
2887 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
2888 dwarf2_per_objfile->all_comp_units =
2889 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2890 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 2891
74a0d9f6
JK
2892 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2893 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2894
2895 if (dwz_elements == 0)
74a0d9f6 2896 return;
2ec9a5e0
TT
2897
2898 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2899 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2900 cu_list_elements / 2);
2ec9a5e0
TT
2901}
2902
1fd400ff 2903/* Create the signatured type hash table from the index. */
673bfd45 2904
74a0d9f6 2905static void
673bfd45 2906create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2907 struct dwarf2_section_info *section,
673bfd45
DE
2908 const gdb_byte *bytes,
2909 offset_type elements)
1fd400ff
TT
2910{
2911 offset_type i;
673bfd45 2912 htab_t sig_types_hash;
1fd400ff 2913
6aa5f3a6
DE
2914 dwarf2_per_objfile->n_type_units
2915 = dwarf2_per_objfile->n_allocated_type_units
2916 = elements / 3;
8d749320
SM
2917 dwarf2_per_objfile->all_type_units =
2918 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 2919
673bfd45 2920 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2921
2922 for (i = 0; i < elements; i += 3)
2923 {
52dc124a
DE
2924 struct signatured_type *sig_type;
2925 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2926 void **slot;
2927
74a0d9f6
JK
2928 gdb_static_assert (sizeof (ULONGEST) >= 8);
2929 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2930 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2931 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2932 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2933 bytes += 3 * 8;
2934
52dc124a 2935 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2936 struct signatured_type);
52dc124a 2937 sig_type->signature = signature;
3019eac3
DE
2938 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2939 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2940 sig_type->per_cu.section = section;
52dc124a
DE
2941 sig_type->per_cu.offset.sect_off = offset;
2942 sig_type->per_cu.objfile = objfile;
2943 sig_type->per_cu.v.quick
1fd400ff
TT
2944 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2945 struct dwarf2_per_cu_quick_data);
2946
52dc124a
DE
2947 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2948 *slot = sig_type;
1fd400ff 2949
b4dd5633 2950 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2951 }
2952
673bfd45 2953 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2954}
2955
9291a0cd
TT
2956/* Read the address map data from the mapped index, and use it to
2957 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2958
9291a0cd
TT
2959static void
2960create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2961{
3e29f34a 2962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd
TT
2963 const gdb_byte *iter, *end;
2964 struct obstack temp_obstack;
2965 struct addrmap *mutable_map;
2966 struct cleanup *cleanup;
2967 CORE_ADDR baseaddr;
2968
2969 obstack_init (&temp_obstack);
2970 cleanup = make_cleanup_obstack_free (&temp_obstack);
2971 mutable_map = addrmap_create_mutable (&temp_obstack);
2972
2973 iter = index->address_table;
2974 end = iter + index->address_table_size;
2975
2976 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2977
2978 while (iter < end)
2979 {
2980 ULONGEST hi, lo, cu_index;
2981 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2982 iter += 8;
2983 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2984 iter += 8;
2985 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2986 iter += 4;
f652bce2 2987
24a55014 2988 if (lo > hi)
f652bce2 2989 {
24a55014
DE
2990 complaint (&symfile_complaints,
2991 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2992 hex_string (lo), hex_string (hi));
24a55014 2993 continue;
f652bce2 2994 }
24a55014
DE
2995
2996 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2997 {
2998 complaint (&symfile_complaints,
2999 _(".gdb_index address table has invalid CU number %u"),
3000 (unsigned) cu_index);
24a55014 3001 continue;
f652bce2 3002 }
24a55014 3003
3e29f34a
MR
3004 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3005 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3006 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3007 }
3008
3009 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3010 &objfile->objfile_obstack);
3011 do_cleanups (cleanup);
3012}
3013
59d7bcaf
JK
3014/* The hash function for strings in the mapped index. This is the same as
3015 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3016 implementation. This is necessary because the hash function is tied to the
3017 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3018 SYMBOL_HASH_NEXT.
3019
3020 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3021
9291a0cd 3022static hashval_t
559a7a62 3023mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3024{
3025 const unsigned char *str = (const unsigned char *) p;
3026 hashval_t r = 0;
3027 unsigned char c;
3028
3029 while ((c = *str++) != 0)
559a7a62
JK
3030 {
3031 if (index_version >= 5)
3032 c = tolower (c);
3033 r = r * 67 + c - 113;
3034 }
9291a0cd
TT
3035
3036 return r;
3037}
3038
3039/* Find a slot in the mapped index INDEX for the object named NAME.
3040 If NAME is found, set *VEC_OUT to point to the CU vector in the
3041 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3042
9291a0cd
TT
3043static int
3044find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3045 offset_type **vec_out)
3046{
0cf03b49
JK
3047 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3048 offset_type hash;
9291a0cd 3049 offset_type slot, step;
559a7a62 3050 int (*cmp) (const char *, const char *);
9291a0cd 3051
0cf03b49 3052 if (current_language->la_language == language_cplus
45280282
IB
3053 || current_language->la_language == language_fortran
3054 || current_language->la_language == language_d)
0cf03b49
JK
3055 {
3056 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3057 not contain any. */
a8719064 3058
72998fb3 3059 if (strchr (name, '(') != NULL)
0cf03b49 3060 {
72998fb3 3061 char *without_params = cp_remove_params (name);
0cf03b49 3062
72998fb3
DE
3063 if (without_params != NULL)
3064 {
3065 make_cleanup (xfree, without_params);
3066 name = without_params;
3067 }
0cf03b49
JK
3068 }
3069 }
3070
559a7a62 3071 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3072 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3073 simulate our NAME being searched is also lowercased. */
3074 hash = mapped_index_string_hash ((index->version == 4
3075 && case_sensitivity == case_sensitive_off
3076 ? 5 : index->version),
3077 name);
3078
3876f04e
DE
3079 slot = hash & (index->symbol_table_slots - 1);
3080 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3081 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3082
3083 for (;;)
3084 {
3085 /* Convert a slot number to an offset into the table. */
3086 offset_type i = 2 * slot;
3087 const char *str;
3876f04e 3088 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3089 {
3090 do_cleanups (back_to);
3091 return 0;
3092 }
9291a0cd 3093
3876f04e 3094 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3095 if (!cmp (name, str))
9291a0cd
TT
3096 {
3097 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3098 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3099 do_cleanups (back_to);
9291a0cd
TT
3100 return 1;
3101 }
3102
3876f04e 3103 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3104 }
3105}
3106
2ec9a5e0
TT
3107/* A helper function that reads the .gdb_index from SECTION and fills
3108 in MAP. FILENAME is the name of the file containing the section;
3109 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3110 ok to use deprecated sections.
3111
3112 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3113 out parameters that are filled in with information about the CU and
3114 TU lists in the section.
3115
3116 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3117
9291a0cd 3118static int
2ec9a5e0
TT
3119read_index_from_section (struct objfile *objfile,
3120 const char *filename,
3121 int deprecated_ok,
3122 struct dwarf2_section_info *section,
3123 struct mapped_index *map,
3124 const gdb_byte **cu_list,
3125 offset_type *cu_list_elements,
3126 const gdb_byte **types_list,
3127 offset_type *types_list_elements)
9291a0cd 3128{
948f8e3d 3129 const gdb_byte *addr;
2ec9a5e0 3130 offset_type version;
b3b272e1 3131 offset_type *metadata;
1fd400ff 3132 int i;
9291a0cd 3133
2ec9a5e0 3134 if (dwarf2_section_empty_p (section))
9291a0cd 3135 return 0;
82430852
JK
3136
3137 /* Older elfutils strip versions could keep the section in the main
3138 executable while splitting it for the separate debug info file. */
a32a8923 3139 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3140 return 0;
3141
2ec9a5e0 3142 dwarf2_read_section (objfile, section);
9291a0cd 3143
2ec9a5e0 3144 addr = section->buffer;
9291a0cd 3145 /* Version check. */
1fd400ff 3146 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3147 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3148 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3149 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3150 indices. */
831adc1f 3151 if (version < 4)
481860b3
GB
3152 {
3153 static int warning_printed = 0;
3154 if (!warning_printed)
3155 {
3156 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3157 filename);
481860b3
GB
3158 warning_printed = 1;
3159 }
3160 return 0;
3161 }
3162 /* Index version 4 uses a different hash function than index version
3163 5 and later.
3164
3165 Versions earlier than 6 did not emit psymbols for inlined
3166 functions. Using these files will cause GDB not to be able to
3167 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3168 indices unless the user has done
3169 "set use-deprecated-index-sections on". */
2ec9a5e0 3170 if (version < 6 && !deprecated_ok)
481860b3
GB
3171 {
3172 static int warning_printed = 0;
3173 if (!warning_printed)
3174 {
e615022a
DE
3175 warning (_("\
3176Skipping deprecated .gdb_index section in %s.\n\
3177Do \"set use-deprecated-index-sections on\" before the file is read\n\
3178to use the section anyway."),
2ec9a5e0 3179 filename);
481860b3
GB
3180 warning_printed = 1;
3181 }
3182 return 0;
3183 }
796a7ff8 3184 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3185 of the TU (for symbols coming from TUs),
3186 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3187 Plus gold-generated indices can have duplicate entries for global symbols,
3188 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3189 These are just performance bugs, and we can't distinguish gdb-generated
3190 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3191
481860b3 3192 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3193 longer backward compatible. */
796a7ff8 3194 if (version > 8)
594e8718 3195 return 0;
9291a0cd 3196
559a7a62 3197 map->version = version;
2ec9a5e0 3198 map->total_size = section->size;
9291a0cd
TT
3199
3200 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3201
3202 i = 0;
2ec9a5e0
TT
3203 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3204 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3205 / 8);
1fd400ff
TT
3206 ++i;
3207
2ec9a5e0
TT
3208 *types_list = addr + MAYBE_SWAP (metadata[i]);
3209 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3210 - MAYBE_SWAP (metadata[i]))
3211 / 8);
987d643c 3212 ++i;
1fd400ff
TT
3213
3214 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3215 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3216 - MAYBE_SWAP (metadata[i]));
3217 ++i;
3218
3876f04e
DE
3219 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3220 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3221 - MAYBE_SWAP (metadata[i]))
3222 / (2 * sizeof (offset_type)));
1fd400ff 3223 ++i;
9291a0cd 3224
f9d83a0b 3225 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3226
2ec9a5e0
TT
3227 return 1;
3228}
3229
3230
3231/* Read the index file. If everything went ok, initialize the "quick"
3232 elements of all the CUs and return 1. Otherwise, return 0. */
3233
3234static int
3235dwarf2_read_index (struct objfile *objfile)
3236{
3237 struct mapped_index local_map, *map;
3238 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3239 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3240 struct dwz_file *dwz;
2ec9a5e0 3241
4262abfb 3242 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3243 use_deprecated_index_sections,
3244 &dwarf2_per_objfile->gdb_index, &local_map,
3245 &cu_list, &cu_list_elements,
3246 &types_list, &types_list_elements))
3247 return 0;
3248
0fefef59 3249 /* Don't use the index if it's empty. */
2ec9a5e0 3250 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3251 return 0;
3252
2ec9a5e0
TT
3253 /* If there is a .dwz file, read it so we can get its CU list as
3254 well. */
4db1a1dc
TT
3255 dwz = dwarf2_get_dwz_file ();
3256 if (dwz != NULL)
2ec9a5e0 3257 {
2ec9a5e0
TT
3258 struct mapped_index dwz_map;
3259 const gdb_byte *dwz_types_ignore;
3260 offset_type dwz_types_elements_ignore;
3261
3262 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3263 1,
3264 &dwz->gdb_index, &dwz_map,
3265 &dwz_list, &dwz_list_elements,
3266 &dwz_types_ignore,
3267 &dwz_types_elements_ignore))
3268 {
3269 warning (_("could not read '.gdb_index' section from %s; skipping"),
3270 bfd_get_filename (dwz->dwz_bfd));
3271 return 0;
3272 }
3273 }
3274
74a0d9f6
JK
3275 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3276 dwz_list_elements);
1fd400ff 3277
8b70b953
TT
3278 if (types_list_elements)
3279 {
3280 struct dwarf2_section_info *section;
3281
3282 /* We can only handle a single .debug_types when we have an
3283 index. */
3284 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3285 return 0;
3286
3287 section = VEC_index (dwarf2_section_info_def,
3288 dwarf2_per_objfile->types, 0);
3289
74a0d9f6
JK
3290 create_signatured_type_table_from_index (objfile, section, types_list,
3291 types_list_elements);
8b70b953 3292 }
9291a0cd 3293
2ec9a5e0
TT
3294 create_addrmap_from_index (objfile, &local_map);
3295
8d749320 3296 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3297 *map = local_map;
9291a0cd
TT
3298
3299 dwarf2_per_objfile->index_table = map;
3300 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3301 dwarf2_per_objfile->quick_file_names_table =
3302 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3303
3304 return 1;
3305}
3306
3307/* A helper for the "quick" functions which sets the global
3308 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3309
9291a0cd
TT
3310static void
3311dw2_setup (struct objfile *objfile)
3312{
9a3c8263
SM
3313 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3314 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3315 gdb_assert (dwarf2_per_objfile);
3316}
3317
dee91e82 3318/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3319
dee91e82
DE
3320static void
3321dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3322 const gdb_byte *info_ptr,
dee91e82
DE
3323 struct die_info *comp_unit_die,
3324 int has_children,
3325 void *data)
9291a0cd 3326{
dee91e82
DE
3327 struct dwarf2_cu *cu = reader->cu;
3328 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3329 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3330 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 3331 struct line_header *lh;
9291a0cd 3332 struct attribute *attr;
dee91e82 3333 int i;
15d034d0 3334 const char *name, *comp_dir;
7b9f3c50
DE
3335 void **slot;
3336 struct quick_file_names *qfn;
3337 unsigned int line_offset;
9291a0cd 3338
0186c6a7
DE
3339 gdb_assert (! this_cu->is_debug_types);
3340
07261596
TT
3341 /* Our callers never want to match partial units -- instead they
3342 will match the enclosing full CU. */
3343 if (comp_unit_die->tag == DW_TAG_partial_unit)
3344 {
3345 this_cu->v.quick->no_file_data = 1;
3346 return;
3347 }
3348
0186c6a7 3349 lh_cu = this_cu;
7b9f3c50
DE
3350 lh = NULL;
3351 slot = NULL;
3352 line_offset = 0;
dee91e82
DE
3353
3354 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3355 if (attr)
3356 {
7b9f3c50
DE
3357 struct quick_file_names find_entry;
3358
3359 line_offset = DW_UNSND (attr);
3360
3361 /* We may have already read in this line header (TU line header sharing).
3362 If we have we're done. */
094b34ac
DE
3363 find_entry.hash.dwo_unit = cu->dwo_unit;
3364 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3365 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3366 &find_entry, INSERT);
3367 if (*slot != NULL)
3368 {
9a3c8263 3369 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3370 return;
7b9f3c50
DE
3371 }
3372
3019eac3 3373 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3374 }
3375 if (lh == NULL)
3376 {
094b34ac 3377 lh_cu->v.quick->no_file_data = 1;
dee91e82 3378 return;
9291a0cd
TT
3379 }
3380
8d749320 3381 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac
DE
3382 qfn->hash.dwo_unit = cu->dwo_unit;
3383 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3384 gdb_assert (slot != NULL);
3385 *slot = qfn;
9291a0cd 3386
dee91e82 3387 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 3388
7b9f3c50 3389 qfn->num_file_names = lh->num_file_names;
8d749320
SM
3390 qfn->file_names =
3391 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
9291a0cd 3392 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
3393 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
3394 qfn->real_names = NULL;
9291a0cd 3395
7b9f3c50 3396 free_line_header (lh);
7b9f3c50 3397
094b34ac 3398 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3399}
3400
3401/* A helper for the "quick" functions which attempts to read the line
3402 table for THIS_CU. */
3403
3404static struct quick_file_names *
e4a48d9d 3405dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3406{
0186c6a7
DE
3407 /* This should never be called for TUs. */
3408 gdb_assert (! this_cu->is_debug_types);
3409 /* Nor type unit groups. */
3410 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3411
dee91e82
DE
3412 if (this_cu->v.quick->file_names != NULL)
3413 return this_cu->v.quick->file_names;
3414 /* If we know there is no line data, no point in looking again. */
3415 if (this_cu->v.quick->no_file_data)
3416 return NULL;
3417
0186c6a7 3418 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3419
3420 if (this_cu->v.quick->no_file_data)
3421 return NULL;
3422 return this_cu->v.quick->file_names;
9291a0cd
TT
3423}
3424
3425/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3426 real path for a given file name from the line table. */
2fdf6df6 3427
9291a0cd 3428static const char *
7b9f3c50
DE
3429dw2_get_real_path (struct objfile *objfile,
3430 struct quick_file_names *qfn, int index)
9291a0cd 3431{
7b9f3c50
DE
3432 if (qfn->real_names == NULL)
3433 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3434 qfn->num_file_names, const char *);
9291a0cd 3435
7b9f3c50
DE
3436 if (qfn->real_names[index] == NULL)
3437 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3438
7b9f3c50 3439 return qfn->real_names[index];
9291a0cd
TT
3440}
3441
3442static struct symtab *
3443dw2_find_last_source_symtab (struct objfile *objfile)
3444{
43f3e411 3445 struct compunit_symtab *cust;
9291a0cd 3446 int index;
ae2de4f8 3447
9291a0cd
TT
3448 dw2_setup (objfile);
3449 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3450 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3451 if (cust == NULL)
3452 return NULL;
3453 return compunit_primary_filetab (cust);
9291a0cd
TT
3454}
3455
7b9f3c50
DE
3456/* Traversal function for dw2_forget_cached_source_info. */
3457
3458static int
3459dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3460{
7b9f3c50 3461 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3462
7b9f3c50 3463 if (file_data->real_names)
9291a0cd 3464 {
7b9f3c50 3465 int i;
9291a0cd 3466
7b9f3c50 3467 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3468 {
7b9f3c50
DE
3469 xfree ((void*) file_data->real_names[i]);
3470 file_data->real_names[i] = NULL;
9291a0cd
TT
3471 }
3472 }
7b9f3c50
DE
3473
3474 return 1;
3475}
3476
3477static void
3478dw2_forget_cached_source_info (struct objfile *objfile)
3479{
3480 dw2_setup (objfile);
3481
3482 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3483 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3484}
3485
f8eba3c6
TT
3486/* Helper function for dw2_map_symtabs_matching_filename that expands
3487 the symtabs and calls the iterator. */
3488
3489static int
3490dw2_map_expand_apply (struct objfile *objfile,
3491 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3492 const char *name, const char *real_path,
f8eba3c6
TT
3493 int (*callback) (struct symtab *, void *),
3494 void *data)
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
f5b95b50 3506 return iterate_over_some_symtabs (name, real_path, callback, data,
43f3e411 3507 objfile->compunit_symtabs, last_made);
f8eba3c6
TT
3508}
3509
3510/* Implementation of the map_symtabs_matching_filename method. */
3511
9291a0cd 3512static int
f8eba3c6 3513dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3514 const char *real_path,
f8eba3c6
TT
3515 int (*callback) (struct symtab *, void *),
3516 void *data)
9291a0cd
TT
3517{
3518 int i;
c011a4f4 3519 const char *name_basename = lbasename (name);
9291a0cd
TT
3520
3521 dw2_setup (objfile);
ae2de4f8 3522
848e3e78
DE
3523 /* The rule is CUs specify all the files, including those used by
3524 any TU, so there's no need to scan TUs here. */
f4dc4d17 3525
848e3e78 3526 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3527 {
3528 int j;
8832e7e3 3529 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3530 struct quick_file_names *file_data;
9291a0cd 3531
3d7bb9d9 3532 /* We only need to look at symtabs not already expanded. */
43f3e411 3533 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3534 continue;
3535
e4a48d9d 3536 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3537 if (file_data == NULL)
9291a0cd
TT
3538 continue;
3539
7b9f3c50 3540 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3541 {
7b9f3c50 3542 const char *this_name = file_data->file_names[j];
da235a7c 3543 const char *this_real_name;
9291a0cd 3544
af529f8f 3545 if (compare_filenames_for_search (this_name, name))
9291a0cd 3546 {
f5b95b50 3547 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3548 callback, data))
3549 return 1;
288e77a7 3550 continue;
4aac40c8 3551 }
9291a0cd 3552
c011a4f4
DE
3553 /* Before we invoke realpath, which can get expensive when many
3554 files are involved, do a quick comparison of the basenames. */
3555 if (! basenames_may_differ
3556 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3557 continue;
3558
da235a7c
JK
3559 this_real_name = dw2_get_real_path (objfile, file_data, j);
3560 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3561 {
da235a7c
JK
3562 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3563 callback, data))
3564 return 1;
288e77a7 3565 continue;
da235a7c 3566 }
9291a0cd 3567
da235a7c
JK
3568 if (real_path != NULL)
3569 {
af529f8f
JK
3570 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3571 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3572 if (this_real_name != NULL
af529f8f 3573 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3574 {
f5b95b50 3575 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3576 callback, data))
3577 return 1;
288e77a7 3578 continue;
9291a0cd
TT
3579 }
3580 }
3581 }
3582 }
3583
9291a0cd
TT
3584 return 0;
3585}
3586
da51c347
DE
3587/* Struct used to manage iterating over all CUs looking for a symbol. */
3588
3589struct dw2_symtab_iterator
9291a0cd 3590{
da51c347
DE
3591 /* The internalized form of .gdb_index. */
3592 struct mapped_index *index;
3593 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3594 int want_specific_block;
3595 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3596 Unused if !WANT_SPECIFIC_BLOCK. */
3597 int block_index;
3598 /* The kind of symbol we're looking for. */
3599 domain_enum domain;
3600 /* The list of CUs from the index entry of the symbol,
3601 or NULL if not found. */
3602 offset_type *vec;
3603 /* The next element in VEC to look at. */
3604 int next;
3605 /* The number of elements in VEC, or zero if there is no match. */
3606 int length;
8943b874
DE
3607 /* Have we seen a global version of the symbol?
3608 If so we can ignore all further global instances.
3609 This is to work around gold/15646, inefficient gold-generated
3610 indices. */
3611 int global_seen;
da51c347 3612};
9291a0cd 3613
da51c347
DE
3614/* Initialize the index symtab iterator ITER.
3615 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3616 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3617
9291a0cd 3618static void
da51c347
DE
3619dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3620 struct mapped_index *index,
3621 int want_specific_block,
3622 int block_index,
3623 domain_enum domain,
3624 const char *name)
3625{
3626 iter->index = index;
3627 iter->want_specific_block = want_specific_block;
3628 iter->block_index = block_index;
3629 iter->domain = domain;
3630 iter->next = 0;
8943b874 3631 iter->global_seen = 0;
da51c347
DE
3632
3633 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3634 iter->length = MAYBE_SWAP (*iter->vec);
3635 else
3636 {
3637 iter->vec = NULL;
3638 iter->length = 0;
3639 }
3640}
3641
3642/* Return the next matching CU or NULL if there are no more. */
3643
3644static struct dwarf2_per_cu_data *
3645dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3646{
3647 for ( ; iter->next < iter->length; ++iter->next)
3648 {
3649 offset_type cu_index_and_attrs =
3650 MAYBE_SWAP (iter->vec[iter->next + 1]);
3651 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3652 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3653 int want_static = iter->block_index != GLOBAL_BLOCK;
3654 /* This value is only valid for index versions >= 7. */
3655 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3656 gdb_index_symbol_kind symbol_kind =
3657 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3658 /* Only check the symbol attributes if they're present.
3659 Indices prior to version 7 don't record them,
3660 and indices >= 7 may elide them for certain symbols
3661 (gold does this). */
3662 int attrs_valid =
3663 (iter->index->version >= 7
3664 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3665
3190f0c6
DE
3666 /* Don't crash on bad data. */
3667 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3668 + dwarf2_per_objfile->n_type_units))
3669 {
3670 complaint (&symfile_complaints,
3671 _(".gdb_index entry has bad CU index"
4262abfb
JK
3672 " [in module %s]"),
3673 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3674 continue;
3675 }
3676
8832e7e3 3677 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3678
da51c347 3679 /* Skip if already read in. */
43f3e411 3680 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3681 continue;
3682
8943b874
DE
3683 /* Check static vs global. */
3684 if (attrs_valid)
3685 {
3686 if (iter->want_specific_block
3687 && want_static != is_static)
3688 continue;
3689 /* Work around gold/15646. */
3690 if (!is_static && iter->global_seen)
3691 continue;
3692 if (!is_static)
3693 iter->global_seen = 1;
3694 }
da51c347
DE
3695
3696 /* Only check the symbol's kind if it has one. */
3697 if (attrs_valid)
3698 {
3699 switch (iter->domain)
3700 {
3701 case VAR_DOMAIN:
3702 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3703 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3704 /* Some types are also in VAR_DOMAIN. */
3705 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3706 continue;
3707 break;
3708 case STRUCT_DOMAIN:
3709 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3710 continue;
3711 break;
3712 case LABEL_DOMAIN:
3713 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3714 continue;
3715 break;
3716 default:
3717 break;
3718 }
3719 }
3720
3721 ++iter->next;
3722 return per_cu;
3723 }
3724
3725 return NULL;
3726}
3727
43f3e411 3728static struct compunit_symtab *
da51c347
DE
3729dw2_lookup_symbol (struct objfile *objfile, int block_index,
3730 const char *name, domain_enum domain)
9291a0cd 3731{
43f3e411 3732 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3733 struct mapped_index *index;
3734
9291a0cd
TT
3735 dw2_setup (objfile);
3736
156942c7
DE
3737 index = dwarf2_per_objfile->index_table;
3738
da51c347 3739 /* index is NULL if OBJF_READNOW. */
156942c7 3740 if (index)
9291a0cd 3741 {
da51c347
DE
3742 struct dw2_symtab_iterator iter;
3743 struct dwarf2_per_cu_data *per_cu;
3744
3745 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3746
da51c347 3747 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3748 {
b2e2f908 3749 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3750 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3751 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3752 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3753
b2e2f908
DE
3754 sym = block_find_symbol (block, name, domain,
3755 block_find_non_opaque_type_preferred,
3756 &with_opaque);
3757
da51c347
DE
3758 /* Some caution must be observed with overloaded functions
3759 and methods, since the index will not contain any overload
3760 information (but NAME might contain it). */
da51c347 3761
b2e2f908
DE
3762 if (sym != NULL
3763 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3764 return stab;
3765 if (with_opaque != NULL
3766 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3767 stab_best = stab;
da51c347
DE
3768
3769 /* Keep looking through other CUs. */
9291a0cd
TT
3770 }
3771 }
9291a0cd 3772
da51c347 3773 return stab_best;
9291a0cd
TT
3774}
3775
3776static void
3777dw2_print_stats (struct objfile *objfile)
3778{
e4a48d9d 3779 int i, total, count;
9291a0cd
TT
3780
3781 dw2_setup (objfile);
e4a48d9d 3782 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3783 count = 0;
e4a48d9d 3784 for (i = 0; i < total; ++i)
9291a0cd 3785 {
8832e7e3 3786 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3787
43f3e411 3788 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3789 ++count;
3790 }
e4a48d9d 3791 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3792 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3793}
3794
779bd270
DE
3795/* This dumps minimal information about the index.
3796 It is called via "mt print objfiles".
3797 One use is to verify .gdb_index has been loaded by the
3798 gdb.dwarf2/gdb-index.exp testcase. */
3799
9291a0cd
TT
3800static void
3801dw2_dump (struct objfile *objfile)
3802{
779bd270
DE
3803 dw2_setup (objfile);
3804 gdb_assert (dwarf2_per_objfile->using_index);
3805 printf_filtered (".gdb_index:");
3806 if (dwarf2_per_objfile->index_table != NULL)
3807 {
3808 printf_filtered (" version %d\n",
3809 dwarf2_per_objfile->index_table->version);
3810 }
3811 else
3812 printf_filtered (" faked for \"readnow\"\n");
3813 printf_filtered ("\n");
9291a0cd
TT
3814}
3815
3816static void
3189cb12
DE
3817dw2_relocate (struct objfile *objfile,
3818 const struct section_offsets *new_offsets,
3819 const struct section_offsets *delta)
9291a0cd
TT
3820{
3821 /* There's nothing to relocate here. */
3822}
3823
3824static void
3825dw2_expand_symtabs_for_function (struct objfile *objfile,
3826 const char *func_name)
3827{
da51c347
DE
3828 struct mapped_index *index;
3829
3830 dw2_setup (objfile);
3831
3832 index = dwarf2_per_objfile->index_table;
3833
3834 /* index is NULL if OBJF_READNOW. */
3835 if (index)
3836 {
3837 struct dw2_symtab_iterator iter;
3838 struct dwarf2_per_cu_data *per_cu;
3839
3840 /* Note: It doesn't matter what we pass for block_index here. */
3841 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3842 func_name);
3843
3844 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3845 dw2_instantiate_symtab (per_cu);
3846 }
9291a0cd
TT
3847}
3848
3849static void
3850dw2_expand_all_symtabs (struct objfile *objfile)
3851{
3852 int i;
3853
3854 dw2_setup (objfile);
1fd400ff
TT
3855
3856 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3857 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3858 {
8832e7e3 3859 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3860
a0f42c21 3861 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3862 }
3863}
3864
3865static void
652a8996
JK
3866dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3867 const char *fullname)
9291a0cd
TT
3868{
3869 int i;
3870
3871 dw2_setup (objfile);
d4637a04
DE
3872
3873 /* We don't need to consider type units here.
3874 This is only called for examining code, e.g. expand_line_sal.
3875 There can be an order of magnitude (or more) more type units
3876 than comp units, and we avoid them if we can. */
3877
3878 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3879 {
3880 int j;
8832e7e3 3881 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3882 struct quick_file_names *file_data;
9291a0cd 3883
3d7bb9d9 3884 /* We only need to look at symtabs not already expanded. */
43f3e411 3885 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3886 continue;
3887
e4a48d9d 3888 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3889 if (file_data == NULL)
9291a0cd
TT
3890 continue;
3891
7b9f3c50 3892 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3893 {
652a8996
JK
3894 const char *this_fullname = file_data->file_names[j];
3895
3896 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3897 {
a0f42c21 3898 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3899 break;
3900 }
3901 }
3902 }
3903}
3904
9291a0cd 3905static void
ade7ed9e 3906dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 3907 const char * name, domain_enum domain,
ade7ed9e 3908 int global,
40658b94
PH
3909 int (*callback) (struct block *,
3910 struct symbol *, void *),
2edb89d3
JK
3911 void *data, symbol_compare_ftype *match,
3912 symbol_compare_ftype *ordered_compare)
9291a0cd 3913{
40658b94 3914 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3915 current language is Ada for a non-Ada objfile using GNU index. As Ada
3916 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3917}
3918
3919static void
f8eba3c6
TT
3920dw2_expand_symtabs_matching
3921 (struct objfile *objfile,
206f2a57
DE
3922 expand_symtabs_file_matcher_ftype *file_matcher,
3923 expand_symtabs_symbol_matcher_ftype *symbol_matcher,
276d885b 3924 expand_symtabs_exp_notify_ftype *expansion_notify,
f8eba3c6
TT
3925 enum search_domain kind,
3926 void *data)
9291a0cd
TT
3927{
3928 int i;
3929 offset_type iter;
4b5246aa 3930 struct mapped_index *index;
9291a0cd
TT
3931
3932 dw2_setup (objfile);
ae2de4f8
DE
3933
3934 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3935 if (!dwarf2_per_objfile->index_table)
3936 return;
4b5246aa 3937 index = dwarf2_per_objfile->index_table;
9291a0cd 3938
7b08b9eb 3939 if (file_matcher != NULL)
24c79950 3940 {
fc4007c9
TT
3941 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3942 htab_eq_pointer,
3943 NULL, xcalloc, xfree));
3944 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3945 htab_eq_pointer,
3946 NULL, xcalloc, xfree));
24c79950 3947
848e3e78
DE
3948 /* The rule is CUs specify all the files, including those used by
3949 any TU, so there's no need to scan TUs here. */
3950
3951 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3952 {
3953 int j;
8832e7e3 3954 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
3955 struct quick_file_names *file_data;
3956 void **slot;
7b08b9eb 3957
61d96d7e
DE
3958 QUIT;
3959
24c79950 3960 per_cu->v.quick->mark = 0;
3d7bb9d9 3961
24c79950 3962 /* We only need to look at symtabs not already expanded. */
43f3e411 3963 if (per_cu->v.quick->compunit_symtab)
24c79950 3964 continue;
7b08b9eb 3965
e4a48d9d 3966 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3967 if (file_data == NULL)
3968 continue;
7b08b9eb 3969
fc4007c9 3970 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 3971 continue;
fc4007c9 3972 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
3973 {
3974 per_cu->v.quick->mark = 1;
3975 continue;
3976 }
3977
3978 for (j = 0; j < file_data->num_file_names; ++j)
3979 {
da235a7c
JK
3980 const char *this_real_name;
3981
fbd9ab74 3982 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3983 {
3984 per_cu->v.quick->mark = 1;
3985 break;
3986 }
da235a7c
JK
3987
3988 /* Before we invoke realpath, which can get expensive when many
3989 files are involved, do a quick comparison of the basenames. */
3990 if (!basenames_may_differ
3991 && !file_matcher (lbasename (file_data->file_names[j]),
3992 data, 1))
3993 continue;
3994
3995 this_real_name = dw2_get_real_path (objfile, file_data, j);
3996 if (file_matcher (this_real_name, data, 0))
3997 {
3998 per_cu->v.quick->mark = 1;
3999 break;
4000 }
24c79950
TT
4001 }
4002
4003 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4004 ? visited_found.get ()
4005 : visited_not_found.get (),
24c79950
TT
4006 file_data, INSERT);
4007 *slot = file_data;
4008 }
24c79950 4009 }
9291a0cd 4010
3876f04e 4011 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4012 {
4013 offset_type idx = 2 * iter;
4014 const char *name;
4015 offset_type *vec, vec_len, vec_idx;
8943b874 4016 int global_seen = 0;
9291a0cd 4017
61d96d7e
DE
4018 QUIT;
4019
3876f04e 4020 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4021 continue;
4022
3876f04e 4023 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4024
206f2a57 4025 if (! (*symbol_matcher) (name, data))
9291a0cd
TT
4026 continue;
4027
4028 /* The name was matched, now expand corresponding CUs that were
4029 marked. */
4b5246aa 4030 vec = (offset_type *) (index->constant_pool
3876f04e 4031 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4032 vec_len = MAYBE_SWAP (vec[0]);
4033 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4034 {
e254ef6a 4035 struct dwarf2_per_cu_data *per_cu;
156942c7 4036 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4037 /* This value is only valid for index versions >= 7. */
4038 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4039 gdb_index_symbol_kind symbol_kind =
4040 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4041 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4042 /* Only check the symbol attributes if they're present.
4043 Indices prior to version 7 don't record them,
4044 and indices >= 7 may elide them for certain symbols
4045 (gold does this). */
4046 int attrs_valid =
4047 (index->version >= 7
4048 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4049
8943b874
DE
4050 /* Work around gold/15646. */
4051 if (attrs_valid)
4052 {
4053 if (!is_static && global_seen)
4054 continue;
4055 if (!is_static)
4056 global_seen = 1;
4057 }
4058
3190f0c6
DE
4059 /* Only check the symbol's kind if it has one. */
4060 if (attrs_valid)
156942c7
DE
4061 {
4062 switch (kind)
4063 {
4064 case VARIABLES_DOMAIN:
4065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4066 continue;
4067 break;
4068 case FUNCTIONS_DOMAIN:
4069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4070 continue;
4071 break;
4072 case TYPES_DOMAIN:
4073 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4074 continue;
4075 break;
4076 default:
4077 break;
4078 }
4079 }
4080
3190f0c6
DE
4081 /* Don't crash on bad data. */
4082 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4083 + dwarf2_per_objfile->n_type_units))
4084 {
4085 complaint (&symfile_complaints,
4086 _(".gdb_index entry has bad CU index"
4262abfb 4087 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4088 continue;
4089 }
4090
8832e7e3 4091 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4092 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4093 {
4094 int symtab_was_null =
4095 (per_cu->v.quick->compunit_symtab == NULL);
4096
4097 dw2_instantiate_symtab (per_cu);
4098
4099 if (expansion_notify != NULL
4100 && symtab_was_null
4101 && per_cu->v.quick->compunit_symtab != NULL)
4102 {
4103 expansion_notify (per_cu->v.quick->compunit_symtab,
4104 data);
4105 }
4106 }
9291a0cd
TT
4107 }
4108 }
4109}
4110
43f3e411 4111/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4112 symtab. */
4113
43f3e411
DE
4114static struct compunit_symtab *
4115recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4116 CORE_ADDR pc)
9703b513
TT
4117{
4118 int i;
4119
43f3e411
DE
4120 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4121 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4122 return cust;
9703b513 4123
43f3e411 4124 if (cust->includes == NULL)
a3ec0bb1
DE
4125 return NULL;
4126
43f3e411 4127 for (i = 0; cust->includes[i]; ++i)
9703b513 4128 {
43f3e411 4129 struct compunit_symtab *s = cust->includes[i];
9703b513 4130
43f3e411 4131 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4132 if (s != NULL)
4133 return s;
4134 }
4135
4136 return NULL;
4137}
4138
43f3e411
DE
4139static struct compunit_symtab *
4140dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4141 struct bound_minimal_symbol msymbol,
4142 CORE_ADDR pc,
4143 struct obj_section *section,
4144 int warn_if_readin)
9291a0cd
TT
4145{
4146 struct dwarf2_per_cu_data *data;
43f3e411 4147 struct compunit_symtab *result;
9291a0cd
TT
4148
4149 dw2_setup (objfile);
4150
4151 if (!objfile->psymtabs_addrmap)
4152 return NULL;
4153
9a3c8263
SM
4154 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4155 pc);
9291a0cd
TT
4156 if (!data)
4157 return NULL;
4158
43f3e411 4159 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4160 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4161 paddress (get_objfile_arch (objfile), pc));
4162
43f3e411
DE
4163 result
4164 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4165 pc);
9703b513
TT
4166 gdb_assert (result != NULL);
4167 return result;
9291a0cd
TT
4168}
4169
9291a0cd 4170static void
44b13c5a 4171dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4172 void *data, int need_fullname)
9291a0cd
TT
4173{
4174 int i;
fc4007c9
TT
4175 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4176 NULL, xcalloc, xfree));
9291a0cd
TT
4177
4178 dw2_setup (objfile);
ae2de4f8 4179
848e3e78
DE
4180 /* The rule is CUs specify all the files, including those used by
4181 any TU, so there's no need to scan TUs here.
4182 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 4183
848e3e78 4184 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4185 {
8832e7e3 4186 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4187
43f3e411 4188 if (per_cu->v.quick->compunit_symtab)
24c79950 4189 {
fc4007c9
TT
4190 void **slot = htab_find_slot (visited.get (),
4191 per_cu->v.quick->file_names,
24c79950
TT
4192 INSERT);
4193
4194 *slot = per_cu->v.quick->file_names;
4195 }
4196 }
4197
848e3e78 4198 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4199 {
4200 int j;
8832e7e3 4201 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4202 struct quick_file_names *file_data;
24c79950 4203 void **slot;
9291a0cd 4204
3d7bb9d9 4205 /* We only need to look at symtabs not already expanded. */
43f3e411 4206 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4207 continue;
4208
e4a48d9d 4209 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4210 if (file_data == NULL)
9291a0cd
TT
4211 continue;
4212
fc4007c9 4213 slot = htab_find_slot (visited.get (), file_data, INSERT);
24c79950
TT
4214 if (*slot)
4215 {
4216 /* Already visited. */
4217 continue;
4218 }
4219 *slot = file_data;
4220
7b9f3c50 4221 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4222 {
74e2f255
DE
4223 const char *this_real_name;
4224
4225 if (need_fullname)
4226 this_real_name = dw2_get_real_path (objfile, file_data, j);
4227 else
4228 this_real_name = NULL;
7b9f3c50 4229 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4230 }
4231 }
4232}
4233
4234static int
4235dw2_has_symbols (struct objfile *objfile)
4236{
4237 return 1;
4238}
4239
4240const struct quick_symbol_functions dwarf2_gdb_index_functions =
4241{
4242 dw2_has_symbols,
4243 dw2_find_last_source_symtab,
4244 dw2_forget_cached_source_info,
f8eba3c6 4245 dw2_map_symtabs_matching_filename,
9291a0cd 4246 dw2_lookup_symbol,
9291a0cd
TT
4247 dw2_print_stats,
4248 dw2_dump,
4249 dw2_relocate,
4250 dw2_expand_symtabs_for_function,
4251 dw2_expand_all_symtabs,
652a8996 4252 dw2_expand_symtabs_with_fullname,
40658b94 4253 dw2_map_matching_symbols,
9291a0cd 4254 dw2_expand_symtabs_matching,
43f3e411 4255 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4256 dw2_map_symbol_filenames
4257};
4258
4259/* Initialize for reading DWARF for this objfile. Return 0 if this
4260 file will use psymtabs, or 1 if using the GNU index. */
4261
4262int
4263dwarf2_initialize_objfile (struct objfile *objfile)
4264{
4265 /* If we're about to read full symbols, don't bother with the
4266 indices. In this case we also don't care if some other debug
4267 format is making psymtabs, because they are all about to be
4268 expanded anyway. */
4269 if ((objfile->flags & OBJF_READNOW))
4270 {
4271 int i;
4272
4273 dwarf2_per_objfile->using_index = 1;
4274 create_all_comp_units (objfile);
0e50663e 4275 create_all_type_units (objfile);
7b9f3c50
DE
4276 dwarf2_per_objfile->quick_file_names_table =
4277 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4278
1fd400ff 4279 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4280 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4281 {
8832e7e3 4282 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4283
e254ef6a
DE
4284 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4285 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4286 }
4287
4288 /* Return 1 so that gdb sees the "quick" functions. However,
4289 these functions will be no-ops because we will have expanded
4290 all symtabs. */
4291 return 1;
4292 }
4293
4294 if (dwarf2_read_index (objfile))
4295 return 1;
4296
9291a0cd
TT
4297 return 0;
4298}
4299
4300\f
4301
dce234bc
PP
4302/* Build a partial symbol table. */
4303
4304void
f29dff0a 4305dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4306{
c9bf0622 4307
f29dff0a 4308 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4309 {
4310 init_psymbol_list (objfile, 1024);
4311 }
4312
492d29ea 4313 TRY
c9bf0622
TT
4314 {
4315 /* This isn't really ideal: all the data we allocate on the
4316 objfile's obstack is still uselessly kept around. However,
4317 freeing it seems unsafe. */
906768f9 4318 psymtab_discarder psymtabs (objfile);
c9bf0622 4319 dwarf2_build_psymtabs_hard (objfile);
906768f9 4320 psymtabs.keep ();
c9bf0622 4321 }
492d29ea
PA
4322 CATCH (except, RETURN_MASK_ERROR)
4323 {
4324 exception_print (gdb_stderr, except);
4325 }
4326 END_CATCH
c906108c 4327}
c906108c 4328
1ce1cefd
DE
4329/* Return the total length of the CU described by HEADER. */
4330
4331static unsigned int
4332get_cu_length (const struct comp_unit_head *header)
4333{
4334 return header->initial_length_size + header->length;
4335}
4336
45452591
DE
4337/* Return TRUE if OFFSET is within CU_HEADER. */
4338
4339static inline int
b64f50a1 4340offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 4341{
b64f50a1 4342 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 4343 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 4344
b64f50a1 4345 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
4346}
4347
3b80fe9b
DE
4348/* Find the base address of the compilation unit for range lists and
4349 location lists. It will normally be specified by DW_AT_low_pc.
4350 In DWARF-3 draft 4, the base address could be overridden by
4351 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4352 compilation units with discontinuous ranges. */
4353
4354static void
4355dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4356{
4357 struct attribute *attr;
4358
4359 cu->base_known = 0;
4360 cu->base_address = 0;
4361
4362 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4363 if (attr)
4364 {
31aa7e4e 4365 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4366 cu->base_known = 1;
4367 }
4368 else
4369 {
4370 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4371 if (attr)
4372 {
31aa7e4e 4373 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4374 cu->base_known = 1;
4375 }
4376 }
4377}
4378
93311388 4379/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4380 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4381 NOTE: This leaves members offset, first_die_offset to be filled in
4382 by the caller. */
107d2387 4383
d521ce57 4384static const gdb_byte *
107d2387 4385read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4386 const gdb_byte *info_ptr,
4387 struct dwarf2_section_info *section,
4388 rcuh_kind section_kind)
107d2387
AC
4389{
4390 int signed_addr;
891d2f0b 4391 unsigned int bytes_read;
43988095
JK
4392 const char *filename = get_section_file_name (section);
4393 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4394
4395 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4396 cu_header->initial_length_size = bytes_read;
4397 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4398 info_ptr += bytes_read;
107d2387
AC
4399 cu_header->version = read_2_bytes (abfd, info_ptr);
4400 info_ptr += 2;
43988095
JK
4401 if (cu_header->version < 5)
4402 switch (section_kind)
4403 {
4404 case rcuh_kind::COMPILE:
4405 cu_header->unit_type = DW_UT_compile;
4406 break;
4407 case rcuh_kind::TYPE:
4408 cu_header->unit_type = DW_UT_type;
4409 break;
4410 default:
4411 internal_error (__FILE__, __LINE__,
4412 _("read_comp_unit_head: invalid section_kind"));
4413 }
4414 else
4415 {
4416 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4417 (read_1_byte (abfd, info_ptr));
4418 info_ptr += 1;
4419 switch (cu_header->unit_type)
4420 {
4421 case DW_UT_compile:
4422 if (section_kind != rcuh_kind::COMPILE)
4423 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4424 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4425 filename);
4426 break;
4427 case DW_UT_type:
4428 section_kind = rcuh_kind::TYPE;
4429 break;
4430 default:
4431 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4432 "(is %d, should be %d or %d) [in module %s]"),
4433 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4434 }
4435
4436 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4437 info_ptr += 1;
4438 }
b64f50a1
JK
4439 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4440 &bytes_read);
613e1657 4441 info_ptr += bytes_read;
43988095
JK
4442 if (cu_header->version < 5)
4443 {
4444 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4445 info_ptr += 1;
4446 }
107d2387
AC
4447 signed_addr = bfd_get_sign_extend_vma (abfd);
4448 if (signed_addr < 0)
8e65ff28 4449 internal_error (__FILE__, __LINE__,
e2e0b3e5 4450 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4451 cu_header->signed_addr_p = signed_addr;
c764a876 4452
43988095
JK
4453 if (section_kind == rcuh_kind::TYPE)
4454 {
4455 LONGEST type_offset;
4456
4457 cu_header->signature = read_8_bytes (abfd, info_ptr);
4458 info_ptr += 8;
4459
4460 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4461 info_ptr += bytes_read;
4462 cu_header->type_offset_in_tu.cu_off = type_offset;
4463 if (cu_header->type_offset_in_tu.cu_off != type_offset)
4464 error (_("Dwarf Error: Too big type_offset in compilation unit "
4465 "header (is %s) [in module %s]"), plongest (type_offset),
4466 filename);
4467 }
4468
107d2387
AC
4469 return info_ptr;
4470}
4471
36586728
TT
4472/* Helper function that returns the proper abbrev section for
4473 THIS_CU. */
4474
4475static struct dwarf2_section_info *
4476get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4477{
4478 struct dwarf2_section_info *abbrev;
4479
4480 if (this_cu->is_dwz)
4481 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4482 else
4483 abbrev = &dwarf2_per_objfile->abbrev;
4484
4485 return abbrev;
4486}
4487
9ff913ba
DE
4488/* Subroutine of read_and_check_comp_unit_head and
4489 read_and_check_type_unit_head to simplify them.
4490 Perform various error checking on the header. */
4491
4492static void
4493error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4494 struct dwarf2_section_info *section,
4495 struct dwarf2_section_info *abbrev_section)
9ff913ba 4496{
a32a8923 4497 const char *filename = get_section_file_name (section);
9ff913ba 4498
43988095 4499 if (header->version < 2 || header->version > 5)
9ff913ba 4500 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4501 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4502 filename);
4503
b64f50a1 4504 if (header->abbrev_offset.sect_off
36586728 4505 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4506 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4507 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4508 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4509 filename);
4510
4511 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4512 avoid potential 32-bit overflow. */
1ce1cefd 4513 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4514 > section->size)
4515 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4516 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4517 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4518 filename);
4519}
4520
4521/* Read in a CU/TU header and perform some basic error checking.
4522 The contents of the header are stored in HEADER.
4523 The result is a pointer to the start of the first DIE. */
adabb602 4524
d521ce57 4525static const gdb_byte *
9ff913ba
DE
4526read_and_check_comp_unit_head (struct comp_unit_head *header,
4527 struct dwarf2_section_info *section,
4bdcc0c1 4528 struct dwarf2_section_info *abbrev_section,
d521ce57 4529 const gdb_byte *info_ptr,
43988095 4530 rcuh_kind section_kind)
72bf9492 4531{
d521ce57 4532 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4533 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4534
b64f50a1 4535 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4536
43988095 4537 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4538
b64f50a1 4539 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4540
4bdcc0c1 4541 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4542
4543 return info_ptr;
348e048f
DE
4544}
4545
f4dc4d17
DE
4546/* Fetch the abbreviation table offset from a comp or type unit header. */
4547
4548static sect_offset
4549read_abbrev_offset (struct dwarf2_section_info *section,
4550 sect_offset offset)
4551{
a32a8923 4552 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4553 const gdb_byte *info_ptr;
ac298888 4554 unsigned int initial_length_size, offset_size;
f4dc4d17 4555 sect_offset abbrev_offset;
43988095 4556 uint16_t version;
f4dc4d17
DE
4557
4558 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4559 info_ptr = section->buffer + offset.sect_off;
ac298888 4560 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4561 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4562 info_ptr += initial_length_size;
4563
4564 version = read_2_bytes (abfd, info_ptr);
4565 info_ptr += 2;
4566 if (version >= 5)
4567 {
4568 /* Skip unit type and address size. */
4569 info_ptr += 2;
4570 }
4571
f4dc4d17
DE
4572 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4573 return abbrev_offset;
4574}
4575
aaa75496
JB
4576/* Allocate a new partial symtab for file named NAME and mark this new
4577 partial symtab as being an include of PST. */
4578
4579static void
d521ce57 4580dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4581 struct objfile *objfile)
4582{
4583 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4584
fbd9ab74
JK
4585 if (!IS_ABSOLUTE_PATH (subpst->filename))
4586 {
4587 /* It shares objfile->objfile_obstack. */
4588 subpst->dirname = pst->dirname;
4589 }
4590
aaa75496
JB
4591 subpst->textlow = 0;
4592 subpst->texthigh = 0;
4593
8d749320
SM
4594 subpst->dependencies
4595 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4596 subpst->dependencies[0] = pst;
4597 subpst->number_of_dependencies = 1;
4598
4599 subpst->globals_offset = 0;
4600 subpst->n_global_syms = 0;
4601 subpst->statics_offset = 0;
4602 subpst->n_static_syms = 0;
43f3e411 4603 subpst->compunit_symtab = NULL;
aaa75496
JB
4604 subpst->read_symtab = pst->read_symtab;
4605 subpst->readin = 0;
4606
4607 /* No private part is necessary for include psymtabs. This property
4608 can be used to differentiate between such include psymtabs and
10b3939b 4609 the regular ones. */
58a9656e 4610 subpst->read_symtab_private = NULL;
aaa75496
JB
4611}
4612
4613/* Read the Line Number Program data and extract the list of files
4614 included by the source file represented by PST. Build an include
d85a05f0 4615 partial symtab for each of these included files. */
aaa75496
JB
4616
4617static void
4618dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4619 struct die_info *die,
4620 struct partial_symtab *pst)
aaa75496 4621{
d85a05f0
DJ
4622 struct line_header *lh = NULL;
4623 struct attribute *attr;
aaa75496 4624
d85a05f0
DJ
4625 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4626 if (attr)
3019eac3 4627 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4628 if (lh == NULL)
4629 return; /* No linetable, so no includes. */
4630
c6da4cef 4631 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
527f3840 4632 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4633
4634 free_line_header (lh);
4635}
4636
348e048f 4637static hashval_t
52dc124a 4638hash_signatured_type (const void *item)
348e048f 4639{
9a3c8263
SM
4640 const struct signatured_type *sig_type
4641 = (const struct signatured_type *) item;
9a619af0 4642
348e048f 4643 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4644 return sig_type->signature;
348e048f
DE
4645}
4646
4647static int
52dc124a 4648eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4649{
9a3c8263
SM
4650 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4651 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4652
348e048f
DE
4653 return lhs->signature == rhs->signature;
4654}
4655
1fd400ff
TT
4656/* Allocate a hash table for signatured types. */
4657
4658static htab_t
673bfd45 4659allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4660{
4661 return htab_create_alloc_ex (41,
52dc124a
DE
4662 hash_signatured_type,
4663 eq_signatured_type,
1fd400ff
TT
4664 NULL,
4665 &objfile->objfile_obstack,
4666 hashtab_obstack_allocate,
4667 dummy_obstack_deallocate);
4668}
4669
d467dd73 4670/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4671
4672static int
d467dd73 4673add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4674{
9a3c8263
SM
4675 struct signatured_type *sigt = (struct signatured_type *) *slot;
4676 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4677
b4dd5633 4678 **datap = sigt;
1fd400ff
TT
4679 ++*datap;
4680
4681 return 1;
4682}
4683
78d4d2c5 4684/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4685 and fill them into TYPES_HTAB. It will process only type units,
4686 therefore DW_UT_type. */
c88ee1f0 4687
78d4d2c5
JK
4688static void
4689create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4690 dwarf2_section_info *section, htab_t &types_htab,
4691 rcuh_kind section_kind)
348e048f 4692{
3019eac3 4693 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4694 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4695 bfd *abfd;
4696 const gdb_byte *info_ptr, *end_ptr;
348e048f 4697
4bdcc0c1
DE
4698 abbrev_section = (dwo_file != NULL
4699 ? &dwo_file->sections.abbrev
4700 : &dwarf2_per_objfile->abbrev);
4701
b4f54984 4702 if (dwarf_read_debug)
43988095
JK
4703 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4704 get_section_name (section),
a32a8923 4705 get_section_file_name (abbrev_section));
09406207 4706
78d4d2c5
JK
4707 dwarf2_read_section (objfile, section);
4708 info_ptr = section->buffer;
348e048f 4709
78d4d2c5
JK
4710 if (info_ptr == NULL)
4711 return;
348e048f 4712
78d4d2c5
JK
4713 /* We can't set abfd until now because the section may be empty or
4714 not present, in which case the bfd is unknown. */
4715 abfd = get_section_bfd_owner (section);
348e048f 4716
78d4d2c5
JK
4717 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4718 because we don't need to read any dies: the signature is in the
4719 header. */
3019eac3 4720
78d4d2c5
JK
4721 end_ptr = info_ptr + section->size;
4722 while (info_ptr < end_ptr)
4723 {
4724 sect_offset offset;
78d4d2c5
JK
4725 struct signatured_type *sig_type;
4726 struct dwo_unit *dwo_tu;
4727 void **slot;
4728 const gdb_byte *ptr = info_ptr;
4729 struct comp_unit_head header;
4730 unsigned int length;
8b70b953 4731
78d4d2c5 4732 offset.sect_off = ptr - section->buffer;
348e048f 4733
78d4d2c5
JK
4734 /* We need to read the type's signature in order to build the hash
4735 table, but we don't need anything else just yet. */
348e048f 4736
43988095
JK
4737 ptr = read_and_check_comp_unit_head (&header, section,
4738 abbrev_section, ptr, section_kind);
348e048f 4739
78d4d2c5 4740 length = get_cu_length (&header);
6caca83c 4741
78d4d2c5
JK
4742 /* Skip dummy type units. */
4743 if (ptr >= info_ptr + length
43988095
JK
4744 || peek_abbrev_code (abfd, ptr) == 0
4745 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4746 {
4747 info_ptr += length;
4748 continue;
4749 }
dee91e82 4750
78d4d2c5
JK
4751 if (types_htab == NULL)
4752 {
4753 if (dwo_file)
4754 types_htab = allocate_dwo_unit_table (objfile);
4755 else
4756 types_htab = allocate_signatured_type_table (objfile);
4757 }
8b70b953 4758
78d4d2c5
JK
4759 if (dwo_file)
4760 {
4761 sig_type = NULL;
4762 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4763 struct dwo_unit);
4764 dwo_tu->dwo_file = dwo_file;
43988095
JK
4765 dwo_tu->signature = header.signature;
4766 dwo_tu->type_offset_in_tu = header.type_offset_in_tu;
78d4d2c5
JK
4767 dwo_tu->section = section;
4768 dwo_tu->offset = offset;
4769 dwo_tu->length = length;
4770 }
4771 else
4772 {
4773 /* N.B.: type_offset is not usable if this type uses a DWO file.
4774 The real type_offset is in the DWO file. */
4775 dwo_tu = NULL;
4776 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4777 struct signatured_type);
43988095
JK
4778 sig_type->signature = header.signature;
4779 sig_type->type_offset_in_tu = header.type_offset_in_tu;
78d4d2c5
JK
4780 sig_type->per_cu.objfile = objfile;
4781 sig_type->per_cu.is_debug_types = 1;
4782 sig_type->per_cu.section = section;
4783 sig_type->per_cu.offset = offset;
4784 sig_type->per_cu.length = length;
4785 }
4786
4787 slot = htab_find_slot (types_htab,
4788 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4789 INSERT);
4790 gdb_assert (slot != NULL);
4791 if (*slot != NULL)
4792 {
4793 sect_offset dup_offset;
0349ea22 4794
3019eac3
DE
4795 if (dwo_file)
4796 {
78d4d2c5
JK
4797 const struct dwo_unit *dup_tu
4798 = (const struct dwo_unit *) *slot;
4799
4800 dup_offset = dup_tu->offset;
3019eac3
DE
4801 }
4802 else
4803 {
78d4d2c5
JK
4804 const struct signatured_type *dup_tu
4805 = (const struct signatured_type *) *slot;
4806
4807 dup_offset = dup_tu->per_cu.offset;
3019eac3 4808 }
8b70b953 4809
78d4d2c5
JK
4810 complaint (&symfile_complaints,
4811 _("debug type entry at offset 0x%x is duplicate to"
4812 " the entry at offset 0x%x, signature %s"),
4813 offset.sect_off, dup_offset.sect_off,
43988095 4814 hex_string (header.signature));
78d4d2c5
JK
4815 }
4816 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4817
78d4d2c5
JK
4818 if (dwarf_read_debug > 1)
4819 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4820 offset.sect_off,
43988095 4821 hex_string (header.signature));
3019eac3 4822
78d4d2c5
JK
4823 info_ptr += length;
4824 }
4825}
3019eac3 4826
78d4d2c5
JK
4827/* Create the hash table of all entries in the .debug_types
4828 (or .debug_types.dwo) section(s).
4829 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4830 otherwise it is NULL.
b3c8eb43 4831
78d4d2c5 4832 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4833
78d4d2c5 4834 Note: This function processes DWO files only, not DWP files. */
348e048f 4835
78d4d2c5
JK
4836static void
4837create_debug_types_hash_table (struct dwo_file *dwo_file,
4838 VEC (dwarf2_section_info_def) *types,
4839 htab_t &types_htab)
4840{
4841 int ix;
4842 struct dwarf2_section_info *section;
4843
4844 if (VEC_empty (dwarf2_section_info_def, types))
4845 return;
348e048f 4846
78d4d2c5
JK
4847 for (ix = 0;
4848 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4849 ++ix)
43988095
JK
4850 create_debug_type_hash_table (dwo_file, section, types_htab,
4851 rcuh_kind::TYPE);
3019eac3
DE
4852}
4853
4854/* Create the hash table of all entries in the .debug_types section,
4855 and initialize all_type_units.
4856 The result is zero if there is an error (e.g. missing .debug_types section),
4857 otherwise non-zero. */
4858
4859static int
4860create_all_type_units (struct objfile *objfile)
4861{
78d4d2c5 4862 htab_t types_htab = NULL;
b4dd5633 4863 struct signatured_type **iter;
3019eac3 4864
43988095
JK
4865 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4866 rcuh_kind::COMPILE);
78d4d2c5 4867 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
4868 if (types_htab == NULL)
4869 {
4870 dwarf2_per_objfile->signatured_types = NULL;
4871 return 0;
4872 }
4873
348e048f
DE
4874 dwarf2_per_objfile->signatured_types = types_htab;
4875
6aa5f3a6
DE
4876 dwarf2_per_objfile->n_type_units
4877 = dwarf2_per_objfile->n_allocated_type_units
4878 = htab_elements (types_htab);
8d749320
SM
4879 dwarf2_per_objfile->all_type_units =
4880 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
4881 iter = &dwarf2_per_objfile->all_type_units[0];
4882 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4883 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4884 == dwarf2_per_objfile->n_type_units);
1fd400ff 4885
348e048f
DE
4886 return 1;
4887}
4888
6aa5f3a6
DE
4889/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4890 If SLOT is non-NULL, it is the entry to use in the hash table.
4891 Otherwise we find one. */
4892
4893static struct signatured_type *
4894add_type_unit (ULONGEST sig, void **slot)
4895{
4896 struct objfile *objfile = dwarf2_per_objfile->objfile;
4897 int n_type_units = dwarf2_per_objfile->n_type_units;
4898 struct signatured_type *sig_type;
4899
4900 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4901 ++n_type_units;
4902 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4903 {
4904 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4905 dwarf2_per_objfile->n_allocated_type_units = 1;
4906 dwarf2_per_objfile->n_allocated_type_units *= 2;
4907 dwarf2_per_objfile->all_type_units
224c3ddb
SM
4908 = XRESIZEVEC (struct signatured_type *,
4909 dwarf2_per_objfile->all_type_units,
4910 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
4911 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4912 }
4913 dwarf2_per_objfile->n_type_units = n_type_units;
4914
4915 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4916 struct signatured_type);
4917 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4918 sig_type->signature = sig;
4919 sig_type->per_cu.is_debug_types = 1;
4920 if (dwarf2_per_objfile->using_index)
4921 {
4922 sig_type->per_cu.v.quick =
4923 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4924 struct dwarf2_per_cu_quick_data);
4925 }
4926
4927 if (slot == NULL)
4928 {
4929 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4930 sig_type, INSERT);
4931 }
4932 gdb_assert (*slot == NULL);
4933 *slot = sig_type;
4934 /* The rest of sig_type must be filled in by the caller. */
4935 return sig_type;
4936}
4937
a2ce51a0
DE
4938/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4939 Fill in SIG_ENTRY with DWO_ENTRY. */
4940
4941static void
4942fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4943 struct signatured_type *sig_entry,
4944 struct dwo_unit *dwo_entry)
4945{
7ee85ab1 4946 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4947 gdb_assert (! sig_entry->per_cu.queued);
4948 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
4949 if (dwarf2_per_objfile->using_index)
4950 {
4951 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 4952 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
4953 }
4954 else
4955 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0
DE
4956 gdb_assert (sig_entry->signature == dwo_entry->signature);
4957 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4958 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4959 gdb_assert (sig_entry->dwo_unit == NULL);
4960
4961 sig_entry->per_cu.section = dwo_entry->section;
4962 sig_entry->per_cu.offset = dwo_entry->offset;
4963 sig_entry->per_cu.length = dwo_entry->length;
4964 sig_entry->per_cu.reading_dwo_directly = 1;
4965 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4966 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4967 sig_entry->dwo_unit = dwo_entry;
4968}
4969
4970/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4971 If we haven't read the TU yet, create the signatured_type data structure
4972 for a TU to be read in directly from a DWO file, bypassing the stub.
4973 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4974 using .gdb_index, then when reading a CU we want to stay in the DWO file
4975 containing that CU. Otherwise we could end up reading several other DWO
4976 files (due to comdat folding) to process the transitive closure of all the
4977 mentioned TUs, and that can be slow. The current DWO file will have every
4978 type signature that it needs.
a2ce51a0
DE
4979 We only do this for .gdb_index because in the psymtab case we already have
4980 to read all the DWOs to build the type unit groups. */
4981
4982static struct signatured_type *
4983lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4984{
4985 struct objfile *objfile = dwarf2_per_objfile->objfile;
4986 struct dwo_file *dwo_file;
4987 struct dwo_unit find_dwo_entry, *dwo_entry;
4988 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 4989 void **slot;
a2ce51a0
DE
4990
4991 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4992
6aa5f3a6
DE
4993 /* If TU skeletons have been removed then we may not have read in any
4994 TUs yet. */
4995 if (dwarf2_per_objfile->signatured_types == NULL)
4996 {
4997 dwarf2_per_objfile->signatured_types
4998 = allocate_signatured_type_table (objfile);
4999 }
a2ce51a0
DE
5000
5001 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5002 Use the global signatured_types array to do our own comdat-folding
5003 of types. If this is the first time we're reading this TU, and
5004 the TU has an entry in .gdb_index, replace the recorded data from
5005 .gdb_index with this TU. */
a2ce51a0 5006
a2ce51a0 5007 find_sig_entry.signature = sig;
6aa5f3a6
DE
5008 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5009 &find_sig_entry, INSERT);
9a3c8263 5010 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5011
5012 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5013 read. Don't reassign the global entry to point to this DWO if that's
5014 the case. Also note that if the TU is already being read, it may not
5015 have come from a DWO, the program may be a mix of Fission-compiled
5016 code and non-Fission-compiled code. */
5017
5018 /* Have we already tried to read this TU?
5019 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5020 needn't exist in the global table yet). */
5021 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5022 return sig_entry;
5023
6aa5f3a6
DE
5024 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5025 dwo_unit of the TU itself. */
5026 dwo_file = cu->dwo_unit->dwo_file;
5027
a2ce51a0
DE
5028 /* Ok, this is the first time we're reading this TU. */
5029 if (dwo_file->tus == NULL)
5030 return NULL;
5031 find_dwo_entry.signature = sig;
9a3c8263 5032 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5033 if (dwo_entry == NULL)
5034 return NULL;
5035
6aa5f3a6
DE
5036 /* If the global table doesn't have an entry for this TU, add one. */
5037 if (sig_entry == NULL)
5038 sig_entry = add_type_unit (sig, slot);
5039
a2ce51a0 5040 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5041 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5042 return sig_entry;
5043}
5044
a2ce51a0
DE
5045/* Subroutine of lookup_signatured_type.
5046 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5047 then try the DWP file. If the TU stub (skeleton) has been removed then
5048 it won't be in .gdb_index. */
a2ce51a0
DE
5049
5050static struct signatured_type *
5051lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5052{
5053 struct objfile *objfile = dwarf2_per_objfile->objfile;
5054 struct dwp_file *dwp_file = get_dwp_file ();
5055 struct dwo_unit *dwo_entry;
5056 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5057 void **slot;
a2ce51a0
DE
5058
5059 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5060 gdb_assert (dwp_file != NULL);
5061
6aa5f3a6
DE
5062 /* If TU skeletons have been removed then we may not have read in any
5063 TUs yet. */
5064 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5065 {
6aa5f3a6
DE
5066 dwarf2_per_objfile->signatured_types
5067 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5068 }
5069
6aa5f3a6
DE
5070 find_sig_entry.signature = sig;
5071 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5072 &find_sig_entry, INSERT);
9a3c8263 5073 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5074
5075 /* Have we already tried to read this TU?
5076 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5077 needn't exist in the global table yet). */
5078 if (sig_entry != NULL)
5079 return sig_entry;
5080
a2ce51a0
DE
5081 if (dwp_file->tus == NULL)
5082 return NULL;
57d63ce2
DE
5083 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5084 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5085 if (dwo_entry == NULL)
5086 return NULL;
5087
6aa5f3a6 5088 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5089 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5090
a2ce51a0
DE
5091 return sig_entry;
5092}
5093
380bca97 5094/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5095 Returns NULL if signature SIG is not present in the table.
5096 It is up to the caller to complain about this. */
348e048f
DE
5097
5098static struct signatured_type *
a2ce51a0 5099lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5100{
a2ce51a0
DE
5101 if (cu->dwo_unit
5102 && dwarf2_per_objfile->using_index)
5103 {
5104 /* We're in a DWO/DWP file, and we're using .gdb_index.
5105 These cases require special processing. */
5106 if (get_dwp_file () == NULL)
5107 return lookup_dwo_signatured_type (cu, sig);
5108 else
5109 return lookup_dwp_signatured_type (cu, sig);
5110 }
5111 else
5112 {
5113 struct signatured_type find_entry, *entry;
348e048f 5114
a2ce51a0
DE
5115 if (dwarf2_per_objfile->signatured_types == NULL)
5116 return NULL;
5117 find_entry.signature = sig;
9a3c8263
SM
5118 entry = ((struct signatured_type *)
5119 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5120 return entry;
5121 }
348e048f 5122}
42e7ad6c
DE
5123\f
5124/* Low level DIE reading support. */
348e048f 5125
d85a05f0
DJ
5126/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5127
5128static void
5129init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5130 struct dwarf2_cu *cu,
3019eac3
DE
5131 struct dwarf2_section_info *section,
5132 struct dwo_file *dwo_file)
d85a05f0 5133{
fceca515 5134 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5135 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5136 reader->cu = cu;
3019eac3 5137 reader->dwo_file = dwo_file;
dee91e82
DE
5138 reader->die_section = section;
5139 reader->buffer = section->buffer;
f664829e 5140 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5141 reader->comp_dir = NULL;
d85a05f0
DJ
5142}
5143
b0c7bfa9
DE
5144/* Subroutine of init_cutu_and_read_dies to simplify it.
5145 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5146 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5147 already.
5148
5149 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5150 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5151 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5152 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5153 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5154 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5155 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5156 are filled in with the info of the DIE from the DWO file.
5157 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5158 provided an abbrev table to use.
5159 The result is non-zero if a valid (non-dummy) DIE was found. */
5160
5161static int
5162read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5163 struct dwo_unit *dwo_unit,
5164 int abbrev_table_provided,
5165 struct die_info *stub_comp_unit_die,
a2ce51a0 5166 const char *stub_comp_dir,
b0c7bfa9 5167 struct die_reader_specs *result_reader,
d521ce57 5168 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5169 struct die_info **result_comp_unit_die,
5170 int *result_has_children)
5171{
5172 struct objfile *objfile = dwarf2_per_objfile->objfile;
5173 struct dwarf2_cu *cu = this_cu->cu;
5174 struct dwarf2_section_info *section;
5175 bfd *abfd;
d521ce57 5176 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5177 ULONGEST signature; /* Or dwo_id. */
5178 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5179 int i,num_extra_attrs;
5180 struct dwarf2_section_info *dwo_abbrev_section;
5181 struct attribute *attr;
5182 struct die_info *comp_unit_die;
5183
b0aeadb3
DE
5184 /* At most one of these may be provided. */
5185 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5186
b0c7bfa9
DE
5187 /* These attributes aren't processed until later:
5188 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5189 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5190 referenced later. However, these attributes are found in the stub
5191 which we won't have later. In order to not impose this complication
5192 on the rest of the code, we read them here and copy them to the
5193 DWO CU/TU die. */
b0c7bfa9
DE
5194
5195 stmt_list = NULL;
5196 low_pc = NULL;
5197 high_pc = NULL;
5198 ranges = NULL;
5199 comp_dir = NULL;
5200
5201 if (stub_comp_unit_die != NULL)
5202 {
5203 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5204 DWO file. */
5205 if (! this_cu->is_debug_types)
5206 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5207 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5208 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5209 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5210 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5211
5212 /* There should be a DW_AT_addr_base attribute here (if needed).
5213 We need the value before we can process DW_FORM_GNU_addr_index. */
5214 cu->addr_base = 0;
5215 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5216 if (attr)
5217 cu->addr_base = DW_UNSND (attr);
5218
5219 /* There should be a DW_AT_ranges_base attribute here (if needed).
5220 We need the value before we can process DW_AT_ranges. */
5221 cu->ranges_base = 0;
5222 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5223 if (attr)
5224 cu->ranges_base = DW_UNSND (attr);
5225 }
a2ce51a0
DE
5226 else if (stub_comp_dir != NULL)
5227 {
5228 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5229 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5230 comp_dir->name = DW_AT_comp_dir;
5231 comp_dir->form = DW_FORM_string;
5232 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5233 DW_STRING (comp_dir) = stub_comp_dir;
5234 }
b0c7bfa9
DE
5235
5236 /* Set up for reading the DWO CU/TU. */
5237 cu->dwo_unit = dwo_unit;
5238 section = dwo_unit->section;
5239 dwarf2_read_section (objfile, section);
a32a8923 5240 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
5241 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5242 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5243 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5244
5245 if (this_cu->is_debug_types)
5246 {
b0c7bfa9
DE
5247 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5248
43988095 5249 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5250 dwo_abbrev_section,
43988095 5251 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5252 /* This is not an assert because it can be caused by bad debug info. */
43988095 5253 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5254 {
5255 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5256 " TU at offset 0x%x [in module %s]"),
5257 hex_string (sig_type->signature),
43988095 5258 hex_string (cu->header.signature),
a2ce51a0
DE
5259 dwo_unit->offset.sect_off,
5260 bfd_get_filename (abfd));
5261 }
b0c7bfa9
DE
5262 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5263 /* For DWOs coming from DWP files, we don't know the CU length
5264 nor the type's offset in the TU until now. */
5265 dwo_unit->length = get_cu_length (&cu->header);
43988095 5266 dwo_unit->type_offset_in_tu = cu->header.type_offset_in_tu;
b0c7bfa9
DE
5267
5268 /* Establish the type offset that can be used to lookup the type.
5269 For DWO files, we don't know it until now. */
5270 sig_type->type_offset_in_section.sect_off =
5271 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5272 }
5273 else
5274 {
5275 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5276 dwo_abbrev_section,
43988095 5277 info_ptr, rcuh_kind::COMPILE);
b0c7bfa9
DE
5278 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5279 /* For DWOs coming from DWP files, we don't know the CU length
5280 until now. */
5281 dwo_unit->length = get_cu_length (&cu->header);
5282 }
5283
02142a6c
DE
5284 /* Replace the CU's original abbrev table with the DWO's.
5285 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5286 if (abbrev_table_provided)
5287 {
5288 /* Don't free the provided abbrev table, the caller of
5289 init_cutu_and_read_dies owns it. */
5290 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5291 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5292 make_cleanup (dwarf2_free_abbrev_table, cu);
5293 }
5294 else
5295 {
5296 dwarf2_free_abbrev_table (cu);
5297 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5298 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5299 }
5300
5301 /* Read in the die, but leave space to copy over the attributes
5302 from the stub. This has the benefit of simplifying the rest of
5303 the code - all the work to maintain the illusion of a single
5304 DW_TAG_{compile,type}_unit DIE is done here. */
5305 num_extra_attrs = ((stmt_list != NULL)
5306 + (low_pc != NULL)
5307 + (high_pc != NULL)
5308 + (ranges != NULL)
5309 + (comp_dir != NULL));
5310 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5311 result_has_children, num_extra_attrs);
5312
5313 /* Copy over the attributes from the stub to the DIE we just read in. */
5314 comp_unit_die = *result_comp_unit_die;
5315 i = comp_unit_die->num_attrs;
5316 if (stmt_list != NULL)
5317 comp_unit_die->attrs[i++] = *stmt_list;
5318 if (low_pc != NULL)
5319 comp_unit_die->attrs[i++] = *low_pc;
5320 if (high_pc != NULL)
5321 comp_unit_die->attrs[i++] = *high_pc;
5322 if (ranges != NULL)
5323 comp_unit_die->attrs[i++] = *ranges;
5324 if (comp_dir != NULL)
5325 comp_unit_die->attrs[i++] = *comp_dir;
5326 comp_unit_die->num_attrs += num_extra_attrs;
5327
b4f54984 5328 if (dwarf_die_debug)
bf6af496
DE
5329 {
5330 fprintf_unfiltered (gdb_stdlog,
5331 "Read die from %s@0x%x of %s:\n",
a32a8923 5332 get_section_name (section),
bf6af496
DE
5333 (unsigned) (begin_info_ptr - section->buffer),
5334 bfd_get_filename (abfd));
b4f54984 5335 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5336 }
5337
a2ce51a0
DE
5338 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5339 TUs by skipping the stub and going directly to the entry in the DWO file.
5340 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5341 to get it via circuitous means. Blech. */
5342 if (comp_dir != NULL)
5343 result_reader->comp_dir = DW_STRING (comp_dir);
5344
b0c7bfa9
DE
5345 /* Skip dummy compilation units. */
5346 if (info_ptr >= begin_info_ptr + dwo_unit->length
5347 || peek_abbrev_code (abfd, info_ptr) == 0)
5348 return 0;
5349
5350 *result_info_ptr = info_ptr;
5351 return 1;
5352}
5353
5354/* Subroutine of init_cutu_and_read_dies to simplify it.
5355 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5356 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5357
5358static struct dwo_unit *
5359lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5360 struct die_info *comp_unit_die)
5361{
5362 struct dwarf2_cu *cu = this_cu->cu;
5363 struct attribute *attr;
5364 ULONGEST signature;
5365 struct dwo_unit *dwo_unit;
5366 const char *comp_dir, *dwo_name;
5367
a2ce51a0
DE
5368 gdb_assert (cu != NULL);
5369
b0c7bfa9 5370 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5371 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5372 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5373
5374 if (this_cu->is_debug_types)
5375 {
5376 struct signatured_type *sig_type;
5377
5378 /* Since this_cu is the first member of struct signatured_type,
5379 we can go from a pointer to one to a pointer to the other. */
5380 sig_type = (struct signatured_type *) this_cu;
5381 signature = sig_type->signature;
5382 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5383 }
5384 else
5385 {
5386 struct attribute *attr;
5387
5388 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5389 if (! attr)
5390 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5391 " [in module %s]"),
4262abfb 5392 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5393 signature = DW_UNSND (attr);
5394 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5395 signature);
5396 }
5397
b0c7bfa9
DE
5398 return dwo_unit;
5399}
5400
a2ce51a0 5401/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5402 See it for a description of the parameters.
5403 Read a TU directly from a DWO file, bypassing the stub.
5404
5405 Note: This function could be a little bit simpler if we shared cleanups
5406 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5407 to do, so we keep this function self-contained. Or we could move this
5408 into our caller, but it's complex enough already. */
a2ce51a0
DE
5409
5410static void
6aa5f3a6
DE
5411init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5412 int use_existing_cu, int keep,
a2ce51a0
DE
5413 die_reader_func_ftype *die_reader_func,
5414 void *data)
5415{
5416 struct dwarf2_cu *cu;
5417 struct signatured_type *sig_type;
6aa5f3a6 5418 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5419 struct die_reader_specs reader;
5420 const gdb_byte *info_ptr;
5421 struct die_info *comp_unit_die;
5422 int has_children;
5423
5424 /* Verify we can do the following downcast, and that we have the
5425 data we need. */
5426 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5427 sig_type = (struct signatured_type *) this_cu;
5428 gdb_assert (sig_type->dwo_unit != NULL);
5429
5430 cleanups = make_cleanup (null_cleanup, NULL);
5431
6aa5f3a6
DE
5432 if (use_existing_cu && this_cu->cu != NULL)
5433 {
5434 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5435 cu = this_cu->cu;
5436 /* There's no need to do the rereading_dwo_cu handling that
5437 init_cutu_and_read_dies does since we don't read the stub. */
5438 }
5439 else
5440 {
5441 /* If !use_existing_cu, this_cu->cu must be NULL. */
5442 gdb_assert (this_cu->cu == NULL);
8d749320 5443 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5444 init_one_comp_unit (cu, this_cu);
5445 /* If an error occurs while loading, release our storage. */
5446 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5447 }
5448
5449 /* A future optimization, if needed, would be to use an existing
5450 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5451 could share abbrev tables. */
a2ce51a0
DE
5452
5453 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5454 0 /* abbrev_table_provided */,
5455 NULL /* stub_comp_unit_die */,
5456 sig_type->dwo_unit->dwo_file->comp_dir,
5457 &reader, &info_ptr,
5458 &comp_unit_die, &has_children) == 0)
5459 {
5460 /* Dummy die. */
5461 do_cleanups (cleanups);
5462 return;
5463 }
5464
5465 /* All the "real" work is done here. */
5466 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5467
6aa5f3a6 5468 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5469 but the alternative is making the latter more complex.
5470 This function is only for the special case of using DWO files directly:
5471 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5472 if (free_cu_cleanup != NULL)
a2ce51a0 5473 {
6aa5f3a6
DE
5474 if (keep)
5475 {
5476 /* We've successfully allocated this compilation unit. Let our
5477 caller clean it up when finished with it. */
5478 discard_cleanups (free_cu_cleanup);
a2ce51a0 5479
6aa5f3a6
DE
5480 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5481 So we have to manually free the abbrev table. */
5482 dwarf2_free_abbrev_table (cu);
a2ce51a0 5483
6aa5f3a6
DE
5484 /* Link this CU into read_in_chain. */
5485 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5486 dwarf2_per_objfile->read_in_chain = this_cu;
5487 }
5488 else
5489 do_cleanups (free_cu_cleanup);
a2ce51a0 5490 }
a2ce51a0
DE
5491
5492 do_cleanups (cleanups);
5493}
5494
fd820528 5495/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5496 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5497
f4dc4d17
DE
5498 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5499 Otherwise the table specified in the comp unit header is read in and used.
5500 This is an optimization for when we already have the abbrev table.
5501
dee91e82
DE
5502 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5503 Otherwise, a new CU is allocated with xmalloc.
5504
5505 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5506 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5507
5508 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5509 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5510
70221824 5511static void
fd820528 5512init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5513 struct abbrev_table *abbrev_table,
fd820528
DE
5514 int use_existing_cu, int keep,
5515 die_reader_func_ftype *die_reader_func,
5516 void *data)
c906108c 5517{
dee91e82 5518 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5519 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5520 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5521 struct dwarf2_cu *cu;
d521ce57 5522 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5523 struct die_reader_specs reader;
d85a05f0 5524 struct die_info *comp_unit_die;
dee91e82 5525 int has_children;
d85a05f0 5526 struct attribute *attr;
365156ad 5527 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5528 struct signatured_type *sig_type = NULL;
4bdcc0c1 5529 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5530 /* Non-zero if CU currently points to a DWO file and we need to
5531 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5532 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5533 int rereading_dwo_cu = 0;
c906108c 5534
b4f54984 5535 if (dwarf_die_debug)
09406207
DE
5536 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5537 this_cu->is_debug_types ? "type" : "comp",
5538 this_cu->offset.sect_off);
5539
dee91e82
DE
5540 if (use_existing_cu)
5541 gdb_assert (keep);
23745b47 5542
a2ce51a0
DE
5543 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5544 file (instead of going through the stub), short-circuit all of this. */
5545 if (this_cu->reading_dwo_directly)
5546 {
5547 /* Narrow down the scope of possibilities to have to understand. */
5548 gdb_assert (this_cu->is_debug_types);
5549 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5550 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5551 die_reader_func, data);
a2ce51a0
DE
5552 return;
5553 }
5554
dee91e82
DE
5555 cleanups = make_cleanup (null_cleanup, NULL);
5556
5557 /* This is cheap if the section is already read in. */
5558 dwarf2_read_section (objfile, section);
5559
5560 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5561
5562 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5563
5564 if (use_existing_cu && this_cu->cu != NULL)
5565 {
5566 cu = this_cu->cu;
42e7ad6c
DE
5567 /* If this CU is from a DWO file we need to start over, we need to
5568 refetch the attributes from the skeleton CU.
5569 This could be optimized by retrieving those attributes from when we
5570 were here the first time: the previous comp_unit_die was stored in
5571 comp_unit_obstack. But there's no data yet that we need this
5572 optimization. */
5573 if (cu->dwo_unit != NULL)
5574 rereading_dwo_cu = 1;
dee91e82
DE
5575 }
5576 else
5577 {
5578 /* If !use_existing_cu, this_cu->cu must be NULL. */
5579 gdb_assert (this_cu->cu == NULL);
8d749320 5580 cu = XNEW (struct dwarf2_cu);
dee91e82 5581 init_one_comp_unit (cu, this_cu);
dee91e82 5582 /* If an error occurs while loading, release our storage. */
365156ad 5583 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5584 }
dee91e82 5585
b0c7bfa9 5586 /* Get the header. */
42e7ad6c
DE
5587 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5588 {
5589 /* We already have the header, there's no need to read it in again. */
5590 info_ptr += cu->header.first_die_offset.cu_off;
5591 }
5592 else
5593 {
3019eac3 5594 if (this_cu->is_debug_types)
dee91e82 5595 {
43988095 5596 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5597 abbrev_section, info_ptr,
43988095 5598 rcuh_kind::TYPE);
dee91e82 5599
42e7ad6c
DE
5600 /* Since per_cu is the first member of struct signatured_type,
5601 we can go from a pointer to one to a pointer to the other. */
5602 sig_type = (struct signatured_type *) this_cu;
43988095 5603 gdb_assert (sig_type->signature == cu->header.signature);
42e7ad6c 5604 gdb_assert (sig_type->type_offset_in_tu.cu_off
43988095 5605 == cu->header.type_offset_in_tu.cu_off);
dee91e82
DE
5606 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5607
42e7ad6c
DE
5608 /* LENGTH has not been set yet for type units if we're
5609 using .gdb_index. */
1ce1cefd 5610 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5611
5612 /* Establish the type offset that can be used to lookup the type. */
5613 sig_type->type_offset_in_section.sect_off =
5614 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
43988095
JK
5615
5616 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5617 }
5618 else
5619 {
4bdcc0c1
DE
5620 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5621 abbrev_section,
43988095
JK
5622 info_ptr,
5623 rcuh_kind::COMPILE);
dee91e82
DE
5624
5625 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5626 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5627 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5628 }
5629 }
10b3939b 5630
6caca83c 5631 /* Skip dummy compilation units. */
dee91e82 5632 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5633 || peek_abbrev_code (abfd, info_ptr) == 0)
5634 {
dee91e82 5635 do_cleanups (cleanups);
21b2bd31 5636 return;
6caca83c
CC
5637 }
5638
433df2d4
DE
5639 /* If we don't have them yet, read the abbrevs for this compilation unit.
5640 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5641 done. Note that it's important that if the CU had an abbrev table
5642 on entry we don't free it when we're done: Somewhere up the call stack
5643 it may be in use. */
f4dc4d17
DE
5644 if (abbrev_table != NULL)
5645 {
5646 gdb_assert (cu->abbrev_table == NULL);
5647 gdb_assert (cu->header.abbrev_offset.sect_off
5648 == abbrev_table->offset.sect_off);
5649 cu->abbrev_table = abbrev_table;
5650 }
5651 else if (cu->abbrev_table == NULL)
dee91e82 5652 {
4bdcc0c1 5653 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5654 make_cleanup (dwarf2_free_abbrev_table, cu);
5655 }
42e7ad6c
DE
5656 else if (rereading_dwo_cu)
5657 {
5658 dwarf2_free_abbrev_table (cu);
5659 dwarf2_read_abbrevs (cu, abbrev_section);
5660 }
af703f96 5661
dee91e82 5662 /* Read the top level CU/TU die. */
3019eac3 5663 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5664 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5665
b0c7bfa9
DE
5666 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5667 from the DWO file.
5668 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5669 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5670 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5671 if (attr)
5672 {
3019eac3 5673 struct dwo_unit *dwo_unit;
b0c7bfa9 5674 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5675
5676 if (has_children)
6a506a2d
DE
5677 {
5678 complaint (&symfile_complaints,
5679 _("compilation unit with DW_AT_GNU_dwo_name"
5680 " has children (offset 0x%x) [in module %s]"),
5681 this_cu->offset.sect_off, bfd_get_filename (abfd));
5682 }
b0c7bfa9 5683 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5684 if (dwo_unit != NULL)
3019eac3 5685 {
6a506a2d
DE
5686 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5687 abbrev_table != NULL,
a2ce51a0 5688 comp_unit_die, NULL,
6a506a2d
DE
5689 &reader, &info_ptr,
5690 &dwo_comp_unit_die, &has_children) == 0)
5691 {
5692 /* Dummy die. */
5693 do_cleanups (cleanups);
5694 return;
5695 }
5696 comp_unit_die = dwo_comp_unit_die;
5697 }
5698 else
5699 {
5700 /* Yikes, we couldn't find the rest of the DIE, we only have
5701 the stub. A complaint has already been logged. There's
5702 not much more we can do except pass on the stub DIE to
5703 die_reader_func. We don't want to throw an error on bad
5704 debug info. */
3019eac3
DE
5705 }
5706 }
5707
b0c7bfa9 5708 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5709 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5710
b0c7bfa9 5711 /* Done, clean up. */
365156ad 5712 if (free_cu_cleanup != NULL)
348e048f 5713 {
365156ad
TT
5714 if (keep)
5715 {
5716 /* We've successfully allocated this compilation unit. Let our
5717 caller clean it up when finished with it. */
5718 discard_cleanups (free_cu_cleanup);
dee91e82 5719
365156ad
TT
5720 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5721 So we have to manually free the abbrev table. */
5722 dwarf2_free_abbrev_table (cu);
dee91e82 5723
365156ad
TT
5724 /* Link this CU into read_in_chain. */
5725 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5726 dwarf2_per_objfile->read_in_chain = this_cu;
5727 }
5728 else
5729 do_cleanups (free_cu_cleanup);
348e048f 5730 }
365156ad
TT
5731
5732 do_cleanups (cleanups);
dee91e82
DE
5733}
5734
33e80786
DE
5735/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5736 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5737 to have already done the lookup to find the DWO file).
dee91e82
DE
5738
5739 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5740 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5741
5742 We fill in THIS_CU->length.
5743
5744 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5745 linker) then DIE_READER_FUNC will not get called.
5746
5747 THIS_CU->cu is always freed when done.
3019eac3
DE
5748 This is done in order to not leave THIS_CU->cu in a state where we have
5749 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5750
5751static void
5752init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5753 struct dwo_file *dwo_file,
dee91e82
DE
5754 die_reader_func_ftype *die_reader_func,
5755 void *data)
5756{
5757 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5758 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5759 bfd *abfd = get_section_bfd_owner (section);
33e80786 5760 struct dwarf2_section_info *abbrev_section;
dee91e82 5761 struct dwarf2_cu cu;
d521ce57 5762 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5763 struct die_reader_specs reader;
5764 struct cleanup *cleanups;
5765 struct die_info *comp_unit_die;
5766 int has_children;
5767
b4f54984 5768 if (dwarf_die_debug)
09406207
DE
5769 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5770 this_cu->is_debug_types ? "type" : "comp",
5771 this_cu->offset.sect_off);
5772
dee91e82
DE
5773 gdb_assert (this_cu->cu == NULL);
5774
33e80786
DE
5775 abbrev_section = (dwo_file != NULL
5776 ? &dwo_file->sections.abbrev
5777 : get_abbrev_section_for_cu (this_cu));
5778
dee91e82
DE
5779 /* This is cheap if the section is already read in. */
5780 dwarf2_read_section (objfile, section);
5781
5782 init_one_comp_unit (&cu, this_cu);
5783
5784 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5785
5786 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5787 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5788 abbrev_section, info_ptr,
43988095
JK
5789 (this_cu->is_debug_types
5790 ? rcuh_kind::TYPE
5791 : rcuh_kind::COMPILE));
dee91e82 5792
1ce1cefd 5793 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5794
5795 /* Skip dummy compilation units. */
5796 if (info_ptr >= begin_info_ptr + this_cu->length
5797 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5798 {
dee91e82 5799 do_cleanups (cleanups);
21b2bd31 5800 return;
93311388 5801 }
72bf9492 5802
dee91e82
DE
5803 dwarf2_read_abbrevs (&cu, abbrev_section);
5804 make_cleanup (dwarf2_free_abbrev_table, &cu);
5805
3019eac3 5806 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5807 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5808
5809 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5810
5811 do_cleanups (cleanups);
5812}
5813
3019eac3
DE
5814/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5815 does not lookup the specified DWO file.
5816 This cannot be used to read DWO files.
dee91e82
DE
5817
5818 THIS_CU->cu is always freed when done.
3019eac3
DE
5819 This is done in order to not leave THIS_CU->cu in a state where we have
5820 to care whether it refers to the "main" CU or the DWO CU.
5821 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5822
5823static void
5824init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5825 die_reader_func_ftype *die_reader_func,
5826 void *data)
5827{
33e80786 5828 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5829}
0018ea6f
DE
5830\f
5831/* Type Unit Groups.
dee91e82 5832
0018ea6f
DE
5833 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5834 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5835 so that all types coming from the same compilation (.o file) are grouped
5836 together. A future step could be to put the types in the same symtab as
5837 the CU the types ultimately came from. */
ff013f42 5838
f4dc4d17
DE
5839static hashval_t
5840hash_type_unit_group (const void *item)
5841{
9a3c8263
SM
5842 const struct type_unit_group *tu_group
5843 = (const struct type_unit_group *) item;
f4dc4d17 5844
094b34ac 5845 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5846}
348e048f
DE
5847
5848static int
f4dc4d17 5849eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5850{
9a3c8263
SM
5851 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5852 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 5853
094b34ac 5854 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5855}
348e048f 5856
f4dc4d17
DE
5857/* Allocate a hash table for type unit groups. */
5858
5859static htab_t
5860allocate_type_unit_groups_table (void)
5861{
5862 return htab_create_alloc_ex (3,
5863 hash_type_unit_group,
5864 eq_type_unit_group,
5865 NULL,
5866 &dwarf2_per_objfile->objfile->objfile_obstack,
5867 hashtab_obstack_allocate,
5868 dummy_obstack_deallocate);
5869}
dee91e82 5870
f4dc4d17
DE
5871/* Type units that don't have DW_AT_stmt_list are grouped into their own
5872 partial symtabs. We combine several TUs per psymtab to not let the size
5873 of any one psymtab grow too big. */
5874#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5875#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5876
094b34ac 5877/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5878 Create the type_unit_group object used to hold one or more TUs. */
5879
5880static struct type_unit_group *
094b34ac 5881create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5882{
5883 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5884 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5885 struct type_unit_group *tu_group;
f4dc4d17
DE
5886
5887 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5888 struct type_unit_group);
094b34ac 5889 per_cu = &tu_group->per_cu;
f4dc4d17 5890 per_cu->objfile = objfile;
f4dc4d17 5891
094b34ac
DE
5892 if (dwarf2_per_objfile->using_index)
5893 {
5894 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5895 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5896 }
5897 else
5898 {
5899 unsigned int line_offset = line_offset_struct.sect_off;
5900 struct partial_symtab *pst;
5901 char *name;
5902
5903 /* Give the symtab a useful name for debug purposes. */
5904 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5905 name = xstrprintf ("<type_units_%d>",
5906 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5907 else
5908 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5909
5910 pst = create_partial_symtab (per_cu, name);
5911 pst->anonymous = 1;
f4dc4d17 5912
094b34ac
DE
5913 xfree (name);
5914 }
f4dc4d17 5915
094b34ac
DE
5916 tu_group->hash.dwo_unit = cu->dwo_unit;
5917 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5918
5919 return tu_group;
5920}
5921
094b34ac
DE
5922/* Look up the type_unit_group for type unit CU, and create it if necessary.
5923 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5924
5925static struct type_unit_group *
ff39bb5e 5926get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5927{
5928 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5929 struct type_unit_group *tu_group;
5930 void **slot;
5931 unsigned int line_offset;
5932 struct type_unit_group type_unit_group_for_lookup;
5933
5934 if (dwarf2_per_objfile->type_unit_groups == NULL)
5935 {
5936 dwarf2_per_objfile->type_unit_groups =
5937 allocate_type_unit_groups_table ();
5938 }
5939
5940 /* Do we need to create a new group, or can we use an existing one? */
5941
5942 if (stmt_list)
5943 {
5944 line_offset = DW_UNSND (stmt_list);
5945 ++tu_stats->nr_symtab_sharers;
5946 }
5947 else
5948 {
5949 /* Ugh, no stmt_list. Rare, but we have to handle it.
5950 We can do various things here like create one group per TU or
5951 spread them over multiple groups to split up the expansion work.
5952 To avoid worst case scenarios (too many groups or too large groups)
5953 we, umm, group them in bunches. */
5954 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5955 | (tu_stats->nr_stmt_less_type_units
5956 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5957 ++tu_stats->nr_stmt_less_type_units;
5958 }
5959
094b34ac
DE
5960 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5961 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5962 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5963 &type_unit_group_for_lookup, INSERT);
5964 if (*slot != NULL)
5965 {
9a3c8263 5966 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
5967 gdb_assert (tu_group != NULL);
5968 }
5969 else
5970 {
5971 sect_offset line_offset_struct;
5972
5973 line_offset_struct.sect_off = line_offset;
094b34ac 5974 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5975 *slot = tu_group;
5976 ++tu_stats->nr_symtabs;
5977 }
5978
5979 return tu_group;
5980}
0018ea6f
DE
5981\f
5982/* Partial symbol tables. */
5983
5984/* Create a psymtab named NAME and assign it to PER_CU.
5985
5986 The caller must fill in the following details:
5987 dirname, textlow, texthigh. */
5988
5989static struct partial_symtab *
5990create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5991{
5992 struct objfile *objfile = per_cu->objfile;
5993 struct partial_symtab *pst;
5994
18a94d75 5995 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
5996 objfile->global_psymbols.next,
5997 objfile->static_psymbols.next);
5998
5999 pst->psymtabs_addrmap_supported = 1;
6000
6001 /* This is the glue that links PST into GDB's symbol API. */
6002 pst->read_symtab_private = per_cu;
6003 pst->read_symtab = dwarf2_read_symtab;
6004 per_cu->v.psymtab = pst;
6005
6006 return pst;
6007}
6008
b93601f3
TT
6009/* The DATA object passed to process_psymtab_comp_unit_reader has this
6010 type. */
6011
6012struct process_psymtab_comp_unit_data
6013{
6014 /* True if we are reading a DW_TAG_partial_unit. */
6015
6016 int want_partial_unit;
6017
6018 /* The "pretend" language that is used if the CU doesn't declare a
6019 language. */
6020
6021 enum language pretend_language;
6022};
6023
0018ea6f
DE
6024/* die_reader_func for process_psymtab_comp_unit. */
6025
6026static void
6027process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6028 const gdb_byte *info_ptr,
0018ea6f
DE
6029 struct die_info *comp_unit_die,
6030 int has_children,
6031 void *data)
6032{
6033 struct dwarf2_cu *cu = reader->cu;
6034 struct objfile *objfile = cu->objfile;
3e29f34a 6035 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6036 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6037 CORE_ADDR baseaddr;
6038 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6039 struct partial_symtab *pst;
3a2b436a 6040 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6041 const char *filename;
9a3c8263
SM
6042 struct process_psymtab_comp_unit_data *info
6043 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6044
b93601f3 6045 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6046 return;
6047
6048 gdb_assert (! per_cu->is_debug_types);
6049
b93601f3 6050 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6051
6052 cu->list_in_scope = &file_symbols;
6053
6054 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6055 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6056 if (filename == NULL)
0018ea6f 6057 filename = "";
0018ea6f
DE
6058
6059 pst = create_partial_symtab (per_cu, filename);
6060
6061 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6062 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6063
6064 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6065
6066 dwarf2_find_base_address (comp_unit_die, cu);
6067
6068 /* Possibly set the default values of LOWPC and HIGHPC from
6069 `DW_AT_ranges'. */
3a2b436a
JK
6070 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6071 &best_highpc, cu, pst);
6072 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6073 /* Store the contiguous range if it is not empty; it can be empty for
6074 CUs with no code. */
6075 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6076 gdbarch_adjust_dwarf2_addr (gdbarch,
6077 best_lowpc + baseaddr),
6078 gdbarch_adjust_dwarf2_addr (gdbarch,
6079 best_highpc + baseaddr) - 1,
6080 pst);
0018ea6f
DE
6081
6082 /* Check if comp unit has_children.
6083 If so, read the rest of the partial symbols from this comp unit.
6084 If not, there's no more debug_info for this comp unit. */
6085 if (has_children)
6086 {
6087 struct partial_die_info *first_die;
6088 CORE_ADDR lowpc, highpc;
6089
6090 lowpc = ((CORE_ADDR) -1);
6091 highpc = ((CORE_ADDR) 0);
6092
6093 first_die = load_partial_dies (reader, info_ptr, 1);
6094
6095 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6096 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6097
6098 /* If we didn't find a lowpc, set it to highpc to avoid
6099 complaints from `maint check'. */
6100 if (lowpc == ((CORE_ADDR) -1))
6101 lowpc = highpc;
6102
6103 /* If the compilation unit didn't have an explicit address range,
6104 then use the information extracted from its child dies. */
e385593e 6105 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6106 {
6107 best_lowpc = lowpc;
6108 best_highpc = highpc;
6109 }
6110 }
3e29f34a
MR
6111 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6112 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6113
8763cede 6114 end_psymtab_common (objfile, pst);
0018ea6f
DE
6115
6116 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6117 {
6118 int i;
6119 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6120 struct dwarf2_per_cu_data *iter;
6121
6122 /* Fill in 'dependencies' here; we fill in 'users' in a
6123 post-pass. */
6124 pst->number_of_dependencies = len;
8d749320
SM
6125 pst->dependencies =
6126 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6127 for (i = 0;
6128 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6129 i, iter);
6130 ++i)
6131 pst->dependencies[i] = iter->v.psymtab;
6132
6133 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6134 }
6135
6136 /* Get the list of files included in the current compilation unit,
6137 and build a psymtab for each of them. */
6138 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6139
b4f54984 6140 if (dwarf_read_debug)
0018ea6f
DE
6141 {
6142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6143
6144 fprintf_unfiltered (gdb_stdlog,
6145 "Psymtab for %s unit @0x%x: %s - %s"
6146 ", %d global, %d static syms\n",
6147 per_cu->is_debug_types ? "type" : "comp",
6148 per_cu->offset.sect_off,
6149 paddress (gdbarch, pst->textlow),
6150 paddress (gdbarch, pst->texthigh),
6151 pst->n_global_syms, pst->n_static_syms);
6152 }
6153}
6154
6155/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6156 Process compilation unit THIS_CU for a psymtab. */
6157
6158static void
6159process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6160 int want_partial_unit,
6161 enum language pretend_language)
0018ea6f 6162{
b93601f3
TT
6163 struct process_psymtab_comp_unit_data info;
6164
0018ea6f
DE
6165 /* If this compilation unit was already read in, free the
6166 cached copy in order to read it in again. This is
6167 necessary because we skipped some symbols when we first
6168 read in the compilation unit (see load_partial_dies).
6169 This problem could be avoided, but the benefit is unclear. */
6170 if (this_cu->cu != NULL)
6171 free_one_cached_comp_unit (this_cu);
6172
6173 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6174 info.want_partial_unit = want_partial_unit;
6175 info.pretend_language = pretend_language;
0018ea6f
DE
6176 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6177 process_psymtab_comp_unit_reader,
b93601f3 6178 &info);
0018ea6f
DE
6179
6180 /* Age out any secondary CUs. */
6181 age_cached_comp_units ();
6182}
f4dc4d17
DE
6183
6184/* Reader function for build_type_psymtabs. */
6185
6186static void
6187build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6188 const gdb_byte *info_ptr,
f4dc4d17
DE
6189 struct die_info *type_unit_die,
6190 int has_children,
6191 void *data)
6192{
6193 struct objfile *objfile = dwarf2_per_objfile->objfile;
6194 struct dwarf2_cu *cu = reader->cu;
6195 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6196 struct signatured_type *sig_type;
f4dc4d17
DE
6197 struct type_unit_group *tu_group;
6198 struct attribute *attr;
6199 struct partial_die_info *first_die;
6200 CORE_ADDR lowpc, highpc;
6201 struct partial_symtab *pst;
6202
6203 gdb_assert (data == NULL);
0186c6a7
DE
6204 gdb_assert (per_cu->is_debug_types);
6205 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6206
6207 if (! has_children)
6208 return;
6209
6210 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6211 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6212
0186c6a7 6213 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6214
6215 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6216 cu->list_in_scope = &file_symbols;
6217 pst = create_partial_symtab (per_cu, "");
6218 pst->anonymous = 1;
6219
6220 first_die = load_partial_dies (reader, info_ptr, 1);
6221
6222 lowpc = (CORE_ADDR) -1;
6223 highpc = (CORE_ADDR) 0;
6224 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6225
8763cede 6226 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6227}
6228
73051182
DE
6229/* Struct used to sort TUs by their abbreviation table offset. */
6230
6231struct tu_abbrev_offset
6232{
6233 struct signatured_type *sig_type;
6234 sect_offset abbrev_offset;
6235};
6236
6237/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6238
6239static int
6240sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6241{
9a3c8263
SM
6242 const struct tu_abbrev_offset * const *a
6243 = (const struct tu_abbrev_offset * const*) ap;
6244 const struct tu_abbrev_offset * const *b
6245 = (const struct tu_abbrev_offset * const*) bp;
73051182
DE
6246 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6247 unsigned int boff = (*b)->abbrev_offset.sect_off;
6248
6249 return (aoff > boff) - (aoff < boff);
6250}
6251
6252/* Efficiently read all the type units.
6253 This does the bulk of the work for build_type_psymtabs.
6254
6255 The efficiency is because we sort TUs by the abbrev table they use and
6256 only read each abbrev table once. In one program there are 200K TUs
6257 sharing 8K abbrev tables.
6258
6259 The main purpose of this function is to support building the
6260 dwarf2_per_objfile->type_unit_groups table.
6261 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6262 can collapse the search space by grouping them by stmt_list.
6263 The savings can be significant, in the same program from above the 200K TUs
6264 share 8K stmt_list tables.
6265
6266 FUNC is expected to call get_type_unit_group, which will create the
6267 struct type_unit_group if necessary and add it to
6268 dwarf2_per_objfile->type_unit_groups. */
6269
6270static void
6271build_type_psymtabs_1 (void)
6272{
73051182
DE
6273 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6274 struct cleanup *cleanups;
6275 struct abbrev_table *abbrev_table;
6276 sect_offset abbrev_offset;
6277 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6278 int i;
6279
6280 /* It's up to the caller to not call us multiple times. */
6281 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6282
6283 if (dwarf2_per_objfile->n_type_units == 0)
6284 return;
6285
6286 /* TUs typically share abbrev tables, and there can be way more TUs than
6287 abbrev tables. Sort by abbrev table to reduce the number of times we
6288 read each abbrev table in.
6289 Alternatives are to punt or to maintain a cache of abbrev tables.
6290 This is simpler and efficient enough for now.
6291
6292 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6293 symtab to use). Typically TUs with the same abbrev offset have the same
6294 stmt_list value too so in practice this should work well.
6295
6296 The basic algorithm here is:
6297
6298 sort TUs by abbrev table
6299 for each TU with same abbrev table:
6300 read abbrev table if first user
6301 read TU top level DIE
6302 [IWBN if DWO skeletons had DW_AT_stmt_list]
6303 call FUNC */
6304
b4f54984 6305 if (dwarf_read_debug)
73051182
DE
6306 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6307
6308 /* Sort in a separate table to maintain the order of all_type_units
6309 for .gdb_index: TU indices directly index all_type_units. */
6310 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6311 dwarf2_per_objfile->n_type_units);
6312 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6313 {
6314 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6315
6316 sorted_by_abbrev[i].sig_type = sig_type;
6317 sorted_by_abbrev[i].abbrev_offset =
6318 read_abbrev_offset (sig_type->per_cu.section,
6319 sig_type->per_cu.offset);
6320 }
6321 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6322 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6323 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6324
6325 abbrev_offset.sect_off = ~(unsigned) 0;
6326 abbrev_table = NULL;
6327 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6328
6329 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6330 {
6331 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6332
6333 /* Switch to the next abbrev table if necessary. */
6334 if (abbrev_table == NULL
6335 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6336 {
6337 if (abbrev_table != NULL)
6338 {
6339 abbrev_table_free (abbrev_table);
6340 /* Reset to NULL in case abbrev_table_read_table throws
6341 an error: abbrev_table_free_cleanup will get called. */
6342 abbrev_table = NULL;
6343 }
6344 abbrev_offset = tu->abbrev_offset;
6345 abbrev_table =
6346 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6347 abbrev_offset);
6348 ++tu_stats->nr_uniq_abbrev_tables;
6349 }
6350
6351 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6352 build_type_psymtabs_reader, NULL);
6353 }
6354
73051182 6355 do_cleanups (cleanups);
6aa5f3a6 6356}
73051182 6357
6aa5f3a6
DE
6358/* Print collected type unit statistics. */
6359
6360static void
6361print_tu_stats (void)
6362{
6363 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6364
6365 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6366 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6367 dwarf2_per_objfile->n_type_units);
6368 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6369 tu_stats->nr_uniq_abbrev_tables);
6370 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6371 tu_stats->nr_symtabs);
6372 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6373 tu_stats->nr_symtab_sharers);
6374 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6375 tu_stats->nr_stmt_less_type_units);
6376 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6377 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6378}
6379
f4dc4d17
DE
6380/* Traversal function for build_type_psymtabs. */
6381
6382static int
6383build_type_psymtab_dependencies (void **slot, void *info)
6384{
6385 struct objfile *objfile = dwarf2_per_objfile->objfile;
6386 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6387 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6388 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6389 int len = VEC_length (sig_type_ptr, tu_group->tus);
6390 struct signatured_type *iter;
f4dc4d17
DE
6391 int i;
6392
6393 gdb_assert (len > 0);
0186c6a7 6394 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6395
6396 pst->number_of_dependencies = len;
8d749320
SM
6397 pst->dependencies =
6398 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6399 for (i = 0;
0186c6a7 6400 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6401 ++i)
6402 {
0186c6a7
DE
6403 gdb_assert (iter->per_cu.is_debug_types);
6404 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6405 iter->type_unit_group = tu_group;
f4dc4d17
DE
6406 }
6407
0186c6a7 6408 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6409
6410 return 1;
6411}
6412
6413/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6414 Build partial symbol tables for the .debug_types comp-units. */
6415
6416static void
6417build_type_psymtabs (struct objfile *objfile)
6418{
0e50663e 6419 if (! create_all_type_units (objfile))
348e048f
DE
6420 return;
6421
73051182 6422 build_type_psymtabs_1 ();
6aa5f3a6 6423}
f4dc4d17 6424
6aa5f3a6
DE
6425/* Traversal function for process_skeletonless_type_unit.
6426 Read a TU in a DWO file and build partial symbols for it. */
6427
6428static int
6429process_skeletonless_type_unit (void **slot, void *info)
6430{
6431 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6432 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6433 struct signatured_type find_entry, *entry;
6434
6435 /* If this TU doesn't exist in the global table, add it and read it in. */
6436
6437 if (dwarf2_per_objfile->signatured_types == NULL)
6438 {
6439 dwarf2_per_objfile->signatured_types
6440 = allocate_signatured_type_table (objfile);
6441 }
6442
6443 find_entry.signature = dwo_unit->signature;
6444 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6445 INSERT);
6446 /* If we've already seen this type there's nothing to do. What's happening
6447 is we're doing our own version of comdat-folding here. */
6448 if (*slot != NULL)
6449 return 1;
6450
6451 /* This does the job that create_all_type_units would have done for
6452 this TU. */
6453 entry = add_type_unit (dwo_unit->signature, slot);
6454 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6455 *slot = entry;
6456
6457 /* This does the job that build_type_psymtabs_1 would have done. */
6458 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6459 build_type_psymtabs_reader, NULL);
6460
6461 return 1;
6462}
6463
6464/* Traversal function for process_skeletonless_type_units. */
6465
6466static int
6467process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6468{
6469 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6470
6471 if (dwo_file->tus != NULL)
6472 {
6473 htab_traverse_noresize (dwo_file->tus,
6474 process_skeletonless_type_unit, info);
6475 }
6476
6477 return 1;
6478}
6479
6480/* Scan all TUs of DWO files, verifying we've processed them.
6481 This is needed in case a TU was emitted without its skeleton.
6482 Note: This can't be done until we know what all the DWO files are. */
6483
6484static void
6485process_skeletonless_type_units (struct objfile *objfile)
6486{
6487 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6488 if (get_dwp_file () == NULL
6489 && dwarf2_per_objfile->dwo_files != NULL)
6490 {
6491 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6492 process_dwo_file_for_skeletonless_type_units,
6493 objfile);
6494 }
348e048f
DE
6495}
6496
60606b2c
TT
6497/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6498
6499static void
6500psymtabs_addrmap_cleanup (void *o)
6501{
9a3c8263 6502 struct objfile *objfile = (struct objfile *) o;
ec61707d 6503
60606b2c
TT
6504 objfile->psymtabs_addrmap = NULL;
6505}
6506
95554aad
TT
6507/* Compute the 'user' field for each psymtab in OBJFILE. */
6508
6509static void
6510set_partial_user (struct objfile *objfile)
6511{
6512 int i;
6513
6514 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6515 {
8832e7e3 6516 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6517 struct partial_symtab *pst = per_cu->v.psymtab;
6518 int j;
6519
36586728
TT
6520 if (pst == NULL)
6521 continue;
6522
95554aad
TT
6523 for (j = 0; j < pst->number_of_dependencies; ++j)
6524 {
6525 /* Set the 'user' field only if it is not already set. */
6526 if (pst->dependencies[j]->user == NULL)
6527 pst->dependencies[j]->user = pst;
6528 }
6529 }
6530}
6531
93311388
DE
6532/* Build the partial symbol table by doing a quick pass through the
6533 .debug_info and .debug_abbrev sections. */
72bf9492 6534
93311388 6535static void
c67a9c90 6536dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6537{
60606b2c
TT
6538 struct cleanup *back_to, *addrmap_cleanup;
6539 struct obstack temp_obstack;
21b2bd31 6540 int i;
93311388 6541
b4f54984 6542 if (dwarf_read_debug)
45cfd468
DE
6543 {
6544 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6545 objfile_name (objfile));
45cfd468
DE
6546 }
6547
98bfdba5
PA
6548 dwarf2_per_objfile->reading_partial_symbols = 1;
6549
be391dca 6550 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6551
93311388
DE
6552 /* Any cached compilation units will be linked by the per-objfile
6553 read_in_chain. Make sure to free them when we're done. */
6554 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6555
348e048f
DE
6556 build_type_psymtabs (objfile);
6557
93311388 6558 create_all_comp_units (objfile);
c906108c 6559
60606b2c
TT
6560 /* Create a temporary address map on a temporary obstack. We later
6561 copy this to the final obstack. */
6562 obstack_init (&temp_obstack);
6563 make_cleanup_obstack_free (&temp_obstack);
6564 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6565 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6566
21b2bd31 6567 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6568 {
8832e7e3 6569 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6570
b93601f3 6571 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6572 }
ff013f42 6573
6aa5f3a6
DE
6574 /* This has to wait until we read the CUs, we need the list of DWOs. */
6575 process_skeletonless_type_units (objfile);
6576
6577 /* Now that all TUs have been processed we can fill in the dependencies. */
6578 if (dwarf2_per_objfile->type_unit_groups != NULL)
6579 {
6580 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6581 build_type_psymtab_dependencies, NULL);
6582 }
6583
b4f54984 6584 if (dwarf_read_debug)
6aa5f3a6
DE
6585 print_tu_stats ();
6586
95554aad
TT
6587 set_partial_user (objfile);
6588
ff013f42
JK
6589 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6590 &objfile->objfile_obstack);
60606b2c 6591 discard_cleanups (addrmap_cleanup);
ff013f42 6592
ae038cb0 6593 do_cleanups (back_to);
45cfd468 6594
b4f54984 6595 if (dwarf_read_debug)
45cfd468 6596 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6597 objfile_name (objfile));
ae038cb0
DJ
6598}
6599
3019eac3 6600/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6601
6602static void
dee91e82 6603load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6604 const gdb_byte *info_ptr,
dee91e82
DE
6605 struct die_info *comp_unit_die,
6606 int has_children,
6607 void *data)
ae038cb0 6608{
dee91e82 6609 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6610
95554aad 6611 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6612
ae038cb0
DJ
6613 /* Check if comp unit has_children.
6614 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6615 If not, there's no more debug_info for this comp unit. */
d85a05f0 6616 if (has_children)
dee91e82
DE
6617 load_partial_dies (reader, info_ptr, 0);
6618}
98bfdba5 6619
dee91e82
DE
6620/* Load the partial DIEs for a secondary CU into memory.
6621 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6622
dee91e82
DE
6623static void
6624load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6625{
f4dc4d17
DE
6626 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6627 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6628}
6629
ae038cb0 6630static void
36586728
TT
6631read_comp_units_from_section (struct objfile *objfile,
6632 struct dwarf2_section_info *section,
6633 unsigned int is_dwz,
6634 int *n_allocated,
6635 int *n_comp_units,
6636 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6637{
d521ce57 6638 const gdb_byte *info_ptr;
a32a8923 6639 bfd *abfd = get_section_bfd_owner (section);
be391dca 6640
b4f54984 6641 if (dwarf_read_debug)
bf6af496 6642 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6643 get_section_name (section),
6644 get_section_file_name (section));
bf6af496 6645
36586728 6646 dwarf2_read_section (objfile, section);
ae038cb0 6647
36586728 6648 info_ptr = section->buffer;
6e70227d 6649
36586728 6650 while (info_ptr < section->buffer + section->size)
ae038cb0 6651 {
c764a876 6652 unsigned int length, initial_length_size;
ae038cb0 6653 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6654 sect_offset offset;
ae038cb0 6655
36586728 6656 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6657
6658 /* Read just enough information to find out where the next
6659 compilation unit is. */
36586728 6660 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6661
6662 /* Save the compilation unit for later lookup. */
8d749320 6663 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0
DJ
6664 memset (this_cu, 0, sizeof (*this_cu));
6665 this_cu->offset = offset;
c764a876 6666 this_cu->length = length + initial_length_size;
36586728 6667 this_cu->is_dwz = is_dwz;
9291a0cd 6668 this_cu->objfile = objfile;
8a0459fd 6669 this_cu->section = section;
ae038cb0 6670
36586728 6671 if (*n_comp_units == *n_allocated)
ae038cb0 6672 {
36586728 6673 *n_allocated *= 2;
224c3ddb
SM
6674 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6675 *all_comp_units, *n_allocated);
ae038cb0 6676 }
36586728
TT
6677 (*all_comp_units)[*n_comp_units] = this_cu;
6678 ++*n_comp_units;
ae038cb0
DJ
6679
6680 info_ptr = info_ptr + this_cu->length;
6681 }
36586728
TT
6682}
6683
6684/* Create a list of all compilation units in OBJFILE.
6685 This is only done for -readnow and building partial symtabs. */
6686
6687static void
6688create_all_comp_units (struct objfile *objfile)
6689{
6690 int n_allocated;
6691 int n_comp_units;
6692 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6693 struct dwz_file *dwz;
36586728
TT
6694
6695 n_comp_units = 0;
6696 n_allocated = 10;
8d749320 6697 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6698
6699 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6700 &n_allocated, &n_comp_units, &all_comp_units);
6701
4db1a1dc
TT
6702 dwz = dwarf2_get_dwz_file ();
6703 if (dwz != NULL)
6704 read_comp_units_from_section (objfile, &dwz->info, 1,
6705 &n_allocated, &n_comp_units,
6706 &all_comp_units);
ae038cb0 6707
8d749320
SM
6708 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6709 struct dwarf2_per_cu_data *,
6710 n_comp_units);
ae038cb0
DJ
6711 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6712 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6713 xfree (all_comp_units);
6714 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6715}
6716
5734ee8b 6717/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6718 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6719 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6720 DW_AT_ranges). See the comments of add_partial_subprogram on how
6721 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6722
72bf9492
DJ
6723static void
6724scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6725 CORE_ADDR *highpc, int set_addrmap,
6726 struct dwarf2_cu *cu)
c906108c 6727{
72bf9492 6728 struct partial_die_info *pdi;
c906108c 6729
91c24f0a
DC
6730 /* Now, march along the PDI's, descending into ones which have
6731 interesting children but skipping the children of the other ones,
6732 until we reach the end of the compilation unit. */
c906108c 6733
72bf9492 6734 pdi = first_die;
91c24f0a 6735
72bf9492
DJ
6736 while (pdi != NULL)
6737 {
6738 fixup_partial_die (pdi, cu);
c906108c 6739
f55ee35c 6740 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6741 children, so we need to look at them. Ditto for anonymous
6742 enums. */
933c6fe4 6743
72bf9492 6744 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6745 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6746 || pdi->tag == DW_TAG_imported_unit)
c906108c 6747 {
72bf9492 6748 switch (pdi->tag)
c906108c
SS
6749 {
6750 case DW_TAG_subprogram:
cdc07690 6751 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6752 break;
72929c62 6753 case DW_TAG_constant:
c906108c
SS
6754 case DW_TAG_variable:
6755 case DW_TAG_typedef:
91c24f0a 6756 case DW_TAG_union_type:
72bf9492 6757 if (!pdi->is_declaration)
63d06c5c 6758 {
72bf9492 6759 add_partial_symbol (pdi, cu);
63d06c5c
DC
6760 }
6761 break;
c906108c 6762 case DW_TAG_class_type:
680b30c7 6763 case DW_TAG_interface_type:
c906108c 6764 case DW_TAG_structure_type:
72bf9492 6765 if (!pdi->is_declaration)
c906108c 6766 {
72bf9492 6767 add_partial_symbol (pdi, cu);
c906108c 6768 }
e98c9e7c
TT
6769 if (cu->language == language_rust && pdi->has_children)
6770 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6771 set_addrmap, cu);
c906108c 6772 break;
91c24f0a 6773 case DW_TAG_enumeration_type:
72bf9492
DJ
6774 if (!pdi->is_declaration)
6775 add_partial_enumeration (pdi, cu);
c906108c
SS
6776 break;
6777 case DW_TAG_base_type:
a02abb62 6778 case DW_TAG_subrange_type:
c906108c 6779 /* File scope base type definitions are added to the partial
c5aa993b 6780 symbol table. */
72bf9492 6781 add_partial_symbol (pdi, cu);
c906108c 6782 break;
d9fa45fe 6783 case DW_TAG_namespace:
cdc07690 6784 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6785 break;
5d7cb8df 6786 case DW_TAG_module:
cdc07690 6787 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6788 break;
95554aad
TT
6789 case DW_TAG_imported_unit:
6790 {
6791 struct dwarf2_per_cu_data *per_cu;
6792
f4dc4d17
DE
6793 /* For now we don't handle imported units in type units. */
6794 if (cu->per_cu->is_debug_types)
6795 {
6796 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6797 " supported in type units [in module %s]"),
4262abfb 6798 objfile_name (cu->objfile));
f4dc4d17
DE
6799 }
6800
95554aad 6801 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6802 pdi->is_dwz,
95554aad
TT
6803 cu->objfile);
6804
6805 /* Go read the partial unit, if needed. */
6806 if (per_cu->v.psymtab == NULL)
b93601f3 6807 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6808
f4dc4d17 6809 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6810 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6811 }
6812 break;
74921315
KS
6813 case DW_TAG_imported_declaration:
6814 add_partial_symbol (pdi, cu);
6815 break;
c906108c
SS
6816 default:
6817 break;
6818 }
6819 }
6820
72bf9492
DJ
6821 /* If the die has a sibling, skip to the sibling. */
6822
6823 pdi = pdi->die_sibling;
6824 }
6825}
6826
6827/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6828
72bf9492 6829 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6830 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
6831 Enumerators are an exception; they use the scope of their parent
6832 enumeration type, i.e. the name of the enumeration type is not
6833 prepended to the enumerator.
91c24f0a 6834
72bf9492
DJ
6835 There are two complexities. One is DW_AT_specification; in this
6836 case "parent" means the parent of the target of the specification,
6837 instead of the direct parent of the DIE. The other is compilers
6838 which do not emit DW_TAG_namespace; in this case we try to guess
6839 the fully qualified name of structure types from their members'
6840 linkage names. This must be done using the DIE's children rather
6841 than the children of any DW_AT_specification target. We only need
6842 to do this for structures at the top level, i.e. if the target of
6843 any DW_AT_specification (if any; otherwise the DIE itself) does not
6844 have a parent. */
6845
6846/* Compute the scope prefix associated with PDI's parent, in
6847 compilation unit CU. The result will be allocated on CU's
6848 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6849 field. NULL is returned if no prefix is necessary. */
15d034d0 6850static const char *
72bf9492
DJ
6851partial_die_parent_scope (struct partial_die_info *pdi,
6852 struct dwarf2_cu *cu)
6853{
15d034d0 6854 const char *grandparent_scope;
72bf9492 6855 struct partial_die_info *parent, *real_pdi;
91c24f0a 6856
72bf9492
DJ
6857 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6858 then this means the parent of the specification DIE. */
6859
6860 real_pdi = pdi;
72bf9492 6861 while (real_pdi->has_specification)
36586728
TT
6862 real_pdi = find_partial_die (real_pdi->spec_offset,
6863 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6864
6865 parent = real_pdi->die_parent;
6866 if (parent == NULL)
6867 return NULL;
6868
6869 if (parent->scope_set)
6870 return parent->scope;
6871
6872 fixup_partial_die (parent, cu);
6873
10b3939b 6874 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6875
acebe513
UW
6876 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6877 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6878 Work around this problem here. */
6879 if (cu->language == language_cplus
6e70227d 6880 && parent->tag == DW_TAG_namespace
acebe513
UW
6881 && strcmp (parent->name, "::") == 0
6882 && grandparent_scope == NULL)
6883 {
6884 parent->scope = NULL;
6885 parent->scope_set = 1;
6886 return NULL;
6887 }
6888
9c6c53f7
SA
6889 if (pdi->tag == DW_TAG_enumerator)
6890 /* Enumerators should not get the name of the enumeration as a prefix. */
6891 parent->scope = grandparent_scope;
6892 else if (parent->tag == DW_TAG_namespace
f55ee35c 6893 || parent->tag == DW_TAG_module
72bf9492
DJ
6894 || parent->tag == DW_TAG_structure_type
6895 || parent->tag == DW_TAG_class_type
680b30c7 6896 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6897 || parent->tag == DW_TAG_union_type
6898 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6899 {
6900 if (grandparent_scope == NULL)
6901 parent->scope = parent->name;
6902 else
3e43a32a
MS
6903 parent->scope = typename_concat (&cu->comp_unit_obstack,
6904 grandparent_scope,
f55ee35c 6905 parent->name, 0, cu);
72bf9492 6906 }
72bf9492
DJ
6907 else
6908 {
6909 /* FIXME drow/2004-04-01: What should we be doing with
6910 function-local names? For partial symbols, we should probably be
6911 ignoring them. */
6912 complaint (&symfile_complaints,
e2e0b3e5 6913 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6914 parent->tag, pdi->offset.sect_off);
72bf9492 6915 parent->scope = grandparent_scope;
c906108c
SS
6916 }
6917
72bf9492
DJ
6918 parent->scope_set = 1;
6919 return parent->scope;
6920}
6921
6922/* Return the fully scoped name associated with PDI, from compilation unit
6923 CU. The result will be allocated with malloc. */
4568ecf9 6924
72bf9492
DJ
6925static char *
6926partial_die_full_name (struct partial_die_info *pdi,
6927 struct dwarf2_cu *cu)
6928{
15d034d0 6929 const char *parent_scope;
72bf9492 6930
98bfdba5
PA
6931 /* If this is a template instantiation, we can not work out the
6932 template arguments from partial DIEs. So, unfortunately, we have
6933 to go through the full DIEs. At least any work we do building
6934 types here will be reused if full symbols are loaded later. */
6935 if (pdi->has_template_arguments)
6936 {
6937 fixup_partial_die (pdi, cu);
6938
6939 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6940 {
6941 struct die_info *die;
6942 struct attribute attr;
6943 struct dwarf2_cu *ref_cu = cu;
6944
b64f50a1 6945 /* DW_FORM_ref_addr is using section offset. */
b4069958 6946 attr.name = (enum dwarf_attribute) 0;
98bfdba5 6947 attr.form = DW_FORM_ref_addr;
4568ecf9 6948 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6949 die = follow_die_ref (NULL, &attr, &ref_cu);
6950
6951 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6952 }
6953 }
6954
72bf9492
DJ
6955 parent_scope = partial_die_parent_scope (pdi, cu);
6956 if (parent_scope == NULL)
6957 return NULL;
6958 else
f55ee35c 6959 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6960}
6961
6962static void
72bf9492 6963add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6964{
e7c27a73 6965 struct objfile *objfile = cu->objfile;
3e29f34a 6966 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 6967 CORE_ADDR addr = 0;
15d034d0 6968 const char *actual_name = NULL;
e142c38c 6969 CORE_ADDR baseaddr;
15d034d0 6970 char *built_actual_name;
e142c38c
DJ
6971
6972 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6973
15d034d0
TT
6974 built_actual_name = partial_die_full_name (pdi, cu);
6975 if (built_actual_name != NULL)
6976 actual_name = built_actual_name;
63d06c5c 6977
72bf9492
DJ
6978 if (actual_name == NULL)
6979 actual_name = pdi->name;
6980
c906108c
SS
6981 switch (pdi->tag)
6982 {
6983 case DW_TAG_subprogram:
3e29f34a 6984 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 6985 if (pdi->is_external || cu->language == language_ada)
c906108c 6986 {
2cfa0c8d
JB
6987 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6988 of the global scope. But in Ada, we want to be able to access
6989 nested procedures globally. So all Ada subprograms are stored
6990 in the global scope. */
f47fb265 6991 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6992 built_actual_name != NULL,
f47fb265
MS
6993 VAR_DOMAIN, LOC_BLOCK,
6994 &objfile->global_psymbols,
1762568f 6995 addr, cu->language, objfile);
c906108c
SS
6996 }
6997 else
6998 {
f47fb265 6999 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7000 built_actual_name != NULL,
f47fb265
MS
7001 VAR_DOMAIN, LOC_BLOCK,
7002 &objfile->static_psymbols,
1762568f 7003 addr, cu->language, objfile);
c906108c 7004 }
0c1b455e
TT
7005
7006 if (pdi->main_subprogram && actual_name != NULL)
7007 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7008 break;
72929c62
JB
7009 case DW_TAG_constant:
7010 {
7011 struct psymbol_allocation_list *list;
7012
7013 if (pdi->is_external)
7014 list = &objfile->global_psymbols;
7015 else
7016 list = &objfile->static_psymbols;
f47fb265 7017 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7018 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7019 list, 0, cu->language, objfile);
72929c62
JB
7020 }
7021 break;
c906108c 7022 case DW_TAG_variable:
95554aad
TT
7023 if (pdi->d.locdesc)
7024 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7025
95554aad 7026 if (pdi->d.locdesc
caac4577
JG
7027 && addr == 0
7028 && !dwarf2_per_objfile->has_section_at_zero)
7029 {
7030 /* A global or static variable may also have been stripped
7031 out by the linker if unused, in which case its address
7032 will be nullified; do not add such variables into partial
7033 symbol table then. */
7034 }
7035 else if (pdi->is_external)
c906108c
SS
7036 {
7037 /* Global Variable.
7038 Don't enter into the minimal symbol tables as there is
7039 a minimal symbol table entry from the ELF symbols already.
7040 Enter into partial symbol table if it has a location
7041 descriptor or a type.
7042 If the location descriptor is missing, new_symbol will create
7043 a LOC_UNRESOLVED symbol, the address of the variable will then
7044 be determined from the minimal symbol table whenever the variable
7045 is referenced.
7046 The address for the partial symbol table entry is not
7047 used by GDB, but it comes in handy for debugging partial symbol
7048 table building. */
7049
95554aad 7050 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7051 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7052 built_actual_name != NULL,
f47fb265
MS
7053 VAR_DOMAIN, LOC_STATIC,
7054 &objfile->global_psymbols,
1762568f 7055 addr + baseaddr,
f47fb265 7056 cu->language, objfile);
c906108c
SS
7057 }
7058 else
7059 {
ff908ebf
AW
7060 int has_loc = pdi->d.locdesc != NULL;
7061
7062 /* Static Variable. Skip symbols whose value we cannot know (those
7063 without location descriptors or constant values). */
7064 if (!has_loc && !pdi->has_const_value)
decbce07 7065 {
15d034d0 7066 xfree (built_actual_name);
decbce07
MS
7067 return;
7068 }
ff908ebf 7069
f47fb265 7070 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7071 built_actual_name != NULL,
f47fb265
MS
7072 VAR_DOMAIN, LOC_STATIC,
7073 &objfile->static_psymbols,
ff908ebf 7074 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7075 cu->language, objfile);
c906108c
SS
7076 }
7077 break;
7078 case DW_TAG_typedef:
7079 case DW_TAG_base_type:
a02abb62 7080 case DW_TAG_subrange_type:
38d518c9 7081 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7082 built_actual_name != NULL,
176620f1 7083 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7084 &objfile->static_psymbols,
1762568f 7085 0, cu->language, objfile);
c906108c 7086 break;
74921315 7087 case DW_TAG_imported_declaration:
72bf9492
DJ
7088 case DW_TAG_namespace:
7089 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7090 built_actual_name != NULL,
72bf9492
DJ
7091 VAR_DOMAIN, LOC_TYPEDEF,
7092 &objfile->global_psymbols,
1762568f 7093 0, cu->language, objfile);
72bf9492 7094 break;
530e8392
KB
7095 case DW_TAG_module:
7096 add_psymbol_to_list (actual_name, strlen (actual_name),
7097 built_actual_name != NULL,
7098 MODULE_DOMAIN, LOC_TYPEDEF,
7099 &objfile->global_psymbols,
1762568f 7100 0, cu->language, objfile);
530e8392 7101 break;
c906108c 7102 case DW_TAG_class_type:
680b30c7 7103 case DW_TAG_interface_type:
c906108c
SS
7104 case DW_TAG_structure_type:
7105 case DW_TAG_union_type:
7106 case DW_TAG_enumeration_type:
fa4028e9
JB
7107 /* Skip external references. The DWARF standard says in the section
7108 about "Structure, Union, and Class Type Entries": "An incomplete
7109 structure, union or class type is represented by a structure,
7110 union or class entry that does not have a byte size attribute
7111 and that has a DW_AT_declaration attribute." */
7112 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7113 {
15d034d0 7114 xfree (built_actual_name);
decbce07
MS
7115 return;
7116 }
fa4028e9 7117
63d06c5c
DC
7118 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7119 static vs. global. */
38d518c9 7120 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7121 built_actual_name != NULL,
176620f1 7122 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7123 cu->language == language_cplus
63d06c5c
DC
7124 ? &objfile->global_psymbols
7125 : &objfile->static_psymbols,
1762568f 7126 0, cu->language, objfile);
c906108c 7127
c906108c
SS
7128 break;
7129 case DW_TAG_enumerator:
38d518c9 7130 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7131 built_actual_name != NULL,
176620f1 7132 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7133 cu->language == language_cplus
f6fe98ef
DJ
7134 ? &objfile->global_psymbols
7135 : &objfile->static_psymbols,
1762568f 7136 0, cu->language, objfile);
c906108c
SS
7137 break;
7138 default:
7139 break;
7140 }
5c4e30ca 7141
15d034d0 7142 xfree (built_actual_name);
c906108c
SS
7143}
7144
5c4e30ca
DC
7145/* Read a partial die corresponding to a namespace; also, add a symbol
7146 corresponding to that namespace to the symbol table. NAMESPACE is
7147 the name of the enclosing namespace. */
91c24f0a 7148
72bf9492
DJ
7149static void
7150add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7151 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7152 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7153{
72bf9492 7154 /* Add a symbol for the namespace. */
e7c27a73 7155
72bf9492 7156 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7157
7158 /* Now scan partial symbols in that namespace. */
7159
91c24f0a 7160 if (pdi->has_children)
cdc07690 7161 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7162}
7163
5d7cb8df
JK
7164/* Read a partial die corresponding to a Fortran module. */
7165
7166static void
7167add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7168 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7169{
530e8392
KB
7170 /* Add a symbol for the namespace. */
7171
7172 add_partial_symbol (pdi, cu);
7173
f55ee35c 7174 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7175
7176 if (pdi->has_children)
cdc07690 7177 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7178}
7179
bc30ff58
JB
7180/* Read a partial die corresponding to a subprogram and create a partial
7181 symbol for that subprogram. When the CU language allows it, this
7182 routine also defines a partial symbol for each nested subprogram
cdc07690 7183 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7184 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7185 and highest PC values found in PDI.
6e70227d 7186
cdc07690
YQ
7187 PDI may also be a lexical block, in which case we simply search
7188 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7189 Again, this is only performed when the CU language allows this
7190 type of definitions. */
7191
7192static void
7193add_partial_subprogram (struct partial_die_info *pdi,
7194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7195 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7196{
7197 if (pdi->tag == DW_TAG_subprogram)
7198 {
7199 if (pdi->has_pc_info)
7200 {
7201 if (pdi->lowpc < *lowpc)
7202 *lowpc = pdi->lowpc;
7203 if (pdi->highpc > *highpc)
7204 *highpc = pdi->highpc;
cdc07690 7205 if (set_addrmap)
5734ee8b 7206 {
5734ee8b 7207 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7208 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7209 CORE_ADDR baseaddr;
7210 CORE_ADDR highpc;
7211 CORE_ADDR lowpc;
5734ee8b
DJ
7212
7213 baseaddr = ANOFFSET (objfile->section_offsets,
7214 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7215 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7216 pdi->lowpc + baseaddr);
7217 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7218 pdi->highpc + baseaddr);
7219 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7220 cu->per_cu->v.psymtab);
5734ee8b 7221 }
481860b3
GB
7222 }
7223
7224 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7225 {
bc30ff58 7226 if (!pdi->is_declaration)
e8d05480
JB
7227 /* Ignore subprogram DIEs that do not have a name, they are
7228 illegal. Do not emit a complaint at this point, we will
7229 do so when we convert this psymtab into a symtab. */
7230 if (pdi->name)
7231 add_partial_symbol (pdi, cu);
bc30ff58
JB
7232 }
7233 }
6e70227d 7234
bc30ff58
JB
7235 if (! pdi->has_children)
7236 return;
7237
7238 if (cu->language == language_ada)
7239 {
7240 pdi = pdi->die_child;
7241 while (pdi != NULL)
7242 {
7243 fixup_partial_die (pdi, cu);
7244 if (pdi->tag == DW_TAG_subprogram
7245 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7246 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7247 pdi = pdi->die_sibling;
7248 }
7249 }
7250}
7251
91c24f0a
DC
7252/* Read a partial die corresponding to an enumeration type. */
7253
72bf9492
DJ
7254static void
7255add_partial_enumeration (struct partial_die_info *enum_pdi,
7256 struct dwarf2_cu *cu)
91c24f0a 7257{
72bf9492 7258 struct partial_die_info *pdi;
91c24f0a
DC
7259
7260 if (enum_pdi->name != NULL)
72bf9492
DJ
7261 add_partial_symbol (enum_pdi, cu);
7262
7263 pdi = enum_pdi->die_child;
7264 while (pdi)
91c24f0a 7265 {
72bf9492 7266 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7267 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7268 else
72bf9492
DJ
7269 add_partial_symbol (pdi, cu);
7270 pdi = pdi->die_sibling;
91c24f0a 7271 }
91c24f0a
DC
7272}
7273
6caca83c
CC
7274/* Return the initial uleb128 in the die at INFO_PTR. */
7275
7276static unsigned int
d521ce57 7277peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7278{
7279 unsigned int bytes_read;
7280
7281 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7282}
7283
4bb7a0a7
DJ
7284/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7285 Return the corresponding abbrev, or NULL if the number is zero (indicating
7286 an empty DIE). In either case *BYTES_READ will be set to the length of
7287 the initial number. */
7288
7289static struct abbrev_info *
d521ce57 7290peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7291 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7292{
7293 bfd *abfd = cu->objfile->obfd;
7294 unsigned int abbrev_number;
7295 struct abbrev_info *abbrev;
7296
7297 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7298
7299 if (abbrev_number == 0)
7300 return NULL;
7301
433df2d4 7302 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7303 if (!abbrev)
7304 {
422b9917
DE
7305 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7306 " at offset 0x%x [in module %s]"),
7307 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7308 cu->header.offset.sect_off, bfd_get_filename (abfd));
4bb7a0a7
DJ
7309 }
7310
7311 return abbrev;
7312}
7313
93311388
DE
7314/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7315 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7316 DIE. Any children of the skipped DIEs will also be skipped. */
7317
d521ce57
TT
7318static const gdb_byte *
7319skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7320{
dee91e82 7321 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7322 struct abbrev_info *abbrev;
7323 unsigned int bytes_read;
7324
7325 while (1)
7326 {
7327 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7328 if (abbrev == NULL)
7329 return info_ptr + bytes_read;
7330 else
dee91e82 7331 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7332 }
7333}
7334
93311388
DE
7335/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7336 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7337 abbrev corresponding to that skipped uleb128 should be passed in
7338 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7339 children. */
7340
d521ce57
TT
7341static const gdb_byte *
7342skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7343 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7344{
7345 unsigned int bytes_read;
7346 struct attribute attr;
dee91e82
DE
7347 bfd *abfd = reader->abfd;
7348 struct dwarf2_cu *cu = reader->cu;
d521ce57 7349 const gdb_byte *buffer = reader->buffer;
f664829e 7350 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7351 unsigned int form, i;
7352
7353 for (i = 0; i < abbrev->num_attrs; i++)
7354 {
7355 /* The only abbrev we care about is DW_AT_sibling. */
7356 if (abbrev->attrs[i].name == DW_AT_sibling)
7357 {
dee91e82 7358 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7359 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7360 complaint (&symfile_complaints,
7361 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7362 else
b9502d3f
WN
7363 {
7364 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7365 const gdb_byte *sibling_ptr = buffer + off;
7366
7367 if (sibling_ptr < info_ptr)
7368 complaint (&symfile_complaints,
7369 _("DW_AT_sibling points backwards"));
22869d73
KS
7370 else if (sibling_ptr > reader->buffer_end)
7371 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7372 else
7373 return sibling_ptr;
7374 }
4bb7a0a7
DJ
7375 }
7376
7377 /* If it isn't DW_AT_sibling, skip this attribute. */
7378 form = abbrev->attrs[i].form;
7379 skip_attribute:
7380 switch (form)
7381 {
4bb7a0a7 7382 case DW_FORM_ref_addr:
ae411497
TT
7383 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7384 and later it is offset sized. */
7385 if (cu->header.version == 2)
7386 info_ptr += cu->header.addr_size;
7387 else
7388 info_ptr += cu->header.offset_size;
7389 break;
36586728
TT
7390 case DW_FORM_GNU_ref_alt:
7391 info_ptr += cu->header.offset_size;
7392 break;
ae411497 7393 case DW_FORM_addr:
4bb7a0a7
DJ
7394 info_ptr += cu->header.addr_size;
7395 break;
7396 case DW_FORM_data1:
7397 case DW_FORM_ref1:
7398 case DW_FORM_flag:
7399 info_ptr += 1;
7400 break;
2dc7f7b3 7401 case DW_FORM_flag_present:
43988095 7402 case DW_FORM_implicit_const:
2dc7f7b3 7403 break;
4bb7a0a7
DJ
7404 case DW_FORM_data2:
7405 case DW_FORM_ref2:
7406 info_ptr += 2;
7407 break;
7408 case DW_FORM_data4:
7409 case DW_FORM_ref4:
7410 info_ptr += 4;
7411 break;
7412 case DW_FORM_data8:
7413 case DW_FORM_ref8:
55f1336d 7414 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7415 info_ptr += 8;
7416 break;
7417 case DW_FORM_string:
9b1c24c8 7418 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7419 info_ptr += bytes_read;
7420 break;
2dc7f7b3 7421 case DW_FORM_sec_offset:
4bb7a0a7 7422 case DW_FORM_strp:
36586728 7423 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7424 info_ptr += cu->header.offset_size;
7425 break;
2dc7f7b3 7426 case DW_FORM_exprloc:
4bb7a0a7
DJ
7427 case DW_FORM_block:
7428 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7429 info_ptr += bytes_read;
7430 break;
7431 case DW_FORM_block1:
7432 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7433 break;
7434 case DW_FORM_block2:
7435 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7436 break;
7437 case DW_FORM_block4:
7438 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7439 break;
7440 case DW_FORM_sdata:
7441 case DW_FORM_udata:
7442 case DW_FORM_ref_udata:
3019eac3
DE
7443 case DW_FORM_GNU_addr_index:
7444 case DW_FORM_GNU_str_index:
d521ce57 7445 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7446 break;
7447 case DW_FORM_indirect:
7448 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7449 info_ptr += bytes_read;
7450 /* We need to continue parsing from here, so just go back to
7451 the top. */
7452 goto skip_attribute;
7453
7454 default:
3e43a32a
MS
7455 error (_("Dwarf Error: Cannot handle %s "
7456 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7457 dwarf_form_name (form),
7458 bfd_get_filename (abfd));
7459 }
7460 }
7461
7462 if (abbrev->has_children)
dee91e82 7463 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7464 else
7465 return info_ptr;
7466}
7467
93311388 7468/* Locate ORIG_PDI's sibling.
dee91e82 7469 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7470
d521ce57 7471static const gdb_byte *
dee91e82
DE
7472locate_pdi_sibling (const struct die_reader_specs *reader,
7473 struct partial_die_info *orig_pdi,
d521ce57 7474 const gdb_byte *info_ptr)
91c24f0a
DC
7475{
7476 /* Do we know the sibling already? */
72bf9492 7477
91c24f0a
DC
7478 if (orig_pdi->sibling)
7479 return orig_pdi->sibling;
7480
7481 /* Are there any children to deal with? */
7482
7483 if (!orig_pdi->has_children)
7484 return info_ptr;
7485
4bb7a0a7 7486 /* Skip the children the long way. */
91c24f0a 7487
dee91e82 7488 return skip_children (reader, info_ptr);
91c24f0a
DC
7489}
7490
257e7a09 7491/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7492 not NULL. */
c906108c
SS
7493
7494static void
257e7a09
YQ
7495dwarf2_read_symtab (struct partial_symtab *self,
7496 struct objfile *objfile)
c906108c 7497{
257e7a09 7498 if (self->readin)
c906108c 7499 {
442e4d9c 7500 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7501 self->filename);
442e4d9c
YQ
7502 }
7503 else
7504 {
7505 if (info_verbose)
c906108c 7506 {
442e4d9c 7507 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7508 self->filename);
442e4d9c 7509 gdb_flush (gdb_stdout);
c906108c 7510 }
c906108c 7511
442e4d9c 7512 /* Restore our global data. */
9a3c8263
SM
7513 dwarf2_per_objfile
7514 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7515 dwarf2_objfile_data_key);
10b3939b 7516
442e4d9c
YQ
7517 /* If this psymtab is constructed from a debug-only objfile, the
7518 has_section_at_zero flag will not necessarily be correct. We
7519 can get the correct value for this flag by looking at the data
7520 associated with the (presumably stripped) associated objfile. */
7521 if (objfile->separate_debug_objfile_backlink)
7522 {
7523 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7524 = ((struct dwarf2_per_objfile *)
7525 objfile_data (objfile->separate_debug_objfile_backlink,
7526 dwarf2_objfile_data_key));
9a619af0 7527
442e4d9c
YQ
7528 dwarf2_per_objfile->has_section_at_zero
7529 = dpo_backlink->has_section_at_zero;
7530 }
b2ab525c 7531
442e4d9c 7532 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7533
257e7a09 7534 psymtab_to_symtab_1 (self);
c906108c 7535
442e4d9c
YQ
7536 /* Finish up the debug error message. */
7537 if (info_verbose)
7538 printf_filtered (_("done.\n"));
c906108c 7539 }
95554aad
TT
7540
7541 process_cu_includes ();
c906108c 7542}
9cdd5dbd
DE
7543\f
7544/* Reading in full CUs. */
c906108c 7545
10b3939b
DJ
7546/* Add PER_CU to the queue. */
7547
7548static void
95554aad
TT
7549queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7550 enum language pretend_language)
10b3939b
DJ
7551{
7552 struct dwarf2_queue_item *item;
7553
7554 per_cu->queued = 1;
8d749320 7555 item = XNEW (struct dwarf2_queue_item);
10b3939b 7556 item->per_cu = per_cu;
95554aad 7557 item->pretend_language = pretend_language;
10b3939b
DJ
7558 item->next = NULL;
7559
7560 if (dwarf2_queue == NULL)
7561 dwarf2_queue = item;
7562 else
7563 dwarf2_queue_tail->next = item;
7564
7565 dwarf2_queue_tail = item;
7566}
7567
89e63ee4
DE
7568/* If PER_CU is not yet queued, add it to the queue.
7569 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7570 dependency.
0907af0c 7571 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7572 meaning either PER_CU is already queued or it is already loaded.
7573
7574 N.B. There is an invariant here that if a CU is queued then it is loaded.
7575 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7576
7577static int
89e63ee4 7578maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7579 struct dwarf2_per_cu_data *per_cu,
7580 enum language pretend_language)
7581{
7582 /* We may arrive here during partial symbol reading, if we need full
7583 DIEs to process an unusual case (e.g. template arguments). Do
7584 not queue PER_CU, just tell our caller to load its DIEs. */
7585 if (dwarf2_per_objfile->reading_partial_symbols)
7586 {
7587 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7588 return 1;
7589 return 0;
7590 }
7591
7592 /* Mark the dependence relation so that we don't flush PER_CU
7593 too early. */
89e63ee4
DE
7594 if (dependent_cu != NULL)
7595 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7596
7597 /* If it's already on the queue, we have nothing to do. */
7598 if (per_cu->queued)
7599 return 0;
7600
7601 /* If the compilation unit is already loaded, just mark it as
7602 used. */
7603 if (per_cu->cu != NULL)
7604 {
7605 per_cu->cu->last_used = 0;
7606 return 0;
7607 }
7608
7609 /* Add it to the queue. */
7610 queue_comp_unit (per_cu, pretend_language);
7611
7612 return 1;
7613}
7614
10b3939b
DJ
7615/* Process the queue. */
7616
7617static void
a0f42c21 7618process_queue (void)
10b3939b
DJ
7619{
7620 struct dwarf2_queue_item *item, *next_item;
7621
b4f54984 7622 if (dwarf_read_debug)
45cfd468
DE
7623 {
7624 fprintf_unfiltered (gdb_stdlog,
7625 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7626 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7627 }
7628
03dd20cc
DJ
7629 /* The queue starts out with one item, but following a DIE reference
7630 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7631 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7632 {
cc12ce38
DE
7633 if ((dwarf2_per_objfile->using_index
7634 ? !item->per_cu->v.quick->compunit_symtab
7635 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7636 /* Skip dummy CUs. */
7637 && item->per_cu->cu != NULL)
f4dc4d17
DE
7638 {
7639 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7640 unsigned int debug_print_threshold;
247f5c4f 7641 char buf[100];
f4dc4d17 7642
247f5c4f 7643 if (per_cu->is_debug_types)
f4dc4d17 7644 {
247f5c4f
DE
7645 struct signatured_type *sig_type =
7646 (struct signatured_type *) per_cu;
7647
7648 sprintf (buf, "TU %s at offset 0x%x",
73be47f5
DE
7649 hex_string (sig_type->signature),
7650 per_cu->offset.sect_off);
7651 /* There can be 100s of TUs.
7652 Only print them in verbose mode. */
7653 debug_print_threshold = 2;
f4dc4d17 7654 }
247f5c4f 7655 else
73be47f5
DE
7656 {
7657 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7658 debug_print_threshold = 1;
7659 }
247f5c4f 7660
b4f54984 7661 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7662 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7663
7664 if (per_cu->is_debug_types)
7665 process_full_type_unit (per_cu, item->pretend_language);
7666 else
7667 process_full_comp_unit (per_cu, item->pretend_language);
7668
b4f54984 7669 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7670 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7671 }
10b3939b
DJ
7672
7673 item->per_cu->queued = 0;
7674 next_item = item->next;
7675 xfree (item);
7676 }
7677
7678 dwarf2_queue_tail = NULL;
45cfd468 7679
b4f54984 7680 if (dwarf_read_debug)
45cfd468
DE
7681 {
7682 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7683 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7684 }
10b3939b
DJ
7685}
7686
7687/* Free all allocated queue entries. This function only releases anything if
7688 an error was thrown; if the queue was processed then it would have been
7689 freed as we went along. */
7690
7691static void
7692dwarf2_release_queue (void *dummy)
7693{
7694 struct dwarf2_queue_item *item, *last;
7695
7696 item = dwarf2_queue;
7697 while (item)
7698 {
7699 /* Anything still marked queued is likely to be in an
7700 inconsistent state, so discard it. */
7701 if (item->per_cu->queued)
7702 {
7703 if (item->per_cu->cu != NULL)
dee91e82 7704 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7705 item->per_cu->queued = 0;
7706 }
7707
7708 last = item;
7709 item = item->next;
7710 xfree (last);
7711 }
7712
7713 dwarf2_queue = dwarf2_queue_tail = NULL;
7714}
7715
7716/* Read in full symbols for PST, and anything it depends on. */
7717
c906108c 7718static void
fba45db2 7719psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7720{
10b3939b 7721 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7722 int i;
7723
95554aad
TT
7724 if (pst->readin)
7725 return;
7726
aaa75496 7727 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7728 if (!pst->dependencies[i]->readin
7729 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7730 {
7731 /* Inform about additional files that need to be read in. */
7732 if (info_verbose)
7733 {
a3f17187 7734 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7735 fputs_filtered (" ", gdb_stdout);
7736 wrap_here ("");
7737 fputs_filtered ("and ", gdb_stdout);
7738 wrap_here ("");
7739 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7740 wrap_here (""); /* Flush output. */
aaa75496
JB
7741 gdb_flush (gdb_stdout);
7742 }
7743 psymtab_to_symtab_1 (pst->dependencies[i]);
7744 }
7745
9a3c8263 7746 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7747
7748 if (per_cu == NULL)
aaa75496
JB
7749 {
7750 /* It's an include file, no symbols to read for it.
7751 Everything is in the parent symtab. */
7752 pst->readin = 1;
7753 return;
7754 }
c906108c 7755
a0f42c21 7756 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7757}
7758
dee91e82
DE
7759/* Trivial hash function for die_info: the hash value of a DIE
7760 is its offset in .debug_info for this objfile. */
10b3939b 7761
dee91e82
DE
7762static hashval_t
7763die_hash (const void *item)
10b3939b 7764{
9a3c8263 7765 const struct die_info *die = (const struct die_info *) item;
6502dd73 7766
dee91e82
DE
7767 return die->offset.sect_off;
7768}
63d06c5c 7769
dee91e82
DE
7770/* Trivial comparison function for die_info structures: two DIEs
7771 are equal if they have the same offset. */
98bfdba5 7772
dee91e82
DE
7773static int
7774die_eq (const void *item_lhs, const void *item_rhs)
7775{
9a3c8263
SM
7776 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7777 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7778
dee91e82
DE
7779 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7780}
c906108c 7781
dee91e82
DE
7782/* die_reader_func for load_full_comp_unit.
7783 This is identical to read_signatured_type_reader,
7784 but is kept separate for now. */
c906108c 7785
dee91e82
DE
7786static void
7787load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7788 const gdb_byte *info_ptr,
dee91e82
DE
7789 struct die_info *comp_unit_die,
7790 int has_children,
7791 void *data)
7792{
7793 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7794 enum language *language_ptr = (enum language *) data;
6caca83c 7795
dee91e82
DE
7796 gdb_assert (cu->die_hash == NULL);
7797 cu->die_hash =
7798 htab_create_alloc_ex (cu->header.length / 12,
7799 die_hash,
7800 die_eq,
7801 NULL,
7802 &cu->comp_unit_obstack,
7803 hashtab_obstack_allocate,
7804 dummy_obstack_deallocate);
e142c38c 7805
dee91e82
DE
7806 if (has_children)
7807 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7808 &info_ptr, comp_unit_die);
7809 cu->dies = comp_unit_die;
7810 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7811
7812 /* We try not to read any attributes in this function, because not
9cdd5dbd 7813 all CUs needed for references have been loaded yet, and symbol
10b3939b 7814 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7815 or we won't be able to build types correctly.
7816 Similarly, if we do not read the producer, we can not apply
7817 producer-specific interpretation. */
95554aad 7818 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7819}
10b3939b 7820
dee91e82 7821/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7822
dee91e82 7823static void
95554aad
TT
7824load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7825 enum language pretend_language)
dee91e82 7826{
3019eac3 7827 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7828
f4dc4d17
DE
7829 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7830 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7831}
7832
3da10d80
KS
7833/* Add a DIE to the delayed physname list. */
7834
7835static void
7836add_to_method_list (struct type *type, int fnfield_index, int index,
7837 const char *name, struct die_info *die,
7838 struct dwarf2_cu *cu)
7839{
7840 struct delayed_method_info mi;
7841 mi.type = type;
7842 mi.fnfield_index = fnfield_index;
7843 mi.index = index;
7844 mi.name = name;
7845 mi.die = die;
7846 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7847}
7848
7849/* A cleanup for freeing the delayed method list. */
7850
7851static void
7852free_delayed_list (void *ptr)
7853{
7854 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7855 if (cu->method_list != NULL)
7856 {
7857 VEC_free (delayed_method_info, cu->method_list);
7858 cu->method_list = NULL;
7859 }
7860}
7861
7862/* Compute the physnames of any methods on the CU's method list.
7863
7864 The computation of method physnames is delayed in order to avoid the
7865 (bad) condition that one of the method's formal parameters is of an as yet
7866 incomplete type. */
7867
7868static void
7869compute_delayed_physnames (struct dwarf2_cu *cu)
7870{
7871 int i;
7872 struct delayed_method_info *mi;
7873 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7874 {
1d06ead6 7875 const char *physname;
3da10d80
KS
7876 struct fn_fieldlist *fn_flp
7877 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7878 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7879 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7880 = physname ? physname : "";
3da10d80
KS
7881 }
7882}
7883
a766d390
DE
7884/* Go objects should be embedded in a DW_TAG_module DIE,
7885 and it's not clear if/how imported objects will appear.
7886 To keep Go support simple until that's worked out,
7887 go back through what we've read and create something usable.
7888 We could do this while processing each DIE, and feels kinda cleaner,
7889 but that way is more invasive.
7890 This is to, for example, allow the user to type "p var" or "b main"
7891 without having to specify the package name, and allow lookups
7892 of module.object to work in contexts that use the expression
7893 parser. */
7894
7895static void
7896fixup_go_packaging (struct dwarf2_cu *cu)
7897{
7898 char *package_name = NULL;
7899 struct pending *list;
7900 int i;
7901
7902 for (list = global_symbols; list != NULL; list = list->next)
7903 {
7904 for (i = 0; i < list->nsyms; ++i)
7905 {
7906 struct symbol *sym = list->symbol[i];
7907
7908 if (SYMBOL_LANGUAGE (sym) == language_go
7909 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7910 {
7911 char *this_package_name = go_symbol_package_name (sym);
7912
7913 if (this_package_name == NULL)
7914 continue;
7915 if (package_name == NULL)
7916 package_name = this_package_name;
7917 else
7918 {
7919 if (strcmp (package_name, this_package_name) != 0)
7920 complaint (&symfile_complaints,
7921 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
7922 (symbol_symtab (sym) != NULL
7923 ? symtab_to_filename_for_display
7924 (symbol_symtab (sym))
4262abfb 7925 : objfile_name (cu->objfile)),
a766d390
DE
7926 this_package_name, package_name);
7927 xfree (this_package_name);
7928 }
7929 }
7930 }
7931 }
7932
7933 if (package_name != NULL)
7934 {
7935 struct objfile *objfile = cu->objfile;
34a68019 7936 const char *saved_package_name
224c3ddb
SM
7937 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7938 package_name,
7939 strlen (package_name));
19f392bc
UW
7940 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7941 saved_package_name);
a766d390
DE
7942 struct symbol *sym;
7943
7944 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7945
e623cf5d 7946 sym = allocate_symbol (objfile);
f85f34ed 7947 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7948 SYMBOL_SET_NAMES (sym, saved_package_name,
7949 strlen (saved_package_name), 0, objfile);
a766d390
DE
7950 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7951 e.g., "main" finds the "main" module and not C's main(). */
7952 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7953 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7954 SYMBOL_TYPE (sym) = type;
7955
7956 add_symbol_to_list (sym, &global_symbols);
7957
7958 xfree (package_name);
7959 }
7960}
7961
95554aad
TT
7962/* Return the symtab for PER_CU. This works properly regardless of
7963 whether we're using the index or psymtabs. */
7964
43f3e411
DE
7965static struct compunit_symtab *
7966get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
7967{
7968 return (dwarf2_per_objfile->using_index
43f3e411
DE
7969 ? per_cu->v.quick->compunit_symtab
7970 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
7971}
7972
7973/* A helper function for computing the list of all symbol tables
7974 included by PER_CU. */
7975
7976static void
43f3e411 7977recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 7978 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 7979 struct dwarf2_per_cu_data *per_cu,
43f3e411 7980 struct compunit_symtab *immediate_parent)
95554aad
TT
7981{
7982 void **slot;
7983 int ix;
43f3e411 7984 struct compunit_symtab *cust;
95554aad
TT
7985 struct dwarf2_per_cu_data *iter;
7986
7987 slot = htab_find_slot (all_children, per_cu, INSERT);
7988 if (*slot != NULL)
7989 {
7990 /* This inclusion and its children have been processed. */
7991 return;
7992 }
7993
7994 *slot = per_cu;
7995 /* Only add a CU if it has a symbol table. */
43f3e411
DE
7996 cust = get_compunit_symtab (per_cu);
7997 if (cust != NULL)
ec94af83
DE
7998 {
7999 /* If this is a type unit only add its symbol table if we haven't
8000 seen it yet (type unit per_cu's can share symtabs). */
8001 if (per_cu->is_debug_types)
8002 {
43f3e411 8003 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8004 if (*slot == NULL)
8005 {
43f3e411
DE
8006 *slot = cust;
8007 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8008 if (cust->user == NULL)
8009 cust->user = immediate_parent;
ec94af83
DE
8010 }
8011 }
8012 else
f9125b6c 8013 {
43f3e411
DE
8014 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8015 if (cust->user == NULL)
8016 cust->user = immediate_parent;
f9125b6c 8017 }
ec94af83 8018 }
95554aad
TT
8019
8020 for (ix = 0;
796a7ff8 8021 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8022 ++ix)
ec94af83
DE
8023 {
8024 recursively_compute_inclusions (result, all_children,
43f3e411 8025 all_type_symtabs, iter, cust);
ec94af83 8026 }
95554aad
TT
8027}
8028
43f3e411 8029/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8030 PER_CU. */
8031
8032static void
43f3e411 8033compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8034{
f4dc4d17
DE
8035 gdb_assert (! per_cu->is_debug_types);
8036
796a7ff8 8037 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8038 {
8039 int ix, len;
ec94af83 8040 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8041 struct compunit_symtab *compunit_symtab_iter;
8042 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8043 htab_t all_children, all_type_symtabs;
43f3e411 8044 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8045
8046 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8047 if (cust == NULL)
95554aad
TT
8048 return;
8049
8050 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8051 NULL, xcalloc, xfree);
ec94af83
DE
8052 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8053 NULL, xcalloc, xfree);
95554aad
TT
8054
8055 for (ix = 0;
796a7ff8 8056 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8057 ix, per_cu_iter);
95554aad 8058 ++ix)
ec94af83
DE
8059 {
8060 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8061 all_type_symtabs, per_cu_iter,
43f3e411 8062 cust);
ec94af83 8063 }
95554aad 8064
ec94af83 8065 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8066 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8067 cust->includes
8d749320
SM
8068 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8069 struct compunit_symtab *, len + 1);
95554aad 8070 for (ix = 0;
43f3e411
DE
8071 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8072 compunit_symtab_iter);
95554aad 8073 ++ix)
43f3e411
DE
8074 cust->includes[ix] = compunit_symtab_iter;
8075 cust->includes[len] = NULL;
95554aad 8076
43f3e411 8077 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8078 htab_delete (all_children);
ec94af83 8079 htab_delete (all_type_symtabs);
95554aad
TT
8080 }
8081}
8082
8083/* Compute the 'includes' field for the symtabs of all the CUs we just
8084 read. */
8085
8086static void
8087process_cu_includes (void)
8088{
8089 int ix;
8090 struct dwarf2_per_cu_data *iter;
8091
8092 for (ix = 0;
8093 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8094 ix, iter);
8095 ++ix)
f4dc4d17
DE
8096 {
8097 if (! iter->is_debug_types)
43f3e411 8098 compute_compunit_symtab_includes (iter);
f4dc4d17 8099 }
95554aad
TT
8100
8101 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8102}
8103
9cdd5dbd 8104/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8105 already been loaded into memory. */
8106
8107static void
95554aad
TT
8108process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8109 enum language pretend_language)
10b3939b 8110{
10b3939b 8111 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8112 struct objfile *objfile = per_cu->objfile;
3e29f34a 8113 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8114 CORE_ADDR lowpc, highpc;
43f3e411 8115 struct compunit_symtab *cust;
3da10d80 8116 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8117 CORE_ADDR baseaddr;
4359dff1 8118 struct block *static_block;
3e29f34a 8119 CORE_ADDR addr;
10b3939b
DJ
8120
8121 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8122
10b3939b
DJ
8123 buildsym_init ();
8124 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8125 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8126
8127 cu->list_in_scope = &file_symbols;
c906108c 8128
95554aad
TT
8129 cu->language = pretend_language;
8130 cu->language_defn = language_def (cu->language);
8131
c906108c 8132 /* Do line number decoding in read_file_scope () */
10b3939b 8133 process_die (cu->dies, cu);
c906108c 8134
a766d390
DE
8135 /* For now fudge the Go package. */
8136 if (cu->language == language_go)
8137 fixup_go_packaging (cu);
8138
3da10d80
KS
8139 /* Now that we have processed all the DIEs in the CU, all the types
8140 should be complete, and it should now be safe to compute all of the
8141 physnames. */
8142 compute_delayed_physnames (cu);
8143 do_cleanups (delayed_list_cleanup);
8144
fae299cd
DC
8145 /* Some compilers don't define a DW_AT_high_pc attribute for the
8146 compilation unit. If the DW_AT_high_pc is missing, synthesize
8147 it, by scanning the DIE's below the compilation unit. */
10b3939b 8148 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8149
3e29f34a
MR
8150 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8151 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8152
8153 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8154 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8155 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8156 addrmap to help ensure it has an accurate map of pc values belonging to
8157 this comp unit. */
8158 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8159
43f3e411
DE
8160 cust = end_symtab_from_static_block (static_block,
8161 SECT_OFF_TEXT (objfile), 0);
c906108c 8162
43f3e411 8163 if (cust != NULL)
c906108c 8164 {
df15bd07 8165 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8166
8be455d7
JK
8167 /* Set symtab language to language from DW_AT_language. If the
8168 compilation is from a C file generated by language preprocessors, do
8169 not set the language if it was already deduced by start_subfile. */
43f3e411 8170 if (!(cu->language == language_c
40e3ad0e 8171 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8172 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8173
8174 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8175 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8176 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8177 there were bugs in prologue debug info, fixed later in GCC-4.5
8178 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8179
8180 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8181 needed, it would be wrong due to missing DW_AT_producer there.
8182
8183 Still one can confuse GDB by using non-standard GCC compilation
8184 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8185 */
ab260dad 8186 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8187 cust->locations_valid = 1;
e0d00bc7
JK
8188
8189 if (gcc_4_minor >= 5)
43f3e411 8190 cust->epilogue_unwind_valid = 1;
96408a79 8191
43f3e411 8192 cust->call_site_htab = cu->call_site_htab;
c906108c 8193 }
9291a0cd
TT
8194
8195 if (dwarf2_per_objfile->using_index)
43f3e411 8196 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8197 else
8198 {
8199 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8200 pst->compunit_symtab = cust;
9291a0cd
TT
8201 pst->readin = 1;
8202 }
c906108c 8203
95554aad
TT
8204 /* Push it for inclusion processing later. */
8205 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8206
c906108c 8207 do_cleanups (back_to);
f4dc4d17 8208}
45cfd468 8209
f4dc4d17
DE
8210/* Generate full symbol information for type unit PER_CU, whose DIEs have
8211 already been loaded into memory. */
8212
8213static void
8214process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8215 enum language pretend_language)
8216{
8217 struct dwarf2_cu *cu = per_cu->cu;
8218 struct objfile *objfile = per_cu->objfile;
43f3e411 8219 struct compunit_symtab *cust;
f4dc4d17 8220 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8221 struct signatured_type *sig_type;
8222
8223 gdb_assert (per_cu->is_debug_types);
8224 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8225
8226 buildsym_init ();
8227 back_to = make_cleanup (really_free_pendings, NULL);
8228 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8229
8230 cu->list_in_scope = &file_symbols;
8231
8232 cu->language = pretend_language;
8233 cu->language_defn = language_def (cu->language);
8234
8235 /* The symbol tables are set up in read_type_unit_scope. */
8236 process_die (cu->dies, cu);
8237
8238 /* For now fudge the Go package. */
8239 if (cu->language == language_go)
8240 fixup_go_packaging (cu);
8241
8242 /* Now that we have processed all the DIEs in the CU, all the types
8243 should be complete, and it should now be safe to compute all of the
8244 physnames. */
8245 compute_delayed_physnames (cu);
8246 do_cleanups (delayed_list_cleanup);
8247
8248 /* TUs share symbol tables.
8249 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8250 of it with end_expandable_symtab. Otherwise, complete the addition of
8251 this TU's symbols to the existing symtab. */
43f3e411 8252 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8253 {
43f3e411
DE
8254 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8255 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8256
43f3e411 8257 if (cust != NULL)
f4dc4d17
DE
8258 {
8259 /* Set symtab language to language from DW_AT_language. If the
8260 compilation is from a C file generated by language preprocessors,
8261 do not set the language if it was already deduced by
8262 start_subfile. */
43f3e411
DE
8263 if (!(cu->language == language_c
8264 && COMPUNIT_FILETABS (cust)->language != language_c))
8265 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8266 }
8267 }
8268 else
8269 {
0ab9ce85 8270 augment_type_symtab ();
43f3e411 8271 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8272 }
8273
8274 if (dwarf2_per_objfile->using_index)
43f3e411 8275 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8276 else
8277 {
8278 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8279 pst->compunit_symtab = cust;
f4dc4d17 8280 pst->readin = 1;
45cfd468 8281 }
f4dc4d17
DE
8282
8283 do_cleanups (back_to);
c906108c
SS
8284}
8285
95554aad
TT
8286/* Process an imported unit DIE. */
8287
8288static void
8289process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8290{
8291 struct attribute *attr;
8292
f4dc4d17
DE
8293 /* For now we don't handle imported units in type units. */
8294 if (cu->per_cu->is_debug_types)
8295 {
8296 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8297 " supported in type units [in module %s]"),
4262abfb 8298 objfile_name (cu->objfile));
f4dc4d17
DE
8299 }
8300
95554aad
TT
8301 attr = dwarf2_attr (die, DW_AT_import, cu);
8302 if (attr != NULL)
8303 {
8304 struct dwarf2_per_cu_data *per_cu;
95554aad 8305 sect_offset offset;
36586728 8306 int is_dwz;
95554aad
TT
8307
8308 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
8309 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8310 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 8311
69d751e3 8312 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8313 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8314 load_full_comp_unit (per_cu, cu->language);
8315
796a7ff8 8316 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8317 per_cu);
8318 }
8319}
8320
adde2bff
DE
8321/* Reset the in_process bit of a die. */
8322
8323static void
8324reset_die_in_process (void *arg)
8325{
9a3c8263 8326 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8327
adde2bff
DE
8328 die->in_process = 0;
8329}
8330
c906108c
SS
8331/* Process a die and its children. */
8332
8333static void
e7c27a73 8334process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8335{
adde2bff
DE
8336 struct cleanup *in_process;
8337
8338 /* We should only be processing those not already in process. */
8339 gdb_assert (!die->in_process);
8340
8341 die->in_process = 1;
8342 in_process = make_cleanup (reset_die_in_process,die);
8343
c906108c
SS
8344 switch (die->tag)
8345 {
8346 case DW_TAG_padding:
8347 break;
8348 case DW_TAG_compile_unit:
95554aad 8349 case DW_TAG_partial_unit:
e7c27a73 8350 read_file_scope (die, cu);
c906108c 8351 break;
348e048f
DE
8352 case DW_TAG_type_unit:
8353 read_type_unit_scope (die, cu);
8354 break;
c906108c 8355 case DW_TAG_subprogram:
c906108c 8356 case DW_TAG_inlined_subroutine:
edb3359d 8357 read_func_scope (die, cu);
c906108c
SS
8358 break;
8359 case DW_TAG_lexical_block:
14898363
L
8360 case DW_TAG_try_block:
8361 case DW_TAG_catch_block:
e7c27a73 8362 read_lexical_block_scope (die, cu);
c906108c 8363 break;
216f72a1 8364 case DW_TAG_call_site:
96408a79
SA
8365 case DW_TAG_GNU_call_site:
8366 read_call_site_scope (die, cu);
8367 break;
c906108c 8368 case DW_TAG_class_type:
680b30c7 8369 case DW_TAG_interface_type:
c906108c
SS
8370 case DW_TAG_structure_type:
8371 case DW_TAG_union_type:
134d01f1 8372 process_structure_scope (die, cu);
c906108c
SS
8373 break;
8374 case DW_TAG_enumeration_type:
134d01f1 8375 process_enumeration_scope (die, cu);
c906108c 8376 break;
134d01f1 8377
f792889a
DJ
8378 /* These dies have a type, but processing them does not create
8379 a symbol or recurse to process the children. Therefore we can
8380 read them on-demand through read_type_die. */
c906108c 8381 case DW_TAG_subroutine_type:
72019c9c 8382 case DW_TAG_set_type:
c906108c 8383 case DW_TAG_array_type:
c906108c 8384 case DW_TAG_pointer_type:
c906108c 8385 case DW_TAG_ptr_to_member_type:
c906108c 8386 case DW_TAG_reference_type:
c906108c 8387 case DW_TAG_string_type:
c906108c 8388 break;
134d01f1 8389
c906108c 8390 case DW_TAG_base_type:
a02abb62 8391 case DW_TAG_subrange_type:
cb249c71 8392 case DW_TAG_typedef:
134d01f1
DJ
8393 /* Add a typedef symbol for the type definition, if it has a
8394 DW_AT_name. */
f792889a 8395 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8396 break;
c906108c 8397 case DW_TAG_common_block:
e7c27a73 8398 read_common_block (die, cu);
c906108c
SS
8399 break;
8400 case DW_TAG_common_inclusion:
8401 break;
d9fa45fe 8402 case DW_TAG_namespace:
4d4ec4e5 8403 cu->processing_has_namespace_info = 1;
e7c27a73 8404 read_namespace (die, cu);
d9fa45fe 8405 break;
5d7cb8df 8406 case DW_TAG_module:
4d4ec4e5 8407 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8408 read_module (die, cu);
8409 break;
d9fa45fe 8410 case DW_TAG_imported_declaration:
74921315
KS
8411 cu->processing_has_namespace_info = 1;
8412 if (read_namespace_alias (die, cu))
8413 break;
8414 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8415 case DW_TAG_imported_module:
4d4ec4e5 8416 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8417 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8418 || cu->language != language_fortran))
8419 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8420 dwarf_tag_name (die->tag));
8421 read_import_statement (die, cu);
d9fa45fe 8422 break;
95554aad
TT
8423
8424 case DW_TAG_imported_unit:
8425 process_imported_unit_die (die, cu);
8426 break;
8427
c906108c 8428 default:
e7c27a73 8429 new_symbol (die, NULL, cu);
c906108c
SS
8430 break;
8431 }
adde2bff
DE
8432
8433 do_cleanups (in_process);
c906108c 8434}
ca69b9e6
DE
8435\f
8436/* DWARF name computation. */
c906108c 8437
94af9270
KS
8438/* A helper function for dwarf2_compute_name which determines whether DIE
8439 needs to have the name of the scope prepended to the name listed in the
8440 die. */
8441
8442static int
8443die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8444{
1c809c68
TT
8445 struct attribute *attr;
8446
94af9270
KS
8447 switch (die->tag)
8448 {
8449 case DW_TAG_namespace:
8450 case DW_TAG_typedef:
8451 case DW_TAG_class_type:
8452 case DW_TAG_interface_type:
8453 case DW_TAG_structure_type:
8454 case DW_TAG_union_type:
8455 case DW_TAG_enumeration_type:
8456 case DW_TAG_enumerator:
8457 case DW_TAG_subprogram:
08a76f8a 8458 case DW_TAG_inlined_subroutine:
94af9270 8459 case DW_TAG_member:
74921315 8460 case DW_TAG_imported_declaration:
94af9270
KS
8461 return 1;
8462
8463 case DW_TAG_variable:
c2b0a229 8464 case DW_TAG_constant:
94af9270
KS
8465 /* We only need to prefix "globally" visible variables. These include
8466 any variable marked with DW_AT_external or any variable that
8467 lives in a namespace. [Variables in anonymous namespaces
8468 require prefixing, but they are not DW_AT_external.] */
8469
8470 if (dwarf2_attr (die, DW_AT_specification, cu))
8471 {
8472 struct dwarf2_cu *spec_cu = cu;
9a619af0 8473
94af9270
KS
8474 return die_needs_namespace (die_specification (die, &spec_cu),
8475 spec_cu);
8476 }
8477
1c809c68 8478 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8479 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8480 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8481 return 0;
8482 /* A variable in a lexical block of some kind does not need a
8483 namespace, even though in C++ such variables may be external
8484 and have a mangled name. */
8485 if (die->parent->tag == DW_TAG_lexical_block
8486 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8487 || die->parent->tag == DW_TAG_catch_block
8488 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8489 return 0;
8490 return 1;
94af9270
KS
8491
8492 default:
8493 return 0;
8494 }
8495}
8496
8497/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8498 compute the physname for the object, which include a method's:
9c37b5ae 8499 - formal parameters (C++),
a766d390 8500 - receiver type (Go),
a766d390
DE
8501
8502 The term "physname" is a bit confusing.
8503 For C++, for example, it is the demangled name.
8504 For Go, for example, it's the mangled name.
94af9270 8505
af6b7be1
JB
8506 For Ada, return the DIE's linkage name rather than the fully qualified
8507 name. PHYSNAME is ignored..
8508
94af9270
KS
8509 The result is allocated on the objfile_obstack and canonicalized. */
8510
8511static const char *
15d034d0
TT
8512dwarf2_compute_name (const char *name,
8513 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8514 int physname)
8515{
bb5ed363
DE
8516 struct objfile *objfile = cu->objfile;
8517
94af9270
KS
8518 if (name == NULL)
8519 name = dwarf2_name (die, cu);
8520
2ee7123e
DE
8521 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8522 but otherwise compute it by typename_concat inside GDB.
8523 FIXME: Actually this is not really true, or at least not always true.
8524 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8525 Fortran names because there is no mangling standard. So new_symbol_full
8526 will set the demangled name to the result of dwarf2_full_name, and it is
8527 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8528 if (cu->language == language_ada
8529 || (cu->language == language_fortran && physname))
8530 {
8531 /* For Ada unit, we prefer the linkage name over the name, as
8532 the former contains the exported name, which the user expects
8533 to be able to reference. Ideally, we want the user to be able
8534 to reference this entity using either natural or linkage name,
8535 but we haven't started looking at this enhancement yet. */
2ee7123e 8536 const char *linkage_name;
f55ee35c 8537
2ee7123e
DE
8538 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8539 if (linkage_name == NULL)
8540 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8541 if (linkage_name != NULL)
8542 return linkage_name;
f55ee35c
JK
8543 }
8544
94af9270
KS
8545 /* These are the only languages we know how to qualify names in. */
8546 if (name != NULL
9c37b5ae 8547 && (cu->language == language_cplus
c44af4eb
TT
8548 || cu->language == language_fortran || cu->language == language_d
8549 || cu->language == language_rust))
94af9270
KS
8550 {
8551 if (die_needs_namespace (die, cu))
8552 {
8553 long length;
0d5cff50 8554 const char *prefix;
34a68019 8555 const char *canonical_name = NULL;
94af9270 8556
d7e74731
PA
8557 string_file buf;
8558
94af9270 8559 prefix = determine_prefix (die, cu);
94af9270
KS
8560 if (*prefix != '\0')
8561 {
f55ee35c
JK
8562 char *prefixed_name = typename_concat (NULL, prefix, name,
8563 physname, cu);
9a619af0 8564
d7e74731 8565 buf.puts (prefixed_name);
94af9270
KS
8566 xfree (prefixed_name);
8567 }
8568 else
d7e74731 8569 buf.puts (name);
94af9270 8570
98bfdba5
PA
8571 /* Template parameters may be specified in the DIE's DW_AT_name, or
8572 as children with DW_TAG_template_type_param or
8573 DW_TAG_value_type_param. If the latter, add them to the name
8574 here. If the name already has template parameters, then
8575 skip this step; some versions of GCC emit both, and
8576 it is more efficient to use the pre-computed name.
8577
8578 Something to keep in mind about this process: it is very
8579 unlikely, or in some cases downright impossible, to produce
8580 something that will match the mangled name of a function.
8581 If the definition of the function has the same debug info,
8582 we should be able to match up with it anyway. But fallbacks
8583 using the minimal symbol, for instance to find a method
8584 implemented in a stripped copy of libstdc++, will not work.
8585 If we do not have debug info for the definition, we will have to
8586 match them up some other way.
8587
8588 When we do name matching there is a related problem with function
8589 templates; two instantiated function templates are allowed to
8590 differ only by their return types, which we do not add here. */
8591
8592 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8593 {
8594 struct attribute *attr;
8595 struct die_info *child;
8596 int first = 1;
8597
8598 die->building_fullname = 1;
8599
8600 for (child = die->child; child != NULL; child = child->sibling)
8601 {
8602 struct type *type;
12df843f 8603 LONGEST value;
d521ce57 8604 const gdb_byte *bytes;
98bfdba5
PA
8605 struct dwarf2_locexpr_baton *baton;
8606 struct value *v;
8607
8608 if (child->tag != DW_TAG_template_type_param
8609 && child->tag != DW_TAG_template_value_param)
8610 continue;
8611
8612 if (first)
8613 {
d7e74731 8614 buf.puts ("<");
98bfdba5
PA
8615 first = 0;
8616 }
8617 else
d7e74731 8618 buf.puts (", ");
98bfdba5
PA
8619
8620 attr = dwarf2_attr (child, DW_AT_type, cu);
8621 if (attr == NULL)
8622 {
8623 complaint (&symfile_complaints,
8624 _("template parameter missing DW_AT_type"));
d7e74731 8625 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8626 continue;
8627 }
8628 type = die_type (child, cu);
8629
8630 if (child->tag == DW_TAG_template_type_param)
8631 {
d7e74731 8632 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8633 continue;
8634 }
8635
8636 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8637 if (attr == NULL)
8638 {
8639 complaint (&symfile_complaints,
3e43a32a
MS
8640 _("template parameter missing "
8641 "DW_AT_const_value"));
d7e74731 8642 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8643 continue;
8644 }
8645
8646 dwarf2_const_value_attr (attr, type, name,
8647 &cu->comp_unit_obstack, cu,
8648 &value, &bytes, &baton);
8649
8650 if (TYPE_NOSIGN (type))
8651 /* GDB prints characters as NUMBER 'CHAR'. If that's
8652 changed, this can use value_print instead. */
d7e74731 8653 c_printchar (value, type, &buf);
98bfdba5
PA
8654 else
8655 {
8656 struct value_print_options opts;
8657
8658 if (baton != NULL)
8659 v = dwarf2_evaluate_loc_desc (type, NULL,
8660 baton->data,
8661 baton->size,
8662 baton->per_cu);
8663 else if (bytes != NULL)
8664 {
8665 v = allocate_value (type);
8666 memcpy (value_contents_writeable (v), bytes,
8667 TYPE_LENGTH (type));
8668 }
8669 else
8670 v = value_from_longest (type, value);
8671
3e43a32a
MS
8672 /* Specify decimal so that we do not depend on
8673 the radix. */
98bfdba5
PA
8674 get_formatted_print_options (&opts, 'd');
8675 opts.raw = 1;
d7e74731 8676 value_print (v, &buf, &opts);
98bfdba5
PA
8677 release_value (v);
8678 value_free (v);
8679 }
8680 }
8681
8682 die->building_fullname = 0;
8683
8684 if (!first)
8685 {
8686 /* Close the argument list, with a space if necessary
8687 (nested templates). */
d7e74731
PA
8688 if (!buf.empty () && buf.string ().back () == '>')
8689 buf.puts (" >");
98bfdba5 8690 else
d7e74731 8691 buf.puts (">");
98bfdba5
PA
8692 }
8693 }
8694
9c37b5ae 8695 /* For C++ methods, append formal parameter type
94af9270 8696 information, if PHYSNAME. */
6e70227d 8697
94af9270 8698 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8699 && cu->language == language_cplus)
94af9270
KS
8700 {
8701 struct type *type = read_type_die (die, cu);
8702
d7e74731 8703 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8704 &type_print_raw_options);
94af9270 8705
9c37b5ae 8706 if (cu->language == language_cplus)
94af9270 8707 {
60430eff
DJ
8708 /* Assume that an artificial first parameter is
8709 "this", but do not crash if it is not. RealView
8710 marks unnamed (and thus unused) parameters as
8711 artificial; there is no way to differentiate
8712 the two cases. */
94af9270
KS
8713 if (TYPE_NFIELDS (type) > 0
8714 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8715 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8716 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8717 0))))
d7e74731 8718 buf.puts (" const");
94af9270
KS
8719 }
8720 }
8721
d7e74731 8722 const std::string &intermediate_name = buf.string ();
94af9270
KS
8723
8724 if (cu->language == language_cplus)
34a68019 8725 canonical_name
322a8516 8726 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8727 &objfile->per_bfd->storage_obstack);
8728
8729 /* If we only computed INTERMEDIATE_NAME, or if
8730 INTERMEDIATE_NAME is already canonical, then we need to
8731 copy it to the appropriate obstack. */
322a8516 8732 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8733 name = ((const char *)
8734 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8735 intermediate_name.c_str (),
8736 intermediate_name.length ()));
34a68019
TT
8737 else
8738 name = canonical_name;
94af9270
KS
8739 }
8740 }
8741
8742 return name;
8743}
8744
0114d602
DJ
8745/* Return the fully qualified name of DIE, based on its DW_AT_name.
8746 If scope qualifiers are appropriate they will be added. The result
34a68019 8747 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8748 not have a name. NAME may either be from a previous call to
8749 dwarf2_name or NULL.
8750
9c37b5ae 8751 The output string will be canonicalized (if C++). */
0114d602
DJ
8752
8753static const char *
15d034d0 8754dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8755{
94af9270
KS
8756 return dwarf2_compute_name (name, die, cu, 0);
8757}
0114d602 8758
94af9270
KS
8759/* Construct a physname for the given DIE in CU. NAME may either be
8760 from a previous call to dwarf2_name or NULL. The result will be
8761 allocated on the objfile_objstack or NULL if the DIE does not have a
8762 name.
0114d602 8763
9c37b5ae 8764 The output string will be canonicalized (if C++). */
0114d602 8765
94af9270 8766static const char *
15d034d0 8767dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8768{
bb5ed363 8769 struct objfile *objfile = cu->objfile;
900e11f9
JK
8770 const char *retval, *mangled = NULL, *canon = NULL;
8771 struct cleanup *back_to;
8772 int need_copy = 1;
8773
8774 /* In this case dwarf2_compute_name is just a shortcut not building anything
8775 on its own. */
8776 if (!die_needs_namespace (die, cu))
8777 return dwarf2_compute_name (name, die, cu, 1);
8778
8779 back_to = make_cleanup (null_cleanup, NULL);
8780
7d45c7c3
KB
8781 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8782 if (mangled == NULL)
8783 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 8784
e98c9e7c
TT
8785 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8786 See https://github.com/rust-lang/rust/issues/32925. */
8787 if (cu->language == language_rust && mangled != NULL
8788 && strchr (mangled, '{') != NULL)
8789 mangled = NULL;
8790
900e11f9
JK
8791 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8792 has computed. */
7d45c7c3 8793 if (mangled != NULL)
900e11f9
JK
8794 {
8795 char *demangled;
8796
900e11f9
JK
8797 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8798 type. It is easier for GDB users to search for such functions as
8799 `name(params)' than `long name(params)'. In such case the minimal
8800 symbol names do not match the full symbol names but for template
8801 functions there is never a need to look up their definition from their
8802 declaration so the only disadvantage remains the minimal symbol
8803 variant `long name(params)' does not have the proper inferior type.
8804 */
8805
a766d390
DE
8806 if (cu->language == language_go)
8807 {
8808 /* This is a lie, but we already lie to the caller new_symbol_full.
8809 new_symbol_full assumes we return the mangled name.
8810 This just undoes that lie until things are cleaned up. */
8811 demangled = NULL;
8812 }
8813 else
8814 {
8de20a37 8815 demangled = gdb_demangle (mangled,
9c37b5ae 8816 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 8817 }
900e11f9
JK
8818 if (demangled)
8819 {
8820 make_cleanup (xfree, demangled);
8821 canon = demangled;
8822 }
8823 else
8824 {
8825 canon = mangled;
8826 need_copy = 0;
8827 }
8828 }
8829
8830 if (canon == NULL || check_physname)
8831 {
8832 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8833
8834 if (canon != NULL && strcmp (physname, canon) != 0)
8835 {
8836 /* It may not mean a bug in GDB. The compiler could also
8837 compute DW_AT_linkage_name incorrectly. But in such case
8838 GDB would need to be bug-to-bug compatible. */
8839
8840 complaint (&symfile_complaints,
8841 _("Computed physname <%s> does not match demangled <%s> "
8842 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8843 physname, canon, mangled, die->offset.sect_off,
8844 objfile_name (objfile));
900e11f9
JK
8845
8846 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8847 is available here - over computed PHYSNAME. It is safer
8848 against both buggy GDB and buggy compilers. */
8849
8850 retval = canon;
8851 }
8852 else
8853 {
8854 retval = physname;
8855 need_copy = 0;
8856 }
8857 }
8858 else
8859 retval = canon;
8860
8861 if (need_copy)
224c3ddb
SM
8862 retval = ((const char *)
8863 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8864 retval, strlen (retval)));
900e11f9
JK
8865
8866 do_cleanups (back_to);
8867 return retval;
0114d602
DJ
8868}
8869
74921315
KS
8870/* Inspect DIE in CU for a namespace alias. If one exists, record
8871 a new symbol for it.
8872
8873 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8874
8875static int
8876read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8877{
8878 struct attribute *attr;
8879
8880 /* If the die does not have a name, this is not a namespace
8881 alias. */
8882 attr = dwarf2_attr (die, DW_AT_name, cu);
8883 if (attr != NULL)
8884 {
8885 int num;
8886 struct die_info *d = die;
8887 struct dwarf2_cu *imported_cu = cu;
8888
8889 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8890 keep inspecting DIEs until we hit the underlying import. */
8891#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8892 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8893 {
8894 attr = dwarf2_attr (d, DW_AT_import, cu);
8895 if (attr == NULL)
8896 break;
8897
8898 d = follow_die_ref (d, attr, &imported_cu);
8899 if (d->tag != DW_TAG_imported_declaration)
8900 break;
8901 }
8902
8903 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8904 {
8905 complaint (&symfile_complaints,
8906 _("DIE at 0x%x has too many recursively imported "
8907 "declarations"), d->offset.sect_off);
8908 return 0;
8909 }
8910
8911 if (attr != NULL)
8912 {
8913 struct type *type;
8914 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8915
8916 type = get_die_type_at_offset (offset, cu->per_cu);
8917 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8918 {
8919 /* This declaration is a global namespace alias. Add
8920 a symbol for it whose type is the aliased namespace. */
8921 new_symbol (die, type, cu);
8922 return 1;
8923 }
8924 }
8925 }
8926
8927 return 0;
8928}
8929
22cee43f
PMR
8930/* Return the using directives repository (global or local?) to use in the
8931 current context for LANGUAGE.
8932
8933 For Ada, imported declarations can materialize renamings, which *may* be
8934 global. However it is impossible (for now?) in DWARF to distinguish
8935 "external" imported declarations and "static" ones. As all imported
8936 declarations seem to be static in all other languages, make them all CU-wide
8937 global only in Ada. */
8938
8939static struct using_direct **
8940using_directives (enum language language)
8941{
8942 if (language == language_ada && context_stack_depth == 0)
8943 return &global_using_directives;
8944 else
8945 return &local_using_directives;
8946}
8947
27aa8d6a
SW
8948/* Read the import statement specified by the given die and record it. */
8949
8950static void
8951read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8952{
bb5ed363 8953 struct objfile *objfile = cu->objfile;
27aa8d6a 8954 struct attribute *import_attr;
32019081 8955 struct die_info *imported_die, *child_die;
de4affc9 8956 struct dwarf2_cu *imported_cu;
27aa8d6a 8957 const char *imported_name;
794684b6 8958 const char *imported_name_prefix;
13387711
SW
8959 const char *canonical_name;
8960 const char *import_alias;
8961 const char *imported_declaration = NULL;
794684b6 8962 const char *import_prefix;
32019081
JK
8963 VEC (const_char_ptr) *excludes = NULL;
8964 struct cleanup *cleanups;
13387711 8965
27aa8d6a
SW
8966 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8967 if (import_attr == NULL)
8968 {
8969 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8970 dwarf_tag_name (die->tag));
8971 return;
8972 }
8973
de4affc9
CC
8974 imported_cu = cu;
8975 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8976 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8977 if (imported_name == NULL)
8978 {
8979 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8980
8981 The import in the following code:
8982 namespace A
8983 {
8984 typedef int B;
8985 }
8986
8987 int main ()
8988 {
8989 using A::B;
8990 B b;
8991 return b;
8992 }
8993
8994 ...
8995 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8996 <52> DW_AT_decl_file : 1
8997 <53> DW_AT_decl_line : 6
8998 <54> DW_AT_import : <0x75>
8999 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9000 <59> DW_AT_name : B
9001 <5b> DW_AT_decl_file : 1
9002 <5c> DW_AT_decl_line : 2
9003 <5d> DW_AT_type : <0x6e>
9004 ...
9005 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9006 <76> DW_AT_byte_size : 4
9007 <77> DW_AT_encoding : 5 (signed)
9008
9009 imports the wrong die ( 0x75 instead of 0x58 ).
9010 This case will be ignored until the gcc bug is fixed. */
9011 return;
9012 }
9013
82856980
SW
9014 /* Figure out the local name after import. */
9015 import_alias = dwarf2_name (die, cu);
27aa8d6a 9016
794684b6
SW
9017 /* Figure out where the statement is being imported to. */
9018 import_prefix = determine_prefix (die, cu);
9019
9020 /* Figure out what the scope of the imported die is and prepend it
9021 to the name of the imported die. */
de4affc9 9022 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9023
f55ee35c
JK
9024 if (imported_die->tag != DW_TAG_namespace
9025 && imported_die->tag != DW_TAG_module)
794684b6 9026 {
13387711
SW
9027 imported_declaration = imported_name;
9028 canonical_name = imported_name_prefix;
794684b6 9029 }
13387711 9030 else if (strlen (imported_name_prefix) > 0)
12aaed36 9031 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9032 imported_name_prefix,
9033 (cu->language == language_d ? "." : "::"),
9034 imported_name, (char *) NULL);
13387711
SW
9035 else
9036 canonical_name = imported_name;
794684b6 9037
32019081
JK
9038 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9039
9040 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9041 for (child_die = die->child; child_die && child_die->tag;
9042 child_die = sibling_die (child_die))
9043 {
9044 /* DWARF-4: A Fortran use statement with a “rename list” may be
9045 represented by an imported module entry with an import attribute
9046 referring to the module and owned entries corresponding to those
9047 entities that are renamed as part of being imported. */
9048
9049 if (child_die->tag != DW_TAG_imported_declaration)
9050 {
9051 complaint (&symfile_complaints,
9052 _("child DW_TAG_imported_declaration expected "
9053 "- DIE at 0x%x [in module %s]"),
4262abfb 9054 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9055 continue;
9056 }
9057
9058 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9059 if (import_attr == NULL)
9060 {
9061 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9062 dwarf_tag_name (child_die->tag));
9063 continue;
9064 }
9065
9066 imported_cu = cu;
9067 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9068 &imported_cu);
9069 imported_name = dwarf2_name (imported_die, imported_cu);
9070 if (imported_name == NULL)
9071 {
9072 complaint (&symfile_complaints,
9073 _("child DW_TAG_imported_declaration has unknown "
9074 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 9075 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9076 continue;
9077 }
9078
9079 VEC_safe_push (const_char_ptr, excludes, imported_name);
9080
9081 process_die (child_die, cu);
9082 }
9083
22cee43f
PMR
9084 add_using_directive (using_directives (cu->language),
9085 import_prefix,
9086 canonical_name,
9087 import_alias,
9088 imported_declaration,
9089 excludes,
9090 0,
9091 &objfile->objfile_obstack);
32019081
JK
9092
9093 do_cleanups (cleanups);
27aa8d6a
SW
9094}
9095
f4dc4d17 9096/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 9097
cb1df416
DJ
9098static void
9099free_cu_line_header (void *arg)
9100{
9a3c8263 9101 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
cb1df416
DJ
9102
9103 free_line_header (cu->line_header);
9104 cu->line_header = NULL;
9105}
9106
1b80a9fa
JK
9107/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9108 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9109 this, it was first present in GCC release 4.3.0. */
9110
9111static int
9112producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9113{
9114 if (!cu->checked_producer)
9115 check_producer (cu);
9116
9117 return cu->producer_is_gcc_lt_4_3;
9118}
9119
9291a0cd
TT
9120static void
9121find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 9122 const char **name, const char **comp_dir)
9291a0cd 9123{
9291a0cd
TT
9124 /* Find the filename. Do not use dwarf2_name here, since the filename
9125 is not a source language identifier. */
7d45c7c3
KB
9126 *name = dwarf2_string_attr (die, DW_AT_name, cu);
9127 *comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9128
7d45c7c3
KB
9129 if (*comp_dir == NULL
9130 && producer_is_gcc_lt_4_3 (cu) && *name != NULL
9131 && IS_ABSOLUTE_PATH (*name))
9291a0cd 9132 {
15d034d0
TT
9133 char *d = ldirname (*name);
9134
9135 *comp_dir = d;
9136 if (d != NULL)
9137 make_cleanup (xfree, d);
9291a0cd
TT
9138 }
9139 if (*comp_dir != NULL)
9140 {
9141 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9142 directory, get rid of it. */
e6a959d6 9143 const char *cp = strchr (*comp_dir, ':');
9291a0cd
TT
9144
9145 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
9146 *comp_dir = cp + 1;
9147 }
9148
9149 if (*name == NULL)
9150 *name = "<unknown>";
9151}
9152
f4dc4d17
DE
9153/* Handle DW_AT_stmt_list for a compilation unit.
9154 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9155 COMP_DIR is the compilation directory. LOWPC is passed to
9156 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9157
9158static void
9159handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9160 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9161{
527f3840 9162 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9163 struct attribute *attr;
527f3840
JK
9164 unsigned int line_offset;
9165 struct line_header line_header_local;
9166 hashval_t line_header_local_hash;
9167 unsigned u;
9168 void **slot;
9169 int decode_mapping;
2ab95328 9170
f4dc4d17
DE
9171 gdb_assert (! cu->per_cu->is_debug_types);
9172
2ab95328 9173 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9174 if (attr == NULL)
9175 return;
9176
9177 line_offset = DW_UNSND (attr);
9178
9179 /* The line header hash table is only created if needed (it exists to
9180 prevent redundant reading of the line table for partial_units).
9181 If we're given a partial_unit, we'll need it. If we're given a
9182 compile_unit, then use the line header hash table if it's already
9183 created, but don't create one just yet. */
9184
9185 if (dwarf2_per_objfile->line_header_hash == NULL
9186 && die->tag == DW_TAG_partial_unit)
2ab95328 9187 {
527f3840
JK
9188 dwarf2_per_objfile->line_header_hash
9189 = htab_create_alloc_ex (127, line_header_hash_voidp,
9190 line_header_eq_voidp,
9191 free_line_header_voidp,
9192 &objfile->objfile_obstack,
9193 hashtab_obstack_allocate,
9194 dummy_obstack_deallocate);
9195 }
2ab95328 9196
527f3840
JK
9197 line_header_local.offset.sect_off = line_offset;
9198 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9199 line_header_local_hash = line_header_hash (&line_header_local);
9200 if (dwarf2_per_objfile->line_header_hash != NULL)
9201 {
9202 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9203 &line_header_local,
9204 line_header_local_hash, NO_INSERT);
9205
9206 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9207 is not present in *SLOT (since if there is something in *SLOT then
9208 it will be for a partial_unit). */
9209 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9210 {
527f3840 9211 gdb_assert (*slot != NULL);
9a3c8263 9212 cu->line_header = (struct line_header *) *slot;
527f3840 9213 return;
dee91e82 9214 }
2ab95328 9215 }
527f3840
JK
9216
9217 /* dwarf_decode_line_header does not yet provide sufficient information.
9218 We always have to call also dwarf_decode_lines for it. */
9219 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9220 if (cu->line_header == NULL)
9221 return;
9222
9223 if (dwarf2_per_objfile->line_header_hash == NULL)
9224 slot = NULL;
9225 else
9226 {
9227 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9228 &line_header_local,
9229 line_header_local_hash, INSERT);
9230 gdb_assert (slot != NULL);
9231 }
9232 if (slot != NULL && *slot == NULL)
9233 {
9234 /* This newly decoded line number information unit will be owned
9235 by line_header_hash hash table. */
9236 *slot = cu->line_header;
9237 }
9238 else
9239 {
9240 /* We cannot free any current entry in (*slot) as that struct line_header
9241 may be already used by multiple CUs. Create only temporary decoded
9242 line_header for this CU - it may happen at most once for each line
9243 number information unit. And if we're not using line_header_hash
9244 then this is what we want as well. */
9245 gdb_assert (die->tag != DW_TAG_partial_unit);
9246 make_cleanup (free_cu_line_header, cu);
9247 }
9248 decode_mapping = (die->tag != DW_TAG_partial_unit);
9249 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9250 decode_mapping);
2ab95328
TT
9251}
9252
95554aad 9253/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9254
c906108c 9255static void
e7c27a73 9256read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9257{
dee91e82 9258 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9259 struct gdbarch *gdbarch = get_objfile_arch (objfile);
debd256d 9260 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 9261 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9262 CORE_ADDR highpc = ((CORE_ADDR) 0);
9263 struct attribute *attr;
15d034d0
TT
9264 const char *name = NULL;
9265 const char *comp_dir = NULL;
c906108c 9266 struct die_info *child_die;
e142c38c 9267 CORE_ADDR baseaddr;
6e70227d 9268
e142c38c 9269 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9270
fae299cd 9271 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9272
9273 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9274 from finish_block. */
2acceee2 9275 if (lowpc == ((CORE_ADDR) -1))
c906108c 9276 lowpc = highpc;
3e29f34a 9277 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9278
9291a0cd 9279 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 9280
95554aad 9281 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9282
f4b8a18d
KW
9283 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9284 standardised yet. As a workaround for the language detection we fall
9285 back to the DW_AT_producer string. */
9286 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9287 cu->language = language_opencl;
9288
3019eac3
DE
9289 /* Similar hack for Go. */
9290 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9291 set_cu_language (DW_LANG_Go, cu);
9292
f4dc4d17 9293 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
9294
9295 /* Decode line number information if present. We do this before
9296 processing child DIEs, so that the line header table is available
9297 for DW_AT_decl_file. */
c3b7b696 9298 handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);
3019eac3
DE
9299
9300 /* Process all dies in compilation unit. */
9301 if (die->child != NULL)
9302 {
9303 child_die = die->child;
9304 while (child_die && child_die->tag)
9305 {
9306 process_die (child_die, cu);
9307 child_die = sibling_die (child_die);
9308 }
9309 }
9310
9311 /* Decode macro information, if present. Dwarf 2 macro information
9312 refers to information in the line number info statement program
9313 header, so we can only read it if we've read the header
9314 successfully. */
0af92d60
JK
9315 attr = dwarf2_attr (die, DW_AT_macros, cu);
9316 if (attr == NULL)
9317 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9318 if (attr && cu->line_header)
9319 {
9320 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9321 complaint (&symfile_complaints,
0af92d60 9322 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9323
43f3e411 9324 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9325 }
9326 else
9327 {
9328 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9329 if (attr && cu->line_header)
9330 {
9331 unsigned int macro_offset = DW_UNSND (attr);
9332
43f3e411 9333 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9334 }
9335 }
9336
9337 do_cleanups (back_to);
9338}
9339
f4dc4d17
DE
9340/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9341 Create the set of symtabs used by this TU, or if this TU is sharing
9342 symtabs with another TU and the symtabs have already been created
9343 then restore those symtabs in the line header.
9344 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9345
9346static void
f4dc4d17 9347setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9348{
f4dc4d17
DE
9349 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9350 struct type_unit_group *tu_group;
9351 int first_time;
9352 struct line_header *lh;
3019eac3 9353 struct attribute *attr;
f4dc4d17 9354 unsigned int i, line_offset;
0186c6a7 9355 struct signatured_type *sig_type;
3019eac3 9356
f4dc4d17 9357 gdb_assert (per_cu->is_debug_types);
0186c6a7 9358 sig_type = (struct signatured_type *) per_cu;
3019eac3 9359
f4dc4d17 9360 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9361
f4dc4d17 9362 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9363 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9364 if (sig_type->type_unit_group == NULL)
9365 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9366 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9367
9368 /* If we've already processed this stmt_list there's no real need to
9369 do it again, we could fake it and just recreate the part we need
9370 (file name,index -> symtab mapping). If data shows this optimization
9371 is useful we can do it then. */
43f3e411 9372 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9373
9374 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9375 debug info. */
9376 lh = NULL;
9377 if (attr != NULL)
3019eac3 9378 {
f4dc4d17
DE
9379 line_offset = DW_UNSND (attr);
9380 lh = dwarf_decode_line_header (line_offset, cu);
9381 }
9382 if (lh == NULL)
9383 {
9384 if (first_time)
9385 dwarf2_start_symtab (cu, "", NULL, 0);
9386 else
9387 {
9388 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9389 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9390 }
f4dc4d17 9391 return;
3019eac3
DE
9392 }
9393
f4dc4d17
DE
9394 cu->line_header = lh;
9395 make_cleanup (free_cu_line_header, cu);
3019eac3 9396
f4dc4d17
DE
9397 if (first_time)
9398 {
43f3e411 9399 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9400
1fd60fc0
DE
9401 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9402 still initializing it, and our caller (a few levels up)
9403 process_full_type_unit still needs to know if this is the first
9404 time. */
9405
f4dc4d17
DE
9406 tu_group->num_symtabs = lh->num_file_names;
9407 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 9408
f4dc4d17
DE
9409 for (i = 0; i < lh->num_file_names; ++i)
9410 {
d521ce57 9411 const char *dir = NULL;
f4dc4d17 9412 struct file_entry *fe = &lh->file_names[i];
3019eac3 9413
afa6c9ab 9414 if (fe->dir_index && lh->include_dirs != NULL)
f4dc4d17 9415 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 9416 dwarf2_start_subfile (fe->name, dir);
3019eac3 9417
f4dc4d17
DE
9418 if (current_subfile->symtab == NULL)
9419 {
9420 /* NOTE: start_subfile will recognize when it's been passed
9421 a file it has already seen. So we can't assume there's a
43f3e411 9422 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9423 lh->file_names may contain dups. */
43f3e411
DE
9424 current_subfile->symtab
9425 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9426 }
9427
9428 fe->symtab = current_subfile->symtab;
9429 tu_group->symtabs[i] = fe->symtab;
9430 }
9431 }
9432 else
3019eac3 9433 {
0ab9ce85 9434 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17
DE
9435
9436 for (i = 0; i < lh->num_file_names; ++i)
9437 {
9438 struct file_entry *fe = &lh->file_names[i];
9439
9440 fe->symtab = tu_group->symtabs[i];
9441 }
3019eac3
DE
9442 }
9443
f4dc4d17
DE
9444 /* The main symtab is allocated last. Type units don't have DW_AT_name
9445 so they don't have a "real" (so to speak) symtab anyway.
9446 There is later code that will assign the main symtab to all symbols
9447 that don't have one. We need to handle the case of a symbol with a
9448 missing symtab (DW_AT_decl_file) anyway. */
9449}
3019eac3 9450
f4dc4d17
DE
9451/* Process DW_TAG_type_unit.
9452 For TUs we want to skip the first top level sibling if it's not the
9453 actual type being defined by this TU. In this case the first top
9454 level sibling is there to provide context only. */
3019eac3 9455
f4dc4d17
DE
9456static void
9457read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9458{
9459 struct die_info *child_die;
3019eac3 9460
f4dc4d17
DE
9461 prepare_one_comp_unit (cu, die, language_minimal);
9462
9463 /* Initialize (or reinitialize) the machinery for building symtabs.
9464 We do this before processing child DIEs, so that the line header table
9465 is available for DW_AT_decl_file. */
9466 setup_type_unit_groups (die, cu);
9467
9468 if (die->child != NULL)
9469 {
9470 child_die = die->child;
9471 while (child_die && child_die->tag)
9472 {
9473 process_die (child_die, cu);
9474 child_die = sibling_die (child_die);
9475 }
9476 }
3019eac3
DE
9477}
9478\f
80626a55
DE
9479/* DWO/DWP files.
9480
9481 http://gcc.gnu.org/wiki/DebugFission
9482 http://gcc.gnu.org/wiki/DebugFissionDWP
9483
9484 To simplify handling of both DWO files ("object" files with the DWARF info)
9485 and DWP files (a file with the DWOs packaged up into one file), we treat
9486 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9487
9488static hashval_t
9489hash_dwo_file (const void *item)
9490{
9a3c8263 9491 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9492 hashval_t hash;
3019eac3 9493
a2ce51a0
DE
9494 hash = htab_hash_string (dwo_file->dwo_name);
9495 if (dwo_file->comp_dir != NULL)
9496 hash += htab_hash_string (dwo_file->comp_dir);
9497 return hash;
3019eac3
DE
9498}
9499
9500static int
9501eq_dwo_file (const void *item_lhs, const void *item_rhs)
9502{
9a3c8263
SM
9503 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9504 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9505
a2ce51a0
DE
9506 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9507 return 0;
9508 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9509 return lhs->comp_dir == rhs->comp_dir;
9510 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9511}
9512
9513/* Allocate a hash table for DWO files. */
9514
9515static htab_t
9516allocate_dwo_file_hash_table (void)
9517{
9518 struct objfile *objfile = dwarf2_per_objfile->objfile;
9519
9520 return htab_create_alloc_ex (41,
9521 hash_dwo_file,
9522 eq_dwo_file,
9523 NULL,
9524 &objfile->objfile_obstack,
9525 hashtab_obstack_allocate,
9526 dummy_obstack_deallocate);
9527}
9528
80626a55
DE
9529/* Lookup DWO file DWO_NAME. */
9530
9531static void **
0ac5b59e 9532lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9533{
9534 struct dwo_file find_entry;
9535 void **slot;
9536
9537 if (dwarf2_per_objfile->dwo_files == NULL)
9538 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9539
9540 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9541 find_entry.dwo_name = dwo_name;
9542 find_entry.comp_dir = comp_dir;
80626a55
DE
9543 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9544
9545 return slot;
9546}
9547
3019eac3
DE
9548static hashval_t
9549hash_dwo_unit (const void *item)
9550{
9a3c8263 9551 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9552
9553 /* This drops the top 32 bits of the id, but is ok for a hash. */
9554 return dwo_unit->signature;
9555}
9556
9557static int
9558eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9559{
9a3c8263
SM
9560 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9561 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9562
9563 /* The signature is assumed to be unique within the DWO file.
9564 So while object file CU dwo_id's always have the value zero,
9565 that's OK, assuming each object file DWO file has only one CU,
9566 and that's the rule for now. */
9567 return lhs->signature == rhs->signature;
9568}
9569
9570/* Allocate a hash table for DWO CUs,TUs.
9571 There is one of these tables for each of CUs,TUs for each DWO file. */
9572
9573static htab_t
9574allocate_dwo_unit_table (struct objfile *objfile)
9575{
9576 /* Start out with a pretty small number.
9577 Generally DWO files contain only one CU and maybe some TUs. */
9578 return htab_create_alloc_ex (3,
9579 hash_dwo_unit,
9580 eq_dwo_unit,
9581 NULL,
9582 &objfile->objfile_obstack,
9583 hashtab_obstack_allocate,
9584 dummy_obstack_deallocate);
9585}
9586
80626a55 9587/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9588
19c3d4c9 9589struct create_dwo_cu_data
3019eac3
DE
9590{
9591 struct dwo_file *dwo_file;
19c3d4c9 9592 struct dwo_unit dwo_unit;
3019eac3
DE
9593};
9594
19c3d4c9 9595/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9596
9597static void
19c3d4c9
DE
9598create_dwo_cu_reader (const struct die_reader_specs *reader,
9599 const gdb_byte *info_ptr,
9600 struct die_info *comp_unit_die,
9601 int has_children,
9602 void *datap)
3019eac3
DE
9603{
9604 struct dwarf2_cu *cu = reader->cu;
3019eac3 9605 sect_offset offset = cu->per_cu->offset;
8a0459fd 9606 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9607 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9608 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9609 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9610 struct attribute *attr;
3019eac3
DE
9611
9612 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9613 if (attr == NULL)
9614 {
19c3d4c9
DE
9615 complaint (&symfile_complaints,
9616 _("Dwarf Error: debug entry at offset 0x%x is missing"
9617 " its dwo_id [in module %s]"),
9618 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
9619 return;
9620 }
9621
3019eac3
DE
9622 dwo_unit->dwo_file = dwo_file;
9623 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9624 dwo_unit->section = section;
3019eac3
DE
9625 dwo_unit->offset = offset;
9626 dwo_unit->length = cu->per_cu->length;
9627
b4f54984 9628 if (dwarf_read_debug)
4031ecc5
DE
9629 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9630 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
9631}
9632
19c3d4c9
DE
9633/* Create the dwo_unit for the lone CU in DWO_FILE.
9634 Note: This function processes DWO files only, not DWP files. */
3019eac3 9635
19c3d4c9
DE
9636static struct dwo_unit *
9637create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9638{
9639 struct objfile *objfile = dwarf2_per_objfile->objfile;
9640 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9641 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9642 struct create_dwo_cu_data create_dwo_cu_data;
9643 struct dwo_unit *dwo_unit;
3019eac3
DE
9644
9645 dwarf2_read_section (objfile, section);
9646 info_ptr = section->buffer;
9647
9648 if (info_ptr == NULL)
9649 return NULL;
9650
b4f54984 9651 if (dwarf_read_debug)
19c3d4c9
DE
9652 {
9653 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9654 get_section_name (section),
9655 get_section_file_name (section));
19c3d4c9 9656 }
3019eac3 9657
19c3d4c9
DE
9658 create_dwo_cu_data.dwo_file = dwo_file;
9659 dwo_unit = NULL;
3019eac3
DE
9660
9661 end_ptr = info_ptr + section->size;
9662 while (info_ptr < end_ptr)
9663 {
9664 struct dwarf2_per_cu_data per_cu;
9665
19c3d4c9
DE
9666 memset (&create_dwo_cu_data.dwo_unit, 0,
9667 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9668 memset (&per_cu, 0, sizeof (per_cu));
9669 per_cu.objfile = objfile;
9670 per_cu.is_debug_types = 0;
9671 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 9672 per_cu.section = section;
3019eac3 9673
33e80786 9674 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9675 create_dwo_cu_reader,
9676 &create_dwo_cu_data);
9677
9678 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9679 {
9680 /* If we've already found one, complain. We only support one
9681 because having more than one requires hacking the dwo_name of
9682 each to match, which is highly unlikely to happen. */
9683 if (dwo_unit != NULL)
9684 {
9685 complaint (&symfile_complaints,
9686 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9687 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9688 break;
9689 }
9690
9691 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9692 *dwo_unit = create_dwo_cu_data.dwo_unit;
9693 }
3019eac3
DE
9694
9695 info_ptr += per_cu.length;
9696 }
9697
19c3d4c9 9698 return dwo_unit;
3019eac3
DE
9699}
9700
80626a55
DE
9701/* DWP file .debug_{cu,tu}_index section format:
9702 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9703
d2415c6c
DE
9704 DWP Version 1:
9705
80626a55
DE
9706 Both index sections have the same format, and serve to map a 64-bit
9707 signature to a set of section numbers. Each section begins with a header,
9708 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9709 indexes, and a pool of 32-bit section numbers. The index sections will be
9710 aligned at 8-byte boundaries in the file.
9711
d2415c6c
DE
9712 The index section header consists of:
9713
9714 V, 32 bit version number
9715 -, 32 bits unused
9716 N, 32 bit number of compilation units or type units in the index
9717 M, 32 bit number of slots in the hash table
80626a55 9718
d2415c6c 9719 Numbers are recorded using the byte order of the application binary.
80626a55 9720
d2415c6c
DE
9721 The hash table begins at offset 16 in the section, and consists of an array
9722 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9723 order of the application binary). Unused slots in the hash table are 0.
9724 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9725
d2415c6c
DE
9726 The parallel table begins immediately after the hash table
9727 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9728 array of 32-bit indexes (using the byte order of the application binary),
9729 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9730 table contains a 32-bit index into the pool of section numbers. For unused
9731 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9732
73869dc2
DE
9733 The pool of section numbers begins immediately following the hash table
9734 (at offset 16 + 12 * M from the beginning of the section). The pool of
9735 section numbers consists of an array of 32-bit words (using the byte order
9736 of the application binary). Each item in the array is indexed starting
9737 from 0. The hash table entry provides the index of the first section
9738 number in the set. Additional section numbers in the set follow, and the
9739 set is terminated by a 0 entry (section number 0 is not used in ELF).
9740
9741 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9742 section must be the first entry in the set, and the .debug_abbrev.dwo must
9743 be the second entry. Other members of the set may follow in any order.
9744
9745 ---
9746
9747 DWP Version 2:
9748
9749 DWP Version 2 combines all the .debug_info, etc. sections into one,
9750 and the entries in the index tables are now offsets into these sections.
9751 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9752 section.
9753
9754 Index Section Contents:
9755 Header
9756 Hash Table of Signatures dwp_hash_table.hash_table
9757 Parallel Table of Indices dwp_hash_table.unit_table
9758 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9759 Table of Section Sizes dwp_hash_table.v2.sizes
9760
9761 The index section header consists of:
9762
9763 V, 32 bit version number
9764 L, 32 bit number of columns in the table of section offsets
9765 N, 32 bit number of compilation units or type units in the index
9766 M, 32 bit number of slots in the hash table
9767
9768 Numbers are recorded using the byte order of the application binary.
9769
9770 The hash table has the same format as version 1.
9771 The parallel table of indices has the same format as version 1,
9772 except that the entries are origin-1 indices into the table of sections
9773 offsets and the table of section sizes.
9774
9775 The table of offsets begins immediately following the parallel table
9776 (at offset 16 + 12 * M from the beginning of the section). The table is
9777 a two-dimensional array of 32-bit words (using the byte order of the
9778 application binary), with L columns and N+1 rows, in row-major order.
9779 Each row in the array is indexed starting from 0. The first row provides
9780 a key to the remaining rows: each column in this row provides an identifier
9781 for a debug section, and the offsets in the same column of subsequent rows
9782 refer to that section. The section identifiers are:
9783
9784 DW_SECT_INFO 1 .debug_info.dwo
9785 DW_SECT_TYPES 2 .debug_types.dwo
9786 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9787 DW_SECT_LINE 4 .debug_line.dwo
9788 DW_SECT_LOC 5 .debug_loc.dwo
9789 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9790 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9791 DW_SECT_MACRO 8 .debug_macro.dwo
9792
9793 The offsets provided by the CU and TU index sections are the base offsets
9794 for the contributions made by each CU or TU to the corresponding section
9795 in the package file. Each CU and TU header contains an abbrev_offset
9796 field, used to find the abbreviations table for that CU or TU within the
9797 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9798 be interpreted as relative to the base offset given in the index section.
9799 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9800 should be interpreted as relative to the base offset for .debug_line.dwo,
9801 and offsets into other debug sections obtained from DWARF attributes should
9802 also be interpreted as relative to the corresponding base offset.
9803
9804 The table of sizes begins immediately following the table of offsets.
9805 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9806 with L columns and N rows, in row-major order. Each row in the array is
9807 indexed starting from 1 (row 0 is shared by the two tables).
9808
9809 ---
9810
9811 Hash table lookup is handled the same in version 1 and 2:
9812
9813 We assume that N and M will not exceed 2^32 - 1.
9814 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9815
d2415c6c
DE
9816 Given a 64-bit compilation unit signature or a type signature S, an entry
9817 in the hash table is located as follows:
80626a55 9818
d2415c6c
DE
9819 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9820 the low-order k bits all set to 1.
80626a55 9821
d2415c6c 9822 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9823
d2415c6c
DE
9824 3) If the hash table entry at index H matches the signature, use that
9825 entry. If the hash table entry at index H is unused (all zeroes),
9826 terminate the search: the signature is not present in the table.
80626a55 9827
d2415c6c 9828 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9829
d2415c6c 9830 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9831 to stop at an unused slot or find the match. */
80626a55
DE
9832
9833/* Create a hash table to map DWO IDs to their CU/TU entry in
9834 .debug_{info,types}.dwo in DWP_FILE.
9835 Returns NULL if there isn't one.
9836 Note: This function processes DWP files only, not DWO files. */
9837
9838static struct dwp_hash_table *
9839create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9840{
9841 struct objfile *objfile = dwarf2_per_objfile->objfile;
9842 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9843 const gdb_byte *index_ptr, *index_end;
80626a55 9844 struct dwarf2_section_info *index;
73869dc2 9845 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9846 struct dwp_hash_table *htab;
9847
9848 if (is_debug_types)
9849 index = &dwp_file->sections.tu_index;
9850 else
9851 index = &dwp_file->sections.cu_index;
9852
9853 if (dwarf2_section_empty_p (index))
9854 return NULL;
9855 dwarf2_read_section (objfile, index);
9856
9857 index_ptr = index->buffer;
9858 index_end = index_ptr + index->size;
9859
9860 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9861 index_ptr += 4;
9862 if (version == 2)
9863 nr_columns = read_4_bytes (dbfd, index_ptr);
9864 else
9865 nr_columns = 0;
9866 index_ptr += 4;
80626a55
DE
9867 nr_units = read_4_bytes (dbfd, index_ptr);
9868 index_ptr += 4;
9869 nr_slots = read_4_bytes (dbfd, index_ptr);
9870 index_ptr += 4;
9871
73869dc2 9872 if (version != 1 && version != 2)
80626a55 9873 {
21aa081e 9874 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9875 " [in module %s]"),
21aa081e 9876 pulongest (version), dwp_file->name);
80626a55
DE
9877 }
9878 if (nr_slots != (nr_slots & -nr_slots))
9879 {
21aa081e 9880 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9881 " is not power of 2 [in module %s]"),
21aa081e 9882 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9883 }
9884
9885 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9886 htab->version = version;
9887 htab->nr_columns = nr_columns;
80626a55
DE
9888 htab->nr_units = nr_units;
9889 htab->nr_slots = nr_slots;
9890 htab->hash_table = index_ptr;
9891 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9892
9893 /* Exit early if the table is empty. */
9894 if (nr_slots == 0 || nr_units == 0
9895 || (version == 2 && nr_columns == 0))
9896 {
9897 /* All must be zero. */
9898 if (nr_slots != 0 || nr_units != 0
9899 || (version == 2 && nr_columns != 0))
9900 {
9901 complaint (&symfile_complaints,
9902 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9903 " all zero [in modules %s]"),
9904 dwp_file->name);
9905 }
9906 return htab;
9907 }
9908
9909 if (version == 1)
9910 {
9911 htab->section_pool.v1.indices =
9912 htab->unit_table + sizeof (uint32_t) * nr_slots;
9913 /* It's harder to decide whether the section is too small in v1.
9914 V1 is deprecated anyway so we punt. */
9915 }
9916 else
9917 {
9918 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9919 int *ids = htab->section_pool.v2.section_ids;
9920 /* Reverse map for error checking. */
9921 int ids_seen[DW_SECT_MAX + 1];
9922 int i;
9923
9924 if (nr_columns < 2)
9925 {
9926 error (_("Dwarf Error: bad DWP hash table, too few columns"
9927 " in section table [in module %s]"),
9928 dwp_file->name);
9929 }
9930 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9931 {
9932 error (_("Dwarf Error: bad DWP hash table, too many columns"
9933 " in section table [in module %s]"),
9934 dwp_file->name);
9935 }
9936 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9937 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9938 for (i = 0; i < nr_columns; ++i)
9939 {
9940 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9941
9942 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9943 {
9944 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9945 " in section table [in module %s]"),
9946 id, dwp_file->name);
9947 }
9948 if (ids_seen[id] != -1)
9949 {
9950 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9951 " id %d in section table [in module %s]"),
9952 id, dwp_file->name);
9953 }
9954 ids_seen[id] = i;
9955 ids[i] = id;
9956 }
9957 /* Must have exactly one info or types section. */
9958 if (((ids_seen[DW_SECT_INFO] != -1)
9959 + (ids_seen[DW_SECT_TYPES] != -1))
9960 != 1)
9961 {
9962 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9963 " DWO info/types section [in module %s]"),
9964 dwp_file->name);
9965 }
9966 /* Must have an abbrev section. */
9967 if (ids_seen[DW_SECT_ABBREV] == -1)
9968 {
9969 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9970 " section [in module %s]"),
9971 dwp_file->name);
9972 }
9973 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9974 htab->section_pool.v2.sizes =
9975 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9976 * nr_units * nr_columns);
9977 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9978 * nr_units * nr_columns))
9979 > index_end)
9980 {
9981 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9982 " [in module %s]"),
9983 dwp_file->name);
9984 }
9985 }
80626a55
DE
9986
9987 return htab;
9988}
9989
9990/* Update SECTIONS with the data from SECTP.
9991
9992 This function is like the other "locate" section routines that are
9993 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 9994 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
9995
9996 The result is non-zero for success, or zero if an error was found. */
9997
9998static int
73869dc2
DE
9999locate_v1_virtual_dwo_sections (asection *sectp,
10000 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10001{
10002 const struct dwop_section_names *names = &dwop_section_names;
10003
10004 if (section_is_p (sectp->name, &names->abbrev_dwo))
10005 {
10006 /* There can be only one. */
049412e3 10007 if (sections->abbrev.s.section != NULL)
80626a55 10008 return 0;
049412e3 10009 sections->abbrev.s.section = sectp;
80626a55
DE
10010 sections->abbrev.size = bfd_get_section_size (sectp);
10011 }
10012 else if (section_is_p (sectp->name, &names->info_dwo)
10013 || section_is_p (sectp->name, &names->types_dwo))
10014 {
10015 /* There can be only one. */
049412e3 10016 if (sections->info_or_types.s.section != NULL)
80626a55 10017 return 0;
049412e3 10018 sections->info_or_types.s.section = sectp;
80626a55
DE
10019 sections->info_or_types.size = bfd_get_section_size (sectp);
10020 }
10021 else if (section_is_p (sectp->name, &names->line_dwo))
10022 {
10023 /* There can be only one. */
049412e3 10024 if (sections->line.s.section != NULL)
80626a55 10025 return 0;
049412e3 10026 sections->line.s.section = sectp;
80626a55
DE
10027 sections->line.size = bfd_get_section_size (sectp);
10028 }
10029 else if (section_is_p (sectp->name, &names->loc_dwo))
10030 {
10031 /* There can be only one. */
049412e3 10032 if (sections->loc.s.section != NULL)
80626a55 10033 return 0;
049412e3 10034 sections->loc.s.section = sectp;
80626a55
DE
10035 sections->loc.size = bfd_get_section_size (sectp);
10036 }
10037 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10038 {
10039 /* There can be only one. */
049412e3 10040 if (sections->macinfo.s.section != NULL)
80626a55 10041 return 0;
049412e3 10042 sections->macinfo.s.section = sectp;
80626a55
DE
10043 sections->macinfo.size = bfd_get_section_size (sectp);
10044 }
10045 else if (section_is_p (sectp->name, &names->macro_dwo))
10046 {
10047 /* There can be only one. */
049412e3 10048 if (sections->macro.s.section != NULL)
80626a55 10049 return 0;
049412e3 10050 sections->macro.s.section = sectp;
80626a55
DE
10051 sections->macro.size = bfd_get_section_size (sectp);
10052 }
10053 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10054 {
10055 /* There can be only one. */
049412e3 10056 if (sections->str_offsets.s.section != NULL)
80626a55 10057 return 0;
049412e3 10058 sections->str_offsets.s.section = sectp;
80626a55
DE
10059 sections->str_offsets.size = bfd_get_section_size (sectp);
10060 }
10061 else
10062 {
10063 /* No other kind of section is valid. */
10064 return 0;
10065 }
10066
10067 return 1;
10068}
10069
73869dc2
DE
10070/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10071 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10072 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10073 This is for DWP version 1 files. */
80626a55
DE
10074
10075static struct dwo_unit *
73869dc2
DE
10076create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10077 uint32_t unit_index,
10078 const char *comp_dir,
10079 ULONGEST signature, int is_debug_types)
80626a55
DE
10080{
10081 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10082 const struct dwp_hash_table *dwp_htab =
10083 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10084 bfd *dbfd = dwp_file->dbfd;
10085 const char *kind = is_debug_types ? "TU" : "CU";
10086 struct dwo_file *dwo_file;
10087 struct dwo_unit *dwo_unit;
73869dc2 10088 struct virtual_v1_dwo_sections sections;
80626a55
DE
10089 void **dwo_file_slot;
10090 char *virtual_dwo_name;
80626a55
DE
10091 struct cleanup *cleanups;
10092 int i;
10093
73869dc2
DE
10094 gdb_assert (dwp_file->version == 1);
10095
b4f54984 10096 if (dwarf_read_debug)
80626a55 10097 {
73869dc2 10098 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10099 kind,
73869dc2 10100 pulongest (unit_index), hex_string (signature),
80626a55
DE
10101 dwp_file->name);
10102 }
10103
19ac8c2e 10104 /* Fetch the sections of this DWO unit.
80626a55
DE
10105 Put a limit on the number of sections we look for so that bad data
10106 doesn't cause us to loop forever. */
10107
73869dc2 10108#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10109 (1 /* .debug_info or .debug_types */ \
10110 + 1 /* .debug_abbrev */ \
10111 + 1 /* .debug_line */ \
10112 + 1 /* .debug_loc */ \
10113 + 1 /* .debug_str_offsets */ \
19ac8c2e 10114 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10115 + 1 /* trailing zero */)
10116
10117 memset (&sections, 0, sizeof (sections));
10118 cleanups = make_cleanup (null_cleanup, 0);
10119
73869dc2 10120 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10121 {
10122 asection *sectp;
10123 uint32_t section_nr =
10124 read_4_bytes (dbfd,
73869dc2
DE
10125 dwp_htab->section_pool.v1.indices
10126 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10127
10128 if (section_nr == 0)
10129 break;
10130 if (section_nr >= dwp_file->num_sections)
10131 {
10132 error (_("Dwarf Error: bad DWP hash table, section number too large"
10133 " [in module %s]"),
10134 dwp_file->name);
10135 }
10136
10137 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10138 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10139 {
10140 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10141 " [in module %s]"),
10142 dwp_file->name);
10143 }
10144 }
10145
10146 if (i < 2
a32a8923
DE
10147 || dwarf2_section_empty_p (&sections.info_or_types)
10148 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10149 {
10150 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10151 " [in module %s]"),
10152 dwp_file->name);
10153 }
73869dc2 10154 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10155 {
10156 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10157 " [in module %s]"),
10158 dwp_file->name);
10159 }
10160
10161 /* It's easier for the rest of the code if we fake a struct dwo_file and
10162 have dwo_unit "live" in that. At least for now.
10163
10164 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10165 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10166 file, we can combine them back into a virtual DWO file to save space
10167 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10168 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10169
2792b94d
PM
10170 virtual_dwo_name =
10171 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10172 get_section_id (&sections.abbrev),
10173 get_section_id (&sections.line),
10174 get_section_id (&sections.loc),
10175 get_section_id (&sections.str_offsets));
80626a55
DE
10176 make_cleanup (xfree, virtual_dwo_name);
10177 /* Can we use an existing virtual DWO file? */
0ac5b59e 10178 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10179 /* Create one if necessary. */
10180 if (*dwo_file_slot == NULL)
10181 {
b4f54984 10182 if (dwarf_read_debug)
80626a55
DE
10183 {
10184 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10185 virtual_dwo_name);
10186 }
10187 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10188 dwo_file->dwo_name
10189 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10190 virtual_dwo_name,
10191 strlen (virtual_dwo_name));
0ac5b59e 10192 dwo_file->comp_dir = comp_dir;
80626a55
DE
10193 dwo_file->sections.abbrev = sections.abbrev;
10194 dwo_file->sections.line = sections.line;
10195 dwo_file->sections.loc = sections.loc;
10196 dwo_file->sections.macinfo = sections.macinfo;
10197 dwo_file->sections.macro = sections.macro;
10198 dwo_file->sections.str_offsets = sections.str_offsets;
10199 /* The "str" section is global to the entire DWP file. */
10200 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10201 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10202 there's no need to record it in dwo_file.
10203 Also, we can't simply record type sections in dwo_file because
10204 we record a pointer into the vector in dwo_unit. As we collect more
10205 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10206 for it, invalidating all copies of pointers into the previous
10207 contents. */
80626a55
DE
10208 *dwo_file_slot = dwo_file;
10209 }
10210 else
10211 {
b4f54984 10212 if (dwarf_read_debug)
80626a55
DE
10213 {
10214 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10215 virtual_dwo_name);
10216 }
9a3c8263 10217 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10218 }
10219 do_cleanups (cleanups);
10220
10221 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10222 dwo_unit->dwo_file = dwo_file;
10223 dwo_unit->signature = signature;
8d749320
SM
10224 dwo_unit->section =
10225 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10226 *dwo_unit->section = sections.info_or_types;
57d63ce2 10227 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10228
10229 return dwo_unit;
10230}
10231
73869dc2
DE
10232/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10233 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10234 piece within that section used by a TU/CU, return a virtual section
10235 of just that piece. */
10236
10237static struct dwarf2_section_info
10238create_dwp_v2_section (struct dwarf2_section_info *section,
10239 bfd_size_type offset, bfd_size_type size)
10240{
10241 struct dwarf2_section_info result;
10242 asection *sectp;
10243
10244 gdb_assert (section != NULL);
10245 gdb_assert (!section->is_virtual);
10246
10247 memset (&result, 0, sizeof (result));
10248 result.s.containing_section = section;
10249 result.is_virtual = 1;
10250
10251 if (size == 0)
10252 return result;
10253
10254 sectp = get_section_bfd_section (section);
10255
10256 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10257 bounds of the real section. This is a pretty-rare event, so just
10258 flag an error (easier) instead of a warning and trying to cope. */
10259 if (sectp == NULL
10260 || offset + size > bfd_get_section_size (sectp))
10261 {
10262 bfd *abfd = sectp->owner;
10263
10264 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10265 " in section %s [in module %s]"),
10266 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10267 objfile_name (dwarf2_per_objfile->objfile));
10268 }
10269
10270 result.virtual_offset = offset;
10271 result.size = size;
10272 return result;
10273}
10274
10275/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10276 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10277 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10278 This is for DWP version 2 files. */
10279
10280static struct dwo_unit *
10281create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10282 uint32_t unit_index,
10283 const char *comp_dir,
10284 ULONGEST signature, int is_debug_types)
10285{
10286 struct objfile *objfile = dwarf2_per_objfile->objfile;
10287 const struct dwp_hash_table *dwp_htab =
10288 is_debug_types ? dwp_file->tus : dwp_file->cus;
10289 bfd *dbfd = dwp_file->dbfd;
10290 const char *kind = is_debug_types ? "TU" : "CU";
10291 struct dwo_file *dwo_file;
10292 struct dwo_unit *dwo_unit;
10293 struct virtual_v2_dwo_sections sections;
10294 void **dwo_file_slot;
10295 char *virtual_dwo_name;
73869dc2
DE
10296 struct cleanup *cleanups;
10297 int i;
10298
10299 gdb_assert (dwp_file->version == 2);
10300
b4f54984 10301 if (dwarf_read_debug)
73869dc2
DE
10302 {
10303 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10304 kind,
10305 pulongest (unit_index), hex_string (signature),
10306 dwp_file->name);
10307 }
10308
10309 /* Fetch the section offsets of this DWO unit. */
10310
10311 memset (&sections, 0, sizeof (sections));
10312 cleanups = make_cleanup (null_cleanup, 0);
10313
10314 for (i = 0; i < dwp_htab->nr_columns; ++i)
10315 {
10316 uint32_t offset = read_4_bytes (dbfd,
10317 dwp_htab->section_pool.v2.offsets
10318 + (((unit_index - 1) * dwp_htab->nr_columns
10319 + i)
10320 * sizeof (uint32_t)));
10321 uint32_t size = read_4_bytes (dbfd,
10322 dwp_htab->section_pool.v2.sizes
10323 + (((unit_index - 1) * dwp_htab->nr_columns
10324 + i)
10325 * sizeof (uint32_t)));
10326
10327 switch (dwp_htab->section_pool.v2.section_ids[i])
10328 {
10329 case DW_SECT_INFO:
10330 case DW_SECT_TYPES:
10331 sections.info_or_types_offset = offset;
10332 sections.info_or_types_size = size;
10333 break;
10334 case DW_SECT_ABBREV:
10335 sections.abbrev_offset = offset;
10336 sections.abbrev_size = size;
10337 break;
10338 case DW_SECT_LINE:
10339 sections.line_offset = offset;
10340 sections.line_size = size;
10341 break;
10342 case DW_SECT_LOC:
10343 sections.loc_offset = offset;
10344 sections.loc_size = size;
10345 break;
10346 case DW_SECT_STR_OFFSETS:
10347 sections.str_offsets_offset = offset;
10348 sections.str_offsets_size = size;
10349 break;
10350 case DW_SECT_MACINFO:
10351 sections.macinfo_offset = offset;
10352 sections.macinfo_size = size;
10353 break;
10354 case DW_SECT_MACRO:
10355 sections.macro_offset = offset;
10356 sections.macro_size = size;
10357 break;
10358 }
10359 }
10360
10361 /* It's easier for the rest of the code if we fake a struct dwo_file and
10362 have dwo_unit "live" in that. At least for now.
10363
10364 The DWP file can be made up of a random collection of CUs and TUs.
10365 However, for each CU + set of TUs that came from the same original DWO
10366 file, we can combine them back into a virtual DWO file to save space
10367 (fewer struct dwo_file objects to allocate). Remember that for really
10368 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10369
10370 virtual_dwo_name =
10371 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10372 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10373 (long) (sections.line_size ? sections.line_offset : 0),
10374 (long) (sections.loc_size ? sections.loc_offset : 0),
10375 (long) (sections.str_offsets_size
10376 ? sections.str_offsets_offset : 0));
10377 make_cleanup (xfree, virtual_dwo_name);
10378 /* Can we use an existing virtual DWO file? */
10379 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10380 /* Create one if necessary. */
10381 if (*dwo_file_slot == NULL)
10382 {
b4f54984 10383 if (dwarf_read_debug)
73869dc2
DE
10384 {
10385 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10386 virtual_dwo_name);
10387 }
10388 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10389 dwo_file->dwo_name
10390 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10391 virtual_dwo_name,
10392 strlen (virtual_dwo_name));
73869dc2
DE
10393 dwo_file->comp_dir = comp_dir;
10394 dwo_file->sections.abbrev =
10395 create_dwp_v2_section (&dwp_file->sections.abbrev,
10396 sections.abbrev_offset, sections.abbrev_size);
10397 dwo_file->sections.line =
10398 create_dwp_v2_section (&dwp_file->sections.line,
10399 sections.line_offset, sections.line_size);
10400 dwo_file->sections.loc =
10401 create_dwp_v2_section (&dwp_file->sections.loc,
10402 sections.loc_offset, sections.loc_size);
10403 dwo_file->sections.macinfo =
10404 create_dwp_v2_section (&dwp_file->sections.macinfo,
10405 sections.macinfo_offset, sections.macinfo_size);
10406 dwo_file->sections.macro =
10407 create_dwp_v2_section (&dwp_file->sections.macro,
10408 sections.macro_offset, sections.macro_size);
10409 dwo_file->sections.str_offsets =
10410 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10411 sections.str_offsets_offset,
10412 sections.str_offsets_size);
10413 /* The "str" section is global to the entire DWP file. */
10414 dwo_file->sections.str = dwp_file->sections.str;
10415 /* The info or types section is assigned below to dwo_unit,
10416 there's no need to record it in dwo_file.
10417 Also, we can't simply record type sections in dwo_file because
10418 we record a pointer into the vector in dwo_unit. As we collect more
10419 types we'll grow the vector and eventually have to reallocate space
10420 for it, invalidating all copies of pointers into the previous
10421 contents. */
10422 *dwo_file_slot = dwo_file;
10423 }
10424 else
10425 {
b4f54984 10426 if (dwarf_read_debug)
73869dc2
DE
10427 {
10428 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10429 virtual_dwo_name);
10430 }
9a3c8263 10431 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10432 }
10433 do_cleanups (cleanups);
10434
10435 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10436 dwo_unit->dwo_file = dwo_file;
10437 dwo_unit->signature = signature;
8d749320
SM
10438 dwo_unit->section =
10439 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10440 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10441 ? &dwp_file->sections.types
10442 : &dwp_file->sections.info,
10443 sections.info_or_types_offset,
10444 sections.info_or_types_size);
10445 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10446
10447 return dwo_unit;
10448}
10449
57d63ce2
DE
10450/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10451 Returns NULL if the signature isn't found. */
80626a55
DE
10452
10453static struct dwo_unit *
57d63ce2
DE
10454lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10455 ULONGEST signature, int is_debug_types)
80626a55 10456{
57d63ce2
DE
10457 const struct dwp_hash_table *dwp_htab =
10458 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10459 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10460 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10461 uint32_t hash = signature & mask;
10462 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10463 unsigned int i;
10464 void **slot;
870f88f7 10465 struct dwo_unit find_dwo_cu;
80626a55
DE
10466
10467 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10468 find_dwo_cu.signature = signature;
19ac8c2e
DE
10469 slot = htab_find_slot (is_debug_types
10470 ? dwp_file->loaded_tus
10471 : dwp_file->loaded_cus,
10472 &find_dwo_cu, INSERT);
80626a55
DE
10473
10474 if (*slot != NULL)
9a3c8263 10475 return (struct dwo_unit *) *slot;
80626a55
DE
10476
10477 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10478 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10479 {
10480 ULONGEST signature_in_table;
10481
10482 signature_in_table =
57d63ce2 10483 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10484 if (signature_in_table == signature)
10485 {
57d63ce2
DE
10486 uint32_t unit_index =
10487 read_4_bytes (dbfd,
10488 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10489
73869dc2
DE
10490 if (dwp_file->version == 1)
10491 {
10492 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10493 comp_dir, signature,
10494 is_debug_types);
10495 }
10496 else
10497 {
10498 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10499 comp_dir, signature,
10500 is_debug_types);
10501 }
9a3c8263 10502 return (struct dwo_unit *) *slot;
80626a55
DE
10503 }
10504 if (signature_in_table == 0)
10505 return NULL;
10506 hash = (hash + hash2) & mask;
10507 }
10508
10509 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10510 " [in module %s]"),
10511 dwp_file->name);
10512}
10513
ab5088bf 10514/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10515 Open the file specified by FILE_NAME and hand it off to BFD for
10516 preliminary analysis. Return a newly initialized bfd *, which
10517 includes a canonicalized copy of FILE_NAME.
80626a55 10518 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10519 SEARCH_CWD is true if the current directory is to be searched.
10520 It will be searched before debug-file-directory.
13aaf454
DE
10521 If successful, the file is added to the bfd include table of the
10522 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10523 If unable to find/open the file, return NULL.
3019eac3
DE
10524 NOTE: This function is derived from symfile_bfd_open. */
10525
192b62ce 10526static gdb_bfd_ref_ptr
6ac97d4c 10527try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10528{
80626a55 10529 int desc, flags;
3019eac3 10530 char *absolute_name;
9c02c129
DE
10531 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10532 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10533 to debug_file_directory. */
10534 char *search_path;
10535 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10536
6ac97d4c
DE
10537 if (search_cwd)
10538 {
10539 if (*debug_file_directory != '\0')
10540 search_path = concat (".", dirname_separator_string,
b36cec19 10541 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10542 else
10543 search_path = xstrdup (".");
10544 }
9c02c129 10545 else
6ac97d4c 10546 search_path = xstrdup (debug_file_directory);
3019eac3 10547
492c0ab7 10548 flags = OPF_RETURN_REALPATH;
80626a55
DE
10549 if (is_dwp)
10550 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10551 desc = openp (search_path, flags, file_name,
3019eac3 10552 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10553 xfree (search_path);
3019eac3
DE
10554 if (desc < 0)
10555 return NULL;
10556
192b62ce 10557 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10558 xfree (absolute_name);
9c02c129
DE
10559 if (sym_bfd == NULL)
10560 return NULL;
192b62ce 10561 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10562
192b62ce
TT
10563 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10564 return NULL;
3019eac3 10565
13aaf454
DE
10566 /* Success. Record the bfd as having been included by the objfile's bfd.
10567 This is important because things like demangled_names_hash lives in the
10568 objfile's per_bfd space and may have references to things like symbol
10569 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10570 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10571
3019eac3
DE
10572 return sym_bfd;
10573}
10574
ab5088bf 10575/* Try to open DWO file FILE_NAME.
3019eac3
DE
10576 COMP_DIR is the DW_AT_comp_dir attribute.
10577 The result is the bfd handle of the file.
10578 If there is a problem finding or opening the file, return NULL.
10579 Upon success, the canonicalized path of the file is stored in the bfd,
10580 same as symfile_bfd_open. */
10581
192b62ce 10582static gdb_bfd_ref_ptr
ab5088bf 10583open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10584{
80626a55 10585 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10586 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10587
10588 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10589
10590 if (comp_dir != NULL)
10591 {
b36cec19
PA
10592 char *path_to_try = concat (comp_dir, SLASH_STRING,
10593 file_name, (char *) NULL);
3019eac3
DE
10594
10595 /* NOTE: If comp_dir is a relative path, this will also try the
10596 search path, which seems useful. */
192b62ce
TT
10597 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10598 1 /*search_cwd*/));
3019eac3
DE
10599 xfree (path_to_try);
10600 if (abfd != NULL)
10601 return abfd;
10602 }
10603
10604 /* That didn't work, try debug-file-directory, which, despite its name,
10605 is a list of paths. */
10606
10607 if (*debug_file_directory == '\0')
10608 return NULL;
10609
6ac97d4c 10610 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10611}
10612
80626a55
DE
10613/* This function is mapped across the sections and remembers the offset and
10614 size of each of the DWO debugging sections we are interested in. */
10615
10616static void
10617dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10618{
9a3c8263 10619 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10620 const struct dwop_section_names *names = &dwop_section_names;
10621
10622 if (section_is_p (sectp->name, &names->abbrev_dwo))
10623 {
049412e3 10624 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10625 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10626 }
10627 else if (section_is_p (sectp->name, &names->info_dwo))
10628 {
049412e3 10629 dwo_sections->info.s.section = sectp;
80626a55
DE
10630 dwo_sections->info.size = bfd_get_section_size (sectp);
10631 }
10632 else if (section_is_p (sectp->name, &names->line_dwo))
10633 {
049412e3 10634 dwo_sections->line.s.section = sectp;
80626a55
DE
10635 dwo_sections->line.size = bfd_get_section_size (sectp);
10636 }
10637 else if (section_is_p (sectp->name, &names->loc_dwo))
10638 {
049412e3 10639 dwo_sections->loc.s.section = sectp;
80626a55
DE
10640 dwo_sections->loc.size = bfd_get_section_size (sectp);
10641 }
10642 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10643 {
049412e3 10644 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10645 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10646 }
10647 else if (section_is_p (sectp->name, &names->macro_dwo))
10648 {
049412e3 10649 dwo_sections->macro.s.section = sectp;
80626a55
DE
10650 dwo_sections->macro.size = bfd_get_section_size (sectp);
10651 }
10652 else if (section_is_p (sectp->name, &names->str_dwo))
10653 {
049412e3 10654 dwo_sections->str.s.section = sectp;
80626a55
DE
10655 dwo_sections->str.size = bfd_get_section_size (sectp);
10656 }
10657 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10658 {
049412e3 10659 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10660 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10661 }
10662 else if (section_is_p (sectp->name, &names->types_dwo))
10663 {
10664 struct dwarf2_section_info type_section;
10665
10666 memset (&type_section, 0, sizeof (type_section));
049412e3 10667 type_section.s.section = sectp;
80626a55
DE
10668 type_section.size = bfd_get_section_size (sectp);
10669 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10670 &type_section);
10671 }
10672}
10673
ab5088bf 10674/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10675 by PER_CU. This is for the non-DWP case.
80626a55 10676 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10677
10678static struct dwo_file *
0ac5b59e
DE
10679open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10680 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10681{
10682 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10683 struct dwo_file *dwo_file;
3019eac3
DE
10684 struct cleanup *cleanups;
10685
192b62ce 10686 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10687 if (dbfd == NULL)
10688 {
b4f54984 10689 if (dwarf_read_debug)
80626a55
DE
10690 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10691 return NULL;
10692 }
10693 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10694 dwo_file->dwo_name = dwo_name;
10695 dwo_file->comp_dir = comp_dir;
192b62ce 10696 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10697
10698 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10699
192b62ce
TT
10700 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10701 &dwo_file->sections);
3019eac3 10702
19c3d4c9 10703 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3 10704
78d4d2c5
JK
10705 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10706 dwo_file->tus);
3019eac3
DE
10707
10708 discard_cleanups (cleanups);
10709
b4f54984 10710 if (dwarf_read_debug)
80626a55
DE
10711 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10712
3019eac3
DE
10713 return dwo_file;
10714}
10715
80626a55 10716/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10717 size of each of the DWP debugging sections common to version 1 and 2 that
10718 we are interested in. */
3019eac3 10719
80626a55 10720static void
73869dc2
DE
10721dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10722 void *dwp_file_ptr)
3019eac3 10723{
9a3c8263 10724 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10725 const struct dwop_section_names *names = &dwop_section_names;
10726 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10727
80626a55 10728 /* Record the ELF section number for later lookup: this is what the
73869dc2 10729 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10730 gdb_assert (elf_section_nr < dwp_file->num_sections);
10731 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10732
80626a55
DE
10733 /* Look for specific sections that we need. */
10734 if (section_is_p (sectp->name, &names->str_dwo))
10735 {
049412e3 10736 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10737 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10738 }
10739 else if (section_is_p (sectp->name, &names->cu_index))
10740 {
049412e3 10741 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10742 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10743 }
10744 else if (section_is_p (sectp->name, &names->tu_index))
10745 {
049412e3 10746 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10747 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10748 }
10749}
3019eac3 10750
73869dc2
DE
10751/* This function is mapped across the sections and remembers the offset and
10752 size of each of the DWP version 2 debugging sections that we are interested
10753 in. This is split into a separate function because we don't know if we
10754 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10755
10756static void
10757dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10758{
9a3c8263 10759 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10760 const struct dwop_section_names *names = &dwop_section_names;
10761 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10762
10763 /* Record the ELF section number for later lookup: this is what the
10764 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10765 gdb_assert (elf_section_nr < dwp_file->num_sections);
10766 dwp_file->elf_sections[elf_section_nr] = sectp;
10767
10768 /* Look for specific sections that we need. */
10769 if (section_is_p (sectp->name, &names->abbrev_dwo))
10770 {
049412e3 10771 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10772 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10773 }
10774 else if (section_is_p (sectp->name, &names->info_dwo))
10775 {
049412e3 10776 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10777 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10778 }
10779 else if (section_is_p (sectp->name, &names->line_dwo))
10780 {
049412e3 10781 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10782 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10783 }
10784 else if (section_is_p (sectp->name, &names->loc_dwo))
10785 {
049412e3 10786 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10787 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10788 }
10789 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10790 {
049412e3 10791 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10792 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10793 }
10794 else if (section_is_p (sectp->name, &names->macro_dwo))
10795 {
049412e3 10796 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
10797 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10798 }
10799 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10800 {
049412e3 10801 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
10802 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10803 }
10804 else if (section_is_p (sectp->name, &names->types_dwo))
10805 {
049412e3 10806 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10807 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10808 }
10809}
10810
80626a55 10811/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10812
80626a55
DE
10813static hashval_t
10814hash_dwp_loaded_cutus (const void *item)
10815{
9a3c8263 10816 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10817
80626a55
DE
10818 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10819 return dwo_unit->signature;
3019eac3
DE
10820}
10821
80626a55 10822/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10823
80626a55
DE
10824static int
10825eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10826{
9a3c8263
SM
10827 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10828 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10829
80626a55
DE
10830 return dua->signature == dub->signature;
10831}
3019eac3 10832
80626a55 10833/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10834
80626a55
DE
10835static htab_t
10836allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10837{
10838 return htab_create_alloc_ex (3,
10839 hash_dwp_loaded_cutus,
10840 eq_dwp_loaded_cutus,
10841 NULL,
10842 &objfile->objfile_obstack,
10843 hashtab_obstack_allocate,
10844 dummy_obstack_deallocate);
10845}
3019eac3 10846
ab5088bf
DE
10847/* Try to open DWP file FILE_NAME.
10848 The result is the bfd handle of the file.
10849 If there is a problem finding or opening the file, return NULL.
10850 Upon success, the canonicalized path of the file is stored in the bfd,
10851 same as symfile_bfd_open. */
10852
192b62ce 10853static gdb_bfd_ref_ptr
ab5088bf
DE
10854open_dwp_file (const char *file_name)
10855{
192b62ce
TT
10856 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10857 1 /*search_cwd*/));
6ac97d4c
DE
10858 if (abfd != NULL)
10859 return abfd;
10860
10861 /* Work around upstream bug 15652.
10862 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10863 [Whether that's a "bug" is debatable, but it is getting in our way.]
10864 We have no real idea where the dwp file is, because gdb's realpath-ing
10865 of the executable's path may have discarded the needed info.
10866 [IWBN if the dwp file name was recorded in the executable, akin to
10867 .gnu_debuglink, but that doesn't exist yet.]
10868 Strip the directory from FILE_NAME and search again. */
10869 if (*debug_file_directory != '\0')
10870 {
10871 /* Don't implicitly search the current directory here.
10872 If the user wants to search "." to handle this case,
10873 it must be added to debug-file-directory. */
10874 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10875 0 /*search_cwd*/);
10876 }
10877
10878 return NULL;
ab5088bf
DE
10879}
10880
80626a55
DE
10881/* Initialize the use of the DWP file for the current objfile.
10882 By convention the name of the DWP file is ${objfile}.dwp.
10883 The result is NULL if it can't be found. */
a766d390 10884
80626a55 10885static struct dwp_file *
ab5088bf 10886open_and_init_dwp_file (void)
80626a55
DE
10887{
10888 struct objfile *objfile = dwarf2_per_objfile->objfile;
10889 struct dwp_file *dwp_file;
10890 char *dwp_name;
6c447423 10891 struct cleanup *cleanups = make_cleanup (null_cleanup, 0);
80626a55 10892
82bf32bc
JK
10893 /* Try to find first .dwp for the binary file before any symbolic links
10894 resolving. */
6c447423
DE
10895
10896 /* If the objfile is a debug file, find the name of the real binary
10897 file and get the name of dwp file from there. */
10898 if (objfile->separate_debug_objfile_backlink != NULL)
10899 {
10900 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10901 const char *backlink_basename = lbasename (backlink->original_name);
10902 char *debug_dirname = ldirname (objfile->original_name);
10903
10904 make_cleanup (xfree, debug_dirname);
10905 dwp_name = xstrprintf ("%s%s%s.dwp", debug_dirname,
10906 SLASH_STRING, backlink_basename);
10907 }
10908 else
10909 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10910 make_cleanup (xfree, dwp_name);
80626a55 10911
192b62ce 10912 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name));
82bf32bc
JK
10913 if (dbfd == NULL
10914 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10915 {
10916 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10917 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10918 make_cleanup (xfree, dwp_name);
10919 dbfd = open_dwp_file (dwp_name);
10920 }
10921
80626a55
DE
10922 if (dbfd == NULL)
10923 {
b4f54984 10924 if (dwarf_read_debug)
80626a55
DE
10925 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10926 do_cleanups (cleanups);
10927 return NULL;
3019eac3 10928 }
80626a55 10929 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
10930 dwp_file->name = bfd_get_filename (dbfd.get ());
10931 dwp_file->dbfd = dbfd.release ();
80626a55 10932 do_cleanups (cleanups);
c906108c 10933
80626a55 10934 /* +1: section 0 is unused */
192b62ce 10935 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
10936 dwp_file->elf_sections =
10937 OBSTACK_CALLOC (&objfile->objfile_obstack,
10938 dwp_file->num_sections, asection *);
10939
192b62ce
TT
10940 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10941 dwp_file);
80626a55
DE
10942
10943 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10944
10945 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10946
73869dc2
DE
10947 /* The DWP file version is stored in the hash table. Oh well. */
10948 if (dwp_file->cus->version != dwp_file->tus->version)
10949 {
10950 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 10951 pretty bizarre. We use pulongest here because that's the established
4d65956b 10952 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
10953 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10954 " TU version %s [in DWP file %s]"),
10955 pulongest (dwp_file->cus->version),
10956 pulongest (dwp_file->tus->version), dwp_name);
73869dc2
DE
10957 }
10958 dwp_file->version = dwp_file->cus->version;
10959
10960 if (dwp_file->version == 2)
192b62ce
TT
10961 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10962 dwp_file);
73869dc2 10963
19ac8c2e
DE
10964 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10965 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 10966
b4f54984 10967 if (dwarf_read_debug)
80626a55
DE
10968 {
10969 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10970 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
10971 " %s CUs, %s TUs\n",
10972 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10973 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
10974 }
10975
10976 return dwp_file;
3019eac3 10977}
c906108c 10978
ab5088bf
DE
10979/* Wrapper around open_and_init_dwp_file, only open it once. */
10980
10981static struct dwp_file *
10982get_dwp_file (void)
10983{
10984 if (! dwarf2_per_objfile->dwp_checked)
10985 {
10986 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10987 dwarf2_per_objfile->dwp_checked = 1;
10988 }
10989 return dwarf2_per_objfile->dwp_file;
10990}
10991
80626a55
DE
10992/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10993 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10994 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 10995 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
10996 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10997
10998 This is called, for example, when wanting to read a variable with a
10999 complex location. Therefore we don't want to do file i/o for every call.
11000 Therefore we don't want to look for a DWO file on every call.
11001 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11002 then we check if we've already seen DWO_NAME, and only THEN do we check
11003 for a DWO file.
11004
1c658ad5 11005 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11006 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11007
3019eac3 11008static struct dwo_unit *
80626a55
DE
11009lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11010 const char *dwo_name, const char *comp_dir,
11011 ULONGEST signature, int is_debug_types)
3019eac3
DE
11012{
11013 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11014 const char *kind = is_debug_types ? "TU" : "CU";
11015 void **dwo_file_slot;
3019eac3 11016 struct dwo_file *dwo_file;
80626a55 11017 struct dwp_file *dwp_file;
cb1df416 11018
6a506a2d
DE
11019 /* First see if there's a DWP file.
11020 If we have a DWP file but didn't find the DWO inside it, don't
11021 look for the original DWO file. It makes gdb behave differently
11022 depending on whether one is debugging in the build tree. */
cf2c3c16 11023
ab5088bf 11024 dwp_file = get_dwp_file ();
80626a55 11025 if (dwp_file != NULL)
cf2c3c16 11026 {
80626a55
DE
11027 const struct dwp_hash_table *dwp_htab =
11028 is_debug_types ? dwp_file->tus : dwp_file->cus;
11029
11030 if (dwp_htab != NULL)
11031 {
11032 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11033 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11034 signature, is_debug_types);
80626a55
DE
11035
11036 if (dwo_cutu != NULL)
11037 {
b4f54984 11038 if (dwarf_read_debug)
80626a55
DE
11039 {
11040 fprintf_unfiltered (gdb_stdlog,
11041 "Virtual DWO %s %s found: @%s\n",
11042 kind, hex_string (signature),
11043 host_address_to_string (dwo_cutu));
11044 }
11045 return dwo_cutu;
11046 }
11047 }
11048 }
6a506a2d 11049 else
80626a55 11050 {
6a506a2d 11051 /* No DWP file, look for the DWO file. */
80626a55 11052
6a506a2d
DE
11053 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11054 if (*dwo_file_slot == NULL)
80626a55 11055 {
6a506a2d
DE
11056 /* Read in the file and build a table of the CUs/TUs it contains. */
11057 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11058 }
6a506a2d 11059 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11060 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11061
6a506a2d 11062 if (dwo_file != NULL)
19c3d4c9 11063 {
6a506a2d
DE
11064 struct dwo_unit *dwo_cutu = NULL;
11065
11066 if (is_debug_types && dwo_file->tus)
11067 {
11068 struct dwo_unit find_dwo_cutu;
11069
11070 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11071 find_dwo_cutu.signature = signature;
9a3c8263
SM
11072 dwo_cutu
11073 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
11074 }
11075 else if (!is_debug_types && dwo_file->cu)
80626a55 11076 {
6a506a2d
DE
11077 if (signature == dwo_file->cu->signature)
11078 dwo_cutu = dwo_file->cu;
11079 }
11080
11081 if (dwo_cutu != NULL)
11082 {
b4f54984 11083 if (dwarf_read_debug)
6a506a2d
DE
11084 {
11085 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11086 kind, dwo_name, hex_string (signature),
11087 host_address_to_string (dwo_cutu));
11088 }
11089 return dwo_cutu;
80626a55
DE
11090 }
11091 }
2e276125 11092 }
9cdd5dbd 11093
80626a55
DE
11094 /* We didn't find it. This could mean a dwo_id mismatch, or
11095 someone deleted the DWO/DWP file, or the search path isn't set up
11096 correctly to find the file. */
11097
b4f54984 11098 if (dwarf_read_debug)
80626a55
DE
11099 {
11100 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11101 kind, dwo_name, hex_string (signature));
11102 }
3019eac3 11103
6656a72d
DE
11104 /* This is a warning and not a complaint because it can be caused by
11105 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11106 {
11107 /* Print the name of the DWP file if we looked there, helps the user
11108 better diagnose the problem. */
11109 char *dwp_text = NULL;
11110 struct cleanup *cleanups;
11111
11112 if (dwp_file != NULL)
11113 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11114 cleanups = make_cleanup (xfree, dwp_text);
11115
11116 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11117 " [in module %s]"),
11118 kind, dwo_name, hex_string (signature),
11119 dwp_text != NULL ? dwp_text : "",
11120 this_unit->is_debug_types ? "TU" : "CU",
11121 this_unit->offset.sect_off, objfile_name (objfile));
11122
11123 do_cleanups (cleanups);
11124 }
3019eac3 11125 return NULL;
5fb290d7
DJ
11126}
11127
80626a55
DE
11128/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11129 See lookup_dwo_cutu_unit for details. */
11130
11131static struct dwo_unit *
11132lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11133 const char *dwo_name, const char *comp_dir,
11134 ULONGEST signature)
11135{
11136 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11137}
11138
11139/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11140 See lookup_dwo_cutu_unit for details. */
11141
11142static struct dwo_unit *
11143lookup_dwo_type_unit (struct signatured_type *this_tu,
11144 const char *dwo_name, const char *comp_dir)
11145{
11146 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11147}
11148
89e63ee4
DE
11149/* Traversal function for queue_and_load_all_dwo_tus. */
11150
11151static int
11152queue_and_load_dwo_tu (void **slot, void *info)
11153{
11154 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11155 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11156 ULONGEST signature = dwo_unit->signature;
11157 struct signatured_type *sig_type =
11158 lookup_dwo_signatured_type (per_cu->cu, signature);
11159
11160 if (sig_type != NULL)
11161 {
11162 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11163
11164 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11165 a real dependency of PER_CU on SIG_TYPE. That is detected later
11166 while processing PER_CU. */
11167 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11168 load_full_type_unit (sig_cu);
11169 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11170 }
11171
11172 return 1;
11173}
11174
11175/* Queue all TUs contained in the DWO of PER_CU to be read in.
11176 The DWO may have the only definition of the type, though it may not be
11177 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11178 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11179
11180static void
11181queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11182{
11183 struct dwo_unit *dwo_unit;
11184 struct dwo_file *dwo_file;
11185
11186 gdb_assert (!per_cu->is_debug_types);
11187 gdb_assert (get_dwp_file () == NULL);
11188 gdb_assert (per_cu->cu != NULL);
11189
11190 dwo_unit = per_cu->cu->dwo_unit;
11191 gdb_assert (dwo_unit != NULL);
11192
11193 dwo_file = dwo_unit->dwo_file;
11194 if (dwo_file->tus != NULL)
11195 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11196}
11197
3019eac3
DE
11198/* Free all resources associated with DWO_FILE.
11199 Close the DWO file and munmap the sections.
11200 All memory should be on the objfile obstack. */
348e048f
DE
11201
11202static void
3019eac3 11203free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11204{
348e048f 11205
5c6fa7ab 11206 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11207 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11208
3019eac3
DE
11209 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11210}
348e048f 11211
3019eac3 11212/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11213
3019eac3
DE
11214static void
11215free_dwo_file_cleanup (void *arg)
11216{
11217 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11218 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11219
3019eac3
DE
11220 free_dwo_file (dwo_file, objfile);
11221}
348e048f 11222
3019eac3 11223/* Traversal function for free_dwo_files. */
2ab95328 11224
3019eac3
DE
11225static int
11226free_dwo_file_from_slot (void **slot, void *info)
11227{
11228 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11229 struct objfile *objfile = (struct objfile *) info;
348e048f 11230
3019eac3 11231 free_dwo_file (dwo_file, objfile);
348e048f 11232
3019eac3
DE
11233 return 1;
11234}
348e048f 11235
3019eac3 11236/* Free all resources associated with DWO_FILES. */
348e048f 11237
3019eac3
DE
11238static void
11239free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11240{
11241 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11242}
3019eac3
DE
11243\f
11244/* Read in various DIEs. */
348e048f 11245
d389af10
JK
11246/* qsort helper for inherit_abstract_dies. */
11247
11248static int
11249unsigned_int_compar (const void *ap, const void *bp)
11250{
11251 unsigned int a = *(unsigned int *) ap;
11252 unsigned int b = *(unsigned int *) bp;
11253
11254 return (a > b) - (b > a);
11255}
11256
11257/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11258 Inherit only the children of the DW_AT_abstract_origin DIE not being
11259 already referenced by DW_AT_abstract_origin from the children of the
11260 current DIE. */
d389af10
JK
11261
11262static void
11263inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11264{
11265 struct die_info *child_die;
11266 unsigned die_children_count;
11267 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11268 sect_offset *offsets;
11269 sect_offset *offsets_end, *offsetp;
d389af10
JK
11270 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11271 struct die_info *origin_die;
11272 /* Iterator of the ORIGIN_DIE children. */
11273 struct die_info *origin_child_die;
11274 struct cleanup *cleanups;
11275 struct attribute *attr;
cd02d79d
PA
11276 struct dwarf2_cu *origin_cu;
11277 struct pending **origin_previous_list_in_scope;
d389af10
JK
11278
11279 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11280 if (!attr)
11281 return;
11282
cd02d79d
PA
11283 /* Note that following die references may follow to a die in a
11284 different cu. */
11285
11286 origin_cu = cu;
11287 origin_die = follow_die_ref (die, attr, &origin_cu);
11288
11289 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11290 symbols in. */
11291 origin_previous_list_in_scope = origin_cu->list_in_scope;
11292 origin_cu->list_in_scope = cu->list_in_scope;
11293
edb3359d
DJ
11294 if (die->tag != origin_die->tag
11295 && !(die->tag == DW_TAG_inlined_subroutine
11296 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11297 complaint (&symfile_complaints,
11298 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 11299 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
11300
11301 child_die = die->child;
11302 die_children_count = 0;
11303 while (child_die && child_die->tag)
11304 {
11305 child_die = sibling_die (child_die);
11306 die_children_count++;
11307 }
8d749320 11308 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11309 cleanups = make_cleanup (xfree, offsets);
11310
11311 offsets_end = offsets;
3ea89b92
PMR
11312 for (child_die = die->child;
11313 child_die && child_die->tag;
11314 child_die = sibling_die (child_die))
11315 {
11316 struct die_info *child_origin_die;
11317 struct dwarf2_cu *child_origin_cu;
11318
11319 /* We are trying to process concrete instance entries:
216f72a1 11320 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11321 it's not relevant to our analysis here. i.e. detecting DIEs that are
11322 present in the abstract instance but not referenced in the concrete
11323 one. */
216f72a1
JK
11324 if (child_die->tag == DW_TAG_call_site
11325 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11326 continue;
11327
c38f313d
DJ
11328 /* For each CHILD_DIE, find the corresponding child of
11329 ORIGIN_DIE. If there is more than one layer of
11330 DW_AT_abstract_origin, follow them all; there shouldn't be,
11331 but GCC versions at least through 4.4 generate this (GCC PR
11332 40573). */
3ea89b92
PMR
11333 child_origin_die = child_die;
11334 child_origin_cu = cu;
c38f313d
DJ
11335 while (1)
11336 {
cd02d79d
PA
11337 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11338 child_origin_cu);
c38f313d
DJ
11339 if (attr == NULL)
11340 break;
cd02d79d
PA
11341 child_origin_die = follow_die_ref (child_origin_die, attr,
11342 &child_origin_cu);
c38f313d
DJ
11343 }
11344
d389af10
JK
11345 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11346 counterpart may exist. */
c38f313d 11347 if (child_origin_die != child_die)
d389af10 11348 {
edb3359d
DJ
11349 if (child_die->tag != child_origin_die->tag
11350 && !(child_die->tag == DW_TAG_inlined_subroutine
11351 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11352 complaint (&symfile_complaints,
11353 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11354 "different tags"), child_die->offset.sect_off,
11355 child_origin_die->offset.sect_off);
c38f313d
DJ
11356 if (child_origin_die->parent != origin_die)
11357 complaint (&symfile_complaints,
11358 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11359 "different parents"), child_die->offset.sect_off,
11360 child_origin_die->offset.sect_off);
c38f313d
DJ
11361 else
11362 *offsets_end++ = child_origin_die->offset;
d389af10 11363 }
d389af10
JK
11364 }
11365 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11366 unsigned_int_compar);
11367 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 11368 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
11369 complaint (&symfile_complaints,
11370 _("Multiple children of DIE 0x%x refer "
11371 "to DIE 0x%x as their abstract origin"),
b64f50a1 11372 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
11373
11374 offsetp = offsets;
11375 origin_child_die = origin_die->child;
11376 while (origin_child_die && origin_child_die->tag)
11377 {
11378 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
11379 while (offsetp < offsets_end
11380 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 11381 offsetp++;
b64f50a1
JK
11382 if (offsetp >= offsets_end
11383 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10 11384 {
adde2bff
DE
11385 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11386 Check whether we're already processing ORIGIN_CHILD_DIE.
11387 This can happen with mutually referenced abstract_origins.
11388 PR 16581. */
11389 if (!origin_child_die->in_process)
11390 process_die (origin_child_die, origin_cu);
d389af10
JK
11391 }
11392 origin_child_die = sibling_die (origin_child_die);
11393 }
cd02d79d 11394 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11395
11396 do_cleanups (cleanups);
11397}
11398
c906108c 11399static void
e7c27a73 11400read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11401{
e7c27a73 11402 struct objfile *objfile = cu->objfile;
3e29f34a 11403 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11404 struct context_stack *newobj;
c906108c
SS
11405 CORE_ADDR lowpc;
11406 CORE_ADDR highpc;
11407 struct die_info *child_die;
edb3359d 11408 struct attribute *attr, *call_line, *call_file;
15d034d0 11409 const char *name;
e142c38c 11410 CORE_ADDR baseaddr;
801e3a5b 11411 struct block *block;
edb3359d 11412 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11413 VEC (symbolp) *template_args = NULL;
11414 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11415
11416 if (inlined_func)
11417 {
11418 /* If we do not have call site information, we can't show the
11419 caller of this inlined function. That's too confusing, so
11420 only use the scope for local variables. */
11421 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11422 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11423 if (call_line == NULL || call_file == NULL)
11424 {
11425 read_lexical_block_scope (die, cu);
11426 return;
11427 }
11428 }
c906108c 11429
e142c38c
DJ
11430 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11431
94af9270 11432 name = dwarf2_name (die, cu);
c906108c 11433
e8d05480
JB
11434 /* Ignore functions with missing or empty names. These are actually
11435 illegal according to the DWARF standard. */
11436 if (name == NULL)
11437 {
11438 complaint (&symfile_complaints,
b64f50a1
JK
11439 _("missing name for subprogram DIE at %d"),
11440 die->offset.sect_off);
e8d05480
JB
11441 return;
11442 }
11443
11444 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11445 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11446 <= PC_BOUNDS_INVALID)
e8d05480 11447 {
ae4d0c03
PM
11448 attr = dwarf2_attr (die, DW_AT_external, cu);
11449 if (!attr || !DW_UNSND (attr))
11450 complaint (&symfile_complaints,
3e43a32a
MS
11451 _("cannot get low and high bounds "
11452 "for subprogram DIE at %d"),
b64f50a1 11453 die->offset.sect_off);
e8d05480
JB
11454 return;
11455 }
c906108c 11456
3e29f34a
MR
11457 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11458 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11459
34eaf542
TT
11460 /* If we have any template arguments, then we must allocate a
11461 different sort of symbol. */
11462 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11463 {
11464 if (child_die->tag == DW_TAG_template_type_param
11465 || child_die->tag == DW_TAG_template_value_param)
11466 {
e623cf5d 11467 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11468 templ_func->base.is_cplus_template_function = 1;
11469 break;
11470 }
11471 }
11472
fe978cb0
PA
11473 newobj = push_context (0, lowpc);
11474 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11475 (struct symbol *) templ_func);
4c2df51b 11476
4cecd739
DJ
11477 /* If there is a location expression for DW_AT_frame_base, record
11478 it. */
e142c38c 11479 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11480 if (attr)
fe978cb0 11481 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11482
63e43d3a
PMR
11483 /* If there is a location for the static link, record it. */
11484 newobj->static_link = NULL;
11485 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11486 if (attr)
11487 {
224c3ddb
SM
11488 newobj->static_link
11489 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11490 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11491 }
11492
e142c38c 11493 cu->list_in_scope = &local_symbols;
c906108c 11494
639d11d3 11495 if (die->child != NULL)
c906108c 11496 {
639d11d3 11497 child_die = die->child;
c906108c
SS
11498 while (child_die && child_die->tag)
11499 {
34eaf542
TT
11500 if (child_die->tag == DW_TAG_template_type_param
11501 || child_die->tag == DW_TAG_template_value_param)
11502 {
11503 struct symbol *arg = new_symbol (child_die, NULL, cu);
11504
f1078f66
DJ
11505 if (arg != NULL)
11506 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11507 }
11508 else
11509 process_die (child_die, cu);
c906108c
SS
11510 child_die = sibling_die (child_die);
11511 }
11512 }
11513
d389af10
JK
11514 inherit_abstract_dies (die, cu);
11515
4a811a97
UW
11516 /* If we have a DW_AT_specification, we might need to import using
11517 directives from the context of the specification DIE. See the
11518 comment in determine_prefix. */
11519 if (cu->language == language_cplus
11520 && dwarf2_attr (die, DW_AT_specification, cu))
11521 {
11522 struct dwarf2_cu *spec_cu = cu;
11523 struct die_info *spec_die = die_specification (die, &spec_cu);
11524
11525 while (spec_die)
11526 {
11527 child_die = spec_die->child;
11528 while (child_die && child_die->tag)
11529 {
11530 if (child_die->tag == DW_TAG_imported_module)
11531 process_die (child_die, spec_cu);
11532 child_die = sibling_die (child_die);
11533 }
11534
11535 /* In some cases, GCC generates specification DIEs that
11536 themselves contain DW_AT_specification attributes. */
11537 spec_die = die_specification (spec_die, &spec_cu);
11538 }
11539 }
11540
fe978cb0 11541 newobj = pop_context ();
c906108c 11542 /* Make a block for the local symbols within. */
fe978cb0 11543 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11544 newobj->static_link, lowpc, highpc);
801e3a5b 11545
df8a16a1 11546 /* For C++, set the block's scope. */
45280282
IB
11547 if ((cu->language == language_cplus
11548 || cu->language == language_fortran
c44af4eb
TT
11549 || cu->language == language_d
11550 || cu->language == language_rust)
4d4ec4e5 11551 && cu->processing_has_namespace_info)
195a3f6c
TT
11552 block_set_scope (block, determine_prefix (die, cu),
11553 &objfile->objfile_obstack);
df8a16a1 11554
801e3a5b
JB
11555 /* If we have address ranges, record them. */
11556 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11557
fe978cb0 11558 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11559
34eaf542
TT
11560 /* Attach template arguments to function. */
11561 if (! VEC_empty (symbolp, template_args))
11562 {
11563 gdb_assert (templ_func != NULL);
11564
11565 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11566 templ_func->template_arguments
8d749320
SM
11567 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11568 templ_func->n_template_arguments);
34eaf542
TT
11569 memcpy (templ_func->template_arguments,
11570 VEC_address (symbolp, template_args),
11571 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11572 VEC_free (symbolp, template_args);
11573 }
11574
208d8187
JB
11575 /* In C++, we can have functions nested inside functions (e.g., when
11576 a function declares a class that has methods). This means that
11577 when we finish processing a function scope, we may need to go
11578 back to building a containing block's symbol lists. */
fe978cb0 11579 local_symbols = newobj->locals;
22cee43f 11580 local_using_directives = newobj->local_using_directives;
208d8187 11581
921e78cf
JB
11582 /* If we've finished processing a top-level function, subsequent
11583 symbols go in the file symbol list. */
11584 if (outermost_context_p ())
e142c38c 11585 cu->list_in_scope = &file_symbols;
c906108c
SS
11586}
11587
11588/* Process all the DIES contained within a lexical block scope. Start
11589 a new scope, process the dies, and then close the scope. */
11590
11591static void
e7c27a73 11592read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11593{
e7c27a73 11594 struct objfile *objfile = cu->objfile;
3e29f34a 11595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11596 struct context_stack *newobj;
c906108c
SS
11597 CORE_ADDR lowpc, highpc;
11598 struct die_info *child_die;
e142c38c
DJ
11599 CORE_ADDR baseaddr;
11600
11601 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11602
11603 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11604 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11605 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11606 be nasty. Might be easier to properly extend generic blocks to
af34e669 11607 describe ranges. */
e385593e
JK
11608 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11609 {
11610 case PC_BOUNDS_NOT_PRESENT:
11611 /* DW_TAG_lexical_block has no attributes, process its children as if
11612 there was no wrapping by that DW_TAG_lexical_block.
11613 GCC does no longer produces such DWARF since GCC r224161. */
11614 for (child_die = die->child;
11615 child_die != NULL && child_die->tag;
11616 child_die = sibling_die (child_die))
11617 process_die (child_die, cu);
11618 return;
11619 case PC_BOUNDS_INVALID:
11620 return;
11621 }
3e29f34a
MR
11622 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11623 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11624
11625 push_context (0, lowpc);
639d11d3 11626 if (die->child != NULL)
c906108c 11627 {
639d11d3 11628 child_die = die->child;
c906108c
SS
11629 while (child_die && child_die->tag)
11630 {
e7c27a73 11631 process_die (child_die, cu);
c906108c
SS
11632 child_die = sibling_die (child_die);
11633 }
11634 }
3ea89b92 11635 inherit_abstract_dies (die, cu);
fe978cb0 11636 newobj = pop_context ();
c906108c 11637
22cee43f 11638 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11639 {
801e3a5b 11640 struct block *block
63e43d3a 11641 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11642 newobj->start_addr, highpc);
801e3a5b
JB
11643
11644 /* Note that recording ranges after traversing children, as we
11645 do here, means that recording a parent's ranges entails
11646 walking across all its children's ranges as they appear in
11647 the address map, which is quadratic behavior.
11648
11649 It would be nicer to record the parent's ranges before
11650 traversing its children, simply overriding whatever you find
11651 there. But since we don't even decide whether to create a
11652 block until after we've traversed its children, that's hard
11653 to do. */
11654 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11655 }
fe978cb0 11656 local_symbols = newobj->locals;
22cee43f 11657 local_using_directives = newobj->local_using_directives;
c906108c
SS
11658}
11659
216f72a1 11660/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11661
11662static void
11663read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11664{
11665 struct objfile *objfile = cu->objfile;
11666 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11667 CORE_ADDR pc, baseaddr;
11668 struct attribute *attr;
11669 struct call_site *call_site, call_site_local;
11670 void **slot;
11671 int nparams;
11672 struct die_info *child_die;
11673
11674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11675
216f72a1
JK
11676 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11677 if (attr == NULL)
11678 {
11679 /* This was a pre-DWARF-5 GNU extension alias
11680 for DW_AT_call_return_pc. */
11681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11682 }
96408a79
SA
11683 if (!attr)
11684 {
11685 complaint (&symfile_complaints,
216f72a1 11686 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11687 "DIE 0x%x [in module %s]"),
4262abfb 11688 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11689 return;
11690 }
31aa7e4e 11691 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11692 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11693
11694 if (cu->call_site_htab == NULL)
11695 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11696 NULL, &objfile->objfile_obstack,
11697 hashtab_obstack_allocate, NULL);
11698 call_site_local.pc = pc;
11699 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11700 if (*slot != NULL)
11701 {
11702 complaint (&symfile_complaints,
216f72a1 11703 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11704 "DIE 0x%x [in module %s]"),
4262abfb
JK
11705 paddress (gdbarch, pc), die->offset.sect_off,
11706 objfile_name (objfile));
96408a79
SA
11707 return;
11708 }
11709
11710 /* Count parameters at the caller. */
11711
11712 nparams = 0;
11713 for (child_die = die->child; child_die && child_die->tag;
11714 child_die = sibling_die (child_die))
11715 {
216f72a1
JK
11716 if (child_die->tag != DW_TAG_call_site_parameter
11717 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11718 {
11719 complaint (&symfile_complaints,
216f72a1
JK
11720 _("Tag %d is not DW_TAG_call_site_parameter in "
11721 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
11722 child_die->tag, child_die->offset.sect_off,
11723 objfile_name (objfile));
96408a79
SA
11724 continue;
11725 }
11726
11727 nparams++;
11728 }
11729
224c3ddb
SM
11730 call_site
11731 = ((struct call_site *)
11732 obstack_alloc (&objfile->objfile_obstack,
11733 sizeof (*call_site)
11734 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11735 *slot = call_site;
11736 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11737 call_site->pc = pc;
11738
216f72a1
JK
11739 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11740 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11741 {
11742 struct die_info *func_die;
11743
11744 /* Skip also over DW_TAG_inlined_subroutine. */
11745 for (func_die = die->parent;
11746 func_die && func_die->tag != DW_TAG_subprogram
11747 && func_die->tag != DW_TAG_subroutine_type;
11748 func_die = func_die->parent);
11749
216f72a1
JK
11750 /* DW_AT_call_all_calls is a superset
11751 of DW_AT_call_all_tail_calls. */
96408a79 11752 if (func_die
216f72a1 11753 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11754 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11755 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11756 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11757 {
11758 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11759 not complete. But keep CALL_SITE for look ups via call_site_htab,
11760 both the initial caller containing the real return address PC and
11761 the final callee containing the current PC of a chain of tail
11762 calls do not need to have the tail call list complete. But any
11763 function candidate for a virtual tail call frame searched via
11764 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11765 determined unambiguously. */
11766 }
11767 else
11768 {
11769 struct type *func_type = NULL;
11770
11771 if (func_die)
11772 func_type = get_die_type (func_die, cu);
11773 if (func_type != NULL)
11774 {
11775 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11776
11777 /* Enlist this call site to the function. */
11778 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11779 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11780 }
11781 else
11782 complaint (&symfile_complaints,
216f72a1 11783 _("Cannot find function owning DW_TAG_call_site "
96408a79 11784 "DIE 0x%x [in module %s]"),
4262abfb 11785 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11786 }
11787 }
11788
216f72a1
JK
11789 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11790 if (attr == NULL)
11791 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11792 if (attr == NULL)
11793 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 11794 if (attr == NULL)
216f72a1
JK
11795 {
11796 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11797 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11798 }
96408a79
SA
11799 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11800 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11801 /* Keep NULL DWARF_BLOCK. */;
11802 else if (attr_form_is_block (attr))
11803 {
11804 struct dwarf2_locexpr_baton *dlbaton;
11805
8d749320 11806 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
11807 dlbaton->data = DW_BLOCK (attr)->data;
11808 dlbaton->size = DW_BLOCK (attr)->size;
11809 dlbaton->per_cu = cu->per_cu;
11810
11811 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11812 }
7771576e 11813 else if (attr_form_is_ref (attr))
96408a79 11814 {
96408a79
SA
11815 struct dwarf2_cu *target_cu = cu;
11816 struct die_info *target_die;
11817
ac9ec31b 11818 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11819 gdb_assert (target_cu->objfile == objfile);
11820 if (die_is_declaration (target_die, target_cu))
11821 {
7d45c7c3 11822 const char *target_physname;
9112db09
JK
11823
11824 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11825 target_physname = dwarf2_string_attr (target_die,
11826 DW_AT_linkage_name,
11827 target_cu);
11828 if (target_physname == NULL)
11829 target_physname = dwarf2_string_attr (target_die,
11830 DW_AT_MIPS_linkage_name,
11831 target_cu);
11832 if (target_physname == NULL)
9112db09 11833 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11834 if (target_physname == NULL)
11835 complaint (&symfile_complaints,
216f72a1 11836 _("DW_AT_call_target target DIE has invalid "
96408a79 11837 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 11838 die->offset.sect_off, objfile_name (objfile));
96408a79 11839 else
7d455152 11840 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11841 }
11842 else
11843 {
11844 CORE_ADDR lowpc;
11845
11846 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11847 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11848 <= PC_BOUNDS_INVALID)
96408a79 11849 complaint (&symfile_complaints,
216f72a1 11850 _("DW_AT_call_target target DIE has invalid "
96408a79 11851 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 11852 die->offset.sect_off, objfile_name (objfile));
96408a79 11853 else
3e29f34a
MR
11854 {
11855 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11856 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11857 }
96408a79
SA
11858 }
11859 }
11860 else
11861 complaint (&symfile_complaints,
216f72a1 11862 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 11863 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 11864 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11865
11866 call_site->per_cu = cu->per_cu;
11867
11868 for (child_die = die->child;
11869 child_die && child_die->tag;
11870 child_die = sibling_die (child_die))
11871 {
96408a79 11872 struct call_site_parameter *parameter;
1788b2d3 11873 struct attribute *loc, *origin;
96408a79 11874
216f72a1
JK
11875 if (child_die->tag != DW_TAG_call_site_parameter
11876 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11877 {
11878 /* Already printed the complaint above. */
11879 continue;
11880 }
11881
11882 gdb_assert (call_site->parameter_count < nparams);
11883 parameter = &call_site->parameter[call_site->parameter_count];
11884
1788b2d3
JK
11885 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11886 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 11887 register is contained in DW_AT_call_value. */
96408a79 11888
24c5c679 11889 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
11890 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11891 if (origin == NULL)
11892 {
11893 /* This was a pre-DWARF-5 GNU extension alias
11894 for DW_AT_call_parameter. */
11895 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11896 }
7771576e 11897 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
11898 {
11899 sect_offset offset;
11900
11901 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11902 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
11903 if (!offset_in_cu_p (&cu->header, offset))
11904 {
11905 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11906 binding can be done only inside one CU. Such referenced DIE
11907 therefore cannot be even moved to DW_TAG_partial_unit. */
11908 complaint (&symfile_complaints,
216f72a1
JK
11909 _("DW_AT_call_parameter offset is not in CU for "
11910 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11911 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
11912 continue;
11913 }
1788b2d3
JK
11914 parameter->u.param_offset.cu_off = (offset.sect_off
11915 - cu->header.offset.sect_off);
11916 }
11917 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11918 {
11919 complaint (&symfile_complaints,
11920 _("No DW_FORM_block* DW_AT_location for "
216f72a1 11921 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11922 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11923 continue;
11924 }
24c5c679 11925 else
96408a79 11926 {
24c5c679
JK
11927 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11928 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11929 if (parameter->u.dwarf_reg != -1)
11930 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11931 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11932 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11933 &parameter->u.fb_offset))
11934 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11935 else
11936 {
11937 complaint (&symfile_complaints,
11938 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11939 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 11940 "DW_TAG_call_site child DIE 0x%x "
24c5c679 11941 "[in module %s]"),
4262abfb 11942 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
11943 continue;
11944 }
96408a79
SA
11945 }
11946
216f72a1
JK
11947 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
11948 if (attr == NULL)
11949 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
11950 if (!attr_form_is_block (attr))
11951 {
11952 complaint (&symfile_complaints,
216f72a1
JK
11953 _("No DW_FORM_block* DW_AT_call_value for "
11954 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11955 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11956 continue;
11957 }
11958 parameter->value = DW_BLOCK (attr)->data;
11959 parameter->value_size = DW_BLOCK (attr)->size;
11960
11961 /* Parameters are not pre-cleared by memset above. */
11962 parameter->data_value = NULL;
11963 parameter->data_value_size = 0;
11964 call_site->parameter_count++;
11965
216f72a1
JK
11966 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
11967 if (attr == NULL)
11968 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
11969 if (attr)
11970 {
11971 if (!attr_form_is_block (attr))
11972 complaint (&symfile_complaints,
216f72a1
JK
11973 _("No DW_FORM_block* DW_AT_call_data_value for "
11974 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11975 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11976 else
11977 {
11978 parameter->data_value = DW_BLOCK (attr)->data;
11979 parameter->data_value_size = DW_BLOCK (attr)->size;
11980 }
11981 }
11982 }
11983}
11984
43988095
JK
11985/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
11986 reading .debug_rnglists.
11987 Callback's type should be:
11988 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
11989 Return true if the attributes are present and valid, otherwise,
11990 return false. */
11991
11992template <typename Callback>
11993static bool
11994dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
11995 Callback &&callback)
11996{
11997 struct objfile *objfile = cu->objfile;
11998 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11999 struct comp_unit_head *cu_header = &cu->header;
12000 bfd *obfd = objfile->obfd;
12001 unsigned int addr_size = cu_header->addr_size;
12002 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12003 /* Base address selection entry. */
12004 CORE_ADDR base;
12005 int found_base;
12006 unsigned int dummy;
12007 const gdb_byte *buffer;
12008 CORE_ADDR low = 0;
12009 CORE_ADDR high = 0;
12010 CORE_ADDR baseaddr;
12011 bool overflow = false;
12012
12013 found_base = cu->base_known;
12014 base = cu->base_address;
12015
12016 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12017 if (offset >= dwarf2_per_objfile->rnglists.size)
12018 {
12019 complaint (&symfile_complaints,
12020 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12021 offset);
12022 return false;
12023 }
12024 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12025
12026 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12027
12028 while (1)
12029 {
12030 CORE_ADDR range_beginning, range_end;
12031 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12032 + dwarf2_per_objfile->rnglists.size);
12033 unsigned int bytes_read;
12034
12035 if (buffer == buf_end)
12036 {
12037 overflow = true;
12038 break;
12039 }
12040 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12041 switch (rlet)
12042 {
12043 case DW_RLE_end_of_list:
12044 break;
12045 case DW_RLE_base_address:
12046 if (buffer + cu->header.addr_size > buf_end)
12047 {
12048 overflow = true;
12049 break;
12050 }
12051 base = read_address (obfd, buffer, cu, &bytes_read);
12052 found_base = 1;
12053 buffer += bytes_read;
12054 break;
12055 case DW_RLE_start_length:
12056 if (buffer + cu->header.addr_size > buf_end)
12057 {
12058 overflow = true;
12059 break;
12060 }
12061 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12062 buffer += bytes_read;
12063 range_end = (range_beginning
12064 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12065 buffer += bytes_read;
12066 if (buffer > buf_end)
12067 {
12068 overflow = true;
12069 break;
12070 }
12071 break;
12072 case DW_RLE_offset_pair:
12073 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12074 buffer += bytes_read;
12075 if (buffer > buf_end)
12076 {
12077 overflow = true;
12078 break;
12079 }
12080 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12081 buffer += bytes_read;
12082 if (buffer > buf_end)
12083 {
12084 overflow = true;
12085 break;
12086 }
12087 break;
12088 case DW_RLE_start_end:
12089 if (buffer + 2 * cu->header.addr_size > buf_end)
12090 {
12091 overflow = true;
12092 break;
12093 }
12094 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12095 buffer += bytes_read;
12096 range_end = read_address (obfd, buffer, cu, &bytes_read);
12097 buffer += bytes_read;
12098 break;
12099 default:
12100 complaint (&symfile_complaints,
12101 _("Invalid .debug_rnglists data (no base address)"));
12102 return false;
12103 }
12104 if (rlet == DW_RLE_end_of_list || overflow)
12105 break;
12106 if (rlet == DW_RLE_base_address)
12107 continue;
12108
12109 if (!found_base)
12110 {
12111 /* We have no valid base address for the ranges
12112 data. */
12113 complaint (&symfile_complaints,
12114 _("Invalid .debug_rnglists data (no base address)"));
12115 return false;
12116 }
12117
12118 if (range_beginning > range_end)
12119 {
12120 /* Inverted range entries are invalid. */
12121 complaint (&symfile_complaints,
12122 _("Invalid .debug_rnglists data (inverted range)"));
12123 return false;
12124 }
12125
12126 /* Empty range entries have no effect. */
12127 if (range_beginning == range_end)
12128 continue;
12129
12130 range_beginning += base;
12131 range_end += base;
12132
12133 /* A not-uncommon case of bad debug info.
12134 Don't pollute the addrmap with bad data. */
12135 if (range_beginning + baseaddr == 0
12136 && !dwarf2_per_objfile->has_section_at_zero)
12137 {
12138 complaint (&symfile_complaints,
12139 _(".debug_rnglists entry has start address of zero"
12140 " [in module %s]"), objfile_name (objfile));
12141 continue;
12142 }
12143
12144 callback (range_beginning, range_end);
12145 }
12146
12147 if (overflow)
12148 {
12149 complaint (&symfile_complaints,
12150 _("Offset %d is not terminated "
12151 "for DW_AT_ranges attribute"),
12152 offset);
12153 return false;
12154 }
12155
12156 return true;
12157}
12158
12159/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12160 Callback's type should be:
12161 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12162 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12163
43988095 12164template <typename Callback>
43039443 12165static int
5f46c5a5 12166dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12167 Callback &&callback)
43039443
JK
12168{
12169 struct objfile *objfile = cu->objfile;
3e29f34a 12170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12171 struct comp_unit_head *cu_header = &cu->header;
12172 bfd *obfd = objfile->obfd;
12173 unsigned int addr_size = cu_header->addr_size;
12174 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12175 /* Base address selection entry. */
12176 CORE_ADDR base;
12177 int found_base;
12178 unsigned int dummy;
d521ce57 12179 const gdb_byte *buffer;
ff013f42 12180 CORE_ADDR baseaddr;
43039443 12181
43988095
JK
12182 if (cu_header->version >= 5)
12183 return dwarf2_rnglists_process (offset, cu, callback);
12184
d00adf39
DE
12185 found_base = cu->base_known;
12186 base = cu->base_address;
43039443 12187
be391dca 12188 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12189 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12190 {
12191 complaint (&symfile_complaints,
12192 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12193 offset);
12194 return 0;
12195 }
dce234bc 12196 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12197
e7030f15 12198 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12199
43039443
JK
12200 while (1)
12201 {
12202 CORE_ADDR range_beginning, range_end;
12203
12204 range_beginning = read_address (obfd, buffer, cu, &dummy);
12205 buffer += addr_size;
12206 range_end = read_address (obfd, buffer, cu, &dummy);
12207 buffer += addr_size;
12208 offset += 2 * addr_size;
12209
12210 /* An end of list marker is a pair of zero addresses. */
12211 if (range_beginning == 0 && range_end == 0)
12212 /* Found the end of list entry. */
12213 break;
12214
12215 /* Each base address selection entry is a pair of 2 values.
12216 The first is the largest possible address, the second is
12217 the base address. Check for a base address here. */
12218 if ((range_beginning & mask) == mask)
12219 {
28d2bfb9
AB
12220 /* If we found the largest possible address, then we already
12221 have the base address in range_end. */
12222 base = range_end;
43039443
JK
12223 found_base = 1;
12224 continue;
12225 }
12226
12227 if (!found_base)
12228 {
12229 /* We have no valid base address for the ranges
12230 data. */
12231 complaint (&symfile_complaints,
12232 _("Invalid .debug_ranges data (no base address)"));
12233 return 0;
12234 }
12235
9277c30c
UW
12236 if (range_beginning > range_end)
12237 {
12238 /* Inverted range entries are invalid. */
12239 complaint (&symfile_complaints,
12240 _("Invalid .debug_ranges data (inverted range)"));
12241 return 0;
12242 }
12243
12244 /* Empty range entries have no effect. */
12245 if (range_beginning == range_end)
12246 continue;
12247
43039443
JK
12248 range_beginning += base;
12249 range_end += base;
12250
01093045
DE
12251 /* A not-uncommon case of bad debug info.
12252 Don't pollute the addrmap with bad data. */
12253 if (range_beginning + baseaddr == 0
12254 && !dwarf2_per_objfile->has_section_at_zero)
12255 {
12256 complaint (&symfile_complaints,
12257 _(".debug_ranges entry has start address of zero"
4262abfb 12258 " [in module %s]"), objfile_name (objfile));
01093045
DE
12259 continue;
12260 }
12261
5f46c5a5
JK
12262 callback (range_beginning, range_end);
12263 }
12264
12265 return 1;
12266}
12267
12268/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12269 Return 1 if the attributes are present and valid, otherwise, return 0.
12270 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12271
12272static int
12273dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12274 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12275 struct partial_symtab *ranges_pst)
12276{
12277 struct objfile *objfile = cu->objfile;
12278 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12279 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12280 SECT_OFF_TEXT (objfile));
12281 int low_set = 0;
12282 CORE_ADDR low = 0;
12283 CORE_ADDR high = 0;
12284 int retval;
12285
12286 retval = dwarf2_ranges_process (offset, cu,
12287 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12288 {
9277c30c 12289 if (ranges_pst != NULL)
3e29f34a
MR
12290 {
12291 CORE_ADDR lowpc;
12292 CORE_ADDR highpc;
12293
12294 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12295 range_beginning + baseaddr);
12296 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12297 range_end + baseaddr);
12298 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12299 ranges_pst);
12300 }
ff013f42 12301
43039443
JK
12302 /* FIXME: This is recording everything as a low-high
12303 segment of consecutive addresses. We should have a
12304 data structure for discontiguous block ranges
12305 instead. */
12306 if (! low_set)
12307 {
12308 low = range_beginning;
12309 high = range_end;
12310 low_set = 1;
12311 }
12312 else
12313 {
12314 if (range_beginning < low)
12315 low = range_beginning;
12316 if (range_end > high)
12317 high = range_end;
12318 }
5f46c5a5
JK
12319 });
12320 if (!retval)
12321 return 0;
43039443
JK
12322
12323 if (! low_set)
12324 /* If the first entry is an end-of-list marker, the range
12325 describes an empty scope, i.e. no instructions. */
12326 return 0;
12327
12328 if (low_return)
12329 *low_return = low;
12330 if (high_return)
12331 *high_return = high;
12332 return 1;
12333}
12334
3a2b436a
JK
12335/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12336 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12337 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12338
3a2b436a 12339static enum pc_bounds_kind
af34e669 12340dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12341 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12342 struct partial_symtab *pst)
c906108c
SS
12343{
12344 struct attribute *attr;
91da1414 12345 struct attribute *attr_high;
af34e669
DJ
12346 CORE_ADDR low = 0;
12347 CORE_ADDR high = 0;
e385593e 12348 enum pc_bounds_kind ret;
c906108c 12349
91da1414
MW
12350 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12351 if (attr_high)
af34e669 12352 {
e142c38c 12353 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12354 if (attr)
91da1414 12355 {
31aa7e4e
JB
12356 low = attr_value_as_address (attr);
12357 high = attr_value_as_address (attr_high);
12358 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12359 high += low;
91da1414 12360 }
af34e669
DJ
12361 else
12362 /* Found high w/o low attribute. */
e385593e 12363 return PC_BOUNDS_INVALID;
af34e669
DJ
12364
12365 /* Found consecutive range of addresses. */
3a2b436a 12366 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12367 }
c906108c 12368 else
af34e669 12369 {
e142c38c 12370 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12371 if (attr != NULL)
12372 {
ab435259
DE
12373 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12374 We take advantage of the fact that DW_AT_ranges does not appear
12375 in DW_TAG_compile_unit of DWO files. */
12376 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12377 unsigned int ranges_offset = (DW_UNSND (attr)
12378 + (need_ranges_base
12379 ? cu->ranges_base
12380 : 0));
2e3cf129 12381
af34e669 12382 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12383 .debug_ranges section. */
2e3cf129 12384 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12385 return PC_BOUNDS_INVALID;
43039443 12386 /* Found discontinuous range of addresses. */
3a2b436a 12387 ret = PC_BOUNDS_RANGES;
af34e669 12388 }
e385593e
JK
12389 else
12390 return PC_BOUNDS_NOT_PRESENT;
af34e669 12391 }
c906108c 12392
9373cf26
JK
12393 /* read_partial_die has also the strict LOW < HIGH requirement. */
12394 if (high <= low)
e385593e 12395 return PC_BOUNDS_INVALID;
c906108c
SS
12396
12397 /* When using the GNU linker, .gnu.linkonce. sections are used to
12398 eliminate duplicate copies of functions and vtables and such.
12399 The linker will arbitrarily choose one and discard the others.
12400 The AT_*_pc values for such functions refer to local labels in
12401 these sections. If the section from that file was discarded, the
12402 labels are not in the output, so the relocs get a value of 0.
12403 If this is a discarded function, mark the pc bounds as invalid,
12404 so that GDB will ignore it. */
72dca2f5 12405 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12406 return PC_BOUNDS_INVALID;
c906108c
SS
12407
12408 *lowpc = low;
96408a79
SA
12409 if (highpc)
12410 *highpc = high;
af34e669 12411 return ret;
c906108c
SS
12412}
12413
b084d499
JB
12414/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12415 its low and high PC addresses. Do nothing if these addresses could not
12416 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12417 and HIGHPC to the high address if greater than HIGHPC. */
12418
12419static void
12420dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12421 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12422 struct dwarf2_cu *cu)
12423{
12424 CORE_ADDR low, high;
12425 struct die_info *child = die->child;
12426
e385593e 12427 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12428 {
325fac50
PA
12429 *lowpc = std::min (*lowpc, low);
12430 *highpc = std::max (*highpc, high);
b084d499
JB
12431 }
12432
12433 /* If the language does not allow nested subprograms (either inside
12434 subprograms or lexical blocks), we're done. */
12435 if (cu->language != language_ada)
12436 return;
6e70227d 12437
b084d499
JB
12438 /* Check all the children of the given DIE. If it contains nested
12439 subprograms, then check their pc bounds. Likewise, we need to
12440 check lexical blocks as well, as they may also contain subprogram
12441 definitions. */
12442 while (child && child->tag)
12443 {
12444 if (child->tag == DW_TAG_subprogram
12445 || child->tag == DW_TAG_lexical_block)
12446 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12447 child = sibling_die (child);
12448 }
12449}
12450
fae299cd
DC
12451/* Get the low and high pc's represented by the scope DIE, and store
12452 them in *LOWPC and *HIGHPC. If the correct values can't be
12453 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12454
12455static void
12456get_scope_pc_bounds (struct die_info *die,
12457 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12458 struct dwarf2_cu *cu)
12459{
12460 CORE_ADDR best_low = (CORE_ADDR) -1;
12461 CORE_ADDR best_high = (CORE_ADDR) 0;
12462 CORE_ADDR current_low, current_high;
12463
3a2b436a 12464 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12465 >= PC_BOUNDS_RANGES)
fae299cd
DC
12466 {
12467 best_low = current_low;
12468 best_high = current_high;
12469 }
12470 else
12471 {
12472 struct die_info *child = die->child;
12473
12474 while (child && child->tag)
12475 {
12476 switch (child->tag) {
12477 case DW_TAG_subprogram:
b084d499 12478 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12479 break;
12480 case DW_TAG_namespace:
f55ee35c 12481 case DW_TAG_module:
fae299cd
DC
12482 /* FIXME: carlton/2004-01-16: Should we do this for
12483 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12484 that current GCC's always emit the DIEs corresponding
12485 to definitions of methods of classes as children of a
12486 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12487 the DIEs giving the declarations, which could be
12488 anywhere). But I don't see any reason why the
12489 standards says that they have to be there. */
12490 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12491
12492 if (current_low != ((CORE_ADDR) -1))
12493 {
325fac50
PA
12494 best_low = std::min (best_low, current_low);
12495 best_high = std::max (best_high, current_high);
fae299cd
DC
12496 }
12497 break;
12498 default:
0963b4bd 12499 /* Ignore. */
fae299cd
DC
12500 break;
12501 }
12502
12503 child = sibling_die (child);
12504 }
12505 }
12506
12507 *lowpc = best_low;
12508 *highpc = best_high;
12509}
12510
801e3a5b
JB
12511/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12512 in DIE. */
380bca97 12513
801e3a5b
JB
12514static void
12515dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12516 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12517{
bb5ed363 12518 struct objfile *objfile = cu->objfile;
3e29f34a 12519 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12520 struct attribute *attr;
91da1414 12521 struct attribute *attr_high;
801e3a5b 12522
91da1414
MW
12523 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12524 if (attr_high)
801e3a5b 12525 {
801e3a5b
JB
12526 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12527 if (attr)
12528 {
31aa7e4e
JB
12529 CORE_ADDR low = attr_value_as_address (attr);
12530 CORE_ADDR high = attr_value_as_address (attr_high);
12531
12532 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12533 high += low;
9a619af0 12534
3e29f34a
MR
12535 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12536 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12537 record_block_range (block, low, high - 1);
801e3a5b
JB
12538 }
12539 }
12540
12541 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12542 if (attr)
12543 {
bb5ed363 12544 bfd *obfd = objfile->obfd;
ab435259
DE
12545 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12546 We take advantage of the fact that DW_AT_ranges does not appear
12547 in DW_TAG_compile_unit of DWO files. */
12548 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12549
12550 /* The value of the DW_AT_ranges attribute is the offset of the
12551 address range list in the .debug_ranges section. */
ab435259
DE
12552 unsigned long offset = (DW_UNSND (attr)
12553 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12554 const gdb_byte *buffer;
801e3a5b
JB
12555
12556 /* For some target architectures, but not others, the
12557 read_address function sign-extends the addresses it returns.
12558 To recognize base address selection entries, we need a
12559 mask. */
12560 unsigned int addr_size = cu->header.addr_size;
12561 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12562
12563 /* The base address, to which the next pair is relative. Note
12564 that this 'base' is a DWARF concept: most entries in a range
12565 list are relative, to reduce the number of relocs against the
12566 debugging information. This is separate from this function's
12567 'baseaddr' argument, which GDB uses to relocate debugging
12568 information from a shared library based on the address at
12569 which the library was loaded. */
d00adf39
DE
12570 CORE_ADDR base = cu->base_address;
12571 int base_known = cu->base_known;
801e3a5b 12572
5f46c5a5
JK
12573 dwarf2_ranges_process (offset, cu,
12574 [&] (CORE_ADDR start, CORE_ADDR end)
12575 {
12576 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12577 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12578 record_block_range (block, start, end - 1);
12579 });
801e3a5b
JB
12580 }
12581}
12582
685b1105
JK
12583/* Check whether the producer field indicates either of GCC < 4.6, or the
12584 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12585
685b1105
JK
12586static void
12587check_producer (struct dwarf2_cu *cu)
60d5a603 12588{
38360086 12589 int major, minor;
60d5a603
JK
12590
12591 if (cu->producer == NULL)
12592 {
12593 /* For unknown compilers expect their behavior is DWARF version
12594 compliant.
12595
12596 GCC started to support .debug_types sections by -gdwarf-4 since
12597 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12598 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12599 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12600 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12601 }
b1ffba5a 12602 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12603 {
38360086
MW
12604 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12605 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12606 }
61012eef 12607 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12608 cu->producer_is_icc = 1;
12609 else
12610 {
12611 /* For other non-GCC compilers, expect their behavior is DWARF version
12612 compliant. */
60d5a603
JK
12613 }
12614
ba919b58 12615 cu->checked_producer = 1;
685b1105 12616}
ba919b58 12617
685b1105
JK
12618/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12619 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12620 during 4.6.0 experimental. */
12621
12622static int
12623producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12624{
12625 if (!cu->checked_producer)
12626 check_producer (cu);
12627
12628 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12629}
12630
12631/* Return the default accessibility type if it is not overriden by
12632 DW_AT_accessibility. */
12633
12634static enum dwarf_access_attribute
12635dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12636{
12637 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12638 {
12639 /* The default DWARF 2 accessibility for members is public, the default
12640 accessibility for inheritance is private. */
12641
12642 if (die->tag != DW_TAG_inheritance)
12643 return DW_ACCESS_public;
12644 else
12645 return DW_ACCESS_private;
12646 }
12647 else
12648 {
12649 /* DWARF 3+ defines the default accessibility a different way. The same
12650 rules apply now for DW_TAG_inheritance as for the members and it only
12651 depends on the container kind. */
12652
12653 if (die->parent->tag == DW_TAG_class_type)
12654 return DW_ACCESS_private;
12655 else
12656 return DW_ACCESS_public;
12657 }
12658}
12659
74ac6d43
TT
12660/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12661 offset. If the attribute was not found return 0, otherwise return
12662 1. If it was found but could not properly be handled, set *OFFSET
12663 to 0. */
12664
12665static int
12666handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12667 LONGEST *offset)
12668{
12669 struct attribute *attr;
12670
12671 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12672 if (attr != NULL)
12673 {
12674 *offset = 0;
12675
12676 /* Note that we do not check for a section offset first here.
12677 This is because DW_AT_data_member_location is new in DWARF 4,
12678 so if we see it, we can assume that a constant form is really
12679 a constant and not a section offset. */
12680 if (attr_form_is_constant (attr))
12681 *offset = dwarf2_get_attr_constant_value (attr, 0);
12682 else if (attr_form_is_section_offset (attr))
12683 dwarf2_complex_location_expr_complaint ();
12684 else if (attr_form_is_block (attr))
12685 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12686 else
12687 dwarf2_complex_location_expr_complaint ();
12688
12689 return 1;
12690 }
12691
12692 return 0;
12693}
12694
c906108c
SS
12695/* Add an aggregate field to the field list. */
12696
12697static void
107d2387 12698dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12699 struct dwarf2_cu *cu)
6e70227d 12700{
e7c27a73 12701 struct objfile *objfile = cu->objfile;
5e2b427d 12702 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12703 struct nextfield *new_field;
12704 struct attribute *attr;
12705 struct field *fp;
15d034d0 12706 const char *fieldname = "";
c906108c
SS
12707
12708 /* Allocate a new field list entry and link it in. */
8d749320 12709 new_field = XNEW (struct nextfield);
b8c9b27d 12710 make_cleanup (xfree, new_field);
c906108c 12711 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12712
12713 if (die->tag == DW_TAG_inheritance)
12714 {
12715 new_field->next = fip->baseclasses;
12716 fip->baseclasses = new_field;
12717 }
12718 else
12719 {
12720 new_field->next = fip->fields;
12721 fip->fields = new_field;
12722 }
c906108c
SS
12723 fip->nfields++;
12724
e142c38c 12725 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12726 if (attr)
12727 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12728 else
12729 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12730 if (new_field->accessibility != DW_ACCESS_public)
12731 fip->non_public_fields = 1;
60d5a603 12732
e142c38c 12733 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12734 if (attr)
12735 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12736 else
12737 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12738
12739 fp = &new_field->field;
a9a9bd0f 12740
e142c38c 12741 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12742 {
74ac6d43
TT
12743 LONGEST offset;
12744
a9a9bd0f 12745 /* Data member other than a C++ static data member. */
6e70227d 12746
c906108c 12747 /* Get type of field. */
e7c27a73 12748 fp->type = die_type (die, cu);
c906108c 12749
d6a843b5 12750 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12751
c906108c 12752 /* Get bit size of field (zero if none). */
e142c38c 12753 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12754 if (attr)
12755 {
12756 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12757 }
12758 else
12759 {
12760 FIELD_BITSIZE (*fp) = 0;
12761 }
12762
12763 /* Get bit offset of field. */
74ac6d43
TT
12764 if (handle_data_member_location (die, cu, &offset))
12765 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12766 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12767 if (attr)
12768 {
5e2b427d 12769 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12770 {
12771 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12772 additional bit offset from the MSB of the containing
12773 anonymous object to the MSB of the field. We don't
12774 have to do anything special since we don't need to
12775 know the size of the anonymous object. */
f41f5e61 12776 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12777 }
12778 else
12779 {
12780 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12781 MSB of the anonymous object, subtract off the number of
12782 bits from the MSB of the field to the MSB of the
12783 object, and then subtract off the number of bits of
12784 the field itself. The result is the bit offset of
12785 the LSB of the field. */
c906108c
SS
12786 int anonymous_size;
12787 int bit_offset = DW_UNSND (attr);
12788
e142c38c 12789 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12790 if (attr)
12791 {
12792 /* The size of the anonymous object containing
12793 the bit field is explicit, so use the
12794 indicated size (in bytes). */
12795 anonymous_size = DW_UNSND (attr);
12796 }
12797 else
12798 {
12799 /* The size of the anonymous object containing
12800 the bit field must be inferred from the type
12801 attribute of the data member containing the
12802 bit field. */
12803 anonymous_size = TYPE_LENGTH (fp->type);
12804 }
f41f5e61
PA
12805 SET_FIELD_BITPOS (*fp,
12806 (FIELD_BITPOS (*fp)
12807 + anonymous_size * bits_per_byte
12808 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12809 }
12810 }
da5b30da
AA
12811 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12812 if (attr != NULL)
12813 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12814 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
12815
12816 /* Get name of field. */
39cbfefa
DJ
12817 fieldname = dwarf2_name (die, cu);
12818 if (fieldname == NULL)
12819 fieldname = "";
d8151005
DJ
12820
12821 /* The name is already allocated along with this objfile, so we don't
12822 need to duplicate it for the type. */
12823 fp->name = fieldname;
c906108c
SS
12824
12825 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12826 pointer or virtual base class pointer) to private. */
e142c38c 12827 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12828 {
d48cc9dd 12829 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12830 new_field->accessibility = DW_ACCESS_private;
12831 fip->non_public_fields = 1;
12832 }
12833 }
a9a9bd0f 12834 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12835 {
a9a9bd0f
DC
12836 /* C++ static member. */
12837
12838 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12839 is a declaration, but all versions of G++ as of this writing
12840 (so through at least 3.2.1) incorrectly generate
12841 DW_TAG_variable tags. */
6e70227d 12842
ff355380 12843 const char *physname;
c906108c 12844
a9a9bd0f 12845 /* Get name of field. */
39cbfefa
DJ
12846 fieldname = dwarf2_name (die, cu);
12847 if (fieldname == NULL)
c906108c
SS
12848 return;
12849
254e6b9e 12850 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12851 if (attr
12852 /* Only create a symbol if this is an external value.
12853 new_symbol checks this and puts the value in the global symbol
12854 table, which we want. If it is not external, new_symbol
12855 will try to put the value in cu->list_in_scope which is wrong. */
12856 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12857 {
12858 /* A static const member, not much different than an enum as far as
12859 we're concerned, except that we can support more types. */
12860 new_symbol (die, NULL, cu);
12861 }
12862
2df3850c 12863 /* Get physical name. */
ff355380 12864 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12865
d8151005
DJ
12866 /* The name is already allocated along with this objfile, so we don't
12867 need to duplicate it for the type. */
12868 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12869 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12870 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12871 }
12872 else if (die->tag == DW_TAG_inheritance)
12873 {
74ac6d43 12874 LONGEST offset;
d4b96c9a 12875
74ac6d43
TT
12876 /* C++ base class field. */
12877 if (handle_data_member_location (die, cu, &offset))
12878 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12879 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12880 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12881 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12882 fip->nbaseclasses++;
12883 }
12884}
12885
98751a41
JK
12886/* Add a typedef defined in the scope of the FIP's class. */
12887
12888static void
12889dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12890 struct dwarf2_cu *cu)
6e70227d 12891{
98751a41 12892 struct typedef_field_list *new_field;
98751a41 12893 struct typedef_field *fp;
98751a41
JK
12894
12895 /* Allocate a new field list entry and link it in. */
8d749320 12896 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
12897 make_cleanup (xfree, new_field);
12898
12899 gdb_assert (die->tag == DW_TAG_typedef);
12900
12901 fp = &new_field->field;
12902
12903 /* Get name of field. */
12904 fp->name = dwarf2_name (die, cu);
12905 if (fp->name == NULL)
12906 return;
12907
12908 fp->type = read_type_die (die, cu);
12909
12910 new_field->next = fip->typedef_field_list;
12911 fip->typedef_field_list = new_field;
12912 fip->typedef_field_list_count++;
12913}
12914
c906108c
SS
12915/* Create the vector of fields, and attach it to the type. */
12916
12917static void
fba45db2 12918dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12919 struct dwarf2_cu *cu)
c906108c
SS
12920{
12921 int nfields = fip->nfields;
12922
12923 /* Record the field count, allocate space for the array of fields,
12924 and create blank accessibility bitfields if necessary. */
12925 TYPE_NFIELDS (type) = nfields;
12926 TYPE_FIELDS (type) = (struct field *)
12927 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12928 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12929
b4ba55a1 12930 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
12931 {
12932 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12933
12934 TYPE_FIELD_PRIVATE_BITS (type) =
12935 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12936 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12937
12938 TYPE_FIELD_PROTECTED_BITS (type) =
12939 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12940 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12941
774b6a14
TT
12942 TYPE_FIELD_IGNORE_BITS (type) =
12943 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12944 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
12945 }
12946
12947 /* If the type has baseclasses, allocate and clear a bit vector for
12948 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 12949 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
12950 {
12951 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 12952 unsigned char *pointer;
c906108c
SS
12953
12954 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 12955 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 12956 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
12957 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12958 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12959 }
12960
3e43a32a
MS
12961 /* Copy the saved-up fields into the field vector. Start from the head of
12962 the list, adding to the tail of the field array, so that they end up in
12963 the same order in the array in which they were added to the list. */
c906108c
SS
12964 while (nfields-- > 0)
12965 {
7d0ccb61
DJ
12966 struct nextfield *fieldp;
12967
12968 if (fip->fields)
12969 {
12970 fieldp = fip->fields;
12971 fip->fields = fieldp->next;
12972 }
12973 else
12974 {
12975 fieldp = fip->baseclasses;
12976 fip->baseclasses = fieldp->next;
12977 }
12978
12979 TYPE_FIELD (type, nfields) = fieldp->field;
12980 switch (fieldp->accessibility)
c906108c 12981 {
c5aa993b 12982 case DW_ACCESS_private:
b4ba55a1
JB
12983 if (cu->language != language_ada)
12984 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 12985 break;
c906108c 12986
c5aa993b 12987 case DW_ACCESS_protected:
b4ba55a1
JB
12988 if (cu->language != language_ada)
12989 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 12990 break;
c906108c 12991
c5aa993b
JM
12992 case DW_ACCESS_public:
12993 break;
c906108c 12994
c5aa993b
JM
12995 default:
12996 /* Unknown accessibility. Complain and treat it as public. */
12997 {
e2e0b3e5 12998 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 12999 fieldp->accessibility);
c5aa993b
JM
13000 }
13001 break;
c906108c
SS
13002 }
13003 if (nfields < fip->nbaseclasses)
13004 {
7d0ccb61 13005 switch (fieldp->virtuality)
c906108c 13006 {
c5aa993b
JM
13007 case DW_VIRTUALITY_virtual:
13008 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13009 if (cu->language == language_ada)
a73c6dcd 13010 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13011 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13012 break;
c906108c
SS
13013 }
13014 }
c906108c
SS
13015 }
13016}
13017
7d27a96d
TT
13018/* Return true if this member function is a constructor, false
13019 otherwise. */
13020
13021static int
13022dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13023{
13024 const char *fieldname;
fe978cb0 13025 const char *type_name;
7d27a96d
TT
13026 int len;
13027
13028 if (die->parent == NULL)
13029 return 0;
13030
13031 if (die->parent->tag != DW_TAG_structure_type
13032 && die->parent->tag != DW_TAG_union_type
13033 && die->parent->tag != DW_TAG_class_type)
13034 return 0;
13035
13036 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13037 type_name = dwarf2_name (die->parent, cu);
13038 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13039 return 0;
13040
13041 len = strlen (fieldname);
fe978cb0
PA
13042 return (strncmp (fieldname, type_name, len) == 0
13043 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13044}
13045
c906108c
SS
13046/* Add a member function to the proper fieldlist. */
13047
13048static void
107d2387 13049dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13050 struct type *type, struct dwarf2_cu *cu)
c906108c 13051{
e7c27a73 13052 struct objfile *objfile = cu->objfile;
c906108c
SS
13053 struct attribute *attr;
13054 struct fnfieldlist *flp;
13055 int i;
13056 struct fn_field *fnp;
15d034d0 13057 const char *fieldname;
c906108c 13058 struct nextfnfield *new_fnfield;
f792889a 13059 struct type *this_type;
60d5a603 13060 enum dwarf_access_attribute accessibility;
c906108c 13061
b4ba55a1 13062 if (cu->language == language_ada)
a73c6dcd 13063 error (_("unexpected member function in Ada type"));
b4ba55a1 13064
2df3850c 13065 /* Get name of member function. */
39cbfefa
DJ
13066 fieldname = dwarf2_name (die, cu);
13067 if (fieldname == NULL)
2df3850c 13068 return;
c906108c 13069
c906108c
SS
13070 /* Look up member function name in fieldlist. */
13071 for (i = 0; i < fip->nfnfields; i++)
13072 {
27bfe10e 13073 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13074 break;
13075 }
13076
13077 /* Create new list element if necessary. */
13078 if (i < fip->nfnfields)
13079 flp = &fip->fnfieldlists[i];
13080 else
13081 {
13082 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13083 {
13084 fip->fnfieldlists = (struct fnfieldlist *)
13085 xrealloc (fip->fnfieldlists,
13086 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13087 * sizeof (struct fnfieldlist));
c906108c 13088 if (fip->nfnfields == 0)
c13c43fd 13089 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13090 }
13091 flp = &fip->fnfieldlists[fip->nfnfields];
13092 flp->name = fieldname;
13093 flp->length = 0;
13094 flp->head = NULL;
3da10d80 13095 i = fip->nfnfields++;
c906108c
SS
13096 }
13097
13098 /* Create a new member function field and chain it to the field list
0963b4bd 13099 entry. */
8d749320 13100 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13101 make_cleanup (xfree, new_fnfield);
c906108c
SS
13102 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13103 new_fnfield->next = flp->head;
13104 flp->head = new_fnfield;
13105 flp->length++;
13106
13107 /* Fill in the member function field info. */
13108 fnp = &new_fnfield->fnfield;
3da10d80
KS
13109
13110 /* Delay processing of the physname until later. */
9c37b5ae 13111 if (cu->language == language_cplus)
3da10d80
KS
13112 {
13113 add_to_method_list (type, i, flp->length - 1, fieldname,
13114 die, cu);
13115 }
13116 else
13117 {
1d06ead6 13118 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13119 fnp->physname = physname ? physname : "";
13120 }
13121
c906108c 13122 fnp->type = alloc_type (objfile);
f792889a
DJ
13123 this_type = read_type_die (die, cu);
13124 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13125 {
f792889a 13126 int nparams = TYPE_NFIELDS (this_type);
c906108c 13127
f792889a 13128 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13129 of the method itself (TYPE_CODE_METHOD). */
13130 smash_to_method_type (fnp->type, type,
f792889a
DJ
13131 TYPE_TARGET_TYPE (this_type),
13132 TYPE_FIELDS (this_type),
13133 TYPE_NFIELDS (this_type),
13134 TYPE_VARARGS (this_type));
c906108c
SS
13135
13136 /* Handle static member functions.
c5aa993b 13137 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13138 member functions. G++ helps GDB by marking the first
13139 parameter for non-static member functions (which is the this
13140 pointer) as artificial. We obtain this information from
13141 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13142 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13143 fnp->voffset = VOFFSET_STATIC;
13144 }
13145 else
e2e0b3e5 13146 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13147 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13148
13149 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13150 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13151 fnp->fcontext = die_containing_type (die, cu);
c906108c 13152
3e43a32a
MS
13153 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13154 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13155
13156 /* Get accessibility. */
e142c38c 13157 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13158 if (attr)
aead7601 13159 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13160 else
13161 accessibility = dwarf2_default_access_attribute (die, cu);
13162 switch (accessibility)
c906108c 13163 {
60d5a603
JK
13164 case DW_ACCESS_private:
13165 fnp->is_private = 1;
13166 break;
13167 case DW_ACCESS_protected:
13168 fnp->is_protected = 1;
13169 break;
c906108c
SS
13170 }
13171
b02dede2 13172 /* Check for artificial methods. */
e142c38c 13173 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13174 if (attr && DW_UNSND (attr) != 0)
13175 fnp->is_artificial = 1;
13176
7d27a96d
TT
13177 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13178
0d564a31 13179 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13180 function. For older versions of GCC, this is an offset in the
13181 appropriate virtual table, as specified by DW_AT_containing_type.
13182 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13183 to the object address. */
13184
e142c38c 13185 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13186 if (attr)
8e19ed76 13187 {
aec5aa8b 13188 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13189 {
aec5aa8b
TT
13190 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13191 {
13192 /* Old-style GCC. */
13193 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13194 }
13195 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13196 || (DW_BLOCK (attr)->size > 1
13197 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13198 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13199 {
aec5aa8b
TT
13200 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13201 if ((fnp->voffset % cu->header.addr_size) != 0)
13202 dwarf2_complex_location_expr_complaint ();
13203 else
13204 fnp->voffset /= cu->header.addr_size;
13205 fnp->voffset += 2;
13206 }
13207 else
13208 dwarf2_complex_location_expr_complaint ();
13209
13210 if (!fnp->fcontext)
7e993ebf
KS
13211 {
13212 /* If there is no `this' field and no DW_AT_containing_type,
13213 we cannot actually find a base class context for the
13214 vtable! */
13215 if (TYPE_NFIELDS (this_type) == 0
13216 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13217 {
13218 complaint (&symfile_complaints,
13219 _("cannot determine context for virtual member "
13220 "function \"%s\" (offset %d)"),
13221 fieldname, die->offset.sect_off);
13222 }
13223 else
13224 {
13225 fnp->fcontext
13226 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13227 }
13228 }
aec5aa8b 13229 }
3690dd37 13230 else if (attr_form_is_section_offset (attr))
8e19ed76 13231 {
4d3c2250 13232 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13233 }
13234 else
13235 {
4d3c2250
KB
13236 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13237 fieldname);
8e19ed76 13238 }
0d564a31 13239 }
d48cc9dd
DJ
13240 else
13241 {
13242 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13243 if (attr && DW_UNSND (attr))
13244 {
13245 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13246 complaint (&symfile_complaints,
3e43a32a
MS
13247 _("Member function \"%s\" (offset %d) is virtual "
13248 "but the vtable offset is not specified"),
b64f50a1 13249 fieldname, die->offset.sect_off);
9655fd1a 13250 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13251 TYPE_CPLUS_DYNAMIC (type) = 1;
13252 }
13253 }
c906108c
SS
13254}
13255
13256/* Create the vector of member function fields, and attach it to the type. */
13257
13258static void
fba45db2 13259dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13260 struct dwarf2_cu *cu)
c906108c
SS
13261{
13262 struct fnfieldlist *flp;
c906108c
SS
13263 int i;
13264
b4ba55a1 13265 if (cu->language == language_ada)
a73c6dcd 13266 error (_("unexpected member functions in Ada type"));
b4ba55a1 13267
c906108c
SS
13268 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13269 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13270 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13271
13272 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13273 {
13274 struct nextfnfield *nfp = flp->head;
13275 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13276 int k;
13277
13278 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13279 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13280 fn_flp->fn_fields = (struct fn_field *)
13281 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13282 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13283 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13284 }
13285
13286 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13287}
13288
1168df01
JB
13289/* Returns non-zero if NAME is the name of a vtable member in CU's
13290 language, zero otherwise. */
13291static int
13292is_vtable_name (const char *name, struct dwarf2_cu *cu)
13293{
13294 static const char vptr[] = "_vptr";
987504bb 13295 static const char vtable[] = "vtable";
1168df01 13296
9c37b5ae
TT
13297 /* Look for the C++ form of the vtable. */
13298 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13299 return 1;
13300
13301 return 0;
13302}
13303
c0dd20ea 13304/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13305 functions, with the ABI-specified layout. If TYPE describes
13306 such a structure, smash it into a member function type.
61049d3b
DJ
13307
13308 GCC shouldn't do this; it should just output pointer to member DIEs.
13309 This is GCC PR debug/28767. */
c0dd20ea 13310
0b92b5bb
TT
13311static void
13312quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13313{
09e2d7c7 13314 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13315
13316 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13317 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13318 return;
c0dd20ea
DJ
13319
13320 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13321 if (TYPE_FIELD_NAME (type, 0) == NULL
13322 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13323 || TYPE_FIELD_NAME (type, 1) == NULL
13324 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13325 return;
c0dd20ea
DJ
13326
13327 /* Find the type of the method. */
0b92b5bb 13328 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13329 if (pfn_type == NULL
13330 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13331 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13332 return;
c0dd20ea
DJ
13333
13334 /* Look for the "this" argument. */
13335 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13336 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13337 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13338 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13339 return;
c0dd20ea 13340
09e2d7c7 13341 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13342 new_type = alloc_type (objfile);
09e2d7c7 13343 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13344 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13345 TYPE_VARARGS (pfn_type));
0b92b5bb 13346 smash_to_methodptr_type (type, new_type);
c0dd20ea 13347}
1168df01 13348
685b1105
JK
13349/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13350 (icc). */
13351
13352static int
13353producer_is_icc (struct dwarf2_cu *cu)
13354{
13355 if (!cu->checked_producer)
13356 check_producer (cu);
13357
13358 return cu->producer_is_icc;
13359}
13360
c906108c 13361/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13362 (definition) to create a type for the structure or union. Fill in
13363 the type's name and general properties; the members will not be
83655187
DE
13364 processed until process_structure_scope. A symbol table entry for
13365 the type will also not be done until process_structure_scope (assuming
13366 the type has a name).
c906108c 13367
c767944b
DJ
13368 NOTE: we need to call these functions regardless of whether or not the
13369 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13370 structure or union. This gets the type entered into our set of
83655187 13371 user defined types. */
c906108c 13372
f792889a 13373static struct type *
134d01f1 13374read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13375{
e7c27a73 13376 struct objfile *objfile = cu->objfile;
c906108c
SS
13377 struct type *type;
13378 struct attribute *attr;
15d034d0 13379 const char *name;
c906108c 13380
348e048f
DE
13381 /* If the definition of this type lives in .debug_types, read that type.
13382 Don't follow DW_AT_specification though, that will take us back up
13383 the chain and we want to go down. */
45e58e77 13384 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13385 if (attr)
13386 {
ac9ec31b 13387 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13388
ac9ec31b 13389 /* The type's CU may not be the same as CU.
02142a6c 13390 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13391 return set_die_type (die, type, cu);
13392 }
13393
c0dd20ea 13394 type = alloc_type (objfile);
c906108c 13395 INIT_CPLUS_SPECIFIC (type);
93311388 13396
39cbfefa
DJ
13397 name = dwarf2_name (die, cu);
13398 if (name != NULL)
c906108c 13399 {
987504bb 13400 if (cu->language == language_cplus
c44af4eb
TT
13401 || cu->language == language_d
13402 || cu->language == language_rust)
63d06c5c 13403 {
15d034d0 13404 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13405
13406 /* dwarf2_full_name might have already finished building the DIE's
13407 type. If so, there is no need to continue. */
13408 if (get_die_type (die, cu) != NULL)
13409 return get_die_type (die, cu);
13410
13411 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13412 if (die->tag == DW_TAG_structure_type
13413 || die->tag == DW_TAG_class_type)
13414 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13415 }
13416 else
13417 {
d8151005
DJ
13418 /* The name is already allocated along with this objfile, so
13419 we don't need to duplicate it for the type. */
7d455152 13420 TYPE_TAG_NAME (type) = name;
94af9270
KS
13421 if (die->tag == DW_TAG_class_type)
13422 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13423 }
c906108c
SS
13424 }
13425
13426 if (die->tag == DW_TAG_structure_type)
13427 {
13428 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13429 }
13430 else if (die->tag == DW_TAG_union_type)
13431 {
13432 TYPE_CODE (type) = TYPE_CODE_UNION;
13433 }
13434 else
13435 {
4753d33b 13436 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13437 }
13438
0cc2414c
TT
13439 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13440 TYPE_DECLARED_CLASS (type) = 1;
13441
e142c38c 13442 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13443 if (attr)
13444 {
155bfbd3
JB
13445 if (attr_form_is_constant (attr))
13446 TYPE_LENGTH (type) = DW_UNSND (attr);
13447 else
13448 {
13449 /* For the moment, dynamic type sizes are not supported
13450 by GDB's struct type. The actual size is determined
13451 on-demand when resolving the type of a given object,
13452 so set the type's length to zero for now. Otherwise,
13453 we record an expression as the length, and that expression
13454 could lead to a very large value, which could eventually
13455 lead to us trying to allocate that much memory when creating
13456 a value of that type. */
13457 TYPE_LENGTH (type) = 0;
13458 }
c906108c
SS
13459 }
13460 else
13461 {
13462 TYPE_LENGTH (type) = 0;
13463 }
13464
422b1cb0 13465 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13466 {
13467 /* ICC does not output the required DW_AT_declaration
13468 on incomplete types, but gives them a size of zero. */
422b1cb0 13469 TYPE_STUB (type) = 1;
685b1105
JK
13470 }
13471 else
13472 TYPE_STUB_SUPPORTED (type) = 1;
13473
dc718098 13474 if (die_is_declaration (die, cu))
876cecd0 13475 TYPE_STUB (type) = 1;
a6c727b2
DJ
13476 else if (attr == NULL && die->child == NULL
13477 && producer_is_realview (cu->producer))
13478 /* RealView does not output the required DW_AT_declaration
13479 on incomplete types. */
13480 TYPE_STUB (type) = 1;
dc718098 13481
c906108c
SS
13482 /* We need to add the type field to the die immediately so we don't
13483 infinitely recurse when dealing with pointers to the structure
0963b4bd 13484 type within the structure itself. */
1c379e20 13485 set_die_type (die, type, cu);
c906108c 13486
7e314c57
JK
13487 /* set_die_type should be already done. */
13488 set_descriptive_type (type, die, cu);
13489
c767944b
DJ
13490 return type;
13491}
13492
13493/* Finish creating a structure or union type, including filling in
13494 its members and creating a symbol for it. */
13495
13496static void
13497process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13498{
13499 struct objfile *objfile = cu->objfile;
ca040673 13500 struct die_info *child_die;
c767944b
DJ
13501 struct type *type;
13502
13503 type = get_die_type (die, cu);
13504 if (type == NULL)
13505 type = read_structure_type (die, cu);
13506
e142c38c 13507 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13508 {
13509 struct field_info fi;
34eaf542 13510 VEC (symbolp) *template_args = NULL;
c767944b 13511 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13512
13513 memset (&fi, 0, sizeof (struct field_info));
13514
639d11d3 13515 child_die = die->child;
c906108c
SS
13516
13517 while (child_die && child_die->tag)
13518 {
a9a9bd0f
DC
13519 if (child_die->tag == DW_TAG_member
13520 || child_die->tag == DW_TAG_variable)
c906108c 13521 {
a9a9bd0f
DC
13522 /* NOTE: carlton/2002-11-05: A C++ static data member
13523 should be a DW_TAG_member that is a declaration, but
13524 all versions of G++ as of this writing (so through at
13525 least 3.2.1) incorrectly generate DW_TAG_variable
13526 tags for them instead. */
e7c27a73 13527 dwarf2_add_field (&fi, child_die, cu);
c906108c 13528 }
8713b1b1 13529 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13530 {
e98c9e7c
TT
13531 /* Rust doesn't have member functions in the C++ sense.
13532 However, it does emit ordinary functions as children
13533 of a struct DIE. */
13534 if (cu->language == language_rust)
13535 read_func_scope (child_die, cu);
13536 else
13537 {
13538 /* C++ member function. */
13539 dwarf2_add_member_fn (&fi, child_die, type, cu);
13540 }
c906108c
SS
13541 }
13542 else if (child_die->tag == DW_TAG_inheritance)
13543 {
13544 /* C++ base class field. */
e7c27a73 13545 dwarf2_add_field (&fi, child_die, cu);
c906108c 13546 }
98751a41
JK
13547 else if (child_die->tag == DW_TAG_typedef)
13548 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13549 else if (child_die->tag == DW_TAG_template_type_param
13550 || child_die->tag == DW_TAG_template_value_param)
13551 {
13552 struct symbol *arg = new_symbol (child_die, NULL, cu);
13553
f1078f66
DJ
13554 if (arg != NULL)
13555 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13556 }
13557
c906108c
SS
13558 child_die = sibling_die (child_die);
13559 }
13560
34eaf542
TT
13561 /* Attach template arguments to type. */
13562 if (! VEC_empty (symbolp, template_args))
13563 {
13564 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13565 TYPE_N_TEMPLATE_ARGUMENTS (type)
13566 = VEC_length (symbolp, template_args);
13567 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13568 = XOBNEWVEC (&objfile->objfile_obstack,
13569 struct symbol *,
13570 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13571 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13572 VEC_address (symbolp, template_args),
13573 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13574 * sizeof (struct symbol *)));
13575 VEC_free (symbolp, template_args);
13576 }
13577
c906108c
SS
13578 /* Attach fields and member functions to the type. */
13579 if (fi.nfields)
e7c27a73 13580 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13581 if (fi.nfnfields)
13582 {
e7c27a73 13583 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13584
c5aa993b 13585 /* Get the type which refers to the base class (possibly this
c906108c 13586 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13587 class from the DW_AT_containing_type attribute. This use of
13588 DW_AT_containing_type is a GNU extension. */
c906108c 13589
e142c38c 13590 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13591 {
e7c27a73 13592 struct type *t = die_containing_type (die, cu);
c906108c 13593
ae6ae975 13594 set_type_vptr_basetype (type, t);
c906108c
SS
13595 if (type == t)
13596 {
c906108c
SS
13597 int i;
13598
13599 /* Our own class provides vtbl ptr. */
13600 for (i = TYPE_NFIELDS (t) - 1;
13601 i >= TYPE_N_BASECLASSES (t);
13602 --i)
13603 {
0d5cff50 13604 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13605
1168df01 13606 if (is_vtable_name (fieldname, cu))
c906108c 13607 {
ae6ae975 13608 set_type_vptr_fieldno (type, i);
c906108c
SS
13609 break;
13610 }
13611 }
13612
13613 /* Complain if virtual function table field not found. */
13614 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13615 complaint (&symfile_complaints,
3e43a32a
MS
13616 _("virtual function table pointer "
13617 "not found when defining class '%s'"),
4d3c2250
KB
13618 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13619 "");
c906108c
SS
13620 }
13621 else
13622 {
ae6ae975 13623 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13624 }
13625 }
f6235d4c 13626 else if (cu->producer
61012eef 13627 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13628 {
13629 /* The IBM XLC compiler does not provide direct indication
13630 of the containing type, but the vtable pointer is
13631 always named __vfp. */
13632
13633 int i;
13634
13635 for (i = TYPE_NFIELDS (type) - 1;
13636 i >= TYPE_N_BASECLASSES (type);
13637 --i)
13638 {
13639 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13640 {
ae6ae975
DE
13641 set_type_vptr_fieldno (type, i);
13642 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13643 break;
13644 }
13645 }
13646 }
c906108c 13647 }
98751a41
JK
13648
13649 /* Copy fi.typedef_field_list linked list elements content into the
13650 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13651 if (fi.typedef_field_list)
13652 {
13653 int i = fi.typedef_field_list_count;
13654
a0d7a4ff 13655 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13656 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13657 = ((struct typedef_field *)
13658 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13659 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13660
13661 /* Reverse the list order to keep the debug info elements order. */
13662 while (--i >= 0)
13663 {
13664 struct typedef_field *dest, *src;
6e70227d 13665
98751a41
JK
13666 dest = &TYPE_TYPEDEF_FIELD (type, i);
13667 src = &fi.typedef_field_list->field;
13668 fi.typedef_field_list = fi.typedef_field_list->next;
13669 *dest = *src;
13670 }
13671 }
c767944b
DJ
13672
13673 do_cleanups (back_to);
c906108c 13674 }
63d06c5c 13675
bb5ed363 13676 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13677
90aeadfc
DC
13678 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13679 snapshots) has been known to create a die giving a declaration
13680 for a class that has, as a child, a die giving a definition for a
13681 nested class. So we have to process our children even if the
13682 current die is a declaration. Normally, of course, a declaration
13683 won't have any children at all. */
134d01f1 13684
ca040673
DE
13685 child_die = die->child;
13686
90aeadfc
DC
13687 while (child_die != NULL && child_die->tag)
13688 {
13689 if (child_die->tag == DW_TAG_member
13690 || child_die->tag == DW_TAG_variable
34eaf542
TT
13691 || child_die->tag == DW_TAG_inheritance
13692 || child_die->tag == DW_TAG_template_value_param
13693 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13694 {
90aeadfc 13695 /* Do nothing. */
134d01f1 13696 }
90aeadfc
DC
13697 else
13698 process_die (child_die, cu);
134d01f1 13699
90aeadfc 13700 child_die = sibling_die (child_die);
134d01f1
DJ
13701 }
13702
fa4028e9
JB
13703 /* Do not consider external references. According to the DWARF standard,
13704 these DIEs are identified by the fact that they have no byte_size
13705 attribute, and a declaration attribute. */
13706 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13707 || !die_is_declaration (die, cu))
c767944b 13708 new_symbol (die, type, cu);
134d01f1
DJ
13709}
13710
55426c9d
JB
13711/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13712 update TYPE using some information only available in DIE's children. */
13713
13714static void
13715update_enumeration_type_from_children (struct die_info *die,
13716 struct type *type,
13717 struct dwarf2_cu *cu)
13718{
13719 struct obstack obstack;
60f7655a 13720 struct die_info *child_die;
55426c9d
JB
13721 int unsigned_enum = 1;
13722 int flag_enum = 1;
13723 ULONGEST mask = 0;
13724 struct cleanup *old_chain;
13725
13726 obstack_init (&obstack);
13727 old_chain = make_cleanup_obstack_free (&obstack);
13728
60f7655a
DE
13729 for (child_die = die->child;
13730 child_die != NULL && child_die->tag;
13731 child_die = sibling_die (child_die))
55426c9d
JB
13732 {
13733 struct attribute *attr;
13734 LONGEST value;
13735 const gdb_byte *bytes;
13736 struct dwarf2_locexpr_baton *baton;
13737 const char *name;
60f7655a 13738
55426c9d
JB
13739 if (child_die->tag != DW_TAG_enumerator)
13740 continue;
13741
13742 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13743 if (attr == NULL)
13744 continue;
13745
13746 name = dwarf2_name (child_die, cu);
13747 if (name == NULL)
13748 name = "<anonymous enumerator>";
13749
13750 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13751 &value, &bytes, &baton);
13752 if (value < 0)
13753 {
13754 unsigned_enum = 0;
13755 flag_enum = 0;
13756 }
13757 else if ((mask & value) != 0)
13758 flag_enum = 0;
13759 else
13760 mask |= value;
13761
13762 /* If we already know that the enum type is neither unsigned, nor
13763 a flag type, no need to look at the rest of the enumerates. */
13764 if (!unsigned_enum && !flag_enum)
13765 break;
55426c9d
JB
13766 }
13767
13768 if (unsigned_enum)
13769 TYPE_UNSIGNED (type) = 1;
13770 if (flag_enum)
13771 TYPE_FLAG_ENUM (type) = 1;
13772
13773 do_cleanups (old_chain);
13774}
13775
134d01f1
DJ
13776/* Given a DW_AT_enumeration_type die, set its type. We do not
13777 complete the type's fields yet, or create any symbols. */
c906108c 13778
f792889a 13779static struct type *
134d01f1 13780read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13781{
e7c27a73 13782 struct objfile *objfile = cu->objfile;
c906108c 13783 struct type *type;
c906108c 13784 struct attribute *attr;
0114d602 13785 const char *name;
134d01f1 13786
348e048f
DE
13787 /* If the definition of this type lives in .debug_types, read that type.
13788 Don't follow DW_AT_specification though, that will take us back up
13789 the chain and we want to go down. */
45e58e77 13790 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13791 if (attr)
13792 {
ac9ec31b 13793 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13794
ac9ec31b 13795 /* The type's CU may not be the same as CU.
02142a6c 13796 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13797 return set_die_type (die, type, cu);
13798 }
13799
c906108c
SS
13800 type = alloc_type (objfile);
13801
13802 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13803 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13804 if (name != NULL)
7d455152 13805 TYPE_TAG_NAME (type) = name;
c906108c 13806
0626fc76
TT
13807 attr = dwarf2_attr (die, DW_AT_type, cu);
13808 if (attr != NULL)
13809 {
13810 struct type *underlying_type = die_type (die, cu);
13811
13812 TYPE_TARGET_TYPE (type) = underlying_type;
13813 }
13814
e142c38c 13815 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13816 if (attr)
13817 {
13818 TYPE_LENGTH (type) = DW_UNSND (attr);
13819 }
13820 else
13821 {
13822 TYPE_LENGTH (type) = 0;
13823 }
13824
137033e9
JB
13825 /* The enumeration DIE can be incomplete. In Ada, any type can be
13826 declared as private in the package spec, and then defined only
13827 inside the package body. Such types are known as Taft Amendment
13828 Types. When another package uses such a type, an incomplete DIE
13829 may be generated by the compiler. */
02eb380e 13830 if (die_is_declaration (die, cu))
876cecd0 13831 TYPE_STUB (type) = 1;
02eb380e 13832
0626fc76
TT
13833 /* Finish the creation of this type by using the enum's children.
13834 We must call this even when the underlying type has been provided
13835 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13836 update_enumeration_type_from_children (die, type, cu);
13837
0626fc76
TT
13838 /* If this type has an underlying type that is not a stub, then we
13839 may use its attributes. We always use the "unsigned" attribute
13840 in this situation, because ordinarily we guess whether the type
13841 is unsigned -- but the guess can be wrong and the underlying type
13842 can tell us the reality. However, we defer to a local size
13843 attribute if one exists, because this lets the compiler override
13844 the underlying type if needed. */
13845 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13846 {
13847 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13848 if (TYPE_LENGTH (type) == 0)
13849 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13850 }
13851
3d567982
TT
13852 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13853
f792889a 13854 return set_die_type (die, type, cu);
134d01f1
DJ
13855}
13856
13857/* Given a pointer to a die which begins an enumeration, process all
13858 the dies that define the members of the enumeration, and create the
13859 symbol for the enumeration type.
13860
13861 NOTE: We reverse the order of the element list. */
13862
13863static void
13864process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13865{
f792889a 13866 struct type *this_type;
134d01f1 13867
f792889a
DJ
13868 this_type = get_die_type (die, cu);
13869 if (this_type == NULL)
13870 this_type = read_enumeration_type (die, cu);
9dc481d3 13871
639d11d3 13872 if (die->child != NULL)
c906108c 13873 {
9dc481d3
DE
13874 struct die_info *child_die;
13875 struct symbol *sym;
13876 struct field *fields = NULL;
13877 int num_fields = 0;
15d034d0 13878 const char *name;
9dc481d3 13879
639d11d3 13880 child_die = die->child;
c906108c
SS
13881 while (child_die && child_die->tag)
13882 {
13883 if (child_die->tag != DW_TAG_enumerator)
13884 {
e7c27a73 13885 process_die (child_die, cu);
c906108c
SS
13886 }
13887 else
13888 {
39cbfefa
DJ
13889 name = dwarf2_name (child_die, cu);
13890 if (name)
c906108c 13891 {
f792889a 13892 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13893
13894 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13895 {
13896 fields = (struct field *)
13897 xrealloc (fields,
13898 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13899 * sizeof (struct field));
c906108c
SS
13900 }
13901
3567439c 13902 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13903 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13904 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13905 FIELD_BITSIZE (fields[num_fields]) = 0;
13906
13907 num_fields++;
13908 }
13909 }
13910
13911 child_die = sibling_die (child_die);
13912 }
13913
13914 if (num_fields)
13915 {
f792889a
DJ
13916 TYPE_NFIELDS (this_type) = num_fields;
13917 TYPE_FIELDS (this_type) = (struct field *)
13918 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13919 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13920 sizeof (struct field) * num_fields);
b8c9b27d 13921 xfree (fields);
c906108c 13922 }
c906108c 13923 }
134d01f1 13924
6c83ed52
TT
13925 /* If we are reading an enum from a .debug_types unit, and the enum
13926 is a declaration, and the enum is not the signatured type in the
13927 unit, then we do not want to add a symbol for it. Adding a
13928 symbol would in some cases obscure the true definition of the
13929 enum, giving users an incomplete type when the definition is
13930 actually available. Note that we do not want to do this for all
13931 enums which are just declarations, because C++0x allows forward
13932 enum declarations. */
3019eac3 13933 if (cu->per_cu->is_debug_types
6c83ed52
TT
13934 && die_is_declaration (die, cu))
13935 {
52dc124a 13936 struct signatured_type *sig_type;
6c83ed52 13937
c0f78cd4 13938 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
13939 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13940 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
13941 return;
13942 }
13943
f792889a 13944 new_symbol (die, this_type, cu);
c906108c
SS
13945}
13946
13947/* Extract all information from a DW_TAG_array_type DIE and put it in
13948 the DIE's type field. For now, this only handles one dimensional
13949 arrays. */
13950
f792889a 13951static struct type *
e7c27a73 13952read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13953{
e7c27a73 13954 struct objfile *objfile = cu->objfile;
c906108c 13955 struct die_info *child_die;
7e314c57 13956 struct type *type;
c906108c
SS
13957 struct type *element_type, *range_type, *index_type;
13958 struct type **range_types = NULL;
13959 struct attribute *attr;
13960 int ndim = 0;
13961 struct cleanup *back_to;
15d034d0 13962 const char *name;
dc53a7ad 13963 unsigned int bit_stride = 0;
c906108c 13964
e7c27a73 13965 element_type = die_type (die, cu);
c906108c 13966
7e314c57
JK
13967 /* The die_type call above may have already set the type for this DIE. */
13968 type = get_die_type (die, cu);
13969 if (type)
13970 return type;
13971
dc53a7ad
JB
13972 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13973 if (attr != NULL)
13974 bit_stride = DW_UNSND (attr) * 8;
13975
13976 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13977 if (attr != NULL)
13978 bit_stride = DW_UNSND (attr);
13979
c906108c
SS
13980 /* Irix 6.2 native cc creates array types without children for
13981 arrays with unspecified length. */
639d11d3 13982 if (die->child == NULL)
c906108c 13983 {
46bf5051 13984 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 13985 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
13986 type = create_array_type_with_stride (NULL, element_type, range_type,
13987 bit_stride);
f792889a 13988 return set_die_type (die, type, cu);
c906108c
SS
13989 }
13990
13991 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 13992 child_die = die->child;
c906108c
SS
13993 while (child_die && child_die->tag)
13994 {
13995 if (child_die->tag == DW_TAG_subrange_type)
13996 {
f792889a 13997 struct type *child_type = read_type_die (child_die, cu);
9a619af0 13998
f792889a 13999 if (child_type != NULL)
a02abb62 14000 {
0963b4bd
MS
14001 /* The range type was succesfully read. Save it for the
14002 array type creation. */
a02abb62
JB
14003 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14004 {
14005 range_types = (struct type **)
14006 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14007 * sizeof (struct type *));
14008 if (ndim == 0)
14009 make_cleanup (free_current_contents, &range_types);
14010 }
f792889a 14011 range_types[ndim++] = child_type;
a02abb62 14012 }
c906108c
SS
14013 }
14014 child_die = sibling_die (child_die);
14015 }
14016
14017 /* Dwarf2 dimensions are output from left to right, create the
14018 necessary array types in backwards order. */
7ca2d3a3 14019
c906108c 14020 type = element_type;
7ca2d3a3
DL
14021
14022 if (read_array_order (die, cu) == DW_ORD_col_major)
14023 {
14024 int i = 0;
9a619af0 14025
7ca2d3a3 14026 while (i < ndim)
dc53a7ad
JB
14027 type = create_array_type_with_stride (NULL, type, range_types[i++],
14028 bit_stride);
7ca2d3a3
DL
14029 }
14030 else
14031 {
14032 while (ndim-- > 0)
dc53a7ad
JB
14033 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14034 bit_stride);
7ca2d3a3 14035 }
c906108c 14036
f5f8a009
EZ
14037 /* Understand Dwarf2 support for vector types (like they occur on
14038 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14039 array type. This is not part of the Dwarf2/3 standard yet, but a
14040 custom vendor extension. The main difference between a regular
14041 array and the vector variant is that vectors are passed by value
14042 to functions. */
e142c38c 14043 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14044 if (attr)
ea37ba09 14045 make_vector_type (type);
f5f8a009 14046
dbc98a8b
KW
14047 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14048 implementation may choose to implement triple vectors using this
14049 attribute. */
14050 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14051 if (attr)
14052 {
14053 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14054 TYPE_LENGTH (type) = DW_UNSND (attr);
14055 else
3e43a32a
MS
14056 complaint (&symfile_complaints,
14057 _("DW_AT_byte_size for array type smaller "
14058 "than the total size of elements"));
dbc98a8b
KW
14059 }
14060
39cbfefa
DJ
14061 name = dwarf2_name (die, cu);
14062 if (name)
14063 TYPE_NAME (type) = name;
6e70227d 14064
0963b4bd 14065 /* Install the type in the die. */
7e314c57
JK
14066 set_die_type (die, type, cu);
14067
14068 /* set_die_type should be already done. */
b4ba55a1
JB
14069 set_descriptive_type (type, die, cu);
14070
c906108c
SS
14071 do_cleanups (back_to);
14072
7e314c57 14073 return type;
c906108c
SS
14074}
14075
7ca2d3a3 14076static enum dwarf_array_dim_ordering
6e70227d 14077read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14078{
14079 struct attribute *attr;
14080
14081 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14082
aead7601
SM
14083 if (attr)
14084 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14085
0963b4bd
MS
14086 /* GNU F77 is a special case, as at 08/2004 array type info is the
14087 opposite order to the dwarf2 specification, but data is still
14088 laid out as per normal fortran.
7ca2d3a3 14089
0963b4bd
MS
14090 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14091 version checking. */
7ca2d3a3 14092
905e0470
PM
14093 if (cu->language == language_fortran
14094 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14095 {
14096 return DW_ORD_row_major;
14097 }
14098
6e70227d 14099 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14100 {
14101 case array_column_major:
14102 return DW_ORD_col_major;
14103 case array_row_major:
14104 default:
14105 return DW_ORD_row_major;
14106 };
14107}
14108
72019c9c 14109/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14110 the DIE's type field. */
72019c9c 14111
f792889a 14112static struct type *
72019c9c
GM
14113read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14114{
7e314c57
JK
14115 struct type *domain_type, *set_type;
14116 struct attribute *attr;
f792889a 14117
7e314c57
JK
14118 domain_type = die_type (die, cu);
14119
14120 /* The die_type call above may have already set the type for this DIE. */
14121 set_type = get_die_type (die, cu);
14122 if (set_type)
14123 return set_type;
14124
14125 set_type = create_set_type (NULL, domain_type);
14126
14127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14128 if (attr)
14129 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14130
f792889a 14131 return set_die_type (die, set_type, cu);
72019c9c 14132}
7ca2d3a3 14133
0971de02
TT
14134/* A helper for read_common_block that creates a locexpr baton.
14135 SYM is the symbol which we are marking as computed.
14136 COMMON_DIE is the DIE for the common block.
14137 COMMON_LOC is the location expression attribute for the common
14138 block itself.
14139 MEMBER_LOC is the location expression attribute for the particular
14140 member of the common block that we are processing.
14141 CU is the CU from which the above come. */
14142
14143static void
14144mark_common_block_symbol_computed (struct symbol *sym,
14145 struct die_info *common_die,
14146 struct attribute *common_loc,
14147 struct attribute *member_loc,
14148 struct dwarf2_cu *cu)
14149{
14150 struct objfile *objfile = dwarf2_per_objfile->objfile;
14151 struct dwarf2_locexpr_baton *baton;
14152 gdb_byte *ptr;
14153 unsigned int cu_off;
14154 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14155 LONGEST offset = 0;
14156
14157 gdb_assert (common_loc && member_loc);
14158 gdb_assert (attr_form_is_block (common_loc));
14159 gdb_assert (attr_form_is_block (member_loc)
14160 || attr_form_is_constant (member_loc));
14161
8d749320 14162 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14163 baton->per_cu = cu->per_cu;
14164 gdb_assert (baton->per_cu);
14165
14166 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14167
14168 if (attr_form_is_constant (member_loc))
14169 {
14170 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14171 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14172 }
14173 else
14174 baton->size += DW_BLOCK (member_loc)->size;
14175
224c3ddb 14176 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14177 baton->data = ptr;
14178
14179 *ptr++ = DW_OP_call4;
14180 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14181 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14182 ptr += 4;
14183
14184 if (attr_form_is_constant (member_loc))
14185 {
14186 *ptr++ = DW_OP_addr;
14187 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14188 ptr += cu->header.addr_size;
14189 }
14190 else
14191 {
14192 /* We have to copy the data here, because DW_OP_call4 will only
14193 use a DW_AT_location attribute. */
14194 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14195 ptr += DW_BLOCK (member_loc)->size;
14196 }
14197
14198 *ptr++ = DW_OP_plus;
14199 gdb_assert (ptr - baton->data == baton->size);
14200
0971de02 14201 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14202 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14203}
14204
4357ac6c
TT
14205/* Create appropriate locally-scoped variables for all the
14206 DW_TAG_common_block entries. Also create a struct common_block
14207 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14208 is used to sepate the common blocks name namespace from regular
14209 variable names. */
c906108c
SS
14210
14211static void
e7c27a73 14212read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14213{
0971de02
TT
14214 struct attribute *attr;
14215
14216 attr = dwarf2_attr (die, DW_AT_location, cu);
14217 if (attr)
14218 {
14219 /* Support the .debug_loc offsets. */
14220 if (attr_form_is_block (attr))
14221 {
14222 /* Ok. */
14223 }
14224 else if (attr_form_is_section_offset (attr))
14225 {
14226 dwarf2_complex_location_expr_complaint ();
14227 attr = NULL;
14228 }
14229 else
14230 {
14231 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14232 "common block member");
14233 attr = NULL;
14234 }
14235 }
14236
639d11d3 14237 if (die->child != NULL)
c906108c 14238 {
4357ac6c
TT
14239 struct objfile *objfile = cu->objfile;
14240 struct die_info *child_die;
14241 size_t n_entries = 0, size;
14242 struct common_block *common_block;
14243 struct symbol *sym;
74ac6d43 14244
4357ac6c
TT
14245 for (child_die = die->child;
14246 child_die && child_die->tag;
14247 child_die = sibling_die (child_die))
14248 ++n_entries;
14249
14250 size = (sizeof (struct common_block)
14251 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14252 common_block
14253 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14254 size);
4357ac6c
TT
14255 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14256 common_block->n_entries = 0;
14257
14258 for (child_die = die->child;
14259 child_die && child_die->tag;
14260 child_die = sibling_die (child_die))
14261 {
14262 /* Create the symbol in the DW_TAG_common_block block in the current
14263 symbol scope. */
e7c27a73 14264 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14265 if (sym != NULL)
14266 {
14267 struct attribute *member_loc;
14268
14269 common_block->contents[common_block->n_entries++] = sym;
14270
14271 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14272 cu);
14273 if (member_loc)
14274 {
14275 /* GDB has handled this for a long time, but it is
14276 not specified by DWARF. It seems to have been
14277 emitted by gfortran at least as recently as:
14278 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14279 complaint (&symfile_complaints,
14280 _("Variable in common block has "
14281 "DW_AT_data_member_location "
14282 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
14283 child_die->offset.sect_off,
14284 objfile_name (cu->objfile));
0971de02
TT
14285
14286 if (attr_form_is_section_offset (member_loc))
14287 dwarf2_complex_location_expr_complaint ();
14288 else if (attr_form_is_constant (member_loc)
14289 || attr_form_is_block (member_loc))
14290 {
14291 if (attr)
14292 mark_common_block_symbol_computed (sym, die, attr,
14293 member_loc, cu);
14294 }
14295 else
14296 dwarf2_complex_location_expr_complaint ();
14297 }
14298 }
c906108c 14299 }
4357ac6c
TT
14300
14301 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14302 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14303 }
14304}
14305
0114d602 14306/* Create a type for a C++ namespace. */
d9fa45fe 14307
0114d602
DJ
14308static struct type *
14309read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14310{
e7c27a73 14311 struct objfile *objfile = cu->objfile;
0114d602 14312 const char *previous_prefix, *name;
9219021c 14313 int is_anonymous;
0114d602
DJ
14314 struct type *type;
14315
14316 /* For extensions, reuse the type of the original namespace. */
14317 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14318 {
14319 struct die_info *ext_die;
14320 struct dwarf2_cu *ext_cu = cu;
9a619af0 14321
0114d602
DJ
14322 ext_die = dwarf2_extension (die, &ext_cu);
14323 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14324
14325 /* EXT_CU may not be the same as CU.
02142a6c 14326 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14327 return set_die_type (die, type, cu);
14328 }
9219021c 14329
e142c38c 14330 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14331
14332 /* Now build the name of the current namespace. */
14333
0114d602
DJ
14334 previous_prefix = determine_prefix (die, cu);
14335 if (previous_prefix[0] != '\0')
14336 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14337 previous_prefix, name, 0, cu);
0114d602
DJ
14338
14339 /* Create the type. */
19f392bc 14340 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14341 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14342
60531b24 14343 return set_die_type (die, type, cu);
0114d602
DJ
14344}
14345
22cee43f 14346/* Read a namespace scope. */
0114d602
DJ
14347
14348static void
14349read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14350{
14351 struct objfile *objfile = cu->objfile;
0114d602 14352 int is_anonymous;
9219021c 14353
5c4e30ca
DC
14354 /* Add a symbol associated to this if we haven't seen the namespace
14355 before. Also, add a using directive if it's an anonymous
14356 namespace. */
9219021c 14357
f2f0e013 14358 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14359 {
14360 struct type *type;
14361
0114d602 14362 type = read_type_die (die, cu);
e7c27a73 14363 new_symbol (die, type, cu);
5c4e30ca 14364
e8e80198 14365 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14366 if (is_anonymous)
0114d602
DJ
14367 {
14368 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14369
22cee43f
PMR
14370 add_using_directive (using_directives (cu->language),
14371 previous_prefix, TYPE_NAME (type), NULL,
14372 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14373 }
5c4e30ca 14374 }
9219021c 14375
639d11d3 14376 if (die->child != NULL)
d9fa45fe 14377 {
639d11d3 14378 struct die_info *child_die = die->child;
6e70227d 14379
d9fa45fe
DC
14380 while (child_die && child_die->tag)
14381 {
e7c27a73 14382 process_die (child_die, cu);
d9fa45fe
DC
14383 child_die = sibling_die (child_die);
14384 }
14385 }
38d518c9
EZ
14386}
14387
f55ee35c
JK
14388/* Read a Fortran module as type. This DIE can be only a declaration used for
14389 imported module. Still we need that type as local Fortran "use ... only"
14390 declaration imports depend on the created type in determine_prefix. */
14391
14392static struct type *
14393read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14394{
14395 struct objfile *objfile = cu->objfile;
15d034d0 14396 const char *module_name;
f55ee35c
JK
14397 struct type *type;
14398
14399 module_name = dwarf2_name (die, cu);
14400 if (!module_name)
3e43a32a
MS
14401 complaint (&symfile_complaints,
14402 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 14403 die->offset.sect_off);
19f392bc 14404 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14405
14406 /* determine_prefix uses TYPE_TAG_NAME. */
14407 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14408
14409 return set_die_type (die, type, cu);
14410}
14411
5d7cb8df
JK
14412/* Read a Fortran module. */
14413
14414static void
14415read_module (struct die_info *die, struct dwarf2_cu *cu)
14416{
14417 struct die_info *child_die = die->child;
530e8392
KB
14418 struct type *type;
14419
14420 type = read_type_die (die, cu);
14421 new_symbol (die, type, cu);
5d7cb8df 14422
5d7cb8df
JK
14423 while (child_die && child_die->tag)
14424 {
14425 process_die (child_die, cu);
14426 child_die = sibling_die (child_die);
14427 }
14428}
14429
38d518c9
EZ
14430/* Return the name of the namespace represented by DIE. Set
14431 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14432 namespace. */
14433
14434static const char *
e142c38c 14435namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14436{
14437 struct die_info *current_die;
14438 const char *name = NULL;
14439
14440 /* Loop through the extensions until we find a name. */
14441
14442 for (current_die = die;
14443 current_die != NULL;
f2f0e013 14444 current_die = dwarf2_extension (die, &cu))
38d518c9 14445 {
96553a0c
DE
14446 /* We don't use dwarf2_name here so that we can detect the absence
14447 of a name -> anonymous namespace. */
7d45c7c3 14448 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14449
38d518c9
EZ
14450 if (name != NULL)
14451 break;
14452 }
14453
14454 /* Is it an anonymous namespace? */
14455
14456 *is_anonymous = (name == NULL);
14457 if (*is_anonymous)
2b1dbab0 14458 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14459
14460 return name;
d9fa45fe
DC
14461}
14462
c906108c
SS
14463/* Extract all information from a DW_TAG_pointer_type DIE and add to
14464 the user defined type vector. */
14465
f792889a 14466static struct type *
e7c27a73 14467read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14468{
5e2b427d 14469 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14470 struct comp_unit_head *cu_header = &cu->header;
c906108c 14471 struct type *type;
8b2dbe47
KB
14472 struct attribute *attr_byte_size;
14473 struct attribute *attr_address_class;
14474 int byte_size, addr_class;
7e314c57
JK
14475 struct type *target_type;
14476
14477 target_type = die_type (die, cu);
c906108c 14478
7e314c57
JK
14479 /* The die_type call above may have already set the type for this DIE. */
14480 type = get_die_type (die, cu);
14481 if (type)
14482 return type;
14483
14484 type = lookup_pointer_type (target_type);
8b2dbe47 14485
e142c38c 14486 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14487 if (attr_byte_size)
14488 byte_size = DW_UNSND (attr_byte_size);
c906108c 14489 else
8b2dbe47
KB
14490 byte_size = cu_header->addr_size;
14491
e142c38c 14492 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14493 if (attr_address_class)
14494 addr_class = DW_UNSND (attr_address_class);
14495 else
14496 addr_class = DW_ADDR_none;
14497
14498 /* If the pointer size or address class is different than the
14499 default, create a type variant marked as such and set the
14500 length accordingly. */
14501 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14502 {
5e2b427d 14503 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14504 {
14505 int type_flags;
14506
849957d9 14507 type_flags = gdbarch_address_class_type_flags
5e2b427d 14508 (gdbarch, byte_size, addr_class);
876cecd0
TT
14509 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14510 == 0);
8b2dbe47
KB
14511 type = make_type_with_address_space (type, type_flags);
14512 }
14513 else if (TYPE_LENGTH (type) != byte_size)
14514 {
3e43a32a
MS
14515 complaint (&symfile_complaints,
14516 _("invalid pointer size %d"), byte_size);
8b2dbe47 14517 }
6e70227d 14518 else
9a619af0
MS
14519 {
14520 /* Should we also complain about unhandled address classes? */
14521 }
c906108c 14522 }
8b2dbe47
KB
14523
14524 TYPE_LENGTH (type) = byte_size;
f792889a 14525 return set_die_type (die, type, cu);
c906108c
SS
14526}
14527
14528/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14529 the user defined type vector. */
14530
f792889a 14531static struct type *
e7c27a73 14532read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14533{
14534 struct type *type;
14535 struct type *to_type;
14536 struct type *domain;
14537
e7c27a73
DJ
14538 to_type = die_type (die, cu);
14539 domain = die_containing_type (die, cu);
0d5de010 14540
7e314c57
JK
14541 /* The calls above may have already set the type for this DIE. */
14542 type = get_die_type (die, cu);
14543 if (type)
14544 return type;
14545
0d5de010
DJ
14546 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14547 type = lookup_methodptr_type (to_type);
7078baeb
TT
14548 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14549 {
14550 struct type *new_type = alloc_type (cu->objfile);
14551
14552 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14553 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14554 TYPE_VARARGS (to_type));
14555 type = lookup_methodptr_type (new_type);
14556 }
0d5de010
DJ
14557 else
14558 type = lookup_memberptr_type (to_type, domain);
c906108c 14559
f792889a 14560 return set_die_type (die, type, cu);
c906108c
SS
14561}
14562
14563/* Extract all information from a DW_TAG_reference_type DIE and add to
14564 the user defined type vector. */
14565
f792889a 14566static struct type *
e7c27a73 14567read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14568{
e7c27a73 14569 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14570 struct type *type, *target_type;
c906108c
SS
14571 struct attribute *attr;
14572
7e314c57
JK
14573 target_type = die_type (die, cu);
14574
14575 /* The die_type call above may have already set the type for this DIE. */
14576 type = get_die_type (die, cu);
14577 if (type)
14578 return type;
14579
14580 type = lookup_reference_type (target_type);
e142c38c 14581 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14582 if (attr)
14583 {
14584 TYPE_LENGTH (type) = DW_UNSND (attr);
14585 }
14586 else
14587 {
107d2387 14588 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14589 }
f792889a 14590 return set_die_type (die, type, cu);
c906108c
SS
14591}
14592
cf363f18
MW
14593/* Add the given cv-qualifiers to the element type of the array. GCC
14594 outputs DWARF type qualifiers that apply to an array, not the
14595 element type. But GDB relies on the array element type to carry
14596 the cv-qualifiers. This mimics section 6.7.3 of the C99
14597 specification. */
14598
14599static struct type *
14600add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14601 struct type *base_type, int cnst, int voltl)
14602{
14603 struct type *el_type, *inner_array;
14604
14605 base_type = copy_type (base_type);
14606 inner_array = base_type;
14607
14608 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14609 {
14610 TYPE_TARGET_TYPE (inner_array) =
14611 copy_type (TYPE_TARGET_TYPE (inner_array));
14612 inner_array = TYPE_TARGET_TYPE (inner_array);
14613 }
14614
14615 el_type = TYPE_TARGET_TYPE (inner_array);
14616 cnst |= TYPE_CONST (el_type);
14617 voltl |= TYPE_VOLATILE (el_type);
14618 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14619
14620 return set_die_type (die, base_type, cu);
14621}
14622
f792889a 14623static struct type *
e7c27a73 14624read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14625{
f792889a 14626 struct type *base_type, *cv_type;
c906108c 14627
e7c27a73 14628 base_type = die_type (die, cu);
7e314c57
JK
14629
14630 /* The die_type call above may have already set the type for this DIE. */
14631 cv_type = get_die_type (die, cu);
14632 if (cv_type)
14633 return cv_type;
14634
2f608a3a
KW
14635 /* In case the const qualifier is applied to an array type, the element type
14636 is so qualified, not the array type (section 6.7.3 of C99). */
14637 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14638 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14639
f792889a
DJ
14640 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14641 return set_die_type (die, cv_type, cu);
c906108c
SS
14642}
14643
f792889a 14644static struct type *
e7c27a73 14645read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14646{
f792889a 14647 struct type *base_type, *cv_type;
c906108c 14648
e7c27a73 14649 base_type = die_type (die, cu);
7e314c57
JK
14650
14651 /* The die_type call above may have already set the type for this DIE. */
14652 cv_type = get_die_type (die, cu);
14653 if (cv_type)
14654 return cv_type;
14655
cf363f18
MW
14656 /* In case the volatile qualifier is applied to an array type, the
14657 element type is so qualified, not the array type (section 6.7.3
14658 of C99). */
14659 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14660 return add_array_cv_type (die, cu, base_type, 0, 1);
14661
f792889a
DJ
14662 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14663 return set_die_type (die, cv_type, cu);
c906108c
SS
14664}
14665
06d66ee9
TT
14666/* Handle DW_TAG_restrict_type. */
14667
14668static struct type *
14669read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14670{
14671 struct type *base_type, *cv_type;
14672
14673 base_type = die_type (die, cu);
14674
14675 /* The die_type call above may have already set the type for this DIE. */
14676 cv_type = get_die_type (die, cu);
14677 if (cv_type)
14678 return cv_type;
14679
14680 cv_type = make_restrict_type (base_type);
14681 return set_die_type (die, cv_type, cu);
14682}
14683
a2c2acaf
MW
14684/* Handle DW_TAG_atomic_type. */
14685
14686static struct type *
14687read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14688{
14689 struct type *base_type, *cv_type;
14690
14691 base_type = die_type (die, cu);
14692
14693 /* The die_type call above may have already set the type for this DIE. */
14694 cv_type = get_die_type (die, cu);
14695 if (cv_type)
14696 return cv_type;
14697
14698 cv_type = make_atomic_type (base_type);
14699 return set_die_type (die, cv_type, cu);
14700}
14701
c906108c
SS
14702/* Extract all information from a DW_TAG_string_type DIE and add to
14703 the user defined type vector. It isn't really a user defined type,
14704 but it behaves like one, with other DIE's using an AT_user_def_type
14705 attribute to reference it. */
14706
f792889a 14707static struct type *
e7c27a73 14708read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14709{
e7c27a73 14710 struct objfile *objfile = cu->objfile;
3b7538c0 14711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14712 struct type *type, *range_type, *index_type, *char_type;
14713 struct attribute *attr;
14714 unsigned int length;
14715
e142c38c 14716 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14717 if (attr)
14718 {
14719 length = DW_UNSND (attr);
14720 }
14721 else
14722 {
0963b4bd 14723 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14724 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14725 if (attr)
14726 {
14727 length = DW_UNSND (attr);
14728 }
14729 else
14730 {
14731 length = 1;
14732 }
c906108c 14733 }
6ccb9162 14734
46bf5051 14735 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14736 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14737 char_type = language_string_char_type (cu->language_defn, gdbarch);
14738 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14739
f792889a 14740 return set_die_type (die, type, cu);
c906108c
SS
14741}
14742
4d804846
JB
14743/* Assuming that DIE corresponds to a function, returns nonzero
14744 if the function is prototyped. */
14745
14746static int
14747prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14748{
14749 struct attribute *attr;
14750
14751 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14752 if (attr && (DW_UNSND (attr) != 0))
14753 return 1;
14754
14755 /* The DWARF standard implies that the DW_AT_prototyped attribute
14756 is only meaninful for C, but the concept also extends to other
14757 languages that allow unprototyped functions (Eg: Objective C).
14758 For all other languages, assume that functions are always
14759 prototyped. */
14760 if (cu->language != language_c
14761 && cu->language != language_objc
14762 && cu->language != language_opencl)
14763 return 1;
14764
14765 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14766 prototyped and unprototyped functions; default to prototyped,
14767 since that is more common in modern code (and RealView warns
14768 about unprototyped functions). */
14769 if (producer_is_realview (cu->producer))
14770 return 1;
14771
14772 return 0;
14773}
14774
c906108c
SS
14775/* Handle DIES due to C code like:
14776
14777 struct foo
c5aa993b
JM
14778 {
14779 int (*funcp)(int a, long l);
14780 int b;
14781 };
c906108c 14782
0963b4bd 14783 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14784
f792889a 14785static struct type *
e7c27a73 14786read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14787{
bb5ed363 14788 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14789 struct type *type; /* Type that this function returns. */
14790 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14791 struct attribute *attr;
14792
e7c27a73 14793 type = die_type (die, cu);
7e314c57
JK
14794
14795 /* The die_type call above may have already set the type for this DIE. */
14796 ftype = get_die_type (die, cu);
14797 if (ftype)
14798 return ftype;
14799
0c8b41f1 14800 ftype = lookup_function_type (type);
c906108c 14801
4d804846 14802 if (prototyped_function_p (die, cu))
a6c727b2 14803 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14804
c055b101
CV
14805 /* Store the calling convention in the type if it's available in
14806 the subroutine die. Otherwise set the calling convention to
14807 the default value DW_CC_normal. */
14808 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14809 if (attr)
14810 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14811 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14812 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14813 else
14814 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14815
743649fd
MW
14816 /* Record whether the function returns normally to its caller or not
14817 if the DWARF producer set that information. */
14818 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14819 if (attr && (DW_UNSND (attr) != 0))
14820 TYPE_NO_RETURN (ftype) = 1;
14821
76c10ea2
GM
14822 /* We need to add the subroutine type to the die immediately so
14823 we don't infinitely recurse when dealing with parameters
0963b4bd 14824 declared as the same subroutine type. */
76c10ea2 14825 set_die_type (die, ftype, cu);
6e70227d 14826
639d11d3 14827 if (die->child != NULL)
c906108c 14828 {
bb5ed363 14829 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14830 struct die_info *child_die;
8072405b 14831 int nparams, iparams;
c906108c
SS
14832
14833 /* Count the number of parameters.
14834 FIXME: GDB currently ignores vararg functions, but knows about
14835 vararg member functions. */
8072405b 14836 nparams = 0;
639d11d3 14837 child_die = die->child;
c906108c
SS
14838 while (child_die && child_die->tag)
14839 {
14840 if (child_die->tag == DW_TAG_formal_parameter)
14841 nparams++;
14842 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14843 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14844 child_die = sibling_die (child_die);
14845 }
14846
14847 /* Allocate storage for parameters and fill them in. */
14848 TYPE_NFIELDS (ftype) = nparams;
14849 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14850 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14851
8072405b
JK
14852 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14853 even if we error out during the parameters reading below. */
14854 for (iparams = 0; iparams < nparams; iparams++)
14855 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14856
14857 iparams = 0;
639d11d3 14858 child_die = die->child;
c906108c
SS
14859 while (child_die && child_die->tag)
14860 {
14861 if (child_die->tag == DW_TAG_formal_parameter)
14862 {
3ce3b1ba
PA
14863 struct type *arg_type;
14864
14865 /* DWARF version 2 has no clean way to discern C++
14866 static and non-static member functions. G++ helps
14867 GDB by marking the first parameter for non-static
14868 member functions (which is the this pointer) as
14869 artificial. We pass this information to
14870 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14871
14872 DWARF version 3 added DW_AT_object_pointer, which GCC
14873 4.5 does not yet generate. */
e142c38c 14874 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14875 if (attr)
14876 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14877 else
9c37b5ae 14878 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
14879 arg_type = die_type (child_die, cu);
14880
14881 /* RealView does not mark THIS as const, which the testsuite
14882 expects. GCC marks THIS as const in method definitions,
14883 but not in the class specifications (GCC PR 43053). */
14884 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14885 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14886 {
14887 int is_this = 0;
14888 struct dwarf2_cu *arg_cu = cu;
14889 const char *name = dwarf2_name (child_die, cu);
14890
14891 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14892 if (attr)
14893 {
14894 /* If the compiler emits this, use it. */
14895 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14896 is_this = 1;
14897 }
14898 else if (name && strcmp (name, "this") == 0)
14899 /* Function definitions will have the argument names. */
14900 is_this = 1;
14901 else if (name == NULL && iparams == 0)
14902 /* Declarations may not have the names, so like
14903 elsewhere in GDB, assume an artificial first
14904 argument is "this". */
14905 is_this = 1;
14906
14907 if (is_this)
14908 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14909 arg_type, 0);
14910 }
14911
14912 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14913 iparams++;
14914 }
14915 child_die = sibling_die (child_die);
14916 }
14917 }
14918
76c10ea2 14919 return ftype;
c906108c
SS
14920}
14921
f792889a 14922static struct type *
e7c27a73 14923read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14924{
e7c27a73 14925 struct objfile *objfile = cu->objfile;
0114d602 14926 const char *name = NULL;
3c8e0968 14927 struct type *this_type, *target_type;
c906108c 14928
94af9270 14929 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
14930 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14931 TYPE_TARGET_STUB (this_type) = 1;
f792889a 14932 set_die_type (die, this_type, cu);
3c8e0968
DE
14933 target_type = die_type (die, cu);
14934 if (target_type != this_type)
14935 TYPE_TARGET_TYPE (this_type) = target_type;
14936 else
14937 {
14938 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14939 spec and cause infinite loops in GDB. */
14940 complaint (&symfile_complaints,
14941 _("Self-referential DW_TAG_typedef "
14942 "- DIE at 0x%x [in module %s]"),
4262abfb 14943 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
14944 TYPE_TARGET_TYPE (this_type) = NULL;
14945 }
f792889a 14946 return this_type;
c906108c
SS
14947}
14948
9b790ce7
UW
14949/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14950 (which may be different from NAME) to the architecture back-end to allow
14951 it to guess the correct format if necessary. */
14952
14953static struct type *
14954dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14955 const char *name_hint)
14956{
14957 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14958 const struct floatformat **format;
14959 struct type *type;
14960
14961 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14962 if (format)
14963 type = init_float_type (objfile, bits, name, format);
14964 else
14965 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14966
14967 return type;
14968}
14969
c906108c
SS
14970/* Find a representation of a given base type and install
14971 it in the TYPE field of the die. */
14972
f792889a 14973static struct type *
e7c27a73 14974read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14975{
e7c27a73 14976 struct objfile *objfile = cu->objfile;
c906108c
SS
14977 struct type *type;
14978 struct attribute *attr;
19f392bc 14979 int encoding = 0, bits = 0;
15d034d0 14980 const char *name;
c906108c 14981
e142c38c 14982 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
14983 if (attr)
14984 {
14985 encoding = DW_UNSND (attr);
14986 }
e142c38c 14987 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14988 if (attr)
14989 {
19f392bc 14990 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 14991 }
39cbfefa 14992 name = dwarf2_name (die, cu);
6ccb9162 14993 if (!name)
c906108c 14994 {
6ccb9162
UW
14995 complaint (&symfile_complaints,
14996 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 14997 }
6ccb9162
UW
14998
14999 switch (encoding)
c906108c 15000 {
6ccb9162
UW
15001 case DW_ATE_address:
15002 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15003 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15004 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15005 break;
15006 case DW_ATE_boolean:
19f392bc 15007 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15008 break;
15009 case DW_ATE_complex_float:
9b790ce7 15010 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15011 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15012 break;
15013 case DW_ATE_decimal_float:
19f392bc 15014 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15015 break;
15016 case DW_ATE_float:
9b790ce7 15017 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15018 break;
15019 case DW_ATE_signed:
19f392bc 15020 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15021 break;
15022 case DW_ATE_unsigned:
3b2b8fea
TT
15023 if (cu->language == language_fortran
15024 && name
61012eef 15025 && startswith (name, "character("))
19f392bc
UW
15026 type = init_character_type (objfile, bits, 1, name);
15027 else
15028 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15029 break;
15030 case DW_ATE_signed_char:
6e70227d 15031 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15032 || cu->language == language_pascal
15033 || cu->language == language_fortran)
19f392bc
UW
15034 type = init_character_type (objfile, bits, 0, name);
15035 else
15036 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15037 break;
15038 case DW_ATE_unsigned_char:
868a0084 15039 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15040 || cu->language == language_pascal
c44af4eb
TT
15041 || cu->language == language_fortran
15042 || cu->language == language_rust)
19f392bc
UW
15043 type = init_character_type (objfile, bits, 1, name);
15044 else
15045 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15046 break;
75079b2b
TT
15047 case DW_ATE_UTF:
15048 /* We just treat this as an integer and then recognize the
15049 type by name elsewhere. */
19f392bc 15050 type = init_integer_type (objfile, bits, 0, name);
75079b2b
TT
15051 break;
15052
6ccb9162
UW
15053 default:
15054 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15055 dwarf_type_encoding_name (encoding));
19f392bc
UW
15056 type = init_type (objfile, TYPE_CODE_ERROR,
15057 bits / TARGET_CHAR_BIT, name);
6ccb9162 15058 break;
c906108c 15059 }
6ccb9162 15060
0114d602 15061 if (name && strcmp (name, "char") == 0)
876cecd0 15062 TYPE_NOSIGN (type) = 1;
0114d602 15063
f792889a 15064 return set_die_type (die, type, cu);
c906108c
SS
15065}
15066
80180f79
SA
15067/* Parse dwarf attribute if it's a block, reference or constant and put the
15068 resulting value of the attribute into struct bound_prop.
15069 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15070
15071static int
15072attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15073 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15074{
15075 struct dwarf2_property_baton *baton;
15076 struct obstack *obstack = &cu->objfile->objfile_obstack;
15077
15078 if (attr == NULL || prop == NULL)
15079 return 0;
15080
15081 if (attr_form_is_block (attr))
15082 {
8d749320 15083 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15084 baton->referenced_type = NULL;
15085 baton->locexpr.per_cu = cu->per_cu;
15086 baton->locexpr.size = DW_BLOCK (attr)->size;
15087 baton->locexpr.data = DW_BLOCK (attr)->data;
15088 prop->data.baton = baton;
15089 prop->kind = PROP_LOCEXPR;
15090 gdb_assert (prop->data.baton != NULL);
15091 }
15092 else if (attr_form_is_ref (attr))
15093 {
15094 struct dwarf2_cu *target_cu = cu;
15095 struct die_info *target_die;
15096 struct attribute *target_attr;
15097
15098 target_die = follow_die_ref (die, attr, &target_cu);
15099 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15100 if (target_attr == NULL)
15101 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15102 target_cu);
80180f79
SA
15103 if (target_attr == NULL)
15104 return 0;
15105
df25ebbd 15106 switch (target_attr->name)
80180f79 15107 {
df25ebbd
JB
15108 case DW_AT_location:
15109 if (attr_form_is_section_offset (target_attr))
15110 {
8d749320 15111 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15112 baton->referenced_type = die_type (target_die, target_cu);
15113 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15114 prop->data.baton = baton;
15115 prop->kind = PROP_LOCLIST;
15116 gdb_assert (prop->data.baton != NULL);
15117 }
15118 else if (attr_form_is_block (target_attr))
15119 {
8d749320 15120 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15121 baton->referenced_type = die_type (target_die, target_cu);
15122 baton->locexpr.per_cu = cu->per_cu;
15123 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15124 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15125 prop->data.baton = baton;
15126 prop->kind = PROP_LOCEXPR;
15127 gdb_assert (prop->data.baton != NULL);
15128 }
15129 else
15130 {
15131 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15132 "dynamic property");
15133 return 0;
15134 }
15135 break;
15136 case DW_AT_data_member_location:
15137 {
15138 LONGEST offset;
15139
15140 if (!handle_data_member_location (target_die, target_cu,
15141 &offset))
15142 return 0;
15143
8d749320 15144 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15145 baton->referenced_type = read_type_die (target_die->parent,
15146 target_cu);
df25ebbd
JB
15147 baton->offset_info.offset = offset;
15148 baton->offset_info.type = die_type (target_die, target_cu);
15149 prop->data.baton = baton;
15150 prop->kind = PROP_ADDR_OFFSET;
15151 break;
15152 }
80180f79
SA
15153 }
15154 }
15155 else if (attr_form_is_constant (attr))
15156 {
15157 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15158 prop->kind = PROP_CONST;
15159 }
15160 else
15161 {
15162 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15163 dwarf2_name (die, cu));
15164 return 0;
15165 }
15166
15167 return 1;
15168}
15169
a02abb62
JB
15170/* Read the given DW_AT_subrange DIE. */
15171
f792889a 15172static struct type *
a02abb62
JB
15173read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15174{
4c9ad8c2 15175 struct type *base_type, *orig_base_type;
a02abb62
JB
15176 struct type *range_type;
15177 struct attribute *attr;
729efb13 15178 struct dynamic_prop low, high;
4fae6e18 15179 int low_default_is_valid;
c451ebe5 15180 int high_bound_is_count = 0;
15d034d0 15181 const char *name;
43bbcdc2 15182 LONGEST negative_mask;
e77813c8 15183
4c9ad8c2
TT
15184 orig_base_type = die_type (die, cu);
15185 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15186 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15187 creating the range type, but we use the result of check_typedef
15188 when examining properties of the type. */
15189 base_type = check_typedef (orig_base_type);
a02abb62 15190
7e314c57
JK
15191 /* The die_type call above may have already set the type for this DIE. */
15192 range_type = get_die_type (die, cu);
15193 if (range_type)
15194 return range_type;
15195
729efb13
SA
15196 low.kind = PROP_CONST;
15197 high.kind = PROP_CONST;
15198 high.data.const_val = 0;
15199
4fae6e18
JK
15200 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15201 omitting DW_AT_lower_bound. */
15202 switch (cu->language)
6e70227d 15203 {
4fae6e18
JK
15204 case language_c:
15205 case language_cplus:
729efb13 15206 low.data.const_val = 0;
4fae6e18
JK
15207 low_default_is_valid = 1;
15208 break;
15209 case language_fortran:
729efb13 15210 low.data.const_val = 1;
4fae6e18
JK
15211 low_default_is_valid = 1;
15212 break;
15213 case language_d:
4fae6e18 15214 case language_objc:
c44af4eb 15215 case language_rust:
729efb13 15216 low.data.const_val = 0;
4fae6e18
JK
15217 low_default_is_valid = (cu->header.version >= 4);
15218 break;
15219 case language_ada:
15220 case language_m2:
15221 case language_pascal:
729efb13 15222 low.data.const_val = 1;
4fae6e18
JK
15223 low_default_is_valid = (cu->header.version >= 4);
15224 break;
15225 default:
729efb13 15226 low.data.const_val = 0;
4fae6e18
JK
15227 low_default_is_valid = 0;
15228 break;
a02abb62
JB
15229 }
15230
e142c38c 15231 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15232 if (attr)
11c1ba78 15233 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15234 else if (!low_default_is_valid)
15235 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15236 "- DIE at 0x%x [in module %s]"),
4262abfb 15237 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 15238
e142c38c 15239 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15240 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15241 {
15242 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15243 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15244 {
c451ebe5
SA
15245 /* If bounds are constant do the final calculation here. */
15246 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15247 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15248 else
15249 high_bound_is_count = 1;
c2ff108b 15250 }
e77813c8
PM
15251 }
15252
15253 /* Dwarf-2 specifications explicitly allows to create subrange types
15254 without specifying a base type.
15255 In that case, the base type must be set to the type of
15256 the lower bound, upper bound or count, in that order, if any of these
15257 three attributes references an object that has a type.
15258 If no base type is found, the Dwarf-2 specifications say that
15259 a signed integer type of size equal to the size of an address should
15260 be used.
15261 For the following C code: `extern char gdb_int [];'
15262 GCC produces an empty range DIE.
15263 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15264 high bound or count are not yet handled by this code. */
e77813c8
PM
15265 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15266 {
15267 struct objfile *objfile = cu->objfile;
15268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15269 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15270 struct type *int_type = objfile_type (objfile)->builtin_int;
15271
15272 /* Test "int", "long int", and "long long int" objfile types,
15273 and select the first one having a size above or equal to the
15274 architecture address size. */
15275 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15276 base_type = int_type;
15277 else
15278 {
15279 int_type = objfile_type (objfile)->builtin_long;
15280 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15281 base_type = int_type;
15282 else
15283 {
15284 int_type = objfile_type (objfile)->builtin_long_long;
15285 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15286 base_type = int_type;
15287 }
15288 }
15289 }
a02abb62 15290
dbb9c2b1
JB
15291 /* Normally, the DWARF producers are expected to use a signed
15292 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15293 But this is unfortunately not always the case, as witnessed
15294 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15295 is used instead. To work around that ambiguity, we treat
15296 the bounds as signed, and thus sign-extend their values, when
15297 the base type is signed. */
6e70227d 15298 negative_mask =
66c6502d 15299 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15300 if (low.kind == PROP_CONST
15301 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15302 low.data.const_val |= negative_mask;
15303 if (high.kind == PROP_CONST
15304 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15305 high.data.const_val |= negative_mask;
43bbcdc2 15306
729efb13 15307 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15308
c451ebe5
SA
15309 if (high_bound_is_count)
15310 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15311
c2ff108b
JK
15312 /* Ada expects an empty array on no boundary attributes. */
15313 if (attr == NULL && cu->language != language_ada)
729efb13 15314 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15315
39cbfefa
DJ
15316 name = dwarf2_name (die, cu);
15317 if (name)
15318 TYPE_NAME (range_type) = name;
6e70227d 15319
e142c38c 15320 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15321 if (attr)
15322 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15323
7e314c57
JK
15324 set_die_type (die, range_type, cu);
15325
15326 /* set_die_type should be already done. */
b4ba55a1
JB
15327 set_descriptive_type (range_type, die, cu);
15328
7e314c57 15329 return range_type;
a02abb62 15330}
6e70227d 15331
f792889a 15332static struct type *
81a17f79
JB
15333read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15334{
15335 struct type *type;
81a17f79 15336
81a17f79
JB
15337 /* For now, we only support the C meaning of an unspecified type: void. */
15338
19f392bc 15339 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15340 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15341
f792889a 15342 return set_die_type (die, type, cu);
81a17f79 15343}
a02abb62 15344
639d11d3
DC
15345/* Read a single die and all its descendents. Set the die's sibling
15346 field to NULL; set other fields in the die correctly, and set all
15347 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15348 location of the info_ptr after reading all of those dies. PARENT
15349 is the parent of the die in question. */
15350
15351static struct die_info *
dee91e82 15352read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15353 const gdb_byte *info_ptr,
15354 const gdb_byte **new_info_ptr,
dee91e82 15355 struct die_info *parent)
639d11d3
DC
15356{
15357 struct die_info *die;
d521ce57 15358 const gdb_byte *cur_ptr;
639d11d3
DC
15359 int has_children;
15360
bf6af496 15361 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15362 if (die == NULL)
15363 {
15364 *new_info_ptr = cur_ptr;
15365 return NULL;
15366 }
93311388 15367 store_in_ref_table (die, reader->cu);
639d11d3
DC
15368
15369 if (has_children)
bf6af496 15370 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15371 else
15372 {
15373 die->child = NULL;
15374 *new_info_ptr = cur_ptr;
15375 }
15376
15377 die->sibling = NULL;
15378 die->parent = parent;
15379 return die;
15380}
15381
15382/* Read a die, all of its descendents, and all of its siblings; set
15383 all of the fields of all of the dies correctly. Arguments are as
15384 in read_die_and_children. */
15385
15386static struct die_info *
bf6af496 15387read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15388 const gdb_byte *info_ptr,
15389 const gdb_byte **new_info_ptr,
bf6af496 15390 struct die_info *parent)
639d11d3
DC
15391{
15392 struct die_info *first_die, *last_sibling;
d521ce57 15393 const gdb_byte *cur_ptr;
639d11d3 15394
c906108c 15395 cur_ptr = info_ptr;
639d11d3
DC
15396 first_die = last_sibling = NULL;
15397
15398 while (1)
c906108c 15399 {
639d11d3 15400 struct die_info *die
dee91e82 15401 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15402
1d325ec1 15403 if (die == NULL)
c906108c 15404 {
639d11d3
DC
15405 *new_info_ptr = cur_ptr;
15406 return first_die;
c906108c 15407 }
1d325ec1
DJ
15408
15409 if (!first_die)
15410 first_die = die;
c906108c 15411 else
1d325ec1
DJ
15412 last_sibling->sibling = die;
15413
15414 last_sibling = die;
c906108c 15415 }
c906108c
SS
15416}
15417
bf6af496
DE
15418/* Read a die, all of its descendents, and all of its siblings; set
15419 all of the fields of all of the dies correctly. Arguments are as
15420 in read_die_and_children.
15421 This the main entry point for reading a DIE and all its children. */
15422
15423static struct die_info *
15424read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15425 const gdb_byte *info_ptr,
15426 const gdb_byte **new_info_ptr,
bf6af496
DE
15427 struct die_info *parent)
15428{
15429 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15430 new_info_ptr, parent);
15431
b4f54984 15432 if (dwarf_die_debug)
bf6af496
DE
15433 {
15434 fprintf_unfiltered (gdb_stdlog,
15435 "Read die from %s@0x%x of %s:\n",
a32a8923 15436 get_section_name (reader->die_section),
bf6af496
DE
15437 (unsigned) (info_ptr - reader->die_section->buffer),
15438 bfd_get_filename (reader->abfd));
b4f54984 15439 dump_die (die, dwarf_die_debug);
bf6af496
DE
15440 }
15441
15442 return die;
15443}
15444
3019eac3
DE
15445/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15446 attributes.
15447 The caller is responsible for filling in the extra attributes
15448 and updating (*DIEP)->num_attrs.
15449 Set DIEP to point to a newly allocated die with its information,
15450 except for its child, sibling, and parent fields.
15451 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15452
d521ce57 15453static const gdb_byte *
3019eac3 15454read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15455 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15456 int *has_children, int num_extra_attrs)
93311388 15457{
b64f50a1
JK
15458 unsigned int abbrev_number, bytes_read, i;
15459 sect_offset offset;
93311388
DE
15460 struct abbrev_info *abbrev;
15461 struct die_info *die;
15462 struct dwarf2_cu *cu = reader->cu;
15463 bfd *abfd = reader->abfd;
15464
b64f50a1 15465 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
15466 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15467 info_ptr += bytes_read;
15468 if (!abbrev_number)
15469 {
15470 *diep = NULL;
15471 *has_children = 0;
15472 return info_ptr;
15473 }
15474
433df2d4 15475 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15476 if (!abbrev)
348e048f
DE
15477 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15478 abbrev_number,
15479 bfd_get_filename (abfd));
15480
3019eac3 15481 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
15482 die->offset = offset;
15483 die->tag = abbrev->tag;
15484 die->abbrev = abbrev_number;
15485
3019eac3
DE
15486 /* Make the result usable.
15487 The caller needs to update num_attrs after adding the extra
15488 attributes. */
93311388
DE
15489 die->num_attrs = abbrev->num_attrs;
15490
15491 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15492 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15493 info_ptr);
93311388
DE
15494
15495 *diep = die;
15496 *has_children = abbrev->has_children;
15497 return info_ptr;
15498}
15499
3019eac3
DE
15500/* Read a die and all its attributes.
15501 Set DIEP to point to a newly allocated die with its information,
15502 except for its child, sibling, and parent fields.
15503 Set HAS_CHILDREN to tell whether the die has children or not. */
15504
d521ce57 15505static const gdb_byte *
3019eac3 15506read_full_die (const struct die_reader_specs *reader,
d521ce57 15507 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15508 int *has_children)
15509{
d521ce57 15510 const gdb_byte *result;
bf6af496
DE
15511
15512 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15513
b4f54984 15514 if (dwarf_die_debug)
bf6af496
DE
15515 {
15516 fprintf_unfiltered (gdb_stdlog,
15517 "Read die from %s@0x%x of %s:\n",
a32a8923 15518 get_section_name (reader->die_section),
bf6af496
DE
15519 (unsigned) (info_ptr - reader->die_section->buffer),
15520 bfd_get_filename (reader->abfd));
b4f54984 15521 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15522 }
15523
15524 return result;
3019eac3 15525}
433df2d4
DE
15526\f
15527/* Abbreviation tables.
3019eac3 15528
433df2d4 15529 In DWARF version 2, the description of the debugging information is
c906108c
SS
15530 stored in a separate .debug_abbrev section. Before we read any
15531 dies from a section we read in all abbreviations and install them
433df2d4
DE
15532 in a hash table. */
15533
15534/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15535
15536static struct abbrev_info *
15537abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15538{
15539 struct abbrev_info *abbrev;
15540
8d749320 15541 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15542 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15543
433df2d4
DE
15544 return abbrev;
15545}
15546
15547/* Add an abbreviation to the table. */
c906108c
SS
15548
15549static void
433df2d4
DE
15550abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15551 unsigned int abbrev_number,
15552 struct abbrev_info *abbrev)
15553{
15554 unsigned int hash_number;
15555
15556 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15557 abbrev->next = abbrev_table->abbrevs[hash_number];
15558 abbrev_table->abbrevs[hash_number] = abbrev;
15559}
dee91e82 15560
433df2d4
DE
15561/* Look up an abbrev in the table.
15562 Returns NULL if the abbrev is not found. */
15563
15564static struct abbrev_info *
15565abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15566 unsigned int abbrev_number)
c906108c 15567{
433df2d4
DE
15568 unsigned int hash_number;
15569 struct abbrev_info *abbrev;
15570
15571 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15572 abbrev = abbrev_table->abbrevs[hash_number];
15573
15574 while (abbrev)
15575 {
15576 if (abbrev->number == abbrev_number)
15577 return abbrev;
15578 abbrev = abbrev->next;
15579 }
15580 return NULL;
15581}
15582
15583/* Read in an abbrev table. */
15584
15585static struct abbrev_table *
15586abbrev_table_read_table (struct dwarf2_section_info *section,
15587 sect_offset offset)
15588{
15589 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15590 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15591 struct abbrev_table *abbrev_table;
d521ce57 15592 const gdb_byte *abbrev_ptr;
c906108c
SS
15593 struct abbrev_info *cur_abbrev;
15594 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15595 unsigned int abbrev_form;
f3dd6933
DJ
15596 struct attr_abbrev *cur_attrs;
15597 unsigned int allocated_attrs;
c906108c 15598
70ba0933 15599 abbrev_table = XNEW (struct abbrev_table);
f4dc4d17 15600 abbrev_table->offset = offset;
433df2d4 15601 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15602 abbrev_table->abbrevs =
15603 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15604 ABBREV_HASH_SIZE);
433df2d4
DE
15605 memset (abbrev_table->abbrevs, 0,
15606 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15607
433df2d4
DE
15608 dwarf2_read_section (objfile, section);
15609 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
15610 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15611 abbrev_ptr += bytes_read;
15612
f3dd6933 15613 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15614 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15615
0963b4bd 15616 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15617 while (abbrev_number)
15618 {
433df2d4 15619 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15620
15621 /* read in abbrev header */
15622 cur_abbrev->number = abbrev_number;
aead7601
SM
15623 cur_abbrev->tag
15624 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15625 abbrev_ptr += bytes_read;
15626 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15627 abbrev_ptr += 1;
15628
15629 /* now read in declarations */
22d2f3ab 15630 for (;;)
c906108c 15631 {
43988095
JK
15632 LONGEST implicit_const;
15633
22d2f3ab
JK
15634 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15635 abbrev_ptr += bytes_read;
15636 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15637 abbrev_ptr += bytes_read;
43988095
JK
15638 if (abbrev_form == DW_FORM_implicit_const)
15639 {
15640 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15641 &bytes_read);
15642 abbrev_ptr += bytes_read;
15643 }
15644 else
15645 {
15646 /* Initialize it due to a false compiler warning. */
15647 implicit_const = -1;
15648 }
22d2f3ab
JK
15649
15650 if (abbrev_name == 0)
15651 break;
15652
f3dd6933 15653 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15654 {
f3dd6933
DJ
15655 allocated_attrs += ATTR_ALLOC_CHUNK;
15656 cur_attrs
224c3ddb 15657 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15658 }
ae038cb0 15659
aead7601
SM
15660 cur_attrs[cur_abbrev->num_attrs].name
15661 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15662 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15663 = (enum dwarf_form) abbrev_form;
43988095 15664 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15665 ++cur_abbrev->num_attrs;
c906108c
SS
15666 }
15667
8d749320
SM
15668 cur_abbrev->attrs =
15669 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15670 cur_abbrev->num_attrs);
f3dd6933
DJ
15671 memcpy (cur_abbrev->attrs, cur_attrs,
15672 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15673
433df2d4 15674 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15675
15676 /* Get next abbreviation.
15677 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15678 always properly terminated with an abbrev number of 0.
15679 Exit loop if we encounter an abbreviation which we have
15680 already read (which means we are about to read the abbreviations
15681 for the next compile unit) or if the end of the abbreviation
15682 table is reached. */
433df2d4 15683 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15684 break;
15685 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15686 abbrev_ptr += bytes_read;
433df2d4 15687 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15688 break;
15689 }
f3dd6933
DJ
15690
15691 xfree (cur_attrs);
433df2d4 15692 return abbrev_table;
c906108c
SS
15693}
15694
433df2d4 15695/* Free the resources held by ABBREV_TABLE. */
c906108c 15696
c906108c 15697static void
433df2d4 15698abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15699{
433df2d4
DE
15700 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15701 xfree (abbrev_table);
c906108c
SS
15702}
15703
f4dc4d17
DE
15704/* Same as abbrev_table_free but as a cleanup.
15705 We pass in a pointer to the pointer to the table so that we can
15706 set the pointer to NULL when we're done. It also simplifies
73051182 15707 build_type_psymtabs_1. */
f4dc4d17
DE
15708
15709static void
15710abbrev_table_free_cleanup (void *table_ptr)
15711{
9a3c8263 15712 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15713
15714 if (*abbrev_table_ptr != NULL)
15715 abbrev_table_free (*abbrev_table_ptr);
15716 *abbrev_table_ptr = NULL;
15717}
15718
433df2d4
DE
15719/* Read the abbrev table for CU from ABBREV_SECTION. */
15720
15721static void
15722dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15723 struct dwarf2_section_info *abbrev_section)
c906108c 15724{
433df2d4
DE
15725 cu->abbrev_table =
15726 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15727}
c906108c 15728
433df2d4 15729/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15730
433df2d4
DE
15731static void
15732dwarf2_free_abbrev_table (void *ptr_to_cu)
15733{
9a3c8263 15734 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15735
a2ce51a0
DE
15736 if (cu->abbrev_table != NULL)
15737 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15738 /* Set this to NULL so that we SEGV if we try to read it later,
15739 and also because free_comp_unit verifies this is NULL. */
15740 cu->abbrev_table = NULL;
15741}
15742\f
72bf9492
DJ
15743/* Returns nonzero if TAG represents a type that we might generate a partial
15744 symbol for. */
15745
15746static int
15747is_type_tag_for_partial (int tag)
15748{
15749 switch (tag)
15750 {
15751#if 0
15752 /* Some types that would be reasonable to generate partial symbols for,
15753 that we don't at present. */
15754 case DW_TAG_array_type:
15755 case DW_TAG_file_type:
15756 case DW_TAG_ptr_to_member_type:
15757 case DW_TAG_set_type:
15758 case DW_TAG_string_type:
15759 case DW_TAG_subroutine_type:
15760#endif
15761 case DW_TAG_base_type:
15762 case DW_TAG_class_type:
680b30c7 15763 case DW_TAG_interface_type:
72bf9492
DJ
15764 case DW_TAG_enumeration_type:
15765 case DW_TAG_structure_type:
15766 case DW_TAG_subrange_type:
15767 case DW_TAG_typedef:
15768 case DW_TAG_union_type:
15769 return 1;
15770 default:
15771 return 0;
15772 }
15773}
15774
15775/* Load all DIEs that are interesting for partial symbols into memory. */
15776
15777static struct partial_die_info *
dee91e82 15778load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15779 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15780{
dee91e82 15781 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15782 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15783 struct partial_die_info *part_die;
15784 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15785 struct abbrev_info *abbrev;
15786 unsigned int bytes_read;
5afb4e99 15787 unsigned int load_all = 0;
72bf9492
DJ
15788 int nesting_level = 1;
15789
15790 parent_die = NULL;
15791 last_die = NULL;
15792
7adf1e79
DE
15793 gdb_assert (cu->per_cu != NULL);
15794 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15795 load_all = 1;
15796
72bf9492
DJ
15797 cu->partial_dies
15798 = htab_create_alloc_ex (cu->header.length / 12,
15799 partial_die_hash,
15800 partial_die_eq,
15801 NULL,
15802 &cu->comp_unit_obstack,
15803 hashtab_obstack_allocate,
15804 dummy_obstack_deallocate);
15805
8d749320 15806 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15807
15808 while (1)
15809 {
15810 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15811
15812 /* A NULL abbrev means the end of a series of children. */
15813 if (abbrev == NULL)
15814 {
15815 if (--nesting_level == 0)
15816 {
15817 /* PART_DIE was probably the last thing allocated on the
15818 comp_unit_obstack, so we could call obstack_free
15819 here. We don't do that because the waste is small,
15820 and will be cleaned up when we're done with this
15821 compilation unit. This way, we're also more robust
15822 against other users of the comp_unit_obstack. */
15823 return first_die;
15824 }
15825 info_ptr += bytes_read;
15826 last_die = parent_die;
15827 parent_die = parent_die->die_parent;
15828 continue;
15829 }
15830
98bfdba5
PA
15831 /* Check for template arguments. We never save these; if
15832 they're seen, we just mark the parent, and go on our way. */
15833 if (parent_die != NULL
15834 && cu->language == language_cplus
15835 && (abbrev->tag == DW_TAG_template_type_param
15836 || abbrev->tag == DW_TAG_template_value_param))
15837 {
15838 parent_die->has_template_arguments = 1;
15839
15840 if (!load_all)
15841 {
15842 /* We don't need a partial DIE for the template argument. */
dee91e82 15843 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15844 continue;
15845 }
15846 }
15847
0d99eb77 15848 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15849 Skip their other children. */
15850 if (!load_all
15851 && cu->language == language_cplus
15852 && parent_die != NULL
15853 && parent_die->tag == DW_TAG_subprogram)
15854 {
dee91e82 15855 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15856 continue;
15857 }
15858
5afb4e99
DJ
15859 /* Check whether this DIE is interesting enough to save. Normally
15860 we would not be interested in members here, but there may be
15861 later variables referencing them via DW_AT_specification (for
15862 static members). */
15863 if (!load_all
15864 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15865 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15866 && abbrev->tag != DW_TAG_enumerator
15867 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15868 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15869 && abbrev->tag != DW_TAG_variable
5afb4e99 15870 && abbrev->tag != DW_TAG_namespace
f55ee35c 15871 && abbrev->tag != DW_TAG_module
95554aad 15872 && abbrev->tag != DW_TAG_member
74921315
KS
15873 && abbrev->tag != DW_TAG_imported_unit
15874 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15875 {
15876 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15877 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15878 continue;
15879 }
15880
dee91e82
DE
15881 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15882 info_ptr);
72bf9492
DJ
15883
15884 /* This two-pass algorithm for processing partial symbols has a
15885 high cost in cache pressure. Thus, handle some simple cases
15886 here which cover the majority of C partial symbols. DIEs
15887 which neither have specification tags in them, nor could have
15888 specification tags elsewhere pointing at them, can simply be
15889 processed and discarded.
15890
15891 This segment is also optional; scan_partial_symbols and
15892 add_partial_symbol will handle these DIEs if we chain
15893 them in normally. When compilers which do not emit large
15894 quantities of duplicate debug information are more common,
15895 this code can probably be removed. */
15896
15897 /* Any complete simple types at the top level (pretty much all
15898 of them, for a language without namespaces), can be processed
15899 directly. */
15900 if (parent_die == NULL
15901 && part_die->has_specification == 0
15902 && part_die->is_declaration == 0
d8228535 15903 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15904 || part_die->tag == DW_TAG_base_type
15905 || part_die->tag == DW_TAG_subrange_type))
15906 {
15907 if (building_psymtab && part_die->name != NULL)
04a679b8 15908 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15909 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15910 &objfile->static_psymbols,
1762568f 15911 0, cu->language, objfile);
dee91e82 15912 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15913 continue;
15914 }
15915
d8228535
JK
15916 /* The exception for DW_TAG_typedef with has_children above is
15917 a workaround of GCC PR debug/47510. In the case of this complaint
15918 type_name_no_tag_or_error will error on such types later.
15919
15920 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15921 it could not find the child DIEs referenced later, this is checked
15922 above. In correct DWARF DW_TAG_typedef should have no children. */
15923
15924 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15925 complaint (&symfile_complaints,
15926 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15927 "- DIE at 0x%x [in module %s]"),
4262abfb 15928 part_die->offset.sect_off, objfile_name (objfile));
d8228535 15929
72bf9492
DJ
15930 /* If we're at the second level, and we're an enumerator, and
15931 our parent has no specification (meaning possibly lives in a
15932 namespace elsewhere), then we can add the partial symbol now
15933 instead of queueing it. */
15934 if (part_die->tag == DW_TAG_enumerator
15935 && parent_die != NULL
15936 && parent_die->die_parent == NULL
15937 && parent_die->tag == DW_TAG_enumeration_type
15938 && parent_die->has_specification == 0)
15939 {
15940 if (part_die->name == NULL)
3e43a32a
MS
15941 complaint (&symfile_complaints,
15942 _("malformed enumerator DIE ignored"));
72bf9492 15943 else if (building_psymtab)
04a679b8 15944 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15945 VAR_DOMAIN, LOC_CONST,
9c37b5ae 15946 cu->language == language_cplus
bb5ed363
DE
15947 ? &objfile->global_psymbols
15948 : &objfile->static_psymbols,
1762568f 15949 0, cu->language, objfile);
72bf9492 15950
dee91e82 15951 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15952 continue;
15953 }
15954
15955 /* We'll save this DIE so link it in. */
15956 part_die->die_parent = parent_die;
15957 part_die->die_sibling = NULL;
15958 part_die->die_child = NULL;
15959
15960 if (last_die && last_die == parent_die)
15961 last_die->die_child = part_die;
15962 else if (last_die)
15963 last_die->die_sibling = part_die;
15964
15965 last_die = part_die;
15966
15967 if (first_die == NULL)
15968 first_die = part_die;
15969
15970 /* Maybe add the DIE to the hash table. Not all DIEs that we
15971 find interesting need to be in the hash table, because we
15972 also have the parent/sibling/child chains; only those that we
15973 might refer to by offset later during partial symbol reading.
15974
15975 For now this means things that might have be the target of a
15976 DW_AT_specification, DW_AT_abstract_origin, or
15977 DW_AT_extension. DW_AT_extension will refer only to
15978 namespaces; DW_AT_abstract_origin refers to functions (and
15979 many things under the function DIE, but we do not recurse
15980 into function DIEs during partial symbol reading) and
15981 possibly variables as well; DW_AT_specification refers to
15982 declarations. Declarations ought to have the DW_AT_declaration
15983 flag. It happens that GCC forgets to put it in sometimes, but
15984 only for functions, not for types.
15985
15986 Adding more things than necessary to the hash table is harmless
15987 except for the performance cost. Adding too few will result in
5afb4e99
DJ
15988 wasted time in find_partial_die, when we reread the compilation
15989 unit with load_all_dies set. */
72bf9492 15990
5afb4e99 15991 if (load_all
72929c62 15992 || abbrev->tag == DW_TAG_constant
5afb4e99 15993 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
15994 || abbrev->tag == DW_TAG_variable
15995 || abbrev->tag == DW_TAG_namespace
15996 || part_die->is_declaration)
15997 {
15998 void **slot;
15999
16000 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 16001 part_die->offset.sect_off, INSERT);
72bf9492
DJ
16002 *slot = part_die;
16003 }
16004
8d749320 16005 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16006
16007 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16008 we have no reason to follow the children of structures; for other
98bfdba5
PA
16009 languages we have to, so that we can get at method physnames
16010 to infer fully qualified class names, for DW_AT_specification,
16011 and for C++ template arguments. For C++, we also look one level
16012 inside functions to find template arguments (if the name of the
16013 function does not already contain the template arguments).
bc30ff58
JB
16014
16015 For Ada, we need to scan the children of subprograms and lexical
16016 blocks as well because Ada allows the definition of nested
16017 entities that could be interesting for the debugger, such as
16018 nested subprograms for instance. */
72bf9492 16019 if (last_die->has_children
5afb4e99
DJ
16020 && (load_all
16021 || last_die->tag == DW_TAG_namespace
f55ee35c 16022 || last_die->tag == DW_TAG_module
72bf9492 16023 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16024 || (cu->language == language_cplus
16025 && last_die->tag == DW_TAG_subprogram
16026 && (last_die->name == NULL
16027 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16028 || (cu->language != language_c
16029 && (last_die->tag == DW_TAG_class_type
680b30c7 16030 || last_die->tag == DW_TAG_interface_type
72bf9492 16031 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16032 || last_die->tag == DW_TAG_union_type))
16033 || (cu->language == language_ada
16034 && (last_die->tag == DW_TAG_subprogram
16035 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16036 {
16037 nesting_level++;
16038 parent_die = last_die;
16039 continue;
16040 }
16041
16042 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16043 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16044
16045 /* Back to the top, do it again. */
16046 }
16047}
16048
c906108c
SS
16049/* Read a minimal amount of information into the minimal die structure. */
16050
d521ce57 16051static const gdb_byte *
dee91e82
DE
16052read_partial_die (const struct die_reader_specs *reader,
16053 struct partial_die_info *part_die,
16054 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16055 const gdb_byte *info_ptr)
c906108c 16056{
dee91e82 16057 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16058 struct objfile *objfile = cu->objfile;
d521ce57 16059 const gdb_byte *buffer = reader->buffer;
fa238c03 16060 unsigned int i;
c906108c 16061 struct attribute attr;
c5aa993b 16062 int has_low_pc_attr = 0;
c906108c 16063 int has_high_pc_attr = 0;
91da1414 16064 int high_pc_relative = 0;
c906108c 16065
72bf9492 16066 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16067
b64f50a1 16068 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
16069
16070 info_ptr += abbrev_len;
16071
16072 if (abbrev == NULL)
16073 return info_ptr;
16074
c906108c
SS
16075 part_die->tag = abbrev->tag;
16076 part_die->has_children = abbrev->has_children;
c906108c
SS
16077
16078 for (i = 0; i < abbrev->num_attrs; ++i)
16079 {
dee91e82 16080 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16081
16082 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16083 partial symbol table. */
c906108c
SS
16084 switch (attr.name)
16085 {
16086 case DW_AT_name:
71c25dea
TT
16087 switch (part_die->tag)
16088 {
16089 case DW_TAG_compile_unit:
95554aad 16090 case DW_TAG_partial_unit:
348e048f 16091 case DW_TAG_type_unit:
71c25dea
TT
16092 /* Compilation units have a DW_AT_name that is a filename, not
16093 a source language identifier. */
16094 case DW_TAG_enumeration_type:
16095 case DW_TAG_enumerator:
16096 /* These tags always have simple identifiers already; no need
16097 to canonicalize them. */
16098 part_die->name = DW_STRING (&attr);
16099 break;
16100 default:
16101 part_die->name
16102 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16103 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16104 break;
16105 }
c906108c 16106 break;
31ef98ae 16107 case DW_AT_linkage_name:
c906108c 16108 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16109 /* Note that both forms of linkage name might appear. We
16110 assume they will be the same, and we only store the last
16111 one we see. */
94af9270
KS
16112 if (cu->language == language_ada)
16113 part_die->name = DW_STRING (&attr);
abc72ce4 16114 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16115 break;
16116 case DW_AT_low_pc:
16117 has_low_pc_attr = 1;
31aa7e4e 16118 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16119 break;
16120 case DW_AT_high_pc:
16121 has_high_pc_attr = 1;
31aa7e4e
JB
16122 part_die->highpc = attr_value_as_address (&attr);
16123 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16124 high_pc_relative = 1;
c906108c
SS
16125 break;
16126 case DW_AT_location:
0963b4bd 16127 /* Support the .debug_loc offsets. */
8e19ed76
PS
16128 if (attr_form_is_block (&attr))
16129 {
95554aad 16130 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16131 }
3690dd37 16132 else if (attr_form_is_section_offset (&attr))
8e19ed76 16133 {
4d3c2250 16134 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16135 }
16136 else
16137 {
4d3c2250
KB
16138 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16139 "partial symbol information");
8e19ed76 16140 }
c906108c 16141 break;
c906108c
SS
16142 case DW_AT_external:
16143 part_die->is_external = DW_UNSND (&attr);
16144 break;
16145 case DW_AT_declaration:
16146 part_die->is_declaration = DW_UNSND (&attr);
16147 break;
16148 case DW_AT_type:
16149 part_die->has_type = 1;
16150 break;
16151 case DW_AT_abstract_origin:
16152 case DW_AT_specification:
72bf9492
DJ
16153 case DW_AT_extension:
16154 part_die->has_specification = 1;
c764a876 16155 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16156 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16157 || cu->per_cu->is_dwz);
c906108c
SS
16158 break;
16159 case DW_AT_sibling:
16160 /* Ignore absolute siblings, they might point outside of
16161 the current compile unit. */
16162 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16163 complaint (&symfile_complaints,
16164 _("ignoring absolute DW_AT_sibling"));
c906108c 16165 else
b9502d3f
WN
16166 {
16167 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16168 const gdb_byte *sibling_ptr = buffer + off;
16169
16170 if (sibling_ptr < info_ptr)
16171 complaint (&symfile_complaints,
16172 _("DW_AT_sibling points backwards"));
22869d73
KS
16173 else if (sibling_ptr > reader->buffer_end)
16174 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16175 else
16176 part_die->sibling = sibling_ptr;
16177 }
c906108c 16178 break;
fa4028e9
JB
16179 case DW_AT_byte_size:
16180 part_die->has_byte_size = 1;
16181 break;
ff908ebf
AW
16182 case DW_AT_const_value:
16183 part_die->has_const_value = 1;
16184 break;
68511cec
CES
16185 case DW_AT_calling_convention:
16186 /* DWARF doesn't provide a way to identify a program's source-level
16187 entry point. DW_AT_calling_convention attributes are only meant
16188 to describe functions' calling conventions.
16189
16190 However, because it's a necessary piece of information in
0c1b455e
TT
16191 Fortran, and before DWARF 4 DW_CC_program was the only
16192 piece of debugging information whose definition refers to
16193 a 'main program' at all, several compilers marked Fortran
16194 main programs with DW_CC_program --- even when those
16195 functions use the standard calling conventions.
16196
16197 Although DWARF now specifies a way to provide this
16198 information, we support this practice for backward
16199 compatibility. */
68511cec 16200 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16201 && cu->language == language_fortran)
16202 part_die->main_subprogram = 1;
68511cec 16203 break;
481860b3
GB
16204 case DW_AT_inline:
16205 if (DW_UNSND (&attr) == DW_INL_inlined
16206 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16207 part_die->may_be_inlined = 1;
16208 break;
95554aad
TT
16209
16210 case DW_AT_import:
16211 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
16212 {
16213 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16214 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16215 || cu->per_cu->is_dwz);
16216 }
95554aad
TT
16217 break;
16218
0c1b455e
TT
16219 case DW_AT_main_subprogram:
16220 part_die->main_subprogram = DW_UNSND (&attr);
16221 break;
16222
c906108c
SS
16223 default:
16224 break;
16225 }
16226 }
16227
91da1414
MW
16228 if (high_pc_relative)
16229 part_die->highpc += part_die->lowpc;
16230
9373cf26
JK
16231 if (has_low_pc_attr && has_high_pc_attr)
16232 {
16233 /* When using the GNU linker, .gnu.linkonce. sections are used to
16234 eliminate duplicate copies of functions and vtables and such.
16235 The linker will arbitrarily choose one and discard the others.
16236 The AT_*_pc values for such functions refer to local labels in
16237 these sections. If the section from that file was discarded, the
16238 labels are not in the output, so the relocs get a value of 0.
16239 If this is a discarded function, mark the pc bounds as invalid,
16240 so that GDB will ignore it. */
16241 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16242 {
bb5ed363 16243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16244
16245 complaint (&symfile_complaints,
16246 _("DW_AT_low_pc %s is zero "
16247 "for DIE at 0x%x [in module %s]"),
16248 paddress (gdbarch, part_die->lowpc),
4262abfb 16249 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16250 }
16251 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16252 else if (part_die->lowpc >= part_die->highpc)
16253 {
bb5ed363 16254 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16255
16256 complaint (&symfile_complaints,
16257 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16258 "for DIE at 0x%x [in module %s]"),
16259 paddress (gdbarch, part_die->lowpc),
16260 paddress (gdbarch, part_die->highpc),
4262abfb 16261 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16262 }
16263 else
16264 part_die->has_pc_info = 1;
16265 }
85cbf3d3 16266
c906108c
SS
16267 return info_ptr;
16268}
16269
72bf9492
DJ
16270/* Find a cached partial DIE at OFFSET in CU. */
16271
16272static struct partial_die_info *
b64f50a1 16273find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
16274{
16275 struct partial_die_info *lookup_die = NULL;
16276 struct partial_die_info part_die;
16277
16278 part_die.offset = offset;
9a3c8263
SM
16279 lookup_die = ((struct partial_die_info *)
16280 htab_find_with_hash (cu->partial_dies, &part_die,
16281 offset.sect_off));
72bf9492 16282
72bf9492
DJ
16283 return lookup_die;
16284}
16285
348e048f
DE
16286/* Find a partial DIE at OFFSET, which may or may not be in CU,
16287 except in the case of .debug_types DIEs which do not reference
16288 outside their CU (they do however referencing other types via
55f1336d 16289 DW_FORM_ref_sig8). */
72bf9492
DJ
16290
16291static struct partial_die_info *
36586728 16292find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16293{
bb5ed363 16294 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16295 struct dwarf2_per_cu_data *per_cu = NULL;
16296 struct partial_die_info *pd = NULL;
72bf9492 16297
36586728
TT
16298 if (offset_in_dwz == cu->per_cu->is_dwz
16299 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
16300 {
16301 pd = find_partial_die_in_comp_unit (offset, cu);
16302 if (pd != NULL)
16303 return pd;
0d99eb77
DE
16304 /* We missed recording what we needed.
16305 Load all dies and try again. */
16306 per_cu = cu->per_cu;
5afb4e99 16307 }
0d99eb77
DE
16308 else
16309 {
16310 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16311 if (cu->per_cu->is_debug_types)
0d99eb77
DE
16312 {
16313 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16314 " external reference to offset 0x%lx [in module %s].\n"),
16315 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16316 bfd_get_filename (objfile->obfd));
16317 }
36586728
TT
16318 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16319 objfile);
72bf9492 16320
0d99eb77
DE
16321 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16322 load_partial_comp_unit (per_cu);
ae038cb0 16323
0d99eb77
DE
16324 per_cu->cu->last_used = 0;
16325 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16326 }
5afb4e99 16327
dee91e82
DE
16328 /* If we didn't find it, and not all dies have been loaded,
16329 load them all and try again. */
16330
5afb4e99
DJ
16331 if (pd == NULL && per_cu->load_all_dies == 0)
16332 {
5afb4e99 16333 per_cu->load_all_dies = 1;
fd820528
DE
16334
16335 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16336 THIS_CU->cu may already be in use. So we can't just free it and
16337 replace its DIEs with the ones we read in. Instead, we leave those
16338 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16339 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16340 set. */
dee91e82 16341 load_partial_comp_unit (per_cu);
5afb4e99
DJ
16342
16343 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16344 }
16345
16346 if (pd == NULL)
16347 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16348 _("could not find partial DIE 0x%x "
16349 "in cache [from module %s]\n"),
b64f50a1 16350 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 16351 return pd;
72bf9492
DJ
16352}
16353
abc72ce4
DE
16354/* See if we can figure out if the class lives in a namespace. We do
16355 this by looking for a member function; its demangled name will
16356 contain namespace info, if there is any. */
16357
16358static void
16359guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16360 struct dwarf2_cu *cu)
16361{
16362 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16363 what template types look like, because the demangler
16364 frequently doesn't give the same name as the debug info. We
16365 could fix this by only using the demangled name to get the
16366 prefix (but see comment in read_structure_type). */
16367
16368 struct partial_die_info *real_pdi;
16369 struct partial_die_info *child_pdi;
16370
16371 /* If this DIE (this DIE's specification, if any) has a parent, then
16372 we should not do this. We'll prepend the parent's fully qualified
16373 name when we create the partial symbol. */
16374
16375 real_pdi = struct_pdi;
16376 while (real_pdi->has_specification)
36586728
TT
16377 real_pdi = find_partial_die (real_pdi->spec_offset,
16378 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16379
16380 if (real_pdi->die_parent != NULL)
16381 return;
16382
16383 for (child_pdi = struct_pdi->die_child;
16384 child_pdi != NULL;
16385 child_pdi = child_pdi->die_sibling)
16386 {
16387 if (child_pdi->tag == DW_TAG_subprogram
16388 && child_pdi->linkage_name != NULL)
16389 {
16390 char *actual_class_name
16391 = language_class_name_from_physname (cu->language_defn,
16392 child_pdi->linkage_name);
16393 if (actual_class_name != NULL)
16394 {
16395 struct_pdi->name
224c3ddb
SM
16396 = ((const char *)
16397 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16398 actual_class_name,
16399 strlen (actual_class_name)));
abc72ce4
DE
16400 xfree (actual_class_name);
16401 }
16402 break;
16403 }
16404 }
16405}
16406
72bf9492
DJ
16407/* Adjust PART_DIE before generating a symbol for it. This function
16408 may set the is_external flag or change the DIE's name. */
16409
16410static void
16411fixup_partial_die (struct partial_die_info *part_die,
16412 struct dwarf2_cu *cu)
16413{
abc72ce4
DE
16414 /* Once we've fixed up a die, there's no point in doing so again.
16415 This also avoids a memory leak if we were to call
16416 guess_partial_die_structure_name multiple times. */
16417 if (part_die->fixup_called)
16418 return;
16419
72bf9492
DJ
16420 /* If we found a reference attribute and the DIE has no name, try
16421 to find a name in the referred to DIE. */
16422
16423 if (part_die->name == NULL && part_die->has_specification)
16424 {
16425 struct partial_die_info *spec_die;
72bf9492 16426
36586728
TT
16427 spec_die = find_partial_die (part_die->spec_offset,
16428 part_die->spec_is_dwz, cu);
72bf9492 16429
10b3939b 16430 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16431
16432 if (spec_die->name)
16433 {
16434 part_die->name = spec_die->name;
16435
16436 /* Copy DW_AT_external attribute if it is set. */
16437 if (spec_die->is_external)
16438 part_die->is_external = spec_die->is_external;
16439 }
16440 }
16441
16442 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16443
16444 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16445 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16446
abc72ce4
DE
16447 /* If there is no parent die to provide a namespace, and there are
16448 children, see if we can determine the namespace from their linkage
122d1940 16449 name. */
abc72ce4 16450 if (cu->language == language_cplus
8b70b953 16451 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16452 && part_die->die_parent == NULL
16453 && part_die->has_children
16454 && (part_die->tag == DW_TAG_class_type
16455 || part_die->tag == DW_TAG_structure_type
16456 || part_die->tag == DW_TAG_union_type))
16457 guess_partial_die_structure_name (part_die, cu);
16458
53832f31
TT
16459 /* GCC might emit a nameless struct or union that has a linkage
16460 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16461 if (part_die->name == NULL
96408a79
SA
16462 && (part_die->tag == DW_TAG_class_type
16463 || part_die->tag == DW_TAG_interface_type
16464 || part_die->tag == DW_TAG_structure_type
16465 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16466 && part_die->linkage_name != NULL)
16467 {
16468 char *demangled;
16469
8de20a37 16470 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16471 if (demangled)
16472 {
96408a79
SA
16473 const char *base;
16474
16475 /* Strip any leading namespaces/classes, keep only the base name.
16476 DW_AT_name for named DIEs does not contain the prefixes. */
16477 base = strrchr (demangled, ':');
16478 if (base && base > demangled && base[-1] == ':')
16479 base++;
16480 else
16481 base = demangled;
16482
34a68019 16483 part_die->name
224c3ddb
SM
16484 = ((const char *)
16485 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16486 base, strlen (base)));
53832f31
TT
16487 xfree (demangled);
16488 }
16489 }
16490
abc72ce4 16491 part_die->fixup_called = 1;
72bf9492
DJ
16492}
16493
a8329558 16494/* Read an attribute value described by an attribute form. */
c906108c 16495
d521ce57 16496static const gdb_byte *
dee91e82
DE
16497read_attribute_value (const struct die_reader_specs *reader,
16498 struct attribute *attr, unsigned form,
43988095 16499 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16500{
dee91e82 16501 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16502 struct objfile *objfile = cu->objfile;
16503 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16504 bfd *abfd = reader->abfd;
e7c27a73 16505 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16506 unsigned int bytes_read;
16507 struct dwarf_block *blk;
16508
aead7601 16509 attr->form = (enum dwarf_form) form;
a8329558 16510 switch (form)
c906108c 16511 {
c906108c 16512 case DW_FORM_ref_addr:
ae411497 16513 if (cu->header.version == 2)
4568ecf9 16514 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16515 else
4568ecf9
DE
16516 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16517 &cu->header, &bytes_read);
ae411497
TT
16518 info_ptr += bytes_read;
16519 break;
36586728
TT
16520 case DW_FORM_GNU_ref_alt:
16521 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16522 info_ptr += bytes_read;
16523 break;
ae411497 16524 case DW_FORM_addr:
e7c27a73 16525 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16526 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16527 info_ptr += bytes_read;
c906108c
SS
16528 break;
16529 case DW_FORM_block2:
7b5a2f43 16530 blk = dwarf_alloc_block (cu);
c906108c
SS
16531 blk->size = read_2_bytes (abfd, info_ptr);
16532 info_ptr += 2;
16533 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16534 info_ptr += blk->size;
16535 DW_BLOCK (attr) = blk;
16536 break;
16537 case DW_FORM_block4:
7b5a2f43 16538 blk = dwarf_alloc_block (cu);
c906108c
SS
16539 blk->size = read_4_bytes (abfd, info_ptr);
16540 info_ptr += 4;
16541 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16542 info_ptr += blk->size;
16543 DW_BLOCK (attr) = blk;
16544 break;
16545 case DW_FORM_data2:
16546 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16547 info_ptr += 2;
16548 break;
16549 case DW_FORM_data4:
16550 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16551 info_ptr += 4;
16552 break;
16553 case DW_FORM_data8:
16554 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16555 info_ptr += 8;
16556 break;
2dc7f7b3
TT
16557 case DW_FORM_sec_offset:
16558 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16559 info_ptr += bytes_read;
16560 break;
c906108c 16561 case DW_FORM_string:
9b1c24c8 16562 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16563 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16564 info_ptr += bytes_read;
16565 break;
4bdf3d34 16566 case DW_FORM_strp:
36586728
TT
16567 if (!cu->per_cu->is_dwz)
16568 {
16569 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16570 &bytes_read);
16571 DW_STRING_IS_CANONICAL (attr) = 0;
16572 info_ptr += bytes_read;
16573 break;
16574 }
16575 /* FALLTHROUGH */
43988095
JK
16576 case DW_FORM_line_strp:
16577 if (!cu->per_cu->is_dwz)
16578 {
16579 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16580 cu_header, &bytes_read);
16581 DW_STRING_IS_CANONICAL (attr) = 0;
16582 info_ptr += bytes_read;
16583 break;
16584 }
16585 /* FALLTHROUGH */
36586728
TT
16586 case DW_FORM_GNU_strp_alt:
16587 {
16588 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16589 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16590 &bytes_read);
16591
16592 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16593 DW_STRING_IS_CANONICAL (attr) = 0;
16594 info_ptr += bytes_read;
16595 }
4bdf3d34 16596 break;
2dc7f7b3 16597 case DW_FORM_exprloc:
c906108c 16598 case DW_FORM_block:
7b5a2f43 16599 blk = dwarf_alloc_block (cu);
c906108c
SS
16600 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16601 info_ptr += bytes_read;
16602 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16603 info_ptr += blk->size;
16604 DW_BLOCK (attr) = blk;
16605 break;
16606 case DW_FORM_block1:
7b5a2f43 16607 blk = dwarf_alloc_block (cu);
c906108c
SS
16608 blk->size = read_1_byte (abfd, info_ptr);
16609 info_ptr += 1;
16610 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16611 info_ptr += blk->size;
16612 DW_BLOCK (attr) = blk;
16613 break;
16614 case DW_FORM_data1:
16615 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16616 info_ptr += 1;
16617 break;
16618 case DW_FORM_flag:
16619 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16620 info_ptr += 1;
16621 break;
2dc7f7b3
TT
16622 case DW_FORM_flag_present:
16623 DW_UNSND (attr) = 1;
16624 break;
c906108c
SS
16625 case DW_FORM_sdata:
16626 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16627 info_ptr += bytes_read;
16628 break;
16629 case DW_FORM_udata:
16630 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16631 info_ptr += bytes_read;
16632 break;
16633 case DW_FORM_ref1:
4568ecf9
DE
16634 DW_UNSND (attr) = (cu->header.offset.sect_off
16635 + read_1_byte (abfd, info_ptr));
c906108c
SS
16636 info_ptr += 1;
16637 break;
16638 case DW_FORM_ref2:
4568ecf9
DE
16639 DW_UNSND (attr) = (cu->header.offset.sect_off
16640 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16641 info_ptr += 2;
16642 break;
16643 case DW_FORM_ref4:
4568ecf9
DE
16644 DW_UNSND (attr) = (cu->header.offset.sect_off
16645 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16646 info_ptr += 4;
16647 break;
613e1657 16648 case DW_FORM_ref8:
4568ecf9
DE
16649 DW_UNSND (attr) = (cu->header.offset.sect_off
16650 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16651 info_ptr += 8;
16652 break;
55f1336d 16653 case DW_FORM_ref_sig8:
ac9ec31b 16654 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16655 info_ptr += 8;
16656 break;
c906108c 16657 case DW_FORM_ref_udata:
4568ecf9
DE
16658 DW_UNSND (attr) = (cu->header.offset.sect_off
16659 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16660 info_ptr += bytes_read;
16661 break;
c906108c 16662 case DW_FORM_indirect:
a8329558
KW
16663 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16664 info_ptr += bytes_read;
43988095
JK
16665 if (form == DW_FORM_implicit_const)
16666 {
16667 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16668 info_ptr += bytes_read;
16669 }
16670 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16671 info_ptr);
16672 break;
16673 case DW_FORM_implicit_const:
16674 DW_SND (attr) = implicit_const;
a8329558 16675 break;
3019eac3
DE
16676 case DW_FORM_GNU_addr_index:
16677 if (reader->dwo_file == NULL)
16678 {
16679 /* For now flag a hard error.
16680 Later we can turn this into a complaint. */
16681 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16682 dwarf_form_name (form),
16683 bfd_get_filename (abfd));
16684 }
16685 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16686 info_ptr += bytes_read;
16687 break;
16688 case DW_FORM_GNU_str_index:
16689 if (reader->dwo_file == NULL)
16690 {
16691 /* For now flag a hard error.
16692 Later we can turn this into a complaint if warranted. */
16693 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16694 dwarf_form_name (form),
16695 bfd_get_filename (abfd));
16696 }
16697 {
16698 ULONGEST str_index =
16699 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16700
342587c4 16701 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16702 DW_STRING_IS_CANONICAL (attr) = 0;
16703 info_ptr += bytes_read;
16704 }
16705 break;
c906108c 16706 default:
8a3fe4f8 16707 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16708 dwarf_form_name (form),
16709 bfd_get_filename (abfd));
c906108c 16710 }
28e94949 16711
36586728 16712 /* Super hack. */
7771576e 16713 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16714 attr->form = DW_FORM_GNU_ref_alt;
16715
28e94949
JB
16716 /* We have seen instances where the compiler tried to emit a byte
16717 size attribute of -1 which ended up being encoded as an unsigned
16718 0xffffffff. Although 0xffffffff is technically a valid size value,
16719 an object of this size seems pretty unlikely so we can relatively
16720 safely treat these cases as if the size attribute was invalid and
16721 treat them as zero by default. */
16722 if (attr->name == DW_AT_byte_size
16723 && form == DW_FORM_data4
16724 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16725 {
16726 complaint
16727 (&symfile_complaints,
43bbcdc2
PH
16728 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16729 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16730 DW_UNSND (attr) = 0;
16731 }
28e94949 16732
c906108c
SS
16733 return info_ptr;
16734}
16735
a8329558
KW
16736/* Read an attribute described by an abbreviated attribute. */
16737
d521ce57 16738static const gdb_byte *
dee91e82
DE
16739read_attribute (const struct die_reader_specs *reader,
16740 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16741 const gdb_byte *info_ptr)
a8329558
KW
16742{
16743 attr->name = abbrev->name;
43988095
JK
16744 return read_attribute_value (reader, attr, abbrev->form,
16745 abbrev->implicit_const, info_ptr);
a8329558
KW
16746}
16747
0963b4bd 16748/* Read dwarf information from a buffer. */
c906108c
SS
16749
16750static unsigned int
a1855c1d 16751read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16752{
fe1b8b76 16753 return bfd_get_8 (abfd, buf);
c906108c
SS
16754}
16755
16756static int
a1855c1d 16757read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16758{
fe1b8b76 16759 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16760}
16761
16762static unsigned int
a1855c1d 16763read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16764{
fe1b8b76 16765 return bfd_get_16 (abfd, buf);
c906108c
SS
16766}
16767
21ae7a4d 16768static int
a1855c1d 16769read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16770{
16771 return bfd_get_signed_16 (abfd, buf);
16772}
16773
c906108c 16774static unsigned int
a1855c1d 16775read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16776{
fe1b8b76 16777 return bfd_get_32 (abfd, buf);
c906108c
SS
16778}
16779
21ae7a4d 16780static int
a1855c1d 16781read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16782{
16783 return bfd_get_signed_32 (abfd, buf);
16784}
16785
93311388 16786static ULONGEST
a1855c1d 16787read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16788{
fe1b8b76 16789 return bfd_get_64 (abfd, buf);
c906108c
SS
16790}
16791
16792static CORE_ADDR
d521ce57 16793read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16794 unsigned int *bytes_read)
c906108c 16795{
e7c27a73 16796 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16797 CORE_ADDR retval = 0;
16798
107d2387 16799 if (cu_header->signed_addr_p)
c906108c 16800 {
107d2387
AC
16801 switch (cu_header->addr_size)
16802 {
16803 case 2:
fe1b8b76 16804 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16805 break;
16806 case 4:
fe1b8b76 16807 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16808 break;
16809 case 8:
fe1b8b76 16810 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16811 break;
16812 default:
8e65ff28 16813 internal_error (__FILE__, __LINE__,
e2e0b3e5 16814 _("read_address: bad switch, signed [in module %s]"),
659b0389 16815 bfd_get_filename (abfd));
107d2387
AC
16816 }
16817 }
16818 else
16819 {
16820 switch (cu_header->addr_size)
16821 {
16822 case 2:
fe1b8b76 16823 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16824 break;
16825 case 4:
fe1b8b76 16826 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16827 break;
16828 case 8:
fe1b8b76 16829 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16830 break;
16831 default:
8e65ff28 16832 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16833 _("read_address: bad switch, "
16834 "unsigned [in module %s]"),
659b0389 16835 bfd_get_filename (abfd));
107d2387 16836 }
c906108c 16837 }
64367e0a 16838
107d2387
AC
16839 *bytes_read = cu_header->addr_size;
16840 return retval;
c906108c
SS
16841}
16842
f7ef9339 16843/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16844 specification allows the initial length to take up either 4 bytes
16845 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16846 bytes describe the length and all offsets will be 8 bytes in length
16847 instead of 4.
16848
f7ef9339
KB
16849 An older, non-standard 64-bit format is also handled by this
16850 function. The older format in question stores the initial length
16851 as an 8-byte quantity without an escape value. Lengths greater
16852 than 2^32 aren't very common which means that the initial 4 bytes
16853 is almost always zero. Since a length value of zero doesn't make
16854 sense for the 32-bit format, this initial zero can be considered to
16855 be an escape value which indicates the presence of the older 64-bit
16856 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16857 greater than 4GB. If it becomes necessary to handle lengths
16858 somewhat larger than 4GB, we could allow other small values (such
16859 as the non-sensical values of 1, 2, and 3) to also be used as
16860 escape values indicating the presence of the old format.
f7ef9339 16861
917c78fc
MK
16862 The value returned via bytes_read should be used to increment the
16863 relevant pointer after calling read_initial_length().
c764a876 16864
613e1657
KB
16865 [ Note: read_initial_length() and read_offset() are based on the
16866 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16867 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16868 from:
16869
f7ef9339 16870 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16871
613e1657
KB
16872 This document is only a draft and is subject to change. (So beware.)
16873
f7ef9339 16874 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16875 determined empirically by examining 64-bit ELF files produced by
16876 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16877
16878 - Kevin, July 16, 2002
613e1657
KB
16879 ] */
16880
16881static LONGEST
d521ce57 16882read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16883{
fe1b8b76 16884 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16885
dd373385 16886 if (length == 0xffffffff)
613e1657 16887 {
fe1b8b76 16888 length = bfd_get_64 (abfd, buf + 4);
613e1657 16889 *bytes_read = 12;
613e1657 16890 }
dd373385 16891 else if (length == 0)
f7ef9339 16892 {
dd373385 16893 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16894 length = bfd_get_64 (abfd, buf);
f7ef9339 16895 *bytes_read = 8;
f7ef9339 16896 }
613e1657
KB
16897 else
16898 {
16899 *bytes_read = 4;
613e1657
KB
16900 }
16901
c764a876
DE
16902 return length;
16903}
dd373385 16904
c764a876
DE
16905/* Cover function for read_initial_length.
16906 Returns the length of the object at BUF, and stores the size of the
16907 initial length in *BYTES_READ and stores the size that offsets will be in
16908 *OFFSET_SIZE.
16909 If the initial length size is not equivalent to that specified in
16910 CU_HEADER then issue a complaint.
16911 This is useful when reading non-comp-unit headers. */
dd373385 16912
c764a876 16913static LONGEST
d521ce57 16914read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
16915 const struct comp_unit_head *cu_header,
16916 unsigned int *bytes_read,
16917 unsigned int *offset_size)
16918{
16919 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16920
16921 gdb_assert (cu_header->initial_length_size == 4
16922 || cu_header->initial_length_size == 8
16923 || cu_header->initial_length_size == 12);
16924
16925 if (cu_header->initial_length_size != *bytes_read)
16926 complaint (&symfile_complaints,
16927 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 16928
c764a876 16929 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 16930 return length;
613e1657
KB
16931}
16932
16933/* Read an offset from the data stream. The size of the offset is
917c78fc 16934 given by cu_header->offset_size. */
613e1657
KB
16935
16936static LONGEST
d521ce57
TT
16937read_offset (bfd *abfd, const gdb_byte *buf,
16938 const struct comp_unit_head *cu_header,
891d2f0b 16939 unsigned int *bytes_read)
c764a876
DE
16940{
16941 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 16942
c764a876
DE
16943 *bytes_read = cu_header->offset_size;
16944 return offset;
16945}
16946
16947/* Read an offset from the data stream. */
16948
16949static LONGEST
d521ce57 16950read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
16951{
16952 LONGEST retval = 0;
16953
c764a876 16954 switch (offset_size)
613e1657
KB
16955 {
16956 case 4:
fe1b8b76 16957 retval = bfd_get_32 (abfd, buf);
613e1657
KB
16958 break;
16959 case 8:
fe1b8b76 16960 retval = bfd_get_64 (abfd, buf);
613e1657
KB
16961 break;
16962 default:
8e65ff28 16963 internal_error (__FILE__, __LINE__,
c764a876 16964 _("read_offset_1: bad switch [in module %s]"),
659b0389 16965 bfd_get_filename (abfd));
613e1657
KB
16966 }
16967
917c78fc 16968 return retval;
613e1657
KB
16969}
16970
d521ce57
TT
16971static const gdb_byte *
16972read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
16973{
16974 /* If the size of a host char is 8 bits, we can return a pointer
16975 to the buffer, otherwise we have to copy the data to a buffer
16976 allocated on the temporary obstack. */
4bdf3d34 16977 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 16978 return buf;
c906108c
SS
16979}
16980
d521ce57
TT
16981static const char *
16982read_direct_string (bfd *abfd, const gdb_byte *buf,
16983 unsigned int *bytes_read_ptr)
c906108c
SS
16984{
16985 /* If the size of a host char is 8 bits, we can return a pointer
16986 to the string, otherwise we have to copy the string to a buffer
16987 allocated on the temporary obstack. */
4bdf3d34 16988 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
16989 if (*buf == '\0')
16990 {
16991 *bytes_read_ptr = 1;
16992 return NULL;
16993 }
d521ce57
TT
16994 *bytes_read_ptr = strlen ((const char *) buf) + 1;
16995 return (const char *) buf;
4bdf3d34
JJ
16996}
16997
43988095
JK
16998/* Return pointer to string at section SECT offset STR_OFFSET with error
16999 reporting strings FORM_NAME and SECT_NAME. */
17000
d521ce57 17001static const char *
43988095
JK
17002read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17003 struct dwarf2_section_info *sect,
17004 const char *form_name,
17005 const char *sect_name)
17006{
17007 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17008 if (sect->buffer == NULL)
17009 error (_("%s used without %s section [in module %s]"),
17010 form_name, sect_name, bfd_get_filename (abfd));
17011 if (str_offset >= sect->size)
17012 error (_("%s pointing outside of %s section [in module %s]"),
17013 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17014 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17015 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17016 return NULL;
43988095
JK
17017 return (const char *) (sect->buffer + str_offset);
17018}
17019
17020/* Return pointer to string at .debug_str offset STR_OFFSET. */
17021
17022static const char *
17023read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17024{
17025 return read_indirect_string_at_offset_from (abfd, str_offset,
17026 &dwarf2_per_objfile->str,
17027 "DW_FORM_strp", ".debug_str");
17028}
17029
17030/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17031
17032static const char *
17033read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17034{
17035 return read_indirect_string_at_offset_from (abfd, str_offset,
17036 &dwarf2_per_objfile->line_str,
17037 "DW_FORM_line_strp",
17038 ".debug_line_str");
c906108c
SS
17039}
17040
36586728
TT
17041/* Read a string at offset STR_OFFSET in the .debug_str section from
17042 the .dwz file DWZ. Throw an error if the offset is too large. If
17043 the string consists of a single NUL byte, return NULL; otherwise
17044 return a pointer to the string. */
17045
d521ce57 17046static const char *
36586728
TT
17047read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17048{
17049 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17050
17051 if (dwz->str.buffer == NULL)
17052 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17053 "section [in module %s]"),
17054 bfd_get_filename (dwz->dwz_bfd));
17055 if (str_offset >= dwz->str.size)
17056 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17057 ".debug_str section [in module %s]"),
17058 bfd_get_filename (dwz->dwz_bfd));
17059 gdb_assert (HOST_CHAR_BIT == 8);
17060 if (dwz->str.buffer[str_offset] == '\0')
17061 return NULL;
d521ce57 17062 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17063}
17064
43988095
JK
17065/* Return pointer to string at .debug_str offset as read from BUF.
17066 BUF is assumed to be in a compilation unit described by CU_HEADER.
17067 Return *BYTES_READ_PTR count of bytes read from BUF. */
17068
d521ce57
TT
17069static const char *
17070read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17071 const struct comp_unit_head *cu_header,
17072 unsigned int *bytes_read_ptr)
17073{
17074 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17075
17076 return read_indirect_string_at_offset (abfd, str_offset);
17077}
17078
43988095
JK
17079/* Return pointer to string at .debug_line_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
17083static const char *
17084read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
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_line_string_at_offset (abfd, str_offset);
17091}
17092
17093ULONGEST
d521ce57 17094read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17095 unsigned int *bytes_read_ptr)
c906108c 17096{
12df843f 17097 ULONGEST result;
ce5d95e1 17098 unsigned int num_read;
870f88f7 17099 int shift;
c906108c
SS
17100 unsigned char byte;
17101
17102 result = 0;
17103 shift = 0;
17104 num_read = 0;
c906108c
SS
17105 while (1)
17106 {
fe1b8b76 17107 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17108 buf++;
17109 num_read++;
12df843f 17110 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17111 if ((byte & 128) == 0)
17112 {
17113 break;
17114 }
17115 shift += 7;
17116 }
17117 *bytes_read_ptr = num_read;
17118 return result;
17119}
17120
12df843f 17121static LONGEST
d521ce57
TT
17122read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17123 unsigned int *bytes_read_ptr)
c906108c 17124{
12df843f 17125 LONGEST result;
870f88f7 17126 int shift, num_read;
c906108c
SS
17127 unsigned char byte;
17128
17129 result = 0;
17130 shift = 0;
c906108c 17131 num_read = 0;
c906108c
SS
17132 while (1)
17133 {
fe1b8b76 17134 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17135 buf++;
17136 num_read++;
12df843f 17137 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17138 shift += 7;
17139 if ((byte & 128) == 0)
17140 {
17141 break;
17142 }
17143 }
77e0b926 17144 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17145 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17146 *bytes_read_ptr = num_read;
17147 return result;
17148}
17149
3019eac3
DE
17150/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17151 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17152 ADDR_SIZE is the size of addresses from the CU header. */
17153
17154static CORE_ADDR
17155read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17156{
17157 struct objfile *objfile = dwarf2_per_objfile->objfile;
17158 bfd *abfd = objfile->obfd;
17159 const gdb_byte *info_ptr;
17160
17161 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17162 if (dwarf2_per_objfile->addr.buffer == NULL)
17163 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17164 objfile_name (objfile));
3019eac3
DE
17165 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17166 error (_("DW_FORM_addr_index pointing outside of "
17167 ".debug_addr section [in module %s]"),
4262abfb 17168 objfile_name (objfile));
3019eac3
DE
17169 info_ptr = (dwarf2_per_objfile->addr.buffer
17170 + addr_base + addr_index * addr_size);
17171 if (addr_size == 4)
17172 return bfd_get_32 (abfd, info_ptr);
17173 else
17174 return bfd_get_64 (abfd, info_ptr);
17175}
17176
17177/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17178
17179static CORE_ADDR
17180read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17181{
17182 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17183}
17184
17185/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17186
17187static CORE_ADDR
d521ce57 17188read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17189 unsigned int *bytes_read)
17190{
17191 bfd *abfd = cu->objfile->obfd;
17192 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17193
17194 return read_addr_index (cu, addr_index);
17195}
17196
17197/* Data structure to pass results from dwarf2_read_addr_index_reader
17198 back to dwarf2_read_addr_index. */
17199
17200struct dwarf2_read_addr_index_data
17201{
17202 ULONGEST addr_base;
17203 int addr_size;
17204};
17205
17206/* die_reader_func for dwarf2_read_addr_index. */
17207
17208static void
17209dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17210 const gdb_byte *info_ptr,
3019eac3
DE
17211 struct die_info *comp_unit_die,
17212 int has_children,
17213 void *data)
17214{
17215 struct dwarf2_cu *cu = reader->cu;
17216 struct dwarf2_read_addr_index_data *aidata =
17217 (struct dwarf2_read_addr_index_data *) data;
17218
17219 aidata->addr_base = cu->addr_base;
17220 aidata->addr_size = cu->header.addr_size;
17221}
17222
17223/* Given an index in .debug_addr, fetch the value.
17224 NOTE: This can be called during dwarf expression evaluation,
17225 long after the debug information has been read, and thus per_cu->cu
17226 may no longer exist. */
17227
17228CORE_ADDR
17229dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17230 unsigned int addr_index)
17231{
17232 struct objfile *objfile = per_cu->objfile;
17233 struct dwarf2_cu *cu = per_cu->cu;
17234 ULONGEST addr_base;
17235 int addr_size;
17236
17237 /* This is intended to be called from outside this file. */
17238 dw2_setup (objfile);
17239
17240 /* We need addr_base and addr_size.
17241 If we don't have PER_CU->cu, we have to get it.
17242 Nasty, but the alternative is storing the needed info in PER_CU,
17243 which at this point doesn't seem justified: it's not clear how frequently
17244 it would get used and it would increase the size of every PER_CU.
17245 Entry points like dwarf2_per_cu_addr_size do a similar thing
17246 so we're not in uncharted territory here.
17247 Alas we need to be a bit more complicated as addr_base is contained
17248 in the DIE.
17249
17250 We don't need to read the entire CU(/TU).
17251 We just need the header and top level die.
a1b64ce1 17252
3019eac3 17253 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17254 For now we skip this optimization. */
3019eac3
DE
17255
17256 if (cu != NULL)
17257 {
17258 addr_base = cu->addr_base;
17259 addr_size = cu->header.addr_size;
17260 }
17261 else
17262 {
17263 struct dwarf2_read_addr_index_data aidata;
17264
a1b64ce1
DE
17265 /* Note: We can't use init_cutu_and_read_dies_simple here,
17266 we need addr_base. */
17267 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17268 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17269 addr_base = aidata.addr_base;
17270 addr_size = aidata.addr_size;
17271 }
17272
17273 return read_addr_index_1 (addr_index, addr_base, addr_size);
17274}
17275
57d63ce2
DE
17276/* Given a DW_FORM_GNU_str_index, fetch the string.
17277 This is only used by the Fission support. */
3019eac3 17278
d521ce57 17279static const char *
342587c4 17280read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17281{
17282 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17283 const char *objf_name = objfile_name (objfile);
3019eac3 17284 bfd *abfd = objfile->obfd;
342587c4 17285 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17286 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17287 struct dwarf2_section_info *str_offsets_section =
17288 &reader->dwo_file->sections.str_offsets;
d521ce57 17289 const gdb_byte *info_ptr;
3019eac3 17290 ULONGEST str_offset;
57d63ce2 17291 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17292
73869dc2
DE
17293 dwarf2_read_section (objfile, str_section);
17294 dwarf2_read_section (objfile, str_offsets_section);
17295 if (str_section->buffer == NULL)
57d63ce2 17296 error (_("%s used without .debug_str.dwo section"
3019eac3 17297 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17298 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17299 if (str_offsets_section->buffer == NULL)
57d63ce2 17300 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 17301 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17302 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17303 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17304 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 17305 " section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17306 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17307 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17308 + str_index * cu->header.offset_size);
17309 if (cu->header.offset_size == 4)
17310 str_offset = bfd_get_32 (abfd, info_ptr);
17311 else
17312 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17313 if (str_offset >= str_section->size)
57d63ce2 17314 error (_("Offset from %s pointing outside of"
3019eac3 17315 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17316 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17317 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17318}
17319
3019eac3
DE
17320/* Return the length of an LEB128 number in BUF. */
17321
17322static int
17323leb128_size (const gdb_byte *buf)
17324{
17325 const gdb_byte *begin = buf;
17326 gdb_byte byte;
17327
17328 while (1)
17329 {
17330 byte = *buf++;
17331 if ((byte & 128) == 0)
17332 return buf - begin;
17333 }
17334}
17335
c906108c 17336static void
e142c38c 17337set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17338{
17339 switch (lang)
17340 {
17341 case DW_LANG_C89:
76bee0cc 17342 case DW_LANG_C99:
0cfd832f 17343 case DW_LANG_C11:
c906108c 17344 case DW_LANG_C:
d1be3247 17345 case DW_LANG_UPC:
e142c38c 17346 cu->language = language_c;
c906108c 17347 break;
9c37b5ae 17348 case DW_LANG_Java:
c906108c 17349 case DW_LANG_C_plus_plus:
0cfd832f
MW
17350 case DW_LANG_C_plus_plus_11:
17351 case DW_LANG_C_plus_plus_14:
e142c38c 17352 cu->language = language_cplus;
c906108c 17353 break;
6aecb9c2
JB
17354 case DW_LANG_D:
17355 cu->language = language_d;
17356 break;
c906108c
SS
17357 case DW_LANG_Fortran77:
17358 case DW_LANG_Fortran90:
b21b22e0 17359 case DW_LANG_Fortran95:
f7de9aab
MW
17360 case DW_LANG_Fortran03:
17361 case DW_LANG_Fortran08:
e142c38c 17362 cu->language = language_fortran;
c906108c 17363 break;
a766d390
DE
17364 case DW_LANG_Go:
17365 cu->language = language_go;
17366 break;
c906108c 17367 case DW_LANG_Mips_Assembler:
e142c38c 17368 cu->language = language_asm;
c906108c
SS
17369 break;
17370 case DW_LANG_Ada83:
8aaf0b47 17371 case DW_LANG_Ada95:
bc5f45f8
JB
17372 cu->language = language_ada;
17373 break;
72019c9c
GM
17374 case DW_LANG_Modula2:
17375 cu->language = language_m2;
17376 break;
fe8e67fd
PM
17377 case DW_LANG_Pascal83:
17378 cu->language = language_pascal;
17379 break;
22566fbd
DJ
17380 case DW_LANG_ObjC:
17381 cu->language = language_objc;
17382 break;
c44af4eb
TT
17383 case DW_LANG_Rust:
17384 case DW_LANG_Rust_old:
17385 cu->language = language_rust;
17386 break;
c906108c
SS
17387 case DW_LANG_Cobol74:
17388 case DW_LANG_Cobol85:
c906108c 17389 default:
e142c38c 17390 cu->language = language_minimal;
c906108c
SS
17391 break;
17392 }
e142c38c 17393 cu->language_defn = language_def (cu->language);
c906108c
SS
17394}
17395
17396/* Return the named attribute or NULL if not there. */
17397
17398static struct attribute *
e142c38c 17399dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17400{
a48e046c 17401 for (;;)
c906108c 17402 {
a48e046c
TT
17403 unsigned int i;
17404 struct attribute *spec = NULL;
17405
17406 for (i = 0; i < die->num_attrs; ++i)
17407 {
17408 if (die->attrs[i].name == name)
17409 return &die->attrs[i];
17410 if (die->attrs[i].name == DW_AT_specification
17411 || die->attrs[i].name == DW_AT_abstract_origin)
17412 spec = &die->attrs[i];
17413 }
17414
17415 if (!spec)
17416 break;
c906108c 17417
f2f0e013 17418 die = follow_die_ref (die, spec, &cu);
f2f0e013 17419 }
c5aa993b 17420
c906108c
SS
17421 return NULL;
17422}
17423
348e048f
DE
17424/* Return the named attribute or NULL if not there,
17425 but do not follow DW_AT_specification, etc.
17426 This is for use in contexts where we're reading .debug_types dies.
17427 Following DW_AT_specification, DW_AT_abstract_origin will take us
17428 back up the chain, and we want to go down. */
17429
17430static struct attribute *
45e58e77 17431dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17432{
17433 unsigned int i;
17434
17435 for (i = 0; i < die->num_attrs; ++i)
17436 if (die->attrs[i].name == name)
17437 return &die->attrs[i];
17438
17439 return NULL;
17440}
17441
7d45c7c3
KB
17442/* Return the string associated with a string-typed attribute, or NULL if it
17443 is either not found or is of an incorrect type. */
17444
17445static const char *
17446dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17447{
17448 struct attribute *attr;
17449 const char *str = NULL;
17450
17451 attr = dwarf2_attr (die, name, cu);
17452
17453 if (attr != NULL)
17454 {
43988095
JK
17455 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17456 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17457 str = DW_STRING (attr);
17458 else
17459 complaint (&symfile_complaints,
17460 _("string type expected for attribute %s for "
17461 "DIE at 0x%x in module %s"),
17462 dwarf_attr_name (name), die->offset.sect_off,
17463 objfile_name (cu->objfile));
17464 }
17465
17466 return str;
17467}
17468
05cf31d1
JB
17469/* Return non-zero iff the attribute NAME is defined for the given DIE,
17470 and holds a non-zero value. This function should only be used for
2dc7f7b3 17471 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17472
17473static int
17474dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17475{
17476 struct attribute *attr = dwarf2_attr (die, name, cu);
17477
17478 return (attr && DW_UNSND (attr));
17479}
17480
3ca72b44 17481static int
e142c38c 17482die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17483{
05cf31d1
JB
17484 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17485 which value is non-zero. However, we have to be careful with
17486 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17487 (via dwarf2_flag_true_p) follows this attribute. So we may
17488 end up accidently finding a declaration attribute that belongs
17489 to a different DIE referenced by the specification attribute,
17490 even though the given DIE does not have a declaration attribute. */
17491 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17492 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17493}
17494
63d06c5c 17495/* Return the die giving the specification for DIE, if there is
f2f0e013 17496 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17497 containing the return value on output. If there is no
17498 specification, but there is an abstract origin, that is
17499 returned. */
63d06c5c
DC
17500
17501static struct die_info *
f2f0e013 17502die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17503{
f2f0e013
DJ
17504 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17505 *spec_cu);
63d06c5c 17506
edb3359d
DJ
17507 if (spec_attr == NULL)
17508 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17509
63d06c5c
DC
17510 if (spec_attr == NULL)
17511 return NULL;
17512 else
f2f0e013 17513 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17514}
c906108c 17515
debd256d 17516/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
17517 refers to.
17518 NOTE: This is also used as a "cleanup" function. */
17519
debd256d
JB
17520static void
17521free_line_header (struct line_header *lh)
17522{
17523 if (lh->standard_opcode_lengths)
a8bc7b56 17524 xfree (lh->standard_opcode_lengths);
debd256d
JB
17525
17526 /* Remember that all the lh->file_names[i].name pointers are
17527 pointers into debug_line_buffer, and don't need to be freed. */
17528 if (lh->file_names)
a8bc7b56 17529 xfree (lh->file_names);
debd256d
JB
17530
17531 /* Similarly for the include directory names. */
17532 if (lh->include_dirs)
a8bc7b56 17533 xfree (lh->include_dirs);
debd256d 17534
a8bc7b56 17535 xfree (lh);
debd256d
JB
17536}
17537
527f3840
JK
17538/* Stub for free_line_header to match void * callback types. */
17539
17540static void
17541free_line_header_voidp (void *arg)
17542{
9a3c8263 17543 struct line_header *lh = (struct line_header *) arg;
527f3840
JK
17544
17545 free_line_header (lh);
17546}
17547
debd256d 17548/* Add an entry to LH's include directory table. */
ae2de4f8 17549
debd256d 17550static void
d521ce57 17551add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 17552{
27e0867f
DE
17553 if (dwarf_line_debug >= 2)
17554 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17555 lh->num_include_dirs + 1, include_dir);
17556
debd256d
JB
17557 /* Grow the array if necessary. */
17558 if (lh->include_dirs_size == 0)
c5aa993b 17559 {
debd256d 17560 lh->include_dirs_size = 1; /* for testing */
8d749320 17561 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
debd256d
JB
17562 }
17563 else if (lh->num_include_dirs >= lh->include_dirs_size)
17564 {
17565 lh->include_dirs_size *= 2;
8d749320
SM
17566 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17567 lh->include_dirs_size);
c5aa993b 17568 }
c906108c 17569
debd256d
JB
17570 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17571}
6e70227d 17572
debd256d 17573/* Add an entry to LH's file name table. */
ae2de4f8 17574
debd256d
JB
17575static void
17576add_file_name (struct line_header *lh,
d521ce57 17577 const char *name,
debd256d
JB
17578 unsigned int dir_index,
17579 unsigned int mod_time,
17580 unsigned int length)
17581{
17582 struct file_entry *fe;
17583
27e0867f
DE
17584 if (dwarf_line_debug >= 2)
17585 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17586 lh->num_file_names + 1, name);
17587
debd256d
JB
17588 /* Grow the array if necessary. */
17589 if (lh->file_names_size == 0)
17590 {
17591 lh->file_names_size = 1; /* for testing */
8d749320 17592 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
debd256d
JB
17593 }
17594 else if (lh->num_file_names >= lh->file_names_size)
17595 {
17596 lh->file_names_size *= 2;
224c3ddb
SM
17597 lh->file_names
17598 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
debd256d
JB
17599 }
17600
17601 fe = &lh->file_names[lh->num_file_names++];
17602 fe->name = name;
17603 fe->dir_index = dir_index;
17604 fe->mod_time = mod_time;
17605 fe->length = length;
aaa75496 17606 fe->included_p = 0;
cb1df416 17607 fe->symtab = NULL;
debd256d 17608}
6e70227d 17609
83769d0b 17610/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17611
17612static struct dwarf2_section_info *
17613get_debug_line_section (struct dwarf2_cu *cu)
17614{
17615 struct dwarf2_section_info *section;
17616
17617 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17618 DWO file. */
17619 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17620 section = &cu->dwo_unit->dwo_file->sections.line;
17621 else if (cu->per_cu->is_dwz)
17622 {
17623 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17624
17625 section = &dwz->line;
17626 }
17627 else
17628 section = &dwarf2_per_objfile->line;
17629
17630 return section;
17631}
17632
43988095
JK
17633/* Forwarding function for read_formatted_entries. */
17634
17635static void
17636add_include_dir_stub (struct line_header *lh, const char *name,
17637 unsigned int dir_index, unsigned int mod_time,
17638 unsigned int length)
17639{
17640 add_include_dir (lh, name);
17641}
17642
17643/* Read directory or file name entry format, starting with byte of
17644 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17645 entries count and the entries themselves in the described entry
17646 format. */
17647
17648static void
17649read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17650 struct line_header *lh,
17651 const struct comp_unit_head *cu_header,
17652 void (*callback) (struct line_header *lh,
17653 const char *name,
17654 unsigned int dir_index,
17655 unsigned int mod_time,
17656 unsigned int length))
17657{
17658 gdb_byte format_count, formati;
17659 ULONGEST data_count, datai;
17660 const gdb_byte *buf = *bufp;
17661 const gdb_byte *format_header_data;
17662 int i;
17663 unsigned int bytes_read;
17664
17665 format_count = read_1_byte (abfd, buf);
17666 buf += 1;
17667 format_header_data = buf;
17668 for (formati = 0; formati < format_count; formati++)
17669 {
17670 read_unsigned_leb128 (abfd, buf, &bytes_read);
17671 buf += bytes_read;
17672 read_unsigned_leb128 (abfd, buf, &bytes_read);
17673 buf += bytes_read;
17674 }
17675
17676 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17677 buf += bytes_read;
17678 for (datai = 0; datai < data_count; datai++)
17679 {
17680 const gdb_byte *format = format_header_data;
17681 struct file_entry fe;
17682
17683 memset (&fe, 0, sizeof (fe));
17684
17685 for (formati = 0; formati < format_count; formati++)
17686 {
17687 ULONGEST content_type, form;
17688 const char *string_trash;
17689 const char **stringp = &string_trash;
17690 unsigned int uint_trash, *uintp = &uint_trash;
17691
17692 content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17693 format += bytes_read;
17694 switch (content_type)
17695 {
17696 case DW_LNCT_path:
17697 stringp = &fe.name;
17698 break;
17699 case DW_LNCT_directory_index:
17700 uintp = &fe.dir_index;
17701 break;
17702 case DW_LNCT_timestamp:
17703 uintp = &fe.mod_time;
17704 break;
17705 case DW_LNCT_size:
17706 uintp = &fe.length;
17707 break;
17708 case DW_LNCT_MD5:
17709 break;
17710 default:
17711 complaint (&symfile_complaints,
17712 _("Unknown format content type %s"),
17713 pulongest (content_type));
17714 }
17715
17716 form = read_unsigned_leb128 (abfd, format, &bytes_read);
17717 format += bytes_read;
17718 switch (form)
17719 {
17720 case DW_FORM_string:
17721 *stringp = read_direct_string (abfd, buf, &bytes_read);
17722 buf += bytes_read;
17723 break;
17724
17725 case DW_FORM_line_strp:
17726 *stringp = read_indirect_line_string (abfd, buf, cu_header, &bytes_read);
17727 buf += bytes_read;
17728 break;
17729
17730 case DW_FORM_data1:
17731 *uintp = read_1_byte (abfd, buf);
17732 buf += 1;
17733 break;
17734
17735 case DW_FORM_data2:
17736 *uintp = read_2_bytes (abfd, buf);
17737 buf += 2;
17738 break;
17739
17740 case DW_FORM_data4:
17741 *uintp = read_4_bytes (abfd, buf);
17742 buf += 4;
17743 break;
17744
17745 case DW_FORM_data8:
17746 *uintp = read_8_bytes (abfd, buf);
17747 buf += 8;
17748 break;
17749
17750 case DW_FORM_udata:
17751 *uintp = read_unsigned_leb128 (abfd, buf, &bytes_read);
17752 buf += bytes_read;
17753 break;
17754
17755 case DW_FORM_block:
17756 /* It is valid only for DW_LNCT_timestamp which is ignored by
17757 current GDB. */
17758 break;
17759 }
17760 }
17761
17762 callback (lh, fe.name, fe.dir_index, fe.mod_time, fe.length);
17763 }
17764
17765 *bufp = buf;
17766}
17767
debd256d 17768/* Read the statement program header starting at OFFSET in
3019eac3 17769 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17770 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17771 Returns NULL if there is a problem reading the header, e.g., if it
17772 has a version we don't understand.
debd256d
JB
17773
17774 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17775 the returned object point into the dwarf line section buffer,
17776 and must not be freed. */
ae2de4f8 17777
debd256d 17778static struct line_header *
3019eac3 17779dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
17780{
17781 struct cleanup *back_to;
17782 struct line_header *lh;
d521ce57 17783 const gdb_byte *line_ptr;
c764a876 17784 unsigned int bytes_read, offset_size;
debd256d 17785 int i;
d521ce57 17786 const char *cur_dir, *cur_file;
3019eac3
DE
17787 struct dwarf2_section_info *section;
17788 bfd *abfd;
17789
36586728 17790 section = get_debug_line_section (cu);
3019eac3
DE
17791 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17792 if (section->buffer == NULL)
debd256d 17793 {
3019eac3
DE
17794 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17795 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17796 else
17797 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17798 return 0;
17799 }
17800
fceca515
DE
17801 /* We can't do this until we know the section is non-empty.
17802 Only then do we know we have such a section. */
a32a8923 17803 abfd = get_section_bfd_owner (section);
fceca515 17804
a738430d
MK
17805 /* Make sure that at least there's room for the total_length field.
17806 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 17807 if (offset + 4 >= section->size)
debd256d 17808 {
4d3c2250 17809 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17810 return 0;
17811 }
17812
8d749320 17813 lh = XNEW (struct line_header);
debd256d
JB
17814 memset (lh, 0, sizeof (*lh));
17815 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17816 (void *) lh);
17817
527f3840
JK
17818 lh->offset.sect_off = offset;
17819 lh->offset_in_dwz = cu->per_cu->is_dwz;
17820
3019eac3 17821 line_ptr = section->buffer + offset;
debd256d 17822
a738430d 17823 /* Read in the header. */
6e70227d 17824 lh->total_length =
c764a876
DE
17825 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17826 &bytes_read, &offset_size);
debd256d 17827 line_ptr += bytes_read;
3019eac3 17828 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17829 {
4d3c2250 17830 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 17831 do_cleanups (back_to);
debd256d
JB
17832 return 0;
17833 }
17834 lh->statement_program_end = line_ptr + lh->total_length;
17835 lh->version = read_2_bytes (abfd, line_ptr);
17836 line_ptr += 2;
43988095 17837 if (lh->version > 5)
cd366ee8
DE
17838 {
17839 /* This is a version we don't understand. The format could have
17840 changed in ways we don't handle properly so just punt. */
17841 complaint (&symfile_complaints,
17842 _("unsupported version in .debug_line section"));
17843 return NULL;
17844 }
43988095
JK
17845 if (lh->version >= 5)
17846 {
17847 gdb_byte segment_selector_size;
17848
17849 /* Skip address size. */
17850 read_1_byte (abfd, line_ptr);
17851 line_ptr += 1;
17852
17853 segment_selector_size = read_1_byte (abfd, line_ptr);
17854 line_ptr += 1;
17855 if (segment_selector_size != 0)
17856 {
17857 complaint (&symfile_complaints,
17858 _("unsupported segment selector size %u "
17859 "in .debug_line section"),
17860 segment_selector_size);
17861 return NULL;
17862 }
17863 }
c764a876
DE
17864 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17865 line_ptr += offset_size;
debd256d
JB
17866 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17867 line_ptr += 1;
2dc7f7b3
TT
17868 if (lh->version >= 4)
17869 {
17870 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17871 line_ptr += 1;
17872 }
17873 else
17874 lh->maximum_ops_per_instruction = 1;
17875
17876 if (lh->maximum_ops_per_instruction == 0)
17877 {
17878 lh->maximum_ops_per_instruction = 1;
17879 complaint (&symfile_complaints,
3e43a32a
MS
17880 _("invalid maximum_ops_per_instruction "
17881 "in `.debug_line' section"));
2dc7f7b3
TT
17882 }
17883
debd256d
JB
17884 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17885 line_ptr += 1;
17886 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17887 line_ptr += 1;
17888 lh->line_range = read_1_byte (abfd, line_ptr);
17889 line_ptr += 1;
17890 lh->opcode_base = read_1_byte (abfd, line_ptr);
17891 line_ptr += 1;
8d749320 17892 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
debd256d
JB
17893
17894 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17895 for (i = 1; i < lh->opcode_base; ++i)
17896 {
17897 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17898 line_ptr += 1;
17899 }
17900
43988095 17901 if (lh->version >= 5)
debd256d 17902 {
43988095
JK
17903 /* Read directory table. */
17904 read_formatted_entries (abfd, &line_ptr, lh, &cu->header,
17905 add_include_dir_stub);
debd256d 17906
43988095
JK
17907 /* Read file name table. */
17908 read_formatted_entries (abfd, &line_ptr, lh, &cu->header, add_file_name);
17909 }
17910 else
debd256d 17911 {
43988095
JK
17912 /* Read directory table. */
17913 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17914 {
17915 line_ptr += bytes_read;
17916 add_include_dir (lh, cur_dir);
17917 }
debd256d
JB
17918 line_ptr += bytes_read;
17919
43988095
JK
17920 /* Read file name table. */
17921 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17922 {
17923 unsigned int dir_index, mod_time, length;
17924
17925 line_ptr += bytes_read;
17926 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17927 line_ptr += bytes_read;
17928 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17929 line_ptr += bytes_read;
17930 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17931 line_ptr += bytes_read;
17932
17933 add_file_name (lh, cur_file, dir_index, mod_time, length);
17934 }
17935 line_ptr += bytes_read;
debd256d 17936 }
6e70227d 17937 lh->statement_program_start = line_ptr;
debd256d 17938
3019eac3 17939 if (line_ptr > (section->buffer + section->size))
4d3c2250 17940 complaint (&symfile_complaints,
3e43a32a
MS
17941 _("line number info header doesn't "
17942 "fit in `.debug_line' section"));
debd256d
JB
17943
17944 discard_cleanups (back_to);
17945 return lh;
17946}
c906108c 17947
c6da4cef
DE
17948/* Subroutine of dwarf_decode_lines to simplify it.
17949 Return the file name of the psymtab for included file FILE_INDEX
17950 in line header LH of PST.
17951 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17952 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17953 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17954
17955 The function creates dangling cleanup registration. */
c6da4cef 17956
d521ce57 17957static const char *
c6da4cef
DE
17958psymtab_include_file_name (const struct line_header *lh, int file_index,
17959 const struct partial_symtab *pst,
17960 const char *comp_dir)
17961{
17962 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
17963 const char *include_name = fe.name;
17964 const char *include_name_to_compare = include_name;
17965 const char *dir_name = NULL;
72b9f47f
TT
17966 const char *pst_filename;
17967 char *copied_name = NULL;
c6da4cef
DE
17968 int file_is_pst;
17969
afa6c9ab 17970 if (fe.dir_index && lh->include_dirs != NULL)
c6da4cef
DE
17971 dir_name = lh->include_dirs[fe.dir_index - 1];
17972
17973 if (!IS_ABSOLUTE_PATH (include_name)
17974 && (dir_name != NULL || comp_dir != NULL))
17975 {
17976 /* Avoid creating a duplicate psymtab for PST.
17977 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17978 Before we do the comparison, however, we need to account
17979 for DIR_NAME and COMP_DIR.
17980 First prepend dir_name (if non-NULL). If we still don't
17981 have an absolute path prepend comp_dir (if non-NULL).
17982 However, the directory we record in the include-file's
17983 psymtab does not contain COMP_DIR (to match the
17984 corresponding symtab(s)).
17985
17986 Example:
17987
17988 bash$ cd /tmp
17989 bash$ gcc -g ./hello.c
17990 include_name = "hello.c"
17991 dir_name = "."
17992 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
17993 DW_AT_name = "./hello.c"
17994
17995 */
c6da4cef
DE
17996
17997 if (dir_name != NULL)
17998 {
d521ce57
TT
17999 char *tem = concat (dir_name, SLASH_STRING,
18000 include_name, (char *)NULL);
18001
18002 make_cleanup (xfree, tem);
18003 include_name = tem;
c6da4cef 18004 include_name_to_compare = include_name;
c6da4cef
DE
18005 }
18006 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18007 {
d521ce57
TT
18008 char *tem = concat (comp_dir, SLASH_STRING,
18009 include_name, (char *)NULL);
18010
18011 make_cleanup (xfree, tem);
18012 include_name_to_compare = tem;
c6da4cef
DE
18013 }
18014 }
18015
18016 pst_filename = pst->filename;
18017 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18018 {
72b9f47f
TT
18019 copied_name = concat (pst->dirname, SLASH_STRING,
18020 pst_filename, (char *)NULL);
18021 pst_filename = copied_name;
c6da4cef
DE
18022 }
18023
1e3fad37 18024 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18025
72b9f47f
TT
18026 if (copied_name != NULL)
18027 xfree (copied_name);
c6da4cef
DE
18028
18029 if (file_is_pst)
18030 return NULL;
18031 return include_name;
18032}
18033
d9b3de22
DE
18034/* State machine to track the state of the line number program. */
18035
18036typedef struct
18037{
18038 /* These are part of the standard DWARF line number state machine. */
18039
18040 unsigned char op_index;
18041 unsigned int file;
18042 unsigned int line;
18043 CORE_ADDR address;
18044 int is_stmt;
18045 unsigned int discriminator;
18046
18047 /* Additional bits of state we need to track. */
18048
18049 /* The last file that we called dwarf2_start_subfile for.
18050 This is only used for TLLs. */
18051 unsigned int last_file;
18052 /* The last file a line number was recorded for. */
18053 struct subfile *last_subfile;
18054
18055 /* The function to call to record a line. */
18056 record_line_ftype *record_line;
18057
18058 /* The last line number that was recorded, used to coalesce
18059 consecutive entries for the same line. This can happen, for
18060 example, when discriminators are present. PR 17276. */
18061 unsigned int last_line;
18062 int line_has_non_zero_discriminator;
18063} lnp_state_machine;
18064
18065/* There's a lot of static state to pass to dwarf_record_line.
18066 This keeps it all together. */
18067
18068typedef struct
18069{
18070 /* The gdbarch. */
18071 struct gdbarch *gdbarch;
18072
18073 /* The line number header. */
18074 struct line_header *line_header;
18075
18076 /* Non-zero if we're recording lines.
18077 Otherwise we're building partial symtabs and are just interested in
18078 finding include files mentioned by the line number program. */
18079 int record_lines_p;
18080} lnp_reader_state;
18081
c91513d8
PP
18082/* Ignore this record_line request. */
18083
18084static void
18085noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18086{
18087 return;
18088}
18089
a05a36a5
DE
18090/* Return non-zero if we should add LINE to the line number table.
18091 LINE is the line to add, LAST_LINE is the last line that was added,
18092 LAST_SUBFILE is the subfile for LAST_LINE.
18093 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18094 had a non-zero discriminator.
18095
18096 We have to be careful in the presence of discriminators.
18097 E.g., for this line:
18098
18099 for (i = 0; i < 100000; i++);
18100
18101 clang can emit four line number entries for that one line,
18102 each with a different discriminator.
18103 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18104
18105 However, we want gdb to coalesce all four entries into one.
18106 Otherwise the user could stepi into the middle of the line and
18107 gdb would get confused about whether the pc really was in the
18108 middle of the line.
18109
18110 Things are further complicated by the fact that two consecutive
18111 line number entries for the same line is a heuristic used by gcc
18112 to denote the end of the prologue. So we can't just discard duplicate
18113 entries, we have to be selective about it. The heuristic we use is
18114 that we only collapse consecutive entries for the same line if at least
18115 one of those entries has a non-zero discriminator. PR 17276.
18116
18117 Note: Addresses in the line number state machine can never go backwards
18118 within one sequence, thus this coalescing is ok. */
18119
18120static int
18121dwarf_record_line_p (unsigned int line, unsigned int last_line,
18122 int line_has_non_zero_discriminator,
18123 struct subfile *last_subfile)
18124{
18125 if (current_subfile != last_subfile)
18126 return 1;
18127 if (line != last_line)
18128 return 1;
18129 /* Same line for the same file that we've seen already.
18130 As a last check, for pr 17276, only record the line if the line
18131 has never had a non-zero discriminator. */
18132 if (!line_has_non_zero_discriminator)
18133 return 1;
18134 return 0;
18135}
18136
252a6764
DE
18137/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18138 in the line table of subfile SUBFILE. */
18139
18140static void
d9b3de22
DE
18141dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18142 unsigned int line, CORE_ADDR address,
18143 record_line_ftype p_record_line)
252a6764
DE
18144{
18145 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18146
27e0867f
DE
18147 if (dwarf_line_debug)
18148 {
18149 fprintf_unfiltered (gdb_stdlog,
18150 "Recording line %u, file %s, address %s\n",
18151 line, lbasename (subfile->name),
18152 paddress (gdbarch, address));
18153 }
18154
d5962de5 18155 (*p_record_line) (subfile, line, addr);
252a6764
DE
18156}
18157
18158/* Subroutine of dwarf_decode_lines_1 to simplify it.
18159 Mark the end of a set of line number records.
d9b3de22 18160 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18161 If SUBFILE is NULL the request is ignored. */
18162
18163static void
18164dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18165 CORE_ADDR address, record_line_ftype p_record_line)
18166{
27e0867f
DE
18167 if (subfile == NULL)
18168 return;
18169
18170 if (dwarf_line_debug)
18171 {
18172 fprintf_unfiltered (gdb_stdlog,
18173 "Finishing current line, file %s, address %s\n",
18174 lbasename (subfile->name),
18175 paddress (gdbarch, address));
18176 }
18177
d9b3de22
DE
18178 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18179}
18180
18181/* Record the line in STATE.
18182 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18183
18184static void
18185dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18186 int end_sequence)
18187{
18188 const struct line_header *lh = reader->line_header;
18189 unsigned int file, line, discriminator;
18190 int is_stmt;
18191
18192 file = state->file;
18193 line = state->line;
18194 is_stmt = state->is_stmt;
18195 discriminator = state->discriminator;
18196
18197 if (dwarf_line_debug)
18198 {
18199 fprintf_unfiltered (gdb_stdlog,
18200 "Processing actual line %u: file %u,"
18201 " address %s, is_stmt %u, discrim %u\n",
18202 line, file,
18203 paddress (reader->gdbarch, state->address),
18204 is_stmt, discriminator);
18205 }
18206
18207 if (file == 0 || file - 1 >= lh->num_file_names)
18208 dwarf2_debug_line_missing_file_complaint ();
18209 /* For now we ignore lines not starting on an instruction boundary.
18210 But not when processing end_sequence for compatibility with the
18211 previous version of the code. */
18212 else if (state->op_index == 0 || end_sequence)
18213 {
18214 lh->file_names[file - 1].included_p = 1;
18215 if (reader->record_lines_p && is_stmt)
18216 {
e815d2d2 18217 if (state->last_subfile != current_subfile || end_sequence)
d9b3de22
DE
18218 {
18219 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18220 state->address, state->record_line);
18221 }
18222
18223 if (!end_sequence)
18224 {
18225 if (dwarf_record_line_p (line, state->last_line,
18226 state->line_has_non_zero_discriminator,
18227 state->last_subfile))
18228 {
18229 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18230 line, state->address,
18231 state->record_line);
18232 }
18233 state->last_subfile = current_subfile;
18234 state->last_line = line;
18235 }
18236 }
18237 }
18238}
18239
18240/* Initialize STATE for the start of a line number program. */
18241
18242static void
18243init_lnp_state_machine (lnp_state_machine *state,
18244 const lnp_reader_state *reader)
18245{
18246 memset (state, 0, sizeof (*state));
18247
18248 /* Just starting, there is no "last file". */
18249 state->last_file = 0;
18250 state->last_subfile = NULL;
18251
18252 state->record_line = record_line;
18253
18254 state->last_line = 0;
18255 state->line_has_non_zero_discriminator = 0;
18256
18257 /* Initialize these according to the DWARF spec. */
18258 state->op_index = 0;
18259 state->file = 1;
18260 state->line = 1;
18261 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18262 was a line entry for it so that the backend has a chance to adjust it
18263 and also record it in case it needs it. This is currently used by MIPS
18264 code, cf. `mips_adjust_dwarf2_line'. */
18265 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18266 state->is_stmt = reader->line_header->default_is_stmt;
18267 state->discriminator = 0;
252a6764
DE
18268}
18269
924c2928
DE
18270/* Check address and if invalid nop-out the rest of the lines in this
18271 sequence. */
18272
18273static void
d9b3de22 18274check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
924c2928
DE
18275 const gdb_byte *line_ptr,
18276 CORE_ADDR lowpc, CORE_ADDR address)
18277{
18278 /* If address < lowpc then it's not a usable value, it's outside the
18279 pc range of the CU. However, we restrict the test to only address
18280 values of zero to preserve GDB's previous behaviour which is to
18281 handle the specific case of a function being GC'd by the linker. */
18282
18283 if (address == 0 && address < lowpc)
18284 {
18285 /* This line table is for a function which has been
18286 GCd by the linker. Ignore it. PR gdb/12528 */
18287
18288 struct objfile *objfile = cu->objfile;
18289 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18290
18291 complaint (&symfile_complaints,
18292 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18293 line_offset, objfile_name (objfile));
d9b3de22
DE
18294 state->record_line = noop_record_line;
18295 /* Note: sm.record_line is left as noop_record_line
924c2928
DE
18296 until we see DW_LNE_end_sequence. */
18297 }
18298}
18299
f3f5162e 18300/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18301 Process the line number information in LH.
18302 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18303 program in order to set included_p for every referenced header. */
debd256d 18304
c906108c 18305static void
43f3e411
DE
18306dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18307 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18308{
d521ce57
TT
18309 const gdb_byte *line_ptr, *extended_end;
18310 const gdb_byte *line_end;
a8c50c1f 18311 unsigned int bytes_read, extended_len;
699ca60a 18312 unsigned char op_code, extended_op;
e142c38c
DJ
18313 CORE_ADDR baseaddr;
18314 struct objfile *objfile = cu->objfile;
f3f5162e 18315 bfd *abfd = objfile->obfd;
fbf65064 18316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
d9b3de22
DE
18317 /* Non-zero if we're recording line info (as opposed to building partial
18318 symtabs). */
18319 int record_lines_p = !decode_for_pst_p;
18320 /* A collection of things we need to pass to dwarf_record_line. */
18321 lnp_reader_state reader_state;
e142c38c
DJ
18322
18323 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18324
debd256d
JB
18325 line_ptr = lh->statement_program_start;
18326 line_end = lh->statement_program_end;
c906108c 18327
d9b3de22
DE
18328 reader_state.gdbarch = gdbarch;
18329 reader_state.line_header = lh;
18330 reader_state.record_lines_p = record_lines_p;
18331
c906108c
SS
18332 /* Read the statement sequences until there's nothing left. */
18333 while (line_ptr < line_end)
18334 {
d9b3de22
DE
18335 /* The DWARF line number program state machine. */
18336 lnp_state_machine state_machine;
c906108c 18337 int end_sequence = 0;
d9b3de22
DE
18338
18339 /* Reset the state machine at the start of each sequence. */
18340 init_lnp_state_machine (&state_machine, &reader_state);
18341
18342 if (record_lines_p && lh->num_file_names >= state_machine.file)
c906108c 18343 {
aaa75496 18344 /* Start a subfile for the current file of the state machine. */
debd256d
JB
18345 /* lh->include_dirs and lh->file_names are 0-based, but the
18346 directory and file name numbers in the statement program
18347 are 1-based. */
d9b3de22 18348 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
d521ce57 18349 const char *dir = NULL;
a738430d 18350
afa6c9ab 18351 if (fe->dir_index && lh->include_dirs != NULL)
debd256d 18352 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb 18353
4d663531 18354 dwarf2_start_subfile (fe->name, dir);
c906108c
SS
18355 }
18356
a738430d 18357 /* Decode the table. */
d9b3de22 18358 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18359 {
18360 op_code = read_1_byte (abfd, line_ptr);
18361 line_ptr += 1;
9aa1fe7e 18362
debd256d 18363 if (op_code >= lh->opcode_base)
6e70227d 18364 {
8e07a239 18365 /* Special opcode. */
699ca60a 18366 unsigned char adj_opcode;
3e29f34a 18367 CORE_ADDR addr_adj;
a05a36a5 18368 int line_delta;
8e07a239 18369
debd256d 18370 adj_opcode = op_code - lh->opcode_base;
d9b3de22
DE
18371 addr_adj = (((state_machine.op_index
18372 + (adj_opcode / lh->line_range))
2dc7f7b3
TT
18373 / lh->maximum_ops_per_instruction)
18374 * lh->minimum_instruction_length);
d9b3de22
DE
18375 state_machine.address
18376 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18377 state_machine.op_index = ((state_machine.op_index
18378 + (adj_opcode / lh->line_range))
18379 % lh->maximum_ops_per_instruction);
a05a36a5 18380 line_delta = lh->line_base + (adj_opcode % lh->line_range);
d9b3de22 18381 state_machine.line += line_delta;
a05a36a5 18382 if (line_delta != 0)
d9b3de22
DE
18383 state_machine.line_has_non_zero_discriminator
18384 = state_machine.discriminator != 0;
18385
18386 dwarf_record_line (&reader_state, &state_machine, 0);
18387 state_machine.discriminator = 0;
9aa1fe7e
GK
18388 }
18389 else switch (op_code)
c906108c
SS
18390 {
18391 case DW_LNS_extended_op:
3e43a32a
MS
18392 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18393 &bytes_read);
473b7be6 18394 line_ptr += bytes_read;
a8c50c1f 18395 extended_end = line_ptr + extended_len;
c906108c
SS
18396 extended_op = read_1_byte (abfd, line_ptr);
18397 line_ptr += 1;
18398 switch (extended_op)
18399 {
18400 case DW_LNE_end_sequence:
d9b3de22 18401 state_machine.record_line = record_line;
c906108c 18402 end_sequence = 1;
c906108c
SS
18403 break;
18404 case DW_LNE_set_address:
d9b3de22
DE
18405 {
18406 CORE_ADDR address
18407 = read_address (abfd, line_ptr, cu, &bytes_read);
18408
18409 line_ptr += bytes_read;
18410 check_line_address (cu, &state_machine, line_ptr,
18411 lowpc, address);
18412 state_machine.op_index = 0;
18413 address += baseaddr;
18414 state_machine.address
18415 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18416 }
c906108c
SS
18417 break;
18418 case DW_LNE_define_file:
debd256d 18419 {
d521ce57 18420 const char *cur_file;
debd256d 18421 unsigned int dir_index, mod_time, length;
6e70227d 18422
3e43a32a
MS
18423 cur_file = read_direct_string (abfd, line_ptr,
18424 &bytes_read);
debd256d
JB
18425 line_ptr += bytes_read;
18426 dir_index =
18427 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18428 line_ptr += bytes_read;
18429 mod_time =
18430 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18431 line_ptr += bytes_read;
18432 length =
18433 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18434 line_ptr += bytes_read;
18435 add_file_name (lh, cur_file, dir_index, mod_time, length);
18436 }
c906108c 18437 break;
d0c6ba3d
CC
18438 case DW_LNE_set_discriminator:
18439 /* The discriminator is not interesting to the debugger;
a05a36a5
DE
18440 just ignore it. We still need to check its value though:
18441 if there are consecutive entries for the same
18442 (non-prologue) line we want to coalesce them.
18443 PR 17276. */
d9b3de22
DE
18444 state_machine.discriminator
18445 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18446 state_machine.line_has_non_zero_discriminator
18447 |= state_machine.discriminator != 0;
a05a36a5 18448 line_ptr += bytes_read;
d0c6ba3d 18449 break;
c906108c 18450 default:
4d3c2250 18451 complaint (&symfile_complaints,
e2e0b3e5 18452 _("mangled .debug_line section"));
debd256d 18453 return;
c906108c 18454 }
a8c50c1f
DJ
18455 /* Make sure that we parsed the extended op correctly. If e.g.
18456 we expected a different address size than the producer used,
18457 we may have read the wrong number of bytes. */
18458 if (line_ptr != extended_end)
18459 {
18460 complaint (&symfile_complaints,
18461 _("mangled .debug_line section"));
18462 return;
18463 }
c906108c
SS
18464 break;
18465 case DW_LNS_copy:
d9b3de22
DE
18466 dwarf_record_line (&reader_state, &state_machine, 0);
18467 state_machine.discriminator = 0;
c906108c
SS
18468 break;
18469 case DW_LNS_advance_pc:
2dc7f7b3
TT
18470 {
18471 CORE_ADDR adjust
18472 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3e29f34a 18473 CORE_ADDR addr_adj;
2dc7f7b3 18474
d9b3de22 18475 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18476 / lh->maximum_ops_per_instruction)
18477 * lh->minimum_instruction_length);
d9b3de22
DE
18478 state_machine.address
18479 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18480 state_machine.op_index = ((state_machine.op_index + adjust)
18481 % lh->maximum_ops_per_instruction);
2dc7f7b3
TT
18482 line_ptr += bytes_read;
18483 }
c906108c
SS
18484 break;
18485 case DW_LNS_advance_line:
a05a36a5
DE
18486 {
18487 int line_delta
18488 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18489
d9b3de22 18490 state_machine.line += line_delta;
a05a36a5 18491 if (line_delta != 0)
d9b3de22
DE
18492 state_machine.line_has_non_zero_discriminator
18493 = state_machine.discriminator != 0;
a05a36a5
DE
18494 line_ptr += bytes_read;
18495 }
c906108c
SS
18496 break;
18497 case DW_LNS_set_file:
d9b3de22
DE
18498 {
18499 /* The arrays lh->include_dirs and lh->file_names are
18500 0-based, but the directory and file name numbers in
18501 the statement program are 1-based. */
18502 struct file_entry *fe;
18503 const char *dir = NULL;
18504
18505 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18506 &bytes_read);
18507 line_ptr += bytes_read;
18508 if (state_machine.file == 0
18509 || state_machine.file - 1 >= lh->num_file_names)
18510 dwarf2_debug_line_missing_file_complaint ();
18511 else
18512 {
18513 fe = &lh->file_names[state_machine.file - 1];
18514 if (fe->dir_index && lh->include_dirs != NULL)
18515 dir = lh->include_dirs[fe->dir_index - 1];
18516 if (record_lines_p)
18517 {
18518 state_machine.last_subfile = current_subfile;
18519 state_machine.line_has_non_zero_discriminator
18520 = state_machine.discriminator != 0;
18521 dwarf2_start_subfile (fe->name, dir);
18522 }
18523 }
18524 }
c906108c
SS
18525 break;
18526 case DW_LNS_set_column:
0ad93d4f 18527 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18528 line_ptr += bytes_read;
18529 break;
18530 case DW_LNS_negate_stmt:
d9b3de22 18531 state_machine.is_stmt = (!state_machine.is_stmt);
c906108c
SS
18532 break;
18533 case DW_LNS_set_basic_block:
c906108c 18534 break;
c2c6d25f
JM
18535 /* Add to the address register of the state machine the
18536 address increment value corresponding to special opcode
a738430d
MK
18537 255. I.e., this value is scaled by the minimum
18538 instruction length since special opcode 255 would have
b021a221 18539 scaled the increment. */
c906108c 18540 case DW_LNS_const_add_pc:
2dc7f7b3
TT
18541 {
18542 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
3e29f34a 18543 CORE_ADDR addr_adj;
2dc7f7b3 18544
d9b3de22 18545 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18546 / lh->maximum_ops_per_instruction)
18547 * lh->minimum_instruction_length);
d9b3de22
DE
18548 state_machine.address
18549 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18550 state_machine.op_index = ((state_machine.op_index + adjust)
18551 % lh->maximum_ops_per_instruction);
2dc7f7b3 18552 }
c906108c
SS
18553 break;
18554 case DW_LNS_fixed_advance_pc:
3e29f34a
MR
18555 {
18556 CORE_ADDR addr_adj;
18557
18558 addr_adj = read_2_bytes (abfd, line_ptr);
d9b3de22
DE
18559 state_machine.address
18560 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18561 state_machine.op_index = 0;
3e29f34a
MR
18562 line_ptr += 2;
18563 }
c906108c 18564 break;
9aa1fe7e 18565 default:
a738430d
MK
18566 {
18567 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18568 int i;
a738430d 18569
debd256d 18570 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18571 {
18572 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18573 line_ptr += bytes_read;
18574 }
18575 }
c906108c
SS
18576 }
18577 }
d9b3de22
DE
18578
18579 if (!end_sequence)
18580 dwarf2_debug_line_missing_end_sequence_complaint ();
18581
18582 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18583 in which case we still finish recording the last line). */
18584 dwarf_record_line (&reader_state, &state_machine, 1);
c906108c 18585 }
f3f5162e
DE
18586}
18587
18588/* Decode the Line Number Program (LNP) for the given line_header
18589 structure and CU. The actual information extracted and the type
18590 of structures created from the LNP depends on the value of PST.
18591
18592 1. If PST is NULL, then this procedure uses the data from the program
18593 to create all necessary symbol tables, and their linetables.
18594
18595 2. If PST is not NULL, this procedure reads the program to determine
18596 the list of files included by the unit represented by PST, and
18597 builds all the associated partial symbol tables.
18598
18599 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18600 It is used for relative paths in the line table.
18601 NOTE: When processing partial symtabs (pst != NULL),
18602 comp_dir == pst->dirname.
18603
18604 NOTE: It is important that psymtabs have the same file name (via strcmp)
18605 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18606 symtab we don't use it in the name of the psymtabs we create.
18607 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18608 A good testcase for this is mb-inline.exp.
18609
527f3840
JK
18610 LOWPC is the lowest address in CU (or 0 if not known).
18611
18612 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18613 for its PC<->lines mapping information. Otherwise only the filename
18614 table is read in. */
f3f5162e
DE
18615
18616static void
18617dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18618 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18619 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18620{
18621 struct objfile *objfile = cu->objfile;
18622 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18623
527f3840
JK
18624 if (decode_mapping)
18625 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18626
18627 if (decode_for_pst_p)
18628 {
18629 int file_index;
18630
18631 /* Now that we're done scanning the Line Header Program, we can
18632 create the psymtab of each included file. */
18633 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18634 if (lh->file_names[file_index].included_p == 1)
18635 {
d521ce57 18636 const char *include_name =
c6da4cef
DE
18637 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18638 if (include_name != NULL)
aaa75496
JB
18639 dwarf2_create_include_psymtab (include_name, pst, objfile);
18640 }
18641 }
cb1df416
DJ
18642 else
18643 {
18644 /* Make sure a symtab is created for every file, even files
18645 which contain only variables (i.e. no code with associated
18646 line numbers). */
43f3e411 18647 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18648 int i;
cb1df416
DJ
18649
18650 for (i = 0; i < lh->num_file_names; i++)
18651 {
d521ce57 18652 const char *dir = NULL;
f3f5162e 18653 struct file_entry *fe;
9a619af0 18654
cb1df416 18655 fe = &lh->file_names[i];
afa6c9ab 18656 if (fe->dir_index && lh->include_dirs != NULL)
cb1df416 18657 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 18658 dwarf2_start_subfile (fe->name, dir);
cb1df416 18659
cb1df416 18660 if (current_subfile->symtab == NULL)
43f3e411
DE
18661 {
18662 current_subfile->symtab
18663 = allocate_symtab (cust, current_subfile->name);
18664 }
cb1df416
DJ
18665 fe->symtab = current_subfile->symtab;
18666 }
18667 }
c906108c
SS
18668}
18669
18670/* Start a subfile for DWARF. FILENAME is the name of the file and
18671 DIRNAME the name of the source directory which contains FILENAME
4d663531 18672 or NULL if not known.
c906108c
SS
18673 This routine tries to keep line numbers from identical absolute and
18674 relative file names in a common subfile.
18675
18676 Using the `list' example from the GDB testsuite, which resides in
18677 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18678 of /srcdir/list0.c yields the following debugging information for list0.c:
18679
c5aa993b 18680 DW_AT_name: /srcdir/list0.c
4d663531 18681 DW_AT_comp_dir: /compdir
357e46e7 18682 files.files[0].name: list0.h
c5aa993b 18683 files.files[0].dir: /srcdir
357e46e7 18684 files.files[1].name: list0.c
c5aa993b 18685 files.files[1].dir: /srcdir
c906108c
SS
18686
18687 The line number information for list0.c has to end up in a single
4f1520fb
FR
18688 subfile, so that `break /srcdir/list0.c:1' works as expected.
18689 start_subfile will ensure that this happens provided that we pass the
18690 concatenation of files.files[1].dir and files.files[1].name as the
18691 subfile's name. */
c906108c
SS
18692
18693static void
4d663531 18694dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18695{
d521ce57 18696 char *copy = NULL;
4f1520fb 18697
4d663531 18698 /* In order not to lose the line information directory,
4f1520fb
FR
18699 we concatenate it to the filename when it makes sense.
18700 Note that the Dwarf3 standard says (speaking of filenames in line
18701 information): ``The directory index is ignored for file names
18702 that represent full path names''. Thus ignoring dirname in the
18703 `else' branch below isn't an issue. */
c906108c 18704
d5166ae1 18705 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18706 {
18707 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18708 filename = copy;
18709 }
c906108c 18710
4d663531 18711 start_subfile (filename);
4f1520fb 18712
d521ce57
TT
18713 if (copy != NULL)
18714 xfree (copy);
c906108c
SS
18715}
18716
f4dc4d17
DE
18717/* Start a symtab for DWARF.
18718 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18719
43f3e411 18720static struct compunit_symtab *
f4dc4d17 18721dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18722 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18723{
43f3e411
DE
18724 struct compunit_symtab *cust
18725 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18726
f4dc4d17
DE
18727 record_debugformat ("DWARF 2");
18728 record_producer (cu->producer);
18729
18730 /* We assume that we're processing GCC output. */
18731 processing_gcc_compilation = 2;
18732
4d4ec4e5 18733 cu->processing_has_namespace_info = 0;
43f3e411
DE
18734
18735 return cust;
f4dc4d17
DE
18736}
18737
4c2df51b
DJ
18738static void
18739var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18740 struct dwarf2_cu *cu)
4c2df51b 18741{
e7c27a73
DJ
18742 struct objfile *objfile = cu->objfile;
18743 struct comp_unit_head *cu_header = &cu->header;
18744
4c2df51b
DJ
18745 /* NOTE drow/2003-01-30: There used to be a comment and some special
18746 code here to turn a symbol with DW_AT_external and a
18747 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18748 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18749 with some versions of binutils) where shared libraries could have
18750 relocations against symbols in their debug information - the
18751 minimal symbol would have the right address, but the debug info
18752 would not. It's no longer necessary, because we will explicitly
18753 apply relocations when we read in the debug information now. */
18754
18755 /* A DW_AT_location attribute with no contents indicates that a
18756 variable has been optimized away. */
18757 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18758 {
f1e6e072 18759 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18760 return;
18761 }
18762
18763 /* Handle one degenerate form of location expression specially, to
18764 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18765 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18766 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18767
18768 if (attr_form_is_block (attr)
3019eac3
DE
18769 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18770 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18771 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18772 && (DW_BLOCK (attr)->size
18773 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18774 {
891d2f0b 18775 unsigned int dummy;
4c2df51b 18776
3019eac3
DE
18777 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18778 SYMBOL_VALUE_ADDRESS (sym) =
18779 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18780 else
18781 SYMBOL_VALUE_ADDRESS (sym) =
18782 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18783 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18784 fixup_symbol_section (sym, objfile);
18785 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18786 SYMBOL_SECTION (sym));
4c2df51b
DJ
18787 return;
18788 }
18789
18790 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18791 expression evaluator, and use LOC_COMPUTED only when necessary
18792 (i.e. when the value of a register or memory location is
18793 referenced, or a thread-local block, etc.). Then again, it might
18794 not be worthwhile. I'm assuming that it isn't unless performance
18795 or memory numbers show me otherwise. */
18796
f1e6e072 18797 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18798
f1e6e072 18799 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18800 cu->has_loclist = 1;
4c2df51b
DJ
18801}
18802
c906108c
SS
18803/* Given a pointer to a DWARF information entry, figure out if we need
18804 to make a symbol table entry for it, and if so, create a new entry
18805 and return a pointer to it.
18806 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18807 used the passed type.
18808 If SPACE is not NULL, use it to hold the new symbol. If it is
18809 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18810
18811static struct symbol *
34eaf542
TT
18812new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18813 struct symbol *space)
c906108c 18814{
e7c27a73 18815 struct objfile *objfile = cu->objfile;
3e29f34a 18816 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18817 struct symbol *sym = NULL;
15d034d0 18818 const char *name;
c906108c
SS
18819 struct attribute *attr = NULL;
18820 struct attribute *attr2 = NULL;
e142c38c 18821 CORE_ADDR baseaddr;
e37fd15a
SW
18822 struct pending **list_to_add = NULL;
18823
edb3359d 18824 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18825
18826 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18827
94af9270 18828 name = dwarf2_name (die, cu);
c906108c
SS
18829 if (name)
18830 {
94af9270 18831 const char *linkagename;
34eaf542 18832 int suppress_add = 0;
94af9270 18833
34eaf542
TT
18834 if (space)
18835 sym = space;
18836 else
e623cf5d 18837 sym = allocate_symbol (objfile);
c906108c 18838 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18839
18840 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18841 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18842 linkagename = dwarf2_physname (name, die, cu);
18843 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18844
f55ee35c
JK
18845 /* Fortran does not have mangling standard and the mangling does differ
18846 between gfortran, iFort etc. */
18847 if (cu->language == language_fortran
b250c185 18848 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18849 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18850 dwarf2_full_name (name, die, cu),
29df156d 18851 NULL);
f55ee35c 18852
c906108c 18853 /* Default assumptions.
c5aa993b 18854 Use the passed type or decode it from the die. */
176620f1 18855 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18856 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18857 if (type != NULL)
18858 SYMBOL_TYPE (sym) = type;
18859 else
e7c27a73 18860 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18861 attr = dwarf2_attr (die,
18862 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18863 cu);
c906108c
SS
18864 if (attr)
18865 {
18866 SYMBOL_LINE (sym) = DW_UNSND (attr);
18867 }
cb1df416 18868
edb3359d
DJ
18869 attr = dwarf2_attr (die,
18870 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18871 cu);
cb1df416
DJ
18872 if (attr)
18873 {
18874 int file_index = DW_UNSND (attr);
9a619af0 18875
cb1df416
DJ
18876 if (cu->line_header == NULL
18877 || file_index > cu->line_header->num_file_names)
18878 complaint (&symfile_complaints,
18879 _("file index out of range"));
1c3d648d 18880 else if (file_index > 0)
cb1df416
DJ
18881 {
18882 struct file_entry *fe;
9a619af0 18883
cb1df416 18884 fe = &cu->line_header->file_names[file_index - 1];
08be3fe3 18885 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18886 }
18887 }
18888
c906108c
SS
18889 switch (die->tag)
18890 {
18891 case DW_TAG_label:
e142c38c 18892 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18893 if (attr)
3e29f34a
MR
18894 {
18895 CORE_ADDR addr;
18896
18897 addr = attr_value_as_address (attr);
18898 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18899 SYMBOL_VALUE_ADDRESS (sym) = addr;
18900 }
0f5238ed
TT
18901 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18902 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18903 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18904 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18905 break;
18906 case DW_TAG_subprogram:
18907 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18908 finish_block. */
f1e6e072 18909 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18910 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18911 if ((attr2 && (DW_UNSND (attr2) != 0))
18912 || cu->language == language_ada)
c906108c 18913 {
2cfa0c8d
JB
18914 /* Subprograms marked external are stored as a global symbol.
18915 Ada subprograms, whether marked external or not, are always
18916 stored as a global symbol, because we want to be able to
18917 access them globally. For instance, we want to be able
18918 to break on a nested subprogram without having to
18919 specify the context. */
e37fd15a 18920 list_to_add = &global_symbols;
c906108c
SS
18921 }
18922 else
18923 {
e37fd15a 18924 list_to_add = cu->list_in_scope;
c906108c
SS
18925 }
18926 break;
edb3359d
DJ
18927 case DW_TAG_inlined_subroutine:
18928 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18929 finish_block. */
f1e6e072 18930 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 18931 SYMBOL_INLINED (sym) = 1;
481860b3 18932 list_to_add = cu->list_in_scope;
edb3359d 18933 break;
34eaf542
TT
18934 case DW_TAG_template_value_param:
18935 suppress_add = 1;
18936 /* Fall through. */
72929c62 18937 case DW_TAG_constant:
c906108c 18938 case DW_TAG_variable:
254e6b9e 18939 case DW_TAG_member:
0963b4bd
MS
18940 /* Compilation with minimal debug info may result in
18941 variables with missing type entries. Change the
18942 misleading `void' type to something sensible. */
c906108c 18943 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 18944 SYMBOL_TYPE (sym)
46bf5051 18945 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 18946
e142c38c 18947 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
18948 /* In the case of DW_TAG_member, we should only be called for
18949 static const members. */
18950 if (die->tag == DW_TAG_member)
18951 {
3863f96c
DE
18952 /* dwarf2_add_field uses die_is_declaration,
18953 so we do the same. */
254e6b9e
DE
18954 gdb_assert (die_is_declaration (die, cu));
18955 gdb_assert (attr);
18956 }
c906108c
SS
18957 if (attr)
18958 {
e7c27a73 18959 dwarf2_const_value (attr, sym, cu);
e142c38c 18960 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 18961 if (!suppress_add)
34eaf542
TT
18962 {
18963 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 18964 list_to_add = &global_symbols;
34eaf542 18965 else
e37fd15a 18966 list_to_add = cu->list_in_scope;
34eaf542 18967 }
c906108c
SS
18968 break;
18969 }
e142c38c 18970 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18971 if (attr)
18972 {
e7c27a73 18973 var_decode_location (attr, sym, cu);
e142c38c 18974 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
18975
18976 /* Fortran explicitly imports any global symbols to the local
18977 scope by DW_TAG_common_block. */
18978 if (cu->language == language_fortran && die->parent
18979 && die->parent->tag == DW_TAG_common_block)
18980 attr2 = NULL;
18981
caac4577
JG
18982 if (SYMBOL_CLASS (sym) == LOC_STATIC
18983 && SYMBOL_VALUE_ADDRESS (sym) == 0
18984 && !dwarf2_per_objfile->has_section_at_zero)
18985 {
18986 /* When a static variable is eliminated by the linker,
18987 the corresponding debug information is not stripped
18988 out, but the variable address is set to null;
18989 do not add such variables into symbol table. */
18990 }
18991 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 18992 {
f55ee35c
JK
18993 /* Workaround gfortran PR debug/40040 - it uses
18994 DW_AT_location for variables in -fPIC libraries which may
18995 get overriden by other libraries/executable and get
18996 a different address. Resolve it by the minimal symbol
18997 which may come from inferior's executable using copy
18998 relocation. Make this workaround only for gfortran as for
18999 other compilers GDB cannot guess the minimal symbol
19000 Fortran mangling kind. */
19001 if (cu->language == language_fortran && die->parent
19002 && die->parent->tag == DW_TAG_module
19003 && cu->producer
28586665 19004 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19005 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19006
1c809c68
TT
19007 /* A variable with DW_AT_external is never static,
19008 but it may be block-scoped. */
19009 list_to_add = (cu->list_in_scope == &file_symbols
19010 ? &global_symbols : cu->list_in_scope);
1c809c68 19011 }
c906108c 19012 else
e37fd15a 19013 list_to_add = cu->list_in_scope;
c906108c
SS
19014 }
19015 else
19016 {
19017 /* We do not know the address of this symbol.
c5aa993b
JM
19018 If it is an external symbol and we have type information
19019 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19020 The address of the variable will then be determined from
19021 the minimal symbol table whenever the variable is
19022 referenced. */
e142c38c 19023 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19024
19025 /* Fortran explicitly imports any global symbols to the local
19026 scope by DW_TAG_common_block. */
19027 if (cu->language == language_fortran && die->parent
19028 && die->parent->tag == DW_TAG_common_block)
19029 {
19030 /* SYMBOL_CLASS doesn't matter here because
19031 read_common_block is going to reset it. */
19032 if (!suppress_add)
19033 list_to_add = cu->list_in_scope;
19034 }
19035 else if (attr2 && (DW_UNSND (attr2) != 0)
19036 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19037 {
0fe7935b
DJ
19038 /* A variable with DW_AT_external is never static, but it
19039 may be block-scoped. */
19040 list_to_add = (cu->list_in_scope == &file_symbols
19041 ? &global_symbols : cu->list_in_scope);
19042
f1e6e072 19043 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19044 }
442ddf59
JK
19045 else if (!die_is_declaration (die, cu))
19046 {
19047 /* Use the default LOC_OPTIMIZED_OUT class. */
19048 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19049 if (!suppress_add)
19050 list_to_add = cu->list_in_scope;
442ddf59 19051 }
c906108c
SS
19052 }
19053 break;
19054 case DW_TAG_formal_parameter:
edb3359d
DJ
19055 /* If we are inside a function, mark this as an argument. If
19056 not, we might be looking at an argument to an inlined function
19057 when we do not have enough information to show inlined frames;
19058 pretend it's a local variable in that case so that the user can
19059 still see it. */
19060 if (context_stack_depth > 0
19061 && context_stack[context_stack_depth - 1].name != NULL)
19062 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19063 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19064 if (attr)
19065 {
e7c27a73 19066 var_decode_location (attr, sym, cu);
c906108c 19067 }
e142c38c 19068 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19069 if (attr)
19070 {
e7c27a73 19071 dwarf2_const_value (attr, sym, cu);
c906108c 19072 }
f346a30d 19073
e37fd15a 19074 list_to_add = cu->list_in_scope;
c906108c
SS
19075 break;
19076 case DW_TAG_unspecified_parameters:
19077 /* From varargs functions; gdb doesn't seem to have any
19078 interest in this information, so just ignore it for now.
19079 (FIXME?) */
19080 break;
34eaf542
TT
19081 case DW_TAG_template_type_param:
19082 suppress_add = 1;
19083 /* Fall through. */
c906108c 19084 case DW_TAG_class_type:
680b30c7 19085 case DW_TAG_interface_type:
c906108c
SS
19086 case DW_TAG_structure_type:
19087 case DW_TAG_union_type:
72019c9c 19088 case DW_TAG_set_type:
c906108c 19089 case DW_TAG_enumeration_type:
f1e6e072 19090 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19091 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19092
63d06c5c 19093 {
9c37b5ae 19094 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19095 really ever be static objects: otherwise, if you try
19096 to, say, break of a class's method and you're in a file
19097 which doesn't mention that class, it won't work unless
19098 the check for all static symbols in lookup_symbol_aux
19099 saves you. See the OtherFileClass tests in
19100 gdb.c++/namespace.exp. */
19101
e37fd15a 19102 if (!suppress_add)
34eaf542 19103 {
34eaf542 19104 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19105 && cu->language == language_cplus
34eaf542 19106 ? &global_symbols : cu->list_in_scope);
63d06c5c 19107
64382290 19108 /* The semantics of C++ state that "struct foo {
9c37b5ae 19109 ... }" also defines a typedef for "foo". */
64382290 19110 if (cu->language == language_cplus
45280282 19111 || cu->language == language_ada
c44af4eb
TT
19112 || cu->language == language_d
19113 || cu->language == language_rust)
64382290
TT
19114 {
19115 /* The symbol's name is already allocated along
19116 with this objfile, so we don't need to
19117 duplicate it for the type. */
19118 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19119 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19120 }
63d06c5c
DC
19121 }
19122 }
c906108c
SS
19123 break;
19124 case DW_TAG_typedef:
f1e6e072 19125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19126 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19127 list_to_add = cu->list_in_scope;
63d06c5c 19128 break;
c906108c 19129 case DW_TAG_base_type:
a02abb62 19130 case DW_TAG_subrange_type:
f1e6e072 19131 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19132 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19133 list_to_add = cu->list_in_scope;
c906108c
SS
19134 break;
19135 case DW_TAG_enumerator:
e142c38c 19136 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19137 if (attr)
19138 {
e7c27a73 19139 dwarf2_const_value (attr, sym, cu);
c906108c 19140 }
63d06c5c
DC
19141 {
19142 /* NOTE: carlton/2003-11-10: See comment above in the
19143 DW_TAG_class_type, etc. block. */
19144
e142c38c 19145 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19146 && cu->language == language_cplus
e142c38c 19147 ? &global_symbols : cu->list_in_scope);
63d06c5c 19148 }
c906108c 19149 break;
74921315 19150 case DW_TAG_imported_declaration:
5c4e30ca 19151 case DW_TAG_namespace:
f1e6e072 19152 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19153 list_to_add = &global_symbols;
5c4e30ca 19154 break;
530e8392
KB
19155 case DW_TAG_module:
19156 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19157 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19158 list_to_add = &global_symbols;
19159 break;
4357ac6c 19160 case DW_TAG_common_block:
f1e6e072 19161 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19162 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19163 add_symbol_to_list (sym, cu->list_in_scope);
19164 break;
c906108c
SS
19165 default:
19166 /* Not a tag we recognize. Hopefully we aren't processing
19167 trash data, but since we must specifically ignore things
19168 we don't recognize, there is nothing else we should do at
0963b4bd 19169 this point. */
e2e0b3e5 19170 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19171 dwarf_tag_name (die->tag));
c906108c
SS
19172 break;
19173 }
df8a16a1 19174
e37fd15a
SW
19175 if (suppress_add)
19176 {
19177 sym->hash_next = objfile->template_symbols;
19178 objfile->template_symbols = sym;
19179 list_to_add = NULL;
19180 }
19181
19182 if (list_to_add != NULL)
19183 add_symbol_to_list (sym, list_to_add);
19184
df8a16a1
DJ
19185 /* For the benefit of old versions of GCC, check for anonymous
19186 namespaces based on the demangled name. */
4d4ec4e5 19187 if (!cu->processing_has_namespace_info
94af9270 19188 && cu->language == language_cplus)
a10964d1 19189 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19190 }
19191 return (sym);
19192}
19193
34eaf542
TT
19194/* A wrapper for new_symbol_full that always allocates a new symbol. */
19195
19196static struct symbol *
19197new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19198{
19199 return new_symbol_full (die, type, cu, NULL);
19200}
19201
98bfdba5
PA
19202/* Given an attr with a DW_FORM_dataN value in host byte order,
19203 zero-extend it as appropriate for the symbol's type. The DWARF
19204 standard (v4) is not entirely clear about the meaning of using
19205 DW_FORM_dataN for a constant with a signed type, where the type is
19206 wider than the data. The conclusion of a discussion on the DWARF
19207 list was that this is unspecified. We choose to always zero-extend
19208 because that is the interpretation long in use by GCC. */
c906108c 19209
98bfdba5 19210static gdb_byte *
ff39bb5e 19211dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19212 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19213{
e7c27a73 19214 struct objfile *objfile = cu->objfile;
e17a4113
UW
19215 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19216 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19217 LONGEST l = DW_UNSND (attr);
19218
19219 if (bits < sizeof (*value) * 8)
19220 {
19221 l &= ((LONGEST) 1 << bits) - 1;
19222 *value = l;
19223 }
19224 else if (bits == sizeof (*value) * 8)
19225 *value = l;
19226 else
19227 {
224c3ddb 19228 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19229 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19230 return bytes;
19231 }
19232
19233 return NULL;
19234}
19235
19236/* Read a constant value from an attribute. Either set *VALUE, or if
19237 the value does not fit in *VALUE, set *BYTES - either already
19238 allocated on the objfile obstack, or newly allocated on OBSTACK,
19239 or, set *BATON, if we translated the constant to a location
19240 expression. */
19241
19242static void
ff39bb5e 19243dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19244 const char *name, struct obstack *obstack,
19245 struct dwarf2_cu *cu,
d521ce57 19246 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19247 struct dwarf2_locexpr_baton **baton)
19248{
19249 struct objfile *objfile = cu->objfile;
19250 struct comp_unit_head *cu_header = &cu->header;
c906108c 19251 struct dwarf_block *blk;
98bfdba5
PA
19252 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19253 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19254
19255 *value = 0;
19256 *bytes = NULL;
19257 *baton = NULL;
c906108c
SS
19258
19259 switch (attr->form)
19260 {
19261 case DW_FORM_addr:
3019eac3 19262 case DW_FORM_GNU_addr_index:
ac56253d 19263 {
ac56253d
TT
19264 gdb_byte *data;
19265
98bfdba5
PA
19266 if (TYPE_LENGTH (type) != cu_header->addr_size)
19267 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19268 cu_header->addr_size,
98bfdba5 19269 TYPE_LENGTH (type));
ac56253d
TT
19270 /* Symbols of this form are reasonably rare, so we just
19271 piggyback on the existing location code rather than writing
19272 a new implementation of symbol_computed_ops. */
8d749320 19273 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19274 (*baton)->per_cu = cu->per_cu;
19275 gdb_assert ((*baton)->per_cu);
ac56253d 19276
98bfdba5 19277 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19278 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19279 (*baton)->data = data;
ac56253d
TT
19280
19281 data[0] = DW_OP_addr;
19282 store_unsigned_integer (&data[1], cu_header->addr_size,
19283 byte_order, DW_ADDR (attr));
19284 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19285 }
c906108c 19286 break;
4ac36638 19287 case DW_FORM_string:
93b5768b 19288 case DW_FORM_strp:
3019eac3 19289 case DW_FORM_GNU_str_index:
36586728 19290 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19291 /* DW_STRING is already allocated on the objfile obstack, point
19292 directly to it. */
d521ce57 19293 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19294 break;
c906108c
SS
19295 case DW_FORM_block1:
19296 case DW_FORM_block2:
19297 case DW_FORM_block4:
19298 case DW_FORM_block:
2dc7f7b3 19299 case DW_FORM_exprloc:
c906108c 19300 blk = DW_BLOCK (attr);
98bfdba5
PA
19301 if (TYPE_LENGTH (type) != blk->size)
19302 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19303 TYPE_LENGTH (type));
19304 *bytes = blk->data;
c906108c 19305 break;
2df3850c
JM
19306
19307 /* The DW_AT_const_value attributes are supposed to carry the
19308 symbol's value "represented as it would be on the target
19309 architecture." By the time we get here, it's already been
19310 converted to host endianness, so we just need to sign- or
19311 zero-extend it as appropriate. */
19312 case DW_FORM_data1:
3aef2284 19313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19314 break;
c906108c 19315 case DW_FORM_data2:
3aef2284 19316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19317 break;
c906108c 19318 case DW_FORM_data4:
3aef2284 19319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19320 break;
c906108c 19321 case DW_FORM_data8:
3aef2284 19322 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19323 break;
19324
c906108c 19325 case DW_FORM_sdata:
98bfdba5 19326 *value = DW_SND (attr);
2df3850c
JM
19327 break;
19328
c906108c 19329 case DW_FORM_udata:
98bfdba5 19330 *value = DW_UNSND (attr);
c906108c 19331 break;
2df3850c 19332
c906108c 19333 default:
4d3c2250 19334 complaint (&symfile_complaints,
e2e0b3e5 19335 _("unsupported const value attribute form: '%s'"),
4d3c2250 19336 dwarf_form_name (attr->form));
98bfdba5 19337 *value = 0;
c906108c
SS
19338 break;
19339 }
19340}
19341
2df3850c 19342
98bfdba5
PA
19343/* Copy constant value from an attribute to a symbol. */
19344
2df3850c 19345static void
ff39bb5e 19346dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19347 struct dwarf2_cu *cu)
2df3850c 19348{
98bfdba5 19349 struct objfile *objfile = cu->objfile;
12df843f 19350 LONGEST value;
d521ce57 19351 const gdb_byte *bytes;
98bfdba5 19352 struct dwarf2_locexpr_baton *baton;
2df3850c 19353
98bfdba5
PA
19354 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19355 SYMBOL_PRINT_NAME (sym),
19356 &objfile->objfile_obstack, cu,
19357 &value, &bytes, &baton);
2df3850c 19358
98bfdba5
PA
19359 if (baton != NULL)
19360 {
98bfdba5 19361 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19362 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19363 }
19364 else if (bytes != NULL)
19365 {
19366 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19367 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19368 }
19369 else
19370 {
19371 SYMBOL_VALUE (sym) = value;
f1e6e072 19372 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19373 }
2df3850c
JM
19374}
19375
c906108c
SS
19376/* Return the type of the die in question using its DW_AT_type attribute. */
19377
19378static struct type *
e7c27a73 19379die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19380{
c906108c 19381 struct attribute *type_attr;
c906108c 19382
e142c38c 19383 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19384 if (!type_attr)
19385 {
19386 /* A missing DW_AT_type represents a void type. */
46bf5051 19387 return objfile_type (cu->objfile)->builtin_void;
c906108c 19388 }
348e048f 19389
673bfd45 19390 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19391}
19392
b4ba55a1
JB
19393/* True iff CU's producer generates GNAT Ada auxiliary information
19394 that allows to find parallel types through that information instead
19395 of having to do expensive parallel lookups by type name. */
19396
19397static int
19398need_gnat_info (struct dwarf2_cu *cu)
19399{
19400 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19401 of GNAT produces this auxiliary information, without any indication
19402 that it is produced. Part of enhancing the FSF version of GNAT
19403 to produce that information will be to put in place an indicator
19404 that we can use in order to determine whether the descriptive type
19405 info is available or not. One suggestion that has been made is
19406 to use a new attribute, attached to the CU die. For now, assume
19407 that the descriptive type info is not available. */
19408 return 0;
19409}
19410
b4ba55a1
JB
19411/* Return the auxiliary type of the die in question using its
19412 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19413 attribute is not present. */
19414
19415static struct type *
19416die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19417{
b4ba55a1 19418 struct attribute *type_attr;
b4ba55a1
JB
19419
19420 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19421 if (!type_attr)
19422 return NULL;
19423
673bfd45 19424 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19425}
19426
19427/* If DIE has a descriptive_type attribute, then set the TYPE's
19428 descriptive type accordingly. */
19429
19430static void
19431set_descriptive_type (struct type *type, struct die_info *die,
19432 struct dwarf2_cu *cu)
19433{
19434 struct type *descriptive_type = die_descriptive_type (die, cu);
19435
19436 if (descriptive_type)
19437 {
19438 ALLOCATE_GNAT_AUX_TYPE (type);
19439 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19440 }
19441}
19442
c906108c
SS
19443/* Return the containing type of the die in question using its
19444 DW_AT_containing_type attribute. */
19445
19446static struct type *
e7c27a73 19447die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19448{
c906108c 19449 struct attribute *type_attr;
c906108c 19450
e142c38c 19451 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19452 if (!type_attr)
19453 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19454 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19455
673bfd45 19456 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19457}
19458
ac9ec31b
DE
19459/* Return an error marker type to use for the ill formed type in DIE/CU. */
19460
19461static struct type *
19462build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19463{
19464 struct objfile *objfile = dwarf2_per_objfile->objfile;
19465 char *message, *saved;
19466
19467 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19468 objfile_name (objfile),
ac9ec31b
DE
19469 cu->header.offset.sect_off,
19470 die->offset.sect_off);
224c3ddb
SM
19471 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19472 message, strlen (message));
ac9ec31b
DE
19473 xfree (message);
19474
19f392bc 19475 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19476}
19477
673bfd45 19478/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19479 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19480 DW_AT_containing_type.
673bfd45
DE
19481 If there is no type substitute an error marker. */
19482
c906108c 19483static struct type *
ff39bb5e 19484lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19485 struct dwarf2_cu *cu)
c906108c 19486{
bb5ed363 19487 struct objfile *objfile = cu->objfile;
f792889a
DJ
19488 struct type *this_type;
19489
ac9ec31b
DE
19490 gdb_assert (attr->name == DW_AT_type
19491 || attr->name == DW_AT_GNAT_descriptive_type
19492 || attr->name == DW_AT_containing_type);
19493
673bfd45
DE
19494 /* First see if we have it cached. */
19495
36586728
TT
19496 if (attr->form == DW_FORM_GNU_ref_alt)
19497 {
19498 struct dwarf2_per_cu_data *per_cu;
19499 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19500
19501 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19502 this_type = get_die_type_at_offset (offset, per_cu);
19503 }
7771576e 19504 else if (attr_form_is_ref (attr))
673bfd45 19505 {
b64f50a1 19506 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
19507
19508 this_type = get_die_type_at_offset (offset, cu->per_cu);
19509 }
55f1336d 19510 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19511 {
ac9ec31b 19512 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19513
ac9ec31b 19514 return get_signatured_type (die, signature, cu);
673bfd45
DE
19515 }
19516 else
19517 {
ac9ec31b
DE
19518 complaint (&symfile_complaints,
19519 _("Dwarf Error: Bad type attribute %s in DIE"
19520 " at 0x%x [in module %s]"),
19521 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 19522 objfile_name (objfile));
ac9ec31b 19523 return build_error_marker_type (cu, die);
673bfd45
DE
19524 }
19525
19526 /* If not cached we need to read it in. */
19527
19528 if (this_type == NULL)
19529 {
ac9ec31b 19530 struct die_info *type_die = NULL;
673bfd45
DE
19531 struct dwarf2_cu *type_cu = cu;
19532
7771576e 19533 if (attr_form_is_ref (attr))
ac9ec31b
DE
19534 type_die = follow_die_ref (die, attr, &type_cu);
19535 if (type_die == NULL)
19536 return build_error_marker_type (cu, die);
19537 /* If we find the type now, it's probably because the type came
3019eac3
DE
19538 from an inter-CU reference and the type's CU got expanded before
19539 ours. */
ac9ec31b 19540 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19541 }
19542
19543 /* If we still don't have a type use an error marker. */
19544
19545 if (this_type == NULL)
ac9ec31b 19546 return build_error_marker_type (cu, die);
673bfd45 19547
f792889a 19548 return this_type;
c906108c
SS
19549}
19550
673bfd45
DE
19551/* Return the type in DIE, CU.
19552 Returns NULL for invalid types.
19553
02142a6c 19554 This first does a lookup in die_type_hash,
673bfd45
DE
19555 and only reads the die in if necessary.
19556
19557 NOTE: This can be called when reading in partial or full symbols. */
19558
f792889a 19559static struct type *
e7c27a73 19560read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19561{
f792889a
DJ
19562 struct type *this_type;
19563
19564 this_type = get_die_type (die, cu);
19565 if (this_type)
19566 return this_type;
19567
673bfd45
DE
19568 return read_type_die_1 (die, cu);
19569}
19570
19571/* Read the type in DIE, CU.
19572 Returns NULL for invalid types. */
19573
19574static struct type *
19575read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19576{
19577 struct type *this_type = NULL;
19578
c906108c
SS
19579 switch (die->tag)
19580 {
19581 case DW_TAG_class_type:
680b30c7 19582 case DW_TAG_interface_type:
c906108c
SS
19583 case DW_TAG_structure_type:
19584 case DW_TAG_union_type:
f792889a 19585 this_type = read_structure_type (die, cu);
c906108c
SS
19586 break;
19587 case DW_TAG_enumeration_type:
f792889a 19588 this_type = read_enumeration_type (die, cu);
c906108c
SS
19589 break;
19590 case DW_TAG_subprogram:
19591 case DW_TAG_subroutine_type:
edb3359d 19592 case DW_TAG_inlined_subroutine:
f792889a 19593 this_type = read_subroutine_type (die, cu);
c906108c
SS
19594 break;
19595 case DW_TAG_array_type:
f792889a 19596 this_type = read_array_type (die, cu);
c906108c 19597 break;
72019c9c 19598 case DW_TAG_set_type:
f792889a 19599 this_type = read_set_type (die, cu);
72019c9c 19600 break;
c906108c 19601 case DW_TAG_pointer_type:
f792889a 19602 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19603 break;
19604 case DW_TAG_ptr_to_member_type:
f792889a 19605 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19606 break;
19607 case DW_TAG_reference_type:
f792889a 19608 this_type = read_tag_reference_type (die, cu);
c906108c
SS
19609 break;
19610 case DW_TAG_const_type:
f792889a 19611 this_type = read_tag_const_type (die, cu);
c906108c
SS
19612 break;
19613 case DW_TAG_volatile_type:
f792889a 19614 this_type = read_tag_volatile_type (die, cu);
c906108c 19615 break;
06d66ee9
TT
19616 case DW_TAG_restrict_type:
19617 this_type = read_tag_restrict_type (die, cu);
19618 break;
c906108c 19619 case DW_TAG_string_type:
f792889a 19620 this_type = read_tag_string_type (die, cu);
c906108c
SS
19621 break;
19622 case DW_TAG_typedef:
f792889a 19623 this_type = read_typedef (die, cu);
c906108c 19624 break;
a02abb62 19625 case DW_TAG_subrange_type:
f792889a 19626 this_type = read_subrange_type (die, cu);
a02abb62 19627 break;
c906108c 19628 case DW_TAG_base_type:
f792889a 19629 this_type = read_base_type (die, cu);
c906108c 19630 break;
81a17f79 19631 case DW_TAG_unspecified_type:
f792889a 19632 this_type = read_unspecified_type (die, cu);
81a17f79 19633 break;
0114d602
DJ
19634 case DW_TAG_namespace:
19635 this_type = read_namespace_type (die, cu);
19636 break;
f55ee35c
JK
19637 case DW_TAG_module:
19638 this_type = read_module_type (die, cu);
19639 break;
a2c2acaf
MW
19640 case DW_TAG_atomic_type:
19641 this_type = read_tag_atomic_type (die, cu);
19642 break;
c906108c 19643 default:
3e43a32a
MS
19644 complaint (&symfile_complaints,
19645 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19646 dwarf_tag_name (die->tag));
c906108c
SS
19647 break;
19648 }
63d06c5c 19649
f792889a 19650 return this_type;
63d06c5c
DC
19651}
19652
abc72ce4
DE
19653/* See if we can figure out if the class lives in a namespace. We do
19654 this by looking for a member function; its demangled name will
19655 contain namespace info, if there is any.
19656 Return the computed name or NULL.
19657 Space for the result is allocated on the objfile's obstack.
19658 This is the full-die version of guess_partial_die_structure_name.
19659 In this case we know DIE has no useful parent. */
19660
19661static char *
19662guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19663{
19664 struct die_info *spec_die;
19665 struct dwarf2_cu *spec_cu;
19666 struct die_info *child;
19667
19668 spec_cu = cu;
19669 spec_die = die_specification (die, &spec_cu);
19670 if (spec_die != NULL)
19671 {
19672 die = spec_die;
19673 cu = spec_cu;
19674 }
19675
19676 for (child = die->child;
19677 child != NULL;
19678 child = child->sibling)
19679 {
19680 if (child->tag == DW_TAG_subprogram)
19681 {
7d45c7c3 19682 const char *linkage_name;
abc72ce4 19683
7d45c7c3
KB
19684 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19685 if (linkage_name == NULL)
19686 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19687 cu);
19688 if (linkage_name != NULL)
abc72ce4
DE
19689 {
19690 char *actual_name
19691 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19692 linkage_name);
abc72ce4
DE
19693 char *name = NULL;
19694
19695 if (actual_name != NULL)
19696 {
15d034d0 19697 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19698
19699 if (die_name != NULL
19700 && strcmp (die_name, actual_name) != 0)
19701 {
19702 /* Strip off the class name from the full name.
19703 We want the prefix. */
19704 int die_name_len = strlen (die_name);
19705 int actual_name_len = strlen (actual_name);
19706
19707 /* Test for '::' as a sanity check. */
19708 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19709 && actual_name[actual_name_len
19710 - die_name_len - 1] == ':')
224c3ddb
SM
19711 name = (char *) obstack_copy0 (
19712 &cu->objfile->per_bfd->storage_obstack,
19713 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19714 }
19715 }
19716 xfree (actual_name);
19717 return name;
19718 }
19719 }
19720 }
19721
19722 return NULL;
19723}
19724
96408a79
SA
19725/* GCC might emit a nameless typedef that has a linkage name. Determine the
19726 prefix part in such case. See
19727 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19728
19729static char *
19730anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19731{
19732 struct attribute *attr;
e6a959d6 19733 const char *base;
96408a79
SA
19734
19735 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19736 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19737 return NULL;
19738
7d45c7c3 19739 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19740 return NULL;
19741
19742 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19743 if (attr == NULL)
19744 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19745 if (attr == NULL || DW_STRING (attr) == NULL)
19746 return NULL;
19747
19748 /* dwarf2_name had to be already called. */
19749 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19750
19751 /* Strip the base name, keep any leading namespaces/classes. */
19752 base = strrchr (DW_STRING (attr), ':');
19753 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19754 return "";
19755
224c3ddb
SM
19756 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19757 DW_STRING (attr),
19758 &base[-1] - DW_STRING (attr));
96408a79
SA
19759}
19760
fdde2d81 19761/* Return the name of the namespace/class that DIE is defined within,
0114d602 19762 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19763
0114d602
DJ
19764 For example, if we're within the method foo() in the following
19765 code:
19766
19767 namespace N {
19768 class C {
19769 void foo () {
19770 }
19771 };
19772 }
19773
19774 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19775
0d5cff50 19776static const char *
e142c38c 19777determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19778{
0114d602
DJ
19779 struct die_info *parent, *spec_die;
19780 struct dwarf2_cu *spec_cu;
19781 struct type *parent_type;
96408a79 19782 char *retval;
63d06c5c 19783
9c37b5ae 19784 if (cu->language != language_cplus
c44af4eb
TT
19785 && cu->language != language_fortran && cu->language != language_d
19786 && cu->language != language_rust)
0114d602
DJ
19787 return "";
19788
96408a79
SA
19789 retval = anonymous_struct_prefix (die, cu);
19790 if (retval)
19791 return retval;
19792
0114d602
DJ
19793 /* We have to be careful in the presence of DW_AT_specification.
19794 For example, with GCC 3.4, given the code
19795
19796 namespace N {
19797 void foo() {
19798 // Definition of N::foo.
19799 }
19800 }
19801
19802 then we'll have a tree of DIEs like this:
19803
19804 1: DW_TAG_compile_unit
19805 2: DW_TAG_namespace // N
19806 3: DW_TAG_subprogram // declaration of N::foo
19807 4: DW_TAG_subprogram // definition of N::foo
19808 DW_AT_specification // refers to die #3
19809
19810 Thus, when processing die #4, we have to pretend that we're in
19811 the context of its DW_AT_specification, namely the contex of die
19812 #3. */
19813 spec_cu = cu;
19814 spec_die = die_specification (die, &spec_cu);
19815 if (spec_die == NULL)
19816 parent = die->parent;
19817 else
63d06c5c 19818 {
0114d602
DJ
19819 parent = spec_die->parent;
19820 cu = spec_cu;
63d06c5c 19821 }
0114d602
DJ
19822
19823 if (parent == NULL)
19824 return "";
98bfdba5
PA
19825 else if (parent->building_fullname)
19826 {
19827 const char *name;
19828 const char *parent_name;
19829
19830 /* It has been seen on RealView 2.2 built binaries,
19831 DW_TAG_template_type_param types actually _defined_ as
19832 children of the parent class:
19833
19834 enum E {};
19835 template class <class Enum> Class{};
19836 Class<enum E> class_e;
19837
19838 1: DW_TAG_class_type (Class)
19839 2: DW_TAG_enumeration_type (E)
19840 3: DW_TAG_enumerator (enum1:0)
19841 3: DW_TAG_enumerator (enum2:1)
19842 ...
19843 2: DW_TAG_template_type_param
19844 DW_AT_type DW_FORM_ref_udata (E)
19845
19846 Besides being broken debug info, it can put GDB into an
19847 infinite loop. Consider:
19848
19849 When we're building the full name for Class<E>, we'll start
19850 at Class, and go look over its template type parameters,
19851 finding E. We'll then try to build the full name of E, and
19852 reach here. We're now trying to build the full name of E,
19853 and look over the parent DIE for containing scope. In the
19854 broken case, if we followed the parent DIE of E, we'd again
19855 find Class, and once again go look at its template type
19856 arguments, etc., etc. Simply don't consider such parent die
19857 as source-level parent of this die (it can't be, the language
19858 doesn't allow it), and break the loop here. */
19859 name = dwarf2_name (die, cu);
19860 parent_name = dwarf2_name (parent, cu);
19861 complaint (&symfile_complaints,
19862 _("template param type '%s' defined within parent '%s'"),
19863 name ? name : "<unknown>",
19864 parent_name ? parent_name : "<unknown>");
19865 return "";
19866 }
63d06c5c 19867 else
0114d602
DJ
19868 switch (parent->tag)
19869 {
63d06c5c 19870 case DW_TAG_namespace:
0114d602 19871 parent_type = read_type_die (parent, cu);
acebe513
UW
19872 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19873 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19874 Work around this problem here. */
19875 if (cu->language == language_cplus
19876 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19877 return "";
0114d602
DJ
19878 /* We give a name to even anonymous namespaces. */
19879 return TYPE_TAG_NAME (parent_type);
63d06c5c 19880 case DW_TAG_class_type:
680b30c7 19881 case DW_TAG_interface_type:
63d06c5c 19882 case DW_TAG_structure_type:
0114d602 19883 case DW_TAG_union_type:
f55ee35c 19884 case DW_TAG_module:
0114d602
DJ
19885 parent_type = read_type_die (parent, cu);
19886 if (TYPE_TAG_NAME (parent_type) != NULL)
19887 return TYPE_TAG_NAME (parent_type);
19888 else
19889 /* An anonymous structure is only allowed non-static data
19890 members; no typedefs, no member functions, et cetera.
19891 So it does not need a prefix. */
19892 return "";
abc72ce4 19893 case DW_TAG_compile_unit:
95554aad 19894 case DW_TAG_partial_unit:
abc72ce4
DE
19895 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19896 if (cu->language == language_cplus
8b70b953 19897 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19898 && die->child != NULL
19899 && (die->tag == DW_TAG_class_type
19900 || die->tag == DW_TAG_structure_type
19901 || die->tag == DW_TAG_union_type))
19902 {
19903 char *name = guess_full_die_structure_name (die, cu);
19904 if (name != NULL)
19905 return name;
19906 }
19907 return "";
3d567982
TT
19908 case DW_TAG_enumeration_type:
19909 parent_type = read_type_die (parent, cu);
19910 if (TYPE_DECLARED_CLASS (parent_type))
19911 {
19912 if (TYPE_TAG_NAME (parent_type) != NULL)
19913 return TYPE_TAG_NAME (parent_type);
19914 return "";
19915 }
19916 /* Fall through. */
63d06c5c 19917 default:
8176b9b8 19918 return determine_prefix (parent, cu);
63d06c5c 19919 }
63d06c5c
DC
19920}
19921
3e43a32a
MS
19922/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19923 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19924 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19925 an obconcat, otherwise allocate storage for the result. The CU argument is
19926 used to determine the language and hence, the appropriate separator. */
987504bb 19927
f55ee35c 19928#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
19929
19930static char *
f55ee35c
JK
19931typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19932 int physname, struct dwarf2_cu *cu)
63d06c5c 19933{
f55ee35c 19934 const char *lead = "";
5c315b68 19935 const char *sep;
63d06c5c 19936
3e43a32a
MS
19937 if (suffix == NULL || suffix[0] == '\0'
19938 || prefix == NULL || prefix[0] == '\0')
987504bb 19939 sep = "";
45280282
IB
19940 else if (cu->language == language_d)
19941 {
19942 /* For D, the 'main' function could be defined in any module, but it
19943 should never be prefixed. */
19944 if (strcmp (suffix, "D main") == 0)
19945 {
19946 prefix = "";
19947 sep = "";
19948 }
19949 else
19950 sep = ".";
19951 }
f55ee35c
JK
19952 else if (cu->language == language_fortran && physname)
19953 {
19954 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19955 DW_AT_MIPS_linkage_name is preferred and used instead. */
19956
19957 lead = "__";
19958 sep = "_MOD_";
19959 }
987504bb
JJ
19960 else
19961 sep = "::";
63d06c5c 19962
6dd47d34
DE
19963 if (prefix == NULL)
19964 prefix = "";
19965 if (suffix == NULL)
19966 suffix = "";
19967
987504bb
JJ
19968 if (obs == NULL)
19969 {
3e43a32a 19970 char *retval
224c3ddb
SM
19971 = ((char *)
19972 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 19973
f55ee35c
JK
19974 strcpy (retval, lead);
19975 strcat (retval, prefix);
6dd47d34
DE
19976 strcat (retval, sep);
19977 strcat (retval, suffix);
63d06c5c
DC
19978 return retval;
19979 }
987504bb
JJ
19980 else
19981 {
19982 /* We have an obstack. */
f55ee35c 19983 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 19984 }
63d06c5c
DC
19985}
19986
c906108c
SS
19987/* Return sibling of die, NULL if no sibling. */
19988
f9aca02d 19989static struct die_info *
fba45db2 19990sibling_die (struct die_info *die)
c906108c 19991{
639d11d3 19992 return die->sibling;
c906108c
SS
19993}
19994
71c25dea
TT
19995/* Get name of a die, return NULL if not found. */
19996
15d034d0
TT
19997static const char *
19998dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
19999 struct obstack *obstack)
20000{
20001 if (name && cu->language == language_cplus)
20002 {
2f408ecb 20003 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20004
2f408ecb 20005 if (!canon_name.empty ())
71c25dea 20006 {
2f408ecb
PA
20007 if (canon_name != name)
20008 name = (const char *) obstack_copy0 (obstack,
20009 canon_name.c_str (),
20010 canon_name.length ());
71c25dea
TT
20011 }
20012 }
20013
20014 return name;
c906108c
SS
20015}
20016
96553a0c
DE
20017/* Get name of a die, return NULL if not found.
20018 Anonymous namespaces are converted to their magic string. */
9219021c 20019
15d034d0 20020static const char *
e142c38c 20021dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20022{
20023 struct attribute *attr;
20024
e142c38c 20025 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20026 if ((!attr || !DW_STRING (attr))
96553a0c 20027 && die->tag != DW_TAG_namespace
53832f31
TT
20028 && die->tag != DW_TAG_class_type
20029 && die->tag != DW_TAG_interface_type
20030 && die->tag != DW_TAG_structure_type
20031 && die->tag != DW_TAG_union_type)
71c25dea
TT
20032 return NULL;
20033
20034 switch (die->tag)
20035 {
20036 case DW_TAG_compile_unit:
95554aad 20037 case DW_TAG_partial_unit:
71c25dea
TT
20038 /* Compilation units have a DW_AT_name that is a filename, not
20039 a source language identifier. */
20040 case DW_TAG_enumeration_type:
20041 case DW_TAG_enumerator:
20042 /* These tags always have simple identifiers already; no need
20043 to canonicalize them. */
20044 return DW_STRING (attr);
907af001 20045
96553a0c
DE
20046 case DW_TAG_namespace:
20047 if (attr != NULL && DW_STRING (attr) != NULL)
20048 return DW_STRING (attr);
20049 return CP_ANONYMOUS_NAMESPACE_STR;
20050
907af001
UW
20051 case DW_TAG_class_type:
20052 case DW_TAG_interface_type:
20053 case DW_TAG_structure_type:
20054 case DW_TAG_union_type:
20055 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20056 structures or unions. These were of the form "._%d" in GCC 4.1,
20057 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20058 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20059 if (attr && DW_STRING (attr)
61012eef
GB
20060 && (startswith (DW_STRING (attr), "._")
20061 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20062 return NULL;
53832f31
TT
20063
20064 /* GCC might emit a nameless typedef that has a linkage name. See
20065 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20066 if (!attr || DW_STRING (attr) == NULL)
20067 {
df5c6c50 20068 char *demangled = NULL;
53832f31
TT
20069
20070 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20071 if (attr == NULL)
20072 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20073
20074 if (attr == NULL || DW_STRING (attr) == NULL)
20075 return NULL;
20076
df5c6c50
JK
20077 /* Avoid demangling DW_STRING (attr) the second time on a second
20078 call for the same DIE. */
20079 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20080 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20081
20082 if (demangled)
20083 {
e6a959d6 20084 const char *base;
96408a79 20085
53832f31 20086 /* FIXME: we already did this for the partial symbol... */
34a68019 20087 DW_STRING (attr)
224c3ddb
SM
20088 = ((const char *)
20089 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20090 demangled, strlen (demangled)));
53832f31
TT
20091 DW_STRING_IS_CANONICAL (attr) = 1;
20092 xfree (demangled);
96408a79
SA
20093
20094 /* Strip any leading namespaces/classes, keep only the base name.
20095 DW_AT_name for named DIEs does not contain the prefixes. */
20096 base = strrchr (DW_STRING (attr), ':');
20097 if (base && base > DW_STRING (attr) && base[-1] == ':')
20098 return &base[1];
20099 else
20100 return DW_STRING (attr);
53832f31
TT
20101 }
20102 }
907af001
UW
20103 break;
20104
71c25dea 20105 default:
907af001
UW
20106 break;
20107 }
20108
20109 if (!DW_STRING_IS_CANONICAL (attr))
20110 {
20111 DW_STRING (attr)
20112 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20113 &cu->objfile->per_bfd->storage_obstack);
907af001 20114 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20115 }
907af001 20116 return DW_STRING (attr);
9219021c
DC
20117}
20118
20119/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20120 is none. *EXT_CU is the CU containing DIE on input, and the CU
20121 containing the return value on output. */
9219021c
DC
20122
20123static struct die_info *
f2f0e013 20124dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20125{
20126 struct attribute *attr;
9219021c 20127
f2f0e013 20128 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20129 if (attr == NULL)
20130 return NULL;
20131
f2f0e013 20132 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20133}
20134
c906108c
SS
20135/* Convert a DIE tag into its string name. */
20136
f39c6ffd 20137static const char *
aa1ee363 20138dwarf_tag_name (unsigned tag)
c906108c 20139{
f39c6ffd
TT
20140 const char *name = get_DW_TAG_name (tag);
20141
20142 if (name == NULL)
20143 return "DW_TAG_<unknown>";
20144
20145 return name;
c906108c
SS
20146}
20147
20148/* Convert a DWARF attribute code into its string name. */
20149
f39c6ffd 20150static const char *
aa1ee363 20151dwarf_attr_name (unsigned attr)
c906108c 20152{
f39c6ffd
TT
20153 const char *name;
20154
c764a876 20155#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20156 if (attr == DW_AT_MIPS_fde)
20157 return "DW_AT_MIPS_fde";
20158#else
20159 if (attr == DW_AT_HP_block_index)
20160 return "DW_AT_HP_block_index";
c764a876 20161#endif
f39c6ffd
TT
20162
20163 name = get_DW_AT_name (attr);
20164
20165 if (name == NULL)
20166 return "DW_AT_<unknown>";
20167
20168 return name;
c906108c
SS
20169}
20170
20171/* Convert a DWARF value form code into its string name. */
20172
f39c6ffd 20173static const char *
aa1ee363 20174dwarf_form_name (unsigned form)
c906108c 20175{
f39c6ffd
TT
20176 const char *name = get_DW_FORM_name (form);
20177
20178 if (name == NULL)
20179 return "DW_FORM_<unknown>";
20180
20181 return name;
c906108c
SS
20182}
20183
20184static char *
fba45db2 20185dwarf_bool_name (unsigned mybool)
c906108c
SS
20186{
20187 if (mybool)
20188 return "TRUE";
20189 else
20190 return "FALSE";
20191}
20192
20193/* Convert a DWARF type code into its string name. */
20194
f39c6ffd 20195static const char *
aa1ee363 20196dwarf_type_encoding_name (unsigned enc)
c906108c 20197{
f39c6ffd 20198 const char *name = get_DW_ATE_name (enc);
c906108c 20199
f39c6ffd
TT
20200 if (name == NULL)
20201 return "DW_ATE_<unknown>";
c906108c 20202
f39c6ffd 20203 return name;
c906108c 20204}
c906108c 20205
f9aca02d 20206static void
d97bc12b 20207dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20208{
20209 unsigned int i;
20210
d97bc12b
DE
20211 print_spaces (indent, f);
20212 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 20213 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
20214
20215 if (die->parent != NULL)
20216 {
20217 print_spaces (indent, f);
20218 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 20219 die->parent->offset.sect_off);
d97bc12b
DE
20220 }
20221
20222 print_spaces (indent, f);
20223 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20224 dwarf_bool_name (die->child != NULL));
c906108c 20225
d97bc12b
DE
20226 print_spaces (indent, f);
20227 fprintf_unfiltered (f, " attributes:\n");
20228
c906108c
SS
20229 for (i = 0; i < die->num_attrs; ++i)
20230 {
d97bc12b
DE
20231 print_spaces (indent, f);
20232 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20233 dwarf_attr_name (die->attrs[i].name),
20234 dwarf_form_name (die->attrs[i].form));
d97bc12b 20235
c906108c
SS
20236 switch (die->attrs[i].form)
20237 {
c906108c 20238 case DW_FORM_addr:
3019eac3 20239 case DW_FORM_GNU_addr_index:
d97bc12b 20240 fprintf_unfiltered (f, "address: ");
5af949e3 20241 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20242 break;
20243 case DW_FORM_block2:
20244 case DW_FORM_block4:
20245 case DW_FORM_block:
20246 case DW_FORM_block1:
56eb65bd
SP
20247 fprintf_unfiltered (f, "block: size %s",
20248 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20249 break;
2dc7f7b3 20250 case DW_FORM_exprloc:
56eb65bd
SP
20251 fprintf_unfiltered (f, "expression: size %s",
20252 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20253 break;
4568ecf9
DE
20254 case DW_FORM_ref_addr:
20255 fprintf_unfiltered (f, "ref address: ");
20256 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20257 break;
36586728
TT
20258 case DW_FORM_GNU_ref_alt:
20259 fprintf_unfiltered (f, "alt ref address: ");
20260 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20261 break;
10b3939b
DJ
20262 case DW_FORM_ref1:
20263 case DW_FORM_ref2:
20264 case DW_FORM_ref4:
4568ecf9
DE
20265 case DW_FORM_ref8:
20266 case DW_FORM_ref_udata:
d97bc12b 20267 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20268 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20269 break;
c906108c
SS
20270 case DW_FORM_data1:
20271 case DW_FORM_data2:
20272 case DW_FORM_data4:
ce5d95e1 20273 case DW_FORM_data8:
c906108c
SS
20274 case DW_FORM_udata:
20275 case DW_FORM_sdata:
43bbcdc2
PH
20276 fprintf_unfiltered (f, "constant: %s",
20277 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20278 break;
2dc7f7b3
TT
20279 case DW_FORM_sec_offset:
20280 fprintf_unfiltered (f, "section offset: %s",
20281 pulongest (DW_UNSND (&die->attrs[i])));
20282 break;
55f1336d 20283 case DW_FORM_ref_sig8:
ac9ec31b
DE
20284 fprintf_unfiltered (f, "signature: %s",
20285 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20286 break;
c906108c 20287 case DW_FORM_string:
4bdf3d34 20288 case DW_FORM_strp:
43988095 20289 case DW_FORM_line_strp:
3019eac3 20290 case DW_FORM_GNU_str_index:
36586728 20291 case DW_FORM_GNU_strp_alt:
8285870a 20292 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20293 DW_STRING (&die->attrs[i])
8285870a
JK
20294 ? DW_STRING (&die->attrs[i]) : "",
20295 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20296 break;
20297 case DW_FORM_flag:
20298 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20299 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20300 else
d97bc12b 20301 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20302 break;
2dc7f7b3
TT
20303 case DW_FORM_flag_present:
20304 fprintf_unfiltered (f, "flag: TRUE");
20305 break;
a8329558 20306 case DW_FORM_indirect:
0963b4bd
MS
20307 /* The reader will have reduced the indirect form to
20308 the "base form" so this form should not occur. */
3e43a32a
MS
20309 fprintf_unfiltered (f,
20310 "unexpected attribute form: DW_FORM_indirect");
a8329558 20311 break;
c906108c 20312 default:
d97bc12b 20313 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20314 die->attrs[i].form);
d97bc12b 20315 break;
c906108c 20316 }
d97bc12b 20317 fprintf_unfiltered (f, "\n");
c906108c
SS
20318 }
20319}
20320
f9aca02d 20321static void
d97bc12b 20322dump_die_for_error (struct die_info *die)
c906108c 20323{
d97bc12b
DE
20324 dump_die_shallow (gdb_stderr, 0, die);
20325}
20326
20327static void
20328dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20329{
20330 int indent = level * 4;
20331
20332 gdb_assert (die != NULL);
20333
20334 if (level >= max_level)
20335 return;
20336
20337 dump_die_shallow (f, indent, die);
20338
20339 if (die->child != NULL)
c906108c 20340 {
d97bc12b
DE
20341 print_spaces (indent, f);
20342 fprintf_unfiltered (f, " Children:");
20343 if (level + 1 < max_level)
20344 {
20345 fprintf_unfiltered (f, "\n");
20346 dump_die_1 (f, level + 1, max_level, die->child);
20347 }
20348 else
20349 {
3e43a32a
MS
20350 fprintf_unfiltered (f,
20351 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20352 }
20353 }
20354
20355 if (die->sibling != NULL && level > 0)
20356 {
20357 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20358 }
20359}
20360
d97bc12b
DE
20361/* This is called from the pdie macro in gdbinit.in.
20362 It's not static so gcc will keep a copy callable from gdb. */
20363
20364void
20365dump_die (struct die_info *die, int max_level)
20366{
20367 dump_die_1 (gdb_stdlog, 0, max_level, die);
20368}
20369
f9aca02d 20370static void
51545339 20371store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20372{
51545339 20373 void **slot;
c906108c 20374
b64f50a1
JK
20375 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20376 INSERT);
51545339
DJ
20377
20378 *slot = die;
c906108c
SS
20379}
20380
b64f50a1
JK
20381/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20382 required kind. */
20383
20384static sect_offset
ff39bb5e 20385dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20386{
4568ecf9 20387 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 20388
7771576e 20389 if (attr_form_is_ref (attr))
b64f50a1 20390 return retval;
93311388 20391
b64f50a1 20392 retval.sect_off = 0;
93311388
DE
20393 complaint (&symfile_complaints,
20394 _("unsupported die ref attribute form: '%s'"),
20395 dwarf_form_name (attr->form));
b64f50a1 20396 return retval;
c906108c
SS
20397}
20398
43bbcdc2
PH
20399/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20400 * the value held by the attribute is not constant. */
a02abb62 20401
43bbcdc2 20402static LONGEST
ff39bb5e 20403dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20404{
20405 if (attr->form == DW_FORM_sdata)
20406 return DW_SND (attr);
20407 else if (attr->form == DW_FORM_udata
20408 || attr->form == DW_FORM_data1
20409 || attr->form == DW_FORM_data2
20410 || attr->form == DW_FORM_data4
20411 || attr->form == DW_FORM_data8)
20412 return DW_UNSND (attr);
20413 else
20414 {
3e43a32a
MS
20415 complaint (&symfile_complaints,
20416 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20417 dwarf_form_name (attr->form));
20418 return default_value;
20419 }
20420}
20421
348e048f
DE
20422/* Follow reference or signature attribute ATTR of SRC_DIE.
20423 On entry *REF_CU is the CU of SRC_DIE.
20424 On exit *REF_CU is the CU of the result. */
20425
20426static struct die_info *
ff39bb5e 20427follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20428 struct dwarf2_cu **ref_cu)
20429{
20430 struct die_info *die;
20431
7771576e 20432 if (attr_form_is_ref (attr))
348e048f 20433 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20434 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20435 die = follow_die_sig (src_die, attr, ref_cu);
20436 else
20437 {
20438 dump_die_for_error (src_die);
20439 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20440 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20441 }
20442
20443 return die;
03dd20cc
DJ
20444}
20445
5c631832 20446/* Follow reference OFFSET.
673bfd45
DE
20447 On entry *REF_CU is the CU of the source die referencing OFFSET.
20448 On exit *REF_CU is the CU of the result.
20449 Returns NULL if OFFSET is invalid. */
f504f079 20450
f9aca02d 20451static struct die_info *
36586728
TT
20452follow_die_offset (sect_offset offset, int offset_in_dwz,
20453 struct dwarf2_cu **ref_cu)
c906108c 20454{
10b3939b 20455 struct die_info temp_die;
f2f0e013 20456 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20457
348e048f
DE
20458 gdb_assert (cu->per_cu != NULL);
20459
98bfdba5
PA
20460 target_cu = cu;
20461
3019eac3 20462 if (cu->per_cu->is_debug_types)
348e048f
DE
20463 {
20464 /* .debug_types CUs cannot reference anything outside their CU.
20465 If they need to, they have to reference a signatured type via
55f1336d 20466 DW_FORM_ref_sig8. */
348e048f 20467 if (! offset_in_cu_p (&cu->header, offset))
5c631832 20468 return NULL;
348e048f 20469 }
36586728
TT
20470 else if (offset_in_dwz != cu->per_cu->is_dwz
20471 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
20472 {
20473 struct dwarf2_per_cu_data *per_cu;
9a619af0 20474
36586728
TT
20475 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20476 cu->objfile);
03dd20cc
DJ
20477
20478 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20479 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20480 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20481
10b3939b
DJ
20482 target_cu = per_cu->cu;
20483 }
98bfdba5
PA
20484 else if (cu->dies == NULL)
20485 {
20486 /* We're loading full DIEs during partial symbol reading. */
20487 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20488 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20489 }
c906108c 20490
f2f0e013 20491 *ref_cu = target_cu;
51545339 20492 temp_die.offset = offset;
9a3c8263
SM
20493 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20494 &temp_die, offset.sect_off);
5c631832 20495}
10b3939b 20496
5c631832
JK
20497/* Follow reference attribute ATTR of SRC_DIE.
20498 On entry *REF_CU is the CU of SRC_DIE.
20499 On exit *REF_CU is the CU of the result. */
20500
20501static struct die_info *
ff39bb5e 20502follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20503 struct dwarf2_cu **ref_cu)
20504{
b64f50a1 20505 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20506 struct dwarf2_cu *cu = *ref_cu;
20507 struct die_info *die;
20508
36586728
TT
20509 die = follow_die_offset (offset,
20510 (attr->form == DW_FORM_GNU_ref_alt
20511 || cu->per_cu->is_dwz),
20512 ref_cu);
5c631832
JK
20513 if (!die)
20514 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20515 "at 0x%x [in module %s]"),
4262abfb
JK
20516 offset.sect_off, src_die->offset.sect_off,
20517 objfile_name (cu->objfile));
348e048f 20518
5c631832
JK
20519 return die;
20520}
20521
d83e736b
JK
20522/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20523 Returned value is intended for DW_OP_call*. Returned
20524 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20525
20526struct dwarf2_locexpr_baton
8b9737bf
TT
20527dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20528 struct dwarf2_per_cu_data *per_cu,
20529 CORE_ADDR (*get_frame_pc) (void *baton),
20530 void *baton)
5c631832 20531{
918dd910 20532 struct dwarf2_cu *cu;
5c631832
JK
20533 struct die_info *die;
20534 struct attribute *attr;
20535 struct dwarf2_locexpr_baton retval;
20536
8cf6f0b1
TT
20537 dw2_setup (per_cu->objfile);
20538
918dd910
JK
20539 if (per_cu->cu == NULL)
20540 load_cu (per_cu);
20541 cu = per_cu->cu;
cc12ce38
DE
20542 if (cu == NULL)
20543 {
20544 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20545 Instead just throw an error, not much else we can do. */
20546 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20547 offset.sect_off, objfile_name (per_cu->objfile));
20548 }
918dd910 20549
36586728 20550 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
20551 if (!die)
20552 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20553 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20554
20555 attr = dwarf2_attr (die, DW_AT_location, cu);
20556 if (!attr)
20557 {
e103e986
JK
20558 /* DWARF: "If there is no such attribute, then there is no effect.".
20559 DATA is ignored if SIZE is 0. */
5c631832 20560
e103e986 20561 retval.data = NULL;
5c631832
JK
20562 retval.size = 0;
20563 }
8cf6f0b1
TT
20564 else if (attr_form_is_section_offset (attr))
20565 {
20566 struct dwarf2_loclist_baton loclist_baton;
20567 CORE_ADDR pc = (*get_frame_pc) (baton);
20568 size_t size;
20569
20570 fill_in_loclist_baton (cu, &loclist_baton, attr);
20571
20572 retval.data = dwarf2_find_location_expression (&loclist_baton,
20573 &size, pc);
20574 retval.size = size;
20575 }
5c631832
JK
20576 else
20577 {
20578 if (!attr_form_is_block (attr))
20579 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20580 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 20581 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20582
20583 retval.data = DW_BLOCK (attr)->data;
20584 retval.size = DW_BLOCK (attr)->size;
20585 }
20586 retval.per_cu = cu->per_cu;
918dd910 20587
918dd910
JK
20588 age_cached_comp_units ();
20589
5c631832 20590 return retval;
348e048f
DE
20591}
20592
8b9737bf
TT
20593/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20594 offset. */
20595
20596struct dwarf2_locexpr_baton
20597dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20598 struct dwarf2_per_cu_data *per_cu,
20599 CORE_ADDR (*get_frame_pc) (void *baton),
20600 void *baton)
20601{
20602 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20603
20604 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20605}
20606
b6807d98
TT
20607/* Write a constant of a given type as target-ordered bytes into
20608 OBSTACK. */
20609
20610static const gdb_byte *
20611write_constant_as_bytes (struct obstack *obstack,
20612 enum bfd_endian byte_order,
20613 struct type *type,
20614 ULONGEST value,
20615 LONGEST *len)
20616{
20617 gdb_byte *result;
20618
20619 *len = TYPE_LENGTH (type);
224c3ddb 20620 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20621 store_unsigned_integer (result, *len, byte_order, value);
20622
20623 return result;
20624}
20625
20626/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20627 pointer to the constant bytes and set LEN to the length of the
20628 data. If memory is needed, allocate it on OBSTACK. If the DIE
20629 does not have a DW_AT_const_value, return NULL. */
20630
20631const gdb_byte *
20632dwarf2_fetch_constant_bytes (sect_offset offset,
20633 struct dwarf2_per_cu_data *per_cu,
20634 struct obstack *obstack,
20635 LONGEST *len)
20636{
20637 struct dwarf2_cu *cu;
20638 struct die_info *die;
20639 struct attribute *attr;
20640 const gdb_byte *result = NULL;
20641 struct type *type;
20642 LONGEST value;
20643 enum bfd_endian byte_order;
20644
20645 dw2_setup (per_cu->objfile);
20646
20647 if (per_cu->cu == NULL)
20648 load_cu (per_cu);
20649 cu = per_cu->cu;
cc12ce38
DE
20650 if (cu == NULL)
20651 {
20652 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20653 Instead just throw an error, not much else we can do. */
20654 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20655 offset.sect_off, objfile_name (per_cu->objfile));
20656 }
b6807d98
TT
20657
20658 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20659 if (!die)
20660 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20661 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
20662
20663
20664 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20665 if (attr == NULL)
20666 return NULL;
20667
20668 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20669 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20670
20671 switch (attr->form)
20672 {
20673 case DW_FORM_addr:
20674 case DW_FORM_GNU_addr_index:
20675 {
20676 gdb_byte *tem;
20677
20678 *len = cu->header.addr_size;
224c3ddb 20679 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20680 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20681 result = tem;
20682 }
20683 break;
20684 case DW_FORM_string:
20685 case DW_FORM_strp:
20686 case DW_FORM_GNU_str_index:
20687 case DW_FORM_GNU_strp_alt:
20688 /* DW_STRING is already allocated on the objfile obstack, point
20689 directly to it. */
20690 result = (const gdb_byte *) DW_STRING (attr);
20691 *len = strlen (DW_STRING (attr));
20692 break;
20693 case DW_FORM_block1:
20694 case DW_FORM_block2:
20695 case DW_FORM_block4:
20696 case DW_FORM_block:
20697 case DW_FORM_exprloc:
20698 result = DW_BLOCK (attr)->data;
20699 *len = DW_BLOCK (attr)->size;
20700 break;
20701
20702 /* The DW_AT_const_value attributes are supposed to carry the
20703 symbol's value "represented as it would be on the target
20704 architecture." By the time we get here, it's already been
20705 converted to host endianness, so we just need to sign- or
20706 zero-extend it as appropriate. */
20707 case DW_FORM_data1:
20708 type = die_type (die, cu);
20709 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20710 if (result == NULL)
20711 result = write_constant_as_bytes (obstack, byte_order,
20712 type, value, len);
20713 break;
20714 case DW_FORM_data2:
20715 type = die_type (die, cu);
20716 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20717 if (result == NULL)
20718 result = write_constant_as_bytes (obstack, byte_order,
20719 type, value, len);
20720 break;
20721 case DW_FORM_data4:
20722 type = die_type (die, cu);
20723 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20724 if (result == NULL)
20725 result = write_constant_as_bytes (obstack, byte_order,
20726 type, value, len);
20727 break;
20728 case DW_FORM_data8:
20729 type = die_type (die, cu);
20730 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20731 if (result == NULL)
20732 result = write_constant_as_bytes (obstack, byte_order,
20733 type, value, len);
20734 break;
20735
20736 case DW_FORM_sdata:
20737 type = die_type (die, cu);
20738 result = write_constant_as_bytes (obstack, byte_order,
20739 type, DW_SND (attr), len);
20740 break;
20741
20742 case DW_FORM_udata:
20743 type = die_type (die, cu);
20744 result = write_constant_as_bytes (obstack, byte_order,
20745 type, DW_UNSND (attr), len);
20746 break;
20747
20748 default:
20749 complaint (&symfile_complaints,
20750 _("unsupported const value attribute form: '%s'"),
20751 dwarf_form_name (attr->form));
20752 break;
20753 }
20754
20755 return result;
20756}
20757
8a9b8146
TT
20758/* Return the type of the DIE at DIE_OFFSET in the CU named by
20759 PER_CU. */
20760
20761struct type *
b64f50a1 20762dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20763 struct dwarf2_per_cu_data *per_cu)
20764{
b64f50a1
JK
20765 sect_offset die_offset_sect;
20766
8a9b8146 20767 dw2_setup (per_cu->objfile);
b64f50a1
JK
20768
20769 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20770 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20771}
20772
ac9ec31b 20773/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20774 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20775 On exit *REF_CU is the CU of the result.
20776 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20777
20778static struct die_info *
ac9ec31b
DE
20779follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20780 struct dwarf2_cu **ref_cu)
348e048f 20781{
348e048f 20782 struct die_info temp_die;
348e048f
DE
20783 struct dwarf2_cu *sig_cu;
20784 struct die_info *die;
20785
ac9ec31b
DE
20786 /* While it might be nice to assert sig_type->type == NULL here,
20787 we can get here for DW_AT_imported_declaration where we need
20788 the DIE not the type. */
348e048f
DE
20789
20790 /* If necessary, add it to the queue and load its DIEs. */
20791
95554aad 20792 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20793 read_signatured_type (sig_type);
348e048f 20794
348e048f 20795 sig_cu = sig_type->per_cu.cu;
69d751e3 20796 gdb_assert (sig_cu != NULL);
3019eac3
DE
20797 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20798 temp_die.offset = sig_type->type_offset_in_section;
9a3c8263
SM
20799 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20800 temp_die.offset.sect_off);
348e048f
DE
20801 if (die)
20802 {
796a7ff8
DE
20803 /* For .gdb_index version 7 keep track of included TUs.
20804 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20805 if (dwarf2_per_objfile->index_table != NULL
20806 && dwarf2_per_objfile->index_table->version <= 7)
20807 {
20808 VEC_safe_push (dwarf2_per_cu_ptr,
20809 (*ref_cu)->per_cu->imported_symtabs,
20810 sig_cu->per_cu);
20811 }
20812
348e048f
DE
20813 *ref_cu = sig_cu;
20814 return die;
20815 }
20816
ac9ec31b
DE
20817 return NULL;
20818}
20819
20820/* Follow signatured type referenced by ATTR in SRC_DIE.
20821 On entry *REF_CU is the CU of SRC_DIE.
20822 On exit *REF_CU is the CU of the result.
20823 The result is the DIE of the type.
20824 If the referenced type cannot be found an error is thrown. */
20825
20826static struct die_info *
ff39bb5e 20827follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20828 struct dwarf2_cu **ref_cu)
20829{
20830 ULONGEST signature = DW_SIGNATURE (attr);
20831 struct signatured_type *sig_type;
20832 struct die_info *die;
20833
20834 gdb_assert (attr->form == DW_FORM_ref_sig8);
20835
a2ce51a0 20836 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20837 /* sig_type will be NULL if the signatured type is missing from
20838 the debug info. */
20839 if (sig_type == NULL)
20840 {
20841 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20842 " from DIE at 0x%x [in module %s]"),
20843 hex_string (signature), src_die->offset.sect_off,
4262abfb 20844 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20845 }
20846
20847 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20848 if (die == NULL)
20849 {
20850 dump_die_for_error (src_die);
20851 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20852 " from DIE at 0x%x [in module %s]"),
20853 hex_string (signature), src_die->offset.sect_off,
4262abfb 20854 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20855 }
20856
20857 return die;
20858}
20859
20860/* Get the type specified by SIGNATURE referenced in DIE/CU,
20861 reading in and processing the type unit if necessary. */
20862
20863static struct type *
20864get_signatured_type (struct die_info *die, ULONGEST signature,
20865 struct dwarf2_cu *cu)
20866{
20867 struct signatured_type *sig_type;
20868 struct dwarf2_cu *type_cu;
20869 struct die_info *type_die;
20870 struct type *type;
20871
a2ce51a0 20872 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20873 /* sig_type will be NULL if the signatured type is missing from
20874 the debug info. */
20875 if (sig_type == NULL)
20876 {
20877 complaint (&symfile_complaints,
20878 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20879 " from DIE at 0x%x [in module %s]"),
20880 hex_string (signature), die->offset.sect_off,
4262abfb 20881 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20882 return build_error_marker_type (cu, die);
20883 }
20884
20885 /* If we already know the type we're done. */
20886 if (sig_type->type != NULL)
20887 return sig_type->type;
20888
20889 type_cu = cu;
20890 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20891 if (type_die != NULL)
20892 {
20893 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20894 is created. This is important, for example, because for c++ classes
20895 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20896 type = read_type_die (type_die, type_cu);
20897 if (type == NULL)
20898 {
20899 complaint (&symfile_complaints,
20900 _("Dwarf Error: Cannot build signatured type %s"
20901 " referenced from DIE at 0x%x [in module %s]"),
20902 hex_string (signature), die->offset.sect_off,
4262abfb 20903 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20904 type = build_error_marker_type (cu, die);
20905 }
20906 }
20907 else
20908 {
20909 complaint (&symfile_complaints,
20910 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20911 " from DIE at 0x%x [in module %s]"),
20912 hex_string (signature), die->offset.sect_off,
4262abfb 20913 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20914 type = build_error_marker_type (cu, die);
20915 }
20916 sig_type->type = type;
20917
20918 return type;
20919}
20920
20921/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20922 reading in and processing the type unit if necessary. */
20923
20924static struct type *
ff39bb5e 20925get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 20926 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
20927{
20928 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 20929 if (attr_form_is_ref (attr))
ac9ec31b
DE
20930 {
20931 struct dwarf2_cu *type_cu = cu;
20932 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20933
20934 return read_type_die (type_die, type_cu);
20935 }
20936 else if (attr->form == DW_FORM_ref_sig8)
20937 {
20938 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20939 }
20940 else
20941 {
20942 complaint (&symfile_complaints,
20943 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20944 " at 0x%x [in module %s]"),
20945 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 20946 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20947 return build_error_marker_type (cu, die);
20948 }
348e048f
DE
20949}
20950
e5fe5e75 20951/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
20952
20953static void
e5fe5e75 20954load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 20955{
52dc124a 20956 struct signatured_type *sig_type;
348e048f 20957
f4dc4d17
DE
20958 /* Caller is responsible for ensuring type_unit_groups don't get here. */
20959 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
20960
6721b2ec
DE
20961 /* We have the per_cu, but we need the signatured_type.
20962 Fortunately this is an easy translation. */
20963 gdb_assert (per_cu->is_debug_types);
20964 sig_type = (struct signatured_type *) per_cu;
348e048f 20965
6721b2ec 20966 gdb_assert (per_cu->cu == NULL);
348e048f 20967
52dc124a 20968 read_signatured_type (sig_type);
348e048f 20969
6721b2ec 20970 gdb_assert (per_cu->cu != NULL);
348e048f
DE
20971}
20972
dee91e82
DE
20973/* die_reader_func for read_signatured_type.
20974 This is identical to load_full_comp_unit_reader,
20975 but is kept separate for now. */
348e048f
DE
20976
20977static void
dee91e82 20978read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 20979 const gdb_byte *info_ptr,
dee91e82
DE
20980 struct die_info *comp_unit_die,
20981 int has_children,
20982 void *data)
348e048f 20983{
dee91e82 20984 struct dwarf2_cu *cu = reader->cu;
348e048f 20985
dee91e82
DE
20986 gdb_assert (cu->die_hash == NULL);
20987 cu->die_hash =
20988 htab_create_alloc_ex (cu->header.length / 12,
20989 die_hash,
20990 die_eq,
20991 NULL,
20992 &cu->comp_unit_obstack,
20993 hashtab_obstack_allocate,
20994 dummy_obstack_deallocate);
348e048f 20995
dee91e82
DE
20996 if (has_children)
20997 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
20998 &info_ptr, comp_unit_die);
20999 cu->dies = comp_unit_die;
21000 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21001
21002 /* We try not to read any attributes in this function, because not
9cdd5dbd 21003 all CUs needed for references have been loaded yet, and symbol
348e048f 21004 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21005 or we won't be able to build types correctly.
21006 Similarly, if we do not read the producer, we can not apply
21007 producer-specific interpretation. */
95554aad 21008 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21009}
348e048f 21010
3019eac3
DE
21011/* Read in a signatured type and build its CU and DIEs.
21012 If the type is a stub for the real type in a DWO file,
21013 read in the real type from the DWO file as well. */
dee91e82
DE
21014
21015static void
21016read_signatured_type (struct signatured_type *sig_type)
21017{
21018 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21019
3019eac3 21020 gdb_assert (per_cu->is_debug_types);
dee91e82 21021 gdb_assert (per_cu->cu == NULL);
348e048f 21022
f4dc4d17
DE
21023 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21024 read_signatured_type_reader, NULL);
7ee85ab1 21025 sig_type->per_cu.tu_read = 1;
c906108c
SS
21026}
21027
c906108c
SS
21028/* Decode simple location descriptions.
21029 Given a pointer to a dwarf block that defines a location, compute
21030 the location and return the value.
21031
4cecd739
DJ
21032 NOTE drow/2003-11-18: This function is called in two situations
21033 now: for the address of static or global variables (partial symbols
21034 only) and for offsets into structures which are expected to be
21035 (more or less) constant. The partial symbol case should go away,
21036 and only the constant case should remain. That will let this
21037 function complain more accurately. A few special modes are allowed
21038 without complaint for global variables (for instance, global
21039 register values and thread-local values).
c906108c
SS
21040
21041 A location description containing no operations indicates that the
4cecd739 21042 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21043 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21044 callers will only want a very basic result and this can become a
21ae7a4d
JK
21045 complaint.
21046
21047 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21048
21049static CORE_ADDR
e7c27a73 21050decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21051{
e7c27a73 21052 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21053 size_t i;
21054 size_t size = blk->size;
d521ce57 21055 const gdb_byte *data = blk->data;
21ae7a4d
JK
21056 CORE_ADDR stack[64];
21057 int stacki;
21058 unsigned int bytes_read, unsnd;
21059 gdb_byte op;
c906108c 21060
21ae7a4d
JK
21061 i = 0;
21062 stacki = 0;
21063 stack[stacki] = 0;
21064 stack[++stacki] = 0;
21065
21066 while (i < size)
21067 {
21068 op = data[i++];
21069 switch (op)
21070 {
21071 case DW_OP_lit0:
21072 case DW_OP_lit1:
21073 case DW_OP_lit2:
21074 case DW_OP_lit3:
21075 case DW_OP_lit4:
21076 case DW_OP_lit5:
21077 case DW_OP_lit6:
21078 case DW_OP_lit7:
21079 case DW_OP_lit8:
21080 case DW_OP_lit9:
21081 case DW_OP_lit10:
21082 case DW_OP_lit11:
21083 case DW_OP_lit12:
21084 case DW_OP_lit13:
21085 case DW_OP_lit14:
21086 case DW_OP_lit15:
21087 case DW_OP_lit16:
21088 case DW_OP_lit17:
21089 case DW_OP_lit18:
21090 case DW_OP_lit19:
21091 case DW_OP_lit20:
21092 case DW_OP_lit21:
21093 case DW_OP_lit22:
21094 case DW_OP_lit23:
21095 case DW_OP_lit24:
21096 case DW_OP_lit25:
21097 case DW_OP_lit26:
21098 case DW_OP_lit27:
21099 case DW_OP_lit28:
21100 case DW_OP_lit29:
21101 case DW_OP_lit30:
21102 case DW_OP_lit31:
21103 stack[++stacki] = op - DW_OP_lit0;
21104 break;
f1bea926 21105
21ae7a4d
JK
21106 case DW_OP_reg0:
21107 case DW_OP_reg1:
21108 case DW_OP_reg2:
21109 case DW_OP_reg3:
21110 case DW_OP_reg4:
21111 case DW_OP_reg5:
21112 case DW_OP_reg6:
21113 case DW_OP_reg7:
21114 case DW_OP_reg8:
21115 case DW_OP_reg9:
21116 case DW_OP_reg10:
21117 case DW_OP_reg11:
21118 case DW_OP_reg12:
21119 case DW_OP_reg13:
21120 case DW_OP_reg14:
21121 case DW_OP_reg15:
21122 case DW_OP_reg16:
21123 case DW_OP_reg17:
21124 case DW_OP_reg18:
21125 case DW_OP_reg19:
21126 case DW_OP_reg20:
21127 case DW_OP_reg21:
21128 case DW_OP_reg22:
21129 case DW_OP_reg23:
21130 case DW_OP_reg24:
21131 case DW_OP_reg25:
21132 case DW_OP_reg26:
21133 case DW_OP_reg27:
21134 case DW_OP_reg28:
21135 case DW_OP_reg29:
21136 case DW_OP_reg30:
21137 case DW_OP_reg31:
21138 stack[++stacki] = op - DW_OP_reg0;
21139 if (i < size)
21140 dwarf2_complex_location_expr_complaint ();
21141 break;
c906108c 21142
21ae7a4d
JK
21143 case DW_OP_regx:
21144 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21145 i += bytes_read;
21146 stack[++stacki] = unsnd;
21147 if (i < size)
21148 dwarf2_complex_location_expr_complaint ();
21149 break;
c906108c 21150
21ae7a4d
JK
21151 case DW_OP_addr:
21152 stack[++stacki] = read_address (objfile->obfd, &data[i],
21153 cu, &bytes_read);
21154 i += bytes_read;
21155 break;
d53d4ac5 21156
21ae7a4d
JK
21157 case DW_OP_const1u:
21158 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21159 i += 1;
21160 break;
21161
21162 case DW_OP_const1s:
21163 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21164 i += 1;
21165 break;
21166
21167 case DW_OP_const2u:
21168 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21169 i += 2;
21170 break;
21171
21172 case DW_OP_const2s:
21173 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21174 i += 2;
21175 break;
d53d4ac5 21176
21ae7a4d
JK
21177 case DW_OP_const4u:
21178 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21179 i += 4;
21180 break;
21181
21182 case DW_OP_const4s:
21183 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21184 i += 4;
21185 break;
21186
585861ea
JK
21187 case DW_OP_const8u:
21188 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21189 i += 8;
21190 break;
21191
21ae7a4d
JK
21192 case DW_OP_constu:
21193 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21194 &bytes_read);
21195 i += bytes_read;
21196 break;
21197
21198 case DW_OP_consts:
21199 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21200 i += bytes_read;
21201 break;
21202
21203 case DW_OP_dup:
21204 stack[stacki + 1] = stack[stacki];
21205 stacki++;
21206 break;
21207
21208 case DW_OP_plus:
21209 stack[stacki - 1] += stack[stacki];
21210 stacki--;
21211 break;
21212
21213 case DW_OP_plus_uconst:
21214 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21215 &bytes_read);
21216 i += bytes_read;
21217 break;
21218
21219 case DW_OP_minus:
21220 stack[stacki - 1] -= stack[stacki];
21221 stacki--;
21222 break;
21223
21224 case DW_OP_deref:
21225 /* If we're not the last op, then we definitely can't encode
21226 this using GDB's address_class enum. This is valid for partial
21227 global symbols, although the variable's address will be bogus
21228 in the psymtab. */
21229 if (i < size)
21230 dwarf2_complex_location_expr_complaint ();
21231 break;
21232
21233 case DW_OP_GNU_push_tls_address:
4aa4e28b 21234 case DW_OP_form_tls_address:
21ae7a4d
JK
21235 /* The top of the stack has the offset from the beginning
21236 of the thread control block at which the variable is located. */
21237 /* Nothing should follow this operator, so the top of stack would
21238 be returned. */
21239 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21240 address will be bogus in the psymtab. Make it always at least
21241 non-zero to not look as a variable garbage collected by linker
21242 which have DW_OP_addr 0. */
21ae7a4d
JK
21243 if (i < size)
21244 dwarf2_complex_location_expr_complaint ();
585861ea 21245 stack[stacki]++;
21ae7a4d
JK
21246 break;
21247
21248 case DW_OP_GNU_uninit:
21249 break;
21250
3019eac3 21251 case DW_OP_GNU_addr_index:
49f6c839 21252 case DW_OP_GNU_const_index:
3019eac3
DE
21253 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21254 &bytes_read);
21255 i += bytes_read;
21256 break;
21257
21ae7a4d
JK
21258 default:
21259 {
f39c6ffd 21260 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21261
21262 if (name)
21263 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21264 name);
21265 else
21266 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21267 op);
21268 }
21269
21270 return (stack[stacki]);
d53d4ac5 21271 }
3c6e0cb3 21272
21ae7a4d
JK
21273 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21274 outside of the allocated space. Also enforce minimum>0. */
21275 if (stacki >= ARRAY_SIZE (stack) - 1)
21276 {
21277 complaint (&symfile_complaints,
21278 _("location description stack overflow"));
21279 return 0;
21280 }
21281
21282 if (stacki <= 0)
21283 {
21284 complaint (&symfile_complaints,
21285 _("location description stack underflow"));
21286 return 0;
21287 }
21288 }
21289 return (stack[stacki]);
c906108c
SS
21290}
21291
21292/* memory allocation interface */
21293
c906108c 21294static struct dwarf_block *
7b5a2f43 21295dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21296{
8d749320 21297 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21298}
21299
c906108c 21300static struct die_info *
b60c80d6 21301dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21302{
21303 struct die_info *die;
b60c80d6
DJ
21304 size_t size = sizeof (struct die_info);
21305
21306 if (num_attrs > 1)
21307 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21308
b60c80d6 21309 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21310 memset (die, 0, sizeof (struct die_info));
21311 return (die);
21312}
2e276125
JB
21313
21314\f
21315/* Macro support. */
21316
233d95b5
JK
21317/* Return file name relative to the compilation directory of file number I in
21318 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21319 responsible for freeing it. */
233d95b5 21320
2e276125 21321static char *
233d95b5 21322file_file_name (int file, struct line_header *lh)
2e276125 21323{
6a83a1e6
EZ
21324 /* Is the file number a valid index into the line header's file name
21325 table? Remember that file numbers start with one, not zero. */
21326 if (1 <= file && file <= lh->num_file_names)
21327 {
21328 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 21329
afa6c9ab
SL
21330 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
21331 || lh->include_dirs == NULL)
6a83a1e6 21332 return xstrdup (fe->name);
233d95b5 21333 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
b36cec19 21334 fe->name, (char *) NULL);
6a83a1e6 21335 }
2e276125
JB
21336 else
21337 {
6a83a1e6
EZ
21338 /* The compiler produced a bogus file number. We can at least
21339 record the macro definitions made in the file, even if we
21340 won't be able to find the file by name. */
21341 char fake_name[80];
9a619af0 21342
8c042590
PM
21343 xsnprintf (fake_name, sizeof (fake_name),
21344 "<bad macro file number %d>", file);
2e276125 21345
6e70227d 21346 complaint (&symfile_complaints,
6a83a1e6
EZ
21347 _("bad file number in macro information (%d)"),
21348 file);
2e276125 21349
6a83a1e6 21350 return xstrdup (fake_name);
2e276125
JB
21351 }
21352}
21353
233d95b5
JK
21354/* Return the full name of file number I in *LH's file name table.
21355 Use COMP_DIR as the name of the current directory of the
21356 compilation. The result is allocated using xmalloc; the caller is
21357 responsible for freeing it. */
21358static char *
21359file_full_name (int file, struct line_header *lh, const char *comp_dir)
21360{
21361 /* Is the file number a valid index into the line header's file name
21362 table? Remember that file numbers start with one, not zero. */
21363 if (1 <= file && file <= lh->num_file_names)
21364 {
21365 char *relative = file_file_name (file, lh);
21366
21367 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21368 return relative;
b36cec19
PA
21369 return reconcat (relative, comp_dir, SLASH_STRING,
21370 relative, (char *) NULL);
233d95b5
JK
21371 }
21372 else
21373 return file_file_name (file, lh);
21374}
21375
2e276125
JB
21376
21377static struct macro_source_file *
21378macro_start_file (int file, int line,
21379 struct macro_source_file *current_file,
43f3e411 21380 struct line_header *lh)
2e276125 21381{
233d95b5
JK
21382 /* File name relative to the compilation directory of this source file. */
21383 char *file_name = file_file_name (file, lh);
2e276125 21384
2e276125 21385 if (! current_file)
abc9d0dc 21386 {
fc474241
DE
21387 /* Note: We don't create a macro table for this compilation unit
21388 at all until we actually get a filename. */
43f3e411 21389 struct macro_table *macro_table = get_macro_table ();
fc474241 21390
abc9d0dc
TT
21391 /* If we have no current file, then this must be the start_file
21392 directive for the compilation unit's main source file. */
fc474241
DE
21393 current_file = macro_set_main (macro_table, file_name);
21394 macro_define_special (macro_table);
abc9d0dc 21395 }
2e276125 21396 else
233d95b5 21397 current_file = macro_include (current_file, line, file_name);
2e276125 21398
233d95b5 21399 xfree (file_name);
6e70227d 21400
2e276125
JB
21401 return current_file;
21402}
21403
21404
21405/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21406 followed by a null byte. */
21407static char *
21408copy_string (const char *buf, int len)
21409{
224c3ddb 21410 char *s = (char *) xmalloc (len + 1);
9a619af0 21411
2e276125
JB
21412 memcpy (s, buf, len);
21413 s[len] = '\0';
2e276125
JB
21414 return s;
21415}
21416
21417
21418static const char *
21419consume_improper_spaces (const char *p, const char *body)
21420{
21421 if (*p == ' ')
21422 {
4d3c2250 21423 complaint (&symfile_complaints,
3e43a32a
MS
21424 _("macro definition contains spaces "
21425 "in formal argument list:\n`%s'"),
4d3c2250 21426 body);
2e276125
JB
21427
21428 while (*p == ' ')
21429 p++;
21430 }
21431
21432 return p;
21433}
21434
21435
21436static void
21437parse_macro_definition (struct macro_source_file *file, int line,
21438 const char *body)
21439{
21440 const char *p;
21441
21442 /* The body string takes one of two forms. For object-like macro
21443 definitions, it should be:
21444
21445 <macro name> " " <definition>
21446
21447 For function-like macro definitions, it should be:
21448
21449 <macro name> "() " <definition>
21450 or
21451 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21452
21453 Spaces may appear only where explicitly indicated, and in the
21454 <definition>.
21455
21456 The Dwarf 2 spec says that an object-like macro's name is always
21457 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21458 the space when the macro's definition is the empty string.
2e276125
JB
21459
21460 The Dwarf 2 spec says that there should be no spaces between the
21461 formal arguments in a function-like macro's formal argument list,
21462 but versions of GCC around March 2002 include spaces after the
21463 commas. */
21464
21465
21466 /* Find the extent of the macro name. The macro name is terminated
21467 by either a space or null character (for an object-like macro) or
21468 an opening paren (for a function-like macro). */
21469 for (p = body; *p; p++)
21470 if (*p == ' ' || *p == '(')
21471 break;
21472
21473 if (*p == ' ' || *p == '\0')
21474 {
21475 /* It's an object-like macro. */
21476 int name_len = p - body;
21477 char *name = copy_string (body, name_len);
21478 const char *replacement;
21479
21480 if (*p == ' ')
21481 replacement = body + name_len + 1;
21482 else
21483 {
4d3c2250 21484 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21485 replacement = body + name_len;
21486 }
6e70227d 21487
2e276125
JB
21488 macro_define_object (file, line, name, replacement);
21489
21490 xfree (name);
21491 }
21492 else if (*p == '(')
21493 {
21494 /* It's a function-like macro. */
21495 char *name = copy_string (body, p - body);
21496 int argc = 0;
21497 int argv_size = 1;
8d749320 21498 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21499
21500 p++;
21501
21502 p = consume_improper_spaces (p, body);
21503
21504 /* Parse the formal argument list. */
21505 while (*p && *p != ')')
21506 {
21507 /* Find the extent of the current argument name. */
21508 const char *arg_start = p;
21509
21510 while (*p && *p != ',' && *p != ')' && *p != ' ')
21511 p++;
21512
21513 if (! *p || p == arg_start)
4d3c2250 21514 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21515 else
21516 {
21517 /* Make sure argv has room for the new argument. */
21518 if (argc >= argv_size)
21519 {
21520 argv_size *= 2;
224c3ddb 21521 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21522 }
21523
21524 argv[argc++] = copy_string (arg_start, p - arg_start);
21525 }
21526
21527 p = consume_improper_spaces (p, body);
21528
21529 /* Consume the comma, if present. */
21530 if (*p == ',')
21531 {
21532 p++;
21533
21534 p = consume_improper_spaces (p, body);
21535 }
21536 }
21537
21538 if (*p == ')')
21539 {
21540 p++;
21541
21542 if (*p == ' ')
21543 /* Perfectly formed definition, no complaints. */
21544 macro_define_function (file, line, name,
6e70227d 21545 argc, (const char **) argv,
2e276125
JB
21546 p + 1);
21547 else if (*p == '\0')
21548 {
21549 /* Complain, but do define it. */
4d3c2250 21550 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21551 macro_define_function (file, line, name,
6e70227d 21552 argc, (const char **) argv,
2e276125
JB
21553 p);
21554 }
21555 else
21556 /* Just complain. */
4d3c2250 21557 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21558 }
21559 else
21560 /* Just complain. */
4d3c2250 21561 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21562
21563 xfree (name);
21564 {
21565 int i;
21566
21567 for (i = 0; i < argc; i++)
21568 xfree (argv[i]);
21569 }
21570 xfree (argv);
21571 }
21572 else
4d3c2250 21573 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21574}
21575
cf2c3c16
TT
21576/* Skip some bytes from BYTES according to the form given in FORM.
21577 Returns the new pointer. */
2e276125 21578
d521ce57
TT
21579static const gdb_byte *
21580skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21581 enum dwarf_form form,
21582 unsigned int offset_size,
21583 struct dwarf2_section_info *section)
2e276125 21584{
cf2c3c16 21585 unsigned int bytes_read;
2e276125 21586
cf2c3c16 21587 switch (form)
2e276125 21588 {
cf2c3c16
TT
21589 case DW_FORM_data1:
21590 case DW_FORM_flag:
21591 ++bytes;
21592 break;
21593
21594 case DW_FORM_data2:
21595 bytes += 2;
21596 break;
21597
21598 case DW_FORM_data4:
21599 bytes += 4;
21600 break;
21601
21602 case DW_FORM_data8:
21603 bytes += 8;
21604 break;
21605
21606 case DW_FORM_string:
21607 read_direct_string (abfd, bytes, &bytes_read);
21608 bytes += bytes_read;
21609 break;
21610
21611 case DW_FORM_sec_offset:
21612 case DW_FORM_strp:
36586728 21613 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21614 bytes += offset_size;
21615 break;
21616
21617 case DW_FORM_block:
21618 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21619 bytes += bytes_read;
21620 break;
21621
21622 case DW_FORM_block1:
21623 bytes += 1 + read_1_byte (abfd, bytes);
21624 break;
21625 case DW_FORM_block2:
21626 bytes += 2 + read_2_bytes (abfd, bytes);
21627 break;
21628 case DW_FORM_block4:
21629 bytes += 4 + read_4_bytes (abfd, bytes);
21630 break;
21631
21632 case DW_FORM_sdata:
21633 case DW_FORM_udata:
3019eac3
DE
21634 case DW_FORM_GNU_addr_index:
21635 case DW_FORM_GNU_str_index:
d521ce57 21636 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21637 if (bytes == NULL)
21638 {
21639 dwarf2_section_buffer_overflow_complaint (section);
21640 return NULL;
21641 }
cf2c3c16
TT
21642 break;
21643
21644 default:
21645 {
21646 complain:
21647 complaint (&symfile_complaints,
21648 _("invalid form 0x%x in `%s'"),
a32a8923 21649 form, get_section_name (section));
cf2c3c16
TT
21650 return NULL;
21651 }
2e276125
JB
21652 }
21653
cf2c3c16
TT
21654 return bytes;
21655}
757a13d0 21656
cf2c3c16
TT
21657/* A helper for dwarf_decode_macros that handles skipping an unknown
21658 opcode. Returns an updated pointer to the macro data buffer; or,
21659 on error, issues a complaint and returns NULL. */
757a13d0 21660
d521ce57 21661static const gdb_byte *
cf2c3c16 21662skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21663 const gdb_byte **opcode_definitions,
21664 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21665 bfd *abfd,
21666 unsigned int offset_size,
21667 struct dwarf2_section_info *section)
21668{
21669 unsigned int bytes_read, i;
21670 unsigned long arg;
d521ce57 21671 const gdb_byte *defn;
2e276125 21672
cf2c3c16 21673 if (opcode_definitions[opcode] == NULL)
2e276125 21674 {
cf2c3c16
TT
21675 complaint (&symfile_complaints,
21676 _("unrecognized DW_MACFINO opcode 0x%x"),
21677 opcode);
21678 return NULL;
21679 }
2e276125 21680
cf2c3c16
TT
21681 defn = opcode_definitions[opcode];
21682 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21683 defn += bytes_read;
2e276125 21684
cf2c3c16
TT
21685 for (i = 0; i < arg; ++i)
21686 {
aead7601
SM
21687 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21688 (enum dwarf_form) defn[i], offset_size,
f664829e 21689 section);
cf2c3c16
TT
21690 if (mac_ptr == NULL)
21691 {
21692 /* skip_form_bytes already issued the complaint. */
21693 return NULL;
21694 }
21695 }
757a13d0 21696
cf2c3c16
TT
21697 return mac_ptr;
21698}
757a13d0 21699
cf2c3c16
TT
21700/* A helper function which parses the header of a macro section.
21701 If the macro section is the extended (for now called "GNU") type,
21702 then this updates *OFFSET_SIZE. Returns a pointer to just after
21703 the header, or issues a complaint and returns NULL on error. */
757a13d0 21704
d521ce57
TT
21705static const gdb_byte *
21706dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21707 bfd *abfd,
d521ce57 21708 const gdb_byte *mac_ptr,
cf2c3c16
TT
21709 unsigned int *offset_size,
21710 int section_is_gnu)
21711{
21712 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21713
cf2c3c16
TT
21714 if (section_is_gnu)
21715 {
21716 unsigned int version, flags;
757a13d0 21717
cf2c3c16 21718 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21719 if (version != 4 && version != 5)
cf2c3c16
TT
21720 {
21721 complaint (&symfile_complaints,
21722 _("unrecognized version `%d' in .debug_macro section"),
21723 version);
21724 return NULL;
21725 }
21726 mac_ptr += 2;
757a13d0 21727
cf2c3c16
TT
21728 flags = read_1_byte (abfd, mac_ptr);
21729 ++mac_ptr;
21730 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21731
cf2c3c16
TT
21732 if ((flags & 2) != 0)
21733 /* We don't need the line table offset. */
21734 mac_ptr += *offset_size;
757a13d0 21735
cf2c3c16
TT
21736 /* Vendor opcode descriptions. */
21737 if ((flags & 4) != 0)
21738 {
21739 unsigned int i, count;
757a13d0 21740
cf2c3c16
TT
21741 count = read_1_byte (abfd, mac_ptr);
21742 ++mac_ptr;
21743 for (i = 0; i < count; ++i)
21744 {
21745 unsigned int opcode, bytes_read;
21746 unsigned long arg;
21747
21748 opcode = read_1_byte (abfd, mac_ptr);
21749 ++mac_ptr;
21750 opcode_definitions[opcode] = mac_ptr;
21751 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21752 mac_ptr += bytes_read;
21753 mac_ptr += arg;
21754 }
757a13d0 21755 }
cf2c3c16 21756 }
757a13d0 21757
cf2c3c16
TT
21758 return mac_ptr;
21759}
757a13d0 21760
cf2c3c16 21761/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21762 including DW_MACRO_import. */
cf2c3c16
TT
21763
21764static void
d521ce57
TT
21765dwarf_decode_macro_bytes (bfd *abfd,
21766 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21767 struct macro_source_file *current_file,
43f3e411 21768 struct line_header *lh,
cf2c3c16 21769 struct dwarf2_section_info *section,
36586728 21770 int section_is_gnu, int section_is_dwz,
cf2c3c16 21771 unsigned int offset_size,
8fc3fc34 21772 htab_t include_hash)
cf2c3c16 21773{
4d663531 21774 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21775 enum dwarf_macro_record_type macinfo_type;
21776 int at_commandline;
d521ce57 21777 const gdb_byte *opcode_definitions[256];
757a13d0 21778
cf2c3c16
TT
21779 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21780 &offset_size, section_is_gnu);
21781 if (mac_ptr == NULL)
21782 {
21783 /* We already issued a complaint. */
21784 return;
21785 }
757a13d0
JK
21786
21787 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21788 GDB is still reading the definitions from command line. First
21789 DW_MACINFO_start_file will need to be ignored as it was already executed
21790 to create CURRENT_FILE for the main source holding also the command line
21791 definitions. On first met DW_MACINFO_start_file this flag is reset to
21792 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21793
21794 at_commandline = 1;
21795
21796 do
21797 {
21798 /* Do we at least have room for a macinfo type byte? */
21799 if (mac_ptr >= mac_end)
21800 {
f664829e 21801 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21802 break;
21803 }
21804
aead7601 21805 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21806 mac_ptr++;
21807
cf2c3c16
TT
21808 /* Note that we rely on the fact that the corresponding GNU and
21809 DWARF constants are the same. */
757a13d0
JK
21810 switch (macinfo_type)
21811 {
21812 /* A zero macinfo type indicates the end of the macro
21813 information. */
21814 case 0:
21815 break;
2e276125 21816
0af92d60
JK
21817 case DW_MACRO_define:
21818 case DW_MACRO_undef:
21819 case DW_MACRO_define_strp:
21820 case DW_MACRO_undef_strp:
21821 case DW_MACRO_define_sup:
21822 case DW_MACRO_undef_sup:
2e276125 21823 {
891d2f0b 21824 unsigned int bytes_read;
2e276125 21825 int line;
d521ce57 21826 const char *body;
cf2c3c16 21827 int is_define;
2e276125 21828
cf2c3c16
TT
21829 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21830 mac_ptr += bytes_read;
21831
0af92d60
JK
21832 if (macinfo_type == DW_MACRO_define
21833 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
21834 {
21835 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21836 mac_ptr += bytes_read;
21837 }
21838 else
21839 {
21840 LONGEST str_offset;
21841
21842 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21843 mac_ptr += offset_size;
2e276125 21844
0af92d60
JK
21845 if (macinfo_type == DW_MACRO_define_sup
21846 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 21847 || section_is_dwz)
36586728
TT
21848 {
21849 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21850
21851 body = read_indirect_string_from_dwz (dwz, str_offset);
21852 }
21853 else
21854 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21855 }
21856
0af92d60
JK
21857 is_define = (macinfo_type == DW_MACRO_define
21858 || macinfo_type == DW_MACRO_define_strp
21859 || macinfo_type == DW_MACRO_define_sup);
2e276125 21860 if (! current_file)
757a13d0
JK
21861 {
21862 /* DWARF violation as no main source is present. */
21863 complaint (&symfile_complaints,
21864 _("debug info with no main source gives macro %s "
21865 "on line %d: %s"),
cf2c3c16
TT
21866 is_define ? _("definition") : _("undefinition"),
21867 line, body);
757a13d0
JK
21868 break;
21869 }
3e43a32a
MS
21870 if ((line == 0 && !at_commandline)
21871 || (line != 0 && at_commandline))
4d3c2250 21872 complaint (&symfile_complaints,
757a13d0
JK
21873 _("debug info gives %s macro %s with %s line %d: %s"),
21874 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21875 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21876 line == 0 ? _("zero") : _("non-zero"), line, body);
21877
cf2c3c16 21878 if (is_define)
757a13d0 21879 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21880 else
21881 {
0af92d60
JK
21882 gdb_assert (macinfo_type == DW_MACRO_undef
21883 || macinfo_type == DW_MACRO_undef_strp
21884 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
21885 macro_undef (current_file, line, body);
21886 }
2e276125
JB
21887 }
21888 break;
21889
0af92d60 21890 case DW_MACRO_start_file:
2e276125 21891 {
891d2f0b 21892 unsigned int bytes_read;
2e276125
JB
21893 int line, file;
21894
21895 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21896 mac_ptr += bytes_read;
21897 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21898 mac_ptr += bytes_read;
21899
3e43a32a
MS
21900 if ((line == 0 && !at_commandline)
21901 || (line != 0 && at_commandline))
757a13d0
JK
21902 complaint (&symfile_complaints,
21903 _("debug info gives source %d included "
21904 "from %s at %s line %d"),
21905 file, at_commandline ? _("command-line") : _("file"),
21906 line == 0 ? _("zero") : _("non-zero"), line);
21907
21908 if (at_commandline)
21909 {
0af92d60 21910 /* This DW_MACRO_start_file was executed in the
cf2c3c16 21911 pass one. */
757a13d0
JK
21912 at_commandline = 0;
21913 }
21914 else
43f3e411 21915 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
21916 }
21917 break;
21918
0af92d60 21919 case DW_MACRO_end_file:
2e276125 21920 if (! current_file)
4d3c2250 21921 complaint (&symfile_complaints,
3e43a32a
MS
21922 _("macro debug info has an unmatched "
21923 "`close_file' directive"));
2e276125
JB
21924 else
21925 {
21926 current_file = current_file->included_by;
21927 if (! current_file)
21928 {
cf2c3c16 21929 enum dwarf_macro_record_type next_type;
2e276125
JB
21930
21931 /* GCC circa March 2002 doesn't produce the zero
21932 type byte marking the end of the compilation
21933 unit. Complain if it's not there, but exit no
21934 matter what. */
21935
21936 /* Do we at least have room for a macinfo type byte? */
21937 if (mac_ptr >= mac_end)
21938 {
f664829e 21939 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
21940 return;
21941 }
21942
21943 /* We don't increment mac_ptr here, so this is just
21944 a look-ahead. */
aead7601
SM
21945 next_type
21946 = (enum dwarf_macro_record_type) read_1_byte (abfd,
21947 mac_ptr);
2e276125 21948 if (next_type != 0)
4d3c2250 21949 complaint (&symfile_complaints,
3e43a32a
MS
21950 _("no terminating 0-type entry for "
21951 "macros in `.debug_macinfo' section"));
2e276125
JB
21952
21953 return;
21954 }
21955 }
21956 break;
21957
0af92d60
JK
21958 case DW_MACRO_import:
21959 case DW_MACRO_import_sup:
cf2c3c16
TT
21960 {
21961 LONGEST offset;
8fc3fc34 21962 void **slot;
a036ba48
TT
21963 bfd *include_bfd = abfd;
21964 struct dwarf2_section_info *include_section = section;
d521ce57 21965 const gdb_byte *include_mac_end = mac_end;
a036ba48 21966 int is_dwz = section_is_dwz;
d521ce57 21967 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
21968
21969 offset = read_offset_1 (abfd, mac_ptr, offset_size);
21970 mac_ptr += offset_size;
21971
0af92d60 21972 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
21973 {
21974 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21975
4d663531 21976 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 21977
a036ba48 21978 include_section = &dwz->macro;
a32a8923 21979 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
21980 include_mac_end = dwz->macro.buffer + dwz->macro.size;
21981 is_dwz = 1;
21982 }
21983
21984 new_mac_ptr = include_section->buffer + offset;
21985 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
21986
8fc3fc34
TT
21987 if (*slot != NULL)
21988 {
21989 /* This has actually happened; see
21990 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
21991 complaint (&symfile_complaints,
0af92d60 21992 _("recursive DW_MACRO_import in "
8fc3fc34
TT
21993 ".debug_macro section"));
21994 }
21995 else
21996 {
d521ce57 21997 *slot = (void *) new_mac_ptr;
36586728 21998
a036ba48 21999 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22000 include_mac_end, current_file, lh,
36586728 22001 section, section_is_gnu, is_dwz,
4d663531 22002 offset_size, include_hash);
8fc3fc34 22003
d521ce57 22004 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22005 }
cf2c3c16
TT
22006 }
22007 break;
22008
2e276125 22009 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22010 if (!section_is_gnu)
22011 {
22012 unsigned int bytes_read;
2e276125 22013
ac298888
TT
22014 /* This reads the constant, but since we don't recognize
22015 any vendor extensions, we ignore it. */
22016 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22017 mac_ptr += bytes_read;
22018 read_direct_string (abfd, mac_ptr, &bytes_read);
22019 mac_ptr += bytes_read;
2e276125 22020
cf2c3c16
TT
22021 /* We don't recognize any vendor extensions. */
22022 break;
22023 }
22024 /* FALLTHROUGH */
22025
22026 default:
22027 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22028 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22029 section);
22030 if (mac_ptr == NULL)
22031 return;
22032 break;
2e276125 22033 }
757a13d0 22034 } while (macinfo_type != 0);
2e276125 22035}
8e19ed76 22036
cf2c3c16 22037static void
09262596 22038dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22039 int section_is_gnu)
cf2c3c16 22040{
bb5ed363 22041 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22042 struct line_header *lh = cu->line_header;
22043 bfd *abfd;
d521ce57 22044 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22045 struct macro_source_file *current_file = 0;
22046 enum dwarf_macro_record_type macinfo_type;
22047 unsigned int offset_size = cu->header.offset_size;
d521ce57 22048 const gdb_byte *opcode_definitions[256];
8fc3fc34 22049 struct cleanup *cleanup;
8fc3fc34 22050 void **slot;
09262596
DE
22051 struct dwarf2_section_info *section;
22052 const char *section_name;
22053
22054 if (cu->dwo_unit != NULL)
22055 {
22056 if (section_is_gnu)
22057 {
22058 section = &cu->dwo_unit->dwo_file->sections.macro;
22059 section_name = ".debug_macro.dwo";
22060 }
22061 else
22062 {
22063 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22064 section_name = ".debug_macinfo.dwo";
22065 }
22066 }
22067 else
22068 {
22069 if (section_is_gnu)
22070 {
22071 section = &dwarf2_per_objfile->macro;
22072 section_name = ".debug_macro";
22073 }
22074 else
22075 {
22076 section = &dwarf2_per_objfile->macinfo;
22077 section_name = ".debug_macinfo";
22078 }
22079 }
cf2c3c16 22080
bb5ed363 22081 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22082 if (section->buffer == NULL)
22083 {
fceca515 22084 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22085 return;
22086 }
a32a8923 22087 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22088
22089 /* First pass: Find the name of the base filename.
22090 This filename is needed in order to process all macros whose definition
22091 (or undefinition) comes from the command line. These macros are defined
22092 before the first DW_MACINFO_start_file entry, and yet still need to be
22093 associated to the base file.
22094
22095 To determine the base file name, we scan the macro definitions until we
22096 reach the first DW_MACINFO_start_file entry. We then initialize
22097 CURRENT_FILE accordingly so that any macro definition found before the
22098 first DW_MACINFO_start_file can still be associated to the base file. */
22099
22100 mac_ptr = section->buffer + offset;
22101 mac_end = section->buffer + section->size;
22102
22103 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22104 &offset_size, section_is_gnu);
22105 if (mac_ptr == NULL)
22106 {
22107 /* We already issued a complaint. */
22108 return;
22109 }
22110
22111 do
22112 {
22113 /* Do we at least have room for a macinfo type byte? */
22114 if (mac_ptr >= mac_end)
22115 {
22116 /* Complaint is printed during the second pass as GDB will probably
22117 stop the first pass earlier upon finding
22118 DW_MACINFO_start_file. */
22119 break;
22120 }
22121
aead7601 22122 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22123 mac_ptr++;
22124
22125 /* Note that we rely on the fact that the corresponding GNU and
22126 DWARF constants are the same. */
22127 switch (macinfo_type)
22128 {
22129 /* A zero macinfo type indicates the end of the macro
22130 information. */
22131 case 0:
22132 break;
22133
0af92d60
JK
22134 case DW_MACRO_define:
22135 case DW_MACRO_undef:
cf2c3c16
TT
22136 /* Only skip the data by MAC_PTR. */
22137 {
22138 unsigned int bytes_read;
22139
22140 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22141 mac_ptr += bytes_read;
22142 read_direct_string (abfd, mac_ptr, &bytes_read);
22143 mac_ptr += bytes_read;
22144 }
22145 break;
22146
0af92d60 22147 case DW_MACRO_start_file:
cf2c3c16
TT
22148 {
22149 unsigned int bytes_read;
22150 int line, file;
22151
22152 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22153 mac_ptr += bytes_read;
22154 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22155 mac_ptr += bytes_read;
22156
43f3e411 22157 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22158 }
22159 break;
22160
0af92d60 22161 case DW_MACRO_end_file:
cf2c3c16
TT
22162 /* No data to skip by MAC_PTR. */
22163 break;
22164
0af92d60
JK
22165 case DW_MACRO_define_strp:
22166 case DW_MACRO_undef_strp:
22167 case DW_MACRO_define_sup:
22168 case DW_MACRO_undef_sup:
cf2c3c16
TT
22169 {
22170 unsigned int bytes_read;
22171
22172 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22173 mac_ptr += bytes_read;
22174 mac_ptr += offset_size;
22175 }
22176 break;
22177
0af92d60
JK
22178 case DW_MACRO_import:
22179 case DW_MACRO_import_sup:
cf2c3c16 22180 /* Note that, according to the spec, a transparent include
0af92d60 22181 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22182 skip this opcode. */
22183 mac_ptr += offset_size;
22184 break;
22185
22186 case DW_MACINFO_vendor_ext:
22187 /* Only skip the data by MAC_PTR. */
22188 if (!section_is_gnu)
22189 {
22190 unsigned int bytes_read;
22191
22192 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22193 mac_ptr += bytes_read;
22194 read_direct_string (abfd, mac_ptr, &bytes_read);
22195 mac_ptr += bytes_read;
22196 }
22197 /* FALLTHROUGH */
22198
22199 default:
22200 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22201 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22202 section);
22203 if (mac_ptr == NULL)
22204 return;
22205 break;
22206 }
22207 } while (macinfo_type != 0 && current_file == NULL);
22208
22209 /* Second pass: Process all entries.
22210
22211 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22212 command-line macro definitions/undefinitions. This flag is unset when we
22213 reach the first DW_MACINFO_start_file entry. */
22214
fc4007c9
TT
22215 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22216 htab_eq_pointer,
22217 NULL, xcalloc, xfree));
8fc3fc34 22218 mac_ptr = section->buffer + offset;
fc4007c9 22219 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22220 *slot = (void *) mac_ptr;
8fc3fc34 22221 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22222 current_file, lh, section,
fc4007c9
TT
22223 section_is_gnu, 0, offset_size,
22224 include_hash.get ());
cf2c3c16
TT
22225}
22226
8e19ed76 22227/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22228 if so return true else false. */
380bca97 22229
8e19ed76 22230static int
6e5a29e1 22231attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22232{
22233 return (attr == NULL ? 0 :
22234 attr->form == DW_FORM_block1
22235 || attr->form == DW_FORM_block2
22236 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22237 || attr->form == DW_FORM_block
22238 || attr->form == DW_FORM_exprloc);
8e19ed76 22239}
4c2df51b 22240
c6a0999f
JB
22241/* Return non-zero if ATTR's value is a section offset --- classes
22242 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22243 You may use DW_UNSND (attr) to retrieve such offsets.
22244
22245 Section 7.5.4, "Attribute Encodings", explains that no attribute
22246 may have a value that belongs to more than one of these classes; it
22247 would be ambiguous if we did, because we use the same forms for all
22248 of them. */
380bca97 22249
3690dd37 22250static int
6e5a29e1 22251attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22252{
22253 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22254 || attr->form == DW_FORM_data8
22255 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22256}
22257
3690dd37
JB
22258/* Return non-zero if ATTR's value falls in the 'constant' class, or
22259 zero otherwise. When this function returns true, you can apply
22260 dwarf2_get_attr_constant_value to it.
22261
22262 However, note that for some attributes you must check
22263 attr_form_is_section_offset before using this test. DW_FORM_data4
22264 and DW_FORM_data8 are members of both the constant class, and of
22265 the classes that contain offsets into other debug sections
22266 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22267 that, if an attribute's can be either a constant or one of the
22268 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22269 taken as section offsets, not constants. */
380bca97 22270
3690dd37 22271static int
6e5a29e1 22272attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22273{
22274 switch (attr->form)
22275 {
22276 case DW_FORM_sdata:
22277 case DW_FORM_udata:
22278 case DW_FORM_data1:
22279 case DW_FORM_data2:
22280 case DW_FORM_data4:
22281 case DW_FORM_data8:
22282 return 1;
22283 default:
22284 return 0;
22285 }
22286}
22287
7771576e
SA
22288
22289/* DW_ADDR is always stored already as sect_offset; despite for the forms
22290 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22291
22292static int
6e5a29e1 22293attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22294{
22295 switch (attr->form)
22296 {
22297 case DW_FORM_ref_addr:
22298 case DW_FORM_ref1:
22299 case DW_FORM_ref2:
22300 case DW_FORM_ref4:
22301 case DW_FORM_ref8:
22302 case DW_FORM_ref_udata:
22303 case DW_FORM_GNU_ref_alt:
22304 return 1;
22305 default:
22306 return 0;
22307 }
22308}
22309
3019eac3
DE
22310/* Return the .debug_loc section to use for CU.
22311 For DWO files use .debug_loc.dwo. */
22312
22313static struct dwarf2_section_info *
22314cu_debug_loc_section (struct dwarf2_cu *cu)
22315{
22316 if (cu->dwo_unit)
43988095
JK
22317 {
22318 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22319
22320 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22321 }
22322 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22323 : &dwarf2_per_objfile->loc);
3019eac3
DE
22324}
22325
8cf6f0b1
TT
22326/* A helper function that fills in a dwarf2_loclist_baton. */
22327
22328static void
22329fill_in_loclist_baton (struct dwarf2_cu *cu,
22330 struct dwarf2_loclist_baton *baton,
ff39bb5e 22331 const struct attribute *attr)
8cf6f0b1 22332{
3019eac3
DE
22333 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22334
22335 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22336
22337 baton->per_cu = cu->per_cu;
22338 gdb_assert (baton->per_cu);
22339 /* We don't know how long the location list is, but make sure we
22340 don't run off the edge of the section. */
3019eac3
DE
22341 baton->size = section->size - DW_UNSND (attr);
22342 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22343 baton->base_address = cu->base_address;
f664829e 22344 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22345}
22346
4c2df51b 22347static void
ff39bb5e 22348dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22349 struct dwarf2_cu *cu, int is_block)
4c2df51b 22350{
bb5ed363 22351 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22352 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22353
3690dd37 22354 if (attr_form_is_section_offset (attr)
3019eac3 22355 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22356 the section. If so, fall through to the complaint in the
22357 other branch. */
3019eac3 22358 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22359 {
0d53c4c4 22360 struct dwarf2_loclist_baton *baton;
4c2df51b 22361
8d749320 22362 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22363
8cf6f0b1 22364 fill_in_loclist_baton (cu, baton, attr);
be391dca 22365
d00adf39 22366 if (cu->base_known == 0)
0d53c4c4 22367 complaint (&symfile_complaints,
3e43a32a
MS
22368 _("Location list used without "
22369 "specifying the CU base address."));
4c2df51b 22370
f1e6e072
TT
22371 SYMBOL_ACLASS_INDEX (sym) = (is_block
22372 ? dwarf2_loclist_block_index
22373 : dwarf2_loclist_index);
0d53c4c4
DJ
22374 SYMBOL_LOCATION_BATON (sym) = baton;
22375 }
22376 else
22377 {
22378 struct dwarf2_locexpr_baton *baton;
22379
8d749320 22380 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22381 baton->per_cu = cu->per_cu;
22382 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22383
22384 if (attr_form_is_block (attr))
22385 {
22386 /* Note that we're just copying the block's data pointer
22387 here, not the actual data. We're still pointing into the
6502dd73
DJ
22388 info_buffer for SYM's objfile; right now we never release
22389 that buffer, but when we do clean up properly this may
22390 need to change. */
0d53c4c4
DJ
22391 baton->size = DW_BLOCK (attr)->size;
22392 baton->data = DW_BLOCK (attr)->data;
22393 }
22394 else
22395 {
22396 dwarf2_invalid_attrib_class_complaint ("location description",
22397 SYMBOL_NATURAL_NAME (sym));
22398 baton->size = 0;
0d53c4c4 22399 }
6e70227d 22400
f1e6e072
TT
22401 SYMBOL_ACLASS_INDEX (sym) = (is_block
22402 ? dwarf2_locexpr_block_index
22403 : dwarf2_locexpr_index);
0d53c4c4
DJ
22404 SYMBOL_LOCATION_BATON (sym) = baton;
22405 }
4c2df51b 22406}
6502dd73 22407
9aa1f1e3
TT
22408/* Return the OBJFILE associated with the compilation unit CU. If CU
22409 came from a separate debuginfo file, then the master objfile is
22410 returned. */
ae0d2f24
UW
22411
22412struct objfile *
22413dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22414{
9291a0cd 22415 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22416
22417 /* Return the master objfile, so that we can report and look up the
22418 correct file containing this variable. */
22419 if (objfile->separate_debug_objfile_backlink)
22420 objfile = objfile->separate_debug_objfile_backlink;
22421
22422 return objfile;
22423}
22424
96408a79
SA
22425/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22426 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22427 CU_HEADERP first. */
22428
22429static const struct comp_unit_head *
22430per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22431 struct dwarf2_per_cu_data *per_cu)
22432{
d521ce57 22433 const gdb_byte *info_ptr;
96408a79
SA
22434
22435 if (per_cu->cu)
22436 return &per_cu->cu->header;
22437
8a0459fd 22438 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
22439
22440 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22441 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22442 rcuh_kind::COMPILE);
96408a79
SA
22443
22444 return cu_headerp;
22445}
22446
ae0d2f24
UW
22447/* Return the address size given in the compilation unit header for CU. */
22448
98714339 22449int
ae0d2f24
UW
22450dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22451{
96408a79
SA
22452 struct comp_unit_head cu_header_local;
22453 const struct comp_unit_head *cu_headerp;
c471e790 22454
96408a79
SA
22455 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22456
22457 return cu_headerp->addr_size;
ae0d2f24
UW
22458}
22459
9eae7c52
TT
22460/* Return the offset size given in the compilation unit header for CU. */
22461
22462int
22463dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22464{
96408a79
SA
22465 struct comp_unit_head cu_header_local;
22466 const struct comp_unit_head *cu_headerp;
9c6c53f7 22467
96408a79
SA
22468 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22469
22470 return cu_headerp->offset_size;
22471}
22472
22473/* See its dwarf2loc.h declaration. */
22474
22475int
22476dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22477{
22478 struct comp_unit_head cu_header_local;
22479 const struct comp_unit_head *cu_headerp;
22480
22481 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22482
22483 if (cu_headerp->version == 2)
22484 return cu_headerp->addr_size;
22485 else
22486 return cu_headerp->offset_size;
181cebd4
JK
22487}
22488
9aa1f1e3
TT
22489/* Return the text offset of the CU. The returned offset comes from
22490 this CU's objfile. If this objfile came from a separate debuginfo
22491 file, then the offset may be different from the corresponding
22492 offset in the parent objfile. */
22493
22494CORE_ADDR
22495dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22496{
bb3fa9d0 22497 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22498
22499 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22500}
22501
43988095
JK
22502/* Return DWARF version number of PER_CU. */
22503
22504short
22505dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22506{
22507 return per_cu->dwarf_version;
22508}
22509
348e048f
DE
22510/* Locate the .debug_info compilation unit from CU's objfile which contains
22511 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22512
22513static struct dwarf2_per_cu_data *
b64f50a1 22514dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 22515 unsigned int offset_in_dwz,
ae038cb0
DJ
22516 struct objfile *objfile)
22517{
22518 struct dwarf2_per_cu_data *this_cu;
22519 int low, high;
36586728 22520 const sect_offset *cu_off;
ae038cb0 22521
ae038cb0
DJ
22522 low = 0;
22523 high = dwarf2_per_objfile->n_comp_units - 1;
22524 while (high > low)
22525 {
36586728 22526 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22527 int mid = low + (high - low) / 2;
9a619af0 22528
36586728
TT
22529 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22530 cu_off = &mid_cu->offset;
22531 if (mid_cu->is_dwz > offset_in_dwz
22532 || (mid_cu->is_dwz == offset_in_dwz
22533 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
22534 high = mid;
22535 else
22536 low = mid + 1;
22537 }
22538 gdb_assert (low == high);
36586728
TT
22539 this_cu = dwarf2_per_objfile->all_comp_units[low];
22540 cu_off = &this_cu->offset;
22541 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 22542 {
36586728 22543 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
22544 error (_("Dwarf Error: could not find partial DIE containing "
22545 "offset 0x%lx [in module %s]"),
b64f50a1 22546 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 22547
b64f50a1
JK
22548 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22549 <= offset.sect_off);
ae038cb0
DJ
22550 return dwarf2_per_objfile->all_comp_units[low-1];
22551 }
22552 else
22553 {
22554 this_cu = dwarf2_per_objfile->all_comp_units[low];
22555 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
22556 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22557 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22558 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
22559 return this_cu;
22560 }
22561}
22562
23745b47 22563/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22564
9816fde3 22565static void
23745b47 22566init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22567{
9816fde3 22568 memset (cu, 0, sizeof (*cu));
23745b47
DE
22569 per_cu->cu = cu;
22570 cu->per_cu = per_cu;
22571 cu->objfile = per_cu->objfile;
93311388 22572 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22573}
22574
22575/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22576
22577static void
95554aad
TT
22578prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22579 enum language pretend_language)
9816fde3
JK
22580{
22581 struct attribute *attr;
22582
22583 /* Set the language we're debugging. */
22584 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22585 if (attr)
22586 set_cu_language (DW_UNSND (attr), cu);
22587 else
9cded63f 22588 {
95554aad 22589 cu->language = pretend_language;
9cded63f
TT
22590 cu->language_defn = language_def (cu->language);
22591 }
dee91e82 22592
7d45c7c3 22593 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22594}
22595
ae038cb0
DJ
22596/* Release one cached compilation unit, CU. We unlink it from the tree
22597 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22598 the caller is responsible for that.
22599 NOTE: DATA is a void * because this function is also used as a
22600 cleanup routine. */
ae038cb0
DJ
22601
22602static void
68dc6402 22603free_heap_comp_unit (void *data)
ae038cb0 22604{
9a3c8263 22605 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22606
23745b47
DE
22607 gdb_assert (cu->per_cu != NULL);
22608 cu->per_cu->cu = NULL;
ae038cb0
DJ
22609 cu->per_cu = NULL;
22610
22611 obstack_free (&cu->comp_unit_obstack, NULL);
22612
22613 xfree (cu);
22614}
22615
72bf9492 22616/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22617 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22618 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22619
22620static void
22621free_stack_comp_unit (void *data)
22622{
9a3c8263 22623 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22624
23745b47
DE
22625 gdb_assert (cu->per_cu != NULL);
22626 cu->per_cu->cu = NULL;
22627 cu->per_cu = NULL;
22628
72bf9492
DJ
22629 obstack_free (&cu->comp_unit_obstack, NULL);
22630 cu->partial_dies = NULL;
ae038cb0
DJ
22631}
22632
22633/* Free all cached compilation units. */
22634
22635static void
22636free_cached_comp_units (void *data)
22637{
22638 struct dwarf2_per_cu_data *per_cu, **last_chain;
22639
22640 per_cu = dwarf2_per_objfile->read_in_chain;
22641 last_chain = &dwarf2_per_objfile->read_in_chain;
22642 while (per_cu != NULL)
22643 {
22644 struct dwarf2_per_cu_data *next_cu;
22645
22646 next_cu = per_cu->cu->read_in_chain;
22647
68dc6402 22648 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22649 *last_chain = next_cu;
22650
22651 per_cu = next_cu;
22652 }
22653}
22654
22655/* Increase the age counter on each cached compilation unit, and free
22656 any that are too old. */
22657
22658static void
22659age_cached_comp_units (void)
22660{
22661 struct dwarf2_per_cu_data *per_cu, **last_chain;
22662
22663 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22664 per_cu = dwarf2_per_objfile->read_in_chain;
22665 while (per_cu != NULL)
22666 {
22667 per_cu->cu->last_used ++;
b4f54984 22668 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22669 dwarf2_mark (per_cu->cu);
22670 per_cu = per_cu->cu->read_in_chain;
22671 }
22672
22673 per_cu = dwarf2_per_objfile->read_in_chain;
22674 last_chain = &dwarf2_per_objfile->read_in_chain;
22675 while (per_cu != NULL)
22676 {
22677 struct dwarf2_per_cu_data *next_cu;
22678
22679 next_cu = per_cu->cu->read_in_chain;
22680
22681 if (!per_cu->cu->mark)
22682 {
68dc6402 22683 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22684 *last_chain = next_cu;
22685 }
22686 else
22687 last_chain = &per_cu->cu->read_in_chain;
22688
22689 per_cu = next_cu;
22690 }
22691}
22692
22693/* Remove a single compilation unit from the cache. */
22694
22695static void
dee91e82 22696free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22697{
22698 struct dwarf2_per_cu_data *per_cu, **last_chain;
22699
22700 per_cu = dwarf2_per_objfile->read_in_chain;
22701 last_chain = &dwarf2_per_objfile->read_in_chain;
22702 while (per_cu != NULL)
22703 {
22704 struct dwarf2_per_cu_data *next_cu;
22705
22706 next_cu = per_cu->cu->read_in_chain;
22707
dee91e82 22708 if (per_cu == target_per_cu)
ae038cb0 22709 {
68dc6402 22710 free_heap_comp_unit (per_cu->cu);
dee91e82 22711 per_cu->cu = NULL;
ae038cb0
DJ
22712 *last_chain = next_cu;
22713 break;
22714 }
22715 else
22716 last_chain = &per_cu->cu->read_in_chain;
22717
22718 per_cu = next_cu;
22719 }
22720}
22721
fe3e1990
DJ
22722/* Release all extra memory associated with OBJFILE. */
22723
22724void
22725dwarf2_free_objfile (struct objfile *objfile)
22726{
9a3c8263
SM
22727 dwarf2_per_objfile
22728 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22729 dwarf2_objfile_data_key);
fe3e1990
DJ
22730
22731 if (dwarf2_per_objfile == NULL)
22732 return;
22733
22734 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22735 free_cached_comp_units (NULL);
22736
7b9f3c50
DE
22737 if (dwarf2_per_objfile->quick_file_names_table)
22738 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22739
527f3840
JK
22740 if (dwarf2_per_objfile->line_header_hash)
22741 htab_delete (dwarf2_per_objfile->line_header_hash);
22742
fe3e1990
DJ
22743 /* Everything else should be on the objfile obstack. */
22744}
22745
dee91e82
DE
22746/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22747 We store these in a hash table separate from the DIEs, and preserve them
22748 when the DIEs are flushed out of cache.
22749
22750 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22751 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22752 or the type may come from a DWO file. Furthermore, while it's more logical
22753 to use per_cu->section+offset, with Fission the section with the data is in
22754 the DWO file but we don't know that section at the point we need it.
22755 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22756 because we can enter the lookup routine, get_die_type_at_offset, from
22757 outside this file, and thus won't necessarily have PER_CU->cu.
22758 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22759
dee91e82 22760struct dwarf2_per_cu_offset_and_type
1c379e20 22761{
dee91e82 22762 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 22763 sect_offset offset;
1c379e20
DJ
22764 struct type *type;
22765};
22766
dee91e82 22767/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22768
22769static hashval_t
dee91e82 22770per_cu_offset_and_type_hash (const void *item)
1c379e20 22771{
9a3c8263
SM
22772 const struct dwarf2_per_cu_offset_and_type *ofs
22773 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22774
dee91e82 22775 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
22776}
22777
dee91e82 22778/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22779
22780static int
dee91e82 22781per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22782{
9a3c8263
SM
22783 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22784 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22785 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22786 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22787
dee91e82
DE
22788 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22789 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
22790}
22791
22792/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22793 table if necessary. For convenience, return TYPE.
22794
22795 The DIEs reading must have careful ordering to:
22796 * Not cause infite loops trying to read in DIEs as a prerequisite for
22797 reading current DIE.
22798 * Not trying to dereference contents of still incompletely read in types
22799 while reading in other DIEs.
22800 * Enable referencing still incompletely read in types just by a pointer to
22801 the type without accessing its fields.
22802
22803 Therefore caller should follow these rules:
22804 * Try to fetch any prerequisite types we may need to build this DIE type
22805 before building the type and calling set_die_type.
e71ec853 22806 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22807 possible before fetching more types to complete the current type.
22808 * Make the type as complete as possible before fetching more types. */
1c379e20 22809
f792889a 22810static struct type *
1c379e20
DJ
22811set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22812{
dee91e82 22813 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22814 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22815 struct attribute *attr;
22816 struct dynamic_prop prop;
1c379e20 22817
b4ba55a1
JB
22818 /* For Ada types, make sure that the gnat-specific data is always
22819 initialized (if not already set). There are a few types where
22820 we should not be doing so, because the type-specific area is
22821 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22822 where the type-specific area is used to store the floatformat).
22823 But this is not a problem, because the gnat-specific information
22824 is actually not needed for these types. */
22825 if (need_gnat_info (cu)
22826 && TYPE_CODE (type) != TYPE_CODE_FUNC
22827 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22828 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22829 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22830 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22831 && !HAVE_GNAT_AUX_INFO (type))
22832 INIT_GNAT_SPECIFIC (type);
22833
3f2f83dd
KB
22834 /* Read DW_AT_allocated and set in type. */
22835 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22836 if (attr_form_is_block (attr))
22837 {
22838 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22839 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22840 }
22841 else if (attr != NULL)
22842 {
22843 complaint (&symfile_complaints,
22844 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22845 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22846 die->offset.sect_off);
22847 }
22848
22849 /* Read DW_AT_associated and set in type. */
22850 attr = dwarf2_attr (die, DW_AT_associated, cu);
22851 if (attr_form_is_block (attr))
22852 {
22853 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22854 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22855 }
22856 else if (attr != NULL)
22857 {
22858 complaint (&symfile_complaints,
22859 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22860 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22861 die->offset.sect_off);
22862 }
22863
3cdcd0ce
JB
22864 /* Read DW_AT_data_location and set in type. */
22865 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22866 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22867 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22868
dee91e82 22869 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22870 {
dee91e82
DE
22871 dwarf2_per_objfile->die_type_hash =
22872 htab_create_alloc_ex (127,
22873 per_cu_offset_and_type_hash,
22874 per_cu_offset_and_type_eq,
22875 NULL,
22876 &objfile->objfile_obstack,
22877 hashtab_obstack_allocate,
22878 dummy_obstack_deallocate);
f792889a 22879 }
1c379e20 22880
dee91e82 22881 ofs.per_cu = cu->per_cu;
1c379e20
DJ
22882 ofs.offset = die->offset;
22883 ofs.type = type;
dee91e82
DE
22884 slot = (struct dwarf2_per_cu_offset_and_type **)
22885 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
22886 if (*slot)
22887 complaint (&symfile_complaints,
22888 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 22889 die->offset.sect_off);
8d749320
SM
22890 *slot = XOBNEW (&objfile->objfile_obstack,
22891 struct dwarf2_per_cu_offset_and_type);
1c379e20 22892 **slot = ofs;
f792889a 22893 return type;
1c379e20
DJ
22894}
22895
02142a6c
DE
22896/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22897 or return NULL if the die does not have a saved type. */
1c379e20
DJ
22898
22899static struct type *
b64f50a1 22900get_die_type_at_offset (sect_offset offset,
673bfd45 22901 struct dwarf2_per_cu_data *per_cu)
1c379e20 22902{
dee91e82 22903 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 22904
dee91e82 22905 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22906 return NULL;
1c379e20 22907
dee91e82 22908 ofs.per_cu = per_cu;
673bfd45 22909 ofs.offset = offset;
9a3c8263
SM
22910 slot = ((struct dwarf2_per_cu_offset_and_type *)
22911 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
22912 if (slot)
22913 return slot->type;
22914 else
22915 return NULL;
22916}
22917
02142a6c 22918/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
22919 or return NULL if DIE does not have a saved type. */
22920
22921static struct type *
22922get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22923{
22924 return get_die_type_at_offset (die->offset, cu->per_cu);
22925}
22926
10b3939b
DJ
22927/* Add a dependence relationship from CU to REF_PER_CU. */
22928
22929static void
22930dwarf2_add_dependence (struct dwarf2_cu *cu,
22931 struct dwarf2_per_cu_data *ref_per_cu)
22932{
22933 void **slot;
22934
22935 if (cu->dependencies == NULL)
22936 cu->dependencies
22937 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
22938 NULL, &cu->comp_unit_obstack,
22939 hashtab_obstack_allocate,
22940 dummy_obstack_deallocate);
22941
22942 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
22943 if (*slot == NULL)
22944 *slot = ref_per_cu;
22945}
1c379e20 22946
f504f079
DE
22947/* Subroutine of dwarf2_mark to pass to htab_traverse.
22948 Set the mark field in every compilation unit in the
ae038cb0
DJ
22949 cache that we must keep because we are keeping CU. */
22950
10b3939b
DJ
22951static int
22952dwarf2_mark_helper (void **slot, void *data)
22953{
22954 struct dwarf2_per_cu_data *per_cu;
22955
22956 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
22957
22958 /* cu->dependencies references may not yet have been ever read if QUIT aborts
22959 reading of the chain. As such dependencies remain valid it is not much
22960 useful to track and undo them during QUIT cleanups. */
22961 if (per_cu->cu == NULL)
22962 return 1;
22963
10b3939b
DJ
22964 if (per_cu->cu->mark)
22965 return 1;
22966 per_cu->cu->mark = 1;
22967
22968 if (per_cu->cu->dependencies != NULL)
22969 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
22970
22971 return 1;
22972}
22973
f504f079
DE
22974/* Set the mark field in CU and in every other compilation unit in the
22975 cache that we must keep because we are keeping CU. */
22976
ae038cb0
DJ
22977static void
22978dwarf2_mark (struct dwarf2_cu *cu)
22979{
22980 if (cu->mark)
22981 return;
22982 cu->mark = 1;
10b3939b
DJ
22983 if (cu->dependencies != NULL)
22984 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
22985}
22986
22987static void
22988dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
22989{
22990 while (per_cu)
22991 {
22992 per_cu->cu->mark = 0;
22993 per_cu = per_cu->cu->read_in_chain;
22994 }
72bf9492
DJ
22995}
22996
72bf9492
DJ
22997/* Trivial hash function for partial_die_info: the hash value of a DIE
22998 is its offset in .debug_info for this objfile. */
22999
23000static hashval_t
23001partial_die_hash (const void *item)
23002{
9a3c8263
SM
23003 const struct partial_die_info *part_die
23004 = (const struct partial_die_info *) item;
9a619af0 23005
b64f50a1 23006 return part_die->offset.sect_off;
72bf9492
DJ
23007}
23008
23009/* Trivial comparison function for partial_die_info structures: two DIEs
23010 are equal if they have the same offset. */
23011
23012static int
23013partial_die_eq (const void *item_lhs, const void *item_rhs)
23014{
9a3c8263
SM
23015 const struct partial_die_info *part_die_lhs
23016 = (const struct partial_die_info *) item_lhs;
23017 const struct partial_die_info *part_die_rhs
23018 = (const struct partial_die_info *) item_rhs;
9a619af0 23019
b64f50a1 23020 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
23021}
23022
b4f54984
DE
23023static struct cmd_list_element *set_dwarf_cmdlist;
23024static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23025
23026static void
b4f54984 23027set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23028{
b4f54984 23029 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23030 gdb_stdout);
ae038cb0
DJ
23031}
23032
23033static void
b4f54984 23034show_dwarf_cmd (char *args, int from_tty)
6e70227d 23035{
b4f54984 23036 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23037}
23038
4bf44c1c 23039/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23040
23041static void
c1bd65d0 23042dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23043{
9a3c8263 23044 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23045 int ix;
8b70b953 23046
626f2d1c
TT
23047 /* Make sure we don't accidentally use dwarf2_per_objfile while
23048 cleaning up. */
23049 dwarf2_per_objfile = NULL;
23050
59b0c7c1
JB
23051 for (ix = 0; ix < data->n_comp_units; ++ix)
23052 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23053
59b0c7c1 23054 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23055 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23056 data->all_type_units[ix]->per_cu.imported_symtabs);
23057 xfree (data->all_type_units);
95554aad 23058
8b70b953 23059 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23060
23061 if (data->dwo_files)
23062 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23063 if (data->dwp_file)
23064 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23065
23066 if (data->dwz_file && data->dwz_file->dwz_bfd)
23067 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23068}
23069
23070\f
ae2de4f8 23071/* The "save gdb-index" command. */
9291a0cd
TT
23072
23073/* The contents of the hash table we create when building the string
23074 table. */
23075struct strtab_entry
23076{
23077 offset_type offset;
23078 const char *str;
23079};
23080
559a7a62
JK
23081/* Hash function for a strtab_entry.
23082
23083 Function is used only during write_hash_table so no index format backward
23084 compatibility is needed. */
b89be57b 23085
9291a0cd
TT
23086static hashval_t
23087hash_strtab_entry (const void *e)
23088{
9a3c8263 23089 const struct strtab_entry *entry = (const struct strtab_entry *) e;
559a7a62 23090 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
23091}
23092
23093/* Equality function for a strtab_entry. */
b89be57b 23094
9291a0cd
TT
23095static int
23096eq_strtab_entry (const void *a, const void *b)
23097{
9a3c8263
SM
23098 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23099 const struct strtab_entry *eb = (const struct strtab_entry *) b;
9291a0cd
TT
23100 return !strcmp (ea->str, eb->str);
23101}
23102
23103/* Create a strtab_entry hash table. */
b89be57b 23104
9291a0cd
TT
23105static htab_t
23106create_strtab (void)
23107{
23108 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23109 xfree, xcalloc, xfree);
23110}
23111
23112/* Add a string to the constant pool. Return the string's offset in
23113 host order. */
b89be57b 23114
9291a0cd
TT
23115static offset_type
23116add_string (htab_t table, struct obstack *cpool, const char *str)
23117{
23118 void **slot;
23119 struct strtab_entry entry;
23120 struct strtab_entry *result;
23121
23122 entry.str = str;
23123 slot = htab_find_slot (table, &entry, INSERT);
23124 if (*slot)
9a3c8263 23125 result = (struct strtab_entry *) *slot;
9291a0cd
TT
23126 else
23127 {
23128 result = XNEW (struct strtab_entry);
23129 result->offset = obstack_object_size (cpool);
23130 result->str = str;
23131 obstack_grow_str0 (cpool, str);
23132 *slot = result;
23133 }
23134 return result->offset;
23135}
23136
23137/* An entry in the symbol table. */
23138struct symtab_index_entry
23139{
23140 /* The name of the symbol. */
23141 const char *name;
23142 /* The offset of the name in the constant pool. */
23143 offset_type index_offset;
23144 /* A sorted vector of the indices of all the CUs that hold an object
23145 of this name. */
23146 VEC (offset_type) *cu_indices;
23147};
23148
23149/* The symbol table. This is a power-of-2-sized hash table. */
23150struct mapped_symtab
23151{
23152 offset_type n_elements;
23153 offset_type size;
23154 struct symtab_index_entry **data;
23155};
23156
23157/* Hash function for a symtab_index_entry. */
b89be57b 23158
9291a0cd
TT
23159static hashval_t
23160hash_symtab_entry (const void *e)
23161{
9a3c8263
SM
23162 const struct symtab_index_entry *entry
23163 = (const struct symtab_index_entry *) e;
9291a0cd
TT
23164 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23165 sizeof (offset_type) * VEC_length (offset_type,
23166 entry->cu_indices),
23167 0);
23168}
23169
23170/* Equality function for a symtab_index_entry. */
b89be57b 23171
9291a0cd
TT
23172static int
23173eq_symtab_entry (const void *a, const void *b)
23174{
9a3c8263
SM
23175 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23176 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
9291a0cd
TT
23177 int len = VEC_length (offset_type, ea->cu_indices);
23178 if (len != VEC_length (offset_type, eb->cu_indices))
23179 return 0;
23180 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23181 VEC_address (offset_type, eb->cu_indices),
23182 sizeof (offset_type) * len);
23183}
23184
23185/* Destroy a symtab_index_entry. */
b89be57b 23186
9291a0cd
TT
23187static void
23188delete_symtab_entry (void *p)
23189{
9a3c8263 23190 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
9291a0cd
TT
23191 VEC_free (offset_type, entry->cu_indices);
23192 xfree (entry);
23193}
23194
23195/* Create a hash table holding symtab_index_entry objects. */
b89be57b 23196
9291a0cd 23197static htab_t
3876f04e 23198create_symbol_hash_table (void)
9291a0cd
TT
23199{
23200 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23201 delete_symtab_entry, xcalloc, xfree);
23202}
23203
23204/* Create a new mapped symtab object. */
b89be57b 23205
9291a0cd
TT
23206static struct mapped_symtab *
23207create_mapped_symtab (void)
23208{
23209 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23210 symtab->n_elements = 0;
23211 symtab->size = 1024;
23212 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23213 return symtab;
23214}
23215
23216/* Destroy a mapped_symtab. */
b89be57b 23217
9291a0cd
TT
23218static void
23219cleanup_mapped_symtab (void *p)
23220{
9a3c8263 23221 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
9291a0cd
TT
23222 /* The contents of the array are freed when the other hash table is
23223 destroyed. */
23224 xfree (symtab->data);
23225 xfree (symtab);
23226}
23227
23228/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
23229 the slot.
23230
23231 Function is used only during write_hash_table so no index format backward
23232 compatibility is needed. */
b89be57b 23233
9291a0cd
TT
23234static struct symtab_index_entry **
23235find_slot (struct mapped_symtab *symtab, const char *name)
23236{
559a7a62 23237 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
23238
23239 index = hash & (symtab->size - 1);
23240 step = ((hash * 17) & (symtab->size - 1)) | 1;
23241
23242 for (;;)
23243 {
23244 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23245 return &symtab->data[index];
23246 index = (index + step) & (symtab->size - 1);
23247 }
23248}
23249
23250/* Expand SYMTAB's hash table. */
b89be57b 23251
9291a0cd
TT
23252static void
23253hash_expand (struct mapped_symtab *symtab)
23254{
23255 offset_type old_size = symtab->size;
23256 offset_type i;
23257 struct symtab_index_entry **old_entries = symtab->data;
23258
23259 symtab->size *= 2;
23260 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23261
23262 for (i = 0; i < old_size; ++i)
23263 {
23264 if (old_entries[i])
23265 {
23266 struct symtab_index_entry **slot = find_slot (symtab,
23267 old_entries[i]->name);
23268 *slot = old_entries[i];
23269 }
23270 }
23271
23272 xfree (old_entries);
23273}
23274
156942c7
DE
23275/* Add an entry to SYMTAB. NAME is the name of the symbol.
23276 CU_INDEX is the index of the CU in which the symbol appears.
23277 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23278
9291a0cd
TT
23279static void
23280add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23281 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23282 offset_type cu_index)
23283{
23284 struct symtab_index_entry **slot;
156942c7 23285 offset_type cu_index_and_attrs;
9291a0cd
TT
23286
23287 ++symtab->n_elements;
23288 if (4 * symtab->n_elements / 3 >= symtab->size)
23289 hash_expand (symtab);
23290
23291 slot = find_slot (symtab, name);
23292 if (!*slot)
23293 {
23294 *slot = XNEW (struct symtab_index_entry);
23295 (*slot)->name = name;
156942c7 23296 /* index_offset is set later. */
9291a0cd
TT
23297 (*slot)->cu_indices = NULL;
23298 }
156942c7
DE
23299
23300 cu_index_and_attrs = 0;
23301 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23302 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23303 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23304
23305 /* We don't want to record an index value twice as we want to avoid the
23306 duplication.
23307 We process all global symbols and then all static symbols
23308 (which would allow us to avoid the duplication by only having to check
23309 the last entry pushed), but a symbol could have multiple kinds in one CU.
23310 To keep things simple we don't worry about the duplication here and
23311 sort and uniqufy the list after we've processed all symbols. */
23312 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23313}
23314
23315/* qsort helper routine for uniquify_cu_indices. */
23316
23317static int
23318offset_type_compare (const void *ap, const void *bp)
23319{
23320 offset_type a = *(offset_type *) ap;
23321 offset_type b = *(offset_type *) bp;
23322
23323 return (a > b) - (b > a);
23324}
23325
23326/* Sort and remove duplicates of all symbols' cu_indices lists. */
23327
23328static void
23329uniquify_cu_indices (struct mapped_symtab *symtab)
23330{
23331 int i;
23332
23333 for (i = 0; i < symtab->size; ++i)
23334 {
23335 struct symtab_index_entry *entry = symtab->data[i];
23336
23337 if (entry
23338 && entry->cu_indices != NULL)
23339 {
23340 unsigned int next_to_insert, next_to_check;
23341 offset_type last_value;
23342
23343 qsort (VEC_address (offset_type, entry->cu_indices),
23344 VEC_length (offset_type, entry->cu_indices),
23345 sizeof (offset_type), offset_type_compare);
23346
23347 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23348 next_to_insert = 1;
23349 for (next_to_check = 1;
23350 next_to_check < VEC_length (offset_type, entry->cu_indices);
23351 ++next_to_check)
23352 {
23353 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23354 != last_value)
23355 {
23356 last_value = VEC_index (offset_type, entry->cu_indices,
23357 next_to_check);
23358 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23359 last_value);
23360 ++next_to_insert;
23361 }
23362 }
23363 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23364 }
23365 }
9291a0cd
TT
23366}
23367
23368/* Add a vector of indices to the constant pool. */
b89be57b 23369
9291a0cd 23370static offset_type
3876f04e 23371add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
23372 struct symtab_index_entry *entry)
23373{
23374 void **slot;
23375
3876f04e 23376 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
23377 if (!*slot)
23378 {
23379 offset_type len = VEC_length (offset_type, entry->cu_indices);
23380 offset_type val = MAYBE_SWAP (len);
23381 offset_type iter;
23382 int i;
23383
23384 *slot = entry;
23385 entry->index_offset = obstack_object_size (cpool);
23386
23387 obstack_grow (cpool, &val, sizeof (val));
23388 for (i = 0;
23389 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23390 ++i)
23391 {
23392 val = MAYBE_SWAP (iter);
23393 obstack_grow (cpool, &val, sizeof (val));
23394 }
23395 }
23396 else
23397 {
9a3c8263
SM
23398 struct symtab_index_entry *old_entry
23399 = (struct symtab_index_entry *) *slot;
9291a0cd
TT
23400 entry->index_offset = old_entry->index_offset;
23401 entry = old_entry;
23402 }
23403 return entry->index_offset;
23404}
23405
23406/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23407 constant pool entries going into the obstack CPOOL. */
b89be57b 23408
9291a0cd
TT
23409static void
23410write_hash_table (struct mapped_symtab *symtab,
23411 struct obstack *output, struct obstack *cpool)
23412{
23413 offset_type i;
3876f04e 23414 htab_t symbol_hash_table;
9291a0cd
TT
23415 htab_t str_table;
23416
3876f04e 23417 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 23418 str_table = create_strtab ();
3876f04e 23419
9291a0cd
TT
23420 /* We add all the index vectors to the constant pool first, to
23421 ensure alignment is ok. */
23422 for (i = 0; i < symtab->size; ++i)
23423 {
23424 if (symtab->data[i])
3876f04e 23425 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
23426 }
23427
23428 /* Now write out the hash table. */
23429 for (i = 0; i < symtab->size; ++i)
23430 {
23431 offset_type str_off, vec_off;
23432
23433 if (symtab->data[i])
23434 {
23435 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23436 vec_off = symtab->data[i]->index_offset;
23437 }
23438 else
23439 {
23440 /* While 0 is a valid constant pool index, it is not valid
23441 to have 0 for both offsets. */
23442 str_off = 0;
23443 vec_off = 0;
23444 }
23445
23446 str_off = MAYBE_SWAP (str_off);
23447 vec_off = MAYBE_SWAP (vec_off);
23448
23449 obstack_grow (output, &str_off, sizeof (str_off));
23450 obstack_grow (output, &vec_off, sizeof (vec_off));
23451 }
23452
23453 htab_delete (str_table);
3876f04e 23454 htab_delete (symbol_hash_table);
9291a0cd
TT
23455}
23456
0a5429f6
DE
23457/* Struct to map psymtab to CU index in the index file. */
23458struct psymtab_cu_index_map
23459{
23460 struct partial_symtab *psymtab;
23461 unsigned int cu_index;
23462};
23463
23464static hashval_t
23465hash_psymtab_cu_index (const void *item)
23466{
9a3c8263
SM
23467 const struct psymtab_cu_index_map *map
23468 = (const struct psymtab_cu_index_map *) item;
0a5429f6
DE
23469
23470 return htab_hash_pointer (map->psymtab);
23471}
23472
23473static int
23474eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23475{
9a3c8263
SM
23476 const struct psymtab_cu_index_map *lhs
23477 = (const struct psymtab_cu_index_map *) item_lhs;
23478 const struct psymtab_cu_index_map *rhs
23479 = (const struct psymtab_cu_index_map *) item_rhs;
0a5429f6
DE
23480
23481 return lhs->psymtab == rhs->psymtab;
23482}
23483
23484/* Helper struct for building the address table. */
23485struct addrmap_index_data
23486{
23487 struct objfile *objfile;
23488 struct obstack *addr_obstack;
23489 htab_t cu_index_htab;
23490
23491 /* Non-zero if the previous_* fields are valid.
23492 We can't write an entry until we see the next entry (since it is only then
23493 that we know the end of the entry). */
23494 int previous_valid;
23495 /* Index of the CU in the table of all CUs in the index file. */
23496 unsigned int previous_cu_index;
0963b4bd 23497 /* Start address of the CU. */
0a5429f6
DE
23498 CORE_ADDR previous_cu_start;
23499};
23500
23501/* Write an address entry to OBSTACK. */
b89be57b 23502
9291a0cd 23503static void
0a5429f6
DE
23504add_address_entry (struct objfile *objfile, struct obstack *obstack,
23505 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23506{
0a5429f6 23507 offset_type cu_index_to_write;
948f8e3d 23508 gdb_byte addr[8];
9291a0cd
TT
23509 CORE_ADDR baseaddr;
23510
23511 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23512
0a5429f6
DE
23513 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23514 obstack_grow (obstack, addr, 8);
23515 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23516 obstack_grow (obstack, addr, 8);
23517 cu_index_to_write = MAYBE_SWAP (cu_index);
23518 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23519}
23520
23521/* Worker function for traversing an addrmap to build the address table. */
23522
23523static int
23524add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23525{
9a3c8263
SM
23526 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23527 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23528
23529 if (data->previous_valid)
23530 add_address_entry (data->objfile, data->addr_obstack,
23531 data->previous_cu_start, start_addr,
23532 data->previous_cu_index);
23533
23534 data->previous_cu_start = start_addr;
23535 if (pst != NULL)
23536 {
23537 struct psymtab_cu_index_map find_map, *map;
23538 find_map.psymtab = pst;
9a3c8263
SM
23539 map = ((struct psymtab_cu_index_map *)
23540 htab_find (data->cu_index_htab, &find_map));
0a5429f6
DE
23541 gdb_assert (map != NULL);
23542 data->previous_cu_index = map->cu_index;
23543 data->previous_valid = 1;
23544 }
23545 else
23546 data->previous_valid = 0;
23547
23548 return 0;
23549}
23550
23551/* Write OBJFILE's address map to OBSTACK.
23552 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23553 in the index file. */
23554
23555static void
23556write_address_map (struct objfile *objfile, struct obstack *obstack,
23557 htab_t cu_index_htab)
23558{
23559 struct addrmap_index_data addrmap_index_data;
23560
23561 /* When writing the address table, we have to cope with the fact that
23562 the addrmap iterator only provides the start of a region; we have to
23563 wait until the next invocation to get the start of the next region. */
23564
23565 addrmap_index_data.objfile = objfile;
23566 addrmap_index_data.addr_obstack = obstack;
23567 addrmap_index_data.cu_index_htab = cu_index_htab;
23568 addrmap_index_data.previous_valid = 0;
23569
23570 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23571 &addrmap_index_data);
23572
23573 /* It's highly unlikely the last entry (end address = 0xff...ff)
23574 is valid, but we should still handle it.
23575 The end address is recorded as the start of the next region, but that
23576 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23577 anyway. */
23578 if (addrmap_index_data.previous_valid)
23579 add_address_entry (objfile, obstack,
23580 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23581 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23582}
23583
156942c7
DE
23584/* Return the symbol kind of PSYM. */
23585
23586static gdb_index_symbol_kind
23587symbol_kind (struct partial_symbol *psym)
23588{
23589 domain_enum domain = PSYMBOL_DOMAIN (psym);
23590 enum address_class aclass = PSYMBOL_CLASS (psym);
23591
23592 switch (domain)
23593 {
23594 case VAR_DOMAIN:
23595 switch (aclass)
23596 {
23597 case LOC_BLOCK:
23598 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23599 case LOC_TYPEDEF:
23600 return GDB_INDEX_SYMBOL_KIND_TYPE;
23601 case LOC_COMPUTED:
23602 case LOC_CONST_BYTES:
23603 case LOC_OPTIMIZED_OUT:
23604 case LOC_STATIC:
23605 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23606 case LOC_CONST:
23607 /* Note: It's currently impossible to recognize psyms as enum values
23608 short of reading the type info. For now punt. */
23609 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23610 default:
23611 /* There are other LOC_FOO values that one might want to classify
23612 as variables, but dwarf2read.c doesn't currently use them. */
23613 return GDB_INDEX_SYMBOL_KIND_OTHER;
23614 }
23615 case STRUCT_DOMAIN:
23616 return GDB_INDEX_SYMBOL_KIND_TYPE;
23617 default:
23618 return GDB_INDEX_SYMBOL_KIND_OTHER;
23619 }
23620}
23621
9291a0cd 23622/* Add a list of partial symbols to SYMTAB. */
b89be57b 23623
9291a0cd
TT
23624static void
23625write_psymbols (struct mapped_symtab *symtab,
987d643c 23626 htab_t psyms_seen,
9291a0cd
TT
23627 struct partial_symbol **psymp,
23628 int count,
987d643c
TT
23629 offset_type cu_index,
23630 int is_static)
9291a0cd
TT
23631{
23632 for (; count-- > 0; ++psymp)
23633 {
156942c7
DE
23634 struct partial_symbol *psym = *psymp;
23635 void **slot;
987d643c 23636
156942c7 23637 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23638 error (_("Ada is not currently supported by the index"));
987d643c 23639
987d643c 23640 /* Only add a given psymbol once. */
156942c7 23641 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23642 if (!*slot)
23643 {
156942c7
DE
23644 gdb_index_symbol_kind kind = symbol_kind (psym);
23645
23646 *slot = psym;
23647 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23648 is_static, kind, cu_index);
987d643c 23649 }
9291a0cd
TT
23650 }
23651}
23652
23653/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23654 exception if there is an error. */
b89be57b 23655
9291a0cd
TT
23656static void
23657write_obstack (FILE *file, struct obstack *obstack)
23658{
23659 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23660 file)
23661 != obstack_object_size (obstack))
23662 error (_("couldn't data write to file"));
23663}
23664
1fd400ff
TT
23665/* A helper struct used when iterating over debug_types. */
23666struct signatured_type_index_data
23667{
23668 struct objfile *objfile;
23669 struct mapped_symtab *symtab;
23670 struct obstack *types_list;
987d643c 23671 htab_t psyms_seen;
1fd400ff
TT
23672 int cu_index;
23673};
23674
23675/* A helper function that writes a single signatured_type to an
23676 obstack. */
b89be57b 23677
1fd400ff
TT
23678static int
23679write_one_signatured_type (void **slot, void *d)
23680{
9a3c8263
SM
23681 struct signatured_type_index_data *info
23682 = (struct signatured_type_index_data *) d;
1fd400ff 23683 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23684 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23685 gdb_byte val[8];
23686
23687 write_psymbols (info->symtab,
987d643c 23688 info->psyms_seen,
3e43a32a
MS
23689 info->objfile->global_psymbols.list
23690 + psymtab->globals_offset,
987d643c
TT
23691 psymtab->n_global_syms, info->cu_index,
23692 0);
1fd400ff 23693 write_psymbols (info->symtab,
987d643c 23694 info->psyms_seen,
3e43a32a
MS
23695 info->objfile->static_psymbols.list
23696 + psymtab->statics_offset,
987d643c
TT
23697 psymtab->n_static_syms, info->cu_index,
23698 1);
1fd400ff 23699
b64f50a1
JK
23700 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23701 entry->per_cu.offset.sect_off);
1fd400ff 23702 obstack_grow (info->types_list, val, 8);
3019eac3
DE
23703 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23704 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
23705 obstack_grow (info->types_list, val, 8);
23706 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23707 obstack_grow (info->types_list, val, 8);
23708
23709 ++info->cu_index;
23710
23711 return 1;
23712}
23713
95554aad
TT
23714/* Recurse into all "included" dependencies and write their symbols as
23715 if they appeared in this psymtab. */
23716
23717static void
23718recursively_write_psymbols (struct objfile *objfile,
23719 struct partial_symtab *psymtab,
23720 struct mapped_symtab *symtab,
23721 htab_t psyms_seen,
23722 offset_type cu_index)
23723{
23724 int i;
23725
23726 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23727 if (psymtab->dependencies[i]->user != NULL)
23728 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23729 symtab, psyms_seen, cu_index);
23730
23731 write_psymbols (symtab,
23732 psyms_seen,
23733 objfile->global_psymbols.list + psymtab->globals_offset,
23734 psymtab->n_global_syms, cu_index,
23735 0);
23736 write_psymbols (symtab,
23737 psyms_seen,
23738 objfile->static_psymbols.list + psymtab->statics_offset,
23739 psymtab->n_static_syms, cu_index,
23740 1);
23741}
23742
9291a0cd 23743/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23744
9291a0cd
TT
23745static void
23746write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23747{
23748 struct cleanup *cleanup;
bef155c3 23749 char *filename;
1fd400ff
TT
23750 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23751 struct obstack cu_list, types_cu_list;
9291a0cd
TT
23752 int i;
23753 FILE *out_file;
23754 struct mapped_symtab *symtab;
23755 offset_type val, size_of_contents, total_len;
23756 struct stat st;
0a5429f6 23757 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23758
9291a0cd
TT
23759 if (dwarf2_per_objfile->using_index)
23760 error (_("Cannot use an index to create the index"));
23761
8b70b953
TT
23762 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23763 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23764
260b681b
DE
23765 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23766 return;
23767
4262abfb
JK
23768 if (stat (objfile_name (objfile), &st) < 0)
23769 perror_with_name (objfile_name (objfile));
9291a0cd 23770
4262abfb 23771 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23772 INDEX_SUFFIX, (char *) NULL);
23773 cleanup = make_cleanup (xfree, filename);
23774
614c279d 23775 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23776 if (!out_file)
23777 error (_("Can't open `%s' for writing"), filename);
23778
bef155c3 23779 gdb::unlinker unlink_file (filename);
9291a0cd
TT
23780
23781 symtab = create_mapped_symtab ();
23782 make_cleanup (cleanup_mapped_symtab, symtab);
23783
23784 obstack_init (&addr_obstack);
23785 make_cleanup_obstack_free (&addr_obstack);
23786
23787 obstack_init (&cu_list);
23788 make_cleanup_obstack_free (&cu_list);
23789
1fd400ff
TT
23790 obstack_init (&types_cu_list);
23791 make_cleanup_obstack_free (&types_cu_list);
23792
fc4007c9
TT
23793 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23794 htab_eq_pointer,
23795 NULL, xcalloc, xfree));
987d643c 23796
0a5429f6
DE
23797 /* While we're scanning CU's create a table that maps a psymtab pointer
23798 (which is what addrmap records) to its index (which is what is recorded
23799 in the index file). This will later be needed to write the address
23800 table. */
fc4007c9
TT
23801 htab_up cu_index_htab (htab_create_alloc (100,
23802 hash_psymtab_cu_index,
23803 eq_psymtab_cu_index,
23804 NULL, xcalloc, xfree));
8d749320
SM
23805 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23806 dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23807 make_cleanup (xfree, psymtab_cu_index_map);
23808
23809 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23810 work here. Also, the debug_types entries do not appear in
23811 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23812 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23813 {
3e43a32a
MS
23814 struct dwarf2_per_cu_data *per_cu
23815 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23816 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23817 gdb_byte val[8];
0a5429f6
DE
23818 struct psymtab_cu_index_map *map;
23819 void **slot;
9291a0cd 23820
92fac807
JK
23821 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23822 It may be referenced from a local scope but in such case it does not
23823 need to be present in .gdb_index. */
23824 if (psymtab == NULL)
23825 continue;
23826
95554aad 23827 if (psymtab->user == NULL)
fc4007c9
TT
23828 recursively_write_psymbols (objfile, psymtab, symtab,
23829 psyms_seen.get (), i);
9291a0cd 23830
0a5429f6
DE
23831 map = &psymtab_cu_index_map[i];
23832 map->psymtab = psymtab;
23833 map->cu_index = i;
fc4007c9 23834 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
0a5429f6
DE
23835 gdb_assert (slot != NULL);
23836 gdb_assert (*slot == NULL);
23837 *slot = map;
9291a0cd 23838
b64f50a1
JK
23839 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23840 per_cu->offset.sect_off);
9291a0cd 23841 obstack_grow (&cu_list, val, 8);
e254ef6a 23842 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23843 obstack_grow (&cu_list, val, 8);
23844 }
23845
0a5429f6 23846 /* Dump the address map. */
fc4007c9 23847 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
0a5429f6 23848
1fd400ff
TT
23849 /* Write out the .debug_type entries, if any. */
23850 if (dwarf2_per_objfile->signatured_types)
23851 {
23852 struct signatured_type_index_data sig_data;
23853
23854 sig_data.objfile = objfile;
23855 sig_data.symtab = symtab;
23856 sig_data.types_list = &types_cu_list;
fc4007c9 23857 sig_data.psyms_seen = psyms_seen.get ();
1fd400ff
TT
23858 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23859 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23860 write_one_signatured_type, &sig_data);
23861 }
23862
156942c7
DE
23863 /* Now that we've processed all symbols we can shrink their cu_indices
23864 lists. */
23865 uniquify_cu_indices (symtab);
23866
9291a0cd
TT
23867 obstack_init (&constant_pool);
23868 make_cleanup_obstack_free (&constant_pool);
23869 obstack_init (&symtab_obstack);
23870 make_cleanup_obstack_free (&symtab_obstack);
23871 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23872
23873 obstack_init (&contents);
23874 make_cleanup_obstack_free (&contents);
1fd400ff 23875 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23876 total_len = size_of_contents;
23877
23878 /* The version number. */
796a7ff8 23879 val = MAYBE_SWAP (8);
9291a0cd
TT
23880 obstack_grow (&contents, &val, sizeof (val));
23881
23882 /* The offset of the CU list from the start of the file. */
23883 val = MAYBE_SWAP (total_len);
23884 obstack_grow (&contents, &val, sizeof (val));
23885 total_len += obstack_object_size (&cu_list);
23886
1fd400ff
TT
23887 /* The offset of the types CU list from the start of the file. */
23888 val = MAYBE_SWAP (total_len);
23889 obstack_grow (&contents, &val, sizeof (val));
23890 total_len += obstack_object_size (&types_cu_list);
23891
9291a0cd
TT
23892 /* The offset of the address table from the start of the file. */
23893 val = MAYBE_SWAP (total_len);
23894 obstack_grow (&contents, &val, sizeof (val));
23895 total_len += obstack_object_size (&addr_obstack);
23896
23897 /* The offset of the symbol table from the start of the file. */
23898 val = MAYBE_SWAP (total_len);
23899 obstack_grow (&contents, &val, sizeof (val));
23900 total_len += obstack_object_size (&symtab_obstack);
23901
23902 /* The offset of the constant pool from the start of the file. */
23903 val = MAYBE_SWAP (total_len);
23904 obstack_grow (&contents, &val, sizeof (val));
23905 total_len += obstack_object_size (&constant_pool);
23906
23907 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23908
23909 write_obstack (out_file, &contents);
23910 write_obstack (out_file, &cu_list);
1fd400ff 23911 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
23912 write_obstack (out_file, &addr_obstack);
23913 write_obstack (out_file, &symtab_obstack);
23914 write_obstack (out_file, &constant_pool);
23915
23916 fclose (out_file);
23917
bef155c3
TT
23918 /* We want to keep the file. */
23919 unlink_file.keep ();
9291a0cd
TT
23920
23921 do_cleanups (cleanup);
23922}
23923
90476074
TT
23924/* Implementation of the `save gdb-index' command.
23925
23926 Note that the file format used by this command is documented in the
23927 GDB manual. Any changes here must be documented there. */
11570e71 23928
9291a0cd
TT
23929static void
23930save_gdb_index_command (char *arg, int from_tty)
23931{
23932 struct objfile *objfile;
23933
23934 if (!arg || !*arg)
96d19272 23935 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
23936
23937 ALL_OBJFILES (objfile)
23938 {
23939 struct stat st;
23940
23941 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 23942 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
23943 continue;
23944
9a3c8263
SM
23945 dwarf2_per_objfile
23946 = (struct dwarf2_per_objfile *) objfile_data (objfile,
23947 dwarf2_objfile_data_key);
9291a0cd
TT
23948 if (dwarf2_per_objfile)
23949 {
9291a0cd 23950
492d29ea 23951 TRY
9291a0cd
TT
23952 {
23953 write_psymtabs_to_index (objfile, arg);
23954 }
492d29ea
PA
23955 CATCH (except, RETURN_MASK_ERROR)
23956 {
23957 exception_fprintf (gdb_stderr, except,
23958 _("Error while writing index for `%s': "),
23959 objfile_name (objfile));
23960 }
23961 END_CATCH
9291a0cd
TT
23962 }
23963 }
dce234bc
PP
23964}
23965
9291a0cd
TT
23966\f
23967
b4f54984 23968int dwarf_always_disassemble;
9eae7c52
TT
23969
23970static void
b4f54984
DE
23971show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
23972 struct cmd_list_element *c, const char *value)
9eae7c52 23973{
3e43a32a
MS
23974 fprintf_filtered (file,
23975 _("Whether to always disassemble "
23976 "DWARF expressions is %s.\n"),
9eae7c52
TT
23977 value);
23978}
23979
900e11f9
JK
23980static void
23981show_check_physname (struct ui_file *file, int from_tty,
23982 struct cmd_list_element *c, const char *value)
23983{
23984 fprintf_filtered (file,
23985 _("Whether to check \"physname\" is %s.\n"),
23986 value);
23987}
23988
6502dd73
DJ
23989void _initialize_dwarf2_read (void);
23990
23991void
23992_initialize_dwarf2_read (void)
23993{
96d19272
JK
23994 struct cmd_list_element *c;
23995
dce234bc 23996 dwarf2_objfile_data_key
c1bd65d0 23997 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 23998
b4f54984
DE
23999 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24000Set DWARF specific variables.\n\
24001Configure DWARF variables such as the cache size"),
24002 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24003 0/*allow-unknown*/, &maintenance_set_cmdlist);
24004
b4f54984
DE
24005 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24006Show DWARF specific variables\n\
24007Show DWARF variables such as the cache size"),
24008 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24009 0/*allow-unknown*/, &maintenance_show_cmdlist);
24010
24011 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24012 &dwarf_max_cache_age, _("\
24013Set the upper bound on the age of cached DWARF compilation units."), _("\
24014Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24015A higher limit means that cached compilation units will be stored\n\
24016in memory longer, and more total memory will be used. Zero disables\n\
24017caching, which can slow down startup."),
2c5b56ce 24018 NULL,
b4f54984
DE
24019 show_dwarf_max_cache_age,
24020 &set_dwarf_cmdlist,
24021 &show_dwarf_cmdlist);
d97bc12b 24022
9eae7c52 24023 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24024 &dwarf_always_disassemble, _("\
9eae7c52
TT
24025Set whether `info address' always disassembles DWARF expressions."), _("\
24026Show whether `info address' always disassembles DWARF expressions."), _("\
24027When enabled, DWARF expressions are always printed in an assembly-like\n\
24028syntax. When disabled, expressions will be printed in a more\n\
24029conversational style, when possible."),
24030 NULL,
b4f54984
DE
24031 show_dwarf_always_disassemble,
24032 &set_dwarf_cmdlist,
24033 &show_dwarf_cmdlist);
24034
24035 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24036Set debugging of the DWARF reader."), _("\
24037Show debugging of the DWARF reader."), _("\
24038When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24039reading and symtab expansion. A value of 1 (one) provides basic\n\
24040information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24041 NULL,
24042 NULL,
24043 &setdebuglist, &showdebuglist);
24044
b4f54984
DE
24045 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24046Set debugging of the DWARF DIE reader."), _("\
24047Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24048When enabled (non-zero), DIEs are dumped after they are read in.\n\
24049The value is the maximum depth to print."),
ccce17b0
YQ
24050 NULL,
24051 NULL,
24052 &setdebuglist, &showdebuglist);
9291a0cd 24053
27e0867f
DE
24054 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24055Set debugging of the dwarf line reader."), _("\
24056Show debugging of the dwarf line reader."), _("\
24057When enabled (non-zero), line number entries are dumped as they are read in.\n\
24058A value of 1 (one) provides basic information.\n\
24059A value greater than 1 provides more verbose information."),
24060 NULL,
24061 NULL,
24062 &setdebuglist, &showdebuglist);
24063
900e11f9
JK
24064 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24065Set cross-checking of \"physname\" code against demangler."), _("\
24066Show cross-checking of \"physname\" code against demangler."), _("\
24067When enabled, GDB's internal \"physname\" code is checked against\n\
24068the demangler."),
24069 NULL, show_check_physname,
24070 &setdebuglist, &showdebuglist);
24071
e615022a
DE
24072 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24073 no_class, &use_deprecated_index_sections, _("\
24074Set whether to use deprecated gdb_index sections."), _("\
24075Show whether to use deprecated gdb_index sections."), _("\
24076When enabled, deprecated .gdb_index sections are used anyway.\n\
24077Normally they are ignored either because of a missing feature or\n\
24078performance issue.\n\
24079Warning: This option must be enabled before gdb reads the file."),
24080 NULL,
24081 NULL,
24082 &setlist, &showlist);
24083
96d19272 24084 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24085 _("\
fc1a9d6e 24086Save a gdb-index file.\n\
11570e71 24087Usage: save gdb-index DIRECTORY"),
96d19272
JK
24088 &save_cmdlist);
24089 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24090
24091 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24092 &dwarf2_locexpr_funcs);
24093 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24094 &dwarf2_loclist_funcs);
24095
24096 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24097 &dwarf2_block_frame_base_locexpr_funcs);
24098 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24099 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24100}
This page took 3.832575 seconds and 4 git commands to generate.