]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
gdb: Make ldirname return a std::string
[thirdparty/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
61baf725 3 Copyright (C) 1994-2017 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270 55#include "typeprint.h"
ccefe4c4 56#include "psympriv.h"
53ce3c39 57#include <sys/stat.h>
96d19272 58#include "completer.h"
34eaf542 59#include "vec.h"
98bfdba5 60#include "c-lang.h"
a766d390 61#include "go-lang.h"
98bfdba5 62#include "valprint.h"
3019eac3 63#include "gdbcore.h" /* for gnutarget */
156942c7 64#include "gdb/gdb-index.h"
60d5a603 65#include <ctype.h>
cbb099e8 66#include "gdb_bfd.h"
4357ac6c 67#include "f-lang.h"
05cba821 68#include "source.h"
614c279d 69#include "filestuff.h"
dc294be5 70#include "build-id.h"
22cee43f 71#include "namespace.h"
bef155c3 72#include "common/gdb_unlinker.h"
14bc53a8 73#include "common/function-view.h"
4c2df51b 74
c906108c 75#include <fcntl.h>
c906108c 76#include <sys/types.h>
325fac50 77#include <algorithm>
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
73be47f5
DE
82/* When == 1, print basic high level tracing messages.
83 When > 1, be more verbose.
b4f54984
DE
84 This is in contrast to the low level DIE reading of dwarf_die_debug. */
85static unsigned int dwarf_read_debug = 0;
45cfd468 86
d97bc12b 87/* When non-zero, dump DIEs after they are read in. */
b4f54984 88static unsigned int dwarf_die_debug = 0;
d97bc12b 89
27e0867f
DE
90/* When non-zero, dump line number entries as they are read in. */
91static unsigned int dwarf_line_debug = 0;
92
900e11f9
JK
93/* When non-zero, cross-check physname against demangler. */
94static int check_physname = 0;
95
481860b3 96/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 97static int use_deprecated_index_sections = 0;
481860b3 98
6502dd73
DJ
99static const struct objfile_data *dwarf2_objfile_data_key;
100
f1e6e072
TT
101/* The "aclass" indices for various kinds of computed DWARF symbols. */
102
103static int dwarf2_locexpr_index;
104static int dwarf2_loclist_index;
105static int dwarf2_locexpr_block_index;
106static int dwarf2_loclist_block_index;
107
73869dc2
DE
108/* A descriptor for dwarf sections.
109
110 S.ASECTION, SIZE are typically initialized when the objfile is first
111 scanned. BUFFER, READIN are filled in later when the section is read.
112 If the section contained compressed data then SIZE is updated to record
113 the uncompressed size of the section.
114
115 DWP file format V2 introduces a wrinkle that is easiest to handle by
116 creating the concept of virtual sections contained within a real section.
117 In DWP V2 the sections of the input DWO files are concatenated together
118 into one section, but section offsets are kept relative to the original
119 input section.
120 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
121 the real section this "virtual" section is contained in, and BUFFER,SIZE
122 describe the virtual section. */
123
dce234bc
PP
124struct dwarf2_section_info
125{
73869dc2
DE
126 union
127 {
e5aa3347 128 /* If this is a real section, the bfd section. */
049412e3 129 asection *section;
73869dc2 130 /* If this is a virtual section, pointer to the containing ("real")
e5aa3347 131 section. */
73869dc2
DE
132 struct dwarf2_section_info *containing_section;
133 } s;
19ac8c2e 134 /* Pointer to section data, only valid if readin. */
d521ce57 135 const gdb_byte *buffer;
73869dc2 136 /* The size of the section, real or virtual. */
dce234bc 137 bfd_size_type size;
73869dc2
DE
138 /* If this is a virtual section, the offset in the real section.
139 Only valid if is_virtual. */
140 bfd_size_type virtual_offset;
be391dca 141 /* True if we have tried to read this section. */
73869dc2
DE
142 char readin;
143 /* True if this is a virtual section, False otherwise.
049412e3 144 This specifies which of s.section and s.containing_section to use. */
73869dc2 145 char is_virtual;
dce234bc
PP
146};
147
8b70b953
TT
148typedef struct dwarf2_section_info dwarf2_section_info_def;
149DEF_VEC_O (dwarf2_section_info_def);
150
9291a0cd
TT
151/* All offsets in the index are of this type. It must be
152 architecture-independent. */
153typedef uint32_t offset_type;
154
155DEF_VEC_I (offset_type);
156
156942c7
DE
157/* Ensure only legit values are used. */
158#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
159 do { \
160 gdb_assert ((unsigned int) (value) <= 1); \
161 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
162 } while (0)
163
164/* Ensure only legit values are used. */
165#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
166 do { \
167 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
168 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
169 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
170 } while (0)
171
172/* Ensure we don't use more than the alloted nuber of bits for the CU. */
173#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
174 do { \
175 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
176 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
177 } while (0)
178
9291a0cd
TT
179/* A description of the mapped index. The file format is described in
180 a comment by the code that writes the index. */
181struct mapped_index
182{
559a7a62
JK
183 /* Index data format version. */
184 int version;
185
9291a0cd
TT
186 /* The total length of the buffer. */
187 off_t total_size;
b11b1f88 188
9291a0cd
TT
189 /* A pointer to the address table data. */
190 const gdb_byte *address_table;
b11b1f88 191
9291a0cd
TT
192 /* Size of the address table data in bytes. */
193 offset_type address_table_size;
b11b1f88 194
3876f04e
DE
195 /* The symbol table, implemented as a hash table. */
196 const offset_type *symbol_table;
b11b1f88 197
9291a0cd 198 /* Size in slots, each slot is 2 offset_types. */
3876f04e 199 offset_type symbol_table_slots;
b11b1f88 200
9291a0cd
TT
201 /* A pointer to the constant pool. */
202 const char *constant_pool;
203};
204
95554aad
TT
205typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
206DEF_VEC_P (dwarf2_per_cu_ptr);
207
52059ffd
TT
208struct tu_stats
209{
210 int nr_uniq_abbrev_tables;
211 int nr_symtabs;
212 int nr_symtab_sharers;
213 int nr_stmt_less_type_units;
214 int nr_all_type_units_reallocs;
215};
216
9cdd5dbd
DE
217/* Collection of data recorded per objfile.
218 This hangs off of dwarf2_objfile_data_key. */
219
6502dd73
DJ
220struct dwarf2_per_objfile
221{
dce234bc
PP
222 struct dwarf2_section_info info;
223 struct dwarf2_section_info abbrev;
224 struct dwarf2_section_info line;
dce234bc 225 struct dwarf2_section_info loc;
43988095 226 struct dwarf2_section_info loclists;
dce234bc 227 struct dwarf2_section_info macinfo;
cf2c3c16 228 struct dwarf2_section_info macro;
dce234bc 229 struct dwarf2_section_info str;
43988095 230 struct dwarf2_section_info line_str;
dce234bc 231 struct dwarf2_section_info ranges;
43988095 232 struct dwarf2_section_info rnglists;
3019eac3 233 struct dwarf2_section_info addr;
dce234bc
PP
234 struct dwarf2_section_info frame;
235 struct dwarf2_section_info eh_frame;
9291a0cd 236 struct dwarf2_section_info gdb_index;
ae038cb0 237
8b70b953
TT
238 VEC (dwarf2_section_info_def) *types;
239
be391dca
TT
240 /* Back link. */
241 struct objfile *objfile;
242
d467dd73 243 /* Table of all the compilation units. This is used to locate
10b3939b 244 the target compilation unit of a particular reference. */
ae038cb0
DJ
245 struct dwarf2_per_cu_data **all_comp_units;
246
247 /* The number of compilation units in ALL_COMP_UNITS. */
248 int n_comp_units;
249
1fd400ff 250 /* The number of .debug_types-related CUs. */
d467dd73 251 int n_type_units;
1fd400ff 252
6aa5f3a6
DE
253 /* The number of elements allocated in all_type_units.
254 If there are skeleton-less TUs, we add them to all_type_units lazily. */
255 int n_allocated_type_units;
256
a2ce51a0
DE
257 /* The .debug_types-related CUs (TUs).
258 This is stored in malloc space because we may realloc it. */
b4dd5633 259 struct signatured_type **all_type_units;
1fd400ff 260
f4dc4d17
DE
261 /* Table of struct type_unit_group objects.
262 The hash key is the DW_AT_stmt_list value. */
263 htab_t type_unit_groups;
72dca2f5 264
348e048f
DE
265 /* A table mapping .debug_types signatures to its signatured_type entry.
266 This is NULL if the .debug_types section hasn't been read in yet. */
267 htab_t signatured_types;
268
f4dc4d17
DE
269 /* Type unit statistics, to see how well the scaling improvements
270 are doing. */
52059ffd 271 struct tu_stats tu_stats;
f4dc4d17
DE
272
273 /* A chain of compilation units that are currently read in, so that
274 they can be freed later. */
275 struct dwarf2_per_cu_data *read_in_chain;
276
3019eac3
DE
277 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
278 This is NULL if the table hasn't been allocated yet. */
279 htab_t dwo_files;
280
80626a55
DE
281 /* Non-zero if we've check for whether there is a DWP file. */
282 int dwp_checked;
283
284 /* The DWP file if there is one, or NULL. */
285 struct dwp_file *dwp_file;
286
36586728
TT
287 /* The shared '.dwz' file, if one exists. This is used when the
288 original data was compressed using 'dwz -m'. */
289 struct dwz_file *dwz_file;
290
72dca2f5
FR
291 /* A flag indicating wether this objfile has a section loaded at a
292 VMA of 0. */
293 int has_section_at_zero;
9291a0cd 294
ae2de4f8
DE
295 /* True if we are using the mapped index,
296 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
297 unsigned char using_index;
298
ae2de4f8 299 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 300 struct mapped_index *index_table;
98bfdba5 301
7b9f3c50 302 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
303 TUs typically share line table entries with a CU, so we maintain a
304 separate table of all line table entries to support the sharing.
305 Note that while there can be way more TUs than CUs, we've already
306 sorted all the TUs into "type unit groups", grouped by their
307 DW_AT_stmt_list value. Therefore the only sharing done here is with a
308 CU and its associated TU group if there is one. */
7b9f3c50
DE
309 htab_t quick_file_names_table;
310
98bfdba5
PA
311 /* Set during partial symbol reading, to prevent queueing of full
312 symbols. */
313 int reading_partial_symbols;
673bfd45 314
dee91e82 315 /* Table mapping type DIEs to their struct type *.
673bfd45 316 This is NULL if not allocated yet.
02142a6c 317 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 318 htab_t die_type_hash;
95554aad
TT
319
320 /* The CUs we recently read. */
321 VEC (dwarf2_per_cu_ptr) *just_read_cus;
527f3840
JK
322
323 /* Table containing line_header indexed by offset and offset_in_dwz. */
324 htab_t line_header_hash;
6502dd73
DJ
325};
326
327static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 328
251d32d9 329/* Default names of the debugging sections. */
c906108c 330
233a11ab
CS
331/* Note that if the debugging section has been compressed, it might
332 have a name like .zdebug_info. */
333
9cdd5dbd
DE
334static const struct dwarf2_debug_sections dwarf2_elf_names =
335{
251d32d9
TG
336 { ".debug_info", ".zdebug_info" },
337 { ".debug_abbrev", ".zdebug_abbrev" },
338 { ".debug_line", ".zdebug_line" },
339 { ".debug_loc", ".zdebug_loc" },
43988095 340 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 341 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 342 { ".debug_macro", ".zdebug_macro" },
251d32d9 343 { ".debug_str", ".zdebug_str" },
43988095 344 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 345 { ".debug_ranges", ".zdebug_ranges" },
43988095 346 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 347 { ".debug_types", ".zdebug_types" },
3019eac3 348 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
349 { ".debug_frame", ".zdebug_frame" },
350 { ".eh_frame", NULL },
24d3216f
TT
351 { ".gdb_index", ".zgdb_index" },
352 23
251d32d9 353};
c906108c 354
80626a55 355/* List of DWO/DWP sections. */
3019eac3 356
80626a55 357static const struct dwop_section_names
3019eac3
DE
358{
359 struct dwarf2_section_names abbrev_dwo;
360 struct dwarf2_section_names info_dwo;
361 struct dwarf2_section_names line_dwo;
362 struct dwarf2_section_names loc_dwo;
43988095 363 struct dwarf2_section_names loclists_dwo;
09262596
DE
364 struct dwarf2_section_names macinfo_dwo;
365 struct dwarf2_section_names macro_dwo;
3019eac3
DE
366 struct dwarf2_section_names str_dwo;
367 struct dwarf2_section_names str_offsets_dwo;
368 struct dwarf2_section_names types_dwo;
80626a55
DE
369 struct dwarf2_section_names cu_index;
370 struct dwarf2_section_names tu_index;
3019eac3 371}
80626a55 372dwop_section_names =
3019eac3
DE
373{
374 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
375 { ".debug_info.dwo", ".zdebug_info.dwo" },
376 { ".debug_line.dwo", ".zdebug_line.dwo" },
377 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 378 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
379 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
380 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
381 { ".debug_str.dwo", ".zdebug_str.dwo" },
382 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
383 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
384 { ".debug_cu_index", ".zdebug_cu_index" },
385 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
386};
387
c906108c
SS
388/* local data types */
389
107d2387
AC
390/* The data in a compilation unit header, after target2host
391 translation, looks like this. */
c906108c 392struct comp_unit_head
a738430d 393{
c764a876 394 unsigned int length;
a738430d 395 short version;
a738430d
MK
396 unsigned char addr_size;
397 unsigned char signed_addr_p;
b64f50a1 398 sect_offset abbrev_offset;
57349743 399
a738430d
MK
400 /* Size of file offsets; either 4 or 8. */
401 unsigned int offset_size;
57349743 402
a738430d
MK
403 /* Size of the length field; either 4 or 12. */
404 unsigned int initial_length_size;
57349743 405
43988095
JK
406 enum dwarf_unit_type unit_type;
407
a738430d
MK
408 /* Offset to the first byte of this compilation unit header in the
409 .debug_info section, for resolving relative reference dies. */
b64f50a1 410 sect_offset offset;
57349743 411
d00adf39
DE
412 /* Offset to first die in this cu from the start of the cu.
413 This will be the first byte following the compilation unit header. */
b64f50a1 414 cu_offset first_die_offset;
43988095
JK
415
416 /* 64-bit signature of this type unit - it is valid only for
417 UNIT_TYPE DW_UT_type. */
418 ULONGEST signature;
419
420 /* For types, offset in the type's DIE of the type defined by this TU. */
421 cu_offset type_offset_in_tu;
a738430d 422};
c906108c 423
3da10d80
KS
424/* Type used for delaying computation of method physnames.
425 See comments for compute_delayed_physnames. */
426struct delayed_method_info
427{
428 /* The type to which the method is attached, i.e., its parent class. */
429 struct type *type;
430
431 /* The index of the method in the type's function fieldlists. */
432 int fnfield_index;
433
434 /* The index of the method in the fieldlist. */
435 int index;
436
437 /* The name of the DIE. */
438 const char *name;
439
440 /* The DIE associated with this method. */
441 struct die_info *die;
442};
443
444typedef struct delayed_method_info delayed_method_info;
445DEF_VEC_O (delayed_method_info);
446
e7c27a73
DJ
447/* Internal state when decoding a particular compilation unit. */
448struct dwarf2_cu
449{
450 /* The objfile containing this compilation unit. */
451 struct objfile *objfile;
452
d00adf39 453 /* The header of the compilation unit. */
e7c27a73 454 struct comp_unit_head header;
e142c38c 455
d00adf39
DE
456 /* Base address of this compilation unit. */
457 CORE_ADDR base_address;
458
459 /* Non-zero if base_address has been set. */
460 int base_known;
461
e142c38c
DJ
462 /* The language we are debugging. */
463 enum language language;
464 const struct language_defn *language_defn;
465
b0f35d58
DL
466 const char *producer;
467
e142c38c
DJ
468 /* The generic symbol table building routines have separate lists for
469 file scope symbols and all all other scopes (local scopes). So
470 we need to select the right one to pass to add_symbol_to_list().
471 We do it by keeping a pointer to the correct list in list_in_scope.
472
473 FIXME: The original dwarf code just treated the file scope as the
474 first local scope, and all other local scopes as nested local
475 scopes, and worked fine. Check to see if we really need to
476 distinguish these in buildsym.c. */
477 struct pending **list_in_scope;
478
433df2d4
DE
479 /* The abbrev table for this CU.
480 Normally this points to the abbrev table in the objfile.
481 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
482 struct abbrev_table *abbrev_table;
72bf9492 483
b64f50a1
JK
484 /* Hash table holding all the loaded partial DIEs
485 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
486 htab_t partial_dies;
487
488 /* Storage for things with the same lifetime as this read-in compilation
489 unit, including partial DIEs. */
490 struct obstack comp_unit_obstack;
491
ae038cb0
DJ
492 /* When multiple dwarf2_cu structures are living in memory, this field
493 chains them all together, so that they can be released efficiently.
494 We will probably also want a generation counter so that most-recently-used
495 compilation units are cached... */
496 struct dwarf2_per_cu_data *read_in_chain;
497
69d751e3 498 /* Backlink to our per_cu entry. */
ae038cb0
DJ
499 struct dwarf2_per_cu_data *per_cu;
500
501 /* How many compilation units ago was this CU last referenced? */
502 int last_used;
503
b64f50a1
JK
504 /* A hash table of DIE cu_offset for following references with
505 die_info->offset.sect_off as hash. */
51545339 506 htab_t die_hash;
10b3939b
DJ
507
508 /* Full DIEs if read in. */
509 struct die_info *dies;
510
511 /* A set of pointers to dwarf2_per_cu_data objects for compilation
512 units referenced by this one. Only set during full symbol processing;
513 partial symbol tables do not have dependencies. */
514 htab_t dependencies;
515
cb1df416
DJ
516 /* Header data from the line table, during full symbol processing. */
517 struct line_header *line_header;
518
3da10d80
KS
519 /* A list of methods which need to have physnames computed
520 after all type information has been read. */
521 VEC (delayed_method_info) *method_list;
522
96408a79
SA
523 /* To be copied to symtab->call_site_htab. */
524 htab_t call_site_htab;
525
034e5797
DE
526 /* Non-NULL if this CU came from a DWO file.
527 There is an invariant here that is important to remember:
528 Except for attributes copied from the top level DIE in the "main"
529 (or "stub") file in preparation for reading the DWO file
530 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
531 Either there isn't a DWO file (in which case this is NULL and the point
532 is moot), or there is and either we're not going to read it (in which
533 case this is NULL) or there is and we are reading it (in which case this
534 is non-NULL). */
3019eac3
DE
535 struct dwo_unit *dwo_unit;
536
537 /* The DW_AT_addr_base attribute if present, zero otherwise
538 (zero is a valid value though).
1dbab08b 539 Note this value comes from the Fission stub CU/TU's DIE. */
3019eac3
DE
540 ULONGEST addr_base;
541
2e3cf129
DE
542 /* The DW_AT_ranges_base attribute if present, zero otherwise
543 (zero is a valid value though).
1dbab08b 544 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 545 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
546 be used without needing to know whether DWO files are in use or not.
547 N.B. This does not apply to DW_AT_ranges appearing in
548 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
549 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
550 DW_AT_ranges_base *would* have to be applied, and we'd have to care
551 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
552 ULONGEST ranges_base;
553
ae038cb0
DJ
554 /* Mark used when releasing cached dies. */
555 unsigned int mark : 1;
556
8be455d7
JK
557 /* This CU references .debug_loc. See the symtab->locations_valid field.
558 This test is imperfect as there may exist optimized debug code not using
559 any location list and still facing inlining issues if handled as
560 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 561 unsigned int has_loclist : 1;
ba919b58 562
1b80a9fa
JK
563 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
564 if all the producer_is_* fields are valid. This information is cached
565 because profiling CU expansion showed excessive time spent in
566 producer_is_gxx_lt_4_6. */
ba919b58
TT
567 unsigned int checked_producer : 1;
568 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 569 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 570 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
571
572 /* When set, the file that we're processing is known to have
573 debugging info for C++ namespaces. GCC 3.3.x did not produce
574 this information, but later versions do. */
575
576 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
577};
578
10b3939b
DJ
579/* Persistent data held for a compilation unit, even when not
580 processing it. We put a pointer to this structure in the
28dee7f5 581 read_symtab_private field of the psymtab. */
10b3939b 582
ae038cb0
DJ
583struct dwarf2_per_cu_data
584{
36586728 585 /* The start offset and length of this compilation unit.
45452591 586 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
587 initial_length_size.
588 If the DIE refers to a DWO file, this is always of the original die,
589 not the DWO file. */
b64f50a1 590 sect_offset offset;
36586728 591 unsigned int length;
ae038cb0 592
43988095
JK
593 /* DWARF standard version this data has been read from (such as 4 or 5). */
594 short dwarf_version;
595
ae038cb0
DJ
596 /* Flag indicating this compilation unit will be read in before
597 any of the current compilation units are processed. */
c764a876 598 unsigned int queued : 1;
ae038cb0 599
0d99eb77
DE
600 /* This flag will be set when reading partial DIEs if we need to load
601 absolutely all DIEs for this compilation unit, instead of just the ones
602 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
603 hash table and don't find it. */
604 unsigned int load_all_dies : 1;
605
0186c6a7
DE
606 /* Non-zero if this CU is from .debug_types.
607 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
608 this is non-zero. */
3019eac3
DE
609 unsigned int is_debug_types : 1;
610
36586728
TT
611 /* Non-zero if this CU is from the .dwz file. */
612 unsigned int is_dwz : 1;
613
a2ce51a0
DE
614 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
615 This flag is only valid if is_debug_types is true.
616 We can't read a CU directly from a DWO file: There are required
617 attributes in the stub. */
618 unsigned int reading_dwo_directly : 1;
619
7ee85ab1
DE
620 /* Non-zero if the TU has been read.
621 This is used to assist the "Stay in DWO Optimization" for Fission:
622 When reading a DWO, it's faster to read TUs from the DWO instead of
623 fetching them from random other DWOs (due to comdat folding).
624 If the TU has already been read, the optimization is unnecessary
625 (and unwise - we don't want to change where gdb thinks the TU lives
626 "midflight").
627 This flag is only valid if is_debug_types is true. */
628 unsigned int tu_read : 1;
629
3019eac3
DE
630 /* The section this CU/TU lives in.
631 If the DIE refers to a DWO file, this is always the original die,
632 not the DWO file. */
8a0459fd 633 struct dwarf2_section_info *section;
348e048f 634
17ea53c3 635 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
cc12ce38
DE
636 of the CU cache it gets reset to NULL again. This is left as NULL for
637 dummy CUs (a CU header, but nothing else). */
ae038cb0 638 struct dwarf2_cu *cu;
1c379e20 639
9cdd5dbd
DE
640 /* The corresponding objfile.
641 Normally we can get the objfile from dwarf2_per_objfile.
642 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
643 struct objfile *objfile;
644
fffbe6a8
YQ
645 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
646 is active. Otherwise, the 'psymtab' field is active. */
9291a0cd
TT
647 union
648 {
649 /* The partial symbol table associated with this compilation unit,
95554aad 650 or NULL for unread partial units. */
9291a0cd
TT
651 struct partial_symtab *psymtab;
652
653 /* Data needed by the "quick" functions. */
654 struct dwarf2_per_cu_quick_data *quick;
655 } v;
95554aad 656
796a7ff8
DE
657 /* The CUs we import using DW_TAG_imported_unit. This is filled in
658 while reading psymtabs, used to compute the psymtab dependencies,
659 and then cleared. Then it is filled in again while reading full
660 symbols, and only deleted when the objfile is destroyed.
661
662 This is also used to work around a difference between the way gold
663 generates .gdb_index version <=7 and the way gdb does. Arguably this
664 is a gold bug. For symbols coming from TUs, gold records in the index
665 the CU that includes the TU instead of the TU itself. This breaks
666 dw2_lookup_symbol: It assumes that if the index says symbol X lives
667 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
668 will find X. Alas TUs live in their own symtab, so after expanding CU Y
669 we need to look in TU Z to find X. Fortunately, this is akin to
670 DW_TAG_imported_unit, so we just use the same mechanism: For
671 .gdb_index version <=7 this also records the TUs that the CU referred
672 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
673 indices so we only pay a price for gold generated indices.
674 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 675 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
676};
677
348e048f
DE
678/* Entry in the signatured_types hash table. */
679
680struct signatured_type
681{
42e7ad6c 682 /* The "per_cu" object of this type.
ac9ec31b 683 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
684 N.B.: This is the first member so that it's easy to convert pointers
685 between them. */
686 struct dwarf2_per_cu_data per_cu;
687
3019eac3 688 /* The type's signature. */
348e048f
DE
689 ULONGEST signature;
690
3019eac3 691 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
692 If this TU is a DWO stub and the definition lives in a DWO file
693 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
694 cu_offset type_offset_in_tu;
695
696 /* Offset in the section of the type's DIE.
697 If the definition lives in a DWO file, this is the offset in the
698 .debug_types.dwo section.
699 The value is zero until the actual value is known.
700 Zero is otherwise not a valid section offset. */
701 sect_offset type_offset_in_section;
0186c6a7
DE
702
703 /* Type units are grouped by their DW_AT_stmt_list entry so that they
704 can share them. This points to the containing symtab. */
705 struct type_unit_group *type_unit_group;
ac9ec31b
DE
706
707 /* The type.
708 The first time we encounter this type we fully read it in and install it
709 in the symbol tables. Subsequent times we only need the type. */
710 struct type *type;
a2ce51a0
DE
711
712 /* Containing DWO unit.
713 This field is valid iff per_cu.reading_dwo_directly. */
714 struct dwo_unit *dwo_unit;
348e048f
DE
715};
716
0186c6a7
DE
717typedef struct signatured_type *sig_type_ptr;
718DEF_VEC_P (sig_type_ptr);
719
094b34ac
DE
720/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
721 This includes type_unit_group and quick_file_names. */
722
723struct stmt_list_hash
724{
725 /* The DWO unit this table is from or NULL if there is none. */
726 struct dwo_unit *dwo_unit;
727
728 /* Offset in .debug_line or .debug_line.dwo. */
729 sect_offset line_offset;
730};
731
f4dc4d17
DE
732/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
733 an object of this type. */
734
735struct type_unit_group
736{
0186c6a7 737 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
738 To simplify things we create an artificial CU that "includes" all the
739 type units using this stmt_list so that the rest of the code still has
740 a "per_cu" handle on the symtab.
741 This PER_CU is recognized by having no section. */
8a0459fd 742#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
743 struct dwarf2_per_cu_data per_cu;
744
0186c6a7
DE
745 /* The TUs that share this DW_AT_stmt_list entry.
746 This is added to while parsing type units to build partial symtabs,
747 and is deleted afterwards and not used again. */
748 VEC (sig_type_ptr) *tus;
f4dc4d17 749
43f3e411 750 /* The compunit symtab.
094b34ac 751 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
752 so we create an essentially anonymous symtab as the compunit symtab. */
753 struct compunit_symtab *compunit_symtab;
f4dc4d17 754
094b34ac
DE
755 /* The data used to construct the hash key. */
756 struct stmt_list_hash hash;
f4dc4d17
DE
757
758 /* The number of symtabs from the line header.
759 The value here must match line_header.num_file_names. */
760 unsigned int num_symtabs;
761
762 /* The symbol tables for this TU (obtained from the files listed in
763 DW_AT_stmt_list).
764 WARNING: The order of entries here must match the order of entries
765 in the line header. After the first TU using this type_unit_group, the
766 line header for the subsequent TUs is recreated from this. This is done
767 because we need to use the same symtabs for each TU using the same
768 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
769 there's no guarantee the line header doesn't have duplicate entries. */
770 struct symtab **symtabs;
771};
772
73869dc2 773/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
774
775struct dwo_sections
776{
777 struct dwarf2_section_info abbrev;
3019eac3
DE
778 struct dwarf2_section_info line;
779 struct dwarf2_section_info loc;
43988095 780 struct dwarf2_section_info loclists;
09262596
DE
781 struct dwarf2_section_info macinfo;
782 struct dwarf2_section_info macro;
3019eac3
DE
783 struct dwarf2_section_info str;
784 struct dwarf2_section_info str_offsets;
80626a55
DE
785 /* In the case of a virtual DWO file, these two are unused. */
786 struct dwarf2_section_info info;
3019eac3
DE
787 VEC (dwarf2_section_info_def) *types;
788};
789
c88ee1f0 790/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
791
792struct dwo_unit
793{
794 /* Backlink to the containing struct dwo_file. */
795 struct dwo_file *dwo_file;
796
797 /* The "id" that distinguishes this CU/TU.
798 .debug_info calls this "dwo_id", .debug_types calls this "signature".
799 Since signatures came first, we stick with it for consistency. */
800 ULONGEST signature;
801
802 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 803 struct dwarf2_section_info *section;
3019eac3 804
19ac8c2e 805 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
3019eac3
DE
806 sect_offset offset;
807 unsigned int length;
808
809 /* For types, offset in the type's DIE of the type defined by this TU. */
810 cu_offset type_offset_in_tu;
811};
812
73869dc2
DE
813/* include/dwarf2.h defines the DWP section codes.
814 It defines a max value but it doesn't define a min value, which we
815 use for error checking, so provide one. */
816
817enum dwp_v2_section_ids
818{
819 DW_SECT_MIN = 1
820};
821
80626a55 822/* Data for one DWO file.
57d63ce2
DE
823
824 This includes virtual DWO files (a virtual DWO file is a DWO file as it
825 appears in a DWP file). DWP files don't really have DWO files per se -
826 comdat folding of types "loses" the DWO file they came from, and from
827 a high level view DWP files appear to contain a mass of random types.
828 However, to maintain consistency with the non-DWP case we pretend DWP
829 files contain virtual DWO files, and we assign each TU with one virtual
830 DWO file (generally based on the line and abbrev section offsets -
831 a heuristic that seems to work in practice). */
3019eac3
DE
832
833struct dwo_file
834{
0ac5b59e 835 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
836 For virtual DWO files the name is constructed from the section offsets
837 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
838 from related CU+TUs. */
0ac5b59e
DE
839 const char *dwo_name;
840
841 /* The DW_AT_comp_dir attribute. */
842 const char *comp_dir;
3019eac3 843
80626a55
DE
844 /* The bfd, when the file is open. Otherwise this is NULL.
845 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
846 bfd *dbfd;
3019eac3 847
73869dc2
DE
848 /* The sections that make up this DWO file.
849 Remember that for virtual DWO files in DWP V2, these are virtual
850 sections (for lack of a better name). */
3019eac3
DE
851 struct dwo_sections sections;
852
19c3d4c9
DE
853 /* The CU in the file.
854 We only support one because having more than one requires hacking the
855 dwo_name of each to match, which is highly unlikely to happen.
856 Doing this means all TUs can share comp_dir: We also assume that
857 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
858 struct dwo_unit *cu;
3019eac3
DE
859
860 /* Table of TUs in the file.
861 Each element is a struct dwo_unit. */
862 htab_t tus;
863};
864
80626a55
DE
865/* These sections are what may appear in a DWP file. */
866
867struct dwp_sections
868{
73869dc2 869 /* These are used by both DWP version 1 and 2. */
80626a55
DE
870 struct dwarf2_section_info str;
871 struct dwarf2_section_info cu_index;
872 struct dwarf2_section_info tu_index;
73869dc2
DE
873
874 /* These are only used by DWP version 2 files.
875 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
876 sections are referenced by section number, and are not recorded here.
877 In DWP version 2 there is at most one copy of all these sections, each
878 section being (effectively) comprised of the concatenation of all of the
879 individual sections that exist in the version 1 format.
880 To keep the code simple we treat each of these concatenated pieces as a
881 section itself (a virtual section?). */
882 struct dwarf2_section_info abbrev;
883 struct dwarf2_section_info info;
884 struct dwarf2_section_info line;
885 struct dwarf2_section_info loc;
886 struct dwarf2_section_info macinfo;
887 struct dwarf2_section_info macro;
888 struct dwarf2_section_info str_offsets;
889 struct dwarf2_section_info types;
80626a55
DE
890};
891
73869dc2
DE
892/* These sections are what may appear in a virtual DWO file in DWP version 1.
893 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 894
73869dc2 895struct virtual_v1_dwo_sections
80626a55
DE
896{
897 struct dwarf2_section_info abbrev;
898 struct dwarf2_section_info line;
899 struct dwarf2_section_info loc;
900 struct dwarf2_section_info macinfo;
901 struct dwarf2_section_info macro;
902 struct dwarf2_section_info str_offsets;
903 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 904 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
905 struct dwarf2_section_info info_or_types;
906};
907
73869dc2
DE
908/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
909 In version 2, the sections of the DWO files are concatenated together
910 and stored in one section of that name. Thus each ELF section contains
911 several "virtual" sections. */
912
913struct virtual_v2_dwo_sections
914{
915 bfd_size_type abbrev_offset;
916 bfd_size_type abbrev_size;
917
918 bfd_size_type line_offset;
919 bfd_size_type line_size;
920
921 bfd_size_type loc_offset;
922 bfd_size_type loc_size;
923
924 bfd_size_type macinfo_offset;
925 bfd_size_type macinfo_size;
926
927 bfd_size_type macro_offset;
928 bfd_size_type macro_size;
929
930 bfd_size_type str_offsets_offset;
931 bfd_size_type str_offsets_size;
932
933 /* Each DWP hash table entry records one CU or one TU.
934 That is recorded here, and copied to dwo_unit.section. */
935 bfd_size_type info_or_types_offset;
936 bfd_size_type info_or_types_size;
937};
938
80626a55
DE
939/* Contents of DWP hash tables. */
940
941struct dwp_hash_table
942{
73869dc2 943 uint32_t version, nr_columns;
80626a55 944 uint32_t nr_units, nr_slots;
73869dc2
DE
945 const gdb_byte *hash_table, *unit_table;
946 union
947 {
948 struct
949 {
950 const gdb_byte *indices;
951 } v1;
952 struct
953 {
954 /* This is indexed by column number and gives the id of the section
955 in that column. */
956#define MAX_NR_V2_DWO_SECTIONS \
957 (1 /* .debug_info or .debug_types */ \
958 + 1 /* .debug_abbrev */ \
959 + 1 /* .debug_line */ \
960 + 1 /* .debug_loc */ \
961 + 1 /* .debug_str_offsets */ \
962 + 1 /* .debug_macro or .debug_macinfo */)
963 int section_ids[MAX_NR_V2_DWO_SECTIONS];
964 const gdb_byte *offsets;
965 const gdb_byte *sizes;
966 } v2;
967 } section_pool;
80626a55
DE
968};
969
970/* Data for one DWP file. */
971
972struct dwp_file
973{
974 /* Name of the file. */
975 const char *name;
976
73869dc2
DE
977 /* File format version. */
978 int version;
979
93417882 980 /* The bfd. */
80626a55
DE
981 bfd *dbfd;
982
983 /* Section info for this file. */
984 struct dwp_sections sections;
985
57d63ce2 986 /* Table of CUs in the file. */
80626a55
DE
987 const struct dwp_hash_table *cus;
988
989 /* Table of TUs in the file. */
990 const struct dwp_hash_table *tus;
991
19ac8c2e
DE
992 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
993 htab_t loaded_cus;
994 htab_t loaded_tus;
80626a55 995
73869dc2
DE
996 /* Table to map ELF section numbers to their sections.
997 This is only needed for the DWP V1 file format. */
80626a55
DE
998 unsigned int num_sections;
999 asection **elf_sections;
1000};
1001
36586728
TT
1002/* This represents a '.dwz' file. */
1003
1004struct dwz_file
1005{
1006 /* A dwz file can only contain a few sections. */
1007 struct dwarf2_section_info abbrev;
1008 struct dwarf2_section_info info;
1009 struct dwarf2_section_info str;
1010 struct dwarf2_section_info line;
1011 struct dwarf2_section_info macro;
2ec9a5e0 1012 struct dwarf2_section_info gdb_index;
36586728
TT
1013
1014 /* The dwz's BFD. */
1015 bfd *dwz_bfd;
1016};
1017
0963b4bd
MS
1018/* Struct used to pass misc. parameters to read_die_and_children, et
1019 al. which are used for both .debug_info and .debug_types dies.
1020 All parameters here are unchanging for the life of the call. This
dee91e82 1021 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1022
1023struct die_reader_specs
1024{
a32a8923 1025 /* The bfd of die_section. */
93311388
DE
1026 bfd* abfd;
1027
1028 /* The CU of the DIE we are parsing. */
1029 struct dwarf2_cu *cu;
1030
80626a55 1031 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1032 struct dwo_file *dwo_file;
1033
dee91e82 1034 /* The section the die comes from.
3019eac3 1035 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1036 struct dwarf2_section_info *die_section;
1037
1038 /* die_section->buffer. */
d521ce57 1039 const gdb_byte *buffer;
f664829e
DE
1040
1041 /* The end of the buffer. */
1042 const gdb_byte *buffer_end;
a2ce51a0
DE
1043
1044 /* The value of the DW_AT_comp_dir attribute. */
1045 const char *comp_dir;
93311388
DE
1046};
1047
fd820528 1048/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 1049typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 1050 const gdb_byte *info_ptr,
dee91e82
DE
1051 struct die_info *comp_unit_die,
1052 int has_children,
1053 void *data);
1054
52059ffd
TT
1055struct file_entry
1056{
1057 const char *name;
1058 unsigned int dir_index;
1059 unsigned int mod_time;
1060 unsigned int length;
83769d0b
DE
1061 /* Non-zero if referenced by the Line Number Program. */
1062 int included_p;
1063 /* The associated symbol table, if any. */
1064 struct symtab *symtab;
52059ffd
TT
1065};
1066
debd256d
JB
1067/* The line number information for a compilation unit (found in the
1068 .debug_line section) begins with a "statement program header",
1069 which contains the following information. */
1070struct line_header
1071{
527f3840
JK
1072 /* Offset of line number information in .debug_line section. */
1073 sect_offset offset;
1074
1075 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1076 unsigned offset_in_dwz : 1;
1077
debd256d
JB
1078 unsigned int total_length;
1079 unsigned short version;
1080 unsigned int header_length;
1081 unsigned char minimum_instruction_length;
2dc7f7b3 1082 unsigned char maximum_ops_per_instruction;
debd256d
JB
1083 unsigned char default_is_stmt;
1084 int line_base;
1085 unsigned char line_range;
1086 unsigned char opcode_base;
1087
1088 /* standard_opcode_lengths[i] is the number of operands for the
1089 standard opcode whose value is i. This means that
1090 standard_opcode_lengths[0] is unused, and the last meaningful
1091 element is standard_opcode_lengths[opcode_base - 1]. */
1092 unsigned char *standard_opcode_lengths;
1093
1094 /* The include_directories table. NOTE! These strings are not
1095 allocated with xmalloc; instead, they are pointers into
1096 debug_line_buffer. If you try to free them, `free' will get
1097 indigestion. */
1098 unsigned int num_include_dirs, include_dirs_size;
d521ce57 1099 const char **include_dirs;
debd256d
JB
1100
1101 /* The file_names table. NOTE! These strings are not allocated
1102 with xmalloc; instead, they are pointers into debug_line_buffer.
1103 Don't try to free them directly. */
1104 unsigned int num_file_names, file_names_size;
52059ffd 1105 struct file_entry *file_names;
debd256d
JB
1106
1107 /* The start and end of the statement program following this
6502dd73 1108 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 1109 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 1110};
c906108c
SS
1111
1112/* When we construct a partial symbol table entry we only
0963b4bd 1113 need this much information. */
c906108c
SS
1114struct partial_die_info
1115 {
72bf9492 1116 /* Offset of this DIE. */
b64f50a1 1117 sect_offset offset;
72bf9492
DJ
1118
1119 /* DWARF-2 tag for this DIE. */
1120 ENUM_BITFIELD(dwarf_tag) tag : 16;
1121
72bf9492
DJ
1122 /* Assorted flags describing the data found in this DIE. */
1123 unsigned int has_children : 1;
1124 unsigned int is_external : 1;
1125 unsigned int is_declaration : 1;
1126 unsigned int has_type : 1;
1127 unsigned int has_specification : 1;
1128 unsigned int has_pc_info : 1;
481860b3 1129 unsigned int may_be_inlined : 1;
72bf9492 1130
0c1b455e
TT
1131 /* This DIE has been marked DW_AT_main_subprogram. */
1132 unsigned int main_subprogram : 1;
1133
72bf9492
DJ
1134 /* Flag set if the SCOPE field of this structure has been
1135 computed. */
1136 unsigned int scope_set : 1;
1137
fa4028e9
JB
1138 /* Flag set if the DIE has a byte_size attribute. */
1139 unsigned int has_byte_size : 1;
1140
ff908ebf
AW
1141 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1142 unsigned int has_const_value : 1;
1143
98bfdba5
PA
1144 /* Flag set if any of the DIE's children are template arguments. */
1145 unsigned int has_template_arguments : 1;
1146
abc72ce4
DE
1147 /* Flag set if fixup_partial_die has been called on this die. */
1148 unsigned int fixup_called : 1;
1149
36586728
TT
1150 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1151 unsigned int is_dwz : 1;
1152
1153 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1154 unsigned int spec_is_dwz : 1;
1155
72bf9492 1156 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1157 sometimes a default name for unnamed DIEs. */
15d034d0 1158 const char *name;
72bf9492 1159
abc72ce4
DE
1160 /* The linkage name, if present. */
1161 const char *linkage_name;
1162
72bf9492
DJ
1163 /* The scope to prepend to our children. This is generally
1164 allocated on the comp_unit_obstack, so will disappear
1165 when this compilation unit leaves the cache. */
15d034d0 1166 const char *scope;
72bf9492 1167
95554aad
TT
1168 /* Some data associated with the partial DIE. The tag determines
1169 which field is live. */
1170 union
1171 {
1172 /* The location description associated with this DIE, if any. */
1173 struct dwarf_block *locdesc;
1174 /* The offset of an import, for DW_TAG_imported_unit. */
1175 sect_offset offset;
1176 } d;
72bf9492
DJ
1177
1178 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1179 CORE_ADDR lowpc;
1180 CORE_ADDR highpc;
72bf9492 1181
93311388 1182 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1183 DW_AT_sibling, if any. */
abc72ce4
DE
1184 /* NOTE: This member isn't strictly necessary, read_partial_die could
1185 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1186 const gdb_byte *sibling;
72bf9492
DJ
1187
1188 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1189 DW_AT_specification (or DW_AT_abstract_origin or
1190 DW_AT_extension). */
b64f50a1 1191 sect_offset spec_offset;
72bf9492
DJ
1192
1193 /* Pointers to this DIE's parent, first child, and next sibling,
1194 if any. */
1195 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1196 };
1197
0963b4bd 1198/* This data structure holds the information of an abbrev. */
c906108c
SS
1199struct abbrev_info
1200 {
1201 unsigned int number; /* number identifying abbrev */
1202 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1203 unsigned short has_children; /* boolean */
1204 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1205 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1206 struct abbrev_info *next; /* next in chain */
1207 };
1208
1209struct attr_abbrev
1210 {
9d25dd43
DE
1211 ENUM_BITFIELD(dwarf_attribute) name : 16;
1212 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1213
1214 /* It is valid only if FORM is DW_FORM_implicit_const. */
1215 LONGEST implicit_const;
c906108c
SS
1216 };
1217
433df2d4
DE
1218/* Size of abbrev_table.abbrev_hash_table. */
1219#define ABBREV_HASH_SIZE 121
1220
1221/* Top level data structure to contain an abbreviation table. */
1222
1223struct abbrev_table
1224{
f4dc4d17
DE
1225 /* Where the abbrev table came from.
1226 This is used as a sanity check when the table is used. */
433df2d4
DE
1227 sect_offset offset;
1228
1229 /* Storage for the abbrev table. */
1230 struct obstack abbrev_obstack;
1231
1232 /* Hash table of abbrevs.
1233 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1234 It could be statically allocated, but the previous code didn't so we
1235 don't either. */
1236 struct abbrev_info **abbrevs;
1237};
1238
0963b4bd 1239/* Attributes have a name and a value. */
b60c80d6
DJ
1240struct attribute
1241 {
9d25dd43 1242 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1243 ENUM_BITFIELD(dwarf_form) form : 15;
1244
1245 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1246 field should be in u.str (existing only for DW_STRING) but it is kept
1247 here for better struct attribute alignment. */
1248 unsigned int string_is_canonical : 1;
1249
b60c80d6
DJ
1250 union
1251 {
15d034d0 1252 const char *str;
b60c80d6 1253 struct dwarf_block *blk;
43bbcdc2
PH
1254 ULONGEST unsnd;
1255 LONGEST snd;
b60c80d6 1256 CORE_ADDR addr;
ac9ec31b 1257 ULONGEST signature;
b60c80d6
DJ
1258 }
1259 u;
1260 };
1261
0963b4bd 1262/* This data structure holds a complete die structure. */
c906108c
SS
1263struct die_info
1264 {
76815b17
DE
1265 /* DWARF-2 tag for this DIE. */
1266 ENUM_BITFIELD(dwarf_tag) tag : 16;
1267
1268 /* Number of attributes */
98bfdba5
PA
1269 unsigned char num_attrs;
1270
1271 /* True if we're presently building the full type name for the
1272 type derived from this DIE. */
1273 unsigned char building_fullname : 1;
76815b17 1274
adde2bff
DE
1275 /* True if this die is in process. PR 16581. */
1276 unsigned char in_process : 1;
1277
76815b17
DE
1278 /* Abbrev number */
1279 unsigned int abbrev;
1280
93311388 1281 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1282 sect_offset offset;
78ba4af6
JB
1283
1284 /* The dies in a compilation unit form an n-ary tree. PARENT
1285 points to this die's parent; CHILD points to the first child of
1286 this node; and all the children of a given node are chained
4950bc1c 1287 together via their SIBLING fields. */
639d11d3
DC
1288 struct die_info *child; /* Its first child, if any. */
1289 struct die_info *sibling; /* Its next sibling, if any. */
1290 struct die_info *parent; /* Its parent, if any. */
c906108c 1291
b60c80d6
DJ
1292 /* An array of attributes, with NUM_ATTRS elements. There may be
1293 zero, but it's not common and zero-sized arrays are not
1294 sufficiently portable C. */
1295 struct attribute attrs[1];
c906108c
SS
1296 };
1297
0963b4bd 1298/* Get at parts of an attribute structure. */
c906108c
SS
1299
1300#define DW_STRING(attr) ((attr)->u.str)
8285870a 1301#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1302#define DW_UNSND(attr) ((attr)->u.unsnd)
1303#define DW_BLOCK(attr) ((attr)->u.blk)
1304#define DW_SND(attr) ((attr)->u.snd)
1305#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1306#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1307
0963b4bd 1308/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1309struct dwarf_block
1310 {
56eb65bd 1311 size_t size;
1d6edc3c
JK
1312
1313 /* Valid only if SIZE is not zero. */
d521ce57 1314 const gdb_byte *data;
c906108c
SS
1315 };
1316
c906108c
SS
1317#ifndef ATTR_ALLOC_CHUNK
1318#define ATTR_ALLOC_CHUNK 4
1319#endif
1320
c906108c
SS
1321/* Allocate fields for structs, unions and enums in this size. */
1322#ifndef DW_FIELD_ALLOC_CHUNK
1323#define DW_FIELD_ALLOC_CHUNK 4
1324#endif
1325
c906108c
SS
1326/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1327 but this would require a corresponding change in unpack_field_as_long
1328 and friends. */
1329static int bits_per_byte = 8;
1330
52059ffd
TT
1331struct nextfield
1332{
1333 struct nextfield *next;
1334 int accessibility;
1335 int virtuality;
1336 struct field field;
1337};
1338
1339struct nextfnfield
1340{
1341 struct nextfnfield *next;
1342 struct fn_field fnfield;
1343};
1344
1345struct fnfieldlist
1346{
1347 const char *name;
1348 int length;
1349 struct nextfnfield *head;
1350};
1351
1352struct typedef_field_list
1353{
1354 struct typedef_field field;
1355 struct typedef_field_list *next;
1356};
1357
c906108c
SS
1358/* The routines that read and process dies for a C struct or C++ class
1359 pass lists of data member fields and lists of member function fields
1360 in an instance of a field_info structure, as defined below. */
1361struct field_info
c5aa993b 1362 {
0963b4bd 1363 /* List of data member and baseclasses fields. */
52059ffd 1364 struct nextfield *fields, *baseclasses;
c906108c 1365
7d0ccb61 1366 /* Number of fields (including baseclasses). */
c5aa993b 1367 int nfields;
c906108c 1368
c5aa993b
JM
1369 /* Number of baseclasses. */
1370 int nbaseclasses;
c906108c 1371
c5aa993b
JM
1372 /* Set if the accesibility of one of the fields is not public. */
1373 int non_public_fields;
c906108c 1374
c5aa993b
JM
1375 /* Member function fields array, entries are allocated in the order they
1376 are encountered in the object file. */
52059ffd 1377 struct nextfnfield *fnfields;
c906108c 1378
c5aa993b
JM
1379 /* Member function fieldlist array, contains name of possibly overloaded
1380 member function, number of overloaded member functions and a pointer
1381 to the head of the member function field chain. */
52059ffd 1382 struct fnfieldlist *fnfieldlists;
c906108c 1383
c5aa993b
JM
1384 /* Number of entries in the fnfieldlists array. */
1385 int nfnfields;
98751a41
JK
1386
1387 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1388 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
52059ffd 1389 struct typedef_field_list *typedef_field_list;
98751a41 1390 unsigned typedef_field_list_count;
c5aa993b 1391 };
c906108c 1392
10b3939b
DJ
1393/* One item on the queue of compilation units to read in full symbols
1394 for. */
1395struct dwarf2_queue_item
1396{
1397 struct dwarf2_per_cu_data *per_cu;
95554aad 1398 enum language pretend_language;
10b3939b
DJ
1399 struct dwarf2_queue_item *next;
1400};
1401
1402/* The current queue. */
1403static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1404
ae038cb0
DJ
1405/* Loaded secondary compilation units are kept in memory until they
1406 have not been referenced for the processing of this many
1407 compilation units. Set this to zero to disable caching. Cache
1408 sizes of up to at least twenty will improve startup time for
1409 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1410static int dwarf_max_cache_age = 5;
920d2a44 1411static void
b4f54984
DE
1412show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1413 struct cmd_list_element *c, const char *value)
920d2a44 1414{
3e43a32a 1415 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1416 "DWARF compilation units is %s.\n"),
920d2a44
AC
1417 value);
1418}
4390d890 1419\f
c906108c
SS
1420/* local function prototypes */
1421
a32a8923
DE
1422static const char *get_section_name (const struct dwarf2_section_info *);
1423
1424static const char *get_section_file_name (const struct dwarf2_section_info *);
1425
4efb68b1 1426static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1427
918dd910
JK
1428static void dwarf2_find_base_address (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
0018ea6f
DE
1431static struct partial_symtab *create_partial_symtab
1432 (struct dwarf2_per_cu_data *per_cu, const char *name);
1433
c67a9c90 1434static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1435
72bf9492
DJ
1436static void scan_partial_symbols (struct partial_die_info *,
1437 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1438 int, struct dwarf2_cu *);
c906108c 1439
72bf9492
DJ
1440static void add_partial_symbol (struct partial_die_info *,
1441 struct dwarf2_cu *);
63d06c5c 1442
72bf9492
DJ
1443static void add_partial_namespace (struct partial_die_info *pdi,
1444 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1445 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1446
5d7cb8df 1447static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1448 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1449 struct dwarf2_cu *cu);
1450
72bf9492
DJ
1451static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1452 struct dwarf2_cu *cu);
91c24f0a 1453
bc30ff58
JB
1454static void add_partial_subprogram (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1456 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1457
257e7a09
YQ
1458static void dwarf2_read_symtab (struct partial_symtab *,
1459 struct objfile *);
c906108c 1460
a14ed312 1461static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1462
433df2d4
DE
1463static struct abbrev_info *abbrev_table_lookup_abbrev
1464 (const struct abbrev_table *, unsigned int);
1465
1466static struct abbrev_table *abbrev_table_read_table
1467 (struct dwarf2_section_info *, sect_offset);
1468
1469static void abbrev_table_free (struct abbrev_table *);
1470
f4dc4d17
DE
1471static void abbrev_table_free_cleanup (void *);
1472
dee91e82
DE
1473static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1474 struct dwarf2_section_info *);
c906108c 1475
f3dd6933 1476static void dwarf2_free_abbrev_table (void *);
c906108c 1477
d521ce57 1478static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1479
dee91e82 1480static struct partial_die_info *load_partial_dies
d521ce57 1481 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1482
d521ce57
TT
1483static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1484 struct partial_die_info *,
1485 struct abbrev_info *,
1486 unsigned int,
1487 const gdb_byte *);
c906108c 1488
36586728 1489static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1490 struct dwarf2_cu *);
72bf9492
DJ
1491
1492static void fixup_partial_die (struct partial_die_info *,
1493 struct dwarf2_cu *);
1494
d521ce57
TT
1495static const gdb_byte *read_attribute (const struct die_reader_specs *,
1496 struct attribute *, struct attr_abbrev *,
1497 const gdb_byte *);
a8329558 1498
a1855c1d 1499static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1500
a1855c1d 1501static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1502
a1855c1d 1503static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1504
a1855c1d 1505static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1506
a1855c1d 1507static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1508
d521ce57 1509static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1510 unsigned int *);
c906108c 1511
d521ce57 1512static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1513
1514static LONGEST read_checked_initial_length_and_offset
d521ce57 1515 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1516 unsigned int *, unsigned int *);
613e1657 1517
d521ce57
TT
1518static LONGEST read_offset (bfd *, const gdb_byte *,
1519 const struct comp_unit_head *,
c764a876
DE
1520 unsigned int *);
1521
d521ce57 1522static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1523
f4dc4d17
DE
1524static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1525 sect_offset);
1526
d521ce57 1527static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1528
d521ce57 1529static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1530
d521ce57
TT
1531static const char *read_indirect_string (bfd *, const gdb_byte *,
1532 const struct comp_unit_head *,
1533 unsigned int *);
4bdf3d34 1534
43988095
JK
1535static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1536 const struct comp_unit_head *,
1537 unsigned int *);
36586728 1538
43988095 1539static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
c906108c 1540
d521ce57 1541static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1542
d521ce57
TT
1543static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1544 const gdb_byte *,
3019eac3
DE
1545 unsigned int *);
1546
d521ce57 1547static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1548 ULONGEST str_index);
3019eac3 1549
e142c38c 1550static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1551
e142c38c
DJ
1552static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1553 struct dwarf2_cu *);
c906108c 1554
348e048f 1555static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1556 unsigned int);
348e048f 1557
7d45c7c3
KB
1558static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1559 struct dwarf2_cu *cu);
1560
05cf31d1
JB
1561static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1562 struct dwarf2_cu *cu);
1563
e142c38c 1564static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1565
e142c38c 1566static struct die_info *die_specification (struct die_info *die,
f2f0e013 1567 struct dwarf2_cu **);
63d06c5c 1568
debd256d
JB
1569static void free_line_header (struct line_header *lh);
1570
3019eac3
DE
1571static struct line_header *dwarf_decode_line_header (unsigned int offset,
1572 struct dwarf2_cu *cu);
debd256d 1573
f3f5162e 1574static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1575 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1576 CORE_ADDR, int decode_mapping);
c906108c 1577
4d663531 1578static void dwarf2_start_subfile (const char *, const char *);
c906108c 1579
43f3e411
DE
1580static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1581 const char *, const char *,
1582 CORE_ADDR);
f4dc4d17 1583
a14ed312 1584static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1585 struct dwarf2_cu *);
c906108c 1586
34eaf542
TT
1587static struct symbol *new_symbol_full (struct die_info *, struct type *,
1588 struct dwarf2_cu *, struct symbol *);
1589
ff39bb5e 1590static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1591 struct dwarf2_cu *);
c906108c 1592
ff39bb5e 1593static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1594 struct type *type,
1595 const char *name,
1596 struct obstack *obstack,
12df843f 1597 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1598 const gdb_byte **bytes,
98bfdba5 1599 struct dwarf2_locexpr_baton **baton);
2df3850c 1600
e7c27a73 1601static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1602
b4ba55a1
JB
1603static int need_gnat_info (struct dwarf2_cu *);
1604
3e43a32a
MS
1605static struct type *die_descriptive_type (struct die_info *,
1606 struct dwarf2_cu *);
b4ba55a1
JB
1607
1608static void set_descriptive_type (struct type *, struct die_info *,
1609 struct dwarf2_cu *);
1610
e7c27a73
DJ
1611static struct type *die_containing_type (struct die_info *,
1612 struct dwarf2_cu *);
c906108c 1613
ff39bb5e 1614static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1615 struct dwarf2_cu *);
c906108c 1616
f792889a 1617static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1618
673bfd45
DE
1619static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1620
0d5cff50 1621static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1622
6e70227d 1623static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1624 const char *suffix, int physname,
1625 struct dwarf2_cu *cu);
63d06c5c 1626
e7c27a73 1627static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1628
348e048f
DE
1629static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1630
e7c27a73 1631static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1632
e7c27a73 1633static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1634
96408a79
SA
1635static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1636
ff013f42
JK
1637static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1638 struct dwarf2_cu *, struct partial_symtab *);
1639
3a2b436a 1640/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1641 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1642enum pc_bounds_kind
1643{
e385593e 1644 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1645 PC_BOUNDS_NOT_PRESENT,
1646
e385593e
JK
1647 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1648 were present but they do not form a valid range of PC addresses. */
1649 PC_BOUNDS_INVALID,
1650
3a2b436a
JK
1651 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1652 PC_BOUNDS_RANGES,
1653
1654 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1655 PC_BOUNDS_HIGH_LOW,
1656};
1657
1658static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1659 CORE_ADDR *, CORE_ADDR *,
1660 struct dwarf2_cu *,
1661 struct partial_symtab *);
c906108c 1662
fae299cd
DC
1663static void get_scope_pc_bounds (struct die_info *,
1664 CORE_ADDR *, CORE_ADDR *,
1665 struct dwarf2_cu *);
1666
801e3a5b
JB
1667static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1668 CORE_ADDR, struct dwarf2_cu *);
1669
a14ed312 1670static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1671 struct dwarf2_cu *);
c906108c 1672
a14ed312 1673static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1674 struct type *, struct dwarf2_cu *);
c906108c 1675
a14ed312 1676static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1677 struct die_info *, struct type *,
e7c27a73 1678 struct dwarf2_cu *);
c906108c 1679
a14ed312 1680static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1681 struct type *,
1682 struct dwarf2_cu *);
c906108c 1683
134d01f1 1684static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1685
e7c27a73 1686static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1687
e7c27a73 1688static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1689
5d7cb8df
JK
1690static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1691
22cee43f
PMR
1692static struct using_direct **using_directives (enum language);
1693
27aa8d6a
SW
1694static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1695
74921315
KS
1696static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1697
f55ee35c
JK
1698static struct type *read_module_type (struct die_info *die,
1699 struct dwarf2_cu *cu);
1700
38d518c9 1701static const char *namespace_name (struct die_info *die,
e142c38c 1702 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1703
134d01f1 1704static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1705
e7c27a73 1706static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1707
6e70227d 1708static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1709 struct dwarf2_cu *);
1710
bf6af496 1711static struct die_info *read_die_and_siblings_1
d521ce57 1712 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1713 struct die_info *);
639d11d3 1714
dee91e82 1715static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1716 const gdb_byte *info_ptr,
1717 const gdb_byte **new_info_ptr,
639d11d3
DC
1718 struct die_info *parent);
1719
d521ce57
TT
1720static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1721 struct die_info **, const gdb_byte *,
1722 int *, int);
3019eac3 1723
d521ce57
TT
1724static const gdb_byte *read_full_die (const struct die_reader_specs *,
1725 struct die_info **, const gdb_byte *,
1726 int *);
93311388 1727
e7c27a73 1728static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1729
15d034d0
TT
1730static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1731 struct obstack *);
71c25dea 1732
15d034d0 1733static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1734
15d034d0 1735static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1736 struct die_info *die,
1737 struct dwarf2_cu *cu);
1738
ca69b9e6
DE
1739static const char *dwarf2_physname (const char *name, struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
e142c38c 1742static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1743 struct dwarf2_cu **);
9219021c 1744
f39c6ffd 1745static const char *dwarf_tag_name (unsigned int);
c906108c 1746
f39c6ffd 1747static const char *dwarf_attr_name (unsigned int);
c906108c 1748
f39c6ffd 1749static const char *dwarf_form_name (unsigned int);
c906108c 1750
a14ed312 1751static char *dwarf_bool_name (unsigned int);
c906108c 1752
f39c6ffd 1753static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1754
f9aca02d 1755static struct die_info *sibling_die (struct die_info *);
c906108c 1756
d97bc12b
DE
1757static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1758
1759static void dump_die_for_error (struct die_info *);
1760
1761static void dump_die_1 (struct ui_file *, int level, int max_level,
1762 struct die_info *);
c906108c 1763
d97bc12b 1764/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1765
51545339 1766static void store_in_ref_table (struct die_info *,
10b3939b 1767 struct dwarf2_cu *);
c906108c 1768
ff39bb5e 1769static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1770
ff39bb5e 1771static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1772
348e048f 1773static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1774 const struct attribute *,
348e048f
DE
1775 struct dwarf2_cu **);
1776
10b3939b 1777static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1778 const struct attribute *,
f2f0e013 1779 struct dwarf2_cu **);
c906108c 1780
348e048f 1781static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1782 const struct attribute *,
348e048f
DE
1783 struct dwarf2_cu **);
1784
ac9ec31b
DE
1785static struct type *get_signatured_type (struct die_info *, ULONGEST,
1786 struct dwarf2_cu *);
1787
1788static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1789 const struct attribute *,
ac9ec31b
DE
1790 struct dwarf2_cu *);
1791
e5fe5e75 1792static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1793
52dc124a 1794static void read_signatured_type (struct signatured_type *);
348e048f 1795
63e43d3a
PMR
1796static int attr_to_dynamic_prop (const struct attribute *attr,
1797 struct die_info *die, struct dwarf2_cu *cu,
1798 struct dynamic_prop *prop);
1799
c906108c
SS
1800/* memory allocation interface */
1801
7b5a2f43 1802static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1803
b60c80d6 1804static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1805
43f3e411 1806static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1807
6e5a29e1 1808static int attr_form_is_block (const struct attribute *);
8e19ed76 1809
6e5a29e1 1810static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1811
6e5a29e1 1812static int attr_form_is_constant (const struct attribute *);
3690dd37 1813
6e5a29e1 1814static int attr_form_is_ref (const struct attribute *);
7771576e 1815
8cf6f0b1
TT
1816static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1817 struct dwarf2_loclist_baton *baton,
ff39bb5e 1818 const struct attribute *attr);
8cf6f0b1 1819
ff39bb5e 1820static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1821 struct symbol *sym,
f1e6e072
TT
1822 struct dwarf2_cu *cu,
1823 int is_block);
4c2df51b 1824
d521ce57
TT
1825static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1826 const gdb_byte *info_ptr,
1827 struct abbrev_info *abbrev);
4bb7a0a7 1828
72bf9492
DJ
1829static void free_stack_comp_unit (void *);
1830
72bf9492
DJ
1831static hashval_t partial_die_hash (const void *item);
1832
1833static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1834
ae038cb0 1835static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1836 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1837
9816fde3 1838static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1839 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1840
1841static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1842 struct die_info *comp_unit_die,
1843 enum language pretend_language);
93311388 1844
68dc6402 1845static void free_heap_comp_unit (void *);
ae038cb0
DJ
1846
1847static void free_cached_comp_units (void *);
1848
1849static void age_cached_comp_units (void);
1850
dee91e82 1851static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1852
f792889a
DJ
1853static struct type *set_die_type (struct die_info *, struct type *,
1854 struct dwarf2_cu *);
1c379e20 1855
ae038cb0
DJ
1856static void create_all_comp_units (struct objfile *);
1857
0e50663e 1858static int create_all_type_units (struct objfile *);
1fd400ff 1859
95554aad
TT
1860static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1861 enum language);
10b3939b 1862
95554aad
TT
1863static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1864 enum language);
10b3939b 1865
f4dc4d17
DE
1866static void process_full_type_unit (struct dwarf2_per_cu_data *,
1867 enum language);
1868
10b3939b
DJ
1869static void dwarf2_add_dependence (struct dwarf2_cu *,
1870 struct dwarf2_per_cu_data *);
1871
ae038cb0
DJ
1872static void dwarf2_mark (struct dwarf2_cu *);
1873
1874static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1875
b64f50a1 1876static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1877 struct dwarf2_per_cu_data *);
673bfd45 1878
f792889a 1879static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1880
9291a0cd
TT
1881static void dwarf2_release_queue (void *dummy);
1882
95554aad
TT
1883static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1884 enum language pretend_language);
1885
a0f42c21 1886static void process_queue (void);
9291a0cd 1887
d721ba37
PA
1888/* The return type of find_file_and_directory. Note, the enclosed
1889 string pointers are only valid while this object is valid. */
1890
1891struct file_and_directory
1892{
1893 /* The filename. This is never NULL. */
1894 const char *name;
1895
1896 /* The compilation directory. NULL if not known. If we needed to
1897 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1898 points directly to the DW_AT_comp_dir string attribute owned by
1899 the obstack that owns the DIE. */
1900 const char *comp_dir;
1901
1902 /* If we needed to build a new string for comp_dir, this is what
1903 owns the storage. */
1904 std::string comp_dir_storage;
1905};
1906
1907static file_and_directory find_file_and_directory (struct die_info *die,
1908 struct dwarf2_cu *cu);
9291a0cd
TT
1909
1910static char *file_full_name (int file, struct line_header *lh,
1911 const char *comp_dir);
1912
43988095
JK
1913/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1914enum class rcuh_kind { COMPILE, TYPE };
1915
d521ce57 1916static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1917 (struct comp_unit_head *header,
1918 struct dwarf2_section_info *section,
d521ce57 1919 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1920 rcuh_kind section_kind);
36586728 1921
fd820528 1922static void init_cutu_and_read_dies
f4dc4d17
DE
1923 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1924 int use_existing_cu, int keep,
3019eac3
DE
1925 die_reader_func_ftype *die_reader_func, void *data);
1926
dee91e82
DE
1927static void init_cutu_and_read_dies_simple
1928 (struct dwarf2_per_cu_data *this_cu,
1929 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1930
673bfd45 1931static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1932
3019eac3
DE
1933static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1934
57d63ce2
DE
1935static struct dwo_unit *lookup_dwo_unit_in_dwp
1936 (struct dwp_file *dwp_file, const char *comp_dir,
1937 ULONGEST signature, int is_debug_types);
a2ce51a0
DE
1938
1939static struct dwp_file *get_dwp_file (void);
1940
3019eac3 1941static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1942 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1943
1944static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1945 (struct signatured_type *, const char *, const char *);
3019eac3 1946
89e63ee4
DE
1947static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1948
3019eac3
DE
1949static void free_dwo_file_cleanup (void *);
1950
95554aad
TT
1951static void process_cu_includes (void);
1952
1b80a9fa 1953static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1954
1955static void free_line_header_voidp (void *arg);
4390d890
DE
1956\f
1957/* Various complaints about symbol reading that don't abort the process. */
1958
1959static void
1960dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1961{
1962 complaint (&symfile_complaints,
1963 _("statement list doesn't fit in .debug_line section"));
1964}
1965
1966static void
1967dwarf2_debug_line_missing_file_complaint (void)
1968{
1969 complaint (&symfile_complaints,
1970 _(".debug_line section has line data without a file"));
1971}
1972
1973static void
1974dwarf2_debug_line_missing_end_sequence_complaint (void)
1975{
1976 complaint (&symfile_complaints,
1977 _(".debug_line section has line "
1978 "program sequence without an end"));
1979}
1980
1981static void
1982dwarf2_complex_location_expr_complaint (void)
1983{
1984 complaint (&symfile_complaints, _("location expression too complex"));
1985}
1986
1987static void
1988dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1989 int arg3)
1990{
1991 complaint (&symfile_complaints,
1992 _("const value length mismatch for '%s', got %d, expected %d"),
1993 arg1, arg2, arg3);
1994}
1995
1996static void
1997dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1998{
1999 complaint (&symfile_complaints,
2000 _("debug info runs off end of %s section"
2001 " [in module %s]"),
a32a8923
DE
2002 get_section_name (section),
2003 get_section_file_name (section));
4390d890 2004}
1b80a9fa 2005
4390d890
DE
2006static void
2007dwarf2_macro_malformed_definition_complaint (const char *arg1)
2008{
2009 complaint (&symfile_complaints,
2010 _("macro debug info contains a "
2011 "malformed macro definition:\n`%s'"),
2012 arg1);
2013}
2014
2015static void
2016dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2017{
2018 complaint (&symfile_complaints,
2019 _("invalid attribute class or form for '%s' in '%s'"),
2020 arg1, arg2);
2021}
527f3840
JK
2022
2023/* Hash function for line_header_hash. */
2024
2025static hashval_t
2026line_header_hash (const struct line_header *ofs)
2027{
2028 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
2029}
2030
2031/* Hash function for htab_create_alloc_ex for line_header_hash. */
2032
2033static hashval_t
2034line_header_hash_voidp (const void *item)
2035{
9a3c8263 2036 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2037
2038 return line_header_hash (ofs);
2039}
2040
2041/* Equality function for line_header_hash. */
2042
2043static int
2044line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2045{
9a3c8263
SM
2046 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2047 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840
JK
2048
2049 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
2050 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2051}
2052
4390d890 2053\f
9291a0cd
TT
2054#if WORDS_BIGENDIAN
2055
2056/* Convert VALUE between big- and little-endian. */
2057static offset_type
2058byte_swap (offset_type value)
2059{
2060 offset_type result;
2061
2062 result = (value & 0xff) << 24;
2063 result |= (value & 0xff00) << 8;
2064 result |= (value & 0xff0000) >> 8;
2065 result |= (value & 0xff000000) >> 24;
2066 return result;
2067}
2068
2069#define MAYBE_SWAP(V) byte_swap (V)
2070
2071#else
2072#define MAYBE_SWAP(V) (V)
2073#endif /* WORDS_BIGENDIAN */
2074
31aa7e4e
JB
2075/* Read the given attribute value as an address, taking the attribute's
2076 form into account. */
2077
2078static CORE_ADDR
2079attr_value_as_address (struct attribute *attr)
2080{
2081 CORE_ADDR addr;
2082
2083 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2084 {
2085 /* Aside from a few clearly defined exceptions, attributes that
2086 contain an address must always be in DW_FORM_addr form.
2087 Unfortunately, some compilers happen to be violating this
2088 requirement by encoding addresses using other forms, such
2089 as DW_FORM_data4 for example. For those broken compilers,
2090 we try to do our best, without any guarantee of success,
2091 to interpret the address correctly. It would also be nice
2092 to generate a complaint, but that would require us to maintain
2093 a list of legitimate cases where a non-address form is allowed,
2094 as well as update callers to pass in at least the CU's DWARF
2095 version. This is more overhead than what we're willing to
2096 expand for a pretty rare case. */
2097 addr = DW_UNSND (attr);
2098 }
2099 else
2100 addr = DW_ADDR (attr);
2101
2102 return addr;
2103}
2104
9291a0cd
TT
2105/* The suffix for an index file. */
2106#define INDEX_SUFFIX ".gdb-index"
2107
c906108c 2108/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2109 information and return true if we have enough to do something.
2110 NAMES points to the dwarf2 section names, or is NULL if the standard
2111 ELF names are used. */
c906108c
SS
2112
2113int
251d32d9
TG
2114dwarf2_has_info (struct objfile *objfile,
2115 const struct dwarf2_debug_sections *names)
c906108c 2116{
9a3c8263
SM
2117 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2118 objfile_data (objfile, dwarf2_objfile_data_key));
be391dca
TT
2119 if (!dwarf2_per_objfile)
2120 {
2121 /* Initialize per-objfile state. */
2122 struct dwarf2_per_objfile *data
8d749320 2123 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
9a619af0 2124
be391dca
TT
2125 memset (data, 0, sizeof (*data));
2126 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2127 dwarf2_per_objfile = data;
6502dd73 2128
251d32d9
TG
2129 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2130 (void *) names);
be391dca
TT
2131 dwarf2_per_objfile->objfile = objfile;
2132 }
73869dc2 2133 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2134 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2135 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2136 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2137}
2138
2139/* Return the containing section of virtual section SECTION. */
2140
2141static struct dwarf2_section_info *
2142get_containing_section (const struct dwarf2_section_info *section)
2143{
2144 gdb_assert (section->is_virtual);
2145 return section->s.containing_section;
c906108c
SS
2146}
2147
a32a8923
DE
2148/* Return the bfd owner of SECTION. */
2149
2150static struct bfd *
2151get_section_bfd_owner (const struct dwarf2_section_info *section)
2152{
73869dc2
DE
2153 if (section->is_virtual)
2154 {
2155 section = get_containing_section (section);
2156 gdb_assert (!section->is_virtual);
2157 }
049412e3 2158 return section->s.section->owner;
a32a8923
DE
2159}
2160
2161/* Return the bfd section of SECTION.
2162 Returns NULL if the section is not present. */
2163
2164static asection *
2165get_section_bfd_section (const struct dwarf2_section_info *section)
2166{
73869dc2
DE
2167 if (section->is_virtual)
2168 {
2169 section = get_containing_section (section);
2170 gdb_assert (!section->is_virtual);
2171 }
049412e3 2172 return section->s.section;
a32a8923
DE
2173}
2174
2175/* Return the name of SECTION. */
2176
2177static const char *
2178get_section_name (const struct dwarf2_section_info *section)
2179{
2180 asection *sectp = get_section_bfd_section (section);
2181
2182 gdb_assert (sectp != NULL);
2183 return bfd_section_name (get_section_bfd_owner (section), sectp);
2184}
2185
2186/* Return the name of the file SECTION is in. */
2187
2188static const char *
2189get_section_file_name (const struct dwarf2_section_info *section)
2190{
2191 bfd *abfd = get_section_bfd_owner (section);
2192
2193 return bfd_get_filename (abfd);
2194}
2195
2196/* Return the id of SECTION.
2197 Returns 0 if SECTION doesn't exist. */
2198
2199static int
2200get_section_id (const struct dwarf2_section_info *section)
2201{
2202 asection *sectp = get_section_bfd_section (section);
2203
2204 if (sectp == NULL)
2205 return 0;
2206 return sectp->id;
2207}
2208
2209/* Return the flags of SECTION.
73869dc2 2210 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2211
2212static int
2213get_section_flags (const struct dwarf2_section_info *section)
2214{
2215 asection *sectp = get_section_bfd_section (section);
2216
2217 gdb_assert (sectp != NULL);
2218 return bfd_get_section_flags (sectp->owner, sectp);
2219}
2220
251d32d9
TG
2221/* When loading sections, we look either for uncompressed section or for
2222 compressed section names. */
233a11ab
CS
2223
2224static int
251d32d9
TG
2225section_is_p (const char *section_name,
2226 const struct dwarf2_section_names *names)
233a11ab 2227{
251d32d9
TG
2228 if (names->normal != NULL
2229 && strcmp (section_name, names->normal) == 0)
2230 return 1;
2231 if (names->compressed != NULL
2232 && strcmp (section_name, names->compressed) == 0)
2233 return 1;
2234 return 0;
233a11ab
CS
2235}
2236
c906108c
SS
2237/* This function is mapped across the sections and remembers the
2238 offset and size of each of the debugging sections we are interested
2239 in. */
2240
2241static void
251d32d9 2242dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 2243{
251d32d9 2244 const struct dwarf2_debug_sections *names;
dc7650b8 2245 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
2246
2247 if (vnames == NULL)
2248 names = &dwarf2_elf_names;
2249 else
2250 names = (const struct dwarf2_debug_sections *) vnames;
2251
dc7650b8
JK
2252 if ((aflag & SEC_HAS_CONTENTS) == 0)
2253 {
2254 }
2255 else if (section_is_p (sectp->name, &names->info))
c906108c 2256 {
049412e3 2257 dwarf2_per_objfile->info.s.section = sectp;
dce234bc 2258 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 2259 }
251d32d9 2260 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 2261 {
049412e3 2262 dwarf2_per_objfile->abbrev.s.section = sectp;
dce234bc 2263 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 2264 }
251d32d9 2265 else if (section_is_p (sectp->name, &names->line))
c906108c 2266 {
049412e3 2267 dwarf2_per_objfile->line.s.section = sectp;
dce234bc 2268 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 2269 }
251d32d9 2270 else if (section_is_p (sectp->name, &names->loc))
c906108c 2271 {
049412e3 2272 dwarf2_per_objfile->loc.s.section = sectp;
dce234bc 2273 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 2274 }
43988095
JK
2275 else if (section_is_p (sectp->name, &names->loclists))
2276 {
2277 dwarf2_per_objfile->loclists.s.section = sectp;
2278 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2279 }
251d32d9 2280 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 2281 {
049412e3 2282 dwarf2_per_objfile->macinfo.s.section = sectp;
dce234bc 2283 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 2284 }
cf2c3c16
TT
2285 else if (section_is_p (sectp->name, &names->macro))
2286 {
049412e3 2287 dwarf2_per_objfile->macro.s.section = sectp;
cf2c3c16
TT
2288 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2289 }
251d32d9 2290 else if (section_is_p (sectp->name, &names->str))
c906108c 2291 {
049412e3 2292 dwarf2_per_objfile->str.s.section = sectp;
dce234bc 2293 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 2294 }
43988095
JK
2295 else if (section_is_p (sectp->name, &names->line_str))
2296 {
2297 dwarf2_per_objfile->line_str.s.section = sectp;
2298 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2299 }
3019eac3
DE
2300 else if (section_is_p (sectp->name, &names->addr))
2301 {
049412e3 2302 dwarf2_per_objfile->addr.s.section = sectp;
3019eac3
DE
2303 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2304 }
251d32d9 2305 else if (section_is_p (sectp->name, &names->frame))
b6af0555 2306 {
049412e3 2307 dwarf2_per_objfile->frame.s.section = sectp;
dce234bc 2308 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 2309 }
251d32d9 2310 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 2311 {
049412e3 2312 dwarf2_per_objfile->eh_frame.s.section = sectp;
dc7650b8 2313 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2314 }
251d32d9 2315 else if (section_is_p (sectp->name, &names->ranges))
af34e669 2316 {
049412e3 2317 dwarf2_per_objfile->ranges.s.section = sectp;
dce234bc 2318 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 2319 }
43988095
JK
2320 else if (section_is_p (sectp->name, &names->rnglists))
2321 {
2322 dwarf2_per_objfile->rnglists.s.section = sectp;
2323 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2324 }
251d32d9 2325 else if (section_is_p (sectp->name, &names->types))
348e048f 2326 {
8b70b953
TT
2327 struct dwarf2_section_info type_section;
2328
2329 memset (&type_section, 0, sizeof (type_section));
049412e3 2330 type_section.s.section = sectp;
8b70b953
TT
2331 type_section.size = bfd_get_section_size (sectp);
2332
2333 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2334 &type_section);
348e048f 2335 }
251d32d9 2336 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd 2337 {
049412e3 2338 dwarf2_per_objfile->gdb_index.s.section = sectp;
9291a0cd
TT
2339 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2340 }
dce234bc 2341
b4e1fd61 2342 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5
FR
2343 && bfd_section_vma (abfd, sectp) == 0)
2344 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
2345}
2346
fceca515
DE
2347/* A helper function that decides whether a section is empty,
2348 or not present. */
9e0ac564
TT
2349
2350static int
19ac8c2e 2351dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2352{
73869dc2
DE
2353 if (section->is_virtual)
2354 return section->size == 0;
049412e3 2355 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2356}
2357
3019eac3
DE
2358/* Read the contents of the section INFO.
2359 OBJFILE is the main object file, but not necessarily the file where
a32a8923
DE
2360 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2361 of the DWO file.
dce234bc 2362 If the section is compressed, uncompress it before returning. */
c906108c 2363
dce234bc
PP
2364static void
2365dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 2366{
a32a8923 2367 asection *sectp;
3019eac3 2368 bfd *abfd;
dce234bc 2369 gdb_byte *buf, *retbuf;
c906108c 2370
be391dca
TT
2371 if (info->readin)
2372 return;
dce234bc 2373 info->buffer = NULL;
be391dca 2374 info->readin = 1;
188dd5d6 2375
9e0ac564 2376 if (dwarf2_section_empty_p (info))
dce234bc 2377 return;
c906108c 2378
a32a8923 2379 sectp = get_section_bfd_section (info);
3019eac3 2380
73869dc2
DE
2381 /* If this is a virtual section we need to read in the real one first. */
2382 if (info->is_virtual)
2383 {
2384 struct dwarf2_section_info *containing_section =
2385 get_containing_section (info);
2386
2387 gdb_assert (sectp != NULL);
2388 if ((sectp->flags & SEC_RELOC) != 0)
2389 {
2390 error (_("Dwarf Error: DWP format V2 with relocations is not"
2391 " supported in section %s [in module %s]"),
2392 get_section_name (info), get_section_file_name (info));
2393 }
2394 dwarf2_read_section (objfile, containing_section);
2395 /* Other code should have already caught virtual sections that don't
2396 fit. */
2397 gdb_assert (info->virtual_offset + info->size
2398 <= containing_section->size);
2399 /* If the real section is empty or there was a problem reading the
2400 section we shouldn't get here. */
2401 gdb_assert (containing_section->buffer != NULL);
2402 info->buffer = containing_section->buffer + info->virtual_offset;
2403 return;
2404 }
2405
4bf44c1c
TT
2406 /* If the section has relocations, we must read it ourselves.
2407 Otherwise we attach it to the BFD. */
2408 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2409 {
d521ce57 2410 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2411 return;
dce234bc 2412 }
dce234bc 2413
224c3ddb 2414 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2415 info->buffer = buf;
dce234bc
PP
2416
2417 /* When debugging .o files, we may need to apply relocations; see
2418 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2419 We never compress sections in .o files, so we only need to
2420 try this when the section is not compressed. */
ac8035ab 2421 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2422 if (retbuf != NULL)
2423 {
2424 info->buffer = retbuf;
2425 return;
2426 }
2427
a32a8923
DE
2428 abfd = get_section_bfd_owner (info);
2429 gdb_assert (abfd != NULL);
2430
dce234bc
PP
2431 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2432 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2433 {
2434 error (_("Dwarf Error: Can't read DWARF data"
2435 " in section %s [in module %s]"),
2436 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2437 }
dce234bc
PP
2438}
2439
9e0ac564
TT
2440/* A helper function that returns the size of a section in a safe way.
2441 If you are positive that the section has been read before using the
2442 size, then it is safe to refer to the dwarf2_section_info object's
2443 "size" field directly. In other cases, you must call this
2444 function, because for compressed sections the size field is not set
2445 correctly until the section has been read. */
2446
2447static bfd_size_type
2448dwarf2_section_size (struct objfile *objfile,
2449 struct dwarf2_section_info *info)
2450{
2451 if (!info->readin)
2452 dwarf2_read_section (objfile, info);
2453 return info->size;
2454}
2455
dce234bc 2456/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2457 SECTION_NAME. */
af34e669 2458
dce234bc 2459void
3017a003
TG
2460dwarf2_get_section_info (struct objfile *objfile,
2461 enum dwarf2_section_enum sect,
d521ce57 2462 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2463 bfd_size_type *sizep)
2464{
2465 struct dwarf2_per_objfile *data
9a3c8263
SM
2466 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2467 dwarf2_objfile_data_key);
dce234bc 2468 struct dwarf2_section_info *info;
a3b2a86b
TT
2469
2470 /* We may see an objfile without any DWARF, in which case we just
2471 return nothing. */
2472 if (data == NULL)
2473 {
2474 *sectp = NULL;
2475 *bufp = NULL;
2476 *sizep = 0;
2477 return;
2478 }
3017a003
TG
2479 switch (sect)
2480 {
2481 case DWARF2_DEBUG_FRAME:
2482 info = &data->frame;
2483 break;
2484 case DWARF2_EH_FRAME:
2485 info = &data->eh_frame;
2486 break;
2487 default:
2488 gdb_assert_not_reached ("unexpected section");
2489 }
dce234bc 2490
9e0ac564 2491 dwarf2_read_section (objfile, info);
dce234bc 2492
a32a8923 2493 *sectp = get_section_bfd_section (info);
dce234bc
PP
2494 *bufp = info->buffer;
2495 *sizep = info->size;
2496}
2497
36586728
TT
2498/* A helper function to find the sections for a .dwz file. */
2499
2500static void
2501locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2502{
9a3c8263 2503 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2504
2505 /* Note that we only support the standard ELF names, because .dwz
2506 is ELF-only (at the time of writing). */
2507 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2508 {
049412e3 2509 dwz_file->abbrev.s.section = sectp;
36586728
TT
2510 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2511 }
2512 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2513 {
049412e3 2514 dwz_file->info.s.section = sectp;
36586728
TT
2515 dwz_file->info.size = bfd_get_section_size (sectp);
2516 }
2517 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2518 {
049412e3 2519 dwz_file->str.s.section = sectp;
36586728
TT
2520 dwz_file->str.size = bfd_get_section_size (sectp);
2521 }
2522 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2523 {
049412e3 2524 dwz_file->line.s.section = sectp;
36586728
TT
2525 dwz_file->line.size = bfd_get_section_size (sectp);
2526 }
2527 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2528 {
049412e3 2529 dwz_file->macro.s.section = sectp;
36586728
TT
2530 dwz_file->macro.size = bfd_get_section_size (sectp);
2531 }
2ec9a5e0
TT
2532 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2533 {
049412e3 2534 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2535 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2536 }
36586728
TT
2537}
2538
4db1a1dc
TT
2539/* Open the separate '.dwz' debug file, if needed. Return NULL if
2540 there is no .gnu_debugaltlink section in the file. Error if there
2541 is such a section but the file cannot be found. */
36586728
TT
2542
2543static struct dwz_file *
2544dwarf2_get_dwz_file (void)
2545{
4db1a1dc 2546 char *data;
36586728
TT
2547 struct cleanup *cleanup;
2548 const char *filename;
2549 struct dwz_file *result;
acd13123 2550 bfd_size_type buildid_len_arg;
dc294be5
TT
2551 size_t buildid_len;
2552 bfd_byte *buildid;
36586728
TT
2553
2554 if (dwarf2_per_objfile->dwz_file != NULL)
2555 return dwarf2_per_objfile->dwz_file;
2556
4db1a1dc
TT
2557 bfd_set_error (bfd_error_no_error);
2558 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
acd13123 2559 &buildid_len_arg, &buildid);
4db1a1dc
TT
2560 if (data == NULL)
2561 {
2562 if (bfd_get_error () == bfd_error_no_error)
2563 return NULL;
2564 error (_("could not read '.gnu_debugaltlink' section: %s"),
2565 bfd_errmsg (bfd_get_error ()));
2566 }
36586728 2567 cleanup = make_cleanup (xfree, data);
dc294be5 2568 make_cleanup (xfree, buildid);
36586728 2569
acd13123
TT
2570 buildid_len = (size_t) buildid_len_arg;
2571
f9d83a0b 2572 filename = (const char *) data;
d721ba37
PA
2573
2574 std::string abs_storage;
36586728
TT
2575 if (!IS_ABSOLUTE_PATH (filename))
2576 {
4262abfb 2577 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728
TT
2578
2579 make_cleanup (xfree, abs);
d721ba37
PA
2580 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2581 filename = abs_storage.c_str ();
36586728
TT
2582 }
2583
dc294be5
TT
2584 /* First try the file name given in the section. If that doesn't
2585 work, try to use the build-id instead. */
192b62ce 2586 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2587 if (dwz_bfd != NULL)
36586728 2588 {
192b62ce
TT
2589 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2590 dwz_bfd.release ();
36586728
TT
2591 }
2592
dc294be5
TT
2593 if (dwz_bfd == NULL)
2594 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2595
2596 if (dwz_bfd == NULL)
2597 error (_("could not find '.gnu_debugaltlink' file for %s"),
2598 objfile_name (dwarf2_per_objfile->objfile));
2599
36586728
TT
2600 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2601 struct dwz_file);
192b62ce 2602 result->dwz_bfd = dwz_bfd.release ();
36586728 2603
192b62ce 2604 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
36586728
TT
2605
2606 do_cleanups (cleanup);
2607
192b62ce 2608 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
8d2cc612 2609 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2610 return result;
2611}
9291a0cd 2612\f
7b9f3c50
DE
2613/* DWARF quick_symbols_functions support. */
2614
2615/* TUs can share .debug_line entries, and there can be a lot more TUs than
2616 unique line tables, so we maintain a separate table of all .debug_line
2617 derived entries to support the sharing.
2618 All the quick functions need is the list of file names. We discard the
2619 line_header when we're done and don't need to record it here. */
2620struct quick_file_names
2621{
094b34ac
DE
2622 /* The data used to construct the hash key. */
2623 struct stmt_list_hash hash;
7b9f3c50
DE
2624
2625 /* The number of entries in file_names, real_names. */
2626 unsigned int num_file_names;
2627
2628 /* The file names from the line table, after being run through
2629 file_full_name. */
2630 const char **file_names;
2631
2632 /* The file names from the line table after being run through
2633 gdb_realpath. These are computed lazily. */
2634 const char **real_names;
2635};
2636
2637/* When using the index (and thus not using psymtabs), each CU has an
2638 object of this type. This is used to hold information needed by
2639 the various "quick" methods. */
2640struct dwarf2_per_cu_quick_data
2641{
2642 /* The file table. This can be NULL if there was no file table
2643 or it's currently not read in.
2644 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2645 struct quick_file_names *file_names;
2646
2647 /* The corresponding symbol table. This is NULL if symbols for this
2648 CU have not yet been read. */
43f3e411 2649 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2650
2651 /* A temporary mark bit used when iterating over all CUs in
2652 expand_symtabs_matching. */
2653 unsigned int mark : 1;
2654
2655 /* True if we've tried to read the file table and found there isn't one.
2656 There will be no point in trying to read it again next time. */
2657 unsigned int no_file_data : 1;
2658};
2659
094b34ac
DE
2660/* Utility hash function for a stmt_list_hash. */
2661
2662static hashval_t
2663hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2664{
2665 hashval_t v = 0;
2666
2667 if (stmt_list_hash->dwo_unit != NULL)
2668 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2669 v += stmt_list_hash->line_offset.sect_off;
2670 return v;
2671}
2672
2673/* Utility equality function for a stmt_list_hash. */
2674
2675static int
2676eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2677 const struct stmt_list_hash *rhs)
2678{
2679 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2680 return 0;
2681 if (lhs->dwo_unit != NULL
2682 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2683 return 0;
2684
2685 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2686}
2687
7b9f3c50
DE
2688/* Hash function for a quick_file_names. */
2689
2690static hashval_t
2691hash_file_name_entry (const void *e)
2692{
9a3c8263
SM
2693 const struct quick_file_names *file_data
2694 = (const struct quick_file_names *) e;
7b9f3c50 2695
094b34ac 2696 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2697}
2698
2699/* Equality function for a quick_file_names. */
2700
2701static int
2702eq_file_name_entry (const void *a, const void *b)
2703{
9a3c8263
SM
2704 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2705 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2706
094b34ac 2707 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2708}
2709
2710/* Delete function for a quick_file_names. */
2711
2712static void
2713delete_file_name_entry (void *e)
2714{
9a3c8263 2715 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2716 int i;
2717
2718 for (i = 0; i < file_data->num_file_names; ++i)
2719 {
2720 xfree ((void*) file_data->file_names[i]);
2721 if (file_data->real_names)
2722 xfree ((void*) file_data->real_names[i]);
2723 }
2724
2725 /* The space for the struct itself lives on objfile_obstack,
2726 so we don't free it here. */
2727}
2728
2729/* Create a quick_file_names hash table. */
2730
2731static htab_t
2732create_quick_file_names_table (unsigned int nr_initial_entries)
2733{
2734 return htab_create_alloc (nr_initial_entries,
2735 hash_file_name_entry, eq_file_name_entry,
2736 delete_file_name_entry, xcalloc, xfree);
2737}
9291a0cd 2738
918dd910
JK
2739/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2740 have to be created afterwards. You should call age_cached_comp_units after
2741 processing PER_CU->CU. dw2_setup must have been already called. */
2742
2743static void
2744load_cu (struct dwarf2_per_cu_data *per_cu)
2745{
3019eac3 2746 if (per_cu->is_debug_types)
e5fe5e75 2747 load_full_type_unit (per_cu);
918dd910 2748 else
95554aad 2749 load_full_comp_unit (per_cu, language_minimal);
918dd910 2750
cc12ce38
DE
2751 if (per_cu->cu == NULL)
2752 return; /* Dummy CU. */
2dc860c0
DE
2753
2754 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2755}
2756
a0f42c21 2757/* Read in the symbols for PER_CU. */
2fdf6df6 2758
9291a0cd 2759static void
a0f42c21 2760dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2761{
2762 struct cleanup *back_to;
2763
f4dc4d17
DE
2764 /* Skip type_unit_groups, reading the type units they contain
2765 is handled elsewhere. */
2766 if (IS_TYPE_UNIT_GROUP (per_cu))
2767 return;
2768
9291a0cd
TT
2769 back_to = make_cleanup (dwarf2_release_queue, NULL);
2770
95554aad 2771 if (dwarf2_per_objfile->using_index
43f3e411 2772 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2773 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2774 {
2775 queue_comp_unit (per_cu, language_minimal);
2776 load_cu (per_cu);
89e63ee4
DE
2777
2778 /* If we just loaded a CU from a DWO, and we're working with an index
2779 that may badly handle TUs, load all the TUs in that DWO as well.
2780 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2781 if (!per_cu->is_debug_types
cc12ce38 2782 && per_cu->cu != NULL
89e63ee4
DE
2783 && per_cu->cu->dwo_unit != NULL
2784 && dwarf2_per_objfile->index_table != NULL
2785 && dwarf2_per_objfile->index_table->version <= 7
2786 /* DWP files aren't supported yet. */
2787 && get_dwp_file () == NULL)
2788 queue_and_load_all_dwo_tus (per_cu);
95554aad 2789 }
9291a0cd 2790
a0f42c21 2791 process_queue ();
9291a0cd
TT
2792
2793 /* Age the cache, releasing compilation units that have not
2794 been used recently. */
2795 age_cached_comp_units ();
2796
2797 do_cleanups (back_to);
2798}
2799
2800/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2801 the objfile from which this CU came. Returns the resulting symbol
2802 table. */
2fdf6df6 2803
43f3e411 2804static struct compunit_symtab *
a0f42c21 2805dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2806{
95554aad 2807 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2808 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
2809 {
2810 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2811 increment_reading_symtab ();
a0f42c21 2812 dw2_do_instantiate_symtab (per_cu);
95554aad 2813 process_cu_includes ();
9291a0cd
TT
2814 do_cleanups (back_to);
2815 }
f194fefb 2816
43f3e411 2817 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2818}
2819
8832e7e3 2820/* Return the CU/TU given its index.
f4dc4d17
DE
2821
2822 This is intended for loops like:
2823
2824 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2825 + dwarf2_per_objfile->n_type_units); ++i)
2826 {
8832e7e3 2827 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
f4dc4d17
DE
2828
2829 ...;
2830 }
2831*/
2fdf6df6 2832
1fd400ff 2833static struct dwarf2_per_cu_data *
8832e7e3 2834dw2_get_cutu (int index)
1fd400ff
TT
2835{
2836 if (index >= dwarf2_per_objfile->n_comp_units)
2837 {
f4dc4d17 2838 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2839 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2840 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2841 }
2842
2843 return dwarf2_per_objfile->all_comp_units[index];
2844}
2845
8832e7e3
DE
2846/* Return the CU given its index.
2847 This differs from dw2_get_cutu in that it's for when you know INDEX
2848 refers to a CU. */
f4dc4d17
DE
2849
2850static struct dwarf2_per_cu_data *
8832e7e3 2851dw2_get_cu (int index)
f4dc4d17 2852{
8832e7e3 2853 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
f4dc4d17 2854
1fd400ff
TT
2855 return dwarf2_per_objfile->all_comp_units[index];
2856}
2857
2ec9a5e0
TT
2858/* A helper for create_cus_from_index that handles a given list of
2859 CUs. */
2fdf6df6 2860
74a0d9f6 2861static void
2ec9a5e0
TT
2862create_cus_from_index_list (struct objfile *objfile,
2863 const gdb_byte *cu_list, offset_type n_elements,
2864 struct dwarf2_section_info *section,
2865 int is_dwz,
2866 int base_offset)
9291a0cd
TT
2867{
2868 offset_type i;
9291a0cd 2869
2ec9a5e0 2870 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2871 {
2872 struct dwarf2_per_cu_data *the_cu;
2873 ULONGEST offset, length;
2874
74a0d9f6
JK
2875 gdb_static_assert (sizeof (ULONGEST) >= 8);
2876 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2877 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2878 cu_list += 2 * 8;
2879
2880 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2881 struct dwarf2_per_cu_data);
b64f50a1 2882 the_cu->offset.sect_off = offset;
9291a0cd
TT
2883 the_cu->length = length;
2884 the_cu->objfile = objfile;
8a0459fd 2885 the_cu->section = section;
9291a0cd
TT
2886 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2887 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2888 the_cu->is_dwz = is_dwz;
2889 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2890 }
9291a0cd
TT
2891}
2892
2ec9a5e0 2893/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2894 the CU objects for this objfile. */
2ec9a5e0 2895
74a0d9f6 2896static void
2ec9a5e0
TT
2897create_cus_from_index (struct objfile *objfile,
2898 const gdb_byte *cu_list, offset_type cu_list_elements,
2899 const gdb_byte *dwz_list, offset_type dwz_elements)
2900{
2901 struct dwz_file *dwz;
2902
2903 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
8d749320
SM
2904 dwarf2_per_objfile->all_comp_units =
2905 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2906 dwarf2_per_objfile->n_comp_units);
2ec9a5e0 2907
74a0d9f6
JK
2908 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2909 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2910
2911 if (dwz_elements == 0)
74a0d9f6 2912 return;
2ec9a5e0
TT
2913
2914 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2915 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2916 cu_list_elements / 2);
2ec9a5e0
TT
2917}
2918
1fd400ff 2919/* Create the signatured type hash table from the index. */
673bfd45 2920
74a0d9f6 2921static void
673bfd45 2922create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2923 struct dwarf2_section_info *section,
673bfd45
DE
2924 const gdb_byte *bytes,
2925 offset_type elements)
1fd400ff
TT
2926{
2927 offset_type i;
673bfd45 2928 htab_t sig_types_hash;
1fd400ff 2929
6aa5f3a6
DE
2930 dwarf2_per_objfile->n_type_units
2931 = dwarf2_per_objfile->n_allocated_type_units
2932 = elements / 3;
8d749320
SM
2933 dwarf2_per_objfile->all_type_units =
2934 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
1fd400ff 2935
673bfd45 2936 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2937
2938 for (i = 0; i < elements; i += 3)
2939 {
52dc124a
DE
2940 struct signatured_type *sig_type;
2941 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2942 void **slot;
2943
74a0d9f6
JK
2944 gdb_static_assert (sizeof (ULONGEST) >= 8);
2945 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2946 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2947 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2948 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2949 bytes += 3 * 8;
2950
52dc124a 2951 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2952 struct signatured_type);
52dc124a 2953 sig_type->signature = signature;
3019eac3
DE
2954 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2955 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2956 sig_type->per_cu.section = section;
52dc124a
DE
2957 sig_type->per_cu.offset.sect_off = offset;
2958 sig_type->per_cu.objfile = objfile;
2959 sig_type->per_cu.v.quick
1fd400ff
TT
2960 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2961 struct dwarf2_per_cu_quick_data);
2962
52dc124a
DE
2963 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2964 *slot = sig_type;
1fd400ff 2965
b4dd5633 2966 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2967 }
2968
673bfd45 2969 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2970}
2971
9291a0cd
TT
2972/* Read the address map data from the mapped index, and use it to
2973 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2974
9291a0cd
TT
2975static void
2976create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2977{
3e29f34a 2978 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd
TT
2979 const gdb_byte *iter, *end;
2980 struct obstack temp_obstack;
2981 struct addrmap *mutable_map;
2982 struct cleanup *cleanup;
2983 CORE_ADDR baseaddr;
2984
2985 obstack_init (&temp_obstack);
2986 cleanup = make_cleanup_obstack_free (&temp_obstack);
2987 mutable_map = addrmap_create_mutable (&temp_obstack);
2988
2989 iter = index->address_table;
2990 end = iter + index->address_table_size;
2991
2992 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2993
2994 while (iter < end)
2995 {
2996 ULONGEST hi, lo, cu_index;
2997 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2998 iter += 8;
2999 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3000 iter += 8;
3001 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3002 iter += 4;
f652bce2 3003
24a55014 3004 if (lo > hi)
f652bce2 3005 {
24a55014
DE
3006 complaint (&symfile_complaints,
3007 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3008 hex_string (lo), hex_string (hi));
24a55014 3009 continue;
f652bce2 3010 }
24a55014
DE
3011
3012 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
3013 {
3014 complaint (&symfile_complaints,
3015 _(".gdb_index address table has invalid CU number %u"),
3016 (unsigned) cu_index);
24a55014 3017 continue;
f652bce2 3018 }
24a55014 3019
3e29f34a
MR
3020 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3021 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3022 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
9291a0cd
TT
3023 }
3024
3025 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3026 &objfile->objfile_obstack);
3027 do_cleanups (cleanup);
3028}
3029
59d7bcaf
JK
3030/* The hash function for strings in the mapped index. This is the same as
3031 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3032 implementation. This is necessary because the hash function is tied to the
3033 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
3034 SYMBOL_HASH_NEXT.
3035
3036 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 3037
9291a0cd 3038static hashval_t
559a7a62 3039mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
3040{
3041 const unsigned char *str = (const unsigned char *) p;
3042 hashval_t r = 0;
3043 unsigned char c;
3044
3045 while ((c = *str++) != 0)
559a7a62
JK
3046 {
3047 if (index_version >= 5)
3048 c = tolower (c);
3049 r = r * 67 + c - 113;
3050 }
9291a0cd
TT
3051
3052 return r;
3053}
3054
3055/* Find a slot in the mapped index INDEX for the object named NAME.
3056 If NAME is found, set *VEC_OUT to point to the CU vector in the
3057 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 3058
9291a0cd
TT
3059static int
3060find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3061 offset_type **vec_out)
3062{
0cf03b49
JK
3063 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3064 offset_type hash;
9291a0cd 3065 offset_type slot, step;
559a7a62 3066 int (*cmp) (const char *, const char *);
9291a0cd 3067
0cf03b49 3068 if (current_language->la_language == language_cplus
45280282
IB
3069 || current_language->la_language == language_fortran
3070 || current_language->la_language == language_d)
0cf03b49
JK
3071 {
3072 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3073 not contain any. */
a8719064 3074
72998fb3 3075 if (strchr (name, '(') != NULL)
0cf03b49 3076 {
72998fb3 3077 char *without_params = cp_remove_params (name);
0cf03b49 3078
72998fb3
DE
3079 if (without_params != NULL)
3080 {
3081 make_cleanup (xfree, without_params);
3082 name = without_params;
3083 }
0cf03b49
JK
3084 }
3085 }
3086
559a7a62 3087 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3088 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3089 simulate our NAME being searched is also lowercased. */
3090 hash = mapped_index_string_hash ((index->version == 4
3091 && case_sensitivity == case_sensitive_off
3092 ? 5 : index->version),
3093 name);
3094
3876f04e
DE
3095 slot = hash & (index->symbol_table_slots - 1);
3096 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 3097 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3098
3099 for (;;)
3100 {
3101 /* Convert a slot number to an offset into the table. */
3102 offset_type i = 2 * slot;
3103 const char *str;
3876f04e 3104 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
3105 {
3106 do_cleanups (back_to);
3107 return 0;
3108 }
9291a0cd 3109
3876f04e 3110 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 3111 if (!cmp (name, str))
9291a0cd
TT
3112 {
3113 *vec_out = (offset_type *) (index->constant_pool
3876f04e 3114 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 3115 do_cleanups (back_to);
9291a0cd
TT
3116 return 1;
3117 }
3118
3876f04e 3119 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
3120 }
3121}
3122
2ec9a5e0
TT
3123/* A helper function that reads the .gdb_index from SECTION and fills
3124 in MAP. FILENAME is the name of the file containing the section;
3125 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3126 ok to use deprecated sections.
3127
3128 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3129 out parameters that are filled in with information about the CU and
3130 TU lists in the section.
3131
3132 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3133
9291a0cd 3134static int
2ec9a5e0
TT
3135read_index_from_section (struct objfile *objfile,
3136 const char *filename,
3137 int deprecated_ok,
3138 struct dwarf2_section_info *section,
3139 struct mapped_index *map,
3140 const gdb_byte **cu_list,
3141 offset_type *cu_list_elements,
3142 const gdb_byte **types_list,
3143 offset_type *types_list_elements)
9291a0cd 3144{
948f8e3d 3145 const gdb_byte *addr;
2ec9a5e0 3146 offset_type version;
b3b272e1 3147 offset_type *metadata;
1fd400ff 3148 int i;
9291a0cd 3149
2ec9a5e0 3150 if (dwarf2_section_empty_p (section))
9291a0cd 3151 return 0;
82430852
JK
3152
3153 /* Older elfutils strip versions could keep the section in the main
3154 executable while splitting it for the separate debug info file. */
a32a8923 3155 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3156 return 0;
3157
2ec9a5e0 3158 dwarf2_read_section (objfile, section);
9291a0cd 3159
2ec9a5e0 3160 addr = section->buffer;
9291a0cd 3161 /* Version check. */
1fd400ff 3162 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3163 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3164 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3165 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3166 indices. */
831adc1f 3167 if (version < 4)
481860b3
GB
3168 {
3169 static int warning_printed = 0;
3170 if (!warning_printed)
3171 {
3172 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3173 filename);
481860b3
GB
3174 warning_printed = 1;
3175 }
3176 return 0;
3177 }
3178 /* Index version 4 uses a different hash function than index version
3179 5 and later.
3180
3181 Versions earlier than 6 did not emit psymbols for inlined
3182 functions. Using these files will cause GDB not to be able to
3183 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3184 indices unless the user has done
3185 "set use-deprecated-index-sections on". */
2ec9a5e0 3186 if (version < 6 && !deprecated_ok)
481860b3
GB
3187 {
3188 static int warning_printed = 0;
3189 if (!warning_printed)
3190 {
e615022a
DE
3191 warning (_("\
3192Skipping deprecated .gdb_index section in %s.\n\
3193Do \"set use-deprecated-index-sections on\" before the file is read\n\
3194to use the section anyway."),
2ec9a5e0 3195 filename);
481860b3
GB
3196 warning_printed = 1;
3197 }
3198 return 0;
3199 }
796a7ff8 3200 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3201 of the TU (for symbols coming from TUs),
3202 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3203 Plus gold-generated indices can have duplicate entries for global symbols,
3204 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3205 These are just performance bugs, and we can't distinguish gdb-generated
3206 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3207
481860b3 3208 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3209 longer backward compatible. */
796a7ff8 3210 if (version > 8)
594e8718 3211 return 0;
9291a0cd 3212
559a7a62 3213 map->version = version;
2ec9a5e0 3214 map->total_size = section->size;
9291a0cd
TT
3215
3216 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3217
3218 i = 0;
2ec9a5e0
TT
3219 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3220 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3221 / 8);
1fd400ff
TT
3222 ++i;
3223
2ec9a5e0
TT
3224 *types_list = addr + MAYBE_SWAP (metadata[i]);
3225 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3226 - MAYBE_SWAP (metadata[i]))
3227 / 8);
987d643c 3228 ++i;
1fd400ff
TT
3229
3230 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3231 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3232 - MAYBE_SWAP (metadata[i]));
3233 ++i;
3234
3876f04e
DE
3235 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3236 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3237 - MAYBE_SWAP (metadata[i]))
3238 / (2 * sizeof (offset_type)));
1fd400ff 3239 ++i;
9291a0cd 3240
f9d83a0b 3241 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3242
2ec9a5e0
TT
3243 return 1;
3244}
3245
3246
3247/* Read the index file. If everything went ok, initialize the "quick"
3248 elements of all the CUs and return 1. Otherwise, return 0. */
3249
3250static int
3251dwarf2_read_index (struct objfile *objfile)
3252{
3253 struct mapped_index local_map, *map;
3254 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3255 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3256 struct dwz_file *dwz;
2ec9a5e0 3257
4262abfb 3258 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0
TT
3259 use_deprecated_index_sections,
3260 &dwarf2_per_objfile->gdb_index, &local_map,
3261 &cu_list, &cu_list_elements,
3262 &types_list, &types_list_elements))
3263 return 0;
3264
0fefef59 3265 /* Don't use the index if it's empty. */
2ec9a5e0 3266 if (local_map.symbol_table_slots == 0)
0fefef59
DE
3267 return 0;
3268
2ec9a5e0
TT
3269 /* If there is a .dwz file, read it so we can get its CU list as
3270 well. */
4db1a1dc
TT
3271 dwz = dwarf2_get_dwz_file ();
3272 if (dwz != NULL)
2ec9a5e0 3273 {
2ec9a5e0
TT
3274 struct mapped_index dwz_map;
3275 const gdb_byte *dwz_types_ignore;
3276 offset_type dwz_types_elements_ignore;
3277
3278 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3279 1,
3280 &dwz->gdb_index, &dwz_map,
3281 &dwz_list, &dwz_list_elements,
3282 &dwz_types_ignore,
3283 &dwz_types_elements_ignore))
3284 {
3285 warning (_("could not read '.gdb_index' section from %s; skipping"),
3286 bfd_get_filename (dwz->dwz_bfd));
3287 return 0;
3288 }
3289 }
3290
74a0d9f6
JK
3291 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3292 dwz_list_elements);
1fd400ff 3293
8b70b953
TT
3294 if (types_list_elements)
3295 {
3296 struct dwarf2_section_info *section;
3297
3298 /* We can only handle a single .debug_types when we have an
3299 index. */
3300 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3301 return 0;
3302
3303 section = VEC_index (dwarf2_section_info_def,
3304 dwarf2_per_objfile->types, 0);
3305
74a0d9f6
JK
3306 create_signatured_type_table_from_index (objfile, section, types_list,
3307 types_list_elements);
8b70b953 3308 }
9291a0cd 3309
2ec9a5e0
TT
3310 create_addrmap_from_index (objfile, &local_map);
3311
8d749320 3312 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
2ec9a5e0 3313 *map = local_map;
9291a0cd
TT
3314
3315 dwarf2_per_objfile->index_table = map;
3316 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
3317 dwarf2_per_objfile->quick_file_names_table =
3318 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
3319
3320 return 1;
3321}
3322
3323/* A helper for the "quick" functions which sets the global
3324 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 3325
9291a0cd
TT
3326static void
3327dw2_setup (struct objfile *objfile)
3328{
9a3c8263
SM
3329 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3330 objfile_data (objfile, dwarf2_objfile_data_key));
9291a0cd
TT
3331 gdb_assert (dwarf2_per_objfile);
3332}
3333
dee91e82 3334/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3335
dee91e82
DE
3336static void
3337dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3338 const gdb_byte *info_ptr,
dee91e82
DE
3339 struct die_info *comp_unit_die,
3340 int has_children,
3341 void *data)
9291a0cd 3342{
dee91e82
DE
3343 struct dwarf2_cu *cu = reader->cu;
3344 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3345 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3346 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 3347 struct line_header *lh;
9291a0cd 3348 struct attribute *attr;
dee91e82 3349 int i;
7b9f3c50
DE
3350 void **slot;
3351 struct quick_file_names *qfn;
3352 unsigned int line_offset;
9291a0cd 3353
0186c6a7
DE
3354 gdb_assert (! this_cu->is_debug_types);
3355
07261596
TT
3356 /* Our callers never want to match partial units -- instead they
3357 will match the enclosing full CU. */
3358 if (comp_unit_die->tag == DW_TAG_partial_unit)
3359 {
3360 this_cu->v.quick->no_file_data = 1;
3361 return;
3362 }
3363
0186c6a7 3364 lh_cu = this_cu;
7b9f3c50
DE
3365 lh = NULL;
3366 slot = NULL;
3367 line_offset = 0;
dee91e82
DE
3368
3369 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3370 if (attr)
3371 {
7b9f3c50
DE
3372 struct quick_file_names find_entry;
3373
3374 line_offset = DW_UNSND (attr);
3375
3376 /* We may have already read in this line header (TU line header sharing).
3377 If we have we're done. */
094b34ac
DE
3378 find_entry.hash.dwo_unit = cu->dwo_unit;
3379 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3380 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3381 &find_entry, INSERT);
3382 if (*slot != NULL)
3383 {
9a3c8263 3384 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3385 return;
7b9f3c50
DE
3386 }
3387
3019eac3 3388 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3389 }
3390 if (lh == NULL)
3391 {
094b34ac 3392 lh_cu->v.quick->no_file_data = 1;
dee91e82 3393 return;
9291a0cd
TT
3394 }
3395
8d749320 3396 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac
DE
3397 qfn->hash.dwo_unit = cu->dwo_unit;
3398 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
3399 gdb_assert (slot != NULL);
3400 *slot = qfn;
9291a0cd 3401
d721ba37 3402 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3403
7b9f3c50 3404 qfn->num_file_names = lh->num_file_names;
8d749320
SM
3405 qfn->file_names =
3406 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->num_file_names);
9291a0cd 3407 for (i = 0; i < lh->num_file_names; ++i)
d721ba37 3408 qfn->file_names[i] = file_full_name (i + 1, lh, fnd.comp_dir);
7b9f3c50 3409 qfn->real_names = NULL;
9291a0cd 3410
7b9f3c50 3411 free_line_header (lh);
7b9f3c50 3412
094b34ac 3413 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3414}
3415
3416/* A helper for the "quick" functions which attempts to read the line
3417 table for THIS_CU. */
3418
3419static struct quick_file_names *
e4a48d9d 3420dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3421{
0186c6a7
DE
3422 /* This should never be called for TUs. */
3423 gdb_assert (! this_cu->is_debug_types);
3424 /* Nor type unit groups. */
3425 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3426
dee91e82
DE
3427 if (this_cu->v.quick->file_names != NULL)
3428 return this_cu->v.quick->file_names;
3429 /* If we know there is no line data, no point in looking again. */
3430 if (this_cu->v.quick->no_file_data)
3431 return NULL;
3432
0186c6a7 3433 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3434
3435 if (this_cu->v.quick->no_file_data)
3436 return NULL;
3437 return this_cu->v.quick->file_names;
9291a0cd
TT
3438}
3439
3440/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3441 real path for a given file name from the line table. */
2fdf6df6 3442
9291a0cd 3443static const char *
7b9f3c50
DE
3444dw2_get_real_path (struct objfile *objfile,
3445 struct quick_file_names *qfn, int index)
9291a0cd 3446{
7b9f3c50
DE
3447 if (qfn->real_names == NULL)
3448 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3449 qfn->num_file_names, const char *);
9291a0cd 3450
7b9f3c50
DE
3451 if (qfn->real_names[index] == NULL)
3452 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3453
7b9f3c50 3454 return qfn->real_names[index];
9291a0cd
TT
3455}
3456
3457static struct symtab *
3458dw2_find_last_source_symtab (struct objfile *objfile)
3459{
43f3e411 3460 struct compunit_symtab *cust;
9291a0cd 3461 int index;
ae2de4f8 3462
9291a0cd
TT
3463 dw2_setup (objfile);
3464 index = dwarf2_per_objfile->n_comp_units - 1;
43f3e411
DE
3465 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3466 if (cust == NULL)
3467 return NULL;
3468 return compunit_primary_filetab (cust);
9291a0cd
TT
3469}
3470
7b9f3c50
DE
3471/* Traversal function for dw2_forget_cached_source_info. */
3472
3473static int
3474dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3475{
7b9f3c50 3476 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3477
7b9f3c50 3478 if (file_data->real_names)
9291a0cd 3479 {
7b9f3c50 3480 int i;
9291a0cd 3481
7b9f3c50 3482 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3483 {
7b9f3c50
DE
3484 xfree ((void*) file_data->real_names[i]);
3485 file_data->real_names[i] = NULL;
9291a0cd
TT
3486 }
3487 }
7b9f3c50
DE
3488
3489 return 1;
3490}
3491
3492static void
3493dw2_forget_cached_source_info (struct objfile *objfile)
3494{
3495 dw2_setup (objfile);
3496
3497 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3498 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3499}
3500
f8eba3c6
TT
3501/* Helper function for dw2_map_symtabs_matching_filename that expands
3502 the symtabs and calls the iterator. */
3503
3504static int
3505dw2_map_expand_apply (struct objfile *objfile,
3506 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3507 const char *name, const char *real_path,
14bc53a8 3508 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3509{
43f3e411 3510 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3511
3512 /* Don't visit already-expanded CUs. */
43f3e411 3513 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3514 return 0;
3515
3516 /* This may expand more than one symtab, and we want to iterate over
3517 all of them. */
a0f42c21 3518 dw2_instantiate_symtab (per_cu);
f8eba3c6 3519
14bc53a8
PA
3520 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3521 last_made, callback);
f8eba3c6
TT
3522}
3523
3524/* Implementation of the map_symtabs_matching_filename method. */
3525
14bc53a8
PA
3526static bool
3527dw2_map_symtabs_matching_filename
3528 (struct objfile *objfile, const char *name, const char *real_path,
3529 gdb::function_view<bool (symtab *)> callback)
9291a0cd
TT
3530{
3531 int i;
c011a4f4 3532 const char *name_basename = lbasename (name);
9291a0cd
TT
3533
3534 dw2_setup (objfile);
ae2de4f8 3535
848e3e78
DE
3536 /* The rule is CUs specify all the files, including those used by
3537 any TU, so there's no need to scan TUs here. */
f4dc4d17 3538
848e3e78 3539 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3540 {
3541 int j;
8832e7e3 3542 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3543 struct quick_file_names *file_data;
9291a0cd 3544
3d7bb9d9 3545 /* We only need to look at symtabs not already expanded. */
43f3e411 3546 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3547 continue;
3548
e4a48d9d 3549 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3550 if (file_data == NULL)
9291a0cd
TT
3551 continue;
3552
7b9f3c50 3553 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3554 {
7b9f3c50 3555 const char *this_name = file_data->file_names[j];
da235a7c 3556 const char *this_real_name;
9291a0cd 3557
af529f8f 3558 if (compare_filenames_for_search (this_name, name))
9291a0cd 3559 {
f5b95b50 3560 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3561 callback))
3562 return true;
288e77a7 3563 continue;
4aac40c8 3564 }
9291a0cd 3565
c011a4f4
DE
3566 /* Before we invoke realpath, which can get expensive when many
3567 files are involved, do a quick comparison of the basenames. */
3568 if (! basenames_may_differ
3569 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3570 continue;
3571
da235a7c
JK
3572 this_real_name = dw2_get_real_path (objfile, file_data, j);
3573 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3574 {
da235a7c 3575 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3576 callback))
3577 return true;
288e77a7 3578 continue;
da235a7c 3579 }
9291a0cd 3580
da235a7c
JK
3581 if (real_path != NULL)
3582 {
af529f8f
JK
3583 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3584 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3585 if (this_real_name != NULL
af529f8f 3586 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3587 {
f5b95b50 3588 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3589 callback))
3590 return true;
288e77a7 3591 continue;
9291a0cd
TT
3592 }
3593 }
3594 }
3595 }
3596
14bc53a8 3597 return false;
9291a0cd
TT
3598}
3599
da51c347
DE
3600/* Struct used to manage iterating over all CUs looking for a symbol. */
3601
3602struct dw2_symtab_iterator
9291a0cd 3603{
da51c347
DE
3604 /* The internalized form of .gdb_index. */
3605 struct mapped_index *index;
3606 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3607 int want_specific_block;
3608 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3609 Unused if !WANT_SPECIFIC_BLOCK. */
3610 int block_index;
3611 /* The kind of symbol we're looking for. */
3612 domain_enum domain;
3613 /* The list of CUs from the index entry of the symbol,
3614 or NULL if not found. */
3615 offset_type *vec;
3616 /* The next element in VEC to look at. */
3617 int next;
3618 /* The number of elements in VEC, or zero if there is no match. */
3619 int length;
8943b874
DE
3620 /* Have we seen a global version of the symbol?
3621 If so we can ignore all further global instances.
3622 This is to work around gold/15646, inefficient gold-generated
3623 indices. */
3624 int global_seen;
da51c347 3625};
9291a0cd 3626
da51c347
DE
3627/* Initialize the index symtab iterator ITER.
3628 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3629 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3630
9291a0cd 3631static void
da51c347
DE
3632dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3633 struct mapped_index *index,
3634 int want_specific_block,
3635 int block_index,
3636 domain_enum domain,
3637 const char *name)
3638{
3639 iter->index = index;
3640 iter->want_specific_block = want_specific_block;
3641 iter->block_index = block_index;
3642 iter->domain = domain;
3643 iter->next = 0;
8943b874 3644 iter->global_seen = 0;
da51c347
DE
3645
3646 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3647 iter->length = MAYBE_SWAP (*iter->vec);
3648 else
3649 {
3650 iter->vec = NULL;
3651 iter->length = 0;
3652 }
3653}
3654
3655/* Return the next matching CU or NULL if there are no more. */
3656
3657static struct dwarf2_per_cu_data *
3658dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3659{
3660 for ( ; iter->next < iter->length; ++iter->next)
3661 {
3662 offset_type cu_index_and_attrs =
3663 MAYBE_SWAP (iter->vec[iter->next + 1]);
3664 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3665 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3666 int want_static = iter->block_index != GLOBAL_BLOCK;
3667 /* This value is only valid for index versions >= 7. */
3668 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3669 gdb_index_symbol_kind symbol_kind =
3670 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3671 /* Only check the symbol attributes if they're present.
3672 Indices prior to version 7 don't record them,
3673 and indices >= 7 may elide them for certain symbols
3674 (gold does this). */
3675 int attrs_valid =
3676 (iter->index->version >= 7
3677 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3678
3190f0c6
DE
3679 /* Don't crash on bad data. */
3680 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3681 + dwarf2_per_objfile->n_type_units))
3682 {
3683 complaint (&symfile_complaints,
3684 _(".gdb_index entry has bad CU index"
4262abfb
JK
3685 " [in module %s]"),
3686 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3687 continue;
3688 }
3689
8832e7e3 3690 per_cu = dw2_get_cutu (cu_index);
3190f0c6 3691
da51c347 3692 /* Skip if already read in. */
43f3e411 3693 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3694 continue;
3695
8943b874
DE
3696 /* Check static vs global. */
3697 if (attrs_valid)
3698 {
3699 if (iter->want_specific_block
3700 && want_static != is_static)
3701 continue;
3702 /* Work around gold/15646. */
3703 if (!is_static && iter->global_seen)
3704 continue;
3705 if (!is_static)
3706 iter->global_seen = 1;
3707 }
da51c347
DE
3708
3709 /* Only check the symbol's kind if it has one. */
3710 if (attrs_valid)
3711 {
3712 switch (iter->domain)
3713 {
3714 case VAR_DOMAIN:
3715 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3716 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3717 /* Some types are also in VAR_DOMAIN. */
3718 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3719 continue;
3720 break;
3721 case STRUCT_DOMAIN:
3722 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3723 continue;
3724 break;
3725 case LABEL_DOMAIN:
3726 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3727 continue;
3728 break;
3729 default:
3730 break;
3731 }
3732 }
3733
3734 ++iter->next;
3735 return per_cu;
3736 }
3737
3738 return NULL;
3739}
3740
43f3e411 3741static struct compunit_symtab *
da51c347
DE
3742dw2_lookup_symbol (struct objfile *objfile, int block_index,
3743 const char *name, domain_enum domain)
9291a0cd 3744{
43f3e411 3745 struct compunit_symtab *stab_best = NULL;
156942c7
DE
3746 struct mapped_index *index;
3747
9291a0cd
TT
3748 dw2_setup (objfile);
3749
156942c7
DE
3750 index = dwarf2_per_objfile->index_table;
3751
da51c347 3752 /* index is NULL if OBJF_READNOW. */
156942c7 3753 if (index)
9291a0cd 3754 {
da51c347
DE
3755 struct dw2_symtab_iterator iter;
3756 struct dwarf2_per_cu_data *per_cu;
3757
3758 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3759
da51c347 3760 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3761 {
b2e2f908 3762 struct symbol *sym, *with_opaque = NULL;
43f3e411
DE
3763 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3764 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
f194fefb 3765 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3766
b2e2f908
DE
3767 sym = block_find_symbol (block, name, domain,
3768 block_find_non_opaque_type_preferred,
3769 &with_opaque);
3770
da51c347
DE
3771 /* Some caution must be observed with overloaded functions
3772 and methods, since the index will not contain any overload
3773 information (but NAME might contain it). */
da51c347 3774
b2e2f908
DE
3775 if (sym != NULL
3776 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3777 return stab;
3778 if (with_opaque != NULL
3779 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3780 stab_best = stab;
da51c347
DE
3781
3782 /* Keep looking through other CUs. */
9291a0cd
TT
3783 }
3784 }
9291a0cd 3785
da51c347 3786 return stab_best;
9291a0cd
TT
3787}
3788
3789static void
3790dw2_print_stats (struct objfile *objfile)
3791{
e4a48d9d 3792 int i, total, count;
9291a0cd
TT
3793
3794 dw2_setup (objfile);
e4a48d9d 3795 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3796 count = 0;
e4a48d9d 3797 for (i = 0; i < total; ++i)
9291a0cd 3798 {
8832e7e3 3799 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3800
43f3e411 3801 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3802 ++count;
3803 }
e4a48d9d 3804 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3805 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3806}
3807
779bd270
DE
3808/* This dumps minimal information about the index.
3809 It is called via "mt print objfiles".
3810 One use is to verify .gdb_index has been loaded by the
3811 gdb.dwarf2/gdb-index.exp testcase. */
3812
9291a0cd
TT
3813static void
3814dw2_dump (struct objfile *objfile)
3815{
779bd270
DE
3816 dw2_setup (objfile);
3817 gdb_assert (dwarf2_per_objfile->using_index);
3818 printf_filtered (".gdb_index:");
3819 if (dwarf2_per_objfile->index_table != NULL)
3820 {
3821 printf_filtered (" version %d\n",
3822 dwarf2_per_objfile->index_table->version);
3823 }
3824 else
3825 printf_filtered (" faked for \"readnow\"\n");
3826 printf_filtered ("\n");
9291a0cd
TT
3827}
3828
3829static void
3189cb12
DE
3830dw2_relocate (struct objfile *objfile,
3831 const struct section_offsets *new_offsets,
3832 const struct section_offsets *delta)
9291a0cd
TT
3833{
3834 /* There's nothing to relocate here. */
3835}
3836
3837static void
3838dw2_expand_symtabs_for_function (struct objfile *objfile,
3839 const char *func_name)
3840{
da51c347
DE
3841 struct mapped_index *index;
3842
3843 dw2_setup (objfile);
3844
3845 index = dwarf2_per_objfile->index_table;
3846
3847 /* index is NULL if OBJF_READNOW. */
3848 if (index)
3849 {
3850 struct dw2_symtab_iterator iter;
3851 struct dwarf2_per_cu_data *per_cu;
3852
3853 /* Note: It doesn't matter what we pass for block_index here. */
3854 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3855 func_name);
3856
3857 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3858 dw2_instantiate_symtab (per_cu);
3859 }
9291a0cd
TT
3860}
3861
3862static void
3863dw2_expand_all_symtabs (struct objfile *objfile)
3864{
3865 int i;
3866
3867 dw2_setup (objfile);
1fd400ff
TT
3868
3869 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3870 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3871 {
8832e7e3 3872 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 3873
a0f42c21 3874 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3875 }
3876}
3877
3878static void
652a8996
JK
3879dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3880 const char *fullname)
9291a0cd
TT
3881{
3882 int i;
3883
3884 dw2_setup (objfile);
d4637a04
DE
3885
3886 /* We don't need to consider type units here.
3887 This is only called for examining code, e.g. expand_line_sal.
3888 There can be an order of magnitude (or more) more type units
3889 than comp units, and we avoid them if we can. */
3890
3891 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3892 {
3893 int j;
8832e7e3 3894 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
7b9f3c50 3895 struct quick_file_names *file_data;
9291a0cd 3896
3d7bb9d9 3897 /* We only need to look at symtabs not already expanded. */
43f3e411 3898 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3899 continue;
3900
e4a48d9d 3901 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3902 if (file_data == NULL)
9291a0cd
TT
3903 continue;
3904
7b9f3c50 3905 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3906 {
652a8996
JK
3907 const char *this_fullname = file_data->file_names[j];
3908
3909 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3910 {
a0f42c21 3911 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3912 break;
3913 }
3914 }
3915 }
3916}
3917
9291a0cd 3918static void
ade7ed9e 3919dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 3920 const char * name, domain_enum domain,
ade7ed9e 3921 int global,
40658b94
PH
3922 int (*callback) (struct block *,
3923 struct symbol *, void *),
2edb89d3
JK
3924 void *data, symbol_compare_ftype *match,
3925 symbol_compare_ftype *ordered_compare)
9291a0cd 3926{
40658b94 3927 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3928 current language is Ada for a non-Ada objfile using GNU index. As Ada
3929 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3930}
3931
3932static void
f8eba3c6
TT
3933dw2_expand_symtabs_matching
3934 (struct objfile *objfile,
14bc53a8
PA
3935 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3936 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3937 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
3938 enum search_domain kind)
9291a0cd
TT
3939{
3940 int i;
3941 offset_type iter;
4b5246aa 3942 struct mapped_index *index;
9291a0cd
TT
3943
3944 dw2_setup (objfile);
ae2de4f8
DE
3945
3946 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3947 if (!dwarf2_per_objfile->index_table)
3948 return;
4b5246aa 3949 index = dwarf2_per_objfile->index_table;
9291a0cd 3950
7b08b9eb 3951 if (file_matcher != NULL)
24c79950 3952 {
fc4007c9
TT
3953 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3954 htab_eq_pointer,
3955 NULL, xcalloc, xfree));
3956 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3957 htab_eq_pointer,
3958 NULL, xcalloc, xfree));
24c79950 3959
848e3e78
DE
3960 /* The rule is CUs specify all the files, including those used by
3961 any TU, so there's no need to scan TUs here. */
3962
3963 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3964 {
3965 int j;
8832e7e3 3966 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
24c79950
TT
3967 struct quick_file_names *file_data;
3968 void **slot;
7b08b9eb 3969
61d96d7e
DE
3970 QUIT;
3971
24c79950 3972 per_cu->v.quick->mark = 0;
3d7bb9d9 3973
24c79950 3974 /* We only need to look at symtabs not already expanded. */
43f3e411 3975 if (per_cu->v.quick->compunit_symtab)
24c79950 3976 continue;
7b08b9eb 3977
e4a48d9d 3978 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3979 if (file_data == NULL)
3980 continue;
7b08b9eb 3981
fc4007c9 3982 if (htab_find (visited_not_found.get (), file_data) != NULL)
24c79950 3983 continue;
fc4007c9 3984 else if (htab_find (visited_found.get (), file_data) != NULL)
24c79950
TT
3985 {
3986 per_cu->v.quick->mark = 1;
3987 continue;
3988 }
3989
3990 for (j = 0; j < file_data->num_file_names; ++j)
3991 {
da235a7c
JK
3992 const char *this_real_name;
3993
14bc53a8 3994 if (file_matcher (file_data->file_names[j], false))
24c79950
TT
3995 {
3996 per_cu->v.quick->mark = 1;
3997 break;
3998 }
da235a7c
JK
3999
4000 /* Before we invoke realpath, which can get expensive when many
4001 files are involved, do a quick comparison of the basenames. */
4002 if (!basenames_may_differ
4003 && !file_matcher (lbasename (file_data->file_names[j]),
14bc53a8 4004 true))
da235a7c
JK
4005 continue;
4006
4007 this_real_name = dw2_get_real_path (objfile, file_data, j);
14bc53a8 4008 if (file_matcher (this_real_name, false))
da235a7c
JK
4009 {
4010 per_cu->v.quick->mark = 1;
4011 break;
4012 }
24c79950
TT
4013 }
4014
4015 slot = htab_find_slot (per_cu->v.quick->mark
fc4007c9
TT
4016 ? visited_found.get ()
4017 : visited_not_found.get (),
24c79950
TT
4018 file_data, INSERT);
4019 *slot = file_data;
4020 }
24c79950 4021 }
9291a0cd 4022
3876f04e 4023 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
4024 {
4025 offset_type idx = 2 * iter;
4026 const char *name;
4027 offset_type *vec, vec_len, vec_idx;
8943b874 4028 int global_seen = 0;
9291a0cd 4029
61d96d7e
DE
4030 QUIT;
4031
3876f04e 4032 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
4033 continue;
4034
3876f04e 4035 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 4036
14bc53a8 4037 if (!symbol_matcher (name))
9291a0cd
TT
4038 continue;
4039
4040 /* The name was matched, now expand corresponding CUs that were
4041 marked. */
4b5246aa 4042 vec = (offset_type *) (index->constant_pool
3876f04e 4043 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
4044 vec_len = MAYBE_SWAP (vec[0]);
4045 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4046 {
e254ef6a 4047 struct dwarf2_per_cu_data *per_cu;
156942c7 4048 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
8943b874
DE
4049 /* This value is only valid for index versions >= 7. */
4050 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
156942c7
DE
4051 gdb_index_symbol_kind symbol_kind =
4052 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4053 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
4054 /* Only check the symbol attributes if they're present.
4055 Indices prior to version 7 don't record them,
4056 and indices >= 7 may elide them for certain symbols
4057 (gold does this). */
4058 int attrs_valid =
4059 (index->version >= 7
4060 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4061
8943b874
DE
4062 /* Work around gold/15646. */
4063 if (attrs_valid)
4064 {
4065 if (!is_static && global_seen)
4066 continue;
4067 if (!is_static)
4068 global_seen = 1;
4069 }
4070
3190f0c6
DE
4071 /* Only check the symbol's kind if it has one. */
4072 if (attrs_valid)
156942c7
DE
4073 {
4074 switch (kind)
4075 {
4076 case VARIABLES_DOMAIN:
4077 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4078 continue;
4079 break;
4080 case FUNCTIONS_DOMAIN:
4081 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4082 continue;
4083 break;
4084 case TYPES_DOMAIN:
4085 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4086 continue;
4087 break;
4088 default:
4089 break;
4090 }
4091 }
4092
3190f0c6
DE
4093 /* Don't crash on bad data. */
4094 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4095 + dwarf2_per_objfile->n_type_units))
4096 {
4097 complaint (&symfile_complaints,
4098 _(".gdb_index entry has bad CU index"
4262abfb 4099 " [in module %s]"), objfile_name (objfile));
3190f0c6
DE
4100 continue;
4101 }
4102
8832e7e3 4103 per_cu = dw2_get_cutu (cu_index);
7b08b9eb 4104 if (file_matcher == NULL || per_cu->v.quick->mark)
276d885b
GB
4105 {
4106 int symtab_was_null =
4107 (per_cu->v.quick->compunit_symtab == NULL);
4108
4109 dw2_instantiate_symtab (per_cu);
4110
4111 if (expansion_notify != NULL
4112 && symtab_was_null
4113 && per_cu->v.quick->compunit_symtab != NULL)
4114 {
14bc53a8 4115 expansion_notify (per_cu->v.quick->compunit_symtab);
276d885b
GB
4116 }
4117 }
9291a0cd
TT
4118 }
4119 }
4120}
4121
43f3e411 4122/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
9703b513
TT
4123 symtab. */
4124
43f3e411
DE
4125static struct compunit_symtab *
4126recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4127 CORE_ADDR pc)
9703b513
TT
4128{
4129 int i;
4130
43f3e411
DE
4131 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4132 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4133 return cust;
9703b513 4134
43f3e411 4135 if (cust->includes == NULL)
a3ec0bb1
DE
4136 return NULL;
4137
43f3e411 4138 for (i = 0; cust->includes[i]; ++i)
9703b513 4139 {
43f3e411 4140 struct compunit_symtab *s = cust->includes[i];
9703b513 4141
43f3e411 4142 s = recursively_find_pc_sect_compunit_symtab (s, pc);
9703b513
TT
4143 if (s != NULL)
4144 return s;
4145 }
4146
4147 return NULL;
4148}
4149
43f3e411
DE
4150static struct compunit_symtab *
4151dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4152 struct bound_minimal_symbol msymbol,
4153 CORE_ADDR pc,
4154 struct obj_section *section,
4155 int warn_if_readin)
9291a0cd
TT
4156{
4157 struct dwarf2_per_cu_data *data;
43f3e411 4158 struct compunit_symtab *result;
9291a0cd
TT
4159
4160 dw2_setup (objfile);
4161
4162 if (!objfile->psymtabs_addrmap)
4163 return NULL;
4164
9a3c8263
SM
4165 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4166 pc);
9291a0cd
TT
4167 if (!data)
4168 return NULL;
4169
43f3e411 4170 if (warn_if_readin && data->v.quick->compunit_symtab)
abebb8b0 4171 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
4172 paddress (get_objfile_arch (objfile), pc));
4173
43f3e411
DE
4174 result
4175 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4176 pc);
9703b513
TT
4177 gdb_assert (result != NULL);
4178 return result;
9291a0cd
TT
4179}
4180
9291a0cd 4181static void
44b13c5a 4182dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 4183 void *data, int need_fullname)
9291a0cd
TT
4184{
4185 int i;
fc4007c9
TT
4186 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4187 NULL, xcalloc, xfree));
9291a0cd
TT
4188
4189 dw2_setup (objfile);
ae2de4f8 4190
848e3e78
DE
4191 /* The rule is CUs specify all the files, including those used by
4192 any TU, so there's no need to scan TUs here.
4193 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 4194
848e3e78 4195 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950 4196 {
8832e7e3 4197 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
24c79950 4198
43f3e411 4199 if (per_cu->v.quick->compunit_symtab)
24c79950 4200 {
fc4007c9
TT
4201 void **slot = htab_find_slot (visited.get (),
4202 per_cu->v.quick->file_names,
24c79950
TT
4203 INSERT);
4204
4205 *slot = per_cu->v.quick->file_names;
4206 }
4207 }
4208
848e3e78 4209 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
4210 {
4211 int j;
8832e7e3 4212 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 4213 struct quick_file_names *file_data;
24c79950 4214 void **slot;
9291a0cd 4215
3d7bb9d9 4216 /* We only need to look at symtabs not already expanded. */
43f3e411 4217 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4218 continue;
4219
e4a48d9d 4220 file_data = dw2_get_file_names (per_cu);
7b9f3c50 4221 if (file_data == NULL)
9291a0cd
TT
4222 continue;
4223
fc4007c9 4224 slot = htab_find_slot (visited.get (), file_data, INSERT);
24c79950
TT
4225 if (*slot)
4226 {
4227 /* Already visited. */
4228 continue;
4229 }
4230 *slot = file_data;
4231
7b9f3c50 4232 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4233 {
74e2f255
DE
4234 const char *this_real_name;
4235
4236 if (need_fullname)
4237 this_real_name = dw2_get_real_path (objfile, file_data, j);
4238 else
4239 this_real_name = NULL;
7b9f3c50 4240 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
4241 }
4242 }
4243}
4244
4245static int
4246dw2_has_symbols (struct objfile *objfile)
4247{
4248 return 1;
4249}
4250
4251const struct quick_symbol_functions dwarf2_gdb_index_functions =
4252{
4253 dw2_has_symbols,
4254 dw2_find_last_source_symtab,
4255 dw2_forget_cached_source_info,
f8eba3c6 4256 dw2_map_symtabs_matching_filename,
9291a0cd 4257 dw2_lookup_symbol,
9291a0cd
TT
4258 dw2_print_stats,
4259 dw2_dump,
4260 dw2_relocate,
4261 dw2_expand_symtabs_for_function,
4262 dw2_expand_all_symtabs,
652a8996 4263 dw2_expand_symtabs_with_fullname,
40658b94 4264 dw2_map_matching_symbols,
9291a0cd 4265 dw2_expand_symtabs_matching,
43f3e411 4266 dw2_find_pc_sect_compunit_symtab,
9291a0cd
TT
4267 dw2_map_symbol_filenames
4268};
4269
4270/* Initialize for reading DWARF for this objfile. Return 0 if this
4271 file will use psymtabs, or 1 if using the GNU index. */
4272
4273int
4274dwarf2_initialize_objfile (struct objfile *objfile)
4275{
4276 /* If we're about to read full symbols, don't bother with the
4277 indices. In this case we also don't care if some other debug
4278 format is making psymtabs, because they are all about to be
4279 expanded anyway. */
4280 if ((objfile->flags & OBJF_READNOW))
4281 {
4282 int i;
4283
4284 dwarf2_per_objfile->using_index = 1;
4285 create_all_comp_units (objfile);
0e50663e 4286 create_all_type_units (objfile);
7b9f3c50
DE
4287 dwarf2_per_objfile->quick_file_names_table =
4288 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 4289
1fd400ff 4290 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 4291 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 4292 {
8832e7e3 4293 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
9291a0cd 4294
e254ef6a
DE
4295 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4296 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
4297 }
4298
4299 /* Return 1 so that gdb sees the "quick" functions. However,
4300 these functions will be no-ops because we will have expanded
4301 all symtabs. */
4302 return 1;
4303 }
4304
4305 if (dwarf2_read_index (objfile))
4306 return 1;
4307
9291a0cd
TT
4308 return 0;
4309}
4310
4311\f
4312
dce234bc
PP
4313/* Build a partial symbol table. */
4314
4315void
f29dff0a 4316dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 4317{
c9bf0622 4318
f29dff0a 4319 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
4320 {
4321 init_psymbol_list (objfile, 1024);
4322 }
4323
492d29ea 4324 TRY
c9bf0622
TT
4325 {
4326 /* This isn't really ideal: all the data we allocate on the
4327 objfile's obstack is still uselessly kept around. However,
4328 freeing it seems unsafe. */
906768f9 4329 psymtab_discarder psymtabs (objfile);
c9bf0622 4330 dwarf2_build_psymtabs_hard (objfile);
906768f9 4331 psymtabs.keep ();
c9bf0622 4332 }
492d29ea
PA
4333 CATCH (except, RETURN_MASK_ERROR)
4334 {
4335 exception_print (gdb_stderr, except);
4336 }
4337 END_CATCH
c906108c 4338}
c906108c 4339
1ce1cefd
DE
4340/* Return the total length of the CU described by HEADER. */
4341
4342static unsigned int
4343get_cu_length (const struct comp_unit_head *header)
4344{
4345 return header->initial_length_size + header->length;
4346}
4347
45452591
DE
4348/* Return TRUE if OFFSET is within CU_HEADER. */
4349
4350static inline int
b64f50a1 4351offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 4352{
b64f50a1 4353 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 4354 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 4355
b64f50a1 4356 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
4357}
4358
3b80fe9b
DE
4359/* Find the base address of the compilation unit for range lists and
4360 location lists. It will normally be specified by DW_AT_low_pc.
4361 In DWARF-3 draft 4, the base address could be overridden by
4362 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4363 compilation units with discontinuous ranges. */
4364
4365static void
4366dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4367{
4368 struct attribute *attr;
4369
4370 cu->base_known = 0;
4371 cu->base_address = 0;
4372
4373 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4374 if (attr)
4375 {
31aa7e4e 4376 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4377 cu->base_known = 1;
4378 }
4379 else
4380 {
4381 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4382 if (attr)
4383 {
31aa7e4e 4384 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
4385 cu->base_known = 1;
4386 }
4387 }
4388}
4389
93311388 4390/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 4391 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
4392 NOTE: This leaves members offset, first_die_offset to be filled in
4393 by the caller. */
107d2387 4394
d521ce57 4395static const gdb_byte *
107d2387 4396read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
4397 const gdb_byte *info_ptr,
4398 struct dwarf2_section_info *section,
4399 rcuh_kind section_kind)
107d2387
AC
4400{
4401 int signed_addr;
891d2f0b 4402 unsigned int bytes_read;
43988095
JK
4403 const char *filename = get_section_file_name (section);
4404 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
4405
4406 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4407 cu_header->initial_length_size = bytes_read;
4408 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 4409 info_ptr += bytes_read;
107d2387
AC
4410 cu_header->version = read_2_bytes (abfd, info_ptr);
4411 info_ptr += 2;
43988095
JK
4412 if (cu_header->version < 5)
4413 switch (section_kind)
4414 {
4415 case rcuh_kind::COMPILE:
4416 cu_header->unit_type = DW_UT_compile;
4417 break;
4418 case rcuh_kind::TYPE:
4419 cu_header->unit_type = DW_UT_type;
4420 break;
4421 default:
4422 internal_error (__FILE__, __LINE__,
4423 _("read_comp_unit_head: invalid section_kind"));
4424 }
4425 else
4426 {
4427 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4428 (read_1_byte (abfd, info_ptr));
4429 info_ptr += 1;
4430 switch (cu_header->unit_type)
4431 {
4432 case DW_UT_compile:
4433 if (section_kind != rcuh_kind::COMPILE)
4434 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4435 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4436 filename);
4437 break;
4438 case DW_UT_type:
4439 section_kind = rcuh_kind::TYPE;
4440 break;
4441 default:
4442 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4443 "(is %d, should be %d or %d) [in module %s]"),
4444 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4445 }
4446
4447 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4448 info_ptr += 1;
4449 }
b64f50a1
JK
4450 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4451 &bytes_read);
613e1657 4452 info_ptr += bytes_read;
43988095
JK
4453 if (cu_header->version < 5)
4454 {
4455 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4456 info_ptr += 1;
4457 }
107d2387
AC
4458 signed_addr = bfd_get_sign_extend_vma (abfd);
4459 if (signed_addr < 0)
8e65ff28 4460 internal_error (__FILE__, __LINE__,
e2e0b3e5 4461 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 4462 cu_header->signed_addr_p = signed_addr;
c764a876 4463
43988095
JK
4464 if (section_kind == rcuh_kind::TYPE)
4465 {
4466 LONGEST type_offset;
4467
4468 cu_header->signature = read_8_bytes (abfd, info_ptr);
4469 info_ptr += 8;
4470
4471 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4472 info_ptr += bytes_read;
4473 cu_header->type_offset_in_tu.cu_off = type_offset;
4474 if (cu_header->type_offset_in_tu.cu_off != type_offset)
4475 error (_("Dwarf Error: Too big type_offset in compilation unit "
4476 "header (is %s) [in module %s]"), plongest (type_offset),
4477 filename);
4478 }
4479
107d2387
AC
4480 return info_ptr;
4481}
4482
36586728
TT
4483/* Helper function that returns the proper abbrev section for
4484 THIS_CU. */
4485
4486static struct dwarf2_section_info *
4487get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4488{
4489 struct dwarf2_section_info *abbrev;
4490
4491 if (this_cu->is_dwz)
4492 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4493 else
4494 abbrev = &dwarf2_per_objfile->abbrev;
4495
4496 return abbrev;
4497}
4498
9ff913ba
DE
4499/* Subroutine of read_and_check_comp_unit_head and
4500 read_and_check_type_unit_head to simplify them.
4501 Perform various error checking on the header. */
4502
4503static void
4504error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4505 struct dwarf2_section_info *section,
4506 struct dwarf2_section_info *abbrev_section)
9ff913ba 4507{
a32a8923 4508 const char *filename = get_section_file_name (section);
9ff913ba 4509
43988095 4510 if (header->version < 2 || header->version > 5)
9ff913ba 4511 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 4512 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
4513 filename);
4514
b64f50a1 4515 if (header->abbrev_offset.sect_off
36586728 4516 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4517 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4518 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4519 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4520 filename);
4521
4522 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4523 avoid potential 32-bit overflow. */
1ce1cefd 4524 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4525 > section->size)
4526 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4527 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4528 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4529 filename);
4530}
4531
4532/* Read in a CU/TU header and perform some basic error checking.
4533 The contents of the header are stored in HEADER.
4534 The result is a pointer to the start of the first DIE. */
adabb602 4535
d521ce57 4536static const gdb_byte *
9ff913ba
DE
4537read_and_check_comp_unit_head (struct comp_unit_head *header,
4538 struct dwarf2_section_info *section,
4bdcc0c1 4539 struct dwarf2_section_info *abbrev_section,
d521ce57 4540 const gdb_byte *info_ptr,
43988095 4541 rcuh_kind section_kind)
72bf9492 4542{
d521ce57 4543 const gdb_byte *beg_of_comp_unit = info_ptr;
a32a8923 4544 bfd *abfd = get_section_bfd_owner (section);
72bf9492 4545
b64f50a1 4546 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4547
43988095 4548 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 4549
b64f50a1 4550 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4551
4bdcc0c1 4552 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4553
4554 return info_ptr;
348e048f
DE
4555}
4556
f4dc4d17
DE
4557/* Fetch the abbreviation table offset from a comp or type unit header. */
4558
4559static sect_offset
4560read_abbrev_offset (struct dwarf2_section_info *section,
4561 sect_offset offset)
4562{
a32a8923 4563 bfd *abfd = get_section_bfd_owner (section);
d521ce57 4564 const gdb_byte *info_ptr;
ac298888 4565 unsigned int initial_length_size, offset_size;
f4dc4d17 4566 sect_offset abbrev_offset;
43988095 4567 uint16_t version;
f4dc4d17
DE
4568
4569 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4570 info_ptr = section->buffer + offset.sect_off;
ac298888 4571 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 4572 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
4573 info_ptr += initial_length_size;
4574
4575 version = read_2_bytes (abfd, info_ptr);
4576 info_ptr += 2;
4577 if (version >= 5)
4578 {
4579 /* Skip unit type and address size. */
4580 info_ptr += 2;
4581 }
4582
f4dc4d17
DE
4583 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4584 return abbrev_offset;
4585}
4586
aaa75496
JB
4587/* Allocate a new partial symtab for file named NAME and mark this new
4588 partial symtab as being an include of PST. */
4589
4590static void
d521ce57 4591dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4592 struct objfile *objfile)
4593{
4594 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4595
fbd9ab74
JK
4596 if (!IS_ABSOLUTE_PATH (subpst->filename))
4597 {
4598 /* It shares objfile->objfile_obstack. */
4599 subpst->dirname = pst->dirname;
4600 }
4601
aaa75496
JB
4602 subpst->textlow = 0;
4603 subpst->texthigh = 0;
4604
8d749320
SM
4605 subpst->dependencies
4606 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
4607 subpst->dependencies[0] = pst;
4608 subpst->number_of_dependencies = 1;
4609
4610 subpst->globals_offset = 0;
4611 subpst->n_global_syms = 0;
4612 subpst->statics_offset = 0;
4613 subpst->n_static_syms = 0;
43f3e411 4614 subpst->compunit_symtab = NULL;
aaa75496
JB
4615 subpst->read_symtab = pst->read_symtab;
4616 subpst->readin = 0;
4617
4618 /* No private part is necessary for include psymtabs. This property
4619 can be used to differentiate between such include psymtabs and
10b3939b 4620 the regular ones. */
58a9656e 4621 subpst->read_symtab_private = NULL;
aaa75496
JB
4622}
4623
4624/* Read the Line Number Program data and extract the list of files
4625 included by the source file represented by PST. Build an include
d85a05f0 4626 partial symtab for each of these included files. */
aaa75496
JB
4627
4628static void
4629dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4630 struct die_info *die,
4631 struct partial_symtab *pst)
aaa75496 4632{
d85a05f0
DJ
4633 struct line_header *lh = NULL;
4634 struct attribute *attr;
aaa75496 4635
d85a05f0
DJ
4636 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4637 if (attr)
3019eac3 4638 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4639 if (lh == NULL)
4640 return; /* No linetable, so no includes. */
4641
c6da4cef 4642 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
527f3840 4643 dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
4644
4645 free_line_header (lh);
4646}
4647
348e048f 4648static hashval_t
52dc124a 4649hash_signatured_type (const void *item)
348e048f 4650{
9a3c8263
SM
4651 const struct signatured_type *sig_type
4652 = (const struct signatured_type *) item;
9a619af0 4653
348e048f 4654 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4655 return sig_type->signature;
348e048f
DE
4656}
4657
4658static int
52dc124a 4659eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 4660{
9a3c8263
SM
4661 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4662 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 4663
348e048f
DE
4664 return lhs->signature == rhs->signature;
4665}
4666
1fd400ff
TT
4667/* Allocate a hash table for signatured types. */
4668
4669static htab_t
673bfd45 4670allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4671{
4672 return htab_create_alloc_ex (41,
52dc124a
DE
4673 hash_signatured_type,
4674 eq_signatured_type,
1fd400ff
TT
4675 NULL,
4676 &objfile->objfile_obstack,
4677 hashtab_obstack_allocate,
4678 dummy_obstack_deallocate);
4679}
4680
d467dd73 4681/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4682
4683static int
d467dd73 4684add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 4685{
9a3c8263
SM
4686 struct signatured_type *sigt = (struct signatured_type *) *slot;
4687 struct signatured_type ***datap = (struct signatured_type ***) datum;
1fd400ff 4688
b4dd5633 4689 **datap = sigt;
1fd400ff
TT
4690 ++*datap;
4691
4692 return 1;
4693}
4694
78d4d2c5 4695/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
4696 and fill them into TYPES_HTAB. It will process only type units,
4697 therefore DW_UT_type. */
c88ee1f0 4698
78d4d2c5
JK
4699static void
4700create_debug_type_hash_table (struct dwo_file *dwo_file,
43988095
JK
4701 dwarf2_section_info *section, htab_t &types_htab,
4702 rcuh_kind section_kind)
348e048f 4703{
3019eac3 4704 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 4705 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
4706 bfd *abfd;
4707 const gdb_byte *info_ptr, *end_ptr;
348e048f 4708
4bdcc0c1
DE
4709 abbrev_section = (dwo_file != NULL
4710 ? &dwo_file->sections.abbrev
4711 : &dwarf2_per_objfile->abbrev);
4712
b4f54984 4713 if (dwarf_read_debug)
43988095
JK
4714 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4715 get_section_name (section),
a32a8923 4716 get_section_file_name (abbrev_section));
09406207 4717
78d4d2c5
JK
4718 dwarf2_read_section (objfile, section);
4719 info_ptr = section->buffer;
348e048f 4720
78d4d2c5
JK
4721 if (info_ptr == NULL)
4722 return;
348e048f 4723
78d4d2c5
JK
4724 /* We can't set abfd until now because the section may be empty or
4725 not present, in which case the bfd is unknown. */
4726 abfd = get_section_bfd_owner (section);
348e048f 4727
78d4d2c5
JK
4728 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4729 because we don't need to read any dies: the signature is in the
4730 header. */
3019eac3 4731
78d4d2c5
JK
4732 end_ptr = info_ptr + section->size;
4733 while (info_ptr < end_ptr)
4734 {
4735 sect_offset offset;
78d4d2c5
JK
4736 struct signatured_type *sig_type;
4737 struct dwo_unit *dwo_tu;
4738 void **slot;
4739 const gdb_byte *ptr = info_ptr;
4740 struct comp_unit_head header;
4741 unsigned int length;
8b70b953 4742
78d4d2c5 4743 offset.sect_off = ptr - section->buffer;
348e048f 4744
a49dd8dd
JK
4745 /* Initialize it due to a false compiler warning. */
4746 header.signature = -1;
4747 header.type_offset_in_tu.cu_off = -1;
4748
78d4d2c5
JK
4749 /* We need to read the type's signature in order to build the hash
4750 table, but we don't need anything else just yet. */
348e048f 4751
43988095
JK
4752 ptr = read_and_check_comp_unit_head (&header, section,
4753 abbrev_section, ptr, section_kind);
348e048f 4754
78d4d2c5 4755 length = get_cu_length (&header);
6caca83c 4756
78d4d2c5
JK
4757 /* Skip dummy type units. */
4758 if (ptr >= info_ptr + length
43988095
JK
4759 || peek_abbrev_code (abfd, ptr) == 0
4760 || header.unit_type != DW_UT_type)
78d4d2c5
JK
4761 {
4762 info_ptr += length;
4763 continue;
4764 }
dee91e82 4765
78d4d2c5
JK
4766 if (types_htab == NULL)
4767 {
4768 if (dwo_file)
4769 types_htab = allocate_dwo_unit_table (objfile);
4770 else
4771 types_htab = allocate_signatured_type_table (objfile);
4772 }
8b70b953 4773
78d4d2c5
JK
4774 if (dwo_file)
4775 {
4776 sig_type = NULL;
4777 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4778 struct dwo_unit);
4779 dwo_tu->dwo_file = dwo_file;
43988095
JK
4780 dwo_tu->signature = header.signature;
4781 dwo_tu->type_offset_in_tu = header.type_offset_in_tu;
78d4d2c5
JK
4782 dwo_tu->section = section;
4783 dwo_tu->offset = offset;
4784 dwo_tu->length = length;
4785 }
4786 else
4787 {
4788 /* N.B.: type_offset is not usable if this type uses a DWO file.
4789 The real type_offset is in the DWO file. */
4790 dwo_tu = NULL;
4791 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4792 struct signatured_type);
43988095
JK
4793 sig_type->signature = header.signature;
4794 sig_type->type_offset_in_tu = header.type_offset_in_tu;
78d4d2c5
JK
4795 sig_type->per_cu.objfile = objfile;
4796 sig_type->per_cu.is_debug_types = 1;
4797 sig_type->per_cu.section = section;
4798 sig_type->per_cu.offset = offset;
4799 sig_type->per_cu.length = length;
4800 }
4801
4802 slot = htab_find_slot (types_htab,
4803 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4804 INSERT);
4805 gdb_assert (slot != NULL);
4806 if (*slot != NULL)
4807 {
4808 sect_offset dup_offset;
0349ea22 4809
3019eac3
DE
4810 if (dwo_file)
4811 {
78d4d2c5
JK
4812 const struct dwo_unit *dup_tu
4813 = (const struct dwo_unit *) *slot;
4814
4815 dup_offset = dup_tu->offset;
3019eac3
DE
4816 }
4817 else
4818 {
78d4d2c5
JK
4819 const struct signatured_type *dup_tu
4820 = (const struct signatured_type *) *slot;
4821
4822 dup_offset = dup_tu->per_cu.offset;
3019eac3 4823 }
8b70b953 4824
78d4d2c5
JK
4825 complaint (&symfile_complaints,
4826 _("debug type entry at offset 0x%x is duplicate to"
4827 " the entry at offset 0x%x, signature %s"),
4828 offset.sect_off, dup_offset.sect_off,
43988095 4829 hex_string (header.signature));
78d4d2c5
JK
4830 }
4831 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 4832
78d4d2c5
JK
4833 if (dwarf_read_debug > 1)
4834 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4835 offset.sect_off,
43988095 4836 hex_string (header.signature));
3019eac3 4837
78d4d2c5
JK
4838 info_ptr += length;
4839 }
4840}
3019eac3 4841
78d4d2c5
JK
4842/* Create the hash table of all entries in the .debug_types
4843 (or .debug_types.dwo) section(s).
4844 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4845 otherwise it is NULL.
b3c8eb43 4846
78d4d2c5 4847 The result is a pointer to the hash table or NULL if there are no types.
348e048f 4848
78d4d2c5 4849 Note: This function processes DWO files only, not DWP files. */
348e048f 4850
78d4d2c5
JK
4851static void
4852create_debug_types_hash_table (struct dwo_file *dwo_file,
4853 VEC (dwarf2_section_info_def) *types,
4854 htab_t &types_htab)
4855{
4856 int ix;
4857 struct dwarf2_section_info *section;
4858
4859 if (VEC_empty (dwarf2_section_info_def, types))
4860 return;
348e048f 4861
78d4d2c5
JK
4862 for (ix = 0;
4863 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4864 ++ix)
43988095
JK
4865 create_debug_type_hash_table (dwo_file, section, types_htab,
4866 rcuh_kind::TYPE);
3019eac3
DE
4867}
4868
4869/* Create the hash table of all entries in the .debug_types section,
4870 and initialize all_type_units.
4871 The result is zero if there is an error (e.g. missing .debug_types section),
4872 otherwise non-zero. */
4873
4874static int
4875create_all_type_units (struct objfile *objfile)
4876{
78d4d2c5 4877 htab_t types_htab = NULL;
b4dd5633 4878 struct signatured_type **iter;
3019eac3 4879
43988095
JK
4880 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4881 rcuh_kind::COMPILE);
78d4d2c5 4882 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
3019eac3
DE
4883 if (types_htab == NULL)
4884 {
4885 dwarf2_per_objfile->signatured_types = NULL;
4886 return 0;
4887 }
4888
348e048f
DE
4889 dwarf2_per_objfile->signatured_types = types_htab;
4890
6aa5f3a6
DE
4891 dwarf2_per_objfile->n_type_units
4892 = dwarf2_per_objfile->n_allocated_type_units
4893 = htab_elements (types_htab);
8d749320
SM
4894 dwarf2_per_objfile->all_type_units =
4895 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
d467dd73
DE
4896 iter = &dwarf2_per_objfile->all_type_units[0];
4897 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4898 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4899 == dwarf2_per_objfile->n_type_units);
1fd400ff 4900
348e048f
DE
4901 return 1;
4902}
4903
6aa5f3a6
DE
4904/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4905 If SLOT is non-NULL, it is the entry to use in the hash table.
4906 Otherwise we find one. */
4907
4908static struct signatured_type *
4909add_type_unit (ULONGEST sig, void **slot)
4910{
4911 struct objfile *objfile = dwarf2_per_objfile->objfile;
4912 int n_type_units = dwarf2_per_objfile->n_type_units;
4913 struct signatured_type *sig_type;
4914
4915 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4916 ++n_type_units;
4917 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4918 {
4919 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4920 dwarf2_per_objfile->n_allocated_type_units = 1;
4921 dwarf2_per_objfile->n_allocated_type_units *= 2;
4922 dwarf2_per_objfile->all_type_units
224c3ddb
SM
4923 = XRESIZEVEC (struct signatured_type *,
4924 dwarf2_per_objfile->all_type_units,
4925 dwarf2_per_objfile->n_allocated_type_units);
6aa5f3a6
DE
4926 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
4927 }
4928 dwarf2_per_objfile->n_type_units = n_type_units;
4929
4930 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4931 struct signatured_type);
4932 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4933 sig_type->signature = sig;
4934 sig_type->per_cu.is_debug_types = 1;
4935 if (dwarf2_per_objfile->using_index)
4936 {
4937 sig_type->per_cu.v.quick =
4938 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4939 struct dwarf2_per_cu_quick_data);
4940 }
4941
4942 if (slot == NULL)
4943 {
4944 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4945 sig_type, INSERT);
4946 }
4947 gdb_assert (*slot == NULL);
4948 *slot = sig_type;
4949 /* The rest of sig_type must be filled in by the caller. */
4950 return sig_type;
4951}
4952
a2ce51a0
DE
4953/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4954 Fill in SIG_ENTRY with DWO_ENTRY. */
4955
4956static void
4957fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4958 struct signatured_type *sig_entry,
4959 struct dwo_unit *dwo_entry)
4960{
7ee85ab1 4961 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4962 gdb_assert (! sig_entry->per_cu.queued);
4963 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
4964 if (dwarf2_per_objfile->using_index)
4965 {
4966 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 4967 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
4968 }
4969 else
4970 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0
DE
4971 gdb_assert (sig_entry->signature == dwo_entry->signature);
4972 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4973 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4974 gdb_assert (sig_entry->dwo_unit == NULL);
4975
4976 sig_entry->per_cu.section = dwo_entry->section;
4977 sig_entry->per_cu.offset = dwo_entry->offset;
4978 sig_entry->per_cu.length = dwo_entry->length;
4979 sig_entry->per_cu.reading_dwo_directly = 1;
4980 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4981 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4982 sig_entry->dwo_unit = dwo_entry;
4983}
4984
4985/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4986 If we haven't read the TU yet, create the signatured_type data structure
4987 for a TU to be read in directly from a DWO file, bypassing the stub.
4988 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4989 using .gdb_index, then when reading a CU we want to stay in the DWO file
4990 containing that CU. Otherwise we could end up reading several other DWO
4991 files (due to comdat folding) to process the transitive closure of all the
4992 mentioned TUs, and that can be slow. The current DWO file will have every
4993 type signature that it needs.
a2ce51a0
DE
4994 We only do this for .gdb_index because in the psymtab case we already have
4995 to read all the DWOs to build the type unit groups. */
4996
4997static struct signatured_type *
4998lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4999{
5000 struct objfile *objfile = dwarf2_per_objfile->objfile;
5001 struct dwo_file *dwo_file;
5002 struct dwo_unit find_dwo_entry, *dwo_entry;
5003 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5004 void **slot;
a2ce51a0
DE
5005
5006 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5007
6aa5f3a6
DE
5008 /* If TU skeletons have been removed then we may not have read in any
5009 TUs yet. */
5010 if (dwarf2_per_objfile->signatured_types == NULL)
5011 {
5012 dwarf2_per_objfile->signatured_types
5013 = allocate_signatured_type_table (objfile);
5014 }
a2ce51a0
DE
5015
5016 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5017 Use the global signatured_types array to do our own comdat-folding
5018 of types. If this is the first time we're reading this TU, and
5019 the TU has an entry in .gdb_index, replace the recorded data from
5020 .gdb_index with this TU. */
a2ce51a0 5021
a2ce51a0 5022 find_sig_entry.signature = sig;
6aa5f3a6
DE
5023 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5024 &find_sig_entry, INSERT);
9a3c8263 5025 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5026
5027 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5028 read. Don't reassign the global entry to point to this DWO if that's
5029 the case. Also note that if the TU is already being read, it may not
5030 have come from a DWO, the program may be a mix of Fission-compiled
5031 code and non-Fission-compiled code. */
5032
5033 /* Have we already tried to read this TU?
5034 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5035 needn't exist in the global table yet). */
5036 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
5037 return sig_entry;
5038
6aa5f3a6
DE
5039 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5040 dwo_unit of the TU itself. */
5041 dwo_file = cu->dwo_unit->dwo_file;
5042
a2ce51a0
DE
5043 /* Ok, this is the first time we're reading this TU. */
5044 if (dwo_file->tus == NULL)
5045 return NULL;
5046 find_dwo_entry.signature = sig;
9a3c8263 5047 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
5048 if (dwo_entry == NULL)
5049 return NULL;
5050
6aa5f3a6
DE
5051 /* If the global table doesn't have an entry for this TU, add one. */
5052 if (sig_entry == NULL)
5053 sig_entry = add_type_unit (sig, slot);
5054
a2ce51a0 5055 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 5056 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
5057 return sig_entry;
5058}
5059
a2ce51a0
DE
5060/* Subroutine of lookup_signatured_type.
5061 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
5062 then try the DWP file. If the TU stub (skeleton) has been removed then
5063 it won't be in .gdb_index. */
a2ce51a0
DE
5064
5065static struct signatured_type *
5066lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5067{
5068 struct objfile *objfile = dwarf2_per_objfile->objfile;
5069 struct dwp_file *dwp_file = get_dwp_file ();
5070 struct dwo_unit *dwo_entry;
5071 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5072 void **slot;
a2ce51a0
DE
5073
5074 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5075 gdb_assert (dwp_file != NULL);
5076
6aa5f3a6
DE
5077 /* If TU skeletons have been removed then we may not have read in any
5078 TUs yet. */
5079 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 5080 {
6aa5f3a6
DE
5081 dwarf2_per_objfile->signatured_types
5082 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
5083 }
5084
6aa5f3a6
DE
5085 find_sig_entry.signature = sig;
5086 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5087 &find_sig_entry, INSERT);
9a3c8263 5088 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
5089
5090 /* Have we already tried to read this TU?
5091 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5092 needn't exist in the global table yet). */
5093 if (sig_entry != NULL)
5094 return sig_entry;
5095
a2ce51a0
DE
5096 if (dwp_file->tus == NULL)
5097 return NULL;
57d63ce2
DE
5098 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5099 sig, 1 /* is_debug_types */);
a2ce51a0
DE
5100 if (dwo_entry == NULL)
5101 return NULL;
5102
6aa5f3a6 5103 sig_entry = add_type_unit (sig, slot);
a2ce51a0
DE
5104 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5105
a2ce51a0
DE
5106 return sig_entry;
5107}
5108
380bca97 5109/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
5110 Returns NULL if signature SIG is not present in the table.
5111 It is up to the caller to complain about this. */
348e048f
DE
5112
5113static struct signatured_type *
a2ce51a0 5114lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 5115{
a2ce51a0
DE
5116 if (cu->dwo_unit
5117 && dwarf2_per_objfile->using_index)
5118 {
5119 /* We're in a DWO/DWP file, and we're using .gdb_index.
5120 These cases require special processing. */
5121 if (get_dwp_file () == NULL)
5122 return lookup_dwo_signatured_type (cu, sig);
5123 else
5124 return lookup_dwp_signatured_type (cu, sig);
5125 }
5126 else
5127 {
5128 struct signatured_type find_entry, *entry;
348e048f 5129
a2ce51a0
DE
5130 if (dwarf2_per_objfile->signatured_types == NULL)
5131 return NULL;
5132 find_entry.signature = sig;
9a3c8263
SM
5133 entry = ((struct signatured_type *)
5134 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
5135 return entry;
5136 }
348e048f 5137}
42e7ad6c
DE
5138\f
5139/* Low level DIE reading support. */
348e048f 5140
d85a05f0
DJ
5141/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5142
5143static void
5144init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 5145 struct dwarf2_cu *cu,
3019eac3
DE
5146 struct dwarf2_section_info *section,
5147 struct dwo_file *dwo_file)
d85a05f0 5148{
fceca515 5149 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 5150 reader->abfd = get_section_bfd_owner (section);
d85a05f0 5151 reader->cu = cu;
3019eac3 5152 reader->dwo_file = dwo_file;
dee91e82
DE
5153 reader->die_section = section;
5154 reader->buffer = section->buffer;
f664829e 5155 reader->buffer_end = section->buffer + section->size;
a2ce51a0 5156 reader->comp_dir = NULL;
d85a05f0
DJ
5157}
5158
b0c7bfa9
DE
5159/* Subroutine of init_cutu_and_read_dies to simplify it.
5160 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5161 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5162 already.
5163
5164 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5165 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
5166 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5167 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
5168 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5169 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
5170 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5171 are filled in with the info of the DIE from the DWO file.
5172 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5173 provided an abbrev table to use.
5174 The result is non-zero if a valid (non-dummy) DIE was found. */
5175
5176static int
5177read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5178 struct dwo_unit *dwo_unit,
5179 int abbrev_table_provided,
5180 struct die_info *stub_comp_unit_die,
a2ce51a0 5181 const char *stub_comp_dir,
b0c7bfa9 5182 struct die_reader_specs *result_reader,
d521ce57 5183 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
5184 struct die_info **result_comp_unit_die,
5185 int *result_has_children)
5186{
5187 struct objfile *objfile = dwarf2_per_objfile->objfile;
5188 struct dwarf2_cu *cu = this_cu->cu;
5189 struct dwarf2_section_info *section;
5190 bfd *abfd;
d521ce57 5191 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
5192 ULONGEST signature; /* Or dwo_id. */
5193 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5194 int i,num_extra_attrs;
5195 struct dwarf2_section_info *dwo_abbrev_section;
5196 struct attribute *attr;
5197 struct die_info *comp_unit_die;
5198
b0aeadb3
DE
5199 /* At most one of these may be provided. */
5200 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 5201
b0c7bfa9
DE
5202 /* These attributes aren't processed until later:
5203 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
5204 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5205 referenced later. However, these attributes are found in the stub
5206 which we won't have later. In order to not impose this complication
5207 on the rest of the code, we read them here and copy them to the
5208 DWO CU/TU die. */
b0c7bfa9
DE
5209
5210 stmt_list = NULL;
5211 low_pc = NULL;
5212 high_pc = NULL;
5213 ranges = NULL;
5214 comp_dir = NULL;
5215
5216 if (stub_comp_unit_die != NULL)
5217 {
5218 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5219 DWO file. */
5220 if (! this_cu->is_debug_types)
5221 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5222 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5223 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5224 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5225 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5226
5227 /* There should be a DW_AT_addr_base attribute here (if needed).
5228 We need the value before we can process DW_FORM_GNU_addr_index. */
5229 cu->addr_base = 0;
5230 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5231 if (attr)
5232 cu->addr_base = DW_UNSND (attr);
5233
5234 /* There should be a DW_AT_ranges_base attribute here (if needed).
5235 We need the value before we can process DW_AT_ranges. */
5236 cu->ranges_base = 0;
5237 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5238 if (attr)
5239 cu->ranges_base = DW_UNSND (attr);
5240 }
a2ce51a0
DE
5241 else if (stub_comp_dir != NULL)
5242 {
5243 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 5244 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
5245 comp_dir->name = DW_AT_comp_dir;
5246 comp_dir->form = DW_FORM_string;
5247 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5248 DW_STRING (comp_dir) = stub_comp_dir;
5249 }
b0c7bfa9
DE
5250
5251 /* Set up for reading the DWO CU/TU. */
5252 cu->dwo_unit = dwo_unit;
5253 section = dwo_unit->section;
5254 dwarf2_read_section (objfile, section);
a32a8923 5255 abfd = get_section_bfd_owner (section);
b0c7bfa9
DE
5256 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5257 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5258 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5259
5260 if (this_cu->is_debug_types)
5261 {
b0c7bfa9
DE
5262 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5263
43988095 5264 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
b0c7bfa9 5265 dwo_abbrev_section,
43988095 5266 info_ptr, rcuh_kind::TYPE);
a2ce51a0 5267 /* This is not an assert because it can be caused by bad debug info. */
43988095 5268 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
5269 {
5270 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5271 " TU at offset 0x%x [in module %s]"),
5272 hex_string (sig_type->signature),
43988095 5273 hex_string (cu->header.signature),
a2ce51a0
DE
5274 dwo_unit->offset.sect_off,
5275 bfd_get_filename (abfd));
5276 }
b0c7bfa9
DE
5277 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5278 /* For DWOs coming from DWP files, we don't know the CU length
5279 nor the type's offset in the TU until now. */
5280 dwo_unit->length = get_cu_length (&cu->header);
43988095 5281 dwo_unit->type_offset_in_tu = cu->header.type_offset_in_tu;
b0c7bfa9
DE
5282
5283 /* Establish the type offset that can be used to lookup the type.
5284 For DWO files, we don't know it until now. */
5285 sig_type->type_offset_in_section.sect_off =
5286 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5287 }
5288 else
5289 {
5290 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5291 dwo_abbrev_section,
43988095 5292 info_ptr, rcuh_kind::COMPILE);
b0c7bfa9
DE
5293 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5294 /* For DWOs coming from DWP files, we don't know the CU length
5295 until now. */
5296 dwo_unit->length = get_cu_length (&cu->header);
5297 }
5298
02142a6c
DE
5299 /* Replace the CU's original abbrev table with the DWO's.
5300 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
5301 if (abbrev_table_provided)
5302 {
5303 /* Don't free the provided abbrev table, the caller of
5304 init_cutu_and_read_dies owns it. */
5305 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5306 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
5307 make_cleanup (dwarf2_free_abbrev_table, cu);
5308 }
5309 else
5310 {
5311 dwarf2_free_abbrev_table (cu);
5312 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 5313 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
5314 }
5315
5316 /* Read in the die, but leave space to copy over the attributes
5317 from the stub. This has the benefit of simplifying the rest of
5318 the code - all the work to maintain the illusion of a single
5319 DW_TAG_{compile,type}_unit DIE is done here. */
5320 num_extra_attrs = ((stmt_list != NULL)
5321 + (low_pc != NULL)
5322 + (high_pc != NULL)
5323 + (ranges != NULL)
5324 + (comp_dir != NULL));
5325 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5326 result_has_children, num_extra_attrs);
5327
5328 /* Copy over the attributes from the stub to the DIE we just read in. */
5329 comp_unit_die = *result_comp_unit_die;
5330 i = comp_unit_die->num_attrs;
5331 if (stmt_list != NULL)
5332 comp_unit_die->attrs[i++] = *stmt_list;
5333 if (low_pc != NULL)
5334 comp_unit_die->attrs[i++] = *low_pc;
5335 if (high_pc != NULL)
5336 comp_unit_die->attrs[i++] = *high_pc;
5337 if (ranges != NULL)
5338 comp_unit_die->attrs[i++] = *ranges;
5339 if (comp_dir != NULL)
5340 comp_unit_die->attrs[i++] = *comp_dir;
5341 comp_unit_die->num_attrs += num_extra_attrs;
5342
b4f54984 5343 if (dwarf_die_debug)
bf6af496
DE
5344 {
5345 fprintf_unfiltered (gdb_stdlog,
5346 "Read die from %s@0x%x of %s:\n",
a32a8923 5347 get_section_name (section),
bf6af496
DE
5348 (unsigned) (begin_info_ptr - section->buffer),
5349 bfd_get_filename (abfd));
b4f54984 5350 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
5351 }
5352
a2ce51a0
DE
5353 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5354 TUs by skipping the stub and going directly to the entry in the DWO file.
5355 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5356 to get it via circuitous means. Blech. */
5357 if (comp_dir != NULL)
5358 result_reader->comp_dir = DW_STRING (comp_dir);
5359
b0c7bfa9
DE
5360 /* Skip dummy compilation units. */
5361 if (info_ptr >= begin_info_ptr + dwo_unit->length
5362 || peek_abbrev_code (abfd, info_ptr) == 0)
5363 return 0;
5364
5365 *result_info_ptr = info_ptr;
5366 return 1;
5367}
5368
5369/* Subroutine of init_cutu_and_read_dies to simplify it.
5370 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 5371 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
5372
5373static struct dwo_unit *
5374lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5375 struct die_info *comp_unit_die)
5376{
5377 struct dwarf2_cu *cu = this_cu->cu;
5378 struct attribute *attr;
5379 ULONGEST signature;
5380 struct dwo_unit *dwo_unit;
5381 const char *comp_dir, *dwo_name;
5382
a2ce51a0
DE
5383 gdb_assert (cu != NULL);
5384
b0c7bfa9 5385 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
5386 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5387 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
5388
5389 if (this_cu->is_debug_types)
5390 {
5391 struct signatured_type *sig_type;
5392
5393 /* Since this_cu is the first member of struct signatured_type,
5394 we can go from a pointer to one to a pointer to the other. */
5395 sig_type = (struct signatured_type *) this_cu;
5396 signature = sig_type->signature;
5397 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5398 }
5399 else
5400 {
5401 struct attribute *attr;
5402
5403 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5404 if (! attr)
5405 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5406 " [in module %s]"),
4262abfb 5407 dwo_name, objfile_name (this_cu->objfile));
b0c7bfa9
DE
5408 signature = DW_UNSND (attr);
5409 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5410 signature);
5411 }
5412
b0c7bfa9
DE
5413 return dwo_unit;
5414}
5415
a2ce51a0 5416/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6
DE
5417 See it for a description of the parameters.
5418 Read a TU directly from a DWO file, bypassing the stub.
5419
5420 Note: This function could be a little bit simpler if we shared cleanups
5421 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5422 to do, so we keep this function self-contained. Or we could move this
5423 into our caller, but it's complex enough already. */
a2ce51a0
DE
5424
5425static void
6aa5f3a6
DE
5426init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5427 int use_existing_cu, int keep,
a2ce51a0
DE
5428 die_reader_func_ftype *die_reader_func,
5429 void *data)
5430{
5431 struct dwarf2_cu *cu;
5432 struct signatured_type *sig_type;
6aa5f3a6 5433 struct cleanup *cleanups, *free_cu_cleanup = NULL;
a2ce51a0
DE
5434 struct die_reader_specs reader;
5435 const gdb_byte *info_ptr;
5436 struct die_info *comp_unit_die;
5437 int has_children;
5438
5439 /* Verify we can do the following downcast, and that we have the
5440 data we need. */
5441 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5442 sig_type = (struct signatured_type *) this_cu;
5443 gdb_assert (sig_type->dwo_unit != NULL);
5444
5445 cleanups = make_cleanup (null_cleanup, NULL);
5446
6aa5f3a6
DE
5447 if (use_existing_cu && this_cu->cu != NULL)
5448 {
5449 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5450 cu = this_cu->cu;
5451 /* There's no need to do the rereading_dwo_cu handling that
5452 init_cutu_and_read_dies does since we don't read the stub. */
5453 }
5454 else
5455 {
5456 /* If !use_existing_cu, this_cu->cu must be NULL. */
5457 gdb_assert (this_cu->cu == NULL);
8d749320 5458 cu = XNEW (struct dwarf2_cu);
6aa5f3a6
DE
5459 init_one_comp_unit (cu, this_cu);
5460 /* If an error occurs while loading, release our storage. */
5461 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5462 }
5463
5464 /* A future optimization, if needed, would be to use an existing
5465 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5466 could share abbrev tables. */
a2ce51a0
DE
5467
5468 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5469 0 /* abbrev_table_provided */,
5470 NULL /* stub_comp_unit_die */,
5471 sig_type->dwo_unit->dwo_file->comp_dir,
5472 &reader, &info_ptr,
5473 &comp_unit_die, &has_children) == 0)
5474 {
5475 /* Dummy die. */
5476 do_cleanups (cleanups);
5477 return;
5478 }
5479
5480 /* All the "real" work is done here. */
5481 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5482
6aa5f3a6 5483 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
5484 but the alternative is making the latter more complex.
5485 This function is only for the special case of using DWO files directly:
5486 no point in overly complicating the general case just to handle this. */
6aa5f3a6 5487 if (free_cu_cleanup != NULL)
a2ce51a0 5488 {
6aa5f3a6
DE
5489 if (keep)
5490 {
5491 /* We've successfully allocated this compilation unit. Let our
5492 caller clean it up when finished with it. */
5493 discard_cleanups (free_cu_cleanup);
a2ce51a0 5494
6aa5f3a6
DE
5495 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5496 So we have to manually free the abbrev table. */
5497 dwarf2_free_abbrev_table (cu);
a2ce51a0 5498
6aa5f3a6
DE
5499 /* Link this CU into read_in_chain. */
5500 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5501 dwarf2_per_objfile->read_in_chain = this_cu;
5502 }
5503 else
5504 do_cleanups (free_cu_cleanup);
a2ce51a0 5505 }
a2ce51a0
DE
5506
5507 do_cleanups (cleanups);
5508}
5509
fd820528 5510/* Initialize a CU (or TU) and read its DIEs.
3019eac3 5511 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 5512
f4dc4d17
DE
5513 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5514 Otherwise the table specified in the comp unit header is read in and used.
5515 This is an optimization for when we already have the abbrev table.
5516
dee91e82
DE
5517 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5518 Otherwise, a new CU is allocated with xmalloc.
5519
5520 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5521 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5522
5523 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5524 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5525
70221824 5526static void
fd820528 5527init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5528 struct abbrev_table *abbrev_table,
fd820528
DE
5529 int use_existing_cu, int keep,
5530 die_reader_func_ftype *die_reader_func,
5531 void *data)
c906108c 5532{
dee91e82 5533 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5534 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5535 bfd *abfd = get_section_bfd_owner (section);
dee91e82 5536 struct dwarf2_cu *cu;
d521ce57 5537 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5538 struct die_reader_specs reader;
d85a05f0 5539 struct die_info *comp_unit_die;
dee91e82 5540 int has_children;
d85a05f0 5541 struct attribute *attr;
365156ad 5542 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5543 struct signatured_type *sig_type = NULL;
4bdcc0c1 5544 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5545 /* Non-zero if CU currently points to a DWO file and we need to
5546 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5547 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5548 int rereading_dwo_cu = 0;
c906108c 5549
b4f54984 5550 if (dwarf_die_debug)
09406207
DE
5551 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5552 this_cu->is_debug_types ? "type" : "comp",
5553 this_cu->offset.sect_off);
5554
dee91e82
DE
5555 if (use_existing_cu)
5556 gdb_assert (keep);
23745b47 5557
a2ce51a0
DE
5558 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5559 file (instead of going through the stub), short-circuit all of this. */
5560 if (this_cu->reading_dwo_directly)
5561 {
5562 /* Narrow down the scope of possibilities to have to understand. */
5563 gdb_assert (this_cu->is_debug_types);
5564 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
5565 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5566 die_reader_func, data);
a2ce51a0
DE
5567 return;
5568 }
5569
dee91e82
DE
5570 cleanups = make_cleanup (null_cleanup, NULL);
5571
5572 /* This is cheap if the section is already read in. */
5573 dwarf2_read_section (objfile, section);
5574
5575 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5576
5577 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5578
5579 if (use_existing_cu && this_cu->cu != NULL)
5580 {
5581 cu = this_cu->cu;
42e7ad6c
DE
5582 /* If this CU is from a DWO file we need to start over, we need to
5583 refetch the attributes from the skeleton CU.
5584 This could be optimized by retrieving those attributes from when we
5585 were here the first time: the previous comp_unit_die was stored in
5586 comp_unit_obstack. But there's no data yet that we need this
5587 optimization. */
5588 if (cu->dwo_unit != NULL)
5589 rereading_dwo_cu = 1;
dee91e82
DE
5590 }
5591 else
5592 {
5593 /* If !use_existing_cu, this_cu->cu must be NULL. */
5594 gdb_assert (this_cu->cu == NULL);
8d749320 5595 cu = XNEW (struct dwarf2_cu);
dee91e82 5596 init_one_comp_unit (cu, this_cu);
dee91e82 5597 /* If an error occurs while loading, release our storage. */
365156ad 5598 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5599 }
dee91e82 5600
b0c7bfa9 5601 /* Get the header. */
42e7ad6c
DE
5602 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5603 {
5604 /* We already have the header, there's no need to read it in again. */
5605 info_ptr += cu->header.first_die_offset.cu_off;
5606 }
5607 else
5608 {
3019eac3 5609 if (this_cu->is_debug_types)
dee91e82 5610 {
43988095 5611 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4bdcc0c1 5612 abbrev_section, info_ptr,
43988095 5613 rcuh_kind::TYPE);
dee91e82 5614
42e7ad6c
DE
5615 /* Since per_cu is the first member of struct signatured_type,
5616 we can go from a pointer to one to a pointer to the other. */
5617 sig_type = (struct signatured_type *) this_cu;
43988095 5618 gdb_assert (sig_type->signature == cu->header.signature);
42e7ad6c 5619 gdb_assert (sig_type->type_offset_in_tu.cu_off
43988095 5620 == cu->header.type_offset_in_tu.cu_off);
dee91e82
DE
5621 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5622
42e7ad6c
DE
5623 /* LENGTH has not been set yet for type units if we're
5624 using .gdb_index. */
1ce1cefd 5625 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5626
5627 /* Establish the type offset that can be used to lookup the type. */
5628 sig_type->type_offset_in_section.sect_off =
5629 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
43988095
JK
5630
5631 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5632 }
5633 else
5634 {
4bdcc0c1
DE
5635 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5636 abbrev_section,
43988095
JK
5637 info_ptr,
5638 rcuh_kind::COMPILE);
dee91e82
DE
5639
5640 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5641 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 5642 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
5643 }
5644 }
10b3939b 5645
6caca83c 5646 /* Skip dummy compilation units. */
dee91e82 5647 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5648 || peek_abbrev_code (abfd, info_ptr) == 0)
5649 {
dee91e82 5650 do_cleanups (cleanups);
21b2bd31 5651 return;
6caca83c
CC
5652 }
5653
433df2d4
DE
5654 /* If we don't have them yet, read the abbrevs for this compilation unit.
5655 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5656 done. Note that it's important that if the CU had an abbrev table
5657 on entry we don't free it when we're done: Somewhere up the call stack
5658 it may be in use. */
f4dc4d17
DE
5659 if (abbrev_table != NULL)
5660 {
5661 gdb_assert (cu->abbrev_table == NULL);
5662 gdb_assert (cu->header.abbrev_offset.sect_off
5663 == abbrev_table->offset.sect_off);
5664 cu->abbrev_table = abbrev_table;
5665 }
5666 else if (cu->abbrev_table == NULL)
dee91e82 5667 {
4bdcc0c1 5668 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5669 make_cleanup (dwarf2_free_abbrev_table, cu);
5670 }
42e7ad6c
DE
5671 else if (rereading_dwo_cu)
5672 {
5673 dwarf2_free_abbrev_table (cu);
5674 dwarf2_read_abbrevs (cu, abbrev_section);
5675 }
af703f96 5676
dee91e82 5677 /* Read the top level CU/TU die. */
3019eac3 5678 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5679 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5680
b0c7bfa9
DE
5681 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5682 from the DWO file.
5683 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5684 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5685 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5686 if (attr)
5687 {
3019eac3 5688 struct dwo_unit *dwo_unit;
b0c7bfa9 5689 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5690
5691 if (has_children)
6a506a2d
DE
5692 {
5693 complaint (&symfile_complaints,
5694 _("compilation unit with DW_AT_GNU_dwo_name"
5695 " has children (offset 0x%x) [in module %s]"),
5696 this_cu->offset.sect_off, bfd_get_filename (abfd));
5697 }
b0c7bfa9 5698 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5699 if (dwo_unit != NULL)
3019eac3 5700 {
6a506a2d
DE
5701 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5702 abbrev_table != NULL,
a2ce51a0 5703 comp_unit_die, NULL,
6a506a2d
DE
5704 &reader, &info_ptr,
5705 &dwo_comp_unit_die, &has_children) == 0)
5706 {
5707 /* Dummy die. */
5708 do_cleanups (cleanups);
5709 return;
5710 }
5711 comp_unit_die = dwo_comp_unit_die;
5712 }
5713 else
5714 {
5715 /* Yikes, we couldn't find the rest of the DIE, we only have
5716 the stub. A complaint has already been logged. There's
5717 not much more we can do except pass on the stub DIE to
5718 die_reader_func. We don't want to throw an error on bad
5719 debug info. */
3019eac3
DE
5720 }
5721 }
5722
b0c7bfa9 5723 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5724 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5725
b0c7bfa9 5726 /* Done, clean up. */
365156ad 5727 if (free_cu_cleanup != NULL)
348e048f 5728 {
365156ad
TT
5729 if (keep)
5730 {
5731 /* We've successfully allocated this compilation unit. Let our
5732 caller clean it up when finished with it. */
5733 discard_cleanups (free_cu_cleanup);
dee91e82 5734
365156ad
TT
5735 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5736 So we have to manually free the abbrev table. */
5737 dwarf2_free_abbrev_table (cu);
dee91e82 5738
365156ad
TT
5739 /* Link this CU into read_in_chain. */
5740 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5741 dwarf2_per_objfile->read_in_chain = this_cu;
5742 }
5743 else
5744 do_cleanups (free_cu_cleanup);
348e048f 5745 }
365156ad
TT
5746
5747 do_cleanups (cleanups);
dee91e82
DE
5748}
5749
33e80786
DE
5750/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5751 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5752 to have already done the lookup to find the DWO file).
dee91e82
DE
5753
5754 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5755 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5756
5757 We fill in THIS_CU->length.
5758
5759 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5760 linker) then DIE_READER_FUNC will not get called.
5761
5762 THIS_CU->cu is always freed when done.
3019eac3
DE
5763 This is done in order to not leave THIS_CU->cu in a state where we have
5764 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5765
5766static void
5767init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 5768 struct dwo_file *dwo_file,
dee91e82
DE
5769 die_reader_func_ftype *die_reader_func,
5770 void *data)
5771{
5772 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5773 struct dwarf2_section_info *section = this_cu->section;
a32a8923 5774 bfd *abfd = get_section_bfd_owner (section);
33e80786 5775 struct dwarf2_section_info *abbrev_section;
dee91e82 5776 struct dwarf2_cu cu;
d521ce57 5777 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5778 struct die_reader_specs reader;
5779 struct cleanup *cleanups;
5780 struct die_info *comp_unit_die;
5781 int has_children;
5782
b4f54984 5783 if (dwarf_die_debug)
09406207
DE
5784 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5785 this_cu->is_debug_types ? "type" : "comp",
5786 this_cu->offset.sect_off);
5787
dee91e82
DE
5788 gdb_assert (this_cu->cu == NULL);
5789
33e80786
DE
5790 abbrev_section = (dwo_file != NULL
5791 ? &dwo_file->sections.abbrev
5792 : get_abbrev_section_for_cu (this_cu));
5793
dee91e82
DE
5794 /* This is cheap if the section is already read in. */
5795 dwarf2_read_section (objfile, section);
5796
5797 init_one_comp_unit (&cu, this_cu);
5798
5799 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5800
5801 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5802 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5803 abbrev_section, info_ptr,
43988095
JK
5804 (this_cu->is_debug_types
5805 ? rcuh_kind::TYPE
5806 : rcuh_kind::COMPILE));
dee91e82 5807
1ce1cefd 5808 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5809
5810 /* Skip dummy compilation units. */
5811 if (info_ptr >= begin_info_ptr + this_cu->length
5812 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5813 {
dee91e82 5814 do_cleanups (cleanups);
21b2bd31 5815 return;
93311388 5816 }
72bf9492 5817
dee91e82
DE
5818 dwarf2_read_abbrevs (&cu, abbrev_section);
5819 make_cleanup (dwarf2_free_abbrev_table, &cu);
5820
3019eac3 5821 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5822 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5823
5824 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5825
5826 do_cleanups (cleanups);
5827}
5828
3019eac3
DE
5829/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5830 does not lookup the specified DWO file.
5831 This cannot be used to read DWO files.
dee91e82
DE
5832
5833 THIS_CU->cu is always freed when done.
3019eac3
DE
5834 This is done in order to not leave THIS_CU->cu in a state where we have
5835 to care whether it refers to the "main" CU or the DWO CU.
5836 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5837
5838static void
5839init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5840 die_reader_func_ftype *die_reader_func,
5841 void *data)
5842{
33e80786 5843 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 5844}
0018ea6f
DE
5845\f
5846/* Type Unit Groups.
dee91e82 5847
0018ea6f
DE
5848 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5849 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5850 so that all types coming from the same compilation (.o file) are grouped
5851 together. A future step could be to put the types in the same symtab as
5852 the CU the types ultimately came from. */
ff013f42 5853
f4dc4d17
DE
5854static hashval_t
5855hash_type_unit_group (const void *item)
5856{
9a3c8263
SM
5857 const struct type_unit_group *tu_group
5858 = (const struct type_unit_group *) item;
f4dc4d17 5859
094b34ac 5860 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5861}
348e048f
DE
5862
5863static int
f4dc4d17 5864eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5865{
9a3c8263
SM
5866 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5867 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 5868
094b34ac 5869 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5870}
348e048f 5871
f4dc4d17
DE
5872/* Allocate a hash table for type unit groups. */
5873
5874static htab_t
5875allocate_type_unit_groups_table (void)
5876{
5877 return htab_create_alloc_ex (3,
5878 hash_type_unit_group,
5879 eq_type_unit_group,
5880 NULL,
5881 &dwarf2_per_objfile->objfile->objfile_obstack,
5882 hashtab_obstack_allocate,
5883 dummy_obstack_deallocate);
5884}
dee91e82 5885
f4dc4d17
DE
5886/* Type units that don't have DW_AT_stmt_list are grouped into their own
5887 partial symtabs. We combine several TUs per psymtab to not let the size
5888 of any one psymtab grow too big. */
5889#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5890#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5891
094b34ac 5892/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5893 Create the type_unit_group object used to hold one or more TUs. */
5894
5895static struct type_unit_group *
094b34ac 5896create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5897{
5898 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5899 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5900 struct type_unit_group *tu_group;
f4dc4d17
DE
5901
5902 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5903 struct type_unit_group);
094b34ac 5904 per_cu = &tu_group->per_cu;
f4dc4d17 5905 per_cu->objfile = objfile;
f4dc4d17 5906
094b34ac
DE
5907 if (dwarf2_per_objfile->using_index)
5908 {
5909 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5910 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5911 }
5912 else
5913 {
5914 unsigned int line_offset = line_offset_struct.sect_off;
5915 struct partial_symtab *pst;
5916 char *name;
5917
5918 /* Give the symtab a useful name for debug purposes. */
5919 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5920 name = xstrprintf ("<type_units_%d>",
5921 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5922 else
5923 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5924
5925 pst = create_partial_symtab (per_cu, name);
5926 pst->anonymous = 1;
f4dc4d17 5927
094b34ac
DE
5928 xfree (name);
5929 }
f4dc4d17 5930
094b34ac
DE
5931 tu_group->hash.dwo_unit = cu->dwo_unit;
5932 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5933
5934 return tu_group;
5935}
5936
094b34ac
DE
5937/* Look up the type_unit_group for type unit CU, and create it if necessary.
5938 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5939
5940static struct type_unit_group *
ff39bb5e 5941get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5942{
5943 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5944 struct type_unit_group *tu_group;
5945 void **slot;
5946 unsigned int line_offset;
5947 struct type_unit_group type_unit_group_for_lookup;
5948
5949 if (dwarf2_per_objfile->type_unit_groups == NULL)
5950 {
5951 dwarf2_per_objfile->type_unit_groups =
5952 allocate_type_unit_groups_table ();
5953 }
5954
5955 /* Do we need to create a new group, or can we use an existing one? */
5956
5957 if (stmt_list)
5958 {
5959 line_offset = DW_UNSND (stmt_list);
5960 ++tu_stats->nr_symtab_sharers;
5961 }
5962 else
5963 {
5964 /* Ugh, no stmt_list. Rare, but we have to handle it.
5965 We can do various things here like create one group per TU or
5966 spread them over multiple groups to split up the expansion work.
5967 To avoid worst case scenarios (too many groups or too large groups)
5968 we, umm, group them in bunches. */
5969 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5970 | (tu_stats->nr_stmt_less_type_units
5971 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5972 ++tu_stats->nr_stmt_less_type_units;
5973 }
5974
094b34ac
DE
5975 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5976 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5977 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5978 &type_unit_group_for_lookup, INSERT);
5979 if (*slot != NULL)
5980 {
9a3c8263 5981 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
5982 gdb_assert (tu_group != NULL);
5983 }
5984 else
5985 {
5986 sect_offset line_offset_struct;
5987
5988 line_offset_struct.sect_off = line_offset;
094b34ac 5989 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5990 *slot = tu_group;
5991 ++tu_stats->nr_symtabs;
5992 }
5993
5994 return tu_group;
5995}
0018ea6f
DE
5996\f
5997/* Partial symbol tables. */
5998
5999/* Create a psymtab named NAME and assign it to PER_CU.
6000
6001 The caller must fill in the following details:
6002 dirname, textlow, texthigh. */
6003
6004static struct partial_symtab *
6005create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6006{
6007 struct objfile *objfile = per_cu->objfile;
6008 struct partial_symtab *pst;
6009
18a94d75 6010 pst = start_psymtab_common (objfile, name, 0,
0018ea6f
DE
6011 objfile->global_psymbols.next,
6012 objfile->static_psymbols.next);
6013
6014 pst->psymtabs_addrmap_supported = 1;
6015
6016 /* This is the glue that links PST into GDB's symbol API. */
6017 pst->read_symtab_private = per_cu;
6018 pst->read_symtab = dwarf2_read_symtab;
6019 per_cu->v.psymtab = pst;
6020
6021 return pst;
6022}
6023
b93601f3
TT
6024/* The DATA object passed to process_psymtab_comp_unit_reader has this
6025 type. */
6026
6027struct process_psymtab_comp_unit_data
6028{
6029 /* True if we are reading a DW_TAG_partial_unit. */
6030
6031 int want_partial_unit;
6032
6033 /* The "pretend" language that is used if the CU doesn't declare a
6034 language. */
6035
6036 enum language pretend_language;
6037};
6038
0018ea6f
DE
6039/* die_reader_func for process_psymtab_comp_unit. */
6040
6041static void
6042process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6043 const gdb_byte *info_ptr,
0018ea6f
DE
6044 struct die_info *comp_unit_die,
6045 int has_children,
6046 void *data)
6047{
6048 struct dwarf2_cu *cu = reader->cu;
6049 struct objfile *objfile = cu->objfile;
3e29f34a 6050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 6051 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6052 CORE_ADDR baseaddr;
6053 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6054 struct partial_symtab *pst;
3a2b436a 6055 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6056 const char *filename;
9a3c8263
SM
6057 struct process_psymtab_comp_unit_data *info
6058 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 6059
b93601f3 6060 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
6061 return;
6062
6063 gdb_assert (! per_cu->is_debug_types);
6064
b93601f3 6065 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
6066
6067 cu->list_in_scope = &file_symbols;
6068
6069 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
6070 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6071 if (filename == NULL)
0018ea6f 6072 filename = "";
0018ea6f
DE
6073
6074 pst = create_partial_symtab (per_cu, filename);
6075
6076 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6077 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
6078
6079 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6080
6081 dwarf2_find_base_address (comp_unit_die, cu);
6082
6083 /* Possibly set the default values of LOWPC and HIGHPC from
6084 `DW_AT_ranges'. */
3a2b436a
JK
6085 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6086 &best_highpc, cu, pst);
6087 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
6088 /* Store the contiguous range if it is not empty; it can be empty for
6089 CUs with no code. */
6090 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
6091 gdbarch_adjust_dwarf2_addr (gdbarch,
6092 best_lowpc + baseaddr),
6093 gdbarch_adjust_dwarf2_addr (gdbarch,
6094 best_highpc + baseaddr) - 1,
6095 pst);
0018ea6f
DE
6096
6097 /* Check if comp unit has_children.
6098 If so, read the rest of the partial symbols from this comp unit.
6099 If not, there's no more debug_info for this comp unit. */
6100 if (has_children)
6101 {
6102 struct partial_die_info *first_die;
6103 CORE_ADDR lowpc, highpc;
6104
6105 lowpc = ((CORE_ADDR) -1);
6106 highpc = ((CORE_ADDR) 0);
6107
6108 first_die = load_partial_dies (reader, info_ptr, 1);
6109
6110 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6111 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6112
6113 /* If we didn't find a lowpc, set it to highpc to avoid
6114 complaints from `maint check'. */
6115 if (lowpc == ((CORE_ADDR) -1))
6116 lowpc = highpc;
6117
6118 /* If the compilation unit didn't have an explicit address range,
6119 then use the information extracted from its child dies. */
e385593e 6120 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6121 {
6122 best_lowpc = lowpc;
6123 best_highpc = highpc;
6124 }
6125 }
3e29f34a
MR
6126 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6127 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 6128
8763cede 6129 end_psymtab_common (objfile, pst);
0018ea6f
DE
6130
6131 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6132 {
6133 int i;
6134 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6135 struct dwarf2_per_cu_data *iter;
6136
6137 /* Fill in 'dependencies' here; we fill in 'users' in a
6138 post-pass. */
6139 pst->number_of_dependencies = len;
8d749320
SM
6140 pst->dependencies =
6141 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
6142 for (i = 0;
6143 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6144 i, iter);
6145 ++i)
6146 pst->dependencies[i] = iter->v.psymtab;
6147
6148 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6149 }
6150
6151 /* Get the list of files included in the current compilation unit,
6152 and build a psymtab for each of them. */
6153 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6154
b4f54984 6155 if (dwarf_read_debug)
0018ea6f
DE
6156 {
6157 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6158
6159 fprintf_unfiltered (gdb_stdlog,
6160 "Psymtab for %s unit @0x%x: %s - %s"
6161 ", %d global, %d static syms\n",
6162 per_cu->is_debug_types ? "type" : "comp",
6163 per_cu->offset.sect_off,
6164 paddress (gdbarch, pst->textlow),
6165 paddress (gdbarch, pst->texthigh),
6166 pst->n_global_syms, pst->n_static_syms);
6167 }
6168}
6169
6170/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6171 Process compilation unit THIS_CU for a psymtab. */
6172
6173static void
6174process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
6175 int want_partial_unit,
6176 enum language pretend_language)
0018ea6f 6177{
b93601f3
TT
6178 struct process_psymtab_comp_unit_data info;
6179
0018ea6f
DE
6180 /* If this compilation unit was already read in, free the
6181 cached copy in order to read it in again. This is
6182 necessary because we skipped some symbols when we first
6183 read in the compilation unit (see load_partial_dies).
6184 This problem could be avoided, but the benefit is unclear. */
6185 if (this_cu->cu != NULL)
6186 free_one_cached_comp_unit (this_cu);
6187
6188 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
6189 info.want_partial_unit = want_partial_unit;
6190 info.pretend_language = pretend_language;
0018ea6f
DE
6191 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6192 process_psymtab_comp_unit_reader,
b93601f3 6193 &info);
0018ea6f
DE
6194
6195 /* Age out any secondary CUs. */
6196 age_cached_comp_units ();
6197}
f4dc4d17
DE
6198
6199/* Reader function for build_type_psymtabs. */
6200
6201static void
6202build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 6203 const gdb_byte *info_ptr,
f4dc4d17
DE
6204 struct die_info *type_unit_die,
6205 int has_children,
6206 void *data)
6207{
6208 struct objfile *objfile = dwarf2_per_objfile->objfile;
6209 struct dwarf2_cu *cu = reader->cu;
6210 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 6211 struct signatured_type *sig_type;
f4dc4d17
DE
6212 struct type_unit_group *tu_group;
6213 struct attribute *attr;
6214 struct partial_die_info *first_die;
6215 CORE_ADDR lowpc, highpc;
6216 struct partial_symtab *pst;
6217
6218 gdb_assert (data == NULL);
0186c6a7
DE
6219 gdb_assert (per_cu->is_debug_types);
6220 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
6221
6222 if (! has_children)
6223 return;
6224
6225 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 6226 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 6227
0186c6a7 6228 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
6229
6230 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6231 cu->list_in_scope = &file_symbols;
6232 pst = create_partial_symtab (per_cu, "");
6233 pst->anonymous = 1;
6234
6235 first_die = load_partial_dies (reader, info_ptr, 1);
6236
6237 lowpc = (CORE_ADDR) -1;
6238 highpc = (CORE_ADDR) 0;
6239 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6240
8763cede 6241 end_psymtab_common (objfile, pst);
f4dc4d17
DE
6242}
6243
73051182
DE
6244/* Struct used to sort TUs by their abbreviation table offset. */
6245
6246struct tu_abbrev_offset
6247{
6248 struct signatured_type *sig_type;
6249 sect_offset abbrev_offset;
6250};
6251
6252/* Helper routine for build_type_psymtabs_1, passed to qsort. */
6253
6254static int
6255sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6256{
9a3c8263
SM
6257 const struct tu_abbrev_offset * const *a
6258 = (const struct tu_abbrev_offset * const*) ap;
6259 const struct tu_abbrev_offset * const *b
6260 = (const struct tu_abbrev_offset * const*) bp;
73051182
DE
6261 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6262 unsigned int boff = (*b)->abbrev_offset.sect_off;
6263
6264 return (aoff > boff) - (aoff < boff);
6265}
6266
6267/* Efficiently read all the type units.
6268 This does the bulk of the work for build_type_psymtabs.
6269
6270 The efficiency is because we sort TUs by the abbrev table they use and
6271 only read each abbrev table once. In one program there are 200K TUs
6272 sharing 8K abbrev tables.
6273
6274 The main purpose of this function is to support building the
6275 dwarf2_per_objfile->type_unit_groups table.
6276 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6277 can collapse the search space by grouping them by stmt_list.
6278 The savings can be significant, in the same program from above the 200K TUs
6279 share 8K stmt_list tables.
6280
6281 FUNC is expected to call get_type_unit_group, which will create the
6282 struct type_unit_group if necessary and add it to
6283 dwarf2_per_objfile->type_unit_groups. */
6284
6285static void
6286build_type_psymtabs_1 (void)
6287{
73051182
DE
6288 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6289 struct cleanup *cleanups;
6290 struct abbrev_table *abbrev_table;
6291 sect_offset abbrev_offset;
6292 struct tu_abbrev_offset *sorted_by_abbrev;
73051182
DE
6293 int i;
6294
6295 /* It's up to the caller to not call us multiple times. */
6296 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6297
6298 if (dwarf2_per_objfile->n_type_units == 0)
6299 return;
6300
6301 /* TUs typically share abbrev tables, and there can be way more TUs than
6302 abbrev tables. Sort by abbrev table to reduce the number of times we
6303 read each abbrev table in.
6304 Alternatives are to punt or to maintain a cache of abbrev tables.
6305 This is simpler and efficient enough for now.
6306
6307 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6308 symtab to use). Typically TUs with the same abbrev offset have the same
6309 stmt_list value too so in practice this should work well.
6310
6311 The basic algorithm here is:
6312
6313 sort TUs by abbrev table
6314 for each TU with same abbrev table:
6315 read abbrev table if first user
6316 read TU top level DIE
6317 [IWBN if DWO skeletons had DW_AT_stmt_list]
6318 call FUNC */
6319
b4f54984 6320 if (dwarf_read_debug)
73051182
DE
6321 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6322
6323 /* Sort in a separate table to maintain the order of all_type_units
6324 for .gdb_index: TU indices directly index all_type_units. */
6325 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6326 dwarf2_per_objfile->n_type_units);
6327 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6328 {
6329 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6330
6331 sorted_by_abbrev[i].sig_type = sig_type;
6332 sorted_by_abbrev[i].abbrev_offset =
6333 read_abbrev_offset (sig_type->per_cu.section,
6334 sig_type->per_cu.offset);
6335 }
6336 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6337 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6338 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6339
6340 abbrev_offset.sect_off = ~(unsigned) 0;
6341 abbrev_table = NULL;
6342 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6343
6344 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6345 {
6346 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6347
6348 /* Switch to the next abbrev table if necessary. */
6349 if (abbrev_table == NULL
6350 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6351 {
6352 if (abbrev_table != NULL)
6353 {
6354 abbrev_table_free (abbrev_table);
6355 /* Reset to NULL in case abbrev_table_read_table throws
6356 an error: abbrev_table_free_cleanup will get called. */
6357 abbrev_table = NULL;
6358 }
6359 abbrev_offset = tu->abbrev_offset;
6360 abbrev_table =
6361 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6362 abbrev_offset);
6363 ++tu_stats->nr_uniq_abbrev_tables;
6364 }
6365
6366 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6367 build_type_psymtabs_reader, NULL);
6368 }
6369
73051182 6370 do_cleanups (cleanups);
6aa5f3a6 6371}
73051182 6372
6aa5f3a6
DE
6373/* Print collected type unit statistics. */
6374
6375static void
6376print_tu_stats (void)
6377{
6378 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6379
6380 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6381 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6382 dwarf2_per_objfile->n_type_units);
6383 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6384 tu_stats->nr_uniq_abbrev_tables);
6385 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6386 tu_stats->nr_symtabs);
6387 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6388 tu_stats->nr_symtab_sharers);
6389 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6390 tu_stats->nr_stmt_less_type_units);
6391 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6392 tu_stats->nr_all_type_units_reallocs);
73051182
DE
6393}
6394
f4dc4d17
DE
6395/* Traversal function for build_type_psymtabs. */
6396
6397static int
6398build_type_psymtab_dependencies (void **slot, void *info)
6399{
6400 struct objfile *objfile = dwarf2_per_objfile->objfile;
6401 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 6402 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 6403 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
6404 int len = VEC_length (sig_type_ptr, tu_group->tus);
6405 struct signatured_type *iter;
f4dc4d17
DE
6406 int i;
6407
6408 gdb_assert (len > 0);
0186c6a7 6409 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
6410
6411 pst->number_of_dependencies = len;
8d749320
SM
6412 pst->dependencies =
6413 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 6414 for (i = 0;
0186c6a7 6415 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
6416 ++i)
6417 {
0186c6a7
DE
6418 gdb_assert (iter->per_cu.is_debug_types);
6419 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 6420 iter->type_unit_group = tu_group;
f4dc4d17
DE
6421 }
6422
0186c6a7 6423 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
6424
6425 return 1;
6426}
6427
6428/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6429 Build partial symbol tables for the .debug_types comp-units. */
6430
6431static void
6432build_type_psymtabs (struct objfile *objfile)
6433{
0e50663e 6434 if (! create_all_type_units (objfile))
348e048f
DE
6435 return;
6436
73051182 6437 build_type_psymtabs_1 ();
6aa5f3a6 6438}
f4dc4d17 6439
6aa5f3a6
DE
6440/* Traversal function for process_skeletonless_type_unit.
6441 Read a TU in a DWO file and build partial symbols for it. */
6442
6443static int
6444process_skeletonless_type_unit (void **slot, void *info)
6445{
6446 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9a3c8263 6447 struct objfile *objfile = (struct objfile *) info;
6aa5f3a6
DE
6448 struct signatured_type find_entry, *entry;
6449
6450 /* If this TU doesn't exist in the global table, add it and read it in. */
6451
6452 if (dwarf2_per_objfile->signatured_types == NULL)
6453 {
6454 dwarf2_per_objfile->signatured_types
6455 = allocate_signatured_type_table (objfile);
6456 }
6457
6458 find_entry.signature = dwo_unit->signature;
6459 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6460 INSERT);
6461 /* If we've already seen this type there's nothing to do. What's happening
6462 is we're doing our own version of comdat-folding here. */
6463 if (*slot != NULL)
6464 return 1;
6465
6466 /* This does the job that create_all_type_units would have done for
6467 this TU. */
6468 entry = add_type_unit (dwo_unit->signature, slot);
6469 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6470 *slot = entry;
6471
6472 /* This does the job that build_type_psymtabs_1 would have done. */
6473 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6474 build_type_psymtabs_reader, NULL);
6475
6476 return 1;
6477}
6478
6479/* Traversal function for process_skeletonless_type_units. */
6480
6481static int
6482process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6483{
6484 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6485
6486 if (dwo_file->tus != NULL)
6487 {
6488 htab_traverse_noresize (dwo_file->tus,
6489 process_skeletonless_type_unit, info);
6490 }
6491
6492 return 1;
6493}
6494
6495/* Scan all TUs of DWO files, verifying we've processed them.
6496 This is needed in case a TU was emitted without its skeleton.
6497 Note: This can't be done until we know what all the DWO files are. */
6498
6499static void
6500process_skeletonless_type_units (struct objfile *objfile)
6501{
6502 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6503 if (get_dwp_file () == NULL
6504 && dwarf2_per_objfile->dwo_files != NULL)
6505 {
6506 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6507 process_dwo_file_for_skeletonless_type_units,
6508 objfile);
6509 }
348e048f
DE
6510}
6511
60606b2c
TT
6512/* A cleanup function that clears objfile's psymtabs_addrmap field. */
6513
6514static void
6515psymtabs_addrmap_cleanup (void *o)
6516{
9a3c8263 6517 struct objfile *objfile = (struct objfile *) o;
ec61707d 6518
60606b2c
TT
6519 objfile->psymtabs_addrmap = NULL;
6520}
6521
95554aad
TT
6522/* Compute the 'user' field for each psymtab in OBJFILE. */
6523
6524static void
6525set_partial_user (struct objfile *objfile)
6526{
6527 int i;
6528
6529 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6530 {
8832e7e3 6531 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
95554aad
TT
6532 struct partial_symtab *pst = per_cu->v.psymtab;
6533 int j;
6534
36586728
TT
6535 if (pst == NULL)
6536 continue;
6537
95554aad
TT
6538 for (j = 0; j < pst->number_of_dependencies; ++j)
6539 {
6540 /* Set the 'user' field only if it is not already set. */
6541 if (pst->dependencies[j]->user == NULL)
6542 pst->dependencies[j]->user = pst;
6543 }
6544 }
6545}
6546
93311388
DE
6547/* Build the partial symbol table by doing a quick pass through the
6548 .debug_info and .debug_abbrev sections. */
72bf9492 6549
93311388 6550static void
c67a9c90 6551dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 6552{
60606b2c
TT
6553 struct cleanup *back_to, *addrmap_cleanup;
6554 struct obstack temp_obstack;
21b2bd31 6555 int i;
93311388 6556
b4f54984 6557 if (dwarf_read_debug)
45cfd468
DE
6558 {
6559 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 6560 objfile_name (objfile));
45cfd468
DE
6561 }
6562
98bfdba5
PA
6563 dwarf2_per_objfile->reading_partial_symbols = 1;
6564
be391dca 6565 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 6566
93311388
DE
6567 /* Any cached compilation units will be linked by the per-objfile
6568 read_in_chain. Make sure to free them when we're done. */
6569 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6570
348e048f
DE
6571 build_type_psymtabs (objfile);
6572
93311388 6573 create_all_comp_units (objfile);
c906108c 6574
60606b2c
TT
6575 /* Create a temporary address map on a temporary obstack. We later
6576 copy this to the final obstack. */
6577 obstack_init (&temp_obstack);
6578 make_cleanup_obstack_free (&temp_obstack);
6579 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6580 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6581
21b2bd31 6582 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6583 {
8832e7e3 6584 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
aaa75496 6585
b93601f3 6586 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6587 }
ff013f42 6588
6aa5f3a6
DE
6589 /* This has to wait until we read the CUs, we need the list of DWOs. */
6590 process_skeletonless_type_units (objfile);
6591
6592 /* Now that all TUs have been processed we can fill in the dependencies. */
6593 if (dwarf2_per_objfile->type_unit_groups != NULL)
6594 {
6595 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6596 build_type_psymtab_dependencies, NULL);
6597 }
6598
b4f54984 6599 if (dwarf_read_debug)
6aa5f3a6
DE
6600 print_tu_stats ();
6601
95554aad
TT
6602 set_partial_user (objfile);
6603
ff013f42
JK
6604 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6605 &objfile->objfile_obstack);
60606b2c 6606 discard_cleanups (addrmap_cleanup);
ff013f42 6607
ae038cb0 6608 do_cleanups (back_to);
45cfd468 6609
b4f54984 6610 if (dwarf_read_debug)
45cfd468 6611 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 6612 objfile_name (objfile));
ae038cb0
DJ
6613}
6614
3019eac3 6615/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6616
6617static void
dee91e82 6618load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6619 const gdb_byte *info_ptr,
dee91e82
DE
6620 struct die_info *comp_unit_die,
6621 int has_children,
6622 void *data)
ae038cb0 6623{
dee91e82 6624 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6625
95554aad 6626 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6627
ae038cb0
DJ
6628 /* Check if comp unit has_children.
6629 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6630 If not, there's no more debug_info for this comp unit. */
d85a05f0 6631 if (has_children)
dee91e82
DE
6632 load_partial_dies (reader, info_ptr, 0);
6633}
98bfdba5 6634
dee91e82
DE
6635/* Load the partial DIEs for a secondary CU into memory.
6636 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6637
dee91e82
DE
6638static void
6639load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6640{
f4dc4d17
DE
6641 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6642 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6643}
6644
ae038cb0 6645static void
36586728
TT
6646read_comp_units_from_section (struct objfile *objfile,
6647 struct dwarf2_section_info *section,
6648 unsigned int is_dwz,
6649 int *n_allocated,
6650 int *n_comp_units,
6651 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6652{
d521ce57 6653 const gdb_byte *info_ptr;
a32a8923 6654 bfd *abfd = get_section_bfd_owner (section);
be391dca 6655
b4f54984 6656 if (dwarf_read_debug)
bf6af496 6657 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
6658 get_section_name (section),
6659 get_section_file_name (section));
bf6af496 6660
36586728 6661 dwarf2_read_section (objfile, section);
ae038cb0 6662
36586728 6663 info_ptr = section->buffer;
6e70227d 6664
36586728 6665 while (info_ptr < section->buffer + section->size)
ae038cb0 6666 {
c764a876 6667 unsigned int length, initial_length_size;
ae038cb0 6668 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6669 sect_offset offset;
ae038cb0 6670
36586728 6671 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6672
6673 /* Read just enough information to find out where the next
6674 compilation unit is. */
36586728 6675 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6676
6677 /* Save the compilation unit for later lookup. */
8d749320 6678 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
ae038cb0
DJ
6679 memset (this_cu, 0, sizeof (*this_cu));
6680 this_cu->offset = offset;
c764a876 6681 this_cu->length = length + initial_length_size;
36586728 6682 this_cu->is_dwz = is_dwz;
9291a0cd 6683 this_cu->objfile = objfile;
8a0459fd 6684 this_cu->section = section;
ae038cb0 6685
36586728 6686 if (*n_comp_units == *n_allocated)
ae038cb0 6687 {
36586728 6688 *n_allocated *= 2;
224c3ddb
SM
6689 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6690 *all_comp_units, *n_allocated);
ae038cb0 6691 }
36586728
TT
6692 (*all_comp_units)[*n_comp_units] = this_cu;
6693 ++*n_comp_units;
ae038cb0
DJ
6694
6695 info_ptr = info_ptr + this_cu->length;
6696 }
36586728
TT
6697}
6698
6699/* Create a list of all compilation units in OBJFILE.
6700 This is only done for -readnow and building partial symtabs. */
6701
6702static void
6703create_all_comp_units (struct objfile *objfile)
6704{
6705 int n_allocated;
6706 int n_comp_units;
6707 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6708 struct dwz_file *dwz;
36586728
TT
6709
6710 n_comp_units = 0;
6711 n_allocated = 10;
8d749320 6712 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
36586728
TT
6713
6714 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6715 &n_allocated, &n_comp_units, &all_comp_units);
6716
4db1a1dc
TT
6717 dwz = dwarf2_get_dwz_file ();
6718 if (dwz != NULL)
6719 read_comp_units_from_section (objfile, &dwz->info, 1,
6720 &n_allocated, &n_comp_units,
6721 &all_comp_units);
ae038cb0 6722
8d749320
SM
6723 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6724 struct dwarf2_per_cu_data *,
6725 n_comp_units);
ae038cb0
DJ
6726 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6727 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6728 xfree (all_comp_units);
6729 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6730}
6731
5734ee8b 6732/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 6733 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 6734 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
6735 DW_AT_ranges). See the comments of add_partial_subprogram on how
6736 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 6737
72bf9492
DJ
6738static void
6739scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
6740 CORE_ADDR *highpc, int set_addrmap,
6741 struct dwarf2_cu *cu)
c906108c 6742{
72bf9492 6743 struct partial_die_info *pdi;
c906108c 6744
91c24f0a
DC
6745 /* Now, march along the PDI's, descending into ones which have
6746 interesting children but skipping the children of the other ones,
6747 until we reach the end of the compilation unit. */
c906108c 6748
72bf9492 6749 pdi = first_die;
91c24f0a 6750
72bf9492
DJ
6751 while (pdi != NULL)
6752 {
6753 fixup_partial_die (pdi, cu);
c906108c 6754
f55ee35c 6755 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6756 children, so we need to look at them. Ditto for anonymous
6757 enums. */
933c6fe4 6758
72bf9492 6759 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6760 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6761 || pdi->tag == DW_TAG_imported_unit)
c906108c 6762 {
72bf9492 6763 switch (pdi->tag)
c906108c
SS
6764 {
6765 case DW_TAG_subprogram:
cdc07690 6766 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 6767 break;
72929c62 6768 case DW_TAG_constant:
c906108c
SS
6769 case DW_TAG_variable:
6770 case DW_TAG_typedef:
91c24f0a 6771 case DW_TAG_union_type:
72bf9492 6772 if (!pdi->is_declaration)
63d06c5c 6773 {
72bf9492 6774 add_partial_symbol (pdi, cu);
63d06c5c
DC
6775 }
6776 break;
c906108c 6777 case DW_TAG_class_type:
680b30c7 6778 case DW_TAG_interface_type:
c906108c 6779 case DW_TAG_structure_type:
72bf9492 6780 if (!pdi->is_declaration)
c906108c 6781 {
72bf9492 6782 add_partial_symbol (pdi, cu);
c906108c 6783 }
e98c9e7c
TT
6784 if (cu->language == language_rust && pdi->has_children)
6785 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6786 set_addrmap, cu);
c906108c 6787 break;
91c24f0a 6788 case DW_TAG_enumeration_type:
72bf9492
DJ
6789 if (!pdi->is_declaration)
6790 add_partial_enumeration (pdi, cu);
c906108c
SS
6791 break;
6792 case DW_TAG_base_type:
a02abb62 6793 case DW_TAG_subrange_type:
c906108c 6794 /* File scope base type definitions are added to the partial
c5aa993b 6795 symbol table. */
72bf9492 6796 add_partial_symbol (pdi, cu);
c906108c 6797 break;
d9fa45fe 6798 case DW_TAG_namespace:
cdc07690 6799 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 6800 break;
5d7cb8df 6801 case DW_TAG_module:
cdc07690 6802 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 6803 break;
95554aad
TT
6804 case DW_TAG_imported_unit:
6805 {
6806 struct dwarf2_per_cu_data *per_cu;
6807
f4dc4d17
DE
6808 /* For now we don't handle imported units in type units. */
6809 if (cu->per_cu->is_debug_types)
6810 {
6811 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6812 " supported in type units [in module %s]"),
4262abfb 6813 objfile_name (cu->objfile));
f4dc4d17
DE
6814 }
6815
95554aad 6816 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6817 pdi->is_dwz,
95554aad
TT
6818 cu->objfile);
6819
6820 /* Go read the partial unit, if needed. */
6821 if (per_cu->v.psymtab == NULL)
b93601f3 6822 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6823
f4dc4d17 6824 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6825 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6826 }
6827 break;
74921315
KS
6828 case DW_TAG_imported_declaration:
6829 add_partial_symbol (pdi, cu);
6830 break;
c906108c
SS
6831 default:
6832 break;
6833 }
6834 }
6835
72bf9492
DJ
6836 /* If the die has a sibling, skip to the sibling. */
6837
6838 pdi = pdi->die_sibling;
6839 }
6840}
6841
6842/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6843
72bf9492 6844 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 6845 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
6846 Enumerators are an exception; they use the scope of their parent
6847 enumeration type, i.e. the name of the enumeration type is not
6848 prepended to the enumerator.
91c24f0a 6849
72bf9492
DJ
6850 There are two complexities. One is DW_AT_specification; in this
6851 case "parent" means the parent of the target of the specification,
6852 instead of the direct parent of the DIE. The other is compilers
6853 which do not emit DW_TAG_namespace; in this case we try to guess
6854 the fully qualified name of structure types from their members'
6855 linkage names. This must be done using the DIE's children rather
6856 than the children of any DW_AT_specification target. We only need
6857 to do this for structures at the top level, i.e. if the target of
6858 any DW_AT_specification (if any; otherwise the DIE itself) does not
6859 have a parent. */
6860
6861/* Compute the scope prefix associated with PDI's parent, in
6862 compilation unit CU. The result will be allocated on CU's
6863 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6864 field. NULL is returned if no prefix is necessary. */
15d034d0 6865static const char *
72bf9492
DJ
6866partial_die_parent_scope (struct partial_die_info *pdi,
6867 struct dwarf2_cu *cu)
6868{
15d034d0 6869 const char *grandparent_scope;
72bf9492 6870 struct partial_die_info *parent, *real_pdi;
91c24f0a 6871
72bf9492
DJ
6872 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6873 then this means the parent of the specification DIE. */
6874
6875 real_pdi = pdi;
72bf9492 6876 while (real_pdi->has_specification)
36586728
TT
6877 real_pdi = find_partial_die (real_pdi->spec_offset,
6878 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6879
6880 parent = real_pdi->die_parent;
6881 if (parent == NULL)
6882 return NULL;
6883
6884 if (parent->scope_set)
6885 return parent->scope;
6886
6887 fixup_partial_die (parent, cu);
6888
10b3939b 6889 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6890
acebe513
UW
6891 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6892 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6893 Work around this problem here. */
6894 if (cu->language == language_cplus
6e70227d 6895 && parent->tag == DW_TAG_namespace
acebe513
UW
6896 && strcmp (parent->name, "::") == 0
6897 && grandparent_scope == NULL)
6898 {
6899 parent->scope = NULL;
6900 parent->scope_set = 1;
6901 return NULL;
6902 }
6903
9c6c53f7
SA
6904 if (pdi->tag == DW_TAG_enumerator)
6905 /* Enumerators should not get the name of the enumeration as a prefix. */
6906 parent->scope = grandparent_scope;
6907 else if (parent->tag == DW_TAG_namespace
f55ee35c 6908 || parent->tag == DW_TAG_module
72bf9492
DJ
6909 || parent->tag == DW_TAG_structure_type
6910 || parent->tag == DW_TAG_class_type
680b30c7 6911 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6912 || parent->tag == DW_TAG_union_type
6913 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6914 {
6915 if (grandparent_scope == NULL)
6916 parent->scope = parent->name;
6917 else
3e43a32a
MS
6918 parent->scope = typename_concat (&cu->comp_unit_obstack,
6919 grandparent_scope,
f55ee35c 6920 parent->name, 0, cu);
72bf9492 6921 }
72bf9492
DJ
6922 else
6923 {
6924 /* FIXME drow/2004-04-01: What should we be doing with
6925 function-local names? For partial symbols, we should probably be
6926 ignoring them. */
6927 complaint (&symfile_complaints,
e2e0b3e5 6928 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6929 parent->tag, pdi->offset.sect_off);
72bf9492 6930 parent->scope = grandparent_scope;
c906108c
SS
6931 }
6932
72bf9492
DJ
6933 parent->scope_set = 1;
6934 return parent->scope;
6935}
6936
6937/* Return the fully scoped name associated with PDI, from compilation unit
6938 CU. The result will be allocated with malloc. */
4568ecf9 6939
72bf9492
DJ
6940static char *
6941partial_die_full_name (struct partial_die_info *pdi,
6942 struct dwarf2_cu *cu)
6943{
15d034d0 6944 const char *parent_scope;
72bf9492 6945
98bfdba5
PA
6946 /* If this is a template instantiation, we can not work out the
6947 template arguments from partial DIEs. So, unfortunately, we have
6948 to go through the full DIEs. At least any work we do building
6949 types here will be reused if full symbols are loaded later. */
6950 if (pdi->has_template_arguments)
6951 {
6952 fixup_partial_die (pdi, cu);
6953
6954 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6955 {
6956 struct die_info *die;
6957 struct attribute attr;
6958 struct dwarf2_cu *ref_cu = cu;
6959
b64f50a1 6960 /* DW_FORM_ref_addr is using section offset. */
b4069958 6961 attr.name = (enum dwarf_attribute) 0;
98bfdba5 6962 attr.form = DW_FORM_ref_addr;
4568ecf9 6963 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6964 die = follow_die_ref (NULL, &attr, &ref_cu);
6965
6966 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6967 }
6968 }
6969
72bf9492
DJ
6970 parent_scope = partial_die_parent_scope (pdi, cu);
6971 if (parent_scope == NULL)
6972 return NULL;
6973 else
f55ee35c 6974 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6975}
6976
6977static void
72bf9492 6978add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6979{
e7c27a73 6980 struct objfile *objfile = cu->objfile;
3e29f34a 6981 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 6982 CORE_ADDR addr = 0;
15d034d0 6983 const char *actual_name = NULL;
e142c38c 6984 CORE_ADDR baseaddr;
15d034d0 6985 char *built_actual_name;
e142c38c
DJ
6986
6987 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6988
15d034d0
TT
6989 built_actual_name = partial_die_full_name (pdi, cu);
6990 if (built_actual_name != NULL)
6991 actual_name = built_actual_name;
63d06c5c 6992
72bf9492
DJ
6993 if (actual_name == NULL)
6994 actual_name = pdi->name;
6995
c906108c
SS
6996 switch (pdi->tag)
6997 {
6998 case DW_TAG_subprogram:
3e29f34a 6999 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 7000 if (pdi->is_external || cu->language == language_ada)
c906108c 7001 {
2cfa0c8d
JB
7002 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7003 of the global scope. But in Ada, we want to be able to access
7004 nested procedures globally. So all Ada subprograms are stored
7005 in the global scope. */
f47fb265 7006 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7007 built_actual_name != NULL,
f47fb265
MS
7008 VAR_DOMAIN, LOC_BLOCK,
7009 &objfile->global_psymbols,
1762568f 7010 addr, cu->language, objfile);
c906108c
SS
7011 }
7012 else
7013 {
f47fb265 7014 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7015 built_actual_name != NULL,
f47fb265
MS
7016 VAR_DOMAIN, LOC_BLOCK,
7017 &objfile->static_psymbols,
1762568f 7018 addr, cu->language, objfile);
c906108c 7019 }
0c1b455e
TT
7020
7021 if (pdi->main_subprogram && actual_name != NULL)
7022 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7023 break;
72929c62
JB
7024 case DW_TAG_constant:
7025 {
7026 struct psymbol_allocation_list *list;
7027
7028 if (pdi->is_external)
7029 list = &objfile->global_psymbols;
7030 else
7031 list = &objfile->static_psymbols;
f47fb265 7032 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7033 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 7034 list, 0, cu->language, objfile);
72929c62
JB
7035 }
7036 break;
c906108c 7037 case DW_TAG_variable:
95554aad
TT
7038 if (pdi->d.locdesc)
7039 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7040
95554aad 7041 if (pdi->d.locdesc
caac4577
JG
7042 && addr == 0
7043 && !dwarf2_per_objfile->has_section_at_zero)
7044 {
7045 /* A global or static variable may also have been stripped
7046 out by the linker if unused, in which case its address
7047 will be nullified; do not add such variables into partial
7048 symbol table then. */
7049 }
7050 else if (pdi->is_external)
c906108c
SS
7051 {
7052 /* Global Variable.
7053 Don't enter into the minimal symbol tables as there is
7054 a minimal symbol table entry from the ELF symbols already.
7055 Enter into partial symbol table if it has a location
7056 descriptor or a type.
7057 If the location descriptor is missing, new_symbol will create
7058 a LOC_UNRESOLVED symbol, the address of the variable will then
7059 be determined from the minimal symbol table whenever the variable
7060 is referenced.
7061 The address for the partial symbol table entry is not
7062 used by GDB, but it comes in handy for debugging partial symbol
7063 table building. */
7064
95554aad 7065 if (pdi->d.locdesc || pdi->has_type)
f47fb265 7066 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7067 built_actual_name != NULL,
f47fb265
MS
7068 VAR_DOMAIN, LOC_STATIC,
7069 &objfile->global_psymbols,
1762568f 7070 addr + baseaddr,
f47fb265 7071 cu->language, objfile);
c906108c
SS
7072 }
7073 else
7074 {
ff908ebf
AW
7075 int has_loc = pdi->d.locdesc != NULL;
7076
7077 /* Static Variable. Skip symbols whose value we cannot know (those
7078 without location descriptors or constant values). */
7079 if (!has_loc && !pdi->has_const_value)
decbce07 7080 {
15d034d0 7081 xfree (built_actual_name);
decbce07
MS
7082 return;
7083 }
ff908ebf 7084
f47fb265 7085 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7086 built_actual_name != NULL,
f47fb265
MS
7087 VAR_DOMAIN, LOC_STATIC,
7088 &objfile->static_psymbols,
ff908ebf 7089 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 7090 cu->language, objfile);
c906108c
SS
7091 }
7092 break;
7093 case DW_TAG_typedef:
7094 case DW_TAG_base_type:
a02abb62 7095 case DW_TAG_subrange_type:
38d518c9 7096 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7097 built_actual_name != NULL,
176620f1 7098 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 7099 &objfile->static_psymbols,
1762568f 7100 0, cu->language, objfile);
c906108c 7101 break;
74921315 7102 case DW_TAG_imported_declaration:
72bf9492
DJ
7103 case DW_TAG_namespace:
7104 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7105 built_actual_name != NULL,
72bf9492
DJ
7106 VAR_DOMAIN, LOC_TYPEDEF,
7107 &objfile->global_psymbols,
1762568f 7108 0, cu->language, objfile);
72bf9492 7109 break;
530e8392
KB
7110 case DW_TAG_module:
7111 add_psymbol_to_list (actual_name, strlen (actual_name),
7112 built_actual_name != NULL,
7113 MODULE_DOMAIN, LOC_TYPEDEF,
7114 &objfile->global_psymbols,
1762568f 7115 0, cu->language, objfile);
530e8392 7116 break;
c906108c 7117 case DW_TAG_class_type:
680b30c7 7118 case DW_TAG_interface_type:
c906108c
SS
7119 case DW_TAG_structure_type:
7120 case DW_TAG_union_type:
7121 case DW_TAG_enumeration_type:
fa4028e9
JB
7122 /* Skip external references. The DWARF standard says in the section
7123 about "Structure, Union, and Class Type Entries": "An incomplete
7124 structure, union or class type is represented by a structure,
7125 union or class entry that does not have a byte size attribute
7126 and that has a DW_AT_declaration attribute." */
7127 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 7128 {
15d034d0 7129 xfree (built_actual_name);
decbce07
MS
7130 return;
7131 }
fa4028e9 7132
63d06c5c
DC
7133 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7134 static vs. global. */
38d518c9 7135 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7136 built_actual_name != NULL,
176620f1 7137 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 7138 cu->language == language_cplus
63d06c5c
DC
7139 ? &objfile->global_psymbols
7140 : &objfile->static_psymbols,
1762568f 7141 0, cu->language, objfile);
c906108c 7142
c906108c
SS
7143 break;
7144 case DW_TAG_enumerator:
38d518c9 7145 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 7146 built_actual_name != NULL,
176620f1 7147 VAR_DOMAIN, LOC_CONST,
9c37b5ae 7148 cu->language == language_cplus
f6fe98ef
DJ
7149 ? &objfile->global_psymbols
7150 : &objfile->static_psymbols,
1762568f 7151 0, cu->language, objfile);
c906108c
SS
7152 break;
7153 default:
7154 break;
7155 }
5c4e30ca 7156
15d034d0 7157 xfree (built_actual_name);
c906108c
SS
7158}
7159
5c4e30ca
DC
7160/* Read a partial die corresponding to a namespace; also, add a symbol
7161 corresponding to that namespace to the symbol table. NAMESPACE is
7162 the name of the enclosing namespace. */
91c24f0a 7163
72bf9492
DJ
7164static void
7165add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7166 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7167 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7168{
72bf9492 7169 /* Add a symbol for the namespace. */
e7c27a73 7170
72bf9492 7171 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7172
7173 /* Now scan partial symbols in that namespace. */
7174
91c24f0a 7175 if (pdi->has_children)
cdc07690 7176 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
7177}
7178
5d7cb8df
JK
7179/* Read a partial die corresponding to a Fortran module. */
7180
7181static void
7182add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 7183 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 7184{
530e8392
KB
7185 /* Add a symbol for the namespace. */
7186
7187 add_partial_symbol (pdi, cu);
7188
f55ee35c 7189 /* Now scan partial symbols in that module. */
5d7cb8df
JK
7190
7191 if (pdi->has_children)
cdc07690 7192 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
7193}
7194
bc30ff58
JB
7195/* Read a partial die corresponding to a subprogram and create a partial
7196 symbol for that subprogram. When the CU language allows it, this
7197 routine also defines a partial symbol for each nested subprogram
cdc07690 7198 that this subprogram contains. If SET_ADDRMAP is true, record the
428fc5fc
YQ
7199 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7200 and highest PC values found in PDI.
6e70227d 7201
cdc07690
YQ
7202 PDI may also be a lexical block, in which case we simply search
7203 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
7204 Again, this is only performed when the CU language allows this
7205 type of definitions. */
7206
7207static void
7208add_partial_subprogram (struct partial_die_info *pdi,
7209 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7210 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58
JB
7211{
7212 if (pdi->tag == DW_TAG_subprogram)
7213 {
7214 if (pdi->has_pc_info)
7215 {
7216 if (pdi->lowpc < *lowpc)
7217 *lowpc = pdi->lowpc;
7218 if (pdi->highpc > *highpc)
7219 *highpc = pdi->highpc;
cdc07690 7220 if (set_addrmap)
5734ee8b 7221 {
5734ee8b 7222 struct objfile *objfile = cu->objfile;
3e29f34a
MR
7223 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7224 CORE_ADDR baseaddr;
7225 CORE_ADDR highpc;
7226 CORE_ADDR lowpc;
5734ee8b
DJ
7227
7228 baseaddr = ANOFFSET (objfile->section_offsets,
7229 SECT_OFF_TEXT (objfile));
3e29f34a
MR
7230 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7231 pdi->lowpc + baseaddr);
7232 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7233 pdi->highpc + baseaddr);
7234 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 7235 cu->per_cu->v.psymtab);
5734ee8b 7236 }
481860b3
GB
7237 }
7238
7239 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7240 {
bc30ff58 7241 if (!pdi->is_declaration)
e8d05480
JB
7242 /* Ignore subprogram DIEs that do not have a name, they are
7243 illegal. Do not emit a complaint at this point, we will
7244 do so when we convert this psymtab into a symtab. */
7245 if (pdi->name)
7246 add_partial_symbol (pdi, cu);
bc30ff58
JB
7247 }
7248 }
6e70227d 7249
bc30ff58
JB
7250 if (! pdi->has_children)
7251 return;
7252
7253 if (cu->language == language_ada)
7254 {
7255 pdi = pdi->die_child;
7256 while (pdi != NULL)
7257 {
7258 fixup_partial_die (pdi, cu);
7259 if (pdi->tag == DW_TAG_subprogram
7260 || pdi->tag == DW_TAG_lexical_block)
cdc07690 7261 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
7262 pdi = pdi->die_sibling;
7263 }
7264 }
7265}
7266
91c24f0a
DC
7267/* Read a partial die corresponding to an enumeration type. */
7268
72bf9492
DJ
7269static void
7270add_partial_enumeration (struct partial_die_info *enum_pdi,
7271 struct dwarf2_cu *cu)
91c24f0a 7272{
72bf9492 7273 struct partial_die_info *pdi;
91c24f0a
DC
7274
7275 if (enum_pdi->name != NULL)
72bf9492
DJ
7276 add_partial_symbol (enum_pdi, cu);
7277
7278 pdi = enum_pdi->die_child;
7279 while (pdi)
91c24f0a 7280 {
72bf9492 7281 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 7282 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 7283 else
72bf9492
DJ
7284 add_partial_symbol (pdi, cu);
7285 pdi = pdi->die_sibling;
91c24f0a 7286 }
91c24f0a
DC
7287}
7288
6caca83c
CC
7289/* Return the initial uleb128 in the die at INFO_PTR. */
7290
7291static unsigned int
d521ce57 7292peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
7293{
7294 unsigned int bytes_read;
7295
7296 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7297}
7298
4bb7a0a7
DJ
7299/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7300 Return the corresponding abbrev, or NULL if the number is zero (indicating
7301 an empty DIE). In either case *BYTES_READ will be set to the length of
7302 the initial number. */
7303
7304static struct abbrev_info *
d521ce57 7305peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 7306 struct dwarf2_cu *cu)
4bb7a0a7
DJ
7307{
7308 bfd *abfd = cu->objfile->obfd;
7309 unsigned int abbrev_number;
7310 struct abbrev_info *abbrev;
7311
7312 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7313
7314 if (abbrev_number == 0)
7315 return NULL;
7316
433df2d4 7317 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
7318 if (!abbrev)
7319 {
422b9917
DE
7320 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7321 " at offset 0x%x [in module %s]"),
7322 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7323 cu->header.offset.sect_off, bfd_get_filename (abfd));
4bb7a0a7
DJ
7324 }
7325
7326 return abbrev;
7327}
7328
93311388
DE
7329/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7330 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
7331 DIE. Any children of the skipped DIEs will also be skipped. */
7332
d521ce57
TT
7333static const gdb_byte *
7334skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 7335{
dee91e82 7336 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
7337 struct abbrev_info *abbrev;
7338 unsigned int bytes_read;
7339
7340 while (1)
7341 {
7342 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7343 if (abbrev == NULL)
7344 return info_ptr + bytes_read;
7345 else
dee91e82 7346 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
7347 }
7348}
7349
93311388
DE
7350/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7351 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
7352 abbrev corresponding to that skipped uleb128 should be passed in
7353 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7354 children. */
7355
d521ce57
TT
7356static const gdb_byte *
7357skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 7358 struct abbrev_info *abbrev)
4bb7a0a7
DJ
7359{
7360 unsigned int bytes_read;
7361 struct attribute attr;
dee91e82
DE
7362 bfd *abfd = reader->abfd;
7363 struct dwarf2_cu *cu = reader->cu;
d521ce57 7364 const gdb_byte *buffer = reader->buffer;
f664829e 7365 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
7366 unsigned int form, i;
7367
7368 for (i = 0; i < abbrev->num_attrs; i++)
7369 {
7370 /* The only abbrev we care about is DW_AT_sibling. */
7371 if (abbrev->attrs[i].name == DW_AT_sibling)
7372 {
dee91e82 7373 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 7374 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
7375 complaint (&symfile_complaints,
7376 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 7377 else
b9502d3f
WN
7378 {
7379 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7380 const gdb_byte *sibling_ptr = buffer + off;
7381
7382 if (sibling_ptr < info_ptr)
7383 complaint (&symfile_complaints,
7384 _("DW_AT_sibling points backwards"));
22869d73
KS
7385 else if (sibling_ptr > reader->buffer_end)
7386 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
7387 else
7388 return sibling_ptr;
7389 }
4bb7a0a7
DJ
7390 }
7391
7392 /* If it isn't DW_AT_sibling, skip this attribute. */
7393 form = abbrev->attrs[i].form;
7394 skip_attribute:
7395 switch (form)
7396 {
4bb7a0a7 7397 case DW_FORM_ref_addr:
ae411497
TT
7398 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7399 and later it is offset sized. */
7400 if (cu->header.version == 2)
7401 info_ptr += cu->header.addr_size;
7402 else
7403 info_ptr += cu->header.offset_size;
7404 break;
36586728
TT
7405 case DW_FORM_GNU_ref_alt:
7406 info_ptr += cu->header.offset_size;
7407 break;
ae411497 7408 case DW_FORM_addr:
4bb7a0a7
DJ
7409 info_ptr += cu->header.addr_size;
7410 break;
7411 case DW_FORM_data1:
7412 case DW_FORM_ref1:
7413 case DW_FORM_flag:
7414 info_ptr += 1;
7415 break;
2dc7f7b3 7416 case DW_FORM_flag_present:
43988095 7417 case DW_FORM_implicit_const:
2dc7f7b3 7418 break;
4bb7a0a7
DJ
7419 case DW_FORM_data2:
7420 case DW_FORM_ref2:
7421 info_ptr += 2;
7422 break;
7423 case DW_FORM_data4:
7424 case DW_FORM_ref4:
7425 info_ptr += 4;
7426 break;
7427 case DW_FORM_data8:
7428 case DW_FORM_ref8:
55f1336d 7429 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
7430 info_ptr += 8;
7431 break;
0224619f
JK
7432 case DW_FORM_data16:
7433 info_ptr += 16;
7434 break;
4bb7a0a7 7435 case DW_FORM_string:
9b1c24c8 7436 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
7437 info_ptr += bytes_read;
7438 break;
2dc7f7b3 7439 case DW_FORM_sec_offset:
4bb7a0a7 7440 case DW_FORM_strp:
36586728 7441 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
7442 info_ptr += cu->header.offset_size;
7443 break;
2dc7f7b3 7444 case DW_FORM_exprloc:
4bb7a0a7
DJ
7445 case DW_FORM_block:
7446 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7447 info_ptr += bytes_read;
7448 break;
7449 case DW_FORM_block1:
7450 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7451 break;
7452 case DW_FORM_block2:
7453 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7454 break;
7455 case DW_FORM_block4:
7456 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7457 break;
7458 case DW_FORM_sdata:
7459 case DW_FORM_udata:
7460 case DW_FORM_ref_udata:
3019eac3
DE
7461 case DW_FORM_GNU_addr_index:
7462 case DW_FORM_GNU_str_index:
d521ce57 7463 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
7464 break;
7465 case DW_FORM_indirect:
7466 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7467 info_ptr += bytes_read;
7468 /* We need to continue parsing from here, so just go back to
7469 the top. */
7470 goto skip_attribute;
7471
7472 default:
3e43a32a
MS
7473 error (_("Dwarf Error: Cannot handle %s "
7474 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
7475 dwarf_form_name (form),
7476 bfd_get_filename (abfd));
7477 }
7478 }
7479
7480 if (abbrev->has_children)
dee91e82 7481 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
7482 else
7483 return info_ptr;
7484}
7485
93311388 7486/* Locate ORIG_PDI's sibling.
dee91e82 7487 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 7488
d521ce57 7489static const gdb_byte *
dee91e82
DE
7490locate_pdi_sibling (const struct die_reader_specs *reader,
7491 struct partial_die_info *orig_pdi,
d521ce57 7492 const gdb_byte *info_ptr)
91c24f0a
DC
7493{
7494 /* Do we know the sibling already? */
72bf9492 7495
91c24f0a
DC
7496 if (orig_pdi->sibling)
7497 return orig_pdi->sibling;
7498
7499 /* Are there any children to deal with? */
7500
7501 if (!orig_pdi->has_children)
7502 return info_ptr;
7503
4bb7a0a7 7504 /* Skip the children the long way. */
91c24f0a 7505
dee91e82 7506 return skip_children (reader, info_ptr);
91c24f0a
DC
7507}
7508
257e7a09 7509/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 7510 not NULL. */
c906108c
SS
7511
7512static void
257e7a09
YQ
7513dwarf2_read_symtab (struct partial_symtab *self,
7514 struct objfile *objfile)
c906108c 7515{
257e7a09 7516 if (self->readin)
c906108c 7517 {
442e4d9c 7518 warning (_("bug: psymtab for %s is already read in."),
257e7a09 7519 self->filename);
442e4d9c
YQ
7520 }
7521 else
7522 {
7523 if (info_verbose)
c906108c 7524 {
442e4d9c 7525 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 7526 self->filename);
442e4d9c 7527 gdb_flush (gdb_stdout);
c906108c 7528 }
c906108c 7529
442e4d9c 7530 /* Restore our global data. */
9a3c8263
SM
7531 dwarf2_per_objfile
7532 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7533 dwarf2_objfile_data_key);
10b3939b 7534
442e4d9c
YQ
7535 /* If this psymtab is constructed from a debug-only objfile, the
7536 has_section_at_zero flag will not necessarily be correct. We
7537 can get the correct value for this flag by looking at the data
7538 associated with the (presumably stripped) associated objfile. */
7539 if (objfile->separate_debug_objfile_backlink)
7540 {
7541 struct dwarf2_per_objfile *dpo_backlink
9a3c8263
SM
7542 = ((struct dwarf2_per_objfile *)
7543 objfile_data (objfile->separate_debug_objfile_backlink,
7544 dwarf2_objfile_data_key));
9a619af0 7545
442e4d9c
YQ
7546 dwarf2_per_objfile->has_section_at_zero
7547 = dpo_backlink->has_section_at_zero;
7548 }
b2ab525c 7549
442e4d9c 7550 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 7551
257e7a09 7552 psymtab_to_symtab_1 (self);
c906108c 7553
442e4d9c
YQ
7554 /* Finish up the debug error message. */
7555 if (info_verbose)
7556 printf_filtered (_("done.\n"));
c906108c 7557 }
95554aad
TT
7558
7559 process_cu_includes ();
c906108c 7560}
9cdd5dbd
DE
7561\f
7562/* Reading in full CUs. */
c906108c 7563
10b3939b
DJ
7564/* Add PER_CU to the queue. */
7565
7566static void
95554aad
TT
7567queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7568 enum language pretend_language)
10b3939b
DJ
7569{
7570 struct dwarf2_queue_item *item;
7571
7572 per_cu->queued = 1;
8d749320 7573 item = XNEW (struct dwarf2_queue_item);
10b3939b 7574 item->per_cu = per_cu;
95554aad 7575 item->pretend_language = pretend_language;
10b3939b
DJ
7576 item->next = NULL;
7577
7578 if (dwarf2_queue == NULL)
7579 dwarf2_queue = item;
7580 else
7581 dwarf2_queue_tail->next = item;
7582
7583 dwarf2_queue_tail = item;
7584}
7585
89e63ee4
DE
7586/* If PER_CU is not yet queued, add it to the queue.
7587 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7588 dependency.
0907af0c 7589 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
7590 meaning either PER_CU is already queued or it is already loaded.
7591
7592 N.B. There is an invariant here that if a CU is queued then it is loaded.
7593 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
7594
7595static int
89e63ee4 7596maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
7597 struct dwarf2_per_cu_data *per_cu,
7598 enum language pretend_language)
7599{
7600 /* We may arrive here during partial symbol reading, if we need full
7601 DIEs to process an unusual case (e.g. template arguments). Do
7602 not queue PER_CU, just tell our caller to load its DIEs. */
7603 if (dwarf2_per_objfile->reading_partial_symbols)
7604 {
7605 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7606 return 1;
7607 return 0;
7608 }
7609
7610 /* Mark the dependence relation so that we don't flush PER_CU
7611 too early. */
89e63ee4
DE
7612 if (dependent_cu != NULL)
7613 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
7614
7615 /* If it's already on the queue, we have nothing to do. */
7616 if (per_cu->queued)
7617 return 0;
7618
7619 /* If the compilation unit is already loaded, just mark it as
7620 used. */
7621 if (per_cu->cu != NULL)
7622 {
7623 per_cu->cu->last_used = 0;
7624 return 0;
7625 }
7626
7627 /* Add it to the queue. */
7628 queue_comp_unit (per_cu, pretend_language);
7629
7630 return 1;
7631}
7632
10b3939b
DJ
7633/* Process the queue. */
7634
7635static void
a0f42c21 7636process_queue (void)
10b3939b
DJ
7637{
7638 struct dwarf2_queue_item *item, *next_item;
7639
b4f54984 7640 if (dwarf_read_debug)
45cfd468
DE
7641 {
7642 fprintf_unfiltered (gdb_stdlog,
7643 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 7644 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
7645 }
7646
03dd20cc
DJ
7647 /* The queue starts out with one item, but following a DIE reference
7648 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7649 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7650 {
cc12ce38
DE
7651 if ((dwarf2_per_objfile->using_index
7652 ? !item->per_cu->v.quick->compunit_symtab
7653 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7654 /* Skip dummy CUs. */
7655 && item->per_cu->cu != NULL)
f4dc4d17
DE
7656 {
7657 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 7658 unsigned int debug_print_threshold;
247f5c4f 7659 char buf[100];
f4dc4d17 7660
247f5c4f 7661 if (per_cu->is_debug_types)
f4dc4d17 7662 {
247f5c4f
DE
7663 struct signatured_type *sig_type =
7664 (struct signatured_type *) per_cu;
7665
7666 sprintf (buf, "TU %s at offset 0x%x",
73be47f5
DE
7667 hex_string (sig_type->signature),
7668 per_cu->offset.sect_off);
7669 /* There can be 100s of TUs.
7670 Only print them in verbose mode. */
7671 debug_print_threshold = 2;
f4dc4d17 7672 }
247f5c4f 7673 else
73be47f5
DE
7674 {
7675 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7676 debug_print_threshold = 1;
7677 }
247f5c4f 7678
b4f54984 7679 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7680 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7681
7682 if (per_cu->is_debug_types)
7683 process_full_type_unit (per_cu, item->pretend_language);
7684 else
7685 process_full_comp_unit (per_cu, item->pretend_language);
7686
b4f54984 7687 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 7688 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7689 }
10b3939b
DJ
7690
7691 item->per_cu->queued = 0;
7692 next_item = item->next;
7693 xfree (item);
7694 }
7695
7696 dwarf2_queue_tail = NULL;
45cfd468 7697
b4f54984 7698 if (dwarf_read_debug)
45cfd468
DE
7699 {
7700 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 7701 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 7702 }
10b3939b
DJ
7703}
7704
7705/* Free all allocated queue entries. This function only releases anything if
7706 an error was thrown; if the queue was processed then it would have been
7707 freed as we went along. */
7708
7709static void
7710dwarf2_release_queue (void *dummy)
7711{
7712 struct dwarf2_queue_item *item, *last;
7713
7714 item = dwarf2_queue;
7715 while (item)
7716 {
7717 /* Anything still marked queued is likely to be in an
7718 inconsistent state, so discard it. */
7719 if (item->per_cu->queued)
7720 {
7721 if (item->per_cu->cu != NULL)
dee91e82 7722 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7723 item->per_cu->queued = 0;
7724 }
7725
7726 last = item;
7727 item = item->next;
7728 xfree (last);
7729 }
7730
7731 dwarf2_queue = dwarf2_queue_tail = NULL;
7732}
7733
7734/* Read in full symbols for PST, and anything it depends on. */
7735
c906108c 7736static void
fba45db2 7737psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7738{
10b3939b 7739 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7740 int i;
7741
95554aad
TT
7742 if (pst->readin)
7743 return;
7744
aaa75496 7745 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7746 if (!pst->dependencies[i]->readin
7747 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7748 {
7749 /* Inform about additional files that need to be read in. */
7750 if (info_verbose)
7751 {
a3f17187 7752 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7753 fputs_filtered (" ", gdb_stdout);
7754 wrap_here ("");
7755 fputs_filtered ("and ", gdb_stdout);
7756 wrap_here ("");
7757 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7758 wrap_here (""); /* Flush output. */
aaa75496
JB
7759 gdb_flush (gdb_stdout);
7760 }
7761 psymtab_to_symtab_1 (pst->dependencies[i]);
7762 }
7763
9a3c8263 7764 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
7765
7766 if (per_cu == NULL)
aaa75496
JB
7767 {
7768 /* It's an include file, no symbols to read for it.
7769 Everything is in the parent symtab. */
7770 pst->readin = 1;
7771 return;
7772 }
c906108c 7773
a0f42c21 7774 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7775}
7776
dee91e82
DE
7777/* Trivial hash function for die_info: the hash value of a DIE
7778 is its offset in .debug_info for this objfile. */
10b3939b 7779
dee91e82
DE
7780static hashval_t
7781die_hash (const void *item)
10b3939b 7782{
9a3c8263 7783 const struct die_info *die = (const struct die_info *) item;
6502dd73 7784
dee91e82
DE
7785 return die->offset.sect_off;
7786}
63d06c5c 7787
dee91e82
DE
7788/* Trivial comparison function for die_info structures: two DIEs
7789 are equal if they have the same offset. */
98bfdba5 7790
dee91e82
DE
7791static int
7792die_eq (const void *item_lhs, const void *item_rhs)
7793{
9a3c8263
SM
7794 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7795 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 7796
dee91e82
DE
7797 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7798}
c906108c 7799
dee91e82
DE
7800/* die_reader_func for load_full_comp_unit.
7801 This is identical to read_signatured_type_reader,
7802 but is kept separate for now. */
c906108c 7803
dee91e82
DE
7804static void
7805load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7806 const gdb_byte *info_ptr,
dee91e82
DE
7807 struct die_info *comp_unit_die,
7808 int has_children,
7809 void *data)
7810{
7811 struct dwarf2_cu *cu = reader->cu;
9a3c8263 7812 enum language *language_ptr = (enum language *) data;
6caca83c 7813
dee91e82
DE
7814 gdb_assert (cu->die_hash == NULL);
7815 cu->die_hash =
7816 htab_create_alloc_ex (cu->header.length / 12,
7817 die_hash,
7818 die_eq,
7819 NULL,
7820 &cu->comp_unit_obstack,
7821 hashtab_obstack_allocate,
7822 dummy_obstack_deallocate);
e142c38c 7823
dee91e82
DE
7824 if (has_children)
7825 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7826 &info_ptr, comp_unit_die);
7827 cu->dies = comp_unit_die;
7828 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7829
7830 /* We try not to read any attributes in this function, because not
9cdd5dbd 7831 all CUs needed for references have been loaded yet, and symbol
10b3939b 7832 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7833 or we won't be able to build types correctly.
7834 Similarly, if we do not read the producer, we can not apply
7835 producer-specific interpretation. */
95554aad 7836 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7837}
10b3939b 7838
dee91e82 7839/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7840
dee91e82 7841static void
95554aad
TT
7842load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7843 enum language pretend_language)
dee91e82 7844{
3019eac3 7845 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7846
f4dc4d17
DE
7847 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7848 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7849}
7850
3da10d80
KS
7851/* Add a DIE to the delayed physname list. */
7852
7853static void
7854add_to_method_list (struct type *type, int fnfield_index, int index,
7855 const char *name, struct die_info *die,
7856 struct dwarf2_cu *cu)
7857{
7858 struct delayed_method_info mi;
7859 mi.type = type;
7860 mi.fnfield_index = fnfield_index;
7861 mi.index = index;
7862 mi.name = name;
7863 mi.die = die;
7864 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7865}
7866
7867/* A cleanup for freeing the delayed method list. */
7868
7869static void
7870free_delayed_list (void *ptr)
7871{
7872 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7873 if (cu->method_list != NULL)
7874 {
7875 VEC_free (delayed_method_info, cu->method_list);
7876 cu->method_list = NULL;
7877 }
7878}
7879
7880/* Compute the physnames of any methods on the CU's method list.
7881
7882 The computation of method physnames is delayed in order to avoid the
7883 (bad) condition that one of the method's formal parameters is of an as yet
7884 incomplete type. */
7885
7886static void
7887compute_delayed_physnames (struct dwarf2_cu *cu)
7888{
7889 int i;
7890 struct delayed_method_info *mi;
7891 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7892 {
1d06ead6 7893 const char *physname;
3da10d80
KS
7894 struct fn_fieldlist *fn_flp
7895 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7896 physname = dwarf2_physname (mi->name, mi->die, cu);
005e54bb
DE
7897 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7898 = physname ? physname : "";
3da10d80
KS
7899 }
7900}
7901
a766d390
DE
7902/* Go objects should be embedded in a DW_TAG_module DIE,
7903 and it's not clear if/how imported objects will appear.
7904 To keep Go support simple until that's worked out,
7905 go back through what we've read and create something usable.
7906 We could do this while processing each DIE, and feels kinda cleaner,
7907 but that way is more invasive.
7908 This is to, for example, allow the user to type "p var" or "b main"
7909 without having to specify the package name, and allow lookups
7910 of module.object to work in contexts that use the expression
7911 parser. */
7912
7913static void
7914fixup_go_packaging (struct dwarf2_cu *cu)
7915{
7916 char *package_name = NULL;
7917 struct pending *list;
7918 int i;
7919
7920 for (list = global_symbols; list != NULL; list = list->next)
7921 {
7922 for (i = 0; i < list->nsyms; ++i)
7923 {
7924 struct symbol *sym = list->symbol[i];
7925
7926 if (SYMBOL_LANGUAGE (sym) == language_go
7927 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7928 {
7929 char *this_package_name = go_symbol_package_name (sym);
7930
7931 if (this_package_name == NULL)
7932 continue;
7933 if (package_name == NULL)
7934 package_name = this_package_name;
7935 else
7936 {
7937 if (strcmp (package_name, this_package_name) != 0)
7938 complaint (&symfile_complaints,
7939 _("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
7940 (symbol_symtab (sym) != NULL
7941 ? symtab_to_filename_for_display
7942 (symbol_symtab (sym))
4262abfb 7943 : objfile_name (cu->objfile)),
a766d390
DE
7944 this_package_name, package_name);
7945 xfree (this_package_name);
7946 }
7947 }
7948 }
7949 }
7950
7951 if (package_name != NULL)
7952 {
7953 struct objfile *objfile = cu->objfile;
34a68019 7954 const char *saved_package_name
224c3ddb
SM
7955 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
7956 package_name,
7957 strlen (package_name));
19f392bc
UW
7958 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7959 saved_package_name);
a766d390
DE
7960 struct symbol *sym;
7961
7962 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7963
e623cf5d 7964 sym = allocate_symbol (objfile);
f85f34ed 7965 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7966 SYMBOL_SET_NAMES (sym, saved_package_name,
7967 strlen (saved_package_name), 0, objfile);
a766d390
DE
7968 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7969 e.g., "main" finds the "main" module and not C's main(). */
7970 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7971 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7972 SYMBOL_TYPE (sym) = type;
7973
7974 add_symbol_to_list (sym, &global_symbols);
7975
7976 xfree (package_name);
7977 }
7978}
7979
95554aad
TT
7980/* Return the symtab for PER_CU. This works properly regardless of
7981 whether we're using the index or psymtabs. */
7982
43f3e411
DE
7983static struct compunit_symtab *
7984get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad
TT
7985{
7986 return (dwarf2_per_objfile->using_index
43f3e411
DE
7987 ? per_cu->v.quick->compunit_symtab
7988 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
7989}
7990
7991/* A helper function for computing the list of all symbol tables
7992 included by PER_CU. */
7993
7994static void
43f3e411 7995recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 7996 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 7997 struct dwarf2_per_cu_data *per_cu,
43f3e411 7998 struct compunit_symtab *immediate_parent)
95554aad
TT
7999{
8000 void **slot;
8001 int ix;
43f3e411 8002 struct compunit_symtab *cust;
95554aad
TT
8003 struct dwarf2_per_cu_data *iter;
8004
8005 slot = htab_find_slot (all_children, per_cu, INSERT);
8006 if (*slot != NULL)
8007 {
8008 /* This inclusion and its children have been processed. */
8009 return;
8010 }
8011
8012 *slot = per_cu;
8013 /* Only add a CU if it has a symbol table. */
43f3e411
DE
8014 cust = get_compunit_symtab (per_cu);
8015 if (cust != NULL)
ec94af83
DE
8016 {
8017 /* If this is a type unit only add its symbol table if we haven't
8018 seen it yet (type unit per_cu's can share symtabs). */
8019 if (per_cu->is_debug_types)
8020 {
43f3e411 8021 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
8022 if (*slot == NULL)
8023 {
43f3e411
DE
8024 *slot = cust;
8025 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8026 if (cust->user == NULL)
8027 cust->user = immediate_parent;
ec94af83
DE
8028 }
8029 }
8030 else
f9125b6c 8031 {
43f3e411
DE
8032 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8033 if (cust->user == NULL)
8034 cust->user = immediate_parent;
f9125b6c 8035 }
ec94af83 8036 }
95554aad
TT
8037
8038 for (ix = 0;
796a7ff8 8039 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 8040 ++ix)
ec94af83
DE
8041 {
8042 recursively_compute_inclusions (result, all_children,
43f3e411 8043 all_type_symtabs, iter, cust);
ec94af83 8044 }
95554aad
TT
8045}
8046
43f3e411 8047/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
8048 PER_CU. */
8049
8050static void
43f3e411 8051compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 8052{
f4dc4d17
DE
8053 gdb_assert (! per_cu->is_debug_types);
8054
796a7ff8 8055 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
8056 {
8057 int ix, len;
ec94af83 8058 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
8059 struct compunit_symtab *compunit_symtab_iter;
8060 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 8061 htab_t all_children, all_type_symtabs;
43f3e411 8062 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
8063
8064 /* If we don't have a symtab, we can just skip this case. */
43f3e411 8065 if (cust == NULL)
95554aad
TT
8066 return;
8067
8068 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8069 NULL, xcalloc, xfree);
ec94af83
DE
8070 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8071 NULL, xcalloc, xfree);
95554aad
TT
8072
8073 for (ix = 0;
796a7ff8 8074 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 8075 ix, per_cu_iter);
95554aad 8076 ++ix)
ec94af83
DE
8077 {
8078 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 8079 all_type_symtabs, per_cu_iter,
43f3e411 8080 cust);
ec94af83 8081 }
95554aad 8082
ec94af83 8083 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
8084 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8085 cust->includes
8d749320
SM
8086 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8087 struct compunit_symtab *, len + 1);
95554aad 8088 for (ix = 0;
43f3e411
DE
8089 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8090 compunit_symtab_iter);
95554aad 8091 ++ix)
43f3e411
DE
8092 cust->includes[ix] = compunit_symtab_iter;
8093 cust->includes[len] = NULL;
95554aad 8094
43f3e411 8095 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 8096 htab_delete (all_children);
ec94af83 8097 htab_delete (all_type_symtabs);
95554aad
TT
8098 }
8099}
8100
8101/* Compute the 'includes' field for the symtabs of all the CUs we just
8102 read. */
8103
8104static void
8105process_cu_includes (void)
8106{
8107 int ix;
8108 struct dwarf2_per_cu_data *iter;
8109
8110 for (ix = 0;
8111 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8112 ix, iter);
8113 ++ix)
f4dc4d17
DE
8114 {
8115 if (! iter->is_debug_types)
43f3e411 8116 compute_compunit_symtab_includes (iter);
f4dc4d17 8117 }
95554aad
TT
8118
8119 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8120}
8121
9cdd5dbd 8122/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
8123 already been loaded into memory. */
8124
8125static void
95554aad
TT
8126process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8127 enum language pretend_language)
10b3939b 8128{
10b3939b 8129 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 8130 struct objfile *objfile = per_cu->objfile;
3e29f34a 8131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 8132 CORE_ADDR lowpc, highpc;
43f3e411 8133 struct compunit_symtab *cust;
3da10d80 8134 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 8135 CORE_ADDR baseaddr;
4359dff1 8136 struct block *static_block;
3e29f34a 8137 CORE_ADDR addr;
10b3939b
DJ
8138
8139 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8140
10b3939b
DJ
8141 buildsym_init ();
8142 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 8143 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
8144
8145 cu->list_in_scope = &file_symbols;
c906108c 8146
95554aad
TT
8147 cu->language = pretend_language;
8148 cu->language_defn = language_def (cu->language);
8149
c906108c 8150 /* Do line number decoding in read_file_scope () */
10b3939b 8151 process_die (cu->dies, cu);
c906108c 8152
a766d390
DE
8153 /* For now fudge the Go package. */
8154 if (cu->language == language_go)
8155 fixup_go_packaging (cu);
8156
3da10d80
KS
8157 /* Now that we have processed all the DIEs in the CU, all the types
8158 should be complete, and it should now be safe to compute all of the
8159 physnames. */
8160 compute_delayed_physnames (cu);
8161 do_cleanups (delayed_list_cleanup);
8162
fae299cd
DC
8163 /* Some compilers don't define a DW_AT_high_pc attribute for the
8164 compilation unit. If the DW_AT_high_pc is missing, synthesize
8165 it, by scanning the DIE's below the compilation unit. */
10b3939b 8166 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 8167
3e29f34a
MR
8168 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8169 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
8170
8171 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8172 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8173 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8174 addrmap to help ensure it has an accurate map of pc values belonging to
8175 this comp unit. */
8176 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8177
43f3e411
DE
8178 cust = end_symtab_from_static_block (static_block,
8179 SECT_OFF_TEXT (objfile), 0);
c906108c 8180
43f3e411 8181 if (cust != NULL)
c906108c 8182 {
df15bd07 8183 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 8184
8be455d7
JK
8185 /* Set symtab language to language from DW_AT_language. If the
8186 compilation is from a C file generated by language preprocessors, do
8187 not set the language if it was already deduced by start_subfile. */
43f3e411 8188 if (!(cu->language == language_c
40e3ad0e 8189 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 8190 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
8191
8192 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8193 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
8194 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8195 there were bugs in prologue debug info, fixed later in GCC-4.5
8196 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
8197
8198 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8199 needed, it would be wrong due to missing DW_AT_producer there.
8200
8201 Still one can confuse GDB by using non-standard GCC compilation
8202 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8203 */
ab260dad 8204 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 8205 cust->locations_valid = 1;
e0d00bc7
JK
8206
8207 if (gcc_4_minor >= 5)
43f3e411 8208 cust->epilogue_unwind_valid = 1;
96408a79 8209
43f3e411 8210 cust->call_site_htab = cu->call_site_htab;
c906108c 8211 }
9291a0cd
TT
8212
8213 if (dwarf2_per_objfile->using_index)
43f3e411 8214 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
8215 else
8216 {
8217 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8218 pst->compunit_symtab = cust;
9291a0cd
TT
8219 pst->readin = 1;
8220 }
c906108c 8221
95554aad
TT
8222 /* Push it for inclusion processing later. */
8223 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8224
c906108c 8225 do_cleanups (back_to);
f4dc4d17 8226}
45cfd468 8227
f4dc4d17
DE
8228/* Generate full symbol information for type unit PER_CU, whose DIEs have
8229 already been loaded into memory. */
8230
8231static void
8232process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8233 enum language pretend_language)
8234{
8235 struct dwarf2_cu *cu = per_cu->cu;
8236 struct objfile *objfile = per_cu->objfile;
43f3e411 8237 struct compunit_symtab *cust;
f4dc4d17 8238 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
8239 struct signatured_type *sig_type;
8240
8241 gdb_assert (per_cu->is_debug_types);
8242 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8243
8244 buildsym_init ();
8245 back_to = make_cleanup (really_free_pendings, NULL);
8246 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8247
8248 cu->list_in_scope = &file_symbols;
8249
8250 cu->language = pretend_language;
8251 cu->language_defn = language_def (cu->language);
8252
8253 /* The symbol tables are set up in read_type_unit_scope. */
8254 process_die (cu->dies, cu);
8255
8256 /* For now fudge the Go package. */
8257 if (cu->language == language_go)
8258 fixup_go_packaging (cu);
8259
8260 /* Now that we have processed all the DIEs in the CU, all the types
8261 should be complete, and it should now be safe to compute all of the
8262 physnames. */
8263 compute_delayed_physnames (cu);
8264 do_cleanups (delayed_list_cleanup);
8265
8266 /* TUs share symbol tables.
8267 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
8268 of it with end_expandable_symtab. Otherwise, complete the addition of
8269 this TU's symbols to the existing symtab. */
43f3e411 8270 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 8271 {
43f3e411
DE
8272 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8273 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 8274
43f3e411 8275 if (cust != NULL)
f4dc4d17
DE
8276 {
8277 /* Set symtab language to language from DW_AT_language. If the
8278 compilation is from a C file generated by language preprocessors,
8279 do not set the language if it was already deduced by
8280 start_subfile. */
43f3e411
DE
8281 if (!(cu->language == language_c
8282 && COMPUNIT_FILETABS (cust)->language != language_c))
8283 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
8284 }
8285 }
8286 else
8287 {
0ab9ce85 8288 augment_type_symtab ();
43f3e411 8289 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
8290 }
8291
8292 if (dwarf2_per_objfile->using_index)
43f3e411 8293 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
8294 else
8295 {
8296 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 8297 pst->compunit_symtab = cust;
f4dc4d17 8298 pst->readin = 1;
45cfd468 8299 }
f4dc4d17
DE
8300
8301 do_cleanups (back_to);
c906108c
SS
8302}
8303
95554aad
TT
8304/* Process an imported unit DIE. */
8305
8306static void
8307process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8308{
8309 struct attribute *attr;
8310
f4dc4d17
DE
8311 /* For now we don't handle imported units in type units. */
8312 if (cu->per_cu->is_debug_types)
8313 {
8314 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8315 " supported in type units [in module %s]"),
4262abfb 8316 objfile_name (cu->objfile));
f4dc4d17
DE
8317 }
8318
95554aad
TT
8319 attr = dwarf2_attr (die, DW_AT_import, cu);
8320 if (attr != NULL)
8321 {
8322 struct dwarf2_per_cu_data *per_cu;
95554aad 8323 sect_offset offset;
36586728 8324 int is_dwz;
95554aad
TT
8325
8326 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
8327 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8328 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 8329
69d751e3 8330 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
8331 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8332 load_full_comp_unit (per_cu, cu->language);
8333
796a7ff8 8334 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
8335 per_cu);
8336 }
8337}
8338
adde2bff
DE
8339/* Reset the in_process bit of a die. */
8340
8341static void
8342reset_die_in_process (void *arg)
8343{
9a3c8263 8344 struct die_info *die = (struct die_info *) arg;
8c3cb9fa 8345
adde2bff
DE
8346 die->in_process = 0;
8347}
8348
c906108c
SS
8349/* Process a die and its children. */
8350
8351static void
e7c27a73 8352process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8353{
adde2bff
DE
8354 struct cleanup *in_process;
8355
8356 /* We should only be processing those not already in process. */
8357 gdb_assert (!die->in_process);
8358
8359 die->in_process = 1;
8360 in_process = make_cleanup (reset_die_in_process,die);
8361
c906108c
SS
8362 switch (die->tag)
8363 {
8364 case DW_TAG_padding:
8365 break;
8366 case DW_TAG_compile_unit:
95554aad 8367 case DW_TAG_partial_unit:
e7c27a73 8368 read_file_scope (die, cu);
c906108c 8369 break;
348e048f
DE
8370 case DW_TAG_type_unit:
8371 read_type_unit_scope (die, cu);
8372 break;
c906108c 8373 case DW_TAG_subprogram:
c906108c 8374 case DW_TAG_inlined_subroutine:
edb3359d 8375 read_func_scope (die, cu);
c906108c
SS
8376 break;
8377 case DW_TAG_lexical_block:
14898363
L
8378 case DW_TAG_try_block:
8379 case DW_TAG_catch_block:
e7c27a73 8380 read_lexical_block_scope (die, cu);
c906108c 8381 break;
216f72a1 8382 case DW_TAG_call_site:
96408a79
SA
8383 case DW_TAG_GNU_call_site:
8384 read_call_site_scope (die, cu);
8385 break;
c906108c 8386 case DW_TAG_class_type:
680b30c7 8387 case DW_TAG_interface_type:
c906108c
SS
8388 case DW_TAG_structure_type:
8389 case DW_TAG_union_type:
134d01f1 8390 process_structure_scope (die, cu);
c906108c
SS
8391 break;
8392 case DW_TAG_enumeration_type:
134d01f1 8393 process_enumeration_scope (die, cu);
c906108c 8394 break;
134d01f1 8395
f792889a
DJ
8396 /* These dies have a type, but processing them does not create
8397 a symbol or recurse to process the children. Therefore we can
8398 read them on-demand through read_type_die. */
c906108c 8399 case DW_TAG_subroutine_type:
72019c9c 8400 case DW_TAG_set_type:
c906108c 8401 case DW_TAG_array_type:
c906108c 8402 case DW_TAG_pointer_type:
c906108c 8403 case DW_TAG_ptr_to_member_type:
c906108c 8404 case DW_TAG_reference_type:
4297a3f0 8405 case DW_TAG_rvalue_reference_type:
c906108c 8406 case DW_TAG_string_type:
c906108c 8407 break;
134d01f1 8408
c906108c 8409 case DW_TAG_base_type:
a02abb62 8410 case DW_TAG_subrange_type:
cb249c71 8411 case DW_TAG_typedef:
134d01f1
DJ
8412 /* Add a typedef symbol for the type definition, if it has a
8413 DW_AT_name. */
f792889a 8414 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 8415 break;
c906108c 8416 case DW_TAG_common_block:
e7c27a73 8417 read_common_block (die, cu);
c906108c
SS
8418 break;
8419 case DW_TAG_common_inclusion:
8420 break;
d9fa45fe 8421 case DW_TAG_namespace:
4d4ec4e5 8422 cu->processing_has_namespace_info = 1;
e7c27a73 8423 read_namespace (die, cu);
d9fa45fe 8424 break;
5d7cb8df 8425 case DW_TAG_module:
4d4ec4e5 8426 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
8427 read_module (die, cu);
8428 break;
d9fa45fe 8429 case DW_TAG_imported_declaration:
74921315
KS
8430 cu->processing_has_namespace_info = 1;
8431 if (read_namespace_alias (die, cu))
8432 break;
8433 /* The declaration is not a global namespace alias: fall through. */
d9fa45fe 8434 case DW_TAG_imported_module:
4d4ec4e5 8435 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
8436 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8437 || cu->language != language_fortran))
8438 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8439 dwarf_tag_name (die->tag));
8440 read_import_statement (die, cu);
d9fa45fe 8441 break;
95554aad
TT
8442
8443 case DW_TAG_imported_unit:
8444 process_imported_unit_die (die, cu);
8445 break;
8446
c906108c 8447 default:
e7c27a73 8448 new_symbol (die, NULL, cu);
c906108c
SS
8449 break;
8450 }
adde2bff
DE
8451
8452 do_cleanups (in_process);
c906108c 8453}
ca69b9e6
DE
8454\f
8455/* DWARF name computation. */
c906108c 8456
94af9270
KS
8457/* A helper function for dwarf2_compute_name which determines whether DIE
8458 needs to have the name of the scope prepended to the name listed in the
8459 die. */
8460
8461static int
8462die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8463{
1c809c68
TT
8464 struct attribute *attr;
8465
94af9270
KS
8466 switch (die->tag)
8467 {
8468 case DW_TAG_namespace:
8469 case DW_TAG_typedef:
8470 case DW_TAG_class_type:
8471 case DW_TAG_interface_type:
8472 case DW_TAG_structure_type:
8473 case DW_TAG_union_type:
8474 case DW_TAG_enumeration_type:
8475 case DW_TAG_enumerator:
8476 case DW_TAG_subprogram:
08a76f8a 8477 case DW_TAG_inlined_subroutine:
94af9270 8478 case DW_TAG_member:
74921315 8479 case DW_TAG_imported_declaration:
94af9270
KS
8480 return 1;
8481
8482 case DW_TAG_variable:
c2b0a229 8483 case DW_TAG_constant:
94af9270
KS
8484 /* We only need to prefix "globally" visible variables. These include
8485 any variable marked with DW_AT_external or any variable that
8486 lives in a namespace. [Variables in anonymous namespaces
8487 require prefixing, but they are not DW_AT_external.] */
8488
8489 if (dwarf2_attr (die, DW_AT_specification, cu))
8490 {
8491 struct dwarf2_cu *spec_cu = cu;
9a619af0 8492
94af9270
KS
8493 return die_needs_namespace (die_specification (die, &spec_cu),
8494 spec_cu);
8495 }
8496
1c809c68 8497 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
8498 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8499 && die->parent->tag != DW_TAG_module)
1c809c68
TT
8500 return 0;
8501 /* A variable in a lexical block of some kind does not need a
8502 namespace, even though in C++ such variables may be external
8503 and have a mangled name. */
8504 if (die->parent->tag == DW_TAG_lexical_block
8505 || die->parent->tag == DW_TAG_try_block
1054b214
TT
8506 || die->parent->tag == DW_TAG_catch_block
8507 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
8508 return 0;
8509 return 1;
94af9270
KS
8510
8511 default:
8512 return 0;
8513 }
8514}
8515
8516/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 8517 compute the physname for the object, which include a method's:
9c37b5ae 8518 - formal parameters (C++),
a766d390 8519 - receiver type (Go),
a766d390
DE
8520
8521 The term "physname" is a bit confusing.
8522 For C++, for example, it is the demangled name.
8523 For Go, for example, it's the mangled name.
94af9270 8524
af6b7be1
JB
8525 For Ada, return the DIE's linkage name rather than the fully qualified
8526 name. PHYSNAME is ignored..
8527
94af9270
KS
8528 The result is allocated on the objfile_obstack and canonicalized. */
8529
8530static const char *
15d034d0
TT
8531dwarf2_compute_name (const char *name,
8532 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
8533 int physname)
8534{
bb5ed363
DE
8535 struct objfile *objfile = cu->objfile;
8536
94af9270
KS
8537 if (name == NULL)
8538 name = dwarf2_name (die, cu);
8539
2ee7123e
DE
8540 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8541 but otherwise compute it by typename_concat inside GDB.
8542 FIXME: Actually this is not really true, or at least not always true.
8543 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8544 Fortran names because there is no mangling standard. So new_symbol_full
8545 will set the demangled name to the result of dwarf2_full_name, and it is
8546 the demangled name that GDB uses if it exists. */
f55ee35c
JK
8547 if (cu->language == language_ada
8548 || (cu->language == language_fortran && physname))
8549 {
8550 /* For Ada unit, we prefer the linkage name over the name, as
8551 the former contains the exported name, which the user expects
8552 to be able to reference. Ideally, we want the user to be able
8553 to reference this entity using either natural or linkage name,
8554 but we haven't started looking at this enhancement yet. */
2ee7123e 8555 const char *linkage_name;
f55ee35c 8556
2ee7123e
DE
8557 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8558 if (linkage_name == NULL)
8559 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8560 if (linkage_name != NULL)
8561 return linkage_name;
f55ee35c
JK
8562 }
8563
94af9270
KS
8564 /* These are the only languages we know how to qualify names in. */
8565 if (name != NULL
9c37b5ae 8566 && (cu->language == language_cplus
c44af4eb
TT
8567 || cu->language == language_fortran || cu->language == language_d
8568 || cu->language == language_rust))
94af9270
KS
8569 {
8570 if (die_needs_namespace (die, cu))
8571 {
8572 long length;
0d5cff50 8573 const char *prefix;
34a68019 8574 const char *canonical_name = NULL;
94af9270 8575
d7e74731
PA
8576 string_file buf;
8577
94af9270 8578 prefix = determine_prefix (die, cu);
94af9270
KS
8579 if (*prefix != '\0')
8580 {
f55ee35c
JK
8581 char *prefixed_name = typename_concat (NULL, prefix, name,
8582 physname, cu);
9a619af0 8583
d7e74731 8584 buf.puts (prefixed_name);
94af9270
KS
8585 xfree (prefixed_name);
8586 }
8587 else
d7e74731 8588 buf.puts (name);
94af9270 8589
98bfdba5
PA
8590 /* Template parameters may be specified in the DIE's DW_AT_name, or
8591 as children with DW_TAG_template_type_param or
8592 DW_TAG_value_type_param. If the latter, add them to the name
8593 here. If the name already has template parameters, then
8594 skip this step; some versions of GCC emit both, and
8595 it is more efficient to use the pre-computed name.
8596
8597 Something to keep in mind about this process: it is very
8598 unlikely, or in some cases downright impossible, to produce
8599 something that will match the mangled name of a function.
8600 If the definition of the function has the same debug info,
8601 we should be able to match up with it anyway. But fallbacks
8602 using the minimal symbol, for instance to find a method
8603 implemented in a stripped copy of libstdc++, will not work.
8604 If we do not have debug info for the definition, we will have to
8605 match them up some other way.
8606
8607 When we do name matching there is a related problem with function
8608 templates; two instantiated function templates are allowed to
8609 differ only by their return types, which we do not add here. */
8610
8611 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8612 {
8613 struct attribute *attr;
8614 struct die_info *child;
8615 int first = 1;
8616
8617 die->building_fullname = 1;
8618
8619 for (child = die->child; child != NULL; child = child->sibling)
8620 {
8621 struct type *type;
12df843f 8622 LONGEST value;
d521ce57 8623 const gdb_byte *bytes;
98bfdba5
PA
8624 struct dwarf2_locexpr_baton *baton;
8625 struct value *v;
8626
8627 if (child->tag != DW_TAG_template_type_param
8628 && child->tag != DW_TAG_template_value_param)
8629 continue;
8630
8631 if (first)
8632 {
d7e74731 8633 buf.puts ("<");
98bfdba5
PA
8634 first = 0;
8635 }
8636 else
d7e74731 8637 buf.puts (", ");
98bfdba5
PA
8638
8639 attr = dwarf2_attr (child, DW_AT_type, cu);
8640 if (attr == NULL)
8641 {
8642 complaint (&symfile_complaints,
8643 _("template parameter missing DW_AT_type"));
d7e74731 8644 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
8645 continue;
8646 }
8647 type = die_type (child, cu);
8648
8649 if (child->tag == DW_TAG_template_type_param)
8650 {
d7e74731 8651 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
8652 continue;
8653 }
8654
8655 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8656 if (attr == NULL)
8657 {
8658 complaint (&symfile_complaints,
3e43a32a
MS
8659 _("template parameter missing "
8660 "DW_AT_const_value"));
d7e74731 8661 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
8662 continue;
8663 }
8664
8665 dwarf2_const_value_attr (attr, type, name,
8666 &cu->comp_unit_obstack, cu,
8667 &value, &bytes, &baton);
8668
8669 if (TYPE_NOSIGN (type))
8670 /* GDB prints characters as NUMBER 'CHAR'. If that's
8671 changed, this can use value_print instead. */
d7e74731 8672 c_printchar (value, type, &buf);
98bfdba5
PA
8673 else
8674 {
8675 struct value_print_options opts;
8676
8677 if (baton != NULL)
8678 v = dwarf2_evaluate_loc_desc (type, NULL,
8679 baton->data,
8680 baton->size,
8681 baton->per_cu);
8682 else if (bytes != NULL)
8683 {
8684 v = allocate_value (type);
8685 memcpy (value_contents_writeable (v), bytes,
8686 TYPE_LENGTH (type));
8687 }
8688 else
8689 v = value_from_longest (type, value);
8690
3e43a32a
MS
8691 /* Specify decimal so that we do not depend on
8692 the radix. */
98bfdba5
PA
8693 get_formatted_print_options (&opts, 'd');
8694 opts.raw = 1;
d7e74731 8695 value_print (v, &buf, &opts);
98bfdba5
PA
8696 release_value (v);
8697 value_free (v);
8698 }
8699 }
8700
8701 die->building_fullname = 0;
8702
8703 if (!first)
8704 {
8705 /* Close the argument list, with a space if necessary
8706 (nested templates). */
d7e74731
PA
8707 if (!buf.empty () && buf.string ().back () == '>')
8708 buf.puts (" >");
98bfdba5 8709 else
d7e74731 8710 buf.puts (">");
98bfdba5
PA
8711 }
8712 }
8713
9c37b5ae 8714 /* For C++ methods, append formal parameter type
94af9270 8715 information, if PHYSNAME. */
6e70227d 8716
94af9270 8717 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 8718 && cu->language == language_cplus)
94af9270
KS
8719 {
8720 struct type *type = read_type_die (die, cu);
8721
d7e74731 8722 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 8723 &type_print_raw_options);
94af9270 8724
9c37b5ae 8725 if (cu->language == language_cplus)
94af9270 8726 {
60430eff
DJ
8727 /* Assume that an artificial first parameter is
8728 "this", but do not crash if it is not. RealView
8729 marks unnamed (and thus unused) parameters as
8730 artificial; there is no way to differentiate
8731 the two cases. */
94af9270
KS
8732 if (TYPE_NFIELDS (type) > 0
8733 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8734 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8735 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8736 0))))
d7e74731 8737 buf.puts (" const");
94af9270
KS
8738 }
8739 }
8740
d7e74731 8741 const std::string &intermediate_name = buf.string ();
94af9270
KS
8742
8743 if (cu->language == language_cplus)
34a68019 8744 canonical_name
322a8516 8745 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
8746 &objfile->per_bfd->storage_obstack);
8747
8748 /* If we only computed INTERMEDIATE_NAME, or if
8749 INTERMEDIATE_NAME is already canonical, then we need to
8750 copy it to the appropriate obstack. */
322a8516 8751 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
8752 name = ((const char *)
8753 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
8754 intermediate_name.c_str (),
8755 intermediate_name.length ()));
34a68019
TT
8756 else
8757 name = canonical_name;
94af9270
KS
8758 }
8759 }
8760
8761 return name;
8762}
8763
0114d602
DJ
8764/* Return the fully qualified name of DIE, based on its DW_AT_name.
8765 If scope qualifiers are appropriate they will be added. The result
34a68019 8766 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
8767 not have a name. NAME may either be from a previous call to
8768 dwarf2_name or NULL.
8769
9c37b5ae 8770 The output string will be canonicalized (if C++). */
0114d602
DJ
8771
8772static const char *
15d034d0 8773dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8774{
94af9270
KS
8775 return dwarf2_compute_name (name, die, cu, 0);
8776}
0114d602 8777
94af9270
KS
8778/* Construct a physname for the given DIE in CU. NAME may either be
8779 from a previous call to dwarf2_name or NULL. The result will be
8780 allocated on the objfile_objstack or NULL if the DIE does not have a
8781 name.
0114d602 8782
9c37b5ae 8783 The output string will be canonicalized (if C++). */
0114d602 8784
94af9270 8785static const char *
15d034d0 8786dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8787{
bb5ed363 8788 struct objfile *objfile = cu->objfile;
900e11f9
JK
8789 const char *retval, *mangled = NULL, *canon = NULL;
8790 struct cleanup *back_to;
8791 int need_copy = 1;
8792
8793 /* In this case dwarf2_compute_name is just a shortcut not building anything
8794 on its own. */
8795 if (!die_needs_namespace (die, cu))
8796 return dwarf2_compute_name (name, die, cu, 1);
8797
8798 back_to = make_cleanup (null_cleanup, NULL);
8799
7d45c7c3
KB
8800 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8801 if (mangled == NULL)
8802 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
900e11f9 8803
e98c9e7c
TT
8804 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8805 See https://github.com/rust-lang/rust/issues/32925. */
8806 if (cu->language == language_rust && mangled != NULL
8807 && strchr (mangled, '{') != NULL)
8808 mangled = NULL;
8809
900e11f9
JK
8810 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8811 has computed. */
7d45c7c3 8812 if (mangled != NULL)
900e11f9
JK
8813 {
8814 char *demangled;
8815
900e11f9
JK
8816 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8817 type. It is easier for GDB users to search for such functions as
8818 `name(params)' than `long name(params)'. In such case the minimal
8819 symbol names do not match the full symbol names but for template
8820 functions there is never a need to look up their definition from their
8821 declaration so the only disadvantage remains the minimal symbol
8822 variant `long name(params)' does not have the proper inferior type.
8823 */
8824
a766d390
DE
8825 if (cu->language == language_go)
8826 {
8827 /* This is a lie, but we already lie to the caller new_symbol_full.
8828 new_symbol_full assumes we return the mangled name.
8829 This just undoes that lie until things are cleaned up. */
8830 demangled = NULL;
8831 }
8832 else
8833 {
8de20a37 8834 demangled = gdb_demangle (mangled,
9c37b5ae 8835 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
a766d390 8836 }
900e11f9
JK
8837 if (demangled)
8838 {
8839 make_cleanup (xfree, demangled);
8840 canon = demangled;
8841 }
8842 else
8843 {
8844 canon = mangled;
8845 need_copy = 0;
8846 }
8847 }
8848
8849 if (canon == NULL || check_physname)
8850 {
8851 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8852
8853 if (canon != NULL && strcmp (physname, canon) != 0)
8854 {
8855 /* It may not mean a bug in GDB. The compiler could also
8856 compute DW_AT_linkage_name incorrectly. But in such case
8857 GDB would need to be bug-to-bug compatible. */
8858
8859 complaint (&symfile_complaints,
8860 _("Computed physname <%s> does not match demangled <%s> "
8861 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
4262abfb
JK
8862 physname, canon, mangled, die->offset.sect_off,
8863 objfile_name (objfile));
900e11f9
JK
8864
8865 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8866 is available here - over computed PHYSNAME. It is safer
8867 against both buggy GDB and buggy compilers. */
8868
8869 retval = canon;
8870 }
8871 else
8872 {
8873 retval = physname;
8874 need_copy = 0;
8875 }
8876 }
8877 else
8878 retval = canon;
8879
8880 if (need_copy)
224c3ddb
SM
8881 retval = ((const char *)
8882 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8883 retval, strlen (retval)));
900e11f9
JK
8884
8885 do_cleanups (back_to);
8886 return retval;
0114d602
DJ
8887}
8888
74921315
KS
8889/* Inspect DIE in CU for a namespace alias. If one exists, record
8890 a new symbol for it.
8891
8892 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8893
8894static int
8895read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8896{
8897 struct attribute *attr;
8898
8899 /* If the die does not have a name, this is not a namespace
8900 alias. */
8901 attr = dwarf2_attr (die, DW_AT_name, cu);
8902 if (attr != NULL)
8903 {
8904 int num;
8905 struct die_info *d = die;
8906 struct dwarf2_cu *imported_cu = cu;
8907
8908 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8909 keep inspecting DIEs until we hit the underlying import. */
8910#define MAX_NESTED_IMPORTED_DECLARATIONS 100
8911 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8912 {
8913 attr = dwarf2_attr (d, DW_AT_import, cu);
8914 if (attr == NULL)
8915 break;
8916
8917 d = follow_die_ref (d, attr, &imported_cu);
8918 if (d->tag != DW_TAG_imported_declaration)
8919 break;
8920 }
8921
8922 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8923 {
8924 complaint (&symfile_complaints,
8925 _("DIE at 0x%x has too many recursively imported "
8926 "declarations"), d->offset.sect_off);
8927 return 0;
8928 }
8929
8930 if (attr != NULL)
8931 {
8932 struct type *type;
8933 sect_offset offset = dwarf2_get_ref_die_offset (attr);
8934
8935 type = get_die_type_at_offset (offset, cu->per_cu);
8936 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
8937 {
8938 /* This declaration is a global namespace alias. Add
8939 a symbol for it whose type is the aliased namespace. */
8940 new_symbol (die, type, cu);
8941 return 1;
8942 }
8943 }
8944 }
8945
8946 return 0;
8947}
8948
22cee43f
PMR
8949/* Return the using directives repository (global or local?) to use in the
8950 current context for LANGUAGE.
8951
8952 For Ada, imported declarations can materialize renamings, which *may* be
8953 global. However it is impossible (for now?) in DWARF to distinguish
8954 "external" imported declarations and "static" ones. As all imported
8955 declarations seem to be static in all other languages, make them all CU-wide
8956 global only in Ada. */
8957
8958static struct using_direct **
8959using_directives (enum language language)
8960{
8961 if (language == language_ada && context_stack_depth == 0)
8962 return &global_using_directives;
8963 else
8964 return &local_using_directives;
8965}
8966
27aa8d6a
SW
8967/* Read the import statement specified by the given die and record it. */
8968
8969static void
8970read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8971{
bb5ed363 8972 struct objfile *objfile = cu->objfile;
27aa8d6a 8973 struct attribute *import_attr;
32019081 8974 struct die_info *imported_die, *child_die;
de4affc9 8975 struct dwarf2_cu *imported_cu;
27aa8d6a 8976 const char *imported_name;
794684b6 8977 const char *imported_name_prefix;
13387711
SW
8978 const char *canonical_name;
8979 const char *import_alias;
8980 const char *imported_declaration = NULL;
794684b6 8981 const char *import_prefix;
32019081
JK
8982 VEC (const_char_ptr) *excludes = NULL;
8983 struct cleanup *cleanups;
13387711 8984
27aa8d6a
SW
8985 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8986 if (import_attr == NULL)
8987 {
8988 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8989 dwarf_tag_name (die->tag));
8990 return;
8991 }
8992
de4affc9
CC
8993 imported_cu = cu;
8994 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8995 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8996 if (imported_name == NULL)
8997 {
8998 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8999
9000 The import in the following code:
9001 namespace A
9002 {
9003 typedef int B;
9004 }
9005
9006 int main ()
9007 {
9008 using A::B;
9009 B b;
9010 return b;
9011 }
9012
9013 ...
9014 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9015 <52> DW_AT_decl_file : 1
9016 <53> DW_AT_decl_line : 6
9017 <54> DW_AT_import : <0x75>
9018 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9019 <59> DW_AT_name : B
9020 <5b> DW_AT_decl_file : 1
9021 <5c> DW_AT_decl_line : 2
9022 <5d> DW_AT_type : <0x6e>
9023 ...
9024 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9025 <76> DW_AT_byte_size : 4
9026 <77> DW_AT_encoding : 5 (signed)
9027
9028 imports the wrong die ( 0x75 instead of 0x58 ).
9029 This case will be ignored until the gcc bug is fixed. */
9030 return;
9031 }
9032
82856980
SW
9033 /* Figure out the local name after import. */
9034 import_alias = dwarf2_name (die, cu);
27aa8d6a 9035
794684b6
SW
9036 /* Figure out where the statement is being imported to. */
9037 import_prefix = determine_prefix (die, cu);
9038
9039 /* Figure out what the scope of the imported die is and prepend it
9040 to the name of the imported die. */
de4affc9 9041 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 9042
f55ee35c
JK
9043 if (imported_die->tag != DW_TAG_namespace
9044 && imported_die->tag != DW_TAG_module)
794684b6 9045 {
13387711
SW
9046 imported_declaration = imported_name;
9047 canonical_name = imported_name_prefix;
794684b6 9048 }
13387711 9049 else if (strlen (imported_name_prefix) > 0)
12aaed36 9050 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
9051 imported_name_prefix,
9052 (cu->language == language_d ? "." : "::"),
9053 imported_name, (char *) NULL);
13387711
SW
9054 else
9055 canonical_name = imported_name;
794684b6 9056
32019081
JK
9057 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9058
9059 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9060 for (child_die = die->child; child_die && child_die->tag;
9061 child_die = sibling_die (child_die))
9062 {
9063 /* DWARF-4: A Fortran use statement with a “rename list” may be
9064 represented by an imported module entry with an import attribute
9065 referring to the module and owned entries corresponding to those
9066 entities that are renamed as part of being imported. */
9067
9068 if (child_die->tag != DW_TAG_imported_declaration)
9069 {
9070 complaint (&symfile_complaints,
9071 _("child DW_TAG_imported_declaration expected "
9072 "- DIE at 0x%x [in module %s]"),
4262abfb 9073 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9074 continue;
9075 }
9076
9077 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9078 if (import_attr == NULL)
9079 {
9080 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9081 dwarf_tag_name (child_die->tag));
9082 continue;
9083 }
9084
9085 imported_cu = cu;
9086 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9087 &imported_cu);
9088 imported_name = dwarf2_name (imported_die, imported_cu);
9089 if (imported_name == NULL)
9090 {
9091 complaint (&symfile_complaints,
9092 _("child DW_TAG_imported_declaration has unknown "
9093 "imported name - DIE at 0x%x [in module %s]"),
4262abfb 9094 child_die->offset.sect_off, objfile_name (objfile));
32019081
JK
9095 continue;
9096 }
9097
9098 VEC_safe_push (const_char_ptr, excludes, imported_name);
9099
9100 process_die (child_die, cu);
9101 }
9102
22cee43f
PMR
9103 add_using_directive (using_directives (cu->language),
9104 import_prefix,
9105 canonical_name,
9106 import_alias,
9107 imported_declaration,
9108 excludes,
9109 0,
9110 &objfile->objfile_obstack);
32019081
JK
9111
9112 do_cleanups (cleanups);
27aa8d6a
SW
9113}
9114
f4dc4d17 9115/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 9116
cb1df416
DJ
9117static void
9118free_cu_line_header (void *arg)
9119{
9a3c8263 9120 struct dwarf2_cu *cu = (struct dwarf2_cu *) arg;
cb1df416
DJ
9121
9122 free_line_header (cu->line_header);
9123 cu->line_header = NULL;
9124}
9125
1b80a9fa
JK
9126/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9127 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9128 this, it was first present in GCC release 4.3.0. */
9129
9130static int
9131producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9132{
9133 if (!cu->checked_producer)
9134 check_producer (cu);
9135
9136 return cu->producer_is_gcc_lt_4_3;
9137}
9138
d721ba37
PA
9139static file_and_directory
9140find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 9141{
d721ba37
PA
9142 file_and_directory res;
9143
9291a0cd
TT
9144 /* Find the filename. Do not use dwarf2_name here, since the filename
9145 is not a source language identifier. */
d721ba37
PA
9146 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9147 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 9148
d721ba37
PA
9149 if (res.comp_dir == NULL
9150 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9151 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 9152 {
d721ba37
PA
9153 res.comp_dir_storage = ldirname (res.name);
9154 if (!res.comp_dir_storage.empty ())
9155 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 9156 }
d721ba37 9157 if (res.comp_dir != NULL)
9291a0cd
TT
9158 {
9159 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9160 directory, get rid of it. */
d721ba37 9161 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 9162
d721ba37
PA
9163 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9164 res.comp_dir = cp + 1;
9291a0cd
TT
9165 }
9166
d721ba37
PA
9167 if (res.name == NULL)
9168 res.name = "<unknown>";
9169
9170 return res;
9291a0cd
TT
9171}
9172
f4dc4d17
DE
9173/* Handle DW_AT_stmt_list for a compilation unit.
9174 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
9175 COMP_DIR is the compilation directory. LOWPC is passed to
9176 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
9177
9178static void
9179handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 9180 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 9181{
527f3840 9182 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 9183 struct attribute *attr;
527f3840
JK
9184 unsigned int line_offset;
9185 struct line_header line_header_local;
9186 hashval_t line_header_local_hash;
9187 unsigned u;
9188 void **slot;
9189 int decode_mapping;
2ab95328 9190
f4dc4d17
DE
9191 gdb_assert (! cu->per_cu->is_debug_types);
9192
2ab95328 9193 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
9194 if (attr == NULL)
9195 return;
9196
9197 line_offset = DW_UNSND (attr);
9198
9199 /* The line header hash table is only created if needed (it exists to
9200 prevent redundant reading of the line table for partial_units).
9201 If we're given a partial_unit, we'll need it. If we're given a
9202 compile_unit, then use the line header hash table if it's already
9203 created, but don't create one just yet. */
9204
9205 if (dwarf2_per_objfile->line_header_hash == NULL
9206 && die->tag == DW_TAG_partial_unit)
2ab95328 9207 {
527f3840
JK
9208 dwarf2_per_objfile->line_header_hash
9209 = htab_create_alloc_ex (127, line_header_hash_voidp,
9210 line_header_eq_voidp,
9211 free_line_header_voidp,
9212 &objfile->objfile_obstack,
9213 hashtab_obstack_allocate,
9214 dummy_obstack_deallocate);
9215 }
2ab95328 9216
527f3840
JK
9217 line_header_local.offset.sect_off = line_offset;
9218 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9219 line_header_local_hash = line_header_hash (&line_header_local);
9220 if (dwarf2_per_objfile->line_header_hash != NULL)
9221 {
9222 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9223 &line_header_local,
9224 line_header_local_hash, NO_INSERT);
9225
9226 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9227 is not present in *SLOT (since if there is something in *SLOT then
9228 it will be for a partial_unit). */
9229 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 9230 {
527f3840 9231 gdb_assert (*slot != NULL);
9a3c8263 9232 cu->line_header = (struct line_header *) *slot;
527f3840 9233 return;
dee91e82 9234 }
2ab95328 9235 }
527f3840
JK
9236
9237 /* dwarf_decode_line_header does not yet provide sufficient information.
9238 We always have to call also dwarf_decode_lines for it. */
9239 cu->line_header = dwarf_decode_line_header (line_offset, cu);
9240 if (cu->line_header == NULL)
9241 return;
9242
9243 if (dwarf2_per_objfile->line_header_hash == NULL)
9244 slot = NULL;
9245 else
9246 {
9247 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9248 &line_header_local,
9249 line_header_local_hash, INSERT);
9250 gdb_assert (slot != NULL);
9251 }
9252 if (slot != NULL && *slot == NULL)
9253 {
9254 /* This newly decoded line number information unit will be owned
9255 by line_header_hash hash table. */
9256 *slot = cu->line_header;
9257 }
9258 else
9259 {
9260 /* We cannot free any current entry in (*slot) as that struct line_header
9261 may be already used by multiple CUs. Create only temporary decoded
9262 line_header for this CU - it may happen at most once for each line
9263 number information unit. And if we're not using line_header_hash
9264 then this is what we want as well. */
9265 gdb_assert (die->tag != DW_TAG_partial_unit);
9266 make_cleanup (free_cu_line_header, cu);
9267 }
9268 decode_mapping = (die->tag != DW_TAG_partial_unit);
9269 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9270 decode_mapping);
2ab95328
TT
9271}
9272
95554aad 9273/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 9274
c906108c 9275static void
e7c27a73 9276read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9277{
dee91e82 9278 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9279 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 9280 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
9281 CORE_ADDR highpc = ((CORE_ADDR) 0);
9282 struct attribute *attr;
c906108c 9283 struct die_info *child_die;
e142c38c 9284 CORE_ADDR baseaddr;
6e70227d 9285
e142c38c 9286 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9287
fae299cd 9288 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
9289
9290 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9291 from finish_block. */
2acceee2 9292 if (lowpc == ((CORE_ADDR) -1))
c906108c 9293 lowpc = highpc;
3e29f34a 9294 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 9295
d721ba37 9296 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 9297
95554aad 9298 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 9299
f4b8a18d
KW
9300 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9301 standardised yet. As a workaround for the language detection we fall
9302 back to the DW_AT_producer string. */
9303 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9304 cu->language = language_opencl;
9305
3019eac3
DE
9306 /* Similar hack for Go. */
9307 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9308 set_cu_language (DW_LANG_Go, cu);
9309
d721ba37 9310 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
9311
9312 /* Decode line number information if present. We do this before
9313 processing child DIEs, so that the line header table is available
9314 for DW_AT_decl_file. */
d721ba37 9315 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
9316
9317 /* Process all dies in compilation unit. */
9318 if (die->child != NULL)
9319 {
9320 child_die = die->child;
9321 while (child_die && child_die->tag)
9322 {
9323 process_die (child_die, cu);
9324 child_die = sibling_die (child_die);
9325 }
9326 }
9327
9328 /* Decode macro information, if present. Dwarf 2 macro information
9329 refers to information in the line number info statement program
9330 header, so we can only read it if we've read the header
9331 successfully. */
0af92d60
JK
9332 attr = dwarf2_attr (die, DW_AT_macros, cu);
9333 if (attr == NULL)
9334 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
9335 if (attr && cu->line_header)
9336 {
9337 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9338 complaint (&symfile_complaints,
0af92d60 9339 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 9340
43f3e411 9341 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
9342 }
9343 else
9344 {
9345 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9346 if (attr && cu->line_header)
9347 {
9348 unsigned int macro_offset = DW_UNSND (attr);
9349
43f3e411 9350 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
9351 }
9352 }
3019eac3
DE
9353}
9354
f4dc4d17
DE
9355/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9356 Create the set of symtabs used by this TU, or if this TU is sharing
9357 symtabs with another TU and the symtabs have already been created
9358 then restore those symtabs in the line header.
9359 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
9360
9361static void
f4dc4d17 9362setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 9363{
f4dc4d17
DE
9364 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9365 struct type_unit_group *tu_group;
9366 int first_time;
9367 struct line_header *lh;
3019eac3 9368 struct attribute *attr;
f4dc4d17 9369 unsigned int i, line_offset;
0186c6a7 9370 struct signatured_type *sig_type;
3019eac3 9371
f4dc4d17 9372 gdb_assert (per_cu->is_debug_types);
0186c6a7 9373 sig_type = (struct signatured_type *) per_cu;
3019eac3 9374
f4dc4d17 9375 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 9376
f4dc4d17 9377 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 9378 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
9379 if (sig_type->type_unit_group == NULL)
9380 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9381 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
9382
9383 /* If we've already processed this stmt_list there's no real need to
9384 do it again, we could fake it and just recreate the part we need
9385 (file name,index -> symtab mapping). If data shows this optimization
9386 is useful we can do it then. */
43f3e411 9387 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
9388
9389 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9390 debug info. */
9391 lh = NULL;
9392 if (attr != NULL)
3019eac3 9393 {
f4dc4d17
DE
9394 line_offset = DW_UNSND (attr);
9395 lh = dwarf_decode_line_header (line_offset, cu);
9396 }
9397 if (lh == NULL)
9398 {
9399 if (first_time)
9400 dwarf2_start_symtab (cu, "", NULL, 0);
9401 else
9402 {
9403 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 9404 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 9405 }
f4dc4d17 9406 return;
3019eac3
DE
9407 }
9408
f4dc4d17
DE
9409 cu->line_header = lh;
9410 make_cleanup (free_cu_line_header, cu);
3019eac3 9411
f4dc4d17
DE
9412 if (first_time)
9413 {
43f3e411 9414 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 9415
1fd60fc0
DE
9416 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9417 still initializing it, and our caller (a few levels up)
9418 process_full_type_unit still needs to know if this is the first
9419 time. */
9420
f4dc4d17
DE
9421 tu_group->num_symtabs = lh->num_file_names;
9422 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 9423
f4dc4d17
DE
9424 for (i = 0; i < lh->num_file_names; ++i)
9425 {
d521ce57 9426 const char *dir = NULL;
f4dc4d17 9427 struct file_entry *fe = &lh->file_names[i];
3019eac3 9428
568c1b9f
PB
9429 if (fe->dir_index && lh->include_dirs != NULL
9430 && (fe->dir_index - 1) < lh->num_include_dirs)
f4dc4d17 9431 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 9432 dwarf2_start_subfile (fe->name, dir);
3019eac3 9433
f4dc4d17
DE
9434 if (current_subfile->symtab == NULL)
9435 {
9436 /* NOTE: start_subfile will recognize when it's been passed
9437 a file it has already seen. So we can't assume there's a
43f3e411 9438 simple mapping from lh->file_names to subfiles, plus
f4dc4d17 9439 lh->file_names may contain dups. */
43f3e411
DE
9440 current_subfile->symtab
9441 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
9442 }
9443
9444 fe->symtab = current_subfile->symtab;
9445 tu_group->symtabs[i] = fe->symtab;
9446 }
9447 }
9448 else
3019eac3 9449 {
0ab9ce85 9450 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17
DE
9451
9452 for (i = 0; i < lh->num_file_names; ++i)
9453 {
9454 struct file_entry *fe = &lh->file_names[i];
9455
9456 fe->symtab = tu_group->symtabs[i];
9457 }
3019eac3
DE
9458 }
9459
f4dc4d17
DE
9460 /* The main symtab is allocated last. Type units don't have DW_AT_name
9461 so they don't have a "real" (so to speak) symtab anyway.
9462 There is later code that will assign the main symtab to all symbols
9463 that don't have one. We need to handle the case of a symbol with a
9464 missing symtab (DW_AT_decl_file) anyway. */
9465}
3019eac3 9466
f4dc4d17
DE
9467/* Process DW_TAG_type_unit.
9468 For TUs we want to skip the first top level sibling if it's not the
9469 actual type being defined by this TU. In this case the first top
9470 level sibling is there to provide context only. */
3019eac3 9471
f4dc4d17
DE
9472static void
9473read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9474{
9475 struct die_info *child_die;
3019eac3 9476
f4dc4d17
DE
9477 prepare_one_comp_unit (cu, die, language_minimal);
9478
9479 /* Initialize (or reinitialize) the machinery for building symtabs.
9480 We do this before processing child DIEs, so that the line header table
9481 is available for DW_AT_decl_file. */
9482 setup_type_unit_groups (die, cu);
9483
9484 if (die->child != NULL)
9485 {
9486 child_die = die->child;
9487 while (child_die && child_die->tag)
9488 {
9489 process_die (child_die, cu);
9490 child_die = sibling_die (child_die);
9491 }
9492 }
3019eac3
DE
9493}
9494\f
80626a55
DE
9495/* DWO/DWP files.
9496
9497 http://gcc.gnu.org/wiki/DebugFission
9498 http://gcc.gnu.org/wiki/DebugFissionDWP
9499
9500 To simplify handling of both DWO files ("object" files with the DWARF info)
9501 and DWP files (a file with the DWOs packaged up into one file), we treat
9502 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
9503
9504static hashval_t
9505hash_dwo_file (const void *item)
9506{
9a3c8263 9507 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 9508 hashval_t hash;
3019eac3 9509
a2ce51a0
DE
9510 hash = htab_hash_string (dwo_file->dwo_name);
9511 if (dwo_file->comp_dir != NULL)
9512 hash += htab_hash_string (dwo_file->comp_dir);
9513 return hash;
3019eac3
DE
9514}
9515
9516static int
9517eq_dwo_file (const void *item_lhs, const void *item_rhs)
9518{
9a3c8263
SM
9519 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9520 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 9521
a2ce51a0
DE
9522 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9523 return 0;
9524 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9525 return lhs->comp_dir == rhs->comp_dir;
9526 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
9527}
9528
9529/* Allocate a hash table for DWO files. */
9530
9531static htab_t
9532allocate_dwo_file_hash_table (void)
9533{
9534 struct objfile *objfile = dwarf2_per_objfile->objfile;
9535
9536 return htab_create_alloc_ex (41,
9537 hash_dwo_file,
9538 eq_dwo_file,
9539 NULL,
9540 &objfile->objfile_obstack,
9541 hashtab_obstack_allocate,
9542 dummy_obstack_deallocate);
9543}
9544
80626a55
DE
9545/* Lookup DWO file DWO_NAME. */
9546
9547static void **
0ac5b59e 9548lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
9549{
9550 struct dwo_file find_entry;
9551 void **slot;
9552
9553 if (dwarf2_per_objfile->dwo_files == NULL)
9554 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9555
9556 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
9557 find_entry.dwo_name = dwo_name;
9558 find_entry.comp_dir = comp_dir;
80626a55
DE
9559 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9560
9561 return slot;
9562}
9563
3019eac3
DE
9564static hashval_t
9565hash_dwo_unit (const void *item)
9566{
9a3c8263 9567 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
9568
9569 /* This drops the top 32 bits of the id, but is ok for a hash. */
9570 return dwo_unit->signature;
9571}
9572
9573static int
9574eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9575{
9a3c8263
SM
9576 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9577 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
9578
9579 /* The signature is assumed to be unique within the DWO file.
9580 So while object file CU dwo_id's always have the value zero,
9581 that's OK, assuming each object file DWO file has only one CU,
9582 and that's the rule for now. */
9583 return lhs->signature == rhs->signature;
9584}
9585
9586/* Allocate a hash table for DWO CUs,TUs.
9587 There is one of these tables for each of CUs,TUs for each DWO file. */
9588
9589static htab_t
9590allocate_dwo_unit_table (struct objfile *objfile)
9591{
9592 /* Start out with a pretty small number.
9593 Generally DWO files contain only one CU and maybe some TUs. */
9594 return htab_create_alloc_ex (3,
9595 hash_dwo_unit,
9596 eq_dwo_unit,
9597 NULL,
9598 &objfile->objfile_obstack,
9599 hashtab_obstack_allocate,
9600 dummy_obstack_deallocate);
9601}
9602
80626a55 9603/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 9604
19c3d4c9 9605struct create_dwo_cu_data
3019eac3
DE
9606{
9607 struct dwo_file *dwo_file;
19c3d4c9 9608 struct dwo_unit dwo_unit;
3019eac3
DE
9609};
9610
19c3d4c9 9611/* die_reader_func for create_dwo_cu. */
3019eac3
DE
9612
9613static void
19c3d4c9
DE
9614create_dwo_cu_reader (const struct die_reader_specs *reader,
9615 const gdb_byte *info_ptr,
9616 struct die_info *comp_unit_die,
9617 int has_children,
9618 void *datap)
3019eac3
DE
9619{
9620 struct dwarf2_cu *cu = reader->cu;
3019eac3 9621 sect_offset offset = cu->per_cu->offset;
8a0459fd 9622 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 9623 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 9624 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 9625 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 9626 struct attribute *attr;
3019eac3
DE
9627
9628 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9629 if (attr == NULL)
9630 {
19c3d4c9
DE
9631 complaint (&symfile_complaints,
9632 _("Dwarf Error: debug entry at offset 0x%x is missing"
9633 " its dwo_id [in module %s]"),
9634 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
9635 return;
9636 }
9637
3019eac3
DE
9638 dwo_unit->dwo_file = dwo_file;
9639 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 9640 dwo_unit->section = section;
3019eac3
DE
9641 dwo_unit->offset = offset;
9642 dwo_unit->length = cu->per_cu->length;
9643
b4f54984 9644 if (dwarf_read_debug)
4031ecc5
DE
9645 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9646 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
9647}
9648
19c3d4c9
DE
9649/* Create the dwo_unit for the lone CU in DWO_FILE.
9650 Note: This function processes DWO files only, not DWP files. */
3019eac3 9651
19c3d4c9
DE
9652static struct dwo_unit *
9653create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
9654{
9655 struct objfile *objfile = dwarf2_per_objfile->objfile;
9656 struct dwarf2_section_info *section = &dwo_file->sections.info;
d521ce57 9657 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
9658 struct create_dwo_cu_data create_dwo_cu_data;
9659 struct dwo_unit *dwo_unit;
3019eac3
DE
9660
9661 dwarf2_read_section (objfile, section);
9662 info_ptr = section->buffer;
9663
9664 if (info_ptr == NULL)
9665 return NULL;
9666
b4f54984 9667 if (dwarf_read_debug)
19c3d4c9
DE
9668 {
9669 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
a32a8923
DE
9670 get_section_name (section),
9671 get_section_file_name (section));
19c3d4c9 9672 }
3019eac3 9673
19c3d4c9
DE
9674 create_dwo_cu_data.dwo_file = dwo_file;
9675 dwo_unit = NULL;
3019eac3
DE
9676
9677 end_ptr = info_ptr + section->size;
9678 while (info_ptr < end_ptr)
9679 {
9680 struct dwarf2_per_cu_data per_cu;
9681
19c3d4c9
DE
9682 memset (&create_dwo_cu_data.dwo_unit, 0,
9683 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
9684 memset (&per_cu, 0, sizeof (per_cu));
9685 per_cu.objfile = objfile;
9686 per_cu.is_debug_types = 0;
9687 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 9688 per_cu.section = section;
3019eac3 9689
33e80786 9690 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
19c3d4c9
DE
9691 create_dwo_cu_reader,
9692 &create_dwo_cu_data);
9693
9694 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9695 {
9696 /* If we've already found one, complain. We only support one
9697 because having more than one requires hacking the dwo_name of
9698 each to match, which is highly unlikely to happen. */
9699 if (dwo_unit != NULL)
9700 {
9701 complaint (&symfile_complaints,
9702 _("Multiple CUs in DWO file %s [in module %s]"),
4262abfb 9703 dwo_file->dwo_name, objfile_name (objfile));
19c3d4c9
DE
9704 break;
9705 }
9706
9707 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9708 *dwo_unit = create_dwo_cu_data.dwo_unit;
9709 }
3019eac3
DE
9710
9711 info_ptr += per_cu.length;
9712 }
9713
19c3d4c9 9714 return dwo_unit;
3019eac3
DE
9715}
9716
80626a55
DE
9717/* DWP file .debug_{cu,tu}_index section format:
9718 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9719
d2415c6c
DE
9720 DWP Version 1:
9721
80626a55
DE
9722 Both index sections have the same format, and serve to map a 64-bit
9723 signature to a set of section numbers. Each section begins with a header,
9724 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9725 indexes, and a pool of 32-bit section numbers. The index sections will be
9726 aligned at 8-byte boundaries in the file.
9727
d2415c6c
DE
9728 The index section header consists of:
9729
9730 V, 32 bit version number
9731 -, 32 bits unused
9732 N, 32 bit number of compilation units or type units in the index
9733 M, 32 bit number of slots in the hash table
80626a55 9734
d2415c6c 9735 Numbers are recorded using the byte order of the application binary.
80626a55 9736
d2415c6c
DE
9737 The hash table begins at offset 16 in the section, and consists of an array
9738 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9739 order of the application binary). Unused slots in the hash table are 0.
9740 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 9741
d2415c6c
DE
9742 The parallel table begins immediately after the hash table
9743 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9744 array of 32-bit indexes (using the byte order of the application binary),
9745 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9746 table contains a 32-bit index into the pool of section numbers. For unused
9747 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 9748
73869dc2
DE
9749 The pool of section numbers begins immediately following the hash table
9750 (at offset 16 + 12 * M from the beginning of the section). The pool of
9751 section numbers consists of an array of 32-bit words (using the byte order
9752 of the application binary). Each item in the array is indexed starting
9753 from 0. The hash table entry provides the index of the first section
9754 number in the set. Additional section numbers in the set follow, and the
9755 set is terminated by a 0 entry (section number 0 is not used in ELF).
9756
9757 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9758 section must be the first entry in the set, and the .debug_abbrev.dwo must
9759 be the second entry. Other members of the set may follow in any order.
9760
9761 ---
9762
9763 DWP Version 2:
9764
9765 DWP Version 2 combines all the .debug_info, etc. sections into one,
9766 and the entries in the index tables are now offsets into these sections.
9767 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9768 section.
9769
9770 Index Section Contents:
9771 Header
9772 Hash Table of Signatures dwp_hash_table.hash_table
9773 Parallel Table of Indices dwp_hash_table.unit_table
9774 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9775 Table of Section Sizes dwp_hash_table.v2.sizes
9776
9777 The index section header consists of:
9778
9779 V, 32 bit version number
9780 L, 32 bit number of columns in the table of section offsets
9781 N, 32 bit number of compilation units or type units in the index
9782 M, 32 bit number of slots in the hash table
9783
9784 Numbers are recorded using the byte order of the application binary.
9785
9786 The hash table has the same format as version 1.
9787 The parallel table of indices has the same format as version 1,
9788 except that the entries are origin-1 indices into the table of sections
9789 offsets and the table of section sizes.
9790
9791 The table of offsets begins immediately following the parallel table
9792 (at offset 16 + 12 * M from the beginning of the section). The table is
9793 a two-dimensional array of 32-bit words (using the byte order of the
9794 application binary), with L columns and N+1 rows, in row-major order.
9795 Each row in the array is indexed starting from 0. The first row provides
9796 a key to the remaining rows: each column in this row provides an identifier
9797 for a debug section, and the offsets in the same column of subsequent rows
9798 refer to that section. The section identifiers are:
9799
9800 DW_SECT_INFO 1 .debug_info.dwo
9801 DW_SECT_TYPES 2 .debug_types.dwo
9802 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9803 DW_SECT_LINE 4 .debug_line.dwo
9804 DW_SECT_LOC 5 .debug_loc.dwo
9805 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9806 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9807 DW_SECT_MACRO 8 .debug_macro.dwo
9808
9809 The offsets provided by the CU and TU index sections are the base offsets
9810 for the contributions made by each CU or TU to the corresponding section
9811 in the package file. Each CU and TU header contains an abbrev_offset
9812 field, used to find the abbreviations table for that CU or TU within the
9813 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9814 be interpreted as relative to the base offset given in the index section.
9815 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9816 should be interpreted as relative to the base offset for .debug_line.dwo,
9817 and offsets into other debug sections obtained from DWARF attributes should
9818 also be interpreted as relative to the corresponding base offset.
9819
9820 The table of sizes begins immediately following the table of offsets.
9821 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9822 with L columns and N rows, in row-major order. Each row in the array is
9823 indexed starting from 1 (row 0 is shared by the two tables).
9824
9825 ---
9826
9827 Hash table lookup is handled the same in version 1 and 2:
9828
9829 We assume that N and M will not exceed 2^32 - 1.
9830 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9831
d2415c6c
DE
9832 Given a 64-bit compilation unit signature or a type signature S, an entry
9833 in the hash table is located as follows:
80626a55 9834
d2415c6c
DE
9835 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9836 the low-order k bits all set to 1.
80626a55 9837
d2415c6c 9838 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 9839
d2415c6c
DE
9840 3) If the hash table entry at index H matches the signature, use that
9841 entry. If the hash table entry at index H is unused (all zeroes),
9842 terminate the search: the signature is not present in the table.
80626a55 9843
d2415c6c 9844 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 9845
d2415c6c 9846 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 9847 to stop at an unused slot or find the match. */
80626a55
DE
9848
9849/* Create a hash table to map DWO IDs to their CU/TU entry in
9850 .debug_{info,types}.dwo in DWP_FILE.
9851 Returns NULL if there isn't one.
9852 Note: This function processes DWP files only, not DWO files. */
9853
9854static struct dwp_hash_table *
9855create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9856{
9857 struct objfile *objfile = dwarf2_per_objfile->objfile;
9858 bfd *dbfd = dwp_file->dbfd;
948f8e3d 9859 const gdb_byte *index_ptr, *index_end;
80626a55 9860 struct dwarf2_section_info *index;
73869dc2 9861 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
9862 struct dwp_hash_table *htab;
9863
9864 if (is_debug_types)
9865 index = &dwp_file->sections.tu_index;
9866 else
9867 index = &dwp_file->sections.cu_index;
9868
9869 if (dwarf2_section_empty_p (index))
9870 return NULL;
9871 dwarf2_read_section (objfile, index);
9872
9873 index_ptr = index->buffer;
9874 index_end = index_ptr + index->size;
9875
9876 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
9877 index_ptr += 4;
9878 if (version == 2)
9879 nr_columns = read_4_bytes (dbfd, index_ptr);
9880 else
9881 nr_columns = 0;
9882 index_ptr += 4;
80626a55
DE
9883 nr_units = read_4_bytes (dbfd, index_ptr);
9884 index_ptr += 4;
9885 nr_slots = read_4_bytes (dbfd, index_ptr);
9886 index_ptr += 4;
9887
73869dc2 9888 if (version != 1 && version != 2)
80626a55 9889 {
21aa081e 9890 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9891 " [in module %s]"),
21aa081e 9892 pulongest (version), dwp_file->name);
80626a55
DE
9893 }
9894 if (nr_slots != (nr_slots & -nr_slots))
9895 {
21aa081e 9896 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9897 " is not power of 2 [in module %s]"),
21aa081e 9898 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9899 }
9900
9901 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
9902 htab->version = version;
9903 htab->nr_columns = nr_columns;
80626a55
DE
9904 htab->nr_units = nr_units;
9905 htab->nr_slots = nr_slots;
9906 htab->hash_table = index_ptr;
9907 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
9908
9909 /* Exit early if the table is empty. */
9910 if (nr_slots == 0 || nr_units == 0
9911 || (version == 2 && nr_columns == 0))
9912 {
9913 /* All must be zero. */
9914 if (nr_slots != 0 || nr_units != 0
9915 || (version == 2 && nr_columns != 0))
9916 {
9917 complaint (&symfile_complaints,
9918 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9919 " all zero [in modules %s]"),
9920 dwp_file->name);
9921 }
9922 return htab;
9923 }
9924
9925 if (version == 1)
9926 {
9927 htab->section_pool.v1.indices =
9928 htab->unit_table + sizeof (uint32_t) * nr_slots;
9929 /* It's harder to decide whether the section is too small in v1.
9930 V1 is deprecated anyway so we punt. */
9931 }
9932 else
9933 {
9934 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9935 int *ids = htab->section_pool.v2.section_ids;
9936 /* Reverse map for error checking. */
9937 int ids_seen[DW_SECT_MAX + 1];
9938 int i;
9939
9940 if (nr_columns < 2)
9941 {
9942 error (_("Dwarf Error: bad DWP hash table, too few columns"
9943 " in section table [in module %s]"),
9944 dwp_file->name);
9945 }
9946 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
9947 {
9948 error (_("Dwarf Error: bad DWP hash table, too many columns"
9949 " in section table [in module %s]"),
9950 dwp_file->name);
9951 }
9952 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9953 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
9954 for (i = 0; i < nr_columns; ++i)
9955 {
9956 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
9957
9958 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
9959 {
9960 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
9961 " in section table [in module %s]"),
9962 id, dwp_file->name);
9963 }
9964 if (ids_seen[id] != -1)
9965 {
9966 error (_("Dwarf Error: bad DWP hash table, duplicate section"
9967 " id %d in section table [in module %s]"),
9968 id, dwp_file->name);
9969 }
9970 ids_seen[id] = i;
9971 ids[i] = id;
9972 }
9973 /* Must have exactly one info or types section. */
9974 if (((ids_seen[DW_SECT_INFO] != -1)
9975 + (ids_seen[DW_SECT_TYPES] != -1))
9976 != 1)
9977 {
9978 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
9979 " DWO info/types section [in module %s]"),
9980 dwp_file->name);
9981 }
9982 /* Must have an abbrev section. */
9983 if (ids_seen[DW_SECT_ABBREV] == -1)
9984 {
9985 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
9986 " section [in module %s]"),
9987 dwp_file->name);
9988 }
9989 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
9990 htab->section_pool.v2.sizes =
9991 htab->section_pool.v2.offsets + (sizeof (uint32_t)
9992 * nr_units * nr_columns);
9993 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
9994 * nr_units * nr_columns))
9995 > index_end)
9996 {
9997 error (_("Dwarf Error: DWP index section is corrupt (too small)"
9998 " [in module %s]"),
9999 dwp_file->name);
10000 }
10001 }
80626a55
DE
10002
10003 return htab;
10004}
10005
10006/* Update SECTIONS with the data from SECTP.
10007
10008 This function is like the other "locate" section routines that are
10009 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 10010 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
10011
10012 The result is non-zero for success, or zero if an error was found. */
10013
10014static int
73869dc2
DE
10015locate_v1_virtual_dwo_sections (asection *sectp,
10016 struct virtual_v1_dwo_sections *sections)
80626a55
DE
10017{
10018 const struct dwop_section_names *names = &dwop_section_names;
10019
10020 if (section_is_p (sectp->name, &names->abbrev_dwo))
10021 {
10022 /* There can be only one. */
049412e3 10023 if (sections->abbrev.s.section != NULL)
80626a55 10024 return 0;
049412e3 10025 sections->abbrev.s.section = sectp;
80626a55
DE
10026 sections->abbrev.size = bfd_get_section_size (sectp);
10027 }
10028 else if (section_is_p (sectp->name, &names->info_dwo)
10029 || section_is_p (sectp->name, &names->types_dwo))
10030 {
10031 /* There can be only one. */
049412e3 10032 if (sections->info_or_types.s.section != NULL)
80626a55 10033 return 0;
049412e3 10034 sections->info_or_types.s.section = sectp;
80626a55
DE
10035 sections->info_or_types.size = bfd_get_section_size (sectp);
10036 }
10037 else if (section_is_p (sectp->name, &names->line_dwo))
10038 {
10039 /* There can be only one. */
049412e3 10040 if (sections->line.s.section != NULL)
80626a55 10041 return 0;
049412e3 10042 sections->line.s.section = sectp;
80626a55
DE
10043 sections->line.size = bfd_get_section_size (sectp);
10044 }
10045 else if (section_is_p (sectp->name, &names->loc_dwo))
10046 {
10047 /* There can be only one. */
049412e3 10048 if (sections->loc.s.section != NULL)
80626a55 10049 return 0;
049412e3 10050 sections->loc.s.section = sectp;
80626a55
DE
10051 sections->loc.size = bfd_get_section_size (sectp);
10052 }
10053 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10054 {
10055 /* There can be only one. */
049412e3 10056 if (sections->macinfo.s.section != NULL)
80626a55 10057 return 0;
049412e3 10058 sections->macinfo.s.section = sectp;
80626a55
DE
10059 sections->macinfo.size = bfd_get_section_size (sectp);
10060 }
10061 else if (section_is_p (sectp->name, &names->macro_dwo))
10062 {
10063 /* There can be only one. */
049412e3 10064 if (sections->macro.s.section != NULL)
80626a55 10065 return 0;
049412e3 10066 sections->macro.s.section = sectp;
80626a55
DE
10067 sections->macro.size = bfd_get_section_size (sectp);
10068 }
10069 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10070 {
10071 /* There can be only one. */
049412e3 10072 if (sections->str_offsets.s.section != NULL)
80626a55 10073 return 0;
049412e3 10074 sections->str_offsets.s.section = sectp;
80626a55
DE
10075 sections->str_offsets.size = bfd_get_section_size (sectp);
10076 }
10077 else
10078 {
10079 /* No other kind of section is valid. */
10080 return 0;
10081 }
10082
10083 return 1;
10084}
10085
73869dc2
DE
10086/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10087 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10088 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10089 This is for DWP version 1 files. */
80626a55
DE
10090
10091static struct dwo_unit *
73869dc2
DE
10092create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10093 uint32_t unit_index,
10094 const char *comp_dir,
10095 ULONGEST signature, int is_debug_types)
80626a55
DE
10096{
10097 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
10098 const struct dwp_hash_table *dwp_htab =
10099 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55
DE
10100 bfd *dbfd = dwp_file->dbfd;
10101 const char *kind = is_debug_types ? "TU" : "CU";
10102 struct dwo_file *dwo_file;
10103 struct dwo_unit *dwo_unit;
73869dc2 10104 struct virtual_v1_dwo_sections sections;
80626a55
DE
10105 void **dwo_file_slot;
10106 char *virtual_dwo_name;
80626a55
DE
10107 struct cleanup *cleanups;
10108 int i;
10109
73869dc2
DE
10110 gdb_assert (dwp_file->version == 1);
10111
b4f54984 10112 if (dwarf_read_debug)
80626a55 10113 {
73869dc2 10114 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 10115 kind,
73869dc2 10116 pulongest (unit_index), hex_string (signature),
80626a55
DE
10117 dwp_file->name);
10118 }
10119
19ac8c2e 10120 /* Fetch the sections of this DWO unit.
80626a55
DE
10121 Put a limit on the number of sections we look for so that bad data
10122 doesn't cause us to loop forever. */
10123
73869dc2 10124#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
10125 (1 /* .debug_info or .debug_types */ \
10126 + 1 /* .debug_abbrev */ \
10127 + 1 /* .debug_line */ \
10128 + 1 /* .debug_loc */ \
10129 + 1 /* .debug_str_offsets */ \
19ac8c2e 10130 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
10131 + 1 /* trailing zero */)
10132
10133 memset (&sections, 0, sizeof (sections));
10134 cleanups = make_cleanup (null_cleanup, 0);
10135
73869dc2 10136 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
10137 {
10138 asection *sectp;
10139 uint32_t section_nr =
10140 read_4_bytes (dbfd,
73869dc2
DE
10141 dwp_htab->section_pool.v1.indices
10142 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
10143
10144 if (section_nr == 0)
10145 break;
10146 if (section_nr >= dwp_file->num_sections)
10147 {
10148 error (_("Dwarf Error: bad DWP hash table, section number too large"
10149 " [in module %s]"),
10150 dwp_file->name);
10151 }
10152
10153 sectp = dwp_file->elf_sections[section_nr];
73869dc2 10154 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
10155 {
10156 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10157 " [in module %s]"),
10158 dwp_file->name);
10159 }
10160 }
10161
10162 if (i < 2
a32a8923
DE
10163 || dwarf2_section_empty_p (&sections.info_or_types)
10164 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
10165 {
10166 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10167 " [in module %s]"),
10168 dwp_file->name);
10169 }
73869dc2 10170 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
10171 {
10172 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10173 " [in module %s]"),
10174 dwp_file->name);
10175 }
10176
10177 /* It's easier for the rest of the code if we fake a struct dwo_file and
10178 have dwo_unit "live" in that. At least for now.
10179
10180 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 10181 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
10182 file, we can combine them back into a virtual DWO file to save space
10183 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
10184 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10185
2792b94d
PM
10186 virtual_dwo_name =
10187 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
a32a8923
DE
10188 get_section_id (&sections.abbrev),
10189 get_section_id (&sections.line),
10190 get_section_id (&sections.loc),
10191 get_section_id (&sections.str_offsets));
80626a55
DE
10192 make_cleanup (xfree, virtual_dwo_name);
10193 /* Can we use an existing virtual DWO file? */
0ac5b59e 10194 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
10195 /* Create one if necessary. */
10196 if (*dwo_file_slot == NULL)
10197 {
b4f54984 10198 if (dwarf_read_debug)
80626a55
DE
10199 {
10200 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10201 virtual_dwo_name);
10202 }
10203 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10204 dwo_file->dwo_name
10205 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10206 virtual_dwo_name,
10207 strlen (virtual_dwo_name));
0ac5b59e 10208 dwo_file->comp_dir = comp_dir;
80626a55
DE
10209 dwo_file->sections.abbrev = sections.abbrev;
10210 dwo_file->sections.line = sections.line;
10211 dwo_file->sections.loc = sections.loc;
10212 dwo_file->sections.macinfo = sections.macinfo;
10213 dwo_file->sections.macro = sections.macro;
10214 dwo_file->sections.str_offsets = sections.str_offsets;
10215 /* The "str" section is global to the entire DWP file. */
10216 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 10217 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
10218 there's no need to record it in dwo_file.
10219 Also, we can't simply record type sections in dwo_file because
10220 we record a pointer into the vector in dwo_unit. As we collect more
10221 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
10222 for it, invalidating all copies of pointers into the previous
10223 contents. */
80626a55
DE
10224 *dwo_file_slot = dwo_file;
10225 }
10226 else
10227 {
b4f54984 10228 if (dwarf_read_debug)
80626a55
DE
10229 {
10230 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10231 virtual_dwo_name);
10232 }
9a3c8263 10233 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55
DE
10234 }
10235 do_cleanups (cleanups);
10236
10237 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10238 dwo_unit->dwo_file = dwo_file;
10239 dwo_unit->signature = signature;
8d749320
SM
10240 dwo_unit->section =
10241 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 10242 *dwo_unit->section = sections.info_or_types;
57d63ce2 10243 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
10244
10245 return dwo_unit;
10246}
10247
73869dc2
DE
10248/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10249 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10250 piece within that section used by a TU/CU, return a virtual section
10251 of just that piece. */
10252
10253static struct dwarf2_section_info
10254create_dwp_v2_section (struct dwarf2_section_info *section,
10255 bfd_size_type offset, bfd_size_type size)
10256{
10257 struct dwarf2_section_info result;
10258 asection *sectp;
10259
10260 gdb_assert (section != NULL);
10261 gdb_assert (!section->is_virtual);
10262
10263 memset (&result, 0, sizeof (result));
10264 result.s.containing_section = section;
10265 result.is_virtual = 1;
10266
10267 if (size == 0)
10268 return result;
10269
10270 sectp = get_section_bfd_section (section);
10271
10272 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10273 bounds of the real section. This is a pretty-rare event, so just
10274 flag an error (easier) instead of a warning and trying to cope. */
10275 if (sectp == NULL
10276 || offset + size > bfd_get_section_size (sectp))
10277 {
10278 bfd *abfd = sectp->owner;
10279
10280 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10281 " in section %s [in module %s]"),
10282 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10283 objfile_name (dwarf2_per_objfile->objfile));
10284 }
10285
10286 result.virtual_offset = offset;
10287 result.size = size;
10288 return result;
10289}
10290
10291/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10292 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10293 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10294 This is for DWP version 2 files. */
10295
10296static struct dwo_unit *
10297create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10298 uint32_t unit_index,
10299 const char *comp_dir,
10300 ULONGEST signature, int is_debug_types)
10301{
10302 struct objfile *objfile = dwarf2_per_objfile->objfile;
10303 const struct dwp_hash_table *dwp_htab =
10304 is_debug_types ? dwp_file->tus : dwp_file->cus;
10305 bfd *dbfd = dwp_file->dbfd;
10306 const char *kind = is_debug_types ? "TU" : "CU";
10307 struct dwo_file *dwo_file;
10308 struct dwo_unit *dwo_unit;
10309 struct virtual_v2_dwo_sections sections;
10310 void **dwo_file_slot;
10311 char *virtual_dwo_name;
73869dc2
DE
10312 struct cleanup *cleanups;
10313 int i;
10314
10315 gdb_assert (dwp_file->version == 2);
10316
b4f54984 10317 if (dwarf_read_debug)
73869dc2
DE
10318 {
10319 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10320 kind,
10321 pulongest (unit_index), hex_string (signature),
10322 dwp_file->name);
10323 }
10324
10325 /* Fetch the section offsets of this DWO unit. */
10326
10327 memset (&sections, 0, sizeof (sections));
10328 cleanups = make_cleanup (null_cleanup, 0);
10329
10330 for (i = 0; i < dwp_htab->nr_columns; ++i)
10331 {
10332 uint32_t offset = read_4_bytes (dbfd,
10333 dwp_htab->section_pool.v2.offsets
10334 + (((unit_index - 1) * dwp_htab->nr_columns
10335 + i)
10336 * sizeof (uint32_t)));
10337 uint32_t size = read_4_bytes (dbfd,
10338 dwp_htab->section_pool.v2.sizes
10339 + (((unit_index - 1) * dwp_htab->nr_columns
10340 + i)
10341 * sizeof (uint32_t)));
10342
10343 switch (dwp_htab->section_pool.v2.section_ids[i])
10344 {
10345 case DW_SECT_INFO:
10346 case DW_SECT_TYPES:
10347 sections.info_or_types_offset = offset;
10348 sections.info_or_types_size = size;
10349 break;
10350 case DW_SECT_ABBREV:
10351 sections.abbrev_offset = offset;
10352 sections.abbrev_size = size;
10353 break;
10354 case DW_SECT_LINE:
10355 sections.line_offset = offset;
10356 sections.line_size = size;
10357 break;
10358 case DW_SECT_LOC:
10359 sections.loc_offset = offset;
10360 sections.loc_size = size;
10361 break;
10362 case DW_SECT_STR_OFFSETS:
10363 sections.str_offsets_offset = offset;
10364 sections.str_offsets_size = size;
10365 break;
10366 case DW_SECT_MACINFO:
10367 sections.macinfo_offset = offset;
10368 sections.macinfo_size = size;
10369 break;
10370 case DW_SECT_MACRO:
10371 sections.macro_offset = offset;
10372 sections.macro_size = size;
10373 break;
10374 }
10375 }
10376
10377 /* It's easier for the rest of the code if we fake a struct dwo_file and
10378 have dwo_unit "live" in that. At least for now.
10379
10380 The DWP file can be made up of a random collection of CUs and TUs.
10381 However, for each CU + set of TUs that came from the same original DWO
10382 file, we can combine them back into a virtual DWO file to save space
10383 (fewer struct dwo_file objects to allocate). Remember that for really
10384 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10385
10386 virtual_dwo_name =
10387 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10388 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10389 (long) (sections.line_size ? sections.line_offset : 0),
10390 (long) (sections.loc_size ? sections.loc_offset : 0),
10391 (long) (sections.str_offsets_size
10392 ? sections.str_offsets_offset : 0));
10393 make_cleanup (xfree, virtual_dwo_name);
10394 /* Can we use an existing virtual DWO file? */
10395 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10396 /* Create one if necessary. */
10397 if (*dwo_file_slot == NULL)
10398 {
b4f54984 10399 if (dwarf_read_debug)
73869dc2
DE
10400 {
10401 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10402 virtual_dwo_name);
10403 }
10404 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
10405 dwo_file->dwo_name
10406 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10407 virtual_dwo_name,
10408 strlen (virtual_dwo_name));
73869dc2
DE
10409 dwo_file->comp_dir = comp_dir;
10410 dwo_file->sections.abbrev =
10411 create_dwp_v2_section (&dwp_file->sections.abbrev,
10412 sections.abbrev_offset, sections.abbrev_size);
10413 dwo_file->sections.line =
10414 create_dwp_v2_section (&dwp_file->sections.line,
10415 sections.line_offset, sections.line_size);
10416 dwo_file->sections.loc =
10417 create_dwp_v2_section (&dwp_file->sections.loc,
10418 sections.loc_offset, sections.loc_size);
10419 dwo_file->sections.macinfo =
10420 create_dwp_v2_section (&dwp_file->sections.macinfo,
10421 sections.macinfo_offset, sections.macinfo_size);
10422 dwo_file->sections.macro =
10423 create_dwp_v2_section (&dwp_file->sections.macro,
10424 sections.macro_offset, sections.macro_size);
10425 dwo_file->sections.str_offsets =
10426 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10427 sections.str_offsets_offset,
10428 sections.str_offsets_size);
10429 /* The "str" section is global to the entire DWP file. */
10430 dwo_file->sections.str = dwp_file->sections.str;
10431 /* The info or types section is assigned below to dwo_unit,
10432 there's no need to record it in dwo_file.
10433 Also, we can't simply record type sections in dwo_file because
10434 we record a pointer into the vector in dwo_unit. As we collect more
10435 types we'll grow the vector and eventually have to reallocate space
10436 for it, invalidating all copies of pointers into the previous
10437 contents. */
10438 *dwo_file_slot = dwo_file;
10439 }
10440 else
10441 {
b4f54984 10442 if (dwarf_read_debug)
73869dc2
DE
10443 {
10444 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10445 virtual_dwo_name);
10446 }
9a3c8263 10447 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2
DE
10448 }
10449 do_cleanups (cleanups);
10450
10451 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10452 dwo_unit->dwo_file = dwo_file;
10453 dwo_unit->signature = signature;
8d749320
SM
10454 dwo_unit->section =
10455 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
73869dc2
DE
10456 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10457 ? &dwp_file->sections.types
10458 : &dwp_file->sections.info,
10459 sections.info_or_types_offset,
10460 sections.info_or_types_size);
10461 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10462
10463 return dwo_unit;
10464}
10465
57d63ce2
DE
10466/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10467 Returns NULL if the signature isn't found. */
80626a55
DE
10468
10469static struct dwo_unit *
57d63ce2
DE
10470lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10471 ULONGEST signature, int is_debug_types)
80626a55 10472{
57d63ce2
DE
10473 const struct dwp_hash_table *dwp_htab =
10474 is_debug_types ? dwp_file->tus : dwp_file->cus;
80626a55 10475 bfd *dbfd = dwp_file->dbfd;
57d63ce2 10476 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
10477 uint32_t hash = signature & mask;
10478 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10479 unsigned int i;
10480 void **slot;
870f88f7 10481 struct dwo_unit find_dwo_cu;
80626a55
DE
10482
10483 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10484 find_dwo_cu.signature = signature;
19ac8c2e
DE
10485 slot = htab_find_slot (is_debug_types
10486 ? dwp_file->loaded_tus
10487 : dwp_file->loaded_cus,
10488 &find_dwo_cu, INSERT);
80626a55
DE
10489
10490 if (*slot != NULL)
9a3c8263 10491 return (struct dwo_unit *) *slot;
80626a55
DE
10492
10493 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 10494 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
10495 {
10496 ULONGEST signature_in_table;
10497
10498 signature_in_table =
57d63ce2 10499 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
10500 if (signature_in_table == signature)
10501 {
57d63ce2
DE
10502 uint32_t unit_index =
10503 read_4_bytes (dbfd,
10504 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 10505
73869dc2
DE
10506 if (dwp_file->version == 1)
10507 {
10508 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10509 comp_dir, signature,
10510 is_debug_types);
10511 }
10512 else
10513 {
10514 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10515 comp_dir, signature,
10516 is_debug_types);
10517 }
9a3c8263 10518 return (struct dwo_unit *) *slot;
80626a55
DE
10519 }
10520 if (signature_in_table == 0)
10521 return NULL;
10522 hash = (hash + hash2) & mask;
10523 }
10524
10525 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10526 " [in module %s]"),
10527 dwp_file->name);
10528}
10529
ab5088bf 10530/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
10531 Open the file specified by FILE_NAME and hand it off to BFD for
10532 preliminary analysis. Return a newly initialized bfd *, which
10533 includes a canonicalized copy of FILE_NAME.
80626a55 10534 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
10535 SEARCH_CWD is true if the current directory is to be searched.
10536 It will be searched before debug-file-directory.
13aaf454
DE
10537 If successful, the file is added to the bfd include table of the
10538 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 10539 If unable to find/open the file, return NULL.
3019eac3
DE
10540 NOTE: This function is derived from symfile_bfd_open. */
10541
192b62ce 10542static gdb_bfd_ref_ptr
6ac97d4c 10543try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3 10544{
80626a55 10545 int desc, flags;
3019eac3 10546 char *absolute_name;
9c02c129
DE
10547 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10548 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10549 to debug_file_directory. */
10550 char *search_path;
10551 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10552
6ac97d4c
DE
10553 if (search_cwd)
10554 {
10555 if (*debug_file_directory != '\0')
10556 search_path = concat (".", dirname_separator_string,
b36cec19 10557 debug_file_directory, (char *) NULL);
6ac97d4c
DE
10558 else
10559 search_path = xstrdup (".");
10560 }
9c02c129 10561 else
6ac97d4c 10562 search_path = xstrdup (debug_file_directory);
3019eac3 10563
492c0ab7 10564 flags = OPF_RETURN_REALPATH;
80626a55
DE
10565 if (is_dwp)
10566 flags |= OPF_SEARCH_IN_PATH;
9c02c129 10567 desc = openp (search_path, flags, file_name,
3019eac3 10568 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 10569 xfree (search_path);
3019eac3
DE
10570 if (desc < 0)
10571 return NULL;
10572
192b62ce 10573 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
a4453b7e 10574 xfree (absolute_name);
9c02c129
DE
10575 if (sym_bfd == NULL)
10576 return NULL;
192b62ce 10577 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 10578
192b62ce
TT
10579 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10580 return NULL;
3019eac3 10581
13aaf454
DE
10582 /* Success. Record the bfd as having been included by the objfile's bfd.
10583 This is important because things like demangled_names_hash lives in the
10584 objfile's per_bfd space and may have references to things like symbol
10585 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 10586 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 10587
3019eac3
DE
10588 return sym_bfd;
10589}
10590
ab5088bf 10591/* Try to open DWO file FILE_NAME.
3019eac3
DE
10592 COMP_DIR is the DW_AT_comp_dir attribute.
10593 The result is the bfd handle of the file.
10594 If there is a problem finding or opening the file, return NULL.
10595 Upon success, the canonicalized path of the file is stored in the bfd,
10596 same as symfile_bfd_open. */
10597
192b62ce 10598static gdb_bfd_ref_ptr
ab5088bf 10599open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3 10600{
80626a55 10601 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 10602 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
10603
10604 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10605
10606 if (comp_dir != NULL)
10607 {
b36cec19
PA
10608 char *path_to_try = concat (comp_dir, SLASH_STRING,
10609 file_name, (char *) NULL);
3019eac3
DE
10610
10611 /* NOTE: If comp_dir is a relative path, this will also try the
10612 search path, which seems useful. */
192b62ce
TT
10613 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10614 1 /*search_cwd*/));
3019eac3
DE
10615 xfree (path_to_try);
10616 if (abfd != NULL)
10617 return abfd;
10618 }
10619
10620 /* That didn't work, try debug-file-directory, which, despite its name,
10621 is a list of paths. */
10622
10623 if (*debug_file_directory == '\0')
10624 return NULL;
10625
6ac97d4c 10626 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
10627}
10628
80626a55
DE
10629/* This function is mapped across the sections and remembers the offset and
10630 size of each of the DWO debugging sections we are interested in. */
10631
10632static void
10633dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10634{
9a3c8263 10635 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
10636 const struct dwop_section_names *names = &dwop_section_names;
10637
10638 if (section_is_p (sectp->name, &names->abbrev_dwo))
10639 {
049412e3 10640 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
10641 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10642 }
10643 else if (section_is_p (sectp->name, &names->info_dwo))
10644 {
049412e3 10645 dwo_sections->info.s.section = sectp;
80626a55
DE
10646 dwo_sections->info.size = bfd_get_section_size (sectp);
10647 }
10648 else if (section_is_p (sectp->name, &names->line_dwo))
10649 {
049412e3 10650 dwo_sections->line.s.section = sectp;
80626a55
DE
10651 dwo_sections->line.size = bfd_get_section_size (sectp);
10652 }
10653 else if (section_is_p (sectp->name, &names->loc_dwo))
10654 {
049412e3 10655 dwo_sections->loc.s.section = sectp;
80626a55
DE
10656 dwo_sections->loc.size = bfd_get_section_size (sectp);
10657 }
10658 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10659 {
049412e3 10660 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
10661 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10662 }
10663 else if (section_is_p (sectp->name, &names->macro_dwo))
10664 {
049412e3 10665 dwo_sections->macro.s.section = sectp;
80626a55
DE
10666 dwo_sections->macro.size = bfd_get_section_size (sectp);
10667 }
10668 else if (section_is_p (sectp->name, &names->str_dwo))
10669 {
049412e3 10670 dwo_sections->str.s.section = sectp;
80626a55
DE
10671 dwo_sections->str.size = bfd_get_section_size (sectp);
10672 }
10673 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10674 {
049412e3 10675 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
10676 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10677 }
10678 else if (section_is_p (sectp->name, &names->types_dwo))
10679 {
10680 struct dwarf2_section_info type_section;
10681
10682 memset (&type_section, 0, sizeof (type_section));
049412e3 10683 type_section.s.section = sectp;
80626a55
DE
10684 type_section.size = bfd_get_section_size (sectp);
10685 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10686 &type_section);
10687 }
10688}
10689
ab5088bf 10690/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 10691 by PER_CU. This is for the non-DWP case.
80626a55 10692 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
10693
10694static struct dwo_file *
0ac5b59e
DE
10695open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10696 const char *dwo_name, const char *comp_dir)
3019eac3
DE
10697{
10698 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 10699 struct dwo_file *dwo_file;
3019eac3
DE
10700 struct cleanup *cleanups;
10701
192b62ce 10702 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
80626a55
DE
10703 if (dbfd == NULL)
10704 {
b4f54984 10705 if (dwarf_read_debug)
80626a55
DE
10706 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10707 return NULL;
10708 }
10709 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
10710 dwo_file->dwo_name = dwo_name;
10711 dwo_file->comp_dir = comp_dir;
192b62ce 10712 dwo_file->dbfd = dbfd.release ();
3019eac3
DE
10713
10714 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10715
192b62ce
TT
10716 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10717 &dwo_file->sections);
3019eac3 10718
19c3d4c9 10719 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3 10720
78d4d2c5
JK
10721 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10722 dwo_file->tus);
3019eac3
DE
10723
10724 discard_cleanups (cleanups);
10725
b4f54984 10726 if (dwarf_read_debug)
80626a55
DE
10727 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10728
3019eac3
DE
10729 return dwo_file;
10730}
10731
80626a55 10732/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
10733 size of each of the DWP debugging sections common to version 1 and 2 that
10734 we are interested in. */
3019eac3 10735
80626a55 10736static void
73869dc2
DE
10737dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10738 void *dwp_file_ptr)
3019eac3 10739{
9a3c8263 10740 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
10741 const struct dwop_section_names *names = &dwop_section_names;
10742 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 10743
80626a55 10744 /* Record the ELF section number for later lookup: this is what the
73869dc2 10745 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
10746 gdb_assert (elf_section_nr < dwp_file->num_sections);
10747 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 10748
80626a55
DE
10749 /* Look for specific sections that we need. */
10750 if (section_is_p (sectp->name, &names->str_dwo))
10751 {
049412e3 10752 dwp_file->sections.str.s.section = sectp;
80626a55
DE
10753 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10754 }
10755 else if (section_is_p (sectp->name, &names->cu_index))
10756 {
049412e3 10757 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
10758 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10759 }
10760 else if (section_is_p (sectp->name, &names->tu_index))
10761 {
049412e3 10762 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
10763 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10764 }
10765}
3019eac3 10766
73869dc2
DE
10767/* This function is mapped across the sections and remembers the offset and
10768 size of each of the DWP version 2 debugging sections that we are interested
10769 in. This is split into a separate function because we don't know if we
10770 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10771
10772static void
10773dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10774{
9a3c8263 10775 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
10776 const struct dwop_section_names *names = &dwop_section_names;
10777 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10778
10779 /* Record the ELF section number for later lookup: this is what the
10780 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10781 gdb_assert (elf_section_nr < dwp_file->num_sections);
10782 dwp_file->elf_sections[elf_section_nr] = sectp;
10783
10784 /* Look for specific sections that we need. */
10785 if (section_is_p (sectp->name, &names->abbrev_dwo))
10786 {
049412e3 10787 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
10788 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10789 }
10790 else if (section_is_p (sectp->name, &names->info_dwo))
10791 {
049412e3 10792 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
10793 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10794 }
10795 else if (section_is_p (sectp->name, &names->line_dwo))
10796 {
049412e3 10797 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
10798 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10799 }
10800 else if (section_is_p (sectp->name, &names->loc_dwo))
10801 {
049412e3 10802 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
10803 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10804 }
10805 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10806 {
049412e3 10807 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
10808 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10809 }
10810 else if (section_is_p (sectp->name, &names->macro_dwo))
10811 {
049412e3 10812 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
10813 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10814 }
10815 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10816 {
049412e3 10817 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
10818 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10819 }
10820 else if (section_is_p (sectp->name, &names->types_dwo))
10821 {
049412e3 10822 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
10823 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10824 }
10825}
10826
80626a55 10827/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 10828
80626a55
DE
10829static hashval_t
10830hash_dwp_loaded_cutus (const void *item)
10831{
9a3c8263 10832 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 10833
80626a55
DE
10834 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10835 return dwo_unit->signature;
3019eac3
DE
10836}
10837
80626a55 10838/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 10839
80626a55
DE
10840static int
10841eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 10842{
9a3c8263
SM
10843 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10844 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 10845
80626a55
DE
10846 return dua->signature == dub->signature;
10847}
3019eac3 10848
80626a55 10849/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 10850
80626a55
DE
10851static htab_t
10852allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10853{
10854 return htab_create_alloc_ex (3,
10855 hash_dwp_loaded_cutus,
10856 eq_dwp_loaded_cutus,
10857 NULL,
10858 &objfile->objfile_obstack,
10859 hashtab_obstack_allocate,
10860 dummy_obstack_deallocate);
10861}
3019eac3 10862
ab5088bf
DE
10863/* Try to open DWP file FILE_NAME.
10864 The result is the bfd handle of the file.
10865 If there is a problem finding or opening the file, return NULL.
10866 Upon success, the canonicalized path of the file is stored in the bfd,
10867 same as symfile_bfd_open. */
10868
192b62ce 10869static gdb_bfd_ref_ptr
ab5088bf
DE
10870open_dwp_file (const char *file_name)
10871{
192b62ce
TT
10872 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10873 1 /*search_cwd*/));
6ac97d4c
DE
10874 if (abfd != NULL)
10875 return abfd;
10876
10877 /* Work around upstream bug 15652.
10878 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10879 [Whether that's a "bug" is debatable, but it is getting in our way.]
10880 We have no real idea where the dwp file is, because gdb's realpath-ing
10881 of the executable's path may have discarded the needed info.
10882 [IWBN if the dwp file name was recorded in the executable, akin to
10883 .gnu_debuglink, but that doesn't exist yet.]
10884 Strip the directory from FILE_NAME and search again. */
10885 if (*debug_file_directory != '\0')
10886 {
10887 /* Don't implicitly search the current directory here.
10888 If the user wants to search "." to handle this case,
10889 it must be added to debug-file-directory. */
10890 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10891 0 /*search_cwd*/);
10892 }
10893
10894 return NULL;
ab5088bf
DE
10895}
10896
80626a55
DE
10897/* Initialize the use of the DWP file for the current objfile.
10898 By convention the name of the DWP file is ${objfile}.dwp.
10899 The result is NULL if it can't be found. */
a766d390 10900
80626a55 10901static struct dwp_file *
ab5088bf 10902open_and_init_dwp_file (void)
80626a55
DE
10903{
10904 struct objfile *objfile = dwarf2_per_objfile->objfile;
10905 struct dwp_file *dwp_file;
80626a55 10906
82bf32bc
JK
10907 /* Try to find first .dwp for the binary file before any symbolic links
10908 resolving. */
6c447423
DE
10909
10910 /* If the objfile is a debug file, find the name of the real binary
10911 file and get the name of dwp file from there. */
d721ba37 10912 std::string dwp_name;
6c447423
DE
10913 if (objfile->separate_debug_objfile_backlink != NULL)
10914 {
10915 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10916 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 10917
d721ba37 10918 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
10919 }
10920 else
d721ba37
PA
10921 dwp_name = objfile->original_name;
10922
10923 dwp_name += ".dwp";
80626a55 10924
d721ba37 10925 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
82bf32bc
JK
10926 if (dbfd == NULL
10927 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10928 {
10929 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
10930 dwp_name = objfile_name (objfile);
10931 dwp_name += ".dwp";
10932 dbfd = open_dwp_file (dwp_name.c_str ());
82bf32bc
JK
10933 }
10934
80626a55
DE
10935 if (dbfd == NULL)
10936 {
b4f54984 10937 if (dwarf_read_debug)
d721ba37 10938 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
80626a55 10939 return NULL;
3019eac3 10940 }
80626a55 10941 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
192b62ce
TT
10942 dwp_file->name = bfd_get_filename (dbfd.get ());
10943 dwp_file->dbfd = dbfd.release ();
c906108c 10944
80626a55 10945 /* +1: section 0 is unused */
192b62ce 10946 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
10947 dwp_file->elf_sections =
10948 OBSTACK_CALLOC (&objfile->objfile_obstack,
10949 dwp_file->num_sections, asection *);
10950
192b62ce
TT
10951 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
10952 dwp_file);
80626a55
DE
10953
10954 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10955
10956 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10957
73869dc2
DE
10958 /* The DWP file version is stored in the hash table. Oh well. */
10959 if (dwp_file->cus->version != dwp_file->tus->version)
10960 {
10961 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 10962 pretty bizarre. We use pulongest here because that's the established
4d65956b 10963 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
10964 error (_("Dwarf Error: DWP file CU version %s doesn't match"
10965 " TU version %s [in DWP file %s]"),
10966 pulongest (dwp_file->cus->version),
d721ba37 10967 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2
DE
10968 }
10969 dwp_file->version = dwp_file->cus->version;
10970
10971 if (dwp_file->version == 2)
192b62ce
TT
10972 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
10973 dwp_file);
73869dc2 10974
19ac8c2e
DE
10975 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10976 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 10977
b4f54984 10978 if (dwarf_read_debug)
80626a55
DE
10979 {
10980 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10981 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
10982 " %s CUs, %s TUs\n",
10983 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10984 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
10985 }
10986
10987 return dwp_file;
3019eac3 10988}
c906108c 10989
ab5088bf
DE
10990/* Wrapper around open_and_init_dwp_file, only open it once. */
10991
10992static struct dwp_file *
10993get_dwp_file (void)
10994{
10995 if (! dwarf2_per_objfile->dwp_checked)
10996 {
10997 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10998 dwarf2_per_objfile->dwp_checked = 1;
10999 }
11000 return dwarf2_per_objfile->dwp_file;
11001}
11002
80626a55
DE
11003/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11004 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11005 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 11006 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
11007 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11008
11009 This is called, for example, when wanting to read a variable with a
11010 complex location. Therefore we don't want to do file i/o for every call.
11011 Therefore we don't want to look for a DWO file on every call.
11012 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11013 then we check if we've already seen DWO_NAME, and only THEN do we check
11014 for a DWO file.
11015
1c658ad5 11016 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 11017 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 11018
3019eac3 11019static struct dwo_unit *
80626a55
DE
11020lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11021 const char *dwo_name, const char *comp_dir,
11022 ULONGEST signature, int is_debug_types)
3019eac3
DE
11023{
11024 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
11025 const char *kind = is_debug_types ? "TU" : "CU";
11026 void **dwo_file_slot;
3019eac3 11027 struct dwo_file *dwo_file;
80626a55 11028 struct dwp_file *dwp_file;
cb1df416 11029
6a506a2d
DE
11030 /* First see if there's a DWP file.
11031 If we have a DWP file but didn't find the DWO inside it, don't
11032 look for the original DWO file. It makes gdb behave differently
11033 depending on whether one is debugging in the build tree. */
cf2c3c16 11034
ab5088bf 11035 dwp_file = get_dwp_file ();
80626a55 11036 if (dwp_file != NULL)
cf2c3c16 11037 {
80626a55
DE
11038 const struct dwp_hash_table *dwp_htab =
11039 is_debug_types ? dwp_file->tus : dwp_file->cus;
11040
11041 if (dwp_htab != NULL)
11042 {
11043 struct dwo_unit *dwo_cutu =
57d63ce2
DE
11044 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11045 signature, is_debug_types);
80626a55
DE
11046
11047 if (dwo_cutu != NULL)
11048 {
b4f54984 11049 if (dwarf_read_debug)
80626a55
DE
11050 {
11051 fprintf_unfiltered (gdb_stdlog,
11052 "Virtual DWO %s %s found: @%s\n",
11053 kind, hex_string (signature),
11054 host_address_to_string (dwo_cutu));
11055 }
11056 return dwo_cutu;
11057 }
11058 }
11059 }
6a506a2d 11060 else
80626a55 11061 {
6a506a2d 11062 /* No DWP file, look for the DWO file. */
80626a55 11063
6a506a2d
DE
11064 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11065 if (*dwo_file_slot == NULL)
80626a55 11066 {
6a506a2d
DE
11067 /* Read in the file and build a table of the CUs/TUs it contains. */
11068 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 11069 }
6a506a2d 11070 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 11071 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 11072
6a506a2d 11073 if (dwo_file != NULL)
19c3d4c9 11074 {
6a506a2d
DE
11075 struct dwo_unit *dwo_cutu = NULL;
11076
11077 if (is_debug_types && dwo_file->tus)
11078 {
11079 struct dwo_unit find_dwo_cutu;
11080
11081 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11082 find_dwo_cutu.signature = signature;
9a3c8263
SM
11083 dwo_cutu
11084 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d
DE
11085 }
11086 else if (!is_debug_types && dwo_file->cu)
80626a55 11087 {
6a506a2d
DE
11088 if (signature == dwo_file->cu->signature)
11089 dwo_cutu = dwo_file->cu;
11090 }
11091
11092 if (dwo_cutu != NULL)
11093 {
b4f54984 11094 if (dwarf_read_debug)
6a506a2d
DE
11095 {
11096 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11097 kind, dwo_name, hex_string (signature),
11098 host_address_to_string (dwo_cutu));
11099 }
11100 return dwo_cutu;
80626a55
DE
11101 }
11102 }
2e276125 11103 }
9cdd5dbd 11104
80626a55
DE
11105 /* We didn't find it. This could mean a dwo_id mismatch, or
11106 someone deleted the DWO/DWP file, or the search path isn't set up
11107 correctly to find the file. */
11108
b4f54984 11109 if (dwarf_read_debug)
80626a55
DE
11110 {
11111 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11112 kind, dwo_name, hex_string (signature));
11113 }
3019eac3 11114
6656a72d
DE
11115 /* This is a warning and not a complaint because it can be caused by
11116 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
11117 {
11118 /* Print the name of the DWP file if we looked there, helps the user
11119 better diagnose the problem. */
11120 char *dwp_text = NULL;
11121 struct cleanup *cleanups;
11122
11123 if (dwp_file != NULL)
11124 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11125 cleanups = make_cleanup (xfree, dwp_text);
11126
11127 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11128 " [in module %s]"),
11129 kind, dwo_name, hex_string (signature),
11130 dwp_text != NULL ? dwp_text : "",
11131 this_unit->is_debug_types ? "TU" : "CU",
11132 this_unit->offset.sect_off, objfile_name (objfile));
11133
11134 do_cleanups (cleanups);
11135 }
3019eac3 11136 return NULL;
5fb290d7
DJ
11137}
11138
80626a55
DE
11139/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11140 See lookup_dwo_cutu_unit for details. */
11141
11142static struct dwo_unit *
11143lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11144 const char *dwo_name, const char *comp_dir,
11145 ULONGEST signature)
11146{
11147 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11148}
11149
11150/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11151 See lookup_dwo_cutu_unit for details. */
11152
11153static struct dwo_unit *
11154lookup_dwo_type_unit (struct signatured_type *this_tu,
11155 const char *dwo_name, const char *comp_dir)
11156{
11157 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11158}
11159
89e63ee4
DE
11160/* Traversal function for queue_and_load_all_dwo_tus. */
11161
11162static int
11163queue_and_load_dwo_tu (void **slot, void *info)
11164{
11165 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11166 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11167 ULONGEST signature = dwo_unit->signature;
11168 struct signatured_type *sig_type =
11169 lookup_dwo_signatured_type (per_cu->cu, signature);
11170
11171 if (sig_type != NULL)
11172 {
11173 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11174
11175 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11176 a real dependency of PER_CU on SIG_TYPE. That is detected later
11177 while processing PER_CU. */
11178 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11179 load_full_type_unit (sig_cu);
11180 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11181 }
11182
11183 return 1;
11184}
11185
11186/* Queue all TUs contained in the DWO of PER_CU to be read in.
11187 The DWO may have the only definition of the type, though it may not be
11188 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11189 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11190
11191static void
11192queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11193{
11194 struct dwo_unit *dwo_unit;
11195 struct dwo_file *dwo_file;
11196
11197 gdb_assert (!per_cu->is_debug_types);
11198 gdb_assert (get_dwp_file () == NULL);
11199 gdb_assert (per_cu->cu != NULL);
11200
11201 dwo_unit = per_cu->cu->dwo_unit;
11202 gdb_assert (dwo_unit != NULL);
11203
11204 dwo_file = dwo_unit->dwo_file;
11205 if (dwo_file->tus != NULL)
11206 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11207}
11208
3019eac3
DE
11209/* Free all resources associated with DWO_FILE.
11210 Close the DWO file and munmap the sections.
11211 All memory should be on the objfile obstack. */
348e048f
DE
11212
11213static void
3019eac3 11214free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 11215{
348e048f 11216
5c6fa7ab 11217 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 11218 gdb_bfd_unref (dwo_file->dbfd);
348e048f 11219
3019eac3
DE
11220 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11221}
348e048f 11222
3019eac3 11223/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 11224
3019eac3
DE
11225static void
11226free_dwo_file_cleanup (void *arg)
11227{
11228 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11229 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 11230
3019eac3
DE
11231 free_dwo_file (dwo_file, objfile);
11232}
348e048f 11233
3019eac3 11234/* Traversal function for free_dwo_files. */
2ab95328 11235
3019eac3
DE
11236static int
11237free_dwo_file_from_slot (void **slot, void *info)
11238{
11239 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11240 struct objfile *objfile = (struct objfile *) info;
348e048f 11241
3019eac3 11242 free_dwo_file (dwo_file, objfile);
348e048f 11243
3019eac3
DE
11244 return 1;
11245}
348e048f 11246
3019eac3 11247/* Free all resources associated with DWO_FILES. */
348e048f 11248
3019eac3
DE
11249static void
11250free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11251{
11252 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 11253}
3019eac3
DE
11254\f
11255/* Read in various DIEs. */
348e048f 11256
d389af10
JK
11257/* qsort helper for inherit_abstract_dies. */
11258
11259static int
11260unsigned_int_compar (const void *ap, const void *bp)
11261{
11262 unsigned int a = *(unsigned int *) ap;
11263 unsigned int b = *(unsigned int *) bp;
11264
11265 return (a > b) - (b > a);
11266}
11267
11268/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
11269 Inherit only the children of the DW_AT_abstract_origin DIE not being
11270 already referenced by DW_AT_abstract_origin from the children of the
11271 current DIE. */
d389af10
JK
11272
11273static void
11274inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11275{
11276 struct die_info *child_die;
11277 unsigned die_children_count;
11278 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
11279 sect_offset *offsets;
11280 sect_offset *offsets_end, *offsetp;
d389af10
JK
11281 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11282 struct die_info *origin_die;
11283 /* Iterator of the ORIGIN_DIE children. */
11284 struct die_info *origin_child_die;
11285 struct cleanup *cleanups;
11286 struct attribute *attr;
cd02d79d
PA
11287 struct dwarf2_cu *origin_cu;
11288 struct pending **origin_previous_list_in_scope;
d389af10
JK
11289
11290 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11291 if (!attr)
11292 return;
11293
cd02d79d
PA
11294 /* Note that following die references may follow to a die in a
11295 different cu. */
11296
11297 origin_cu = cu;
11298 origin_die = follow_die_ref (die, attr, &origin_cu);
11299
11300 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11301 symbols in. */
11302 origin_previous_list_in_scope = origin_cu->list_in_scope;
11303 origin_cu->list_in_scope = cu->list_in_scope;
11304
edb3359d
DJ
11305 if (die->tag != origin_die->tag
11306 && !(die->tag == DW_TAG_inlined_subroutine
11307 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11308 complaint (&symfile_complaints,
11309 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 11310 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
11311
11312 child_die = die->child;
11313 die_children_count = 0;
11314 while (child_die && child_die->tag)
11315 {
11316 child_die = sibling_die (child_die);
11317 die_children_count++;
11318 }
8d749320 11319 offsets = XNEWVEC (sect_offset, die_children_count);
d389af10
JK
11320 cleanups = make_cleanup (xfree, offsets);
11321
11322 offsets_end = offsets;
3ea89b92
PMR
11323 for (child_die = die->child;
11324 child_die && child_die->tag;
11325 child_die = sibling_die (child_die))
11326 {
11327 struct die_info *child_origin_die;
11328 struct dwarf2_cu *child_origin_cu;
11329
11330 /* We are trying to process concrete instance entries:
216f72a1 11331 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
11332 it's not relevant to our analysis here. i.e. detecting DIEs that are
11333 present in the abstract instance but not referenced in the concrete
11334 one. */
216f72a1
JK
11335 if (child_die->tag == DW_TAG_call_site
11336 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
11337 continue;
11338
c38f313d
DJ
11339 /* For each CHILD_DIE, find the corresponding child of
11340 ORIGIN_DIE. If there is more than one layer of
11341 DW_AT_abstract_origin, follow them all; there shouldn't be,
11342 but GCC versions at least through 4.4 generate this (GCC PR
11343 40573). */
3ea89b92
PMR
11344 child_origin_die = child_die;
11345 child_origin_cu = cu;
c38f313d
DJ
11346 while (1)
11347 {
cd02d79d
PA
11348 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11349 child_origin_cu);
c38f313d
DJ
11350 if (attr == NULL)
11351 break;
cd02d79d
PA
11352 child_origin_die = follow_die_ref (child_origin_die, attr,
11353 &child_origin_cu);
c38f313d
DJ
11354 }
11355
d389af10
JK
11356 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11357 counterpart may exist. */
c38f313d 11358 if (child_origin_die != child_die)
d389af10 11359 {
edb3359d
DJ
11360 if (child_die->tag != child_origin_die->tag
11361 && !(child_die->tag == DW_TAG_inlined_subroutine
11362 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
11363 complaint (&symfile_complaints,
11364 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11365 "different tags"), child_die->offset.sect_off,
11366 child_origin_die->offset.sect_off);
c38f313d
DJ
11367 if (child_origin_die->parent != origin_die)
11368 complaint (&symfile_complaints,
11369 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
11370 "different parents"), child_die->offset.sect_off,
11371 child_origin_die->offset.sect_off);
c38f313d
DJ
11372 else
11373 *offsets_end++ = child_origin_die->offset;
d389af10 11374 }
d389af10
JK
11375 }
11376 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11377 unsigned_int_compar);
11378 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 11379 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
11380 complaint (&symfile_complaints,
11381 _("Multiple children of DIE 0x%x refer "
11382 "to DIE 0x%x as their abstract origin"),
b64f50a1 11383 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
11384
11385 offsetp = offsets;
11386 origin_child_die = origin_die->child;
11387 while (origin_child_die && origin_child_die->tag)
11388 {
11389 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
11390 while (offsetp < offsets_end
11391 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 11392 offsetp++;
b64f50a1
JK
11393 if (offsetp >= offsets_end
11394 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10 11395 {
adde2bff
DE
11396 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11397 Check whether we're already processing ORIGIN_CHILD_DIE.
11398 This can happen with mutually referenced abstract_origins.
11399 PR 16581. */
11400 if (!origin_child_die->in_process)
11401 process_die (origin_child_die, origin_cu);
d389af10
JK
11402 }
11403 origin_child_die = sibling_die (origin_child_die);
11404 }
cd02d79d 11405 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
11406
11407 do_cleanups (cleanups);
11408}
11409
c906108c 11410static void
e7c27a73 11411read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11412{
e7c27a73 11413 struct objfile *objfile = cu->objfile;
3e29f34a 11414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11415 struct context_stack *newobj;
c906108c
SS
11416 CORE_ADDR lowpc;
11417 CORE_ADDR highpc;
11418 struct die_info *child_die;
edb3359d 11419 struct attribute *attr, *call_line, *call_file;
15d034d0 11420 const char *name;
e142c38c 11421 CORE_ADDR baseaddr;
801e3a5b 11422 struct block *block;
edb3359d 11423 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
11424 VEC (symbolp) *template_args = NULL;
11425 struct template_symbol *templ_func = NULL;
edb3359d
DJ
11426
11427 if (inlined_func)
11428 {
11429 /* If we do not have call site information, we can't show the
11430 caller of this inlined function. That's too confusing, so
11431 only use the scope for local variables. */
11432 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11433 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11434 if (call_line == NULL || call_file == NULL)
11435 {
11436 read_lexical_block_scope (die, cu);
11437 return;
11438 }
11439 }
c906108c 11440
e142c38c
DJ
11441 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11442
94af9270 11443 name = dwarf2_name (die, cu);
c906108c 11444
e8d05480
JB
11445 /* Ignore functions with missing or empty names. These are actually
11446 illegal according to the DWARF standard. */
11447 if (name == NULL)
11448 {
11449 complaint (&symfile_complaints,
b64f50a1
JK
11450 _("missing name for subprogram DIE at %d"),
11451 die->offset.sect_off);
e8d05480
JB
11452 return;
11453 }
11454
11455 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 11456 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 11457 <= PC_BOUNDS_INVALID)
e8d05480 11458 {
ae4d0c03
PM
11459 attr = dwarf2_attr (die, DW_AT_external, cu);
11460 if (!attr || !DW_UNSND (attr))
11461 complaint (&symfile_complaints,
3e43a32a
MS
11462 _("cannot get low and high bounds "
11463 "for subprogram DIE at %d"),
b64f50a1 11464 die->offset.sect_off);
e8d05480
JB
11465 return;
11466 }
c906108c 11467
3e29f34a
MR
11468 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11469 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 11470
34eaf542
TT
11471 /* If we have any template arguments, then we must allocate a
11472 different sort of symbol. */
11473 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11474 {
11475 if (child_die->tag == DW_TAG_template_type_param
11476 || child_die->tag == DW_TAG_template_value_param)
11477 {
e623cf5d 11478 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
11479 templ_func->base.is_cplus_template_function = 1;
11480 break;
11481 }
11482 }
11483
fe978cb0
PA
11484 newobj = push_context (0, lowpc);
11485 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
34eaf542 11486 (struct symbol *) templ_func);
4c2df51b 11487
4cecd739
DJ
11488 /* If there is a location expression for DW_AT_frame_base, record
11489 it. */
e142c38c 11490 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 11491 if (attr)
fe978cb0 11492 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 11493
63e43d3a
PMR
11494 /* If there is a location for the static link, record it. */
11495 newobj->static_link = NULL;
11496 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11497 if (attr)
11498 {
224c3ddb
SM
11499 newobj->static_link
11500 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
11501 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11502 }
11503
e142c38c 11504 cu->list_in_scope = &local_symbols;
c906108c 11505
639d11d3 11506 if (die->child != NULL)
c906108c 11507 {
639d11d3 11508 child_die = die->child;
c906108c
SS
11509 while (child_die && child_die->tag)
11510 {
34eaf542
TT
11511 if (child_die->tag == DW_TAG_template_type_param
11512 || child_die->tag == DW_TAG_template_value_param)
11513 {
11514 struct symbol *arg = new_symbol (child_die, NULL, cu);
11515
f1078f66
DJ
11516 if (arg != NULL)
11517 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11518 }
11519 else
11520 process_die (child_die, cu);
c906108c
SS
11521 child_die = sibling_die (child_die);
11522 }
11523 }
11524
d389af10
JK
11525 inherit_abstract_dies (die, cu);
11526
4a811a97
UW
11527 /* If we have a DW_AT_specification, we might need to import using
11528 directives from the context of the specification DIE. See the
11529 comment in determine_prefix. */
11530 if (cu->language == language_cplus
11531 && dwarf2_attr (die, DW_AT_specification, cu))
11532 {
11533 struct dwarf2_cu *spec_cu = cu;
11534 struct die_info *spec_die = die_specification (die, &spec_cu);
11535
11536 while (spec_die)
11537 {
11538 child_die = spec_die->child;
11539 while (child_die && child_die->tag)
11540 {
11541 if (child_die->tag == DW_TAG_imported_module)
11542 process_die (child_die, spec_cu);
11543 child_die = sibling_die (child_die);
11544 }
11545
11546 /* In some cases, GCC generates specification DIEs that
11547 themselves contain DW_AT_specification attributes. */
11548 spec_die = die_specification (spec_die, &spec_cu);
11549 }
11550 }
11551
fe978cb0 11552 newobj = pop_context ();
c906108c 11553 /* Make a block for the local symbols within. */
fe978cb0 11554 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 11555 newobj->static_link, lowpc, highpc);
801e3a5b 11556
df8a16a1 11557 /* For C++, set the block's scope. */
45280282
IB
11558 if ((cu->language == language_cplus
11559 || cu->language == language_fortran
c44af4eb
TT
11560 || cu->language == language_d
11561 || cu->language == language_rust)
4d4ec4e5 11562 && cu->processing_has_namespace_info)
195a3f6c
TT
11563 block_set_scope (block, determine_prefix (die, cu),
11564 &objfile->objfile_obstack);
df8a16a1 11565
801e3a5b
JB
11566 /* If we have address ranges, record them. */
11567 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 11568
fe978cb0 11569 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 11570
34eaf542
TT
11571 /* Attach template arguments to function. */
11572 if (! VEC_empty (symbolp, template_args))
11573 {
11574 gdb_assert (templ_func != NULL);
11575
11576 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11577 templ_func->template_arguments
8d749320
SM
11578 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11579 templ_func->n_template_arguments);
34eaf542
TT
11580 memcpy (templ_func->template_arguments,
11581 VEC_address (symbolp, template_args),
11582 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11583 VEC_free (symbolp, template_args);
11584 }
11585
208d8187
JB
11586 /* In C++, we can have functions nested inside functions (e.g., when
11587 a function declares a class that has methods). This means that
11588 when we finish processing a function scope, we may need to go
11589 back to building a containing block's symbol lists. */
fe978cb0 11590 local_symbols = newobj->locals;
22cee43f 11591 local_using_directives = newobj->local_using_directives;
208d8187 11592
921e78cf
JB
11593 /* If we've finished processing a top-level function, subsequent
11594 symbols go in the file symbol list. */
11595 if (outermost_context_p ())
e142c38c 11596 cu->list_in_scope = &file_symbols;
c906108c
SS
11597}
11598
11599/* Process all the DIES contained within a lexical block scope. Start
11600 a new scope, process the dies, and then close the scope. */
11601
11602static void
e7c27a73 11603read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11604{
e7c27a73 11605 struct objfile *objfile = cu->objfile;
3e29f34a 11606 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 11607 struct context_stack *newobj;
c906108c
SS
11608 CORE_ADDR lowpc, highpc;
11609 struct die_info *child_die;
e142c38c
DJ
11610 CORE_ADDR baseaddr;
11611
11612 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
11613
11614 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
11615 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11616 as multiple lexical blocks? Handling children in a sane way would
6e70227d 11617 be nasty. Might be easier to properly extend generic blocks to
af34e669 11618 describe ranges. */
e385593e
JK
11619 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11620 {
11621 case PC_BOUNDS_NOT_PRESENT:
11622 /* DW_TAG_lexical_block has no attributes, process its children as if
11623 there was no wrapping by that DW_TAG_lexical_block.
11624 GCC does no longer produces such DWARF since GCC r224161. */
11625 for (child_die = die->child;
11626 child_die != NULL && child_die->tag;
11627 child_die = sibling_die (child_die))
11628 process_die (child_die, cu);
11629 return;
11630 case PC_BOUNDS_INVALID:
11631 return;
11632 }
3e29f34a
MR
11633 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11634 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
11635
11636 push_context (0, lowpc);
639d11d3 11637 if (die->child != NULL)
c906108c 11638 {
639d11d3 11639 child_die = die->child;
c906108c
SS
11640 while (child_die && child_die->tag)
11641 {
e7c27a73 11642 process_die (child_die, cu);
c906108c
SS
11643 child_die = sibling_die (child_die);
11644 }
11645 }
3ea89b92 11646 inherit_abstract_dies (die, cu);
fe978cb0 11647 newobj = pop_context ();
c906108c 11648
22cee43f 11649 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 11650 {
801e3a5b 11651 struct block *block
63e43d3a 11652 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 11653 newobj->start_addr, highpc);
801e3a5b
JB
11654
11655 /* Note that recording ranges after traversing children, as we
11656 do here, means that recording a parent's ranges entails
11657 walking across all its children's ranges as they appear in
11658 the address map, which is quadratic behavior.
11659
11660 It would be nicer to record the parent's ranges before
11661 traversing its children, simply overriding whatever you find
11662 there. But since we don't even decide whether to create a
11663 block until after we've traversed its children, that's hard
11664 to do. */
11665 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 11666 }
fe978cb0 11667 local_symbols = newobj->locals;
22cee43f 11668 local_using_directives = newobj->local_using_directives;
c906108c
SS
11669}
11670
216f72a1 11671/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
11672
11673static void
11674read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11675{
11676 struct objfile *objfile = cu->objfile;
11677 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11678 CORE_ADDR pc, baseaddr;
11679 struct attribute *attr;
11680 struct call_site *call_site, call_site_local;
11681 void **slot;
11682 int nparams;
11683 struct die_info *child_die;
11684
11685 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11686
216f72a1
JK
11687 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11688 if (attr == NULL)
11689 {
11690 /* This was a pre-DWARF-5 GNU extension alias
11691 for DW_AT_call_return_pc. */
11692 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11693 }
96408a79
SA
11694 if (!attr)
11695 {
11696 complaint (&symfile_complaints,
216f72a1 11697 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
96408a79 11698 "DIE 0x%x [in module %s]"),
4262abfb 11699 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11700 return;
11701 }
31aa7e4e 11702 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 11703 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
11704
11705 if (cu->call_site_htab == NULL)
11706 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11707 NULL, &objfile->objfile_obstack,
11708 hashtab_obstack_allocate, NULL);
11709 call_site_local.pc = pc;
11710 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11711 if (*slot != NULL)
11712 {
11713 complaint (&symfile_complaints,
216f72a1 11714 _("Duplicate PC %s for DW_TAG_call_site "
96408a79 11715 "DIE 0x%x [in module %s]"),
4262abfb
JK
11716 paddress (gdbarch, pc), die->offset.sect_off,
11717 objfile_name (objfile));
96408a79
SA
11718 return;
11719 }
11720
11721 /* Count parameters at the caller. */
11722
11723 nparams = 0;
11724 for (child_die = die->child; child_die && child_die->tag;
11725 child_die = sibling_die (child_die))
11726 {
216f72a1
JK
11727 if (child_die->tag != DW_TAG_call_site_parameter
11728 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11729 {
11730 complaint (&symfile_complaints,
216f72a1
JK
11731 _("Tag %d is not DW_TAG_call_site_parameter in "
11732 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb
JK
11733 child_die->tag, child_die->offset.sect_off,
11734 objfile_name (objfile));
96408a79
SA
11735 continue;
11736 }
11737
11738 nparams++;
11739 }
11740
224c3ddb
SM
11741 call_site
11742 = ((struct call_site *)
11743 obstack_alloc (&objfile->objfile_obstack,
11744 sizeof (*call_site)
11745 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
11746 *slot = call_site;
11747 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11748 call_site->pc = pc;
11749
216f72a1
JK
11750 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11751 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
11752 {
11753 struct die_info *func_die;
11754
11755 /* Skip also over DW_TAG_inlined_subroutine. */
11756 for (func_die = die->parent;
11757 func_die && func_die->tag != DW_TAG_subprogram
11758 && func_die->tag != DW_TAG_subroutine_type;
11759 func_die = func_die->parent);
11760
216f72a1
JK
11761 /* DW_AT_call_all_calls is a superset
11762 of DW_AT_call_all_tail_calls. */
96408a79 11763 if (func_die
216f72a1 11764 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 11765 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 11766 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
11767 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11768 {
11769 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11770 not complete. But keep CALL_SITE for look ups via call_site_htab,
11771 both the initial caller containing the real return address PC and
11772 the final callee containing the current PC of a chain of tail
11773 calls do not need to have the tail call list complete. But any
11774 function candidate for a virtual tail call frame searched via
11775 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11776 determined unambiguously. */
11777 }
11778 else
11779 {
11780 struct type *func_type = NULL;
11781
11782 if (func_die)
11783 func_type = get_die_type (func_die, cu);
11784 if (func_type != NULL)
11785 {
11786 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11787
11788 /* Enlist this call site to the function. */
11789 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11790 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11791 }
11792 else
11793 complaint (&symfile_complaints,
216f72a1 11794 _("Cannot find function owning DW_TAG_call_site "
96408a79 11795 "DIE 0x%x [in module %s]"),
4262abfb 11796 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11797 }
11798 }
11799
216f72a1
JK
11800 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11801 if (attr == NULL)
11802 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11803 if (attr == NULL)
11804 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 11805 if (attr == NULL)
216f72a1
JK
11806 {
11807 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11808 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11809 }
96408a79
SA
11810 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11811 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11812 /* Keep NULL DWARF_BLOCK. */;
11813 else if (attr_form_is_block (attr))
11814 {
11815 struct dwarf2_locexpr_baton *dlbaton;
11816
8d749320 11817 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
11818 dlbaton->data = DW_BLOCK (attr)->data;
11819 dlbaton->size = DW_BLOCK (attr)->size;
11820 dlbaton->per_cu = cu->per_cu;
11821
11822 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11823 }
7771576e 11824 else if (attr_form_is_ref (attr))
96408a79 11825 {
96408a79
SA
11826 struct dwarf2_cu *target_cu = cu;
11827 struct die_info *target_die;
11828
ac9ec31b 11829 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
11830 gdb_assert (target_cu->objfile == objfile);
11831 if (die_is_declaration (target_die, target_cu))
11832 {
7d45c7c3 11833 const char *target_physname;
9112db09
JK
11834
11835 /* Prefer the mangled name; otherwise compute the demangled one. */
7d45c7c3
KB
11836 target_physname = dwarf2_string_attr (target_die,
11837 DW_AT_linkage_name,
11838 target_cu);
11839 if (target_physname == NULL)
11840 target_physname = dwarf2_string_attr (target_die,
11841 DW_AT_MIPS_linkage_name,
11842 target_cu);
11843 if (target_physname == NULL)
9112db09 11844 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
11845 if (target_physname == NULL)
11846 complaint (&symfile_complaints,
216f72a1 11847 _("DW_AT_call_target target DIE has invalid "
96408a79 11848 "physname, for referencing DIE 0x%x [in module %s]"),
4262abfb 11849 die->offset.sect_off, objfile_name (objfile));
96408a79 11850 else
7d455152 11851 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
11852 }
11853 else
11854 {
11855 CORE_ADDR lowpc;
11856
11857 /* DW_AT_entry_pc should be preferred. */
3a2b436a 11858 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 11859 <= PC_BOUNDS_INVALID)
96408a79 11860 complaint (&symfile_complaints,
216f72a1 11861 _("DW_AT_call_target target DIE has invalid "
96408a79 11862 "low pc, for referencing DIE 0x%x [in module %s]"),
4262abfb 11863 die->offset.sect_off, objfile_name (objfile));
96408a79 11864 else
3e29f34a
MR
11865 {
11866 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11867 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11868 }
96408a79
SA
11869 }
11870 }
11871 else
11872 complaint (&symfile_complaints,
216f72a1 11873 _("DW_TAG_call_site DW_AT_call_target is neither "
96408a79 11874 "block nor reference, for DIE 0x%x [in module %s]"),
4262abfb 11875 die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11876
11877 call_site->per_cu = cu->per_cu;
11878
11879 for (child_die = die->child;
11880 child_die && child_die->tag;
11881 child_die = sibling_die (child_die))
11882 {
96408a79 11883 struct call_site_parameter *parameter;
1788b2d3 11884 struct attribute *loc, *origin;
96408a79 11885
216f72a1
JK
11886 if (child_die->tag != DW_TAG_call_site_parameter
11887 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
11888 {
11889 /* Already printed the complaint above. */
11890 continue;
11891 }
11892
11893 gdb_assert (call_site->parameter_count < nparams);
11894 parameter = &call_site->parameter[call_site->parameter_count];
11895
1788b2d3
JK
11896 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11897 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 11898 register is contained in DW_AT_call_value. */
96408a79 11899
24c5c679 11900 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
11901 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11902 if (origin == NULL)
11903 {
11904 /* This was a pre-DWARF-5 GNU extension alias
11905 for DW_AT_call_parameter. */
11906 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11907 }
7771576e 11908 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
11909 {
11910 sect_offset offset;
11911
11912 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11913 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
11914 if (!offset_in_cu_p (&cu->header, offset))
11915 {
11916 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11917 binding can be done only inside one CU. Such referenced DIE
11918 therefore cannot be even moved to DW_TAG_partial_unit. */
11919 complaint (&symfile_complaints,
216f72a1
JK
11920 _("DW_AT_call_parameter offset is not in CU for "
11921 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11922 child_die->offset.sect_off, objfile_name (objfile));
d76b7dbc
JK
11923 continue;
11924 }
1788b2d3
JK
11925 parameter->u.param_offset.cu_off = (offset.sect_off
11926 - cu->header.offset.sect_off);
11927 }
11928 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
11929 {
11930 complaint (&symfile_complaints,
11931 _("No DW_FORM_block* DW_AT_location for "
216f72a1 11932 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11933 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11934 continue;
11935 }
24c5c679 11936 else
96408a79 11937 {
24c5c679
JK
11938 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11939 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11940 if (parameter->u.dwarf_reg != -1)
11941 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11942 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11943 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11944 &parameter->u.fb_offset))
11945 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11946 else
11947 {
11948 complaint (&symfile_complaints,
11949 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11950 "for DW_FORM_block* DW_AT_location is supported for "
216f72a1 11951 "DW_TAG_call_site child DIE 0x%x "
24c5c679 11952 "[in module %s]"),
4262abfb 11953 child_die->offset.sect_off, objfile_name (objfile));
24c5c679
JK
11954 continue;
11955 }
96408a79
SA
11956 }
11957
216f72a1
JK
11958 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
11959 if (attr == NULL)
11960 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
11961 if (!attr_form_is_block (attr))
11962 {
11963 complaint (&symfile_complaints,
216f72a1
JK
11964 _("No DW_FORM_block* DW_AT_call_value for "
11965 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11966 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11967 continue;
11968 }
11969 parameter->value = DW_BLOCK (attr)->data;
11970 parameter->value_size = DW_BLOCK (attr)->size;
11971
11972 /* Parameters are not pre-cleared by memset above. */
11973 parameter->data_value = NULL;
11974 parameter->data_value_size = 0;
11975 call_site->parameter_count++;
11976
216f72a1
JK
11977 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
11978 if (attr == NULL)
11979 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
11980 if (attr)
11981 {
11982 if (!attr_form_is_block (attr))
11983 complaint (&symfile_complaints,
216f72a1
JK
11984 _("No DW_FORM_block* DW_AT_call_data_value for "
11985 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
4262abfb 11986 child_die->offset.sect_off, objfile_name (objfile));
96408a79
SA
11987 else
11988 {
11989 parameter->data_value = DW_BLOCK (attr)->data;
11990 parameter->data_value_size = DW_BLOCK (attr)->size;
11991 }
11992 }
11993 }
11994}
11995
43988095
JK
11996/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
11997 reading .debug_rnglists.
11998 Callback's type should be:
11999 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12000 Return true if the attributes are present and valid, otherwise,
12001 return false. */
12002
12003template <typename Callback>
12004static bool
12005dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12006 Callback &&callback)
12007{
12008 struct objfile *objfile = cu->objfile;
12009 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12010 struct comp_unit_head *cu_header = &cu->header;
12011 bfd *obfd = objfile->obfd;
12012 unsigned int addr_size = cu_header->addr_size;
12013 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12014 /* Base address selection entry. */
12015 CORE_ADDR base;
12016 int found_base;
12017 unsigned int dummy;
12018 const gdb_byte *buffer;
12019 CORE_ADDR low = 0;
12020 CORE_ADDR high = 0;
12021 CORE_ADDR baseaddr;
12022 bool overflow = false;
12023
12024 found_base = cu->base_known;
12025 base = cu->base_address;
12026
12027 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12028 if (offset >= dwarf2_per_objfile->rnglists.size)
12029 {
12030 complaint (&symfile_complaints,
12031 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12032 offset);
12033 return false;
12034 }
12035 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12036
12037 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12038
12039 while (1)
12040 {
7814882a
JK
12041 /* Initialize it due to a false compiler warning. */
12042 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
12043 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12044 + dwarf2_per_objfile->rnglists.size);
12045 unsigned int bytes_read;
12046
12047 if (buffer == buf_end)
12048 {
12049 overflow = true;
12050 break;
12051 }
12052 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12053 switch (rlet)
12054 {
12055 case DW_RLE_end_of_list:
12056 break;
12057 case DW_RLE_base_address:
12058 if (buffer + cu->header.addr_size > buf_end)
12059 {
12060 overflow = true;
12061 break;
12062 }
12063 base = read_address (obfd, buffer, cu, &bytes_read);
12064 found_base = 1;
12065 buffer += bytes_read;
12066 break;
12067 case DW_RLE_start_length:
12068 if (buffer + cu->header.addr_size > buf_end)
12069 {
12070 overflow = true;
12071 break;
12072 }
12073 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12074 buffer += bytes_read;
12075 range_end = (range_beginning
12076 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12077 buffer += bytes_read;
12078 if (buffer > buf_end)
12079 {
12080 overflow = true;
12081 break;
12082 }
12083 break;
12084 case DW_RLE_offset_pair:
12085 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12086 buffer += bytes_read;
12087 if (buffer > buf_end)
12088 {
12089 overflow = true;
12090 break;
12091 }
12092 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12093 buffer += bytes_read;
12094 if (buffer > buf_end)
12095 {
12096 overflow = true;
12097 break;
12098 }
12099 break;
12100 case DW_RLE_start_end:
12101 if (buffer + 2 * cu->header.addr_size > buf_end)
12102 {
12103 overflow = true;
12104 break;
12105 }
12106 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12107 buffer += bytes_read;
12108 range_end = read_address (obfd, buffer, cu, &bytes_read);
12109 buffer += bytes_read;
12110 break;
12111 default:
12112 complaint (&symfile_complaints,
12113 _("Invalid .debug_rnglists data (no base address)"));
12114 return false;
12115 }
12116 if (rlet == DW_RLE_end_of_list || overflow)
12117 break;
12118 if (rlet == DW_RLE_base_address)
12119 continue;
12120
12121 if (!found_base)
12122 {
12123 /* We have no valid base address for the ranges
12124 data. */
12125 complaint (&symfile_complaints,
12126 _("Invalid .debug_rnglists data (no base address)"));
12127 return false;
12128 }
12129
12130 if (range_beginning > range_end)
12131 {
12132 /* Inverted range entries are invalid. */
12133 complaint (&symfile_complaints,
12134 _("Invalid .debug_rnglists data (inverted range)"));
12135 return false;
12136 }
12137
12138 /* Empty range entries have no effect. */
12139 if (range_beginning == range_end)
12140 continue;
12141
12142 range_beginning += base;
12143 range_end += base;
12144
12145 /* A not-uncommon case of bad debug info.
12146 Don't pollute the addrmap with bad data. */
12147 if (range_beginning + baseaddr == 0
12148 && !dwarf2_per_objfile->has_section_at_zero)
12149 {
12150 complaint (&symfile_complaints,
12151 _(".debug_rnglists entry has start address of zero"
12152 " [in module %s]"), objfile_name (objfile));
12153 continue;
12154 }
12155
12156 callback (range_beginning, range_end);
12157 }
12158
12159 if (overflow)
12160 {
12161 complaint (&symfile_complaints,
12162 _("Offset %d is not terminated "
12163 "for DW_AT_ranges attribute"),
12164 offset);
12165 return false;
12166 }
12167
12168 return true;
12169}
12170
12171/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12172 Callback's type should be:
12173 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 12174 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 12175
43988095 12176template <typename Callback>
43039443 12177static int
5f46c5a5 12178dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 12179 Callback &&callback)
43039443
JK
12180{
12181 struct objfile *objfile = cu->objfile;
3e29f34a 12182 struct gdbarch *gdbarch = get_objfile_arch (objfile);
43039443
JK
12183 struct comp_unit_head *cu_header = &cu->header;
12184 bfd *obfd = objfile->obfd;
12185 unsigned int addr_size = cu_header->addr_size;
12186 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12187 /* Base address selection entry. */
12188 CORE_ADDR base;
12189 int found_base;
12190 unsigned int dummy;
d521ce57 12191 const gdb_byte *buffer;
ff013f42 12192 CORE_ADDR baseaddr;
43039443 12193
43988095
JK
12194 if (cu_header->version >= 5)
12195 return dwarf2_rnglists_process (offset, cu, callback);
12196
d00adf39
DE
12197 found_base = cu->base_known;
12198 base = cu->base_address;
43039443 12199
be391dca 12200 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 12201 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
12202 {
12203 complaint (&symfile_complaints,
12204 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12205 offset);
12206 return 0;
12207 }
dce234bc 12208 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 12209
e7030f15 12210 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 12211
43039443
JK
12212 while (1)
12213 {
12214 CORE_ADDR range_beginning, range_end;
12215
12216 range_beginning = read_address (obfd, buffer, cu, &dummy);
12217 buffer += addr_size;
12218 range_end = read_address (obfd, buffer, cu, &dummy);
12219 buffer += addr_size;
12220 offset += 2 * addr_size;
12221
12222 /* An end of list marker is a pair of zero addresses. */
12223 if (range_beginning == 0 && range_end == 0)
12224 /* Found the end of list entry. */
12225 break;
12226
12227 /* Each base address selection entry is a pair of 2 values.
12228 The first is the largest possible address, the second is
12229 the base address. Check for a base address here. */
12230 if ((range_beginning & mask) == mask)
12231 {
28d2bfb9
AB
12232 /* If we found the largest possible address, then we already
12233 have the base address in range_end. */
12234 base = range_end;
43039443
JK
12235 found_base = 1;
12236 continue;
12237 }
12238
12239 if (!found_base)
12240 {
12241 /* We have no valid base address for the ranges
12242 data. */
12243 complaint (&symfile_complaints,
12244 _("Invalid .debug_ranges data (no base address)"));
12245 return 0;
12246 }
12247
9277c30c
UW
12248 if (range_beginning > range_end)
12249 {
12250 /* Inverted range entries are invalid. */
12251 complaint (&symfile_complaints,
12252 _("Invalid .debug_ranges data (inverted range)"));
12253 return 0;
12254 }
12255
12256 /* Empty range entries have no effect. */
12257 if (range_beginning == range_end)
12258 continue;
12259
43039443
JK
12260 range_beginning += base;
12261 range_end += base;
12262
01093045
DE
12263 /* A not-uncommon case of bad debug info.
12264 Don't pollute the addrmap with bad data. */
12265 if (range_beginning + baseaddr == 0
12266 && !dwarf2_per_objfile->has_section_at_zero)
12267 {
12268 complaint (&symfile_complaints,
12269 _(".debug_ranges entry has start address of zero"
4262abfb 12270 " [in module %s]"), objfile_name (objfile));
01093045
DE
12271 continue;
12272 }
12273
5f46c5a5
JK
12274 callback (range_beginning, range_end);
12275 }
12276
12277 return 1;
12278}
12279
12280/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12281 Return 1 if the attributes are present and valid, otherwise, return 0.
12282 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12283
12284static int
12285dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12286 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12287 struct partial_symtab *ranges_pst)
12288{
12289 struct objfile *objfile = cu->objfile;
12290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12291 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12292 SECT_OFF_TEXT (objfile));
12293 int low_set = 0;
12294 CORE_ADDR low = 0;
12295 CORE_ADDR high = 0;
12296 int retval;
12297
12298 retval = dwarf2_ranges_process (offset, cu,
12299 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12300 {
9277c30c 12301 if (ranges_pst != NULL)
3e29f34a
MR
12302 {
12303 CORE_ADDR lowpc;
12304 CORE_ADDR highpc;
12305
12306 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12307 range_beginning + baseaddr);
12308 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12309 range_end + baseaddr);
12310 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12311 ranges_pst);
12312 }
ff013f42 12313
43039443
JK
12314 /* FIXME: This is recording everything as a low-high
12315 segment of consecutive addresses. We should have a
12316 data structure for discontiguous block ranges
12317 instead. */
12318 if (! low_set)
12319 {
12320 low = range_beginning;
12321 high = range_end;
12322 low_set = 1;
12323 }
12324 else
12325 {
12326 if (range_beginning < low)
12327 low = range_beginning;
12328 if (range_end > high)
12329 high = range_end;
12330 }
5f46c5a5
JK
12331 });
12332 if (!retval)
12333 return 0;
43039443
JK
12334
12335 if (! low_set)
12336 /* If the first entry is an end-of-list marker, the range
12337 describes an empty scope, i.e. no instructions. */
12338 return 0;
12339
12340 if (low_return)
12341 *low_return = low;
12342 if (high_return)
12343 *high_return = high;
12344 return 1;
12345}
12346
3a2b436a
JK
12347/* Get low and high pc attributes from a die. See enum pc_bounds_kind
12348 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 12349 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 12350
3a2b436a 12351static enum pc_bounds_kind
af34e669 12352dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
12353 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12354 struct partial_symtab *pst)
c906108c
SS
12355{
12356 struct attribute *attr;
91da1414 12357 struct attribute *attr_high;
af34e669
DJ
12358 CORE_ADDR low = 0;
12359 CORE_ADDR high = 0;
e385593e 12360 enum pc_bounds_kind ret;
c906108c 12361
91da1414
MW
12362 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12363 if (attr_high)
af34e669 12364 {
e142c38c 12365 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 12366 if (attr)
91da1414 12367 {
31aa7e4e
JB
12368 low = attr_value_as_address (attr);
12369 high = attr_value_as_address (attr_high);
12370 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12371 high += low;
91da1414 12372 }
af34e669
DJ
12373 else
12374 /* Found high w/o low attribute. */
e385593e 12375 return PC_BOUNDS_INVALID;
af34e669
DJ
12376
12377 /* Found consecutive range of addresses. */
3a2b436a 12378 ret = PC_BOUNDS_HIGH_LOW;
af34e669 12379 }
c906108c 12380 else
af34e669 12381 {
e142c38c 12382 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
12383 if (attr != NULL)
12384 {
ab435259
DE
12385 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12386 We take advantage of the fact that DW_AT_ranges does not appear
12387 in DW_TAG_compile_unit of DWO files. */
12388 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12389 unsigned int ranges_offset = (DW_UNSND (attr)
12390 + (need_ranges_base
12391 ? cu->ranges_base
12392 : 0));
2e3cf129 12393
af34e669 12394 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 12395 .debug_ranges section. */
2e3cf129 12396 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 12397 return PC_BOUNDS_INVALID;
43039443 12398 /* Found discontinuous range of addresses. */
3a2b436a 12399 ret = PC_BOUNDS_RANGES;
af34e669 12400 }
e385593e
JK
12401 else
12402 return PC_BOUNDS_NOT_PRESENT;
af34e669 12403 }
c906108c 12404
9373cf26
JK
12405 /* read_partial_die has also the strict LOW < HIGH requirement. */
12406 if (high <= low)
e385593e 12407 return PC_BOUNDS_INVALID;
c906108c
SS
12408
12409 /* When using the GNU linker, .gnu.linkonce. sections are used to
12410 eliminate duplicate copies of functions and vtables and such.
12411 The linker will arbitrarily choose one and discard the others.
12412 The AT_*_pc values for such functions refer to local labels in
12413 these sections. If the section from that file was discarded, the
12414 labels are not in the output, so the relocs get a value of 0.
12415 If this is a discarded function, mark the pc bounds as invalid,
12416 so that GDB will ignore it. */
72dca2f5 12417 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 12418 return PC_BOUNDS_INVALID;
c906108c
SS
12419
12420 *lowpc = low;
96408a79
SA
12421 if (highpc)
12422 *highpc = high;
af34e669 12423 return ret;
c906108c
SS
12424}
12425
b084d499
JB
12426/* Assuming that DIE represents a subprogram DIE or a lexical block, get
12427 its low and high PC addresses. Do nothing if these addresses could not
12428 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12429 and HIGHPC to the high address if greater than HIGHPC. */
12430
12431static void
12432dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12433 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12434 struct dwarf2_cu *cu)
12435{
12436 CORE_ADDR low, high;
12437 struct die_info *child = die->child;
12438
e385593e 12439 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 12440 {
325fac50
PA
12441 *lowpc = std::min (*lowpc, low);
12442 *highpc = std::max (*highpc, high);
b084d499
JB
12443 }
12444
12445 /* If the language does not allow nested subprograms (either inside
12446 subprograms or lexical blocks), we're done. */
12447 if (cu->language != language_ada)
12448 return;
6e70227d 12449
b084d499
JB
12450 /* Check all the children of the given DIE. If it contains nested
12451 subprograms, then check their pc bounds. Likewise, we need to
12452 check lexical blocks as well, as they may also contain subprogram
12453 definitions. */
12454 while (child && child->tag)
12455 {
12456 if (child->tag == DW_TAG_subprogram
12457 || child->tag == DW_TAG_lexical_block)
12458 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12459 child = sibling_die (child);
12460 }
12461}
12462
fae299cd
DC
12463/* Get the low and high pc's represented by the scope DIE, and store
12464 them in *LOWPC and *HIGHPC. If the correct values can't be
12465 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12466
12467static void
12468get_scope_pc_bounds (struct die_info *die,
12469 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12470 struct dwarf2_cu *cu)
12471{
12472 CORE_ADDR best_low = (CORE_ADDR) -1;
12473 CORE_ADDR best_high = (CORE_ADDR) 0;
12474 CORE_ADDR current_low, current_high;
12475
3a2b436a 12476 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 12477 >= PC_BOUNDS_RANGES)
fae299cd
DC
12478 {
12479 best_low = current_low;
12480 best_high = current_high;
12481 }
12482 else
12483 {
12484 struct die_info *child = die->child;
12485
12486 while (child && child->tag)
12487 {
12488 switch (child->tag) {
12489 case DW_TAG_subprogram:
b084d499 12490 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
12491 break;
12492 case DW_TAG_namespace:
f55ee35c 12493 case DW_TAG_module:
fae299cd
DC
12494 /* FIXME: carlton/2004-01-16: Should we do this for
12495 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12496 that current GCC's always emit the DIEs corresponding
12497 to definitions of methods of classes as children of a
12498 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12499 the DIEs giving the declarations, which could be
12500 anywhere). But I don't see any reason why the
12501 standards says that they have to be there. */
12502 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12503
12504 if (current_low != ((CORE_ADDR) -1))
12505 {
325fac50
PA
12506 best_low = std::min (best_low, current_low);
12507 best_high = std::max (best_high, current_high);
fae299cd
DC
12508 }
12509 break;
12510 default:
0963b4bd 12511 /* Ignore. */
fae299cd
DC
12512 break;
12513 }
12514
12515 child = sibling_die (child);
12516 }
12517 }
12518
12519 *lowpc = best_low;
12520 *highpc = best_high;
12521}
12522
801e3a5b
JB
12523/* Record the address ranges for BLOCK, offset by BASEADDR, as given
12524 in DIE. */
380bca97 12525
801e3a5b
JB
12526static void
12527dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12528 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12529{
bb5ed363 12530 struct objfile *objfile = cu->objfile;
3e29f34a 12531 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 12532 struct attribute *attr;
91da1414 12533 struct attribute *attr_high;
801e3a5b 12534
91da1414
MW
12535 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12536 if (attr_high)
801e3a5b 12537 {
801e3a5b
JB
12538 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12539 if (attr)
12540 {
31aa7e4e
JB
12541 CORE_ADDR low = attr_value_as_address (attr);
12542 CORE_ADDR high = attr_value_as_address (attr_high);
12543
12544 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12545 high += low;
9a619af0 12546
3e29f34a
MR
12547 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12548 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12549 record_block_range (block, low, high - 1);
801e3a5b
JB
12550 }
12551 }
12552
12553 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12554 if (attr)
12555 {
bb5ed363 12556 bfd *obfd = objfile->obfd;
ab435259
DE
12557 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12558 We take advantage of the fact that DW_AT_ranges does not appear
12559 in DW_TAG_compile_unit of DWO files. */
12560 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
12561
12562 /* The value of the DW_AT_ranges attribute is the offset of the
12563 address range list in the .debug_ranges section. */
ab435259
DE
12564 unsigned long offset = (DW_UNSND (attr)
12565 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 12566 const gdb_byte *buffer;
801e3a5b
JB
12567
12568 /* For some target architectures, but not others, the
12569 read_address function sign-extends the addresses it returns.
12570 To recognize base address selection entries, we need a
12571 mask. */
12572 unsigned int addr_size = cu->header.addr_size;
12573 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12574
12575 /* The base address, to which the next pair is relative. Note
12576 that this 'base' is a DWARF concept: most entries in a range
12577 list are relative, to reduce the number of relocs against the
12578 debugging information. This is separate from this function's
12579 'baseaddr' argument, which GDB uses to relocate debugging
12580 information from a shared library based on the address at
12581 which the library was loaded. */
d00adf39
DE
12582 CORE_ADDR base = cu->base_address;
12583 int base_known = cu->base_known;
801e3a5b 12584
5f46c5a5
JK
12585 dwarf2_ranges_process (offset, cu,
12586 [&] (CORE_ADDR start, CORE_ADDR end)
12587 {
58fdfd2c
JK
12588 start += baseaddr;
12589 end += baseaddr;
5f46c5a5
JK
12590 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12591 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12592 record_block_range (block, start, end - 1);
12593 });
801e3a5b
JB
12594 }
12595}
12596
685b1105
JK
12597/* Check whether the producer field indicates either of GCC < 4.6, or the
12598 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 12599
685b1105
JK
12600static void
12601check_producer (struct dwarf2_cu *cu)
60d5a603 12602{
38360086 12603 int major, minor;
60d5a603
JK
12604
12605 if (cu->producer == NULL)
12606 {
12607 /* For unknown compilers expect their behavior is DWARF version
12608 compliant.
12609
12610 GCC started to support .debug_types sections by -gdwarf-4 since
12611 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12612 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12613 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12614 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 12615 }
b1ffba5a 12616 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 12617 {
38360086
MW
12618 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12619 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 12620 }
61012eef 12621 else if (startswith (cu->producer, "Intel(R) C"))
685b1105
JK
12622 cu->producer_is_icc = 1;
12623 else
12624 {
12625 /* For other non-GCC compilers, expect their behavior is DWARF version
12626 compliant. */
60d5a603
JK
12627 }
12628
ba919b58 12629 cu->checked_producer = 1;
685b1105 12630}
ba919b58 12631
685b1105
JK
12632/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12633 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12634 during 4.6.0 experimental. */
12635
12636static int
12637producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12638{
12639 if (!cu->checked_producer)
12640 check_producer (cu);
12641
12642 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
12643}
12644
12645/* Return the default accessibility type if it is not overriden by
12646 DW_AT_accessibility. */
12647
12648static enum dwarf_access_attribute
12649dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12650{
12651 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12652 {
12653 /* The default DWARF 2 accessibility for members is public, the default
12654 accessibility for inheritance is private. */
12655
12656 if (die->tag != DW_TAG_inheritance)
12657 return DW_ACCESS_public;
12658 else
12659 return DW_ACCESS_private;
12660 }
12661 else
12662 {
12663 /* DWARF 3+ defines the default accessibility a different way. The same
12664 rules apply now for DW_TAG_inheritance as for the members and it only
12665 depends on the container kind. */
12666
12667 if (die->parent->tag == DW_TAG_class_type)
12668 return DW_ACCESS_private;
12669 else
12670 return DW_ACCESS_public;
12671 }
12672}
12673
74ac6d43
TT
12674/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12675 offset. If the attribute was not found return 0, otherwise return
12676 1. If it was found but could not properly be handled, set *OFFSET
12677 to 0. */
12678
12679static int
12680handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12681 LONGEST *offset)
12682{
12683 struct attribute *attr;
12684
12685 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12686 if (attr != NULL)
12687 {
12688 *offset = 0;
12689
12690 /* Note that we do not check for a section offset first here.
12691 This is because DW_AT_data_member_location is new in DWARF 4,
12692 so if we see it, we can assume that a constant form is really
12693 a constant and not a section offset. */
12694 if (attr_form_is_constant (attr))
12695 *offset = dwarf2_get_attr_constant_value (attr, 0);
12696 else if (attr_form_is_section_offset (attr))
12697 dwarf2_complex_location_expr_complaint ();
12698 else if (attr_form_is_block (attr))
12699 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12700 else
12701 dwarf2_complex_location_expr_complaint ();
12702
12703 return 1;
12704 }
12705
12706 return 0;
12707}
12708
c906108c
SS
12709/* Add an aggregate field to the field list. */
12710
12711static void
107d2387 12712dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 12713 struct dwarf2_cu *cu)
6e70227d 12714{
e7c27a73 12715 struct objfile *objfile = cu->objfile;
5e2b427d 12716 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12717 struct nextfield *new_field;
12718 struct attribute *attr;
12719 struct field *fp;
15d034d0 12720 const char *fieldname = "";
c906108c
SS
12721
12722 /* Allocate a new field list entry and link it in. */
8d749320 12723 new_field = XNEW (struct nextfield);
b8c9b27d 12724 make_cleanup (xfree, new_field);
c906108c 12725 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
12726
12727 if (die->tag == DW_TAG_inheritance)
12728 {
12729 new_field->next = fip->baseclasses;
12730 fip->baseclasses = new_field;
12731 }
12732 else
12733 {
12734 new_field->next = fip->fields;
12735 fip->fields = new_field;
12736 }
c906108c
SS
12737 fip->nfields++;
12738
e142c38c 12739 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
12740 if (attr)
12741 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
12742 else
12743 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
12744 if (new_field->accessibility != DW_ACCESS_public)
12745 fip->non_public_fields = 1;
60d5a603 12746
e142c38c 12747 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
12748 if (attr)
12749 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
12750 else
12751 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
12752
12753 fp = &new_field->field;
a9a9bd0f 12754
e142c38c 12755 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 12756 {
74ac6d43
TT
12757 LONGEST offset;
12758
a9a9bd0f 12759 /* Data member other than a C++ static data member. */
6e70227d 12760
c906108c 12761 /* Get type of field. */
e7c27a73 12762 fp->type = die_type (die, cu);
c906108c 12763
d6a843b5 12764 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 12765
c906108c 12766 /* Get bit size of field (zero if none). */
e142c38c 12767 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
12768 if (attr)
12769 {
12770 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12771 }
12772 else
12773 {
12774 FIELD_BITSIZE (*fp) = 0;
12775 }
12776
12777 /* Get bit offset of field. */
74ac6d43
TT
12778 if (handle_data_member_location (die, cu, &offset))
12779 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 12780 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
12781 if (attr)
12782 {
5e2b427d 12783 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
12784 {
12785 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
12786 additional bit offset from the MSB of the containing
12787 anonymous object to the MSB of the field. We don't
12788 have to do anything special since we don't need to
12789 know the size of the anonymous object. */
f41f5e61 12790 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
12791 }
12792 else
12793 {
12794 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
12795 MSB of the anonymous object, subtract off the number of
12796 bits from the MSB of the field to the MSB of the
12797 object, and then subtract off the number of bits of
12798 the field itself. The result is the bit offset of
12799 the LSB of the field. */
c906108c
SS
12800 int anonymous_size;
12801 int bit_offset = DW_UNSND (attr);
12802
e142c38c 12803 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12804 if (attr)
12805 {
12806 /* The size of the anonymous object containing
12807 the bit field is explicit, so use the
12808 indicated size (in bytes). */
12809 anonymous_size = DW_UNSND (attr);
12810 }
12811 else
12812 {
12813 /* The size of the anonymous object containing
12814 the bit field must be inferred from the type
12815 attribute of the data member containing the
12816 bit field. */
12817 anonymous_size = TYPE_LENGTH (fp->type);
12818 }
f41f5e61
PA
12819 SET_FIELD_BITPOS (*fp,
12820 (FIELD_BITPOS (*fp)
12821 + anonymous_size * bits_per_byte
12822 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
12823 }
12824 }
da5b30da
AA
12825 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12826 if (attr != NULL)
12827 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12828 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
12829
12830 /* Get name of field. */
39cbfefa
DJ
12831 fieldname = dwarf2_name (die, cu);
12832 if (fieldname == NULL)
12833 fieldname = "";
d8151005
DJ
12834
12835 /* The name is already allocated along with this objfile, so we don't
12836 need to duplicate it for the type. */
12837 fp->name = fieldname;
c906108c
SS
12838
12839 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 12840 pointer or virtual base class pointer) to private. */
e142c38c 12841 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 12842 {
d48cc9dd 12843 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
12844 new_field->accessibility = DW_ACCESS_private;
12845 fip->non_public_fields = 1;
12846 }
12847 }
a9a9bd0f 12848 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 12849 {
a9a9bd0f
DC
12850 /* C++ static member. */
12851
12852 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12853 is a declaration, but all versions of G++ as of this writing
12854 (so through at least 3.2.1) incorrectly generate
12855 DW_TAG_variable tags. */
6e70227d 12856
ff355380 12857 const char *physname;
c906108c 12858
a9a9bd0f 12859 /* Get name of field. */
39cbfefa
DJ
12860 fieldname = dwarf2_name (die, cu);
12861 if (fieldname == NULL)
c906108c
SS
12862 return;
12863
254e6b9e 12864 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
12865 if (attr
12866 /* Only create a symbol if this is an external value.
12867 new_symbol checks this and puts the value in the global symbol
12868 table, which we want. If it is not external, new_symbol
12869 will try to put the value in cu->list_in_scope which is wrong. */
12870 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
12871 {
12872 /* A static const member, not much different than an enum as far as
12873 we're concerned, except that we can support more types. */
12874 new_symbol (die, NULL, cu);
12875 }
12876
2df3850c 12877 /* Get physical name. */
ff355380 12878 physname = dwarf2_physname (fieldname, die, cu);
c906108c 12879
d8151005
DJ
12880 /* The name is already allocated along with this objfile, so we don't
12881 need to duplicate it for the type. */
12882 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 12883 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 12884 FIELD_NAME (*fp) = fieldname;
c906108c
SS
12885 }
12886 else if (die->tag == DW_TAG_inheritance)
12887 {
74ac6d43 12888 LONGEST offset;
d4b96c9a 12889
74ac6d43
TT
12890 /* C++ base class field. */
12891 if (handle_data_member_location (die, cu, &offset))
12892 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 12893 FIELD_BITSIZE (*fp) = 0;
e7c27a73 12894 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
12895 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12896 fip->nbaseclasses++;
12897 }
12898}
12899
98751a41
JK
12900/* Add a typedef defined in the scope of the FIP's class. */
12901
12902static void
12903dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12904 struct dwarf2_cu *cu)
6e70227d 12905{
98751a41 12906 struct typedef_field_list *new_field;
98751a41 12907 struct typedef_field *fp;
98751a41
JK
12908
12909 /* Allocate a new field list entry and link it in. */
8d749320 12910 new_field = XCNEW (struct typedef_field_list);
98751a41
JK
12911 make_cleanup (xfree, new_field);
12912
12913 gdb_assert (die->tag == DW_TAG_typedef);
12914
12915 fp = &new_field->field;
12916
12917 /* Get name of field. */
12918 fp->name = dwarf2_name (die, cu);
12919 if (fp->name == NULL)
12920 return;
12921
12922 fp->type = read_type_die (die, cu);
12923
12924 new_field->next = fip->typedef_field_list;
12925 fip->typedef_field_list = new_field;
12926 fip->typedef_field_list_count++;
12927}
12928
c906108c
SS
12929/* Create the vector of fields, and attach it to the type. */
12930
12931static void
fba45db2 12932dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 12933 struct dwarf2_cu *cu)
c906108c
SS
12934{
12935 int nfields = fip->nfields;
12936
12937 /* Record the field count, allocate space for the array of fields,
12938 and create blank accessibility bitfields if necessary. */
12939 TYPE_NFIELDS (type) = nfields;
12940 TYPE_FIELDS (type) = (struct field *)
12941 TYPE_ALLOC (type, sizeof (struct field) * nfields);
12942 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12943
b4ba55a1 12944 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
12945 {
12946 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12947
12948 TYPE_FIELD_PRIVATE_BITS (type) =
12949 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12950 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12951
12952 TYPE_FIELD_PROTECTED_BITS (type) =
12953 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12954 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12955
774b6a14
TT
12956 TYPE_FIELD_IGNORE_BITS (type) =
12957 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12958 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
12959 }
12960
12961 /* If the type has baseclasses, allocate and clear a bit vector for
12962 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 12963 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
12964 {
12965 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 12966 unsigned char *pointer;
c906108c
SS
12967
12968 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 12969 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 12970 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
12971 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12972 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12973 }
12974
3e43a32a
MS
12975 /* Copy the saved-up fields into the field vector. Start from the head of
12976 the list, adding to the tail of the field array, so that they end up in
12977 the same order in the array in which they were added to the list. */
c906108c
SS
12978 while (nfields-- > 0)
12979 {
7d0ccb61
DJ
12980 struct nextfield *fieldp;
12981
12982 if (fip->fields)
12983 {
12984 fieldp = fip->fields;
12985 fip->fields = fieldp->next;
12986 }
12987 else
12988 {
12989 fieldp = fip->baseclasses;
12990 fip->baseclasses = fieldp->next;
12991 }
12992
12993 TYPE_FIELD (type, nfields) = fieldp->field;
12994 switch (fieldp->accessibility)
c906108c 12995 {
c5aa993b 12996 case DW_ACCESS_private:
b4ba55a1
JB
12997 if (cu->language != language_ada)
12998 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 12999 break;
c906108c 13000
c5aa993b 13001 case DW_ACCESS_protected:
b4ba55a1
JB
13002 if (cu->language != language_ada)
13003 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 13004 break;
c906108c 13005
c5aa993b
JM
13006 case DW_ACCESS_public:
13007 break;
c906108c 13008
c5aa993b
JM
13009 default:
13010 /* Unknown accessibility. Complain and treat it as public. */
13011 {
e2e0b3e5 13012 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 13013 fieldp->accessibility);
c5aa993b
JM
13014 }
13015 break;
c906108c
SS
13016 }
13017 if (nfields < fip->nbaseclasses)
13018 {
7d0ccb61 13019 switch (fieldp->virtuality)
c906108c 13020 {
c5aa993b
JM
13021 case DW_VIRTUALITY_virtual:
13022 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 13023 if (cu->language == language_ada)
a73c6dcd 13024 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
13025 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13026 break;
c906108c
SS
13027 }
13028 }
c906108c
SS
13029 }
13030}
13031
7d27a96d
TT
13032/* Return true if this member function is a constructor, false
13033 otherwise. */
13034
13035static int
13036dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13037{
13038 const char *fieldname;
fe978cb0 13039 const char *type_name;
7d27a96d
TT
13040 int len;
13041
13042 if (die->parent == NULL)
13043 return 0;
13044
13045 if (die->parent->tag != DW_TAG_structure_type
13046 && die->parent->tag != DW_TAG_union_type
13047 && die->parent->tag != DW_TAG_class_type)
13048 return 0;
13049
13050 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
13051 type_name = dwarf2_name (die->parent, cu);
13052 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
13053 return 0;
13054
13055 len = strlen (fieldname);
fe978cb0
PA
13056 return (strncmp (fieldname, type_name, len) == 0
13057 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
13058}
13059
c906108c
SS
13060/* Add a member function to the proper fieldlist. */
13061
13062static void
107d2387 13063dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 13064 struct type *type, struct dwarf2_cu *cu)
c906108c 13065{
e7c27a73 13066 struct objfile *objfile = cu->objfile;
c906108c
SS
13067 struct attribute *attr;
13068 struct fnfieldlist *flp;
13069 int i;
13070 struct fn_field *fnp;
15d034d0 13071 const char *fieldname;
c906108c 13072 struct nextfnfield *new_fnfield;
f792889a 13073 struct type *this_type;
60d5a603 13074 enum dwarf_access_attribute accessibility;
c906108c 13075
b4ba55a1 13076 if (cu->language == language_ada)
a73c6dcd 13077 error (_("unexpected member function in Ada type"));
b4ba55a1 13078
2df3850c 13079 /* Get name of member function. */
39cbfefa
DJ
13080 fieldname = dwarf2_name (die, cu);
13081 if (fieldname == NULL)
2df3850c 13082 return;
c906108c 13083
c906108c
SS
13084 /* Look up member function name in fieldlist. */
13085 for (i = 0; i < fip->nfnfields; i++)
13086 {
27bfe10e 13087 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
13088 break;
13089 }
13090
13091 /* Create new list element if necessary. */
13092 if (i < fip->nfnfields)
13093 flp = &fip->fnfieldlists[i];
13094 else
13095 {
13096 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13097 {
13098 fip->fnfieldlists = (struct fnfieldlist *)
13099 xrealloc (fip->fnfieldlists,
13100 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13101 * sizeof (struct fnfieldlist));
c906108c 13102 if (fip->nfnfields == 0)
c13c43fd 13103 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
13104 }
13105 flp = &fip->fnfieldlists[fip->nfnfields];
13106 flp->name = fieldname;
13107 flp->length = 0;
13108 flp->head = NULL;
3da10d80 13109 i = fip->nfnfields++;
c906108c
SS
13110 }
13111
13112 /* Create a new member function field and chain it to the field list
0963b4bd 13113 entry. */
8d749320 13114 new_fnfield = XNEW (struct nextfnfield);
b8c9b27d 13115 make_cleanup (xfree, new_fnfield);
c906108c
SS
13116 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13117 new_fnfield->next = flp->head;
13118 flp->head = new_fnfield;
13119 flp->length++;
13120
13121 /* Fill in the member function field info. */
13122 fnp = &new_fnfield->fnfield;
3da10d80
KS
13123
13124 /* Delay processing of the physname until later. */
9c37b5ae 13125 if (cu->language == language_cplus)
3da10d80
KS
13126 {
13127 add_to_method_list (type, i, flp->length - 1, fieldname,
13128 die, cu);
13129 }
13130 else
13131 {
1d06ead6 13132 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
13133 fnp->physname = physname ? physname : "";
13134 }
13135
c906108c 13136 fnp->type = alloc_type (objfile);
f792889a
DJ
13137 this_type = read_type_die (die, cu);
13138 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 13139 {
f792889a 13140 int nparams = TYPE_NFIELDS (this_type);
c906108c 13141
f792889a 13142 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
13143 of the method itself (TYPE_CODE_METHOD). */
13144 smash_to_method_type (fnp->type, type,
f792889a
DJ
13145 TYPE_TARGET_TYPE (this_type),
13146 TYPE_FIELDS (this_type),
13147 TYPE_NFIELDS (this_type),
13148 TYPE_VARARGS (this_type));
c906108c
SS
13149
13150 /* Handle static member functions.
c5aa993b 13151 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
13152 member functions. G++ helps GDB by marking the first
13153 parameter for non-static member functions (which is the this
13154 pointer) as artificial. We obtain this information from
13155 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 13156 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
13157 fnp->voffset = VOFFSET_STATIC;
13158 }
13159 else
e2e0b3e5 13160 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 13161 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
13162
13163 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 13164 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 13165 fnp->fcontext = die_containing_type (die, cu);
c906108c 13166
3e43a32a
MS
13167 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13168 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
13169
13170 /* Get accessibility. */
e142c38c 13171 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 13172 if (attr)
aead7601 13173 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
13174 else
13175 accessibility = dwarf2_default_access_attribute (die, cu);
13176 switch (accessibility)
c906108c 13177 {
60d5a603
JK
13178 case DW_ACCESS_private:
13179 fnp->is_private = 1;
13180 break;
13181 case DW_ACCESS_protected:
13182 fnp->is_protected = 1;
13183 break;
c906108c
SS
13184 }
13185
b02dede2 13186 /* Check for artificial methods. */
e142c38c 13187 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
13188 if (attr && DW_UNSND (attr) != 0)
13189 fnp->is_artificial = 1;
13190
7d27a96d
TT
13191 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13192
0d564a31 13193 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
13194 function. For older versions of GCC, this is an offset in the
13195 appropriate virtual table, as specified by DW_AT_containing_type.
13196 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
13197 to the object address. */
13198
e142c38c 13199 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 13200 if (attr)
8e19ed76 13201 {
aec5aa8b 13202 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 13203 {
aec5aa8b
TT
13204 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13205 {
13206 /* Old-style GCC. */
13207 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13208 }
13209 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13210 || (DW_BLOCK (attr)->size > 1
13211 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13212 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13213 {
aec5aa8b
TT
13214 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13215 if ((fnp->voffset % cu->header.addr_size) != 0)
13216 dwarf2_complex_location_expr_complaint ();
13217 else
13218 fnp->voffset /= cu->header.addr_size;
13219 fnp->voffset += 2;
13220 }
13221 else
13222 dwarf2_complex_location_expr_complaint ();
13223
13224 if (!fnp->fcontext)
7e993ebf
KS
13225 {
13226 /* If there is no `this' field and no DW_AT_containing_type,
13227 we cannot actually find a base class context for the
13228 vtable! */
13229 if (TYPE_NFIELDS (this_type) == 0
13230 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13231 {
13232 complaint (&symfile_complaints,
13233 _("cannot determine context for virtual member "
13234 "function \"%s\" (offset %d)"),
13235 fieldname, die->offset.sect_off);
13236 }
13237 else
13238 {
13239 fnp->fcontext
13240 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13241 }
13242 }
aec5aa8b 13243 }
3690dd37 13244 else if (attr_form_is_section_offset (attr))
8e19ed76 13245 {
4d3c2250 13246 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
13247 }
13248 else
13249 {
4d3c2250
KB
13250 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13251 fieldname);
8e19ed76 13252 }
0d564a31 13253 }
d48cc9dd
DJ
13254 else
13255 {
13256 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13257 if (attr && DW_UNSND (attr))
13258 {
13259 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13260 complaint (&symfile_complaints,
3e43a32a
MS
13261 _("Member function \"%s\" (offset %d) is virtual "
13262 "but the vtable offset is not specified"),
b64f50a1 13263 fieldname, die->offset.sect_off);
9655fd1a 13264 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
13265 TYPE_CPLUS_DYNAMIC (type) = 1;
13266 }
13267 }
c906108c
SS
13268}
13269
13270/* Create the vector of member function fields, and attach it to the type. */
13271
13272static void
fba45db2 13273dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 13274 struct dwarf2_cu *cu)
c906108c
SS
13275{
13276 struct fnfieldlist *flp;
c906108c
SS
13277 int i;
13278
b4ba55a1 13279 if (cu->language == language_ada)
a73c6dcd 13280 error (_("unexpected member functions in Ada type"));
b4ba55a1 13281
c906108c
SS
13282 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13283 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13284 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13285
13286 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13287 {
13288 struct nextfnfield *nfp = flp->head;
13289 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13290 int k;
13291
13292 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13293 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13294 fn_flp->fn_fields = (struct fn_field *)
13295 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13296 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 13297 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
13298 }
13299
13300 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
13301}
13302
1168df01
JB
13303/* Returns non-zero if NAME is the name of a vtable member in CU's
13304 language, zero otherwise. */
13305static int
13306is_vtable_name (const char *name, struct dwarf2_cu *cu)
13307{
13308 static const char vptr[] = "_vptr";
987504bb 13309 static const char vtable[] = "vtable";
1168df01 13310
9c37b5ae
TT
13311 /* Look for the C++ form of the vtable. */
13312 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
13313 return 1;
13314
13315 return 0;
13316}
13317
c0dd20ea 13318/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
13319 functions, with the ABI-specified layout. If TYPE describes
13320 such a structure, smash it into a member function type.
61049d3b
DJ
13321
13322 GCC shouldn't do this; it should just output pointer to member DIEs.
13323 This is GCC PR debug/28767. */
c0dd20ea 13324
0b92b5bb
TT
13325static void
13326quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 13327{
09e2d7c7 13328 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
13329
13330 /* Check for a structure with no name and two children. */
0b92b5bb
TT
13331 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13332 return;
c0dd20ea
DJ
13333
13334 /* Check for __pfn and __delta members. */
0b92b5bb
TT
13335 if (TYPE_FIELD_NAME (type, 0) == NULL
13336 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13337 || TYPE_FIELD_NAME (type, 1) == NULL
13338 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13339 return;
c0dd20ea
DJ
13340
13341 /* Find the type of the method. */
0b92b5bb 13342 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
13343 if (pfn_type == NULL
13344 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13345 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 13346 return;
c0dd20ea
DJ
13347
13348 /* Look for the "this" argument. */
13349 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13350 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 13351 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 13352 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 13353 return;
c0dd20ea 13354
09e2d7c7 13355 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 13356 new_type = alloc_type (objfile);
09e2d7c7 13357 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
13358 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13359 TYPE_VARARGS (pfn_type));
0b92b5bb 13360 smash_to_methodptr_type (type, new_type);
c0dd20ea 13361}
1168df01 13362
685b1105
JK
13363/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13364 (icc). */
13365
13366static int
13367producer_is_icc (struct dwarf2_cu *cu)
13368{
13369 if (!cu->checked_producer)
13370 check_producer (cu);
13371
13372 return cu->producer_is_icc;
13373}
13374
c906108c 13375/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
13376 (definition) to create a type for the structure or union. Fill in
13377 the type's name and general properties; the members will not be
83655187
DE
13378 processed until process_structure_scope. A symbol table entry for
13379 the type will also not be done until process_structure_scope (assuming
13380 the type has a name).
c906108c 13381
c767944b
DJ
13382 NOTE: we need to call these functions regardless of whether or not the
13383 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 13384 structure or union. This gets the type entered into our set of
83655187 13385 user defined types. */
c906108c 13386
f792889a 13387static struct type *
134d01f1 13388read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13389{
e7c27a73 13390 struct objfile *objfile = cu->objfile;
c906108c
SS
13391 struct type *type;
13392 struct attribute *attr;
15d034d0 13393 const char *name;
c906108c 13394
348e048f
DE
13395 /* If the definition of this type lives in .debug_types, read that type.
13396 Don't follow DW_AT_specification though, that will take us back up
13397 the chain and we want to go down. */
45e58e77 13398 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13399 if (attr)
13400 {
ac9ec31b 13401 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13402
ac9ec31b 13403 /* The type's CU may not be the same as CU.
02142a6c 13404 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13405 return set_die_type (die, type, cu);
13406 }
13407
c0dd20ea 13408 type = alloc_type (objfile);
c906108c 13409 INIT_CPLUS_SPECIFIC (type);
93311388 13410
39cbfefa
DJ
13411 name = dwarf2_name (die, cu);
13412 if (name != NULL)
c906108c 13413 {
987504bb 13414 if (cu->language == language_cplus
c44af4eb
TT
13415 || cu->language == language_d
13416 || cu->language == language_rust)
63d06c5c 13417 {
15d034d0 13418 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
13419
13420 /* dwarf2_full_name might have already finished building the DIE's
13421 type. If so, there is no need to continue. */
13422 if (get_die_type (die, cu) != NULL)
13423 return get_die_type (die, cu);
13424
13425 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
13426 if (die->tag == DW_TAG_structure_type
13427 || die->tag == DW_TAG_class_type)
13428 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
13429 }
13430 else
13431 {
d8151005
DJ
13432 /* The name is already allocated along with this objfile, so
13433 we don't need to duplicate it for the type. */
7d455152 13434 TYPE_TAG_NAME (type) = name;
94af9270
KS
13435 if (die->tag == DW_TAG_class_type)
13436 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 13437 }
c906108c
SS
13438 }
13439
13440 if (die->tag == DW_TAG_structure_type)
13441 {
13442 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13443 }
13444 else if (die->tag == DW_TAG_union_type)
13445 {
13446 TYPE_CODE (type) = TYPE_CODE_UNION;
13447 }
13448 else
13449 {
4753d33b 13450 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
13451 }
13452
0cc2414c
TT
13453 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13454 TYPE_DECLARED_CLASS (type) = 1;
13455
e142c38c 13456 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13457 if (attr)
13458 {
155bfbd3
JB
13459 if (attr_form_is_constant (attr))
13460 TYPE_LENGTH (type) = DW_UNSND (attr);
13461 else
13462 {
13463 /* For the moment, dynamic type sizes are not supported
13464 by GDB's struct type. The actual size is determined
13465 on-demand when resolving the type of a given object,
13466 so set the type's length to zero for now. Otherwise,
13467 we record an expression as the length, and that expression
13468 could lead to a very large value, which could eventually
13469 lead to us trying to allocate that much memory when creating
13470 a value of that type. */
13471 TYPE_LENGTH (type) = 0;
13472 }
c906108c
SS
13473 }
13474 else
13475 {
13476 TYPE_LENGTH (type) = 0;
13477 }
13478
422b1cb0 13479 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
685b1105
JK
13480 {
13481 /* ICC does not output the required DW_AT_declaration
13482 on incomplete types, but gives them a size of zero. */
422b1cb0 13483 TYPE_STUB (type) = 1;
685b1105
JK
13484 }
13485 else
13486 TYPE_STUB_SUPPORTED (type) = 1;
13487
dc718098 13488 if (die_is_declaration (die, cu))
876cecd0 13489 TYPE_STUB (type) = 1;
a6c727b2
DJ
13490 else if (attr == NULL && die->child == NULL
13491 && producer_is_realview (cu->producer))
13492 /* RealView does not output the required DW_AT_declaration
13493 on incomplete types. */
13494 TYPE_STUB (type) = 1;
dc718098 13495
c906108c
SS
13496 /* We need to add the type field to the die immediately so we don't
13497 infinitely recurse when dealing with pointers to the structure
0963b4bd 13498 type within the structure itself. */
1c379e20 13499 set_die_type (die, type, cu);
c906108c 13500
7e314c57
JK
13501 /* set_die_type should be already done. */
13502 set_descriptive_type (type, die, cu);
13503
c767944b
DJ
13504 return type;
13505}
13506
13507/* Finish creating a structure or union type, including filling in
13508 its members and creating a symbol for it. */
13509
13510static void
13511process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13512{
13513 struct objfile *objfile = cu->objfile;
ca040673 13514 struct die_info *child_die;
c767944b
DJ
13515 struct type *type;
13516
13517 type = get_die_type (die, cu);
13518 if (type == NULL)
13519 type = read_structure_type (die, cu);
13520
e142c38c 13521 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
13522 {
13523 struct field_info fi;
34eaf542 13524 VEC (symbolp) *template_args = NULL;
c767944b 13525 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
13526
13527 memset (&fi, 0, sizeof (struct field_info));
13528
639d11d3 13529 child_die = die->child;
c906108c
SS
13530
13531 while (child_die && child_die->tag)
13532 {
a9a9bd0f
DC
13533 if (child_die->tag == DW_TAG_member
13534 || child_die->tag == DW_TAG_variable)
c906108c 13535 {
a9a9bd0f
DC
13536 /* NOTE: carlton/2002-11-05: A C++ static data member
13537 should be a DW_TAG_member that is a declaration, but
13538 all versions of G++ as of this writing (so through at
13539 least 3.2.1) incorrectly generate DW_TAG_variable
13540 tags for them instead. */
e7c27a73 13541 dwarf2_add_field (&fi, child_die, cu);
c906108c 13542 }
8713b1b1 13543 else if (child_die->tag == DW_TAG_subprogram)
c906108c 13544 {
e98c9e7c
TT
13545 /* Rust doesn't have member functions in the C++ sense.
13546 However, it does emit ordinary functions as children
13547 of a struct DIE. */
13548 if (cu->language == language_rust)
13549 read_func_scope (child_die, cu);
13550 else
13551 {
13552 /* C++ member function. */
13553 dwarf2_add_member_fn (&fi, child_die, type, cu);
13554 }
c906108c
SS
13555 }
13556 else if (child_die->tag == DW_TAG_inheritance)
13557 {
13558 /* C++ base class field. */
e7c27a73 13559 dwarf2_add_field (&fi, child_die, cu);
c906108c 13560 }
98751a41
JK
13561 else if (child_die->tag == DW_TAG_typedef)
13562 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
13563 else if (child_die->tag == DW_TAG_template_type_param
13564 || child_die->tag == DW_TAG_template_value_param)
13565 {
13566 struct symbol *arg = new_symbol (child_die, NULL, cu);
13567
f1078f66
DJ
13568 if (arg != NULL)
13569 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
13570 }
13571
c906108c
SS
13572 child_die = sibling_die (child_die);
13573 }
13574
34eaf542
TT
13575 /* Attach template arguments to type. */
13576 if (! VEC_empty (symbolp, template_args))
13577 {
13578 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13579 TYPE_N_TEMPLATE_ARGUMENTS (type)
13580 = VEC_length (symbolp, template_args);
13581 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
13582 = XOBNEWVEC (&objfile->objfile_obstack,
13583 struct symbol *,
13584 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542
TT
13585 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13586 VEC_address (symbolp, template_args),
13587 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13588 * sizeof (struct symbol *)));
13589 VEC_free (symbolp, template_args);
13590 }
13591
c906108c
SS
13592 /* Attach fields and member functions to the type. */
13593 if (fi.nfields)
e7c27a73 13594 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
13595 if (fi.nfnfields)
13596 {
e7c27a73 13597 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 13598
c5aa993b 13599 /* Get the type which refers to the base class (possibly this
c906108c 13600 class itself) which contains the vtable pointer for the current
0d564a31
DJ
13601 class from the DW_AT_containing_type attribute. This use of
13602 DW_AT_containing_type is a GNU extension. */
c906108c 13603
e142c38c 13604 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 13605 {
e7c27a73 13606 struct type *t = die_containing_type (die, cu);
c906108c 13607
ae6ae975 13608 set_type_vptr_basetype (type, t);
c906108c
SS
13609 if (type == t)
13610 {
c906108c
SS
13611 int i;
13612
13613 /* Our own class provides vtbl ptr. */
13614 for (i = TYPE_NFIELDS (t) - 1;
13615 i >= TYPE_N_BASECLASSES (t);
13616 --i)
13617 {
0d5cff50 13618 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 13619
1168df01 13620 if (is_vtable_name (fieldname, cu))
c906108c 13621 {
ae6ae975 13622 set_type_vptr_fieldno (type, i);
c906108c
SS
13623 break;
13624 }
13625 }
13626
13627 /* Complain if virtual function table field not found. */
13628 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 13629 complaint (&symfile_complaints,
3e43a32a
MS
13630 _("virtual function table pointer "
13631 "not found when defining class '%s'"),
4d3c2250
KB
13632 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13633 "");
c906108c
SS
13634 }
13635 else
13636 {
ae6ae975 13637 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
13638 }
13639 }
f6235d4c 13640 else if (cu->producer
61012eef 13641 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
13642 {
13643 /* The IBM XLC compiler does not provide direct indication
13644 of the containing type, but the vtable pointer is
13645 always named __vfp. */
13646
13647 int i;
13648
13649 for (i = TYPE_NFIELDS (type) - 1;
13650 i >= TYPE_N_BASECLASSES (type);
13651 --i)
13652 {
13653 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13654 {
ae6ae975
DE
13655 set_type_vptr_fieldno (type, i);
13656 set_type_vptr_basetype (type, type);
f6235d4c
EZ
13657 break;
13658 }
13659 }
13660 }
c906108c 13661 }
98751a41
JK
13662
13663 /* Copy fi.typedef_field_list linked list elements content into the
13664 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13665 if (fi.typedef_field_list)
13666 {
13667 int i = fi.typedef_field_list_count;
13668
a0d7a4ff 13669 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 13670 TYPE_TYPEDEF_FIELD_ARRAY (type)
224c3ddb
SM
13671 = ((struct typedef_field *)
13672 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
98751a41
JK
13673 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13674
13675 /* Reverse the list order to keep the debug info elements order. */
13676 while (--i >= 0)
13677 {
13678 struct typedef_field *dest, *src;
6e70227d 13679
98751a41
JK
13680 dest = &TYPE_TYPEDEF_FIELD (type, i);
13681 src = &fi.typedef_field_list->field;
13682 fi.typedef_field_list = fi.typedef_field_list->next;
13683 *dest = *src;
13684 }
13685 }
c767944b
DJ
13686
13687 do_cleanups (back_to);
c906108c 13688 }
63d06c5c 13689
bb5ed363 13690 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 13691
90aeadfc
DC
13692 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13693 snapshots) has been known to create a die giving a declaration
13694 for a class that has, as a child, a die giving a definition for a
13695 nested class. So we have to process our children even if the
13696 current die is a declaration. Normally, of course, a declaration
13697 won't have any children at all. */
134d01f1 13698
ca040673
DE
13699 child_die = die->child;
13700
90aeadfc
DC
13701 while (child_die != NULL && child_die->tag)
13702 {
13703 if (child_die->tag == DW_TAG_member
13704 || child_die->tag == DW_TAG_variable
34eaf542
TT
13705 || child_die->tag == DW_TAG_inheritance
13706 || child_die->tag == DW_TAG_template_value_param
13707 || child_die->tag == DW_TAG_template_type_param)
134d01f1 13708 {
90aeadfc 13709 /* Do nothing. */
134d01f1 13710 }
90aeadfc
DC
13711 else
13712 process_die (child_die, cu);
134d01f1 13713
90aeadfc 13714 child_die = sibling_die (child_die);
134d01f1
DJ
13715 }
13716
fa4028e9
JB
13717 /* Do not consider external references. According to the DWARF standard,
13718 these DIEs are identified by the fact that they have no byte_size
13719 attribute, and a declaration attribute. */
13720 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13721 || !die_is_declaration (die, cu))
c767944b 13722 new_symbol (die, type, cu);
134d01f1
DJ
13723}
13724
55426c9d
JB
13725/* Assuming DIE is an enumeration type, and TYPE is its associated type,
13726 update TYPE using some information only available in DIE's children. */
13727
13728static void
13729update_enumeration_type_from_children (struct die_info *die,
13730 struct type *type,
13731 struct dwarf2_cu *cu)
13732{
13733 struct obstack obstack;
60f7655a 13734 struct die_info *child_die;
55426c9d
JB
13735 int unsigned_enum = 1;
13736 int flag_enum = 1;
13737 ULONGEST mask = 0;
13738 struct cleanup *old_chain;
13739
13740 obstack_init (&obstack);
13741 old_chain = make_cleanup_obstack_free (&obstack);
13742
60f7655a
DE
13743 for (child_die = die->child;
13744 child_die != NULL && child_die->tag;
13745 child_die = sibling_die (child_die))
55426c9d
JB
13746 {
13747 struct attribute *attr;
13748 LONGEST value;
13749 const gdb_byte *bytes;
13750 struct dwarf2_locexpr_baton *baton;
13751 const char *name;
60f7655a 13752
55426c9d
JB
13753 if (child_die->tag != DW_TAG_enumerator)
13754 continue;
13755
13756 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13757 if (attr == NULL)
13758 continue;
13759
13760 name = dwarf2_name (child_die, cu);
13761 if (name == NULL)
13762 name = "<anonymous enumerator>";
13763
13764 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13765 &value, &bytes, &baton);
13766 if (value < 0)
13767 {
13768 unsigned_enum = 0;
13769 flag_enum = 0;
13770 }
13771 else if ((mask & value) != 0)
13772 flag_enum = 0;
13773 else
13774 mask |= value;
13775
13776 /* If we already know that the enum type is neither unsigned, nor
13777 a flag type, no need to look at the rest of the enumerates. */
13778 if (!unsigned_enum && !flag_enum)
13779 break;
55426c9d
JB
13780 }
13781
13782 if (unsigned_enum)
13783 TYPE_UNSIGNED (type) = 1;
13784 if (flag_enum)
13785 TYPE_FLAG_ENUM (type) = 1;
13786
13787 do_cleanups (old_chain);
13788}
13789
134d01f1
DJ
13790/* Given a DW_AT_enumeration_type die, set its type. We do not
13791 complete the type's fields yet, or create any symbols. */
c906108c 13792
f792889a 13793static struct type *
134d01f1 13794read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13795{
e7c27a73 13796 struct objfile *objfile = cu->objfile;
c906108c 13797 struct type *type;
c906108c 13798 struct attribute *attr;
0114d602 13799 const char *name;
134d01f1 13800
348e048f
DE
13801 /* If the definition of this type lives in .debug_types, read that type.
13802 Don't follow DW_AT_specification though, that will take us back up
13803 the chain and we want to go down. */
45e58e77 13804 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
13805 if (attr)
13806 {
ac9ec31b 13807 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 13808
ac9ec31b 13809 /* The type's CU may not be the same as CU.
02142a6c 13810 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
13811 return set_die_type (die, type, cu);
13812 }
13813
c906108c
SS
13814 type = alloc_type (objfile);
13815
13816 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 13817 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 13818 if (name != NULL)
7d455152 13819 TYPE_TAG_NAME (type) = name;
c906108c 13820
0626fc76
TT
13821 attr = dwarf2_attr (die, DW_AT_type, cu);
13822 if (attr != NULL)
13823 {
13824 struct type *underlying_type = die_type (die, cu);
13825
13826 TYPE_TARGET_TYPE (type) = underlying_type;
13827 }
13828
e142c38c 13829 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13830 if (attr)
13831 {
13832 TYPE_LENGTH (type) = DW_UNSND (attr);
13833 }
13834 else
13835 {
13836 TYPE_LENGTH (type) = 0;
13837 }
13838
137033e9
JB
13839 /* The enumeration DIE can be incomplete. In Ada, any type can be
13840 declared as private in the package spec, and then defined only
13841 inside the package body. Such types are known as Taft Amendment
13842 Types. When another package uses such a type, an incomplete DIE
13843 may be generated by the compiler. */
02eb380e 13844 if (die_is_declaration (die, cu))
876cecd0 13845 TYPE_STUB (type) = 1;
02eb380e 13846
0626fc76
TT
13847 /* Finish the creation of this type by using the enum's children.
13848 We must call this even when the underlying type has been provided
13849 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
13850 update_enumeration_type_from_children (die, type, cu);
13851
0626fc76
TT
13852 /* If this type has an underlying type that is not a stub, then we
13853 may use its attributes. We always use the "unsigned" attribute
13854 in this situation, because ordinarily we guess whether the type
13855 is unsigned -- but the guess can be wrong and the underlying type
13856 can tell us the reality. However, we defer to a local size
13857 attribute if one exists, because this lets the compiler override
13858 the underlying type if needed. */
13859 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13860 {
13861 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13862 if (TYPE_LENGTH (type) == 0)
13863 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13864 }
13865
3d567982
TT
13866 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13867
f792889a 13868 return set_die_type (die, type, cu);
134d01f1
DJ
13869}
13870
13871/* Given a pointer to a die which begins an enumeration, process all
13872 the dies that define the members of the enumeration, and create the
13873 symbol for the enumeration type.
13874
13875 NOTE: We reverse the order of the element list. */
13876
13877static void
13878process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13879{
f792889a 13880 struct type *this_type;
134d01f1 13881
f792889a
DJ
13882 this_type = get_die_type (die, cu);
13883 if (this_type == NULL)
13884 this_type = read_enumeration_type (die, cu);
9dc481d3 13885
639d11d3 13886 if (die->child != NULL)
c906108c 13887 {
9dc481d3
DE
13888 struct die_info *child_die;
13889 struct symbol *sym;
13890 struct field *fields = NULL;
13891 int num_fields = 0;
15d034d0 13892 const char *name;
9dc481d3 13893
639d11d3 13894 child_die = die->child;
c906108c
SS
13895 while (child_die && child_die->tag)
13896 {
13897 if (child_die->tag != DW_TAG_enumerator)
13898 {
e7c27a73 13899 process_die (child_die, cu);
c906108c
SS
13900 }
13901 else
13902 {
39cbfefa
DJ
13903 name = dwarf2_name (child_die, cu);
13904 if (name)
c906108c 13905 {
f792889a 13906 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
13907
13908 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13909 {
13910 fields = (struct field *)
13911 xrealloc (fields,
13912 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 13913 * sizeof (struct field));
c906108c
SS
13914 }
13915
3567439c 13916 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 13917 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 13918 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
13919 FIELD_BITSIZE (fields[num_fields]) = 0;
13920
13921 num_fields++;
13922 }
13923 }
13924
13925 child_die = sibling_die (child_die);
13926 }
13927
13928 if (num_fields)
13929 {
f792889a
DJ
13930 TYPE_NFIELDS (this_type) = num_fields;
13931 TYPE_FIELDS (this_type) = (struct field *)
13932 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13933 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 13934 sizeof (struct field) * num_fields);
b8c9b27d 13935 xfree (fields);
c906108c 13936 }
c906108c 13937 }
134d01f1 13938
6c83ed52
TT
13939 /* If we are reading an enum from a .debug_types unit, and the enum
13940 is a declaration, and the enum is not the signatured type in the
13941 unit, then we do not want to add a symbol for it. Adding a
13942 symbol would in some cases obscure the true definition of the
13943 enum, giving users an incomplete type when the definition is
13944 actually available. Note that we do not want to do this for all
13945 enums which are just declarations, because C++0x allows forward
13946 enum declarations. */
3019eac3 13947 if (cu->per_cu->is_debug_types
6c83ed52
TT
13948 && die_is_declaration (die, cu))
13949 {
52dc124a 13950 struct signatured_type *sig_type;
6c83ed52 13951
c0f78cd4 13952 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
13953 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13954 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
13955 return;
13956 }
13957
f792889a 13958 new_symbol (die, this_type, cu);
c906108c
SS
13959}
13960
13961/* Extract all information from a DW_TAG_array_type DIE and put it in
13962 the DIE's type field. For now, this only handles one dimensional
13963 arrays. */
13964
f792889a 13965static struct type *
e7c27a73 13966read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13967{
e7c27a73 13968 struct objfile *objfile = cu->objfile;
c906108c 13969 struct die_info *child_die;
7e314c57 13970 struct type *type;
c906108c
SS
13971 struct type *element_type, *range_type, *index_type;
13972 struct type **range_types = NULL;
13973 struct attribute *attr;
13974 int ndim = 0;
13975 struct cleanup *back_to;
15d034d0 13976 const char *name;
dc53a7ad 13977 unsigned int bit_stride = 0;
c906108c 13978
e7c27a73 13979 element_type = die_type (die, cu);
c906108c 13980
7e314c57
JK
13981 /* The die_type call above may have already set the type for this DIE. */
13982 type = get_die_type (die, cu);
13983 if (type)
13984 return type;
13985
dc53a7ad
JB
13986 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13987 if (attr != NULL)
13988 bit_stride = DW_UNSND (attr) * 8;
13989
13990 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13991 if (attr != NULL)
13992 bit_stride = DW_UNSND (attr);
13993
c906108c
SS
13994 /* Irix 6.2 native cc creates array types without children for
13995 arrays with unspecified length. */
639d11d3 13996 if (die->child == NULL)
c906108c 13997 {
46bf5051 13998 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 13999 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad
JB
14000 type = create_array_type_with_stride (NULL, element_type, range_type,
14001 bit_stride);
f792889a 14002 return set_die_type (die, type, cu);
c906108c
SS
14003 }
14004
14005 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 14006 child_die = die->child;
c906108c
SS
14007 while (child_die && child_die->tag)
14008 {
14009 if (child_die->tag == DW_TAG_subrange_type)
14010 {
f792889a 14011 struct type *child_type = read_type_die (child_die, cu);
9a619af0 14012
f792889a 14013 if (child_type != NULL)
a02abb62 14014 {
0963b4bd
MS
14015 /* The range type was succesfully read. Save it for the
14016 array type creation. */
a02abb62
JB
14017 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14018 {
14019 range_types = (struct type **)
14020 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14021 * sizeof (struct type *));
14022 if (ndim == 0)
14023 make_cleanup (free_current_contents, &range_types);
14024 }
f792889a 14025 range_types[ndim++] = child_type;
a02abb62 14026 }
c906108c
SS
14027 }
14028 child_die = sibling_die (child_die);
14029 }
14030
14031 /* Dwarf2 dimensions are output from left to right, create the
14032 necessary array types in backwards order. */
7ca2d3a3 14033
c906108c 14034 type = element_type;
7ca2d3a3
DL
14035
14036 if (read_array_order (die, cu) == DW_ORD_col_major)
14037 {
14038 int i = 0;
9a619af0 14039
7ca2d3a3 14040 while (i < ndim)
dc53a7ad
JB
14041 type = create_array_type_with_stride (NULL, type, range_types[i++],
14042 bit_stride);
7ca2d3a3
DL
14043 }
14044 else
14045 {
14046 while (ndim-- > 0)
dc53a7ad
JB
14047 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14048 bit_stride);
7ca2d3a3 14049 }
c906108c 14050
f5f8a009
EZ
14051 /* Understand Dwarf2 support for vector types (like they occur on
14052 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14053 array type. This is not part of the Dwarf2/3 standard yet, but a
14054 custom vendor extension. The main difference between a regular
14055 array and the vector variant is that vectors are passed by value
14056 to functions. */
e142c38c 14057 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 14058 if (attr)
ea37ba09 14059 make_vector_type (type);
f5f8a009 14060
dbc98a8b
KW
14061 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14062 implementation may choose to implement triple vectors using this
14063 attribute. */
14064 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14065 if (attr)
14066 {
14067 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14068 TYPE_LENGTH (type) = DW_UNSND (attr);
14069 else
3e43a32a
MS
14070 complaint (&symfile_complaints,
14071 _("DW_AT_byte_size for array type smaller "
14072 "than the total size of elements"));
dbc98a8b
KW
14073 }
14074
39cbfefa
DJ
14075 name = dwarf2_name (die, cu);
14076 if (name)
14077 TYPE_NAME (type) = name;
6e70227d 14078
0963b4bd 14079 /* Install the type in the die. */
7e314c57
JK
14080 set_die_type (die, type, cu);
14081
14082 /* set_die_type should be already done. */
b4ba55a1
JB
14083 set_descriptive_type (type, die, cu);
14084
c906108c
SS
14085 do_cleanups (back_to);
14086
7e314c57 14087 return type;
c906108c
SS
14088}
14089
7ca2d3a3 14090static enum dwarf_array_dim_ordering
6e70227d 14091read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
14092{
14093 struct attribute *attr;
14094
14095 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14096
aead7601
SM
14097 if (attr)
14098 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 14099
0963b4bd
MS
14100 /* GNU F77 is a special case, as at 08/2004 array type info is the
14101 opposite order to the dwarf2 specification, but data is still
14102 laid out as per normal fortran.
7ca2d3a3 14103
0963b4bd
MS
14104 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14105 version checking. */
7ca2d3a3 14106
905e0470
PM
14107 if (cu->language == language_fortran
14108 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
14109 {
14110 return DW_ORD_row_major;
14111 }
14112
6e70227d 14113 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
14114 {
14115 case array_column_major:
14116 return DW_ORD_col_major;
14117 case array_row_major:
14118 default:
14119 return DW_ORD_row_major;
14120 };
14121}
14122
72019c9c 14123/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 14124 the DIE's type field. */
72019c9c 14125
f792889a 14126static struct type *
72019c9c
GM
14127read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14128{
7e314c57
JK
14129 struct type *domain_type, *set_type;
14130 struct attribute *attr;
f792889a 14131
7e314c57
JK
14132 domain_type = die_type (die, cu);
14133
14134 /* The die_type call above may have already set the type for this DIE. */
14135 set_type = get_die_type (die, cu);
14136 if (set_type)
14137 return set_type;
14138
14139 set_type = create_set_type (NULL, domain_type);
14140
14141 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
14142 if (attr)
14143 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 14144
f792889a 14145 return set_die_type (die, set_type, cu);
72019c9c 14146}
7ca2d3a3 14147
0971de02
TT
14148/* A helper for read_common_block that creates a locexpr baton.
14149 SYM is the symbol which we are marking as computed.
14150 COMMON_DIE is the DIE for the common block.
14151 COMMON_LOC is the location expression attribute for the common
14152 block itself.
14153 MEMBER_LOC is the location expression attribute for the particular
14154 member of the common block that we are processing.
14155 CU is the CU from which the above come. */
14156
14157static void
14158mark_common_block_symbol_computed (struct symbol *sym,
14159 struct die_info *common_die,
14160 struct attribute *common_loc,
14161 struct attribute *member_loc,
14162 struct dwarf2_cu *cu)
14163{
14164 struct objfile *objfile = dwarf2_per_objfile->objfile;
14165 struct dwarf2_locexpr_baton *baton;
14166 gdb_byte *ptr;
14167 unsigned int cu_off;
14168 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14169 LONGEST offset = 0;
14170
14171 gdb_assert (common_loc && member_loc);
14172 gdb_assert (attr_form_is_block (common_loc));
14173 gdb_assert (attr_form_is_block (member_loc)
14174 || attr_form_is_constant (member_loc));
14175
8d749320 14176 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
14177 baton->per_cu = cu->per_cu;
14178 gdb_assert (baton->per_cu);
14179
14180 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14181
14182 if (attr_form_is_constant (member_loc))
14183 {
14184 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14185 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14186 }
14187 else
14188 baton->size += DW_BLOCK (member_loc)->size;
14189
224c3ddb 14190 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
14191 baton->data = ptr;
14192
14193 *ptr++ = DW_OP_call4;
14194 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14195 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14196 ptr += 4;
14197
14198 if (attr_form_is_constant (member_loc))
14199 {
14200 *ptr++ = DW_OP_addr;
14201 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14202 ptr += cu->header.addr_size;
14203 }
14204 else
14205 {
14206 /* We have to copy the data here, because DW_OP_call4 will only
14207 use a DW_AT_location attribute. */
14208 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14209 ptr += DW_BLOCK (member_loc)->size;
14210 }
14211
14212 *ptr++ = DW_OP_plus;
14213 gdb_assert (ptr - baton->data == baton->size);
14214
0971de02 14215 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 14216 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
14217}
14218
4357ac6c
TT
14219/* Create appropriate locally-scoped variables for all the
14220 DW_TAG_common_block entries. Also create a struct common_block
14221 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14222 is used to sepate the common blocks name namespace from regular
14223 variable names. */
c906108c
SS
14224
14225static void
e7c27a73 14226read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14227{
0971de02
TT
14228 struct attribute *attr;
14229
14230 attr = dwarf2_attr (die, DW_AT_location, cu);
14231 if (attr)
14232 {
14233 /* Support the .debug_loc offsets. */
14234 if (attr_form_is_block (attr))
14235 {
14236 /* Ok. */
14237 }
14238 else if (attr_form_is_section_offset (attr))
14239 {
14240 dwarf2_complex_location_expr_complaint ();
14241 attr = NULL;
14242 }
14243 else
14244 {
14245 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14246 "common block member");
14247 attr = NULL;
14248 }
14249 }
14250
639d11d3 14251 if (die->child != NULL)
c906108c 14252 {
4357ac6c
TT
14253 struct objfile *objfile = cu->objfile;
14254 struct die_info *child_die;
14255 size_t n_entries = 0, size;
14256 struct common_block *common_block;
14257 struct symbol *sym;
74ac6d43 14258
4357ac6c
TT
14259 for (child_die = die->child;
14260 child_die && child_die->tag;
14261 child_die = sibling_die (child_die))
14262 ++n_entries;
14263
14264 size = (sizeof (struct common_block)
14265 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
14266 common_block
14267 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14268 size);
4357ac6c
TT
14269 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14270 common_block->n_entries = 0;
14271
14272 for (child_die = die->child;
14273 child_die && child_die->tag;
14274 child_die = sibling_die (child_die))
14275 {
14276 /* Create the symbol in the DW_TAG_common_block block in the current
14277 symbol scope. */
e7c27a73 14278 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
14279 if (sym != NULL)
14280 {
14281 struct attribute *member_loc;
14282
14283 common_block->contents[common_block->n_entries++] = sym;
14284
14285 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14286 cu);
14287 if (member_loc)
14288 {
14289 /* GDB has handled this for a long time, but it is
14290 not specified by DWARF. It seems to have been
14291 emitted by gfortran at least as recently as:
14292 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14293 complaint (&symfile_complaints,
14294 _("Variable in common block has "
14295 "DW_AT_data_member_location "
14296 "- DIE at 0x%x [in module %s]"),
4262abfb
JK
14297 child_die->offset.sect_off,
14298 objfile_name (cu->objfile));
0971de02
TT
14299
14300 if (attr_form_is_section_offset (member_loc))
14301 dwarf2_complex_location_expr_complaint ();
14302 else if (attr_form_is_constant (member_loc)
14303 || attr_form_is_block (member_loc))
14304 {
14305 if (attr)
14306 mark_common_block_symbol_computed (sym, die, attr,
14307 member_loc, cu);
14308 }
14309 else
14310 dwarf2_complex_location_expr_complaint ();
14311 }
14312 }
c906108c 14313 }
4357ac6c
TT
14314
14315 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14316 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
14317 }
14318}
14319
0114d602 14320/* Create a type for a C++ namespace. */
d9fa45fe 14321
0114d602
DJ
14322static struct type *
14323read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 14324{
e7c27a73 14325 struct objfile *objfile = cu->objfile;
0114d602 14326 const char *previous_prefix, *name;
9219021c 14327 int is_anonymous;
0114d602
DJ
14328 struct type *type;
14329
14330 /* For extensions, reuse the type of the original namespace. */
14331 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14332 {
14333 struct die_info *ext_die;
14334 struct dwarf2_cu *ext_cu = cu;
9a619af0 14335
0114d602
DJ
14336 ext_die = dwarf2_extension (die, &ext_cu);
14337 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
14338
14339 /* EXT_CU may not be the same as CU.
02142a6c 14340 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
14341 return set_die_type (die, type, cu);
14342 }
9219021c 14343
e142c38c 14344 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
14345
14346 /* Now build the name of the current namespace. */
14347
0114d602
DJ
14348 previous_prefix = determine_prefix (die, cu);
14349 if (previous_prefix[0] != '\0')
14350 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 14351 previous_prefix, name, 0, cu);
0114d602
DJ
14352
14353 /* Create the type. */
19f392bc 14354 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602
DJ
14355 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14356
60531b24 14357 return set_die_type (die, type, cu);
0114d602
DJ
14358}
14359
22cee43f 14360/* Read a namespace scope. */
0114d602
DJ
14361
14362static void
14363read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14364{
14365 struct objfile *objfile = cu->objfile;
0114d602 14366 int is_anonymous;
9219021c 14367
5c4e30ca
DC
14368 /* Add a symbol associated to this if we haven't seen the namespace
14369 before. Also, add a using directive if it's an anonymous
14370 namespace. */
9219021c 14371
f2f0e013 14372 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
14373 {
14374 struct type *type;
14375
0114d602 14376 type = read_type_die (die, cu);
e7c27a73 14377 new_symbol (die, type, cu);
5c4e30ca 14378
e8e80198 14379 namespace_name (die, &is_anonymous, cu);
5c4e30ca 14380 if (is_anonymous)
0114d602
DJ
14381 {
14382 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 14383
22cee43f
PMR
14384 add_using_directive (using_directives (cu->language),
14385 previous_prefix, TYPE_NAME (type), NULL,
14386 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 14387 }
5c4e30ca 14388 }
9219021c 14389
639d11d3 14390 if (die->child != NULL)
d9fa45fe 14391 {
639d11d3 14392 struct die_info *child_die = die->child;
6e70227d 14393
d9fa45fe
DC
14394 while (child_die && child_die->tag)
14395 {
e7c27a73 14396 process_die (child_die, cu);
d9fa45fe
DC
14397 child_die = sibling_die (child_die);
14398 }
14399 }
38d518c9
EZ
14400}
14401
f55ee35c
JK
14402/* Read a Fortran module as type. This DIE can be only a declaration used for
14403 imported module. Still we need that type as local Fortran "use ... only"
14404 declaration imports depend on the created type in determine_prefix. */
14405
14406static struct type *
14407read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14408{
14409 struct objfile *objfile = cu->objfile;
15d034d0 14410 const char *module_name;
f55ee35c
JK
14411 struct type *type;
14412
14413 module_name = dwarf2_name (die, cu);
14414 if (!module_name)
3e43a32a
MS
14415 complaint (&symfile_complaints,
14416 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 14417 die->offset.sect_off);
19f392bc 14418 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c
JK
14419
14420 /* determine_prefix uses TYPE_TAG_NAME. */
14421 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14422
14423 return set_die_type (die, type, cu);
14424}
14425
5d7cb8df
JK
14426/* Read a Fortran module. */
14427
14428static void
14429read_module (struct die_info *die, struct dwarf2_cu *cu)
14430{
14431 struct die_info *child_die = die->child;
530e8392
KB
14432 struct type *type;
14433
14434 type = read_type_die (die, cu);
14435 new_symbol (die, type, cu);
5d7cb8df 14436
5d7cb8df
JK
14437 while (child_die && child_die->tag)
14438 {
14439 process_die (child_die, cu);
14440 child_die = sibling_die (child_die);
14441 }
14442}
14443
38d518c9
EZ
14444/* Return the name of the namespace represented by DIE. Set
14445 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14446 namespace. */
14447
14448static const char *
e142c38c 14449namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
14450{
14451 struct die_info *current_die;
14452 const char *name = NULL;
14453
14454 /* Loop through the extensions until we find a name. */
14455
14456 for (current_die = die;
14457 current_die != NULL;
f2f0e013 14458 current_die = dwarf2_extension (die, &cu))
38d518c9 14459 {
96553a0c
DE
14460 /* We don't use dwarf2_name here so that we can detect the absence
14461 of a name -> anonymous namespace. */
7d45c7c3 14462 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 14463
38d518c9
EZ
14464 if (name != NULL)
14465 break;
14466 }
14467
14468 /* Is it an anonymous namespace? */
14469
14470 *is_anonymous = (name == NULL);
14471 if (*is_anonymous)
2b1dbab0 14472 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
14473
14474 return name;
d9fa45fe
DC
14475}
14476
c906108c
SS
14477/* Extract all information from a DW_TAG_pointer_type DIE and add to
14478 the user defined type vector. */
14479
f792889a 14480static struct type *
e7c27a73 14481read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14482{
5e2b427d 14483 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 14484 struct comp_unit_head *cu_header = &cu->header;
c906108c 14485 struct type *type;
8b2dbe47
KB
14486 struct attribute *attr_byte_size;
14487 struct attribute *attr_address_class;
14488 int byte_size, addr_class;
7e314c57
JK
14489 struct type *target_type;
14490
14491 target_type = die_type (die, cu);
c906108c 14492
7e314c57
JK
14493 /* The die_type call above may have already set the type for this DIE. */
14494 type = get_die_type (die, cu);
14495 if (type)
14496 return type;
14497
14498 type = lookup_pointer_type (target_type);
8b2dbe47 14499
e142c38c 14500 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
14501 if (attr_byte_size)
14502 byte_size = DW_UNSND (attr_byte_size);
c906108c 14503 else
8b2dbe47
KB
14504 byte_size = cu_header->addr_size;
14505
e142c38c 14506 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
14507 if (attr_address_class)
14508 addr_class = DW_UNSND (attr_address_class);
14509 else
14510 addr_class = DW_ADDR_none;
14511
14512 /* If the pointer size or address class is different than the
14513 default, create a type variant marked as such and set the
14514 length accordingly. */
14515 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 14516 {
5e2b427d 14517 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
14518 {
14519 int type_flags;
14520
849957d9 14521 type_flags = gdbarch_address_class_type_flags
5e2b427d 14522 (gdbarch, byte_size, addr_class);
876cecd0
TT
14523 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14524 == 0);
8b2dbe47
KB
14525 type = make_type_with_address_space (type, type_flags);
14526 }
14527 else if (TYPE_LENGTH (type) != byte_size)
14528 {
3e43a32a
MS
14529 complaint (&symfile_complaints,
14530 _("invalid pointer size %d"), byte_size);
8b2dbe47 14531 }
6e70227d 14532 else
9a619af0
MS
14533 {
14534 /* Should we also complain about unhandled address classes? */
14535 }
c906108c 14536 }
8b2dbe47
KB
14537
14538 TYPE_LENGTH (type) = byte_size;
f792889a 14539 return set_die_type (die, type, cu);
c906108c
SS
14540}
14541
14542/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14543 the user defined type vector. */
14544
f792889a 14545static struct type *
e7c27a73 14546read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
14547{
14548 struct type *type;
14549 struct type *to_type;
14550 struct type *domain;
14551
e7c27a73
DJ
14552 to_type = die_type (die, cu);
14553 domain = die_containing_type (die, cu);
0d5de010 14554
7e314c57
JK
14555 /* The calls above may have already set the type for this DIE. */
14556 type = get_die_type (die, cu);
14557 if (type)
14558 return type;
14559
0d5de010
DJ
14560 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14561 type = lookup_methodptr_type (to_type);
7078baeb
TT
14562 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14563 {
14564 struct type *new_type = alloc_type (cu->objfile);
14565
14566 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14567 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14568 TYPE_VARARGS (to_type));
14569 type = lookup_methodptr_type (new_type);
14570 }
0d5de010
DJ
14571 else
14572 type = lookup_memberptr_type (to_type, domain);
c906108c 14573
f792889a 14574 return set_die_type (die, type, cu);
c906108c
SS
14575}
14576
4297a3f0 14577/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
14578 the user defined type vector. */
14579
f792889a 14580static struct type *
4297a3f0
AV
14581read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14582 enum type_code refcode)
c906108c 14583{
e7c27a73 14584 struct comp_unit_head *cu_header = &cu->header;
7e314c57 14585 struct type *type, *target_type;
c906108c
SS
14586 struct attribute *attr;
14587
4297a3f0
AV
14588 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14589
7e314c57
JK
14590 target_type = die_type (die, cu);
14591
14592 /* The die_type call above may have already set the type for this DIE. */
14593 type = get_die_type (die, cu);
14594 if (type)
14595 return type;
14596
4297a3f0 14597 type = lookup_reference_type (target_type, refcode);
e142c38c 14598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14599 if (attr)
14600 {
14601 TYPE_LENGTH (type) = DW_UNSND (attr);
14602 }
14603 else
14604 {
107d2387 14605 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 14606 }
f792889a 14607 return set_die_type (die, type, cu);
c906108c
SS
14608}
14609
cf363f18
MW
14610/* Add the given cv-qualifiers to the element type of the array. GCC
14611 outputs DWARF type qualifiers that apply to an array, not the
14612 element type. But GDB relies on the array element type to carry
14613 the cv-qualifiers. This mimics section 6.7.3 of the C99
14614 specification. */
14615
14616static struct type *
14617add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14618 struct type *base_type, int cnst, int voltl)
14619{
14620 struct type *el_type, *inner_array;
14621
14622 base_type = copy_type (base_type);
14623 inner_array = base_type;
14624
14625 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14626 {
14627 TYPE_TARGET_TYPE (inner_array) =
14628 copy_type (TYPE_TARGET_TYPE (inner_array));
14629 inner_array = TYPE_TARGET_TYPE (inner_array);
14630 }
14631
14632 el_type = TYPE_TARGET_TYPE (inner_array);
14633 cnst |= TYPE_CONST (el_type);
14634 voltl |= TYPE_VOLATILE (el_type);
14635 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14636
14637 return set_die_type (die, base_type, cu);
14638}
14639
f792889a 14640static struct type *
e7c27a73 14641read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14642{
f792889a 14643 struct type *base_type, *cv_type;
c906108c 14644
e7c27a73 14645 base_type = die_type (die, cu);
7e314c57
JK
14646
14647 /* The die_type call above may have already set the type for this DIE. */
14648 cv_type = get_die_type (die, cu);
14649 if (cv_type)
14650 return cv_type;
14651
2f608a3a
KW
14652 /* In case the const qualifier is applied to an array type, the element type
14653 is so qualified, not the array type (section 6.7.3 of C99). */
14654 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 14655 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 14656
f792889a
DJ
14657 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14658 return set_die_type (die, cv_type, cu);
c906108c
SS
14659}
14660
f792889a 14661static struct type *
e7c27a73 14662read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14663{
f792889a 14664 struct type *base_type, *cv_type;
c906108c 14665
e7c27a73 14666 base_type = die_type (die, cu);
7e314c57
JK
14667
14668 /* The die_type call above may have already set the type for this DIE. */
14669 cv_type = get_die_type (die, cu);
14670 if (cv_type)
14671 return cv_type;
14672
cf363f18
MW
14673 /* In case the volatile qualifier is applied to an array type, the
14674 element type is so qualified, not the array type (section 6.7.3
14675 of C99). */
14676 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14677 return add_array_cv_type (die, cu, base_type, 0, 1);
14678
f792889a
DJ
14679 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14680 return set_die_type (die, cv_type, cu);
c906108c
SS
14681}
14682
06d66ee9
TT
14683/* Handle DW_TAG_restrict_type. */
14684
14685static struct type *
14686read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14687{
14688 struct type *base_type, *cv_type;
14689
14690 base_type = die_type (die, cu);
14691
14692 /* The die_type call above may have already set the type for this DIE. */
14693 cv_type = get_die_type (die, cu);
14694 if (cv_type)
14695 return cv_type;
14696
14697 cv_type = make_restrict_type (base_type);
14698 return set_die_type (die, cv_type, cu);
14699}
14700
a2c2acaf
MW
14701/* Handle DW_TAG_atomic_type. */
14702
14703static struct type *
14704read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14705{
14706 struct type *base_type, *cv_type;
14707
14708 base_type = die_type (die, cu);
14709
14710 /* The die_type call above may have already set the type for this DIE. */
14711 cv_type = get_die_type (die, cu);
14712 if (cv_type)
14713 return cv_type;
14714
14715 cv_type = make_atomic_type (base_type);
14716 return set_die_type (die, cv_type, cu);
14717}
14718
c906108c
SS
14719/* Extract all information from a DW_TAG_string_type DIE and add to
14720 the user defined type vector. It isn't really a user defined type,
14721 but it behaves like one, with other DIE's using an AT_user_def_type
14722 attribute to reference it. */
14723
f792889a 14724static struct type *
e7c27a73 14725read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14726{
e7c27a73 14727 struct objfile *objfile = cu->objfile;
3b7538c0 14728 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14729 struct type *type, *range_type, *index_type, *char_type;
14730 struct attribute *attr;
14731 unsigned int length;
14732
e142c38c 14733 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
14734 if (attr)
14735 {
14736 length = DW_UNSND (attr);
14737 }
14738 else
14739 {
0963b4bd 14740 /* Check for the DW_AT_byte_size attribute. */
e142c38c 14741 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
14742 if (attr)
14743 {
14744 length = DW_UNSND (attr);
14745 }
14746 else
14747 {
14748 length = 1;
14749 }
c906108c 14750 }
6ccb9162 14751
46bf5051 14752 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 14753 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
14754 char_type = language_string_char_type (cu->language_defn, gdbarch);
14755 type = create_string_type (NULL, char_type, range_type);
6ccb9162 14756
f792889a 14757 return set_die_type (die, type, cu);
c906108c
SS
14758}
14759
4d804846
JB
14760/* Assuming that DIE corresponds to a function, returns nonzero
14761 if the function is prototyped. */
14762
14763static int
14764prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14765{
14766 struct attribute *attr;
14767
14768 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14769 if (attr && (DW_UNSND (attr) != 0))
14770 return 1;
14771
14772 /* The DWARF standard implies that the DW_AT_prototyped attribute
14773 is only meaninful for C, but the concept also extends to other
14774 languages that allow unprototyped functions (Eg: Objective C).
14775 For all other languages, assume that functions are always
14776 prototyped. */
14777 if (cu->language != language_c
14778 && cu->language != language_objc
14779 && cu->language != language_opencl)
14780 return 1;
14781
14782 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14783 prototyped and unprototyped functions; default to prototyped,
14784 since that is more common in modern code (and RealView warns
14785 about unprototyped functions). */
14786 if (producer_is_realview (cu->producer))
14787 return 1;
14788
14789 return 0;
14790}
14791
c906108c
SS
14792/* Handle DIES due to C code like:
14793
14794 struct foo
c5aa993b
JM
14795 {
14796 int (*funcp)(int a, long l);
14797 int b;
14798 };
c906108c 14799
0963b4bd 14800 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 14801
f792889a 14802static struct type *
e7c27a73 14803read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14804{
bb5ed363 14805 struct objfile *objfile = cu->objfile;
0963b4bd
MS
14806 struct type *type; /* Type that this function returns. */
14807 struct type *ftype; /* Function that returns above type. */
c906108c
SS
14808 struct attribute *attr;
14809
e7c27a73 14810 type = die_type (die, cu);
7e314c57
JK
14811
14812 /* The die_type call above may have already set the type for this DIE. */
14813 ftype = get_die_type (die, cu);
14814 if (ftype)
14815 return ftype;
14816
0c8b41f1 14817 ftype = lookup_function_type (type);
c906108c 14818
4d804846 14819 if (prototyped_function_p (die, cu))
a6c727b2 14820 TYPE_PROTOTYPED (ftype) = 1;
c906108c 14821
c055b101
CV
14822 /* Store the calling convention in the type if it's available in
14823 the subroutine die. Otherwise set the calling convention to
14824 the default value DW_CC_normal. */
14825 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
14826 if (attr)
14827 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14828 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14829 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14830 else
14831 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 14832
743649fd
MW
14833 /* Record whether the function returns normally to its caller or not
14834 if the DWARF producer set that information. */
14835 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14836 if (attr && (DW_UNSND (attr) != 0))
14837 TYPE_NO_RETURN (ftype) = 1;
14838
76c10ea2
GM
14839 /* We need to add the subroutine type to the die immediately so
14840 we don't infinitely recurse when dealing with parameters
0963b4bd 14841 declared as the same subroutine type. */
76c10ea2 14842 set_die_type (die, ftype, cu);
6e70227d 14843
639d11d3 14844 if (die->child != NULL)
c906108c 14845 {
bb5ed363 14846 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 14847 struct die_info *child_die;
8072405b 14848 int nparams, iparams;
c906108c
SS
14849
14850 /* Count the number of parameters.
14851 FIXME: GDB currently ignores vararg functions, but knows about
14852 vararg member functions. */
8072405b 14853 nparams = 0;
639d11d3 14854 child_die = die->child;
c906108c
SS
14855 while (child_die && child_die->tag)
14856 {
14857 if (child_die->tag == DW_TAG_formal_parameter)
14858 nparams++;
14859 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 14860 TYPE_VARARGS (ftype) = 1;
c906108c
SS
14861 child_die = sibling_die (child_die);
14862 }
14863
14864 /* Allocate storage for parameters and fill them in. */
14865 TYPE_NFIELDS (ftype) = nparams;
14866 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 14867 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 14868
8072405b
JK
14869 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14870 even if we error out during the parameters reading below. */
14871 for (iparams = 0; iparams < nparams; iparams++)
14872 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14873
14874 iparams = 0;
639d11d3 14875 child_die = die->child;
c906108c
SS
14876 while (child_die && child_die->tag)
14877 {
14878 if (child_die->tag == DW_TAG_formal_parameter)
14879 {
3ce3b1ba
PA
14880 struct type *arg_type;
14881
14882 /* DWARF version 2 has no clean way to discern C++
14883 static and non-static member functions. G++ helps
14884 GDB by marking the first parameter for non-static
14885 member functions (which is the this pointer) as
14886 artificial. We pass this information to
14887 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14888
14889 DWARF version 3 added DW_AT_object_pointer, which GCC
14890 4.5 does not yet generate. */
e142c38c 14891 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
14892 if (attr)
14893 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14894 else
9c37b5ae 14895 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
14896 arg_type = die_type (child_die, cu);
14897
14898 /* RealView does not mark THIS as const, which the testsuite
14899 expects. GCC marks THIS as const in method definitions,
14900 but not in the class specifications (GCC PR 43053). */
14901 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14902 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14903 {
14904 int is_this = 0;
14905 struct dwarf2_cu *arg_cu = cu;
14906 const char *name = dwarf2_name (child_die, cu);
14907
14908 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14909 if (attr)
14910 {
14911 /* If the compiler emits this, use it. */
14912 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14913 is_this = 1;
14914 }
14915 else if (name && strcmp (name, "this") == 0)
14916 /* Function definitions will have the argument names. */
14917 is_this = 1;
14918 else if (name == NULL && iparams == 0)
14919 /* Declarations may not have the names, so like
14920 elsewhere in GDB, assume an artificial first
14921 argument is "this". */
14922 is_this = 1;
14923
14924 if (is_this)
14925 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14926 arg_type, 0);
14927 }
14928
14929 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
14930 iparams++;
14931 }
14932 child_die = sibling_die (child_die);
14933 }
14934 }
14935
76c10ea2 14936 return ftype;
c906108c
SS
14937}
14938
f792889a 14939static struct type *
e7c27a73 14940read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14941{
e7c27a73 14942 struct objfile *objfile = cu->objfile;
0114d602 14943 const char *name = NULL;
3c8e0968 14944 struct type *this_type, *target_type;
c906108c 14945
94af9270 14946 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
14947 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14948 TYPE_TARGET_STUB (this_type) = 1;
f792889a 14949 set_die_type (die, this_type, cu);
3c8e0968
DE
14950 target_type = die_type (die, cu);
14951 if (target_type != this_type)
14952 TYPE_TARGET_TYPE (this_type) = target_type;
14953 else
14954 {
14955 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14956 spec and cause infinite loops in GDB. */
14957 complaint (&symfile_complaints,
14958 _("Self-referential DW_TAG_typedef "
14959 "- DIE at 0x%x [in module %s]"),
4262abfb 14960 die->offset.sect_off, objfile_name (objfile));
3c8e0968
DE
14961 TYPE_TARGET_TYPE (this_type) = NULL;
14962 }
f792889a 14963 return this_type;
c906108c
SS
14964}
14965
9b790ce7
UW
14966/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14967 (which may be different from NAME) to the architecture back-end to allow
14968 it to guess the correct format if necessary. */
14969
14970static struct type *
14971dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
14972 const char *name_hint)
14973{
14974 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14975 const struct floatformat **format;
14976 struct type *type;
14977
14978 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14979 if (format)
14980 type = init_float_type (objfile, bits, name, format);
14981 else
14982 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
14983
14984 return type;
14985}
14986
c906108c
SS
14987/* Find a representation of a given base type and install
14988 it in the TYPE field of the die. */
14989
f792889a 14990static struct type *
e7c27a73 14991read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14992{
e7c27a73 14993 struct objfile *objfile = cu->objfile;
c906108c
SS
14994 struct type *type;
14995 struct attribute *attr;
19f392bc 14996 int encoding = 0, bits = 0;
15d034d0 14997 const char *name;
c906108c 14998
e142c38c 14999 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
15000 if (attr)
15001 {
15002 encoding = DW_UNSND (attr);
15003 }
e142c38c 15004 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15005 if (attr)
15006 {
19f392bc 15007 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 15008 }
39cbfefa 15009 name = dwarf2_name (die, cu);
6ccb9162 15010 if (!name)
c906108c 15011 {
6ccb9162
UW
15012 complaint (&symfile_complaints,
15013 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 15014 }
6ccb9162
UW
15015
15016 switch (encoding)
c906108c 15017 {
6ccb9162
UW
15018 case DW_ATE_address:
15019 /* Turn DW_ATE_address into a void * pointer. */
19f392bc
UW
15020 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15021 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
15022 break;
15023 case DW_ATE_boolean:
19f392bc 15024 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
15025 break;
15026 case DW_ATE_complex_float:
9b790ce7 15027 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 15028 type = init_complex_type (objfile, name, type);
6ccb9162
UW
15029 break;
15030 case DW_ATE_decimal_float:
19f392bc 15031 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
15032 break;
15033 case DW_ATE_float:
9b790ce7 15034 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
15035 break;
15036 case DW_ATE_signed:
19f392bc 15037 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15038 break;
15039 case DW_ATE_unsigned:
3b2b8fea
TT
15040 if (cu->language == language_fortran
15041 && name
61012eef 15042 && startswith (name, "character("))
19f392bc
UW
15043 type = init_character_type (objfile, bits, 1, name);
15044 else
15045 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
15046 break;
15047 case DW_ATE_signed_char:
6e70227d 15048 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
15049 || cu->language == language_pascal
15050 || cu->language == language_fortran)
19f392bc
UW
15051 type = init_character_type (objfile, bits, 0, name);
15052 else
15053 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
15054 break;
15055 case DW_ATE_unsigned_char:
868a0084 15056 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 15057 || cu->language == language_pascal
c44af4eb
TT
15058 || cu->language == language_fortran
15059 || cu->language == language_rust)
19f392bc
UW
15060 type = init_character_type (objfile, bits, 1, name);
15061 else
15062 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 15063 break;
75079b2b
TT
15064 case DW_ATE_UTF:
15065 /* We just treat this as an integer and then recognize the
15066 type by name elsewhere. */
19f392bc 15067 type = init_integer_type (objfile, bits, 0, name);
75079b2b
TT
15068 break;
15069
6ccb9162
UW
15070 default:
15071 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15072 dwarf_type_encoding_name (encoding));
19f392bc
UW
15073 type = init_type (objfile, TYPE_CODE_ERROR,
15074 bits / TARGET_CHAR_BIT, name);
6ccb9162 15075 break;
c906108c 15076 }
6ccb9162 15077
0114d602 15078 if (name && strcmp (name, "char") == 0)
876cecd0 15079 TYPE_NOSIGN (type) = 1;
0114d602 15080
f792889a 15081 return set_die_type (die, type, cu);
c906108c
SS
15082}
15083
80180f79
SA
15084/* Parse dwarf attribute if it's a block, reference or constant and put the
15085 resulting value of the attribute into struct bound_prop.
15086 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15087
15088static int
15089attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15090 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15091{
15092 struct dwarf2_property_baton *baton;
15093 struct obstack *obstack = &cu->objfile->objfile_obstack;
15094
15095 if (attr == NULL || prop == NULL)
15096 return 0;
15097
15098 if (attr_form_is_block (attr))
15099 {
8d749320 15100 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
15101 baton->referenced_type = NULL;
15102 baton->locexpr.per_cu = cu->per_cu;
15103 baton->locexpr.size = DW_BLOCK (attr)->size;
15104 baton->locexpr.data = DW_BLOCK (attr)->data;
15105 prop->data.baton = baton;
15106 prop->kind = PROP_LOCEXPR;
15107 gdb_assert (prop->data.baton != NULL);
15108 }
15109 else if (attr_form_is_ref (attr))
15110 {
15111 struct dwarf2_cu *target_cu = cu;
15112 struct die_info *target_die;
15113 struct attribute *target_attr;
15114
15115 target_die = follow_die_ref (die, attr, &target_cu);
15116 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
15117 if (target_attr == NULL)
15118 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15119 target_cu);
80180f79
SA
15120 if (target_attr == NULL)
15121 return 0;
15122
df25ebbd 15123 switch (target_attr->name)
80180f79 15124 {
df25ebbd
JB
15125 case DW_AT_location:
15126 if (attr_form_is_section_offset (target_attr))
15127 {
8d749320 15128 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15129 baton->referenced_type = die_type (target_die, target_cu);
15130 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15131 prop->data.baton = baton;
15132 prop->kind = PROP_LOCLIST;
15133 gdb_assert (prop->data.baton != NULL);
15134 }
15135 else if (attr_form_is_block (target_attr))
15136 {
8d749320 15137 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
15138 baton->referenced_type = die_type (target_die, target_cu);
15139 baton->locexpr.per_cu = cu->per_cu;
15140 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15141 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15142 prop->data.baton = baton;
15143 prop->kind = PROP_LOCEXPR;
15144 gdb_assert (prop->data.baton != NULL);
15145 }
15146 else
15147 {
15148 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15149 "dynamic property");
15150 return 0;
15151 }
15152 break;
15153 case DW_AT_data_member_location:
15154 {
15155 LONGEST offset;
15156
15157 if (!handle_data_member_location (target_die, target_cu,
15158 &offset))
15159 return 0;
15160
8d749320 15161 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
15162 baton->referenced_type = read_type_die (target_die->parent,
15163 target_cu);
df25ebbd
JB
15164 baton->offset_info.offset = offset;
15165 baton->offset_info.type = die_type (target_die, target_cu);
15166 prop->data.baton = baton;
15167 prop->kind = PROP_ADDR_OFFSET;
15168 break;
15169 }
80180f79
SA
15170 }
15171 }
15172 else if (attr_form_is_constant (attr))
15173 {
15174 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15175 prop->kind = PROP_CONST;
15176 }
15177 else
15178 {
15179 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15180 dwarf2_name (die, cu));
15181 return 0;
15182 }
15183
15184 return 1;
15185}
15186
a02abb62
JB
15187/* Read the given DW_AT_subrange DIE. */
15188
f792889a 15189static struct type *
a02abb62
JB
15190read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15191{
4c9ad8c2 15192 struct type *base_type, *orig_base_type;
a02abb62
JB
15193 struct type *range_type;
15194 struct attribute *attr;
729efb13 15195 struct dynamic_prop low, high;
4fae6e18 15196 int low_default_is_valid;
c451ebe5 15197 int high_bound_is_count = 0;
15d034d0 15198 const char *name;
43bbcdc2 15199 LONGEST negative_mask;
e77813c8 15200
4c9ad8c2
TT
15201 orig_base_type = die_type (die, cu);
15202 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15203 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15204 creating the range type, but we use the result of check_typedef
15205 when examining properties of the type. */
15206 base_type = check_typedef (orig_base_type);
a02abb62 15207
7e314c57
JK
15208 /* The die_type call above may have already set the type for this DIE. */
15209 range_type = get_die_type (die, cu);
15210 if (range_type)
15211 return range_type;
15212
729efb13
SA
15213 low.kind = PROP_CONST;
15214 high.kind = PROP_CONST;
15215 high.data.const_val = 0;
15216
4fae6e18
JK
15217 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15218 omitting DW_AT_lower_bound. */
15219 switch (cu->language)
6e70227d 15220 {
4fae6e18
JK
15221 case language_c:
15222 case language_cplus:
729efb13 15223 low.data.const_val = 0;
4fae6e18
JK
15224 low_default_is_valid = 1;
15225 break;
15226 case language_fortran:
729efb13 15227 low.data.const_val = 1;
4fae6e18
JK
15228 low_default_is_valid = 1;
15229 break;
15230 case language_d:
4fae6e18 15231 case language_objc:
c44af4eb 15232 case language_rust:
729efb13 15233 low.data.const_val = 0;
4fae6e18
JK
15234 low_default_is_valid = (cu->header.version >= 4);
15235 break;
15236 case language_ada:
15237 case language_m2:
15238 case language_pascal:
729efb13 15239 low.data.const_val = 1;
4fae6e18
JK
15240 low_default_is_valid = (cu->header.version >= 4);
15241 break;
15242 default:
729efb13 15243 low.data.const_val = 0;
4fae6e18
JK
15244 low_default_is_valid = 0;
15245 break;
a02abb62
JB
15246 }
15247
e142c38c 15248 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 15249 if (attr)
11c1ba78 15250 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18
JK
15251 else if (!low_default_is_valid)
15252 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15253 "- DIE at 0x%x [in module %s]"),
4262abfb 15254 die->offset.sect_off, objfile_name (cu->objfile));
a02abb62 15255
e142c38c 15256 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 15257 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
15258 {
15259 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 15260 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 15261 {
c451ebe5
SA
15262 /* If bounds are constant do the final calculation here. */
15263 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15264 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15265 else
15266 high_bound_is_count = 1;
c2ff108b 15267 }
e77813c8
PM
15268 }
15269
15270 /* Dwarf-2 specifications explicitly allows to create subrange types
15271 without specifying a base type.
15272 In that case, the base type must be set to the type of
15273 the lower bound, upper bound or count, in that order, if any of these
15274 three attributes references an object that has a type.
15275 If no base type is found, the Dwarf-2 specifications say that
15276 a signed integer type of size equal to the size of an address should
15277 be used.
15278 For the following C code: `extern char gdb_int [];'
15279 GCC produces an empty range DIE.
15280 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 15281 high bound or count are not yet handled by this code. */
e77813c8
PM
15282 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15283 {
15284 struct objfile *objfile = cu->objfile;
15285 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15286 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15287 struct type *int_type = objfile_type (objfile)->builtin_int;
15288
15289 /* Test "int", "long int", and "long long int" objfile types,
15290 and select the first one having a size above or equal to the
15291 architecture address size. */
15292 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15293 base_type = int_type;
15294 else
15295 {
15296 int_type = objfile_type (objfile)->builtin_long;
15297 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15298 base_type = int_type;
15299 else
15300 {
15301 int_type = objfile_type (objfile)->builtin_long_long;
15302 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15303 base_type = int_type;
15304 }
15305 }
15306 }
a02abb62 15307
dbb9c2b1
JB
15308 /* Normally, the DWARF producers are expected to use a signed
15309 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15310 But this is unfortunately not always the case, as witnessed
15311 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15312 is used instead. To work around that ambiguity, we treat
15313 the bounds as signed, and thus sign-extend their values, when
15314 the base type is signed. */
6e70227d 15315 negative_mask =
66c6502d 15316 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
15317 if (low.kind == PROP_CONST
15318 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15319 low.data.const_val |= negative_mask;
15320 if (high.kind == PROP_CONST
15321 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15322 high.data.const_val |= negative_mask;
43bbcdc2 15323
729efb13 15324 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 15325
c451ebe5
SA
15326 if (high_bound_is_count)
15327 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15328
c2ff108b
JK
15329 /* Ada expects an empty array on no boundary attributes. */
15330 if (attr == NULL && cu->language != language_ada)
729efb13 15331 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 15332
39cbfefa
DJ
15333 name = dwarf2_name (die, cu);
15334 if (name)
15335 TYPE_NAME (range_type) = name;
6e70227d 15336
e142c38c 15337 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
15338 if (attr)
15339 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15340
7e314c57
JK
15341 set_die_type (die, range_type, cu);
15342
15343 /* set_die_type should be already done. */
b4ba55a1
JB
15344 set_descriptive_type (range_type, die, cu);
15345
7e314c57 15346 return range_type;
a02abb62 15347}
6e70227d 15348
f792889a 15349static struct type *
81a17f79
JB
15350read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15351{
15352 struct type *type;
81a17f79 15353
81a17f79
JB
15354 /* For now, we only support the C meaning of an unspecified type: void. */
15355
19f392bc 15356 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
0114d602 15357 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 15358
f792889a 15359 return set_die_type (die, type, cu);
81a17f79 15360}
a02abb62 15361
639d11d3
DC
15362/* Read a single die and all its descendents. Set the die's sibling
15363 field to NULL; set other fields in the die correctly, and set all
15364 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15365 location of the info_ptr after reading all of those dies. PARENT
15366 is the parent of the die in question. */
15367
15368static struct die_info *
dee91e82 15369read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
15370 const gdb_byte *info_ptr,
15371 const gdb_byte **new_info_ptr,
dee91e82 15372 struct die_info *parent)
639d11d3
DC
15373{
15374 struct die_info *die;
d521ce57 15375 const gdb_byte *cur_ptr;
639d11d3
DC
15376 int has_children;
15377
bf6af496 15378 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
15379 if (die == NULL)
15380 {
15381 *new_info_ptr = cur_ptr;
15382 return NULL;
15383 }
93311388 15384 store_in_ref_table (die, reader->cu);
639d11d3
DC
15385
15386 if (has_children)
bf6af496 15387 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
15388 else
15389 {
15390 die->child = NULL;
15391 *new_info_ptr = cur_ptr;
15392 }
15393
15394 die->sibling = NULL;
15395 die->parent = parent;
15396 return die;
15397}
15398
15399/* Read a die, all of its descendents, and all of its siblings; set
15400 all of the fields of all of the dies correctly. Arguments are as
15401 in read_die_and_children. */
15402
15403static struct die_info *
bf6af496 15404read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
15405 const gdb_byte *info_ptr,
15406 const gdb_byte **new_info_ptr,
bf6af496 15407 struct die_info *parent)
639d11d3
DC
15408{
15409 struct die_info *first_die, *last_sibling;
d521ce57 15410 const gdb_byte *cur_ptr;
639d11d3 15411
c906108c 15412 cur_ptr = info_ptr;
639d11d3
DC
15413 first_die = last_sibling = NULL;
15414
15415 while (1)
c906108c 15416 {
639d11d3 15417 struct die_info *die
dee91e82 15418 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 15419
1d325ec1 15420 if (die == NULL)
c906108c 15421 {
639d11d3
DC
15422 *new_info_ptr = cur_ptr;
15423 return first_die;
c906108c 15424 }
1d325ec1
DJ
15425
15426 if (!first_die)
15427 first_die = die;
c906108c 15428 else
1d325ec1
DJ
15429 last_sibling->sibling = die;
15430
15431 last_sibling = die;
c906108c 15432 }
c906108c
SS
15433}
15434
bf6af496
DE
15435/* Read a die, all of its descendents, and all of its siblings; set
15436 all of the fields of all of the dies correctly. Arguments are as
15437 in read_die_and_children.
15438 This the main entry point for reading a DIE and all its children. */
15439
15440static struct die_info *
15441read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
15442 const gdb_byte *info_ptr,
15443 const gdb_byte **new_info_ptr,
bf6af496
DE
15444 struct die_info *parent)
15445{
15446 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15447 new_info_ptr, parent);
15448
b4f54984 15449 if (dwarf_die_debug)
bf6af496
DE
15450 {
15451 fprintf_unfiltered (gdb_stdlog,
15452 "Read die from %s@0x%x of %s:\n",
a32a8923 15453 get_section_name (reader->die_section),
bf6af496
DE
15454 (unsigned) (info_ptr - reader->die_section->buffer),
15455 bfd_get_filename (reader->abfd));
b4f54984 15456 dump_die (die, dwarf_die_debug);
bf6af496
DE
15457 }
15458
15459 return die;
15460}
15461
3019eac3
DE
15462/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15463 attributes.
15464 The caller is responsible for filling in the extra attributes
15465 and updating (*DIEP)->num_attrs.
15466 Set DIEP to point to a newly allocated die with its information,
15467 except for its child, sibling, and parent fields.
15468 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 15469
d521ce57 15470static const gdb_byte *
3019eac3 15471read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 15472 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 15473 int *has_children, int num_extra_attrs)
93311388 15474{
b64f50a1
JK
15475 unsigned int abbrev_number, bytes_read, i;
15476 sect_offset offset;
93311388
DE
15477 struct abbrev_info *abbrev;
15478 struct die_info *die;
15479 struct dwarf2_cu *cu = reader->cu;
15480 bfd *abfd = reader->abfd;
15481
b64f50a1 15482 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
15483 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15484 info_ptr += bytes_read;
15485 if (!abbrev_number)
15486 {
15487 *diep = NULL;
15488 *has_children = 0;
15489 return info_ptr;
15490 }
15491
433df2d4 15492 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 15493 if (!abbrev)
348e048f
DE
15494 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15495 abbrev_number,
15496 bfd_get_filename (abfd));
15497
3019eac3 15498 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
15499 die->offset = offset;
15500 die->tag = abbrev->tag;
15501 die->abbrev = abbrev_number;
15502
3019eac3
DE
15503 /* Make the result usable.
15504 The caller needs to update num_attrs after adding the extra
15505 attributes. */
93311388
DE
15506 die->num_attrs = abbrev->num_attrs;
15507
15508 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
15509 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15510 info_ptr);
93311388
DE
15511
15512 *diep = die;
15513 *has_children = abbrev->has_children;
15514 return info_ptr;
15515}
15516
3019eac3
DE
15517/* Read a die and all its attributes.
15518 Set DIEP to point to a newly allocated die with its information,
15519 except for its child, sibling, and parent fields.
15520 Set HAS_CHILDREN to tell whether the die has children or not. */
15521
d521ce57 15522static const gdb_byte *
3019eac3 15523read_full_die (const struct die_reader_specs *reader,
d521ce57 15524 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
15525 int *has_children)
15526{
d521ce57 15527 const gdb_byte *result;
bf6af496
DE
15528
15529 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15530
b4f54984 15531 if (dwarf_die_debug)
bf6af496
DE
15532 {
15533 fprintf_unfiltered (gdb_stdlog,
15534 "Read die from %s@0x%x of %s:\n",
a32a8923 15535 get_section_name (reader->die_section),
bf6af496
DE
15536 (unsigned) (info_ptr - reader->die_section->buffer),
15537 bfd_get_filename (reader->abfd));
b4f54984 15538 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
15539 }
15540
15541 return result;
3019eac3 15542}
433df2d4
DE
15543\f
15544/* Abbreviation tables.
3019eac3 15545
433df2d4 15546 In DWARF version 2, the description of the debugging information is
c906108c
SS
15547 stored in a separate .debug_abbrev section. Before we read any
15548 dies from a section we read in all abbreviations and install them
433df2d4
DE
15549 in a hash table. */
15550
15551/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15552
15553static struct abbrev_info *
15554abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15555{
15556 struct abbrev_info *abbrev;
15557
8d749320 15558 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
433df2d4 15559 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 15560
433df2d4
DE
15561 return abbrev;
15562}
15563
15564/* Add an abbreviation to the table. */
c906108c
SS
15565
15566static void
433df2d4
DE
15567abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15568 unsigned int abbrev_number,
15569 struct abbrev_info *abbrev)
15570{
15571 unsigned int hash_number;
15572
15573 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15574 abbrev->next = abbrev_table->abbrevs[hash_number];
15575 abbrev_table->abbrevs[hash_number] = abbrev;
15576}
dee91e82 15577
433df2d4
DE
15578/* Look up an abbrev in the table.
15579 Returns NULL if the abbrev is not found. */
15580
15581static struct abbrev_info *
15582abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15583 unsigned int abbrev_number)
c906108c 15584{
433df2d4
DE
15585 unsigned int hash_number;
15586 struct abbrev_info *abbrev;
15587
15588 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15589 abbrev = abbrev_table->abbrevs[hash_number];
15590
15591 while (abbrev)
15592 {
15593 if (abbrev->number == abbrev_number)
15594 return abbrev;
15595 abbrev = abbrev->next;
15596 }
15597 return NULL;
15598}
15599
15600/* Read in an abbrev table. */
15601
15602static struct abbrev_table *
15603abbrev_table_read_table (struct dwarf2_section_info *section,
15604 sect_offset offset)
15605{
15606 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 15607 bfd *abfd = get_section_bfd_owner (section);
433df2d4 15608 struct abbrev_table *abbrev_table;
d521ce57 15609 const gdb_byte *abbrev_ptr;
c906108c
SS
15610 struct abbrev_info *cur_abbrev;
15611 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 15612 unsigned int abbrev_form;
f3dd6933
DJ
15613 struct attr_abbrev *cur_attrs;
15614 unsigned int allocated_attrs;
c906108c 15615
70ba0933 15616 abbrev_table = XNEW (struct abbrev_table);
f4dc4d17 15617 abbrev_table->offset = offset;
433df2d4 15618 obstack_init (&abbrev_table->abbrev_obstack);
8d749320
SM
15619 abbrev_table->abbrevs =
15620 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15621 ABBREV_HASH_SIZE);
433df2d4
DE
15622 memset (abbrev_table->abbrevs, 0,
15623 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 15624
433df2d4
DE
15625 dwarf2_read_section (objfile, section);
15626 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
15627 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15628 abbrev_ptr += bytes_read;
15629
f3dd6933 15630 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 15631 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 15632
0963b4bd 15633 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
15634 while (abbrev_number)
15635 {
433df2d4 15636 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
15637
15638 /* read in abbrev header */
15639 cur_abbrev->number = abbrev_number;
aead7601
SM
15640 cur_abbrev->tag
15641 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
15642 abbrev_ptr += bytes_read;
15643 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15644 abbrev_ptr += 1;
15645
15646 /* now read in declarations */
22d2f3ab 15647 for (;;)
c906108c 15648 {
43988095
JK
15649 LONGEST implicit_const;
15650
22d2f3ab
JK
15651 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15652 abbrev_ptr += bytes_read;
15653 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15654 abbrev_ptr += bytes_read;
43988095
JK
15655 if (abbrev_form == DW_FORM_implicit_const)
15656 {
15657 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15658 &bytes_read);
15659 abbrev_ptr += bytes_read;
15660 }
15661 else
15662 {
15663 /* Initialize it due to a false compiler warning. */
15664 implicit_const = -1;
15665 }
22d2f3ab
JK
15666
15667 if (abbrev_name == 0)
15668 break;
15669
f3dd6933 15670 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 15671 {
f3dd6933
DJ
15672 allocated_attrs += ATTR_ALLOC_CHUNK;
15673 cur_attrs
224c3ddb 15674 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 15675 }
ae038cb0 15676
aead7601
SM
15677 cur_attrs[cur_abbrev->num_attrs].name
15678 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 15679 cur_attrs[cur_abbrev->num_attrs].form
aead7601 15680 = (enum dwarf_form) abbrev_form;
43988095 15681 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 15682 ++cur_abbrev->num_attrs;
c906108c
SS
15683 }
15684
8d749320
SM
15685 cur_abbrev->attrs =
15686 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15687 cur_abbrev->num_attrs);
f3dd6933
DJ
15688 memcpy (cur_abbrev->attrs, cur_attrs,
15689 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15690
433df2d4 15691 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
15692
15693 /* Get next abbreviation.
15694 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
15695 always properly terminated with an abbrev number of 0.
15696 Exit loop if we encounter an abbreviation which we have
15697 already read (which means we are about to read the abbreviations
15698 for the next compile unit) or if the end of the abbreviation
15699 table is reached. */
433df2d4 15700 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
15701 break;
15702 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15703 abbrev_ptr += bytes_read;
433df2d4 15704 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
15705 break;
15706 }
f3dd6933
DJ
15707
15708 xfree (cur_attrs);
433df2d4 15709 return abbrev_table;
c906108c
SS
15710}
15711
433df2d4 15712/* Free the resources held by ABBREV_TABLE. */
c906108c 15713
c906108c 15714static void
433df2d4 15715abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 15716{
433df2d4
DE
15717 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15718 xfree (abbrev_table);
c906108c
SS
15719}
15720
f4dc4d17
DE
15721/* Same as abbrev_table_free but as a cleanup.
15722 We pass in a pointer to the pointer to the table so that we can
15723 set the pointer to NULL when we're done. It also simplifies
73051182 15724 build_type_psymtabs_1. */
f4dc4d17
DE
15725
15726static void
15727abbrev_table_free_cleanup (void *table_ptr)
15728{
9a3c8263 15729 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
f4dc4d17
DE
15730
15731 if (*abbrev_table_ptr != NULL)
15732 abbrev_table_free (*abbrev_table_ptr);
15733 *abbrev_table_ptr = NULL;
15734}
15735
433df2d4
DE
15736/* Read the abbrev table for CU from ABBREV_SECTION. */
15737
15738static void
15739dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15740 struct dwarf2_section_info *abbrev_section)
c906108c 15741{
433df2d4
DE
15742 cu->abbrev_table =
15743 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15744}
c906108c 15745
433df2d4 15746/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 15747
433df2d4
DE
15748static void
15749dwarf2_free_abbrev_table (void *ptr_to_cu)
15750{
9a3c8263 15751 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
c906108c 15752
a2ce51a0
DE
15753 if (cu->abbrev_table != NULL)
15754 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
15755 /* Set this to NULL so that we SEGV if we try to read it later,
15756 and also because free_comp_unit verifies this is NULL. */
15757 cu->abbrev_table = NULL;
15758}
15759\f
72bf9492
DJ
15760/* Returns nonzero if TAG represents a type that we might generate a partial
15761 symbol for. */
15762
15763static int
15764is_type_tag_for_partial (int tag)
15765{
15766 switch (tag)
15767 {
15768#if 0
15769 /* Some types that would be reasonable to generate partial symbols for,
15770 that we don't at present. */
15771 case DW_TAG_array_type:
15772 case DW_TAG_file_type:
15773 case DW_TAG_ptr_to_member_type:
15774 case DW_TAG_set_type:
15775 case DW_TAG_string_type:
15776 case DW_TAG_subroutine_type:
15777#endif
15778 case DW_TAG_base_type:
15779 case DW_TAG_class_type:
680b30c7 15780 case DW_TAG_interface_type:
72bf9492
DJ
15781 case DW_TAG_enumeration_type:
15782 case DW_TAG_structure_type:
15783 case DW_TAG_subrange_type:
15784 case DW_TAG_typedef:
15785 case DW_TAG_union_type:
15786 return 1;
15787 default:
15788 return 0;
15789 }
15790}
15791
15792/* Load all DIEs that are interesting for partial symbols into memory. */
15793
15794static struct partial_die_info *
dee91e82 15795load_partial_dies (const struct die_reader_specs *reader,
d521ce57 15796 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 15797{
dee91e82 15798 struct dwarf2_cu *cu = reader->cu;
bb5ed363 15799 struct objfile *objfile = cu->objfile;
72bf9492
DJ
15800 struct partial_die_info *part_die;
15801 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15802 struct abbrev_info *abbrev;
15803 unsigned int bytes_read;
5afb4e99 15804 unsigned int load_all = 0;
72bf9492
DJ
15805 int nesting_level = 1;
15806
15807 parent_die = NULL;
15808 last_die = NULL;
15809
7adf1e79
DE
15810 gdb_assert (cu->per_cu != NULL);
15811 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
15812 load_all = 1;
15813
72bf9492
DJ
15814 cu->partial_dies
15815 = htab_create_alloc_ex (cu->header.length / 12,
15816 partial_die_hash,
15817 partial_die_eq,
15818 NULL,
15819 &cu->comp_unit_obstack,
15820 hashtab_obstack_allocate,
15821 dummy_obstack_deallocate);
15822
8d749320 15823 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
15824
15825 while (1)
15826 {
15827 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15828
15829 /* A NULL abbrev means the end of a series of children. */
15830 if (abbrev == NULL)
15831 {
15832 if (--nesting_level == 0)
15833 {
15834 /* PART_DIE was probably the last thing allocated on the
15835 comp_unit_obstack, so we could call obstack_free
15836 here. We don't do that because the waste is small,
15837 and will be cleaned up when we're done with this
15838 compilation unit. This way, we're also more robust
15839 against other users of the comp_unit_obstack. */
15840 return first_die;
15841 }
15842 info_ptr += bytes_read;
15843 last_die = parent_die;
15844 parent_die = parent_die->die_parent;
15845 continue;
15846 }
15847
98bfdba5
PA
15848 /* Check for template arguments. We never save these; if
15849 they're seen, we just mark the parent, and go on our way. */
15850 if (parent_die != NULL
15851 && cu->language == language_cplus
15852 && (abbrev->tag == DW_TAG_template_type_param
15853 || abbrev->tag == DW_TAG_template_value_param))
15854 {
15855 parent_die->has_template_arguments = 1;
15856
15857 if (!load_all)
15858 {
15859 /* We don't need a partial DIE for the template argument. */
dee91e82 15860 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15861 continue;
15862 }
15863 }
15864
0d99eb77 15865 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
15866 Skip their other children. */
15867 if (!load_all
15868 && cu->language == language_cplus
15869 && parent_die != NULL
15870 && parent_die->tag == DW_TAG_subprogram)
15871 {
dee91e82 15872 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
15873 continue;
15874 }
15875
5afb4e99
DJ
15876 /* Check whether this DIE is interesting enough to save. Normally
15877 we would not be interested in members here, but there may be
15878 later variables referencing them via DW_AT_specification (for
15879 static members). */
15880 if (!load_all
15881 && !is_type_tag_for_partial (abbrev->tag)
72929c62 15882 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
15883 && abbrev->tag != DW_TAG_enumerator
15884 && abbrev->tag != DW_TAG_subprogram
bc30ff58 15885 && abbrev->tag != DW_TAG_lexical_block
72bf9492 15886 && abbrev->tag != DW_TAG_variable
5afb4e99 15887 && abbrev->tag != DW_TAG_namespace
f55ee35c 15888 && abbrev->tag != DW_TAG_module
95554aad 15889 && abbrev->tag != DW_TAG_member
74921315
KS
15890 && abbrev->tag != DW_TAG_imported_unit
15891 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
15892 {
15893 /* Otherwise we skip to the next sibling, if any. */
dee91e82 15894 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
15895 continue;
15896 }
15897
dee91e82
DE
15898 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15899 info_ptr);
72bf9492
DJ
15900
15901 /* This two-pass algorithm for processing partial symbols has a
15902 high cost in cache pressure. Thus, handle some simple cases
15903 here which cover the majority of C partial symbols. DIEs
15904 which neither have specification tags in them, nor could have
15905 specification tags elsewhere pointing at them, can simply be
15906 processed and discarded.
15907
15908 This segment is also optional; scan_partial_symbols and
15909 add_partial_symbol will handle these DIEs if we chain
15910 them in normally. When compilers which do not emit large
15911 quantities of duplicate debug information are more common,
15912 this code can probably be removed. */
15913
15914 /* Any complete simple types at the top level (pretty much all
15915 of them, for a language without namespaces), can be processed
15916 directly. */
15917 if (parent_die == NULL
15918 && part_die->has_specification == 0
15919 && part_die->is_declaration == 0
d8228535 15920 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
15921 || part_die->tag == DW_TAG_base_type
15922 || part_die->tag == DW_TAG_subrange_type))
15923 {
15924 if (building_psymtab && part_die->name != NULL)
04a679b8 15925 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15926 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 15927 &objfile->static_psymbols,
1762568f 15928 0, cu->language, objfile);
dee91e82 15929 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15930 continue;
15931 }
15932
d8228535
JK
15933 /* The exception for DW_TAG_typedef with has_children above is
15934 a workaround of GCC PR debug/47510. In the case of this complaint
15935 type_name_no_tag_or_error will error on such types later.
15936
15937 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15938 it could not find the child DIEs referenced later, this is checked
15939 above. In correct DWARF DW_TAG_typedef should have no children. */
15940
15941 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
15942 complaint (&symfile_complaints,
15943 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
15944 "- DIE at 0x%x [in module %s]"),
4262abfb 15945 part_die->offset.sect_off, objfile_name (objfile));
d8228535 15946
72bf9492
DJ
15947 /* If we're at the second level, and we're an enumerator, and
15948 our parent has no specification (meaning possibly lives in a
15949 namespace elsewhere), then we can add the partial symbol now
15950 instead of queueing it. */
15951 if (part_die->tag == DW_TAG_enumerator
15952 && parent_die != NULL
15953 && parent_die->die_parent == NULL
15954 && parent_die->tag == DW_TAG_enumeration_type
15955 && parent_die->has_specification == 0)
15956 {
15957 if (part_die->name == NULL)
3e43a32a
MS
15958 complaint (&symfile_complaints,
15959 _("malformed enumerator DIE ignored"));
72bf9492 15960 else if (building_psymtab)
04a679b8 15961 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 15962 VAR_DOMAIN, LOC_CONST,
9c37b5ae 15963 cu->language == language_cplus
bb5ed363
DE
15964 ? &objfile->global_psymbols
15965 : &objfile->static_psymbols,
1762568f 15966 0, cu->language, objfile);
72bf9492 15967
dee91e82 15968 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
15969 continue;
15970 }
15971
15972 /* We'll save this DIE so link it in. */
15973 part_die->die_parent = parent_die;
15974 part_die->die_sibling = NULL;
15975 part_die->die_child = NULL;
15976
15977 if (last_die && last_die == parent_die)
15978 last_die->die_child = part_die;
15979 else if (last_die)
15980 last_die->die_sibling = part_die;
15981
15982 last_die = part_die;
15983
15984 if (first_die == NULL)
15985 first_die = part_die;
15986
15987 /* Maybe add the DIE to the hash table. Not all DIEs that we
15988 find interesting need to be in the hash table, because we
15989 also have the parent/sibling/child chains; only those that we
15990 might refer to by offset later during partial symbol reading.
15991
15992 For now this means things that might have be the target of a
15993 DW_AT_specification, DW_AT_abstract_origin, or
15994 DW_AT_extension. DW_AT_extension will refer only to
15995 namespaces; DW_AT_abstract_origin refers to functions (and
15996 many things under the function DIE, but we do not recurse
15997 into function DIEs during partial symbol reading) and
15998 possibly variables as well; DW_AT_specification refers to
15999 declarations. Declarations ought to have the DW_AT_declaration
16000 flag. It happens that GCC forgets to put it in sometimes, but
16001 only for functions, not for types.
16002
16003 Adding more things than necessary to the hash table is harmless
16004 except for the performance cost. Adding too few will result in
5afb4e99
DJ
16005 wasted time in find_partial_die, when we reread the compilation
16006 unit with load_all_dies set. */
72bf9492 16007
5afb4e99 16008 if (load_all
72929c62 16009 || abbrev->tag == DW_TAG_constant
5afb4e99 16010 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
16011 || abbrev->tag == DW_TAG_variable
16012 || abbrev->tag == DW_TAG_namespace
16013 || part_die->is_declaration)
16014 {
16015 void **slot;
16016
16017 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 16018 part_die->offset.sect_off, INSERT);
72bf9492
DJ
16019 *slot = part_die;
16020 }
16021
8d749320 16022 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
72bf9492
DJ
16023
16024 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 16025 we have no reason to follow the children of structures; for other
98bfdba5
PA
16026 languages we have to, so that we can get at method physnames
16027 to infer fully qualified class names, for DW_AT_specification,
16028 and for C++ template arguments. For C++, we also look one level
16029 inside functions to find template arguments (if the name of the
16030 function does not already contain the template arguments).
bc30ff58
JB
16031
16032 For Ada, we need to scan the children of subprograms and lexical
16033 blocks as well because Ada allows the definition of nested
16034 entities that could be interesting for the debugger, such as
16035 nested subprograms for instance. */
72bf9492 16036 if (last_die->has_children
5afb4e99
DJ
16037 && (load_all
16038 || last_die->tag == DW_TAG_namespace
f55ee35c 16039 || last_die->tag == DW_TAG_module
72bf9492 16040 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
16041 || (cu->language == language_cplus
16042 && last_die->tag == DW_TAG_subprogram
16043 && (last_die->name == NULL
16044 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
16045 || (cu->language != language_c
16046 && (last_die->tag == DW_TAG_class_type
680b30c7 16047 || last_die->tag == DW_TAG_interface_type
72bf9492 16048 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
16049 || last_die->tag == DW_TAG_union_type))
16050 || (cu->language == language_ada
16051 && (last_die->tag == DW_TAG_subprogram
16052 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
16053 {
16054 nesting_level++;
16055 parent_die = last_die;
16056 continue;
16057 }
16058
16059 /* Otherwise we skip to the next sibling, if any. */
dee91e82 16060 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
16061
16062 /* Back to the top, do it again. */
16063 }
16064}
16065
c906108c
SS
16066/* Read a minimal amount of information into the minimal die structure. */
16067
d521ce57 16068static const gdb_byte *
dee91e82
DE
16069read_partial_die (const struct die_reader_specs *reader,
16070 struct partial_die_info *part_die,
16071 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 16072 const gdb_byte *info_ptr)
c906108c 16073{
dee91e82 16074 struct dwarf2_cu *cu = reader->cu;
bb5ed363 16075 struct objfile *objfile = cu->objfile;
d521ce57 16076 const gdb_byte *buffer = reader->buffer;
fa238c03 16077 unsigned int i;
c906108c 16078 struct attribute attr;
c5aa993b 16079 int has_low_pc_attr = 0;
c906108c 16080 int has_high_pc_attr = 0;
91da1414 16081 int high_pc_relative = 0;
c906108c 16082
72bf9492 16083 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 16084
b64f50a1 16085 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
16086
16087 info_ptr += abbrev_len;
16088
16089 if (abbrev == NULL)
16090 return info_ptr;
16091
c906108c
SS
16092 part_die->tag = abbrev->tag;
16093 part_die->has_children = abbrev->has_children;
c906108c
SS
16094
16095 for (i = 0; i < abbrev->num_attrs; ++i)
16096 {
dee91e82 16097 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
16098
16099 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 16100 partial symbol table. */
c906108c
SS
16101 switch (attr.name)
16102 {
16103 case DW_AT_name:
71c25dea
TT
16104 switch (part_die->tag)
16105 {
16106 case DW_TAG_compile_unit:
95554aad 16107 case DW_TAG_partial_unit:
348e048f 16108 case DW_TAG_type_unit:
71c25dea
TT
16109 /* Compilation units have a DW_AT_name that is a filename, not
16110 a source language identifier. */
16111 case DW_TAG_enumeration_type:
16112 case DW_TAG_enumerator:
16113 /* These tags always have simple identifiers already; no need
16114 to canonicalize them. */
16115 part_die->name = DW_STRING (&attr);
16116 break;
16117 default:
16118 part_die->name
16119 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
34a68019 16120 &objfile->per_bfd->storage_obstack);
71c25dea
TT
16121 break;
16122 }
c906108c 16123 break;
31ef98ae 16124 case DW_AT_linkage_name:
c906108c 16125 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
16126 /* Note that both forms of linkage name might appear. We
16127 assume they will be the same, and we only store the last
16128 one we see. */
94af9270
KS
16129 if (cu->language == language_ada)
16130 part_die->name = DW_STRING (&attr);
abc72ce4 16131 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
16132 break;
16133 case DW_AT_low_pc:
16134 has_low_pc_attr = 1;
31aa7e4e 16135 part_die->lowpc = attr_value_as_address (&attr);
c906108c
SS
16136 break;
16137 case DW_AT_high_pc:
16138 has_high_pc_attr = 1;
31aa7e4e
JB
16139 part_die->highpc = attr_value_as_address (&attr);
16140 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16141 high_pc_relative = 1;
c906108c
SS
16142 break;
16143 case DW_AT_location:
0963b4bd 16144 /* Support the .debug_loc offsets. */
8e19ed76
PS
16145 if (attr_form_is_block (&attr))
16146 {
95554aad 16147 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 16148 }
3690dd37 16149 else if (attr_form_is_section_offset (&attr))
8e19ed76 16150 {
4d3c2250 16151 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
16152 }
16153 else
16154 {
4d3c2250
KB
16155 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16156 "partial symbol information");
8e19ed76 16157 }
c906108c 16158 break;
c906108c
SS
16159 case DW_AT_external:
16160 part_die->is_external = DW_UNSND (&attr);
16161 break;
16162 case DW_AT_declaration:
16163 part_die->is_declaration = DW_UNSND (&attr);
16164 break;
16165 case DW_AT_type:
16166 part_die->has_type = 1;
16167 break;
16168 case DW_AT_abstract_origin:
16169 case DW_AT_specification:
72bf9492
DJ
16170 case DW_AT_extension:
16171 part_die->has_specification = 1;
c764a876 16172 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
16173 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16174 || cu->per_cu->is_dwz);
c906108c
SS
16175 break;
16176 case DW_AT_sibling:
16177 /* Ignore absolute siblings, they might point outside of
16178 the current compile unit. */
16179 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
16180 complaint (&symfile_complaints,
16181 _("ignoring absolute DW_AT_sibling"));
c906108c 16182 else
b9502d3f
WN
16183 {
16184 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16185 const gdb_byte *sibling_ptr = buffer + off;
16186
16187 if (sibling_ptr < info_ptr)
16188 complaint (&symfile_complaints,
16189 _("DW_AT_sibling points backwards"));
22869d73
KS
16190 else if (sibling_ptr > reader->buffer_end)
16191 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
16192 else
16193 part_die->sibling = sibling_ptr;
16194 }
c906108c 16195 break;
fa4028e9
JB
16196 case DW_AT_byte_size:
16197 part_die->has_byte_size = 1;
16198 break;
ff908ebf
AW
16199 case DW_AT_const_value:
16200 part_die->has_const_value = 1;
16201 break;
68511cec
CES
16202 case DW_AT_calling_convention:
16203 /* DWARF doesn't provide a way to identify a program's source-level
16204 entry point. DW_AT_calling_convention attributes are only meant
16205 to describe functions' calling conventions.
16206
16207 However, because it's a necessary piece of information in
0c1b455e
TT
16208 Fortran, and before DWARF 4 DW_CC_program was the only
16209 piece of debugging information whose definition refers to
16210 a 'main program' at all, several compilers marked Fortran
16211 main programs with DW_CC_program --- even when those
16212 functions use the standard calling conventions.
16213
16214 Although DWARF now specifies a way to provide this
16215 information, we support this practice for backward
16216 compatibility. */
68511cec 16217 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e
TT
16218 && cu->language == language_fortran)
16219 part_die->main_subprogram = 1;
68511cec 16220 break;
481860b3
GB
16221 case DW_AT_inline:
16222 if (DW_UNSND (&attr) == DW_INL_inlined
16223 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16224 part_die->may_be_inlined = 1;
16225 break;
95554aad
TT
16226
16227 case DW_AT_import:
16228 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
16229 {
16230 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16231 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16232 || cu->per_cu->is_dwz);
16233 }
95554aad
TT
16234 break;
16235
0c1b455e
TT
16236 case DW_AT_main_subprogram:
16237 part_die->main_subprogram = DW_UNSND (&attr);
16238 break;
16239
c906108c
SS
16240 default:
16241 break;
16242 }
16243 }
16244
91da1414
MW
16245 if (high_pc_relative)
16246 part_die->highpc += part_die->lowpc;
16247
9373cf26
JK
16248 if (has_low_pc_attr && has_high_pc_attr)
16249 {
16250 /* When using the GNU linker, .gnu.linkonce. sections are used to
16251 eliminate duplicate copies of functions and vtables and such.
16252 The linker will arbitrarily choose one and discard the others.
16253 The AT_*_pc values for such functions refer to local labels in
16254 these sections. If the section from that file was discarded, the
16255 labels are not in the output, so the relocs get a value of 0.
16256 If this is a discarded function, mark the pc bounds as invalid,
16257 so that GDB will ignore it. */
16258 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16259 {
bb5ed363 16260 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16261
16262 complaint (&symfile_complaints,
16263 _("DW_AT_low_pc %s is zero "
16264 "for DIE at 0x%x [in module %s]"),
16265 paddress (gdbarch, part_die->lowpc),
4262abfb 16266 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16267 }
16268 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16269 else if (part_die->lowpc >= part_die->highpc)
16270 {
bb5ed363 16271 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
16272
16273 complaint (&symfile_complaints,
16274 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16275 "for DIE at 0x%x [in module %s]"),
16276 paddress (gdbarch, part_die->lowpc),
16277 paddress (gdbarch, part_die->highpc),
4262abfb 16278 part_die->offset.sect_off, objfile_name (objfile));
9373cf26
JK
16279 }
16280 else
16281 part_die->has_pc_info = 1;
16282 }
85cbf3d3 16283
c906108c
SS
16284 return info_ptr;
16285}
16286
72bf9492
DJ
16287/* Find a cached partial DIE at OFFSET in CU. */
16288
16289static struct partial_die_info *
b64f50a1 16290find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
16291{
16292 struct partial_die_info *lookup_die = NULL;
16293 struct partial_die_info part_die;
16294
16295 part_die.offset = offset;
9a3c8263
SM
16296 lookup_die = ((struct partial_die_info *)
16297 htab_find_with_hash (cu->partial_dies, &part_die,
16298 offset.sect_off));
72bf9492 16299
72bf9492
DJ
16300 return lookup_die;
16301}
16302
348e048f
DE
16303/* Find a partial DIE at OFFSET, which may or may not be in CU,
16304 except in the case of .debug_types DIEs which do not reference
16305 outside their CU (they do however referencing other types via
55f1336d 16306 DW_FORM_ref_sig8). */
72bf9492
DJ
16307
16308static struct partial_die_info *
36586728 16309find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 16310{
bb5ed363 16311 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
16312 struct dwarf2_per_cu_data *per_cu = NULL;
16313 struct partial_die_info *pd = NULL;
72bf9492 16314
36586728
TT
16315 if (offset_in_dwz == cu->per_cu->is_dwz
16316 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
16317 {
16318 pd = find_partial_die_in_comp_unit (offset, cu);
16319 if (pd != NULL)
16320 return pd;
0d99eb77
DE
16321 /* We missed recording what we needed.
16322 Load all dies and try again. */
16323 per_cu = cu->per_cu;
5afb4e99 16324 }
0d99eb77
DE
16325 else
16326 {
16327 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 16328 if (cu->per_cu->is_debug_types)
0d99eb77
DE
16329 {
16330 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16331 " external reference to offset 0x%lx [in module %s].\n"),
16332 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16333 bfd_get_filename (objfile->obfd));
16334 }
36586728
TT
16335 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16336 objfile);
72bf9492 16337
0d99eb77
DE
16338 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16339 load_partial_comp_unit (per_cu);
ae038cb0 16340
0d99eb77
DE
16341 per_cu->cu->last_used = 0;
16342 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16343 }
5afb4e99 16344
dee91e82
DE
16345 /* If we didn't find it, and not all dies have been loaded,
16346 load them all and try again. */
16347
5afb4e99
DJ
16348 if (pd == NULL && per_cu->load_all_dies == 0)
16349 {
5afb4e99 16350 per_cu->load_all_dies = 1;
fd820528
DE
16351
16352 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16353 THIS_CU->cu may already be in use. So we can't just free it and
16354 replace its DIEs with the ones we read in. Instead, we leave those
16355 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16356 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16357 set. */
dee91e82 16358 load_partial_comp_unit (per_cu);
5afb4e99
DJ
16359
16360 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16361 }
16362
16363 if (pd == NULL)
16364 internal_error (__FILE__, __LINE__,
3e43a32a
MS
16365 _("could not find partial DIE 0x%x "
16366 "in cache [from module %s]\n"),
b64f50a1 16367 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 16368 return pd;
72bf9492
DJ
16369}
16370
abc72ce4
DE
16371/* See if we can figure out if the class lives in a namespace. We do
16372 this by looking for a member function; its demangled name will
16373 contain namespace info, if there is any. */
16374
16375static void
16376guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16377 struct dwarf2_cu *cu)
16378{
16379 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16380 what template types look like, because the demangler
16381 frequently doesn't give the same name as the debug info. We
16382 could fix this by only using the demangled name to get the
16383 prefix (but see comment in read_structure_type). */
16384
16385 struct partial_die_info *real_pdi;
16386 struct partial_die_info *child_pdi;
16387
16388 /* If this DIE (this DIE's specification, if any) has a parent, then
16389 we should not do this. We'll prepend the parent's fully qualified
16390 name when we create the partial symbol. */
16391
16392 real_pdi = struct_pdi;
16393 while (real_pdi->has_specification)
36586728
TT
16394 real_pdi = find_partial_die (real_pdi->spec_offset,
16395 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
16396
16397 if (real_pdi->die_parent != NULL)
16398 return;
16399
16400 for (child_pdi = struct_pdi->die_child;
16401 child_pdi != NULL;
16402 child_pdi = child_pdi->die_sibling)
16403 {
16404 if (child_pdi->tag == DW_TAG_subprogram
16405 && child_pdi->linkage_name != NULL)
16406 {
16407 char *actual_class_name
16408 = language_class_name_from_physname (cu->language_defn,
16409 child_pdi->linkage_name);
16410 if (actual_class_name != NULL)
16411 {
16412 struct_pdi->name
224c3ddb
SM
16413 = ((const char *)
16414 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16415 actual_class_name,
16416 strlen (actual_class_name)));
abc72ce4
DE
16417 xfree (actual_class_name);
16418 }
16419 break;
16420 }
16421 }
16422}
16423
72bf9492
DJ
16424/* Adjust PART_DIE before generating a symbol for it. This function
16425 may set the is_external flag or change the DIE's name. */
16426
16427static void
16428fixup_partial_die (struct partial_die_info *part_die,
16429 struct dwarf2_cu *cu)
16430{
abc72ce4
DE
16431 /* Once we've fixed up a die, there's no point in doing so again.
16432 This also avoids a memory leak if we were to call
16433 guess_partial_die_structure_name multiple times. */
16434 if (part_die->fixup_called)
16435 return;
16436
72bf9492
DJ
16437 /* If we found a reference attribute and the DIE has no name, try
16438 to find a name in the referred to DIE. */
16439
16440 if (part_die->name == NULL && part_die->has_specification)
16441 {
16442 struct partial_die_info *spec_die;
72bf9492 16443
36586728
TT
16444 spec_die = find_partial_die (part_die->spec_offset,
16445 part_die->spec_is_dwz, cu);
72bf9492 16446
10b3939b 16447 fixup_partial_die (spec_die, cu);
72bf9492
DJ
16448
16449 if (spec_die->name)
16450 {
16451 part_die->name = spec_die->name;
16452
16453 /* Copy DW_AT_external attribute if it is set. */
16454 if (spec_die->is_external)
16455 part_die->is_external = spec_die->is_external;
16456 }
16457 }
16458
16459 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
16460
16461 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 16462 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 16463
abc72ce4
DE
16464 /* If there is no parent die to provide a namespace, and there are
16465 children, see if we can determine the namespace from their linkage
122d1940 16466 name. */
abc72ce4 16467 if (cu->language == language_cplus
8b70b953 16468 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
16469 && part_die->die_parent == NULL
16470 && part_die->has_children
16471 && (part_die->tag == DW_TAG_class_type
16472 || part_die->tag == DW_TAG_structure_type
16473 || part_die->tag == DW_TAG_union_type))
16474 guess_partial_die_structure_name (part_die, cu);
16475
53832f31
TT
16476 /* GCC might emit a nameless struct or union that has a linkage
16477 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16478 if (part_die->name == NULL
96408a79
SA
16479 && (part_die->tag == DW_TAG_class_type
16480 || part_die->tag == DW_TAG_interface_type
16481 || part_die->tag == DW_TAG_structure_type
16482 || part_die->tag == DW_TAG_union_type)
53832f31
TT
16483 && part_die->linkage_name != NULL)
16484 {
16485 char *demangled;
16486
8de20a37 16487 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
16488 if (demangled)
16489 {
96408a79
SA
16490 const char *base;
16491
16492 /* Strip any leading namespaces/classes, keep only the base name.
16493 DW_AT_name for named DIEs does not contain the prefixes. */
16494 base = strrchr (demangled, ':');
16495 if (base && base > demangled && base[-1] == ':')
16496 base++;
16497 else
16498 base = demangled;
16499
34a68019 16500 part_die->name
224c3ddb
SM
16501 = ((const char *)
16502 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16503 base, strlen (base)));
53832f31
TT
16504 xfree (demangled);
16505 }
16506 }
16507
abc72ce4 16508 part_die->fixup_called = 1;
72bf9492
DJ
16509}
16510
a8329558 16511/* Read an attribute value described by an attribute form. */
c906108c 16512
d521ce57 16513static const gdb_byte *
dee91e82
DE
16514read_attribute_value (const struct die_reader_specs *reader,
16515 struct attribute *attr, unsigned form,
43988095 16516 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 16517{
dee91e82 16518 struct dwarf2_cu *cu = reader->cu;
3e29f34a
MR
16519 struct objfile *objfile = cu->objfile;
16520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 16521 bfd *abfd = reader->abfd;
e7c27a73 16522 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16523 unsigned int bytes_read;
16524 struct dwarf_block *blk;
16525
aead7601 16526 attr->form = (enum dwarf_form) form;
a8329558 16527 switch (form)
c906108c 16528 {
c906108c 16529 case DW_FORM_ref_addr:
ae411497 16530 if (cu->header.version == 2)
4568ecf9 16531 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 16532 else
4568ecf9
DE
16533 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16534 &cu->header, &bytes_read);
ae411497
TT
16535 info_ptr += bytes_read;
16536 break;
36586728
TT
16537 case DW_FORM_GNU_ref_alt:
16538 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16539 info_ptr += bytes_read;
16540 break;
ae411497 16541 case DW_FORM_addr:
e7c27a73 16542 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 16543 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 16544 info_ptr += bytes_read;
c906108c
SS
16545 break;
16546 case DW_FORM_block2:
7b5a2f43 16547 blk = dwarf_alloc_block (cu);
c906108c
SS
16548 blk->size = read_2_bytes (abfd, info_ptr);
16549 info_ptr += 2;
16550 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16551 info_ptr += blk->size;
16552 DW_BLOCK (attr) = blk;
16553 break;
16554 case DW_FORM_block4:
7b5a2f43 16555 blk = dwarf_alloc_block (cu);
c906108c
SS
16556 blk->size = read_4_bytes (abfd, info_ptr);
16557 info_ptr += 4;
16558 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16559 info_ptr += blk->size;
16560 DW_BLOCK (attr) = blk;
16561 break;
16562 case DW_FORM_data2:
16563 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16564 info_ptr += 2;
16565 break;
16566 case DW_FORM_data4:
16567 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16568 info_ptr += 4;
16569 break;
16570 case DW_FORM_data8:
16571 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16572 info_ptr += 8;
16573 break;
0224619f
JK
16574 case DW_FORM_data16:
16575 blk = dwarf_alloc_block (cu);
16576 blk->size = 16;
16577 blk->data = read_n_bytes (abfd, info_ptr, 16);
16578 info_ptr += 16;
16579 DW_BLOCK (attr) = blk;
16580 break;
2dc7f7b3
TT
16581 case DW_FORM_sec_offset:
16582 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16583 info_ptr += bytes_read;
16584 break;
c906108c 16585 case DW_FORM_string:
9b1c24c8 16586 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 16587 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
16588 info_ptr += bytes_read;
16589 break;
4bdf3d34 16590 case DW_FORM_strp:
36586728
TT
16591 if (!cu->per_cu->is_dwz)
16592 {
16593 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16594 &bytes_read);
16595 DW_STRING_IS_CANONICAL (attr) = 0;
16596 info_ptr += bytes_read;
16597 break;
16598 }
16599 /* FALLTHROUGH */
43988095
JK
16600 case DW_FORM_line_strp:
16601 if (!cu->per_cu->is_dwz)
16602 {
16603 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16604 cu_header, &bytes_read);
16605 DW_STRING_IS_CANONICAL (attr) = 0;
16606 info_ptr += bytes_read;
16607 break;
16608 }
16609 /* FALLTHROUGH */
36586728
TT
16610 case DW_FORM_GNU_strp_alt:
16611 {
16612 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16613 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16614 &bytes_read);
16615
16616 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16617 DW_STRING_IS_CANONICAL (attr) = 0;
16618 info_ptr += bytes_read;
16619 }
4bdf3d34 16620 break;
2dc7f7b3 16621 case DW_FORM_exprloc:
c906108c 16622 case DW_FORM_block:
7b5a2f43 16623 blk = dwarf_alloc_block (cu);
c906108c
SS
16624 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16625 info_ptr += bytes_read;
16626 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16627 info_ptr += blk->size;
16628 DW_BLOCK (attr) = blk;
16629 break;
16630 case DW_FORM_block1:
7b5a2f43 16631 blk = dwarf_alloc_block (cu);
c906108c
SS
16632 blk->size = read_1_byte (abfd, info_ptr);
16633 info_ptr += 1;
16634 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16635 info_ptr += blk->size;
16636 DW_BLOCK (attr) = blk;
16637 break;
16638 case DW_FORM_data1:
16639 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16640 info_ptr += 1;
16641 break;
16642 case DW_FORM_flag:
16643 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16644 info_ptr += 1;
16645 break;
2dc7f7b3
TT
16646 case DW_FORM_flag_present:
16647 DW_UNSND (attr) = 1;
16648 break;
c906108c
SS
16649 case DW_FORM_sdata:
16650 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16651 info_ptr += bytes_read;
16652 break;
16653 case DW_FORM_udata:
16654 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16655 info_ptr += bytes_read;
16656 break;
16657 case DW_FORM_ref1:
4568ecf9
DE
16658 DW_UNSND (attr) = (cu->header.offset.sect_off
16659 + read_1_byte (abfd, info_ptr));
c906108c
SS
16660 info_ptr += 1;
16661 break;
16662 case DW_FORM_ref2:
4568ecf9
DE
16663 DW_UNSND (attr) = (cu->header.offset.sect_off
16664 + read_2_bytes (abfd, info_ptr));
c906108c
SS
16665 info_ptr += 2;
16666 break;
16667 case DW_FORM_ref4:
4568ecf9
DE
16668 DW_UNSND (attr) = (cu->header.offset.sect_off
16669 + read_4_bytes (abfd, info_ptr));
c906108c
SS
16670 info_ptr += 4;
16671 break;
613e1657 16672 case DW_FORM_ref8:
4568ecf9
DE
16673 DW_UNSND (attr) = (cu->header.offset.sect_off
16674 + read_8_bytes (abfd, info_ptr));
613e1657
KB
16675 info_ptr += 8;
16676 break;
55f1336d 16677 case DW_FORM_ref_sig8:
ac9ec31b 16678 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
16679 info_ptr += 8;
16680 break;
c906108c 16681 case DW_FORM_ref_udata:
4568ecf9
DE
16682 DW_UNSND (attr) = (cu->header.offset.sect_off
16683 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
16684 info_ptr += bytes_read;
16685 break;
c906108c 16686 case DW_FORM_indirect:
a8329558
KW
16687 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16688 info_ptr += bytes_read;
43988095
JK
16689 if (form == DW_FORM_implicit_const)
16690 {
16691 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16692 info_ptr += bytes_read;
16693 }
16694 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16695 info_ptr);
16696 break;
16697 case DW_FORM_implicit_const:
16698 DW_SND (attr) = implicit_const;
a8329558 16699 break;
3019eac3
DE
16700 case DW_FORM_GNU_addr_index:
16701 if (reader->dwo_file == NULL)
16702 {
16703 /* For now flag a hard error.
16704 Later we can turn this into a complaint. */
16705 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16706 dwarf_form_name (form),
16707 bfd_get_filename (abfd));
16708 }
16709 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16710 info_ptr += bytes_read;
16711 break;
16712 case DW_FORM_GNU_str_index:
16713 if (reader->dwo_file == NULL)
16714 {
16715 /* For now flag a hard error.
16716 Later we can turn this into a complaint if warranted. */
16717 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16718 dwarf_form_name (form),
16719 bfd_get_filename (abfd));
16720 }
16721 {
16722 ULONGEST str_index =
16723 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16724
342587c4 16725 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
16726 DW_STRING_IS_CANONICAL (attr) = 0;
16727 info_ptr += bytes_read;
16728 }
16729 break;
c906108c 16730 default:
8a3fe4f8 16731 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
16732 dwarf_form_name (form),
16733 bfd_get_filename (abfd));
c906108c 16734 }
28e94949 16735
36586728 16736 /* Super hack. */
7771576e 16737 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
16738 attr->form = DW_FORM_GNU_ref_alt;
16739
28e94949
JB
16740 /* We have seen instances where the compiler tried to emit a byte
16741 size attribute of -1 which ended up being encoded as an unsigned
16742 0xffffffff. Although 0xffffffff is technically a valid size value,
16743 an object of this size seems pretty unlikely so we can relatively
16744 safely treat these cases as if the size attribute was invalid and
16745 treat them as zero by default. */
16746 if (attr->name == DW_AT_byte_size
16747 && form == DW_FORM_data4
16748 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
16749 {
16750 complaint
16751 (&symfile_complaints,
43bbcdc2
PH
16752 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16753 hex_string (DW_UNSND (attr)));
01c66ae6
JB
16754 DW_UNSND (attr) = 0;
16755 }
28e94949 16756
c906108c
SS
16757 return info_ptr;
16758}
16759
a8329558
KW
16760/* Read an attribute described by an abbreviated attribute. */
16761
d521ce57 16762static const gdb_byte *
dee91e82
DE
16763read_attribute (const struct die_reader_specs *reader,
16764 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 16765 const gdb_byte *info_ptr)
a8329558
KW
16766{
16767 attr->name = abbrev->name;
43988095
JK
16768 return read_attribute_value (reader, attr, abbrev->form,
16769 abbrev->implicit_const, info_ptr);
a8329558
KW
16770}
16771
0963b4bd 16772/* Read dwarf information from a buffer. */
c906108c
SS
16773
16774static unsigned int
a1855c1d 16775read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16776{
fe1b8b76 16777 return bfd_get_8 (abfd, buf);
c906108c
SS
16778}
16779
16780static int
a1855c1d 16781read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 16782{
fe1b8b76 16783 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
16784}
16785
16786static unsigned int
a1855c1d 16787read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16788{
fe1b8b76 16789 return bfd_get_16 (abfd, buf);
c906108c
SS
16790}
16791
21ae7a4d 16792static int
a1855c1d 16793read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16794{
16795 return bfd_get_signed_16 (abfd, buf);
16796}
16797
c906108c 16798static unsigned int
a1855c1d 16799read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16800{
fe1b8b76 16801 return bfd_get_32 (abfd, buf);
c906108c
SS
16802}
16803
21ae7a4d 16804static int
a1855c1d 16805read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
16806{
16807 return bfd_get_signed_32 (abfd, buf);
16808}
16809
93311388 16810static ULONGEST
a1855c1d 16811read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 16812{
fe1b8b76 16813 return bfd_get_64 (abfd, buf);
c906108c
SS
16814}
16815
16816static CORE_ADDR
d521ce57 16817read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 16818 unsigned int *bytes_read)
c906108c 16819{
e7c27a73 16820 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
16821 CORE_ADDR retval = 0;
16822
107d2387 16823 if (cu_header->signed_addr_p)
c906108c 16824 {
107d2387
AC
16825 switch (cu_header->addr_size)
16826 {
16827 case 2:
fe1b8b76 16828 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
16829 break;
16830 case 4:
fe1b8b76 16831 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
16832 break;
16833 case 8:
fe1b8b76 16834 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
16835 break;
16836 default:
8e65ff28 16837 internal_error (__FILE__, __LINE__,
e2e0b3e5 16838 _("read_address: bad switch, signed [in module %s]"),
659b0389 16839 bfd_get_filename (abfd));
107d2387
AC
16840 }
16841 }
16842 else
16843 {
16844 switch (cu_header->addr_size)
16845 {
16846 case 2:
fe1b8b76 16847 retval = bfd_get_16 (abfd, buf);
107d2387
AC
16848 break;
16849 case 4:
fe1b8b76 16850 retval = bfd_get_32 (abfd, buf);
107d2387
AC
16851 break;
16852 case 8:
fe1b8b76 16853 retval = bfd_get_64 (abfd, buf);
107d2387
AC
16854 break;
16855 default:
8e65ff28 16856 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
16857 _("read_address: bad switch, "
16858 "unsigned [in module %s]"),
659b0389 16859 bfd_get_filename (abfd));
107d2387 16860 }
c906108c 16861 }
64367e0a 16862
107d2387
AC
16863 *bytes_read = cu_header->addr_size;
16864 return retval;
c906108c
SS
16865}
16866
f7ef9339 16867/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
16868 specification allows the initial length to take up either 4 bytes
16869 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16870 bytes describe the length and all offsets will be 8 bytes in length
16871 instead of 4.
16872
f7ef9339
KB
16873 An older, non-standard 64-bit format is also handled by this
16874 function. The older format in question stores the initial length
16875 as an 8-byte quantity without an escape value. Lengths greater
16876 than 2^32 aren't very common which means that the initial 4 bytes
16877 is almost always zero. Since a length value of zero doesn't make
16878 sense for the 32-bit format, this initial zero can be considered to
16879 be an escape value which indicates the presence of the older 64-bit
16880 format. As written, the code can't detect (old format) lengths
917c78fc
MK
16881 greater than 4GB. If it becomes necessary to handle lengths
16882 somewhat larger than 4GB, we could allow other small values (such
16883 as the non-sensical values of 1, 2, and 3) to also be used as
16884 escape values indicating the presence of the old format.
f7ef9339 16885
917c78fc
MK
16886 The value returned via bytes_read should be used to increment the
16887 relevant pointer after calling read_initial_length().
c764a876 16888
613e1657
KB
16889 [ Note: read_initial_length() and read_offset() are based on the
16890 document entitled "DWARF Debugging Information Format", revision
f7ef9339 16891 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
16892 from:
16893
f7ef9339 16894 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 16895
613e1657
KB
16896 This document is only a draft and is subject to change. (So beware.)
16897
f7ef9339 16898 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
16899 determined empirically by examining 64-bit ELF files produced by
16900 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
16901
16902 - Kevin, July 16, 2002
613e1657
KB
16903 ] */
16904
16905static LONGEST
d521ce57 16906read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 16907{
fe1b8b76 16908 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 16909
dd373385 16910 if (length == 0xffffffff)
613e1657 16911 {
fe1b8b76 16912 length = bfd_get_64 (abfd, buf + 4);
613e1657 16913 *bytes_read = 12;
613e1657 16914 }
dd373385 16915 else if (length == 0)
f7ef9339 16916 {
dd373385 16917 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 16918 length = bfd_get_64 (abfd, buf);
f7ef9339 16919 *bytes_read = 8;
f7ef9339 16920 }
613e1657
KB
16921 else
16922 {
16923 *bytes_read = 4;
613e1657
KB
16924 }
16925
c764a876
DE
16926 return length;
16927}
dd373385 16928
c764a876
DE
16929/* Cover function for read_initial_length.
16930 Returns the length of the object at BUF, and stores the size of the
16931 initial length in *BYTES_READ and stores the size that offsets will be in
16932 *OFFSET_SIZE.
16933 If the initial length size is not equivalent to that specified in
16934 CU_HEADER then issue a complaint.
16935 This is useful when reading non-comp-unit headers. */
dd373385 16936
c764a876 16937static LONGEST
d521ce57 16938read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
16939 const struct comp_unit_head *cu_header,
16940 unsigned int *bytes_read,
16941 unsigned int *offset_size)
16942{
16943 LONGEST length = read_initial_length (abfd, buf, bytes_read);
16944
16945 gdb_assert (cu_header->initial_length_size == 4
16946 || cu_header->initial_length_size == 8
16947 || cu_header->initial_length_size == 12);
16948
16949 if (cu_header->initial_length_size != *bytes_read)
16950 complaint (&symfile_complaints,
16951 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 16952
c764a876 16953 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 16954 return length;
613e1657
KB
16955}
16956
16957/* Read an offset from the data stream. The size of the offset is
917c78fc 16958 given by cu_header->offset_size. */
613e1657
KB
16959
16960static LONGEST
d521ce57
TT
16961read_offset (bfd *abfd, const gdb_byte *buf,
16962 const struct comp_unit_head *cu_header,
891d2f0b 16963 unsigned int *bytes_read)
c764a876
DE
16964{
16965 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 16966
c764a876
DE
16967 *bytes_read = cu_header->offset_size;
16968 return offset;
16969}
16970
16971/* Read an offset from the data stream. */
16972
16973static LONGEST
d521ce57 16974read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
16975{
16976 LONGEST retval = 0;
16977
c764a876 16978 switch (offset_size)
613e1657
KB
16979 {
16980 case 4:
fe1b8b76 16981 retval = bfd_get_32 (abfd, buf);
613e1657
KB
16982 break;
16983 case 8:
fe1b8b76 16984 retval = bfd_get_64 (abfd, buf);
613e1657
KB
16985 break;
16986 default:
8e65ff28 16987 internal_error (__FILE__, __LINE__,
c764a876 16988 _("read_offset_1: bad switch [in module %s]"),
659b0389 16989 bfd_get_filename (abfd));
613e1657
KB
16990 }
16991
917c78fc 16992 return retval;
613e1657
KB
16993}
16994
d521ce57
TT
16995static const gdb_byte *
16996read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
16997{
16998 /* If the size of a host char is 8 bits, we can return a pointer
16999 to the buffer, otherwise we have to copy the data to a buffer
17000 allocated on the temporary obstack. */
4bdf3d34 17001 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 17002 return buf;
c906108c
SS
17003}
17004
d521ce57
TT
17005static const char *
17006read_direct_string (bfd *abfd, const gdb_byte *buf,
17007 unsigned int *bytes_read_ptr)
c906108c
SS
17008{
17009 /* If the size of a host char is 8 bits, we can return a pointer
17010 to the string, otherwise we have to copy the string to a buffer
17011 allocated on the temporary obstack. */
4bdf3d34 17012 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
17013 if (*buf == '\0')
17014 {
17015 *bytes_read_ptr = 1;
17016 return NULL;
17017 }
d521ce57
TT
17018 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17019 return (const char *) buf;
4bdf3d34
JJ
17020}
17021
43988095
JK
17022/* Return pointer to string at section SECT offset STR_OFFSET with error
17023 reporting strings FORM_NAME and SECT_NAME. */
17024
d521ce57 17025static const char *
43988095
JK
17026read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17027 struct dwarf2_section_info *sect,
17028 const char *form_name,
17029 const char *sect_name)
17030{
17031 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17032 if (sect->buffer == NULL)
17033 error (_("%s used without %s section [in module %s]"),
17034 form_name, sect_name, bfd_get_filename (abfd));
17035 if (str_offset >= sect->size)
17036 error (_("%s pointing outside of %s section [in module %s]"),
17037 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 17038 gdb_assert (HOST_CHAR_BIT == 8);
43988095 17039 if (sect->buffer[str_offset] == '\0')
4bdf3d34 17040 return NULL;
43988095
JK
17041 return (const char *) (sect->buffer + str_offset);
17042}
17043
17044/* Return pointer to string at .debug_str offset STR_OFFSET. */
17045
17046static const char *
17047read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17048{
17049 return read_indirect_string_at_offset_from (abfd, str_offset,
17050 &dwarf2_per_objfile->str,
17051 "DW_FORM_strp", ".debug_str");
17052}
17053
17054/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17055
17056static const char *
17057read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17058{
17059 return read_indirect_string_at_offset_from (abfd, str_offset,
17060 &dwarf2_per_objfile->line_str,
17061 "DW_FORM_line_strp",
17062 ".debug_line_str");
c906108c
SS
17063}
17064
36586728
TT
17065/* Read a string at offset STR_OFFSET in the .debug_str section from
17066 the .dwz file DWZ. Throw an error if the offset is too large. If
17067 the string consists of a single NUL byte, return NULL; otherwise
17068 return a pointer to the string. */
17069
d521ce57 17070static const char *
36586728
TT
17071read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17072{
17073 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17074
17075 if (dwz->str.buffer == NULL)
17076 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17077 "section [in module %s]"),
17078 bfd_get_filename (dwz->dwz_bfd));
17079 if (str_offset >= dwz->str.size)
17080 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17081 ".debug_str section [in module %s]"),
17082 bfd_get_filename (dwz->dwz_bfd));
17083 gdb_assert (HOST_CHAR_BIT == 8);
17084 if (dwz->str.buffer[str_offset] == '\0')
17085 return NULL;
d521ce57 17086 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
17087}
17088
43988095
JK
17089/* Return pointer to string at .debug_str offset as read from BUF.
17090 BUF is assumed to be in a compilation unit described by CU_HEADER.
17091 Return *BYTES_READ_PTR count of bytes read from BUF. */
17092
d521ce57
TT
17093static const char *
17094read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
17095 const struct comp_unit_head *cu_header,
17096 unsigned int *bytes_read_ptr)
17097{
17098 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17099
17100 return read_indirect_string_at_offset (abfd, str_offset);
17101}
17102
43988095
JK
17103/* Return pointer to string at .debug_line_str offset as read from BUF.
17104 BUF is assumed to be in a compilation unit described by CU_HEADER.
17105 Return *BYTES_READ_PTR count of bytes read from BUF. */
17106
17107static const char *
17108read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17109 const struct comp_unit_head *cu_header,
17110 unsigned int *bytes_read_ptr)
17111{
17112 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17113
17114 return read_indirect_line_string_at_offset (abfd, str_offset);
17115}
17116
17117ULONGEST
d521ce57 17118read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 17119 unsigned int *bytes_read_ptr)
c906108c 17120{
12df843f 17121 ULONGEST result;
ce5d95e1 17122 unsigned int num_read;
870f88f7 17123 int shift;
c906108c
SS
17124 unsigned char byte;
17125
17126 result = 0;
17127 shift = 0;
17128 num_read = 0;
c906108c
SS
17129 while (1)
17130 {
fe1b8b76 17131 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17132 buf++;
17133 num_read++;
12df843f 17134 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
17135 if ((byte & 128) == 0)
17136 {
17137 break;
17138 }
17139 shift += 7;
17140 }
17141 *bytes_read_ptr = num_read;
17142 return result;
17143}
17144
12df843f 17145static LONGEST
d521ce57
TT
17146read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17147 unsigned int *bytes_read_ptr)
c906108c 17148{
12df843f 17149 LONGEST result;
870f88f7 17150 int shift, num_read;
c906108c
SS
17151 unsigned char byte;
17152
17153 result = 0;
17154 shift = 0;
c906108c 17155 num_read = 0;
c906108c
SS
17156 while (1)
17157 {
fe1b8b76 17158 byte = bfd_get_8 (abfd, buf);
c906108c
SS
17159 buf++;
17160 num_read++;
12df843f 17161 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
17162 shift += 7;
17163 if ((byte & 128) == 0)
17164 {
17165 break;
17166 }
17167 }
77e0b926 17168 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 17169 result |= -(((LONGEST) 1) << shift);
c906108c
SS
17170 *bytes_read_ptr = num_read;
17171 return result;
17172}
17173
3019eac3
DE
17174/* Given index ADDR_INDEX in .debug_addr, fetch the value.
17175 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17176 ADDR_SIZE is the size of addresses from the CU header. */
17177
17178static CORE_ADDR
17179read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17180{
17181 struct objfile *objfile = dwarf2_per_objfile->objfile;
17182 bfd *abfd = objfile->obfd;
17183 const gdb_byte *info_ptr;
17184
17185 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17186 if (dwarf2_per_objfile->addr.buffer == NULL)
17187 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 17188 objfile_name (objfile));
3019eac3
DE
17189 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17190 error (_("DW_FORM_addr_index pointing outside of "
17191 ".debug_addr section [in module %s]"),
4262abfb 17192 objfile_name (objfile));
3019eac3
DE
17193 info_ptr = (dwarf2_per_objfile->addr.buffer
17194 + addr_base + addr_index * addr_size);
17195 if (addr_size == 4)
17196 return bfd_get_32 (abfd, info_ptr);
17197 else
17198 return bfd_get_64 (abfd, info_ptr);
17199}
17200
17201/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17202
17203static CORE_ADDR
17204read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17205{
17206 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17207}
17208
17209/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17210
17211static CORE_ADDR
d521ce57 17212read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
17213 unsigned int *bytes_read)
17214{
17215 bfd *abfd = cu->objfile->obfd;
17216 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17217
17218 return read_addr_index (cu, addr_index);
17219}
17220
17221/* Data structure to pass results from dwarf2_read_addr_index_reader
17222 back to dwarf2_read_addr_index. */
17223
17224struct dwarf2_read_addr_index_data
17225{
17226 ULONGEST addr_base;
17227 int addr_size;
17228};
17229
17230/* die_reader_func for dwarf2_read_addr_index. */
17231
17232static void
17233dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 17234 const gdb_byte *info_ptr,
3019eac3
DE
17235 struct die_info *comp_unit_die,
17236 int has_children,
17237 void *data)
17238{
17239 struct dwarf2_cu *cu = reader->cu;
17240 struct dwarf2_read_addr_index_data *aidata =
17241 (struct dwarf2_read_addr_index_data *) data;
17242
17243 aidata->addr_base = cu->addr_base;
17244 aidata->addr_size = cu->header.addr_size;
17245}
17246
17247/* Given an index in .debug_addr, fetch the value.
17248 NOTE: This can be called during dwarf expression evaluation,
17249 long after the debug information has been read, and thus per_cu->cu
17250 may no longer exist. */
17251
17252CORE_ADDR
17253dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17254 unsigned int addr_index)
17255{
17256 struct objfile *objfile = per_cu->objfile;
17257 struct dwarf2_cu *cu = per_cu->cu;
17258 ULONGEST addr_base;
17259 int addr_size;
17260
17261 /* This is intended to be called from outside this file. */
17262 dw2_setup (objfile);
17263
17264 /* We need addr_base and addr_size.
17265 If we don't have PER_CU->cu, we have to get it.
17266 Nasty, but the alternative is storing the needed info in PER_CU,
17267 which at this point doesn't seem justified: it's not clear how frequently
17268 it would get used and it would increase the size of every PER_CU.
17269 Entry points like dwarf2_per_cu_addr_size do a similar thing
17270 so we're not in uncharted territory here.
17271 Alas we need to be a bit more complicated as addr_base is contained
17272 in the DIE.
17273
17274 We don't need to read the entire CU(/TU).
17275 We just need the header and top level die.
a1b64ce1 17276
3019eac3 17277 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 17278 For now we skip this optimization. */
3019eac3
DE
17279
17280 if (cu != NULL)
17281 {
17282 addr_base = cu->addr_base;
17283 addr_size = cu->header.addr_size;
17284 }
17285 else
17286 {
17287 struct dwarf2_read_addr_index_data aidata;
17288
a1b64ce1
DE
17289 /* Note: We can't use init_cutu_and_read_dies_simple here,
17290 we need addr_base. */
17291 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17292 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
17293 addr_base = aidata.addr_base;
17294 addr_size = aidata.addr_size;
17295 }
17296
17297 return read_addr_index_1 (addr_index, addr_base, addr_size);
17298}
17299
57d63ce2
DE
17300/* Given a DW_FORM_GNU_str_index, fetch the string.
17301 This is only used by the Fission support. */
3019eac3 17302
d521ce57 17303static const char *
342587c4 17304read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3
DE
17305{
17306 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 17307 const char *objf_name = objfile_name (objfile);
3019eac3 17308 bfd *abfd = objfile->obfd;
342587c4 17309 struct dwarf2_cu *cu = reader->cu;
73869dc2
DE
17310 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17311 struct dwarf2_section_info *str_offsets_section =
17312 &reader->dwo_file->sections.str_offsets;
d521ce57 17313 const gdb_byte *info_ptr;
3019eac3 17314 ULONGEST str_offset;
57d63ce2 17315 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 17316
73869dc2
DE
17317 dwarf2_read_section (objfile, str_section);
17318 dwarf2_read_section (objfile, str_offsets_section);
17319 if (str_section->buffer == NULL)
57d63ce2 17320 error (_("%s used without .debug_str.dwo section"
3019eac3 17321 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17322 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17323 if (str_offsets_section->buffer == NULL)
57d63ce2 17324 error (_("%s used without .debug_str_offsets.dwo section"
3019eac3 17325 " in CU at offset 0x%lx [in module %s]"),
c5164cbc 17326 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17327 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 17328 error (_("%s pointing outside of .debug_str_offsets.dwo"
3019eac3 17329 " section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17330 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17331 info_ptr = (str_offsets_section->buffer
3019eac3
DE
17332 + str_index * cu->header.offset_size);
17333 if (cu->header.offset_size == 4)
17334 str_offset = bfd_get_32 (abfd, info_ptr);
17335 else
17336 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 17337 if (str_offset >= str_section->size)
57d63ce2 17338 error (_("Offset from %s pointing outside of"
3019eac3 17339 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
c5164cbc 17340 form_name, (long) cu->header.offset.sect_off, objf_name);
73869dc2 17341 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
17342}
17343
3019eac3
DE
17344/* Return the length of an LEB128 number in BUF. */
17345
17346static int
17347leb128_size (const gdb_byte *buf)
17348{
17349 const gdb_byte *begin = buf;
17350 gdb_byte byte;
17351
17352 while (1)
17353 {
17354 byte = *buf++;
17355 if ((byte & 128) == 0)
17356 return buf - begin;
17357 }
17358}
17359
c906108c 17360static void
e142c38c 17361set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
17362{
17363 switch (lang)
17364 {
17365 case DW_LANG_C89:
76bee0cc 17366 case DW_LANG_C99:
0cfd832f 17367 case DW_LANG_C11:
c906108c 17368 case DW_LANG_C:
d1be3247 17369 case DW_LANG_UPC:
e142c38c 17370 cu->language = language_c;
c906108c 17371 break;
9c37b5ae 17372 case DW_LANG_Java:
c906108c 17373 case DW_LANG_C_plus_plus:
0cfd832f
MW
17374 case DW_LANG_C_plus_plus_11:
17375 case DW_LANG_C_plus_plus_14:
e142c38c 17376 cu->language = language_cplus;
c906108c 17377 break;
6aecb9c2
JB
17378 case DW_LANG_D:
17379 cu->language = language_d;
17380 break;
c906108c
SS
17381 case DW_LANG_Fortran77:
17382 case DW_LANG_Fortran90:
b21b22e0 17383 case DW_LANG_Fortran95:
f7de9aab
MW
17384 case DW_LANG_Fortran03:
17385 case DW_LANG_Fortran08:
e142c38c 17386 cu->language = language_fortran;
c906108c 17387 break;
a766d390
DE
17388 case DW_LANG_Go:
17389 cu->language = language_go;
17390 break;
c906108c 17391 case DW_LANG_Mips_Assembler:
e142c38c 17392 cu->language = language_asm;
c906108c
SS
17393 break;
17394 case DW_LANG_Ada83:
8aaf0b47 17395 case DW_LANG_Ada95:
bc5f45f8
JB
17396 cu->language = language_ada;
17397 break;
72019c9c
GM
17398 case DW_LANG_Modula2:
17399 cu->language = language_m2;
17400 break;
fe8e67fd
PM
17401 case DW_LANG_Pascal83:
17402 cu->language = language_pascal;
17403 break;
22566fbd
DJ
17404 case DW_LANG_ObjC:
17405 cu->language = language_objc;
17406 break;
c44af4eb
TT
17407 case DW_LANG_Rust:
17408 case DW_LANG_Rust_old:
17409 cu->language = language_rust;
17410 break;
c906108c
SS
17411 case DW_LANG_Cobol74:
17412 case DW_LANG_Cobol85:
c906108c 17413 default:
e142c38c 17414 cu->language = language_minimal;
c906108c
SS
17415 break;
17416 }
e142c38c 17417 cu->language_defn = language_def (cu->language);
c906108c
SS
17418}
17419
17420/* Return the named attribute or NULL if not there. */
17421
17422static struct attribute *
e142c38c 17423dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 17424{
a48e046c 17425 for (;;)
c906108c 17426 {
a48e046c
TT
17427 unsigned int i;
17428 struct attribute *spec = NULL;
17429
17430 for (i = 0; i < die->num_attrs; ++i)
17431 {
17432 if (die->attrs[i].name == name)
17433 return &die->attrs[i];
17434 if (die->attrs[i].name == DW_AT_specification
17435 || die->attrs[i].name == DW_AT_abstract_origin)
17436 spec = &die->attrs[i];
17437 }
17438
17439 if (!spec)
17440 break;
c906108c 17441
f2f0e013 17442 die = follow_die_ref (die, spec, &cu);
f2f0e013 17443 }
c5aa993b 17444
c906108c
SS
17445 return NULL;
17446}
17447
348e048f
DE
17448/* Return the named attribute or NULL if not there,
17449 but do not follow DW_AT_specification, etc.
17450 This is for use in contexts where we're reading .debug_types dies.
17451 Following DW_AT_specification, DW_AT_abstract_origin will take us
17452 back up the chain, and we want to go down. */
17453
17454static struct attribute *
45e58e77 17455dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
17456{
17457 unsigned int i;
17458
17459 for (i = 0; i < die->num_attrs; ++i)
17460 if (die->attrs[i].name == name)
17461 return &die->attrs[i];
17462
17463 return NULL;
17464}
17465
7d45c7c3
KB
17466/* Return the string associated with a string-typed attribute, or NULL if it
17467 is either not found or is of an incorrect type. */
17468
17469static const char *
17470dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17471{
17472 struct attribute *attr;
17473 const char *str = NULL;
17474
17475 attr = dwarf2_attr (die, name, cu);
17476
17477 if (attr != NULL)
17478 {
43988095
JK
17479 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17480 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
17481 str = DW_STRING (attr);
17482 else
17483 complaint (&symfile_complaints,
17484 _("string type expected for attribute %s for "
17485 "DIE at 0x%x in module %s"),
17486 dwarf_attr_name (name), die->offset.sect_off,
17487 objfile_name (cu->objfile));
17488 }
17489
17490 return str;
17491}
17492
05cf31d1
JB
17493/* Return non-zero iff the attribute NAME is defined for the given DIE,
17494 and holds a non-zero value. This function should only be used for
2dc7f7b3 17495 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
17496
17497static int
17498dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17499{
17500 struct attribute *attr = dwarf2_attr (die, name, cu);
17501
17502 return (attr && DW_UNSND (attr));
17503}
17504
3ca72b44 17505static int
e142c38c 17506die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 17507{
05cf31d1
JB
17508 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17509 which value is non-zero. However, we have to be careful with
17510 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17511 (via dwarf2_flag_true_p) follows this attribute. So we may
17512 end up accidently finding a declaration attribute that belongs
17513 to a different DIE referenced by the specification attribute,
17514 even though the given DIE does not have a declaration attribute. */
17515 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17516 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
17517}
17518
63d06c5c 17519/* Return the die giving the specification for DIE, if there is
f2f0e013 17520 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
17521 containing the return value on output. If there is no
17522 specification, but there is an abstract origin, that is
17523 returned. */
63d06c5c
DC
17524
17525static struct die_info *
f2f0e013 17526die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 17527{
f2f0e013
DJ
17528 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17529 *spec_cu);
63d06c5c 17530
edb3359d
DJ
17531 if (spec_attr == NULL)
17532 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17533
63d06c5c
DC
17534 if (spec_attr == NULL)
17535 return NULL;
17536 else
f2f0e013 17537 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 17538}
c906108c 17539
debd256d 17540/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
17541 refers to.
17542 NOTE: This is also used as a "cleanup" function. */
17543
debd256d
JB
17544static void
17545free_line_header (struct line_header *lh)
17546{
17547 if (lh->standard_opcode_lengths)
a8bc7b56 17548 xfree (lh->standard_opcode_lengths);
debd256d
JB
17549
17550 /* Remember that all the lh->file_names[i].name pointers are
17551 pointers into debug_line_buffer, and don't need to be freed. */
17552 if (lh->file_names)
a8bc7b56 17553 xfree (lh->file_names);
debd256d
JB
17554
17555 /* Similarly for the include directory names. */
17556 if (lh->include_dirs)
a8bc7b56 17557 xfree (lh->include_dirs);
debd256d 17558
a8bc7b56 17559 xfree (lh);
debd256d
JB
17560}
17561
527f3840
JK
17562/* Stub for free_line_header to match void * callback types. */
17563
17564static void
17565free_line_header_voidp (void *arg)
17566{
9a3c8263 17567 struct line_header *lh = (struct line_header *) arg;
527f3840
JK
17568
17569 free_line_header (lh);
17570}
17571
debd256d 17572/* Add an entry to LH's include directory table. */
ae2de4f8 17573
debd256d 17574static void
d521ce57 17575add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 17576{
27e0867f
DE
17577 if (dwarf_line_debug >= 2)
17578 fprintf_unfiltered (gdb_stdlog, "Adding dir %u: %s\n",
17579 lh->num_include_dirs + 1, include_dir);
17580
debd256d
JB
17581 /* Grow the array if necessary. */
17582 if (lh->include_dirs_size == 0)
c5aa993b 17583 {
debd256d 17584 lh->include_dirs_size = 1; /* for testing */
8d749320 17585 lh->include_dirs = XNEWVEC (const char *, lh->include_dirs_size);
debd256d
JB
17586 }
17587 else if (lh->num_include_dirs >= lh->include_dirs_size)
17588 {
17589 lh->include_dirs_size *= 2;
8d749320
SM
17590 lh->include_dirs = XRESIZEVEC (const char *, lh->include_dirs,
17591 lh->include_dirs_size);
c5aa993b 17592 }
c906108c 17593
debd256d
JB
17594 lh->include_dirs[lh->num_include_dirs++] = include_dir;
17595}
6e70227d 17596
debd256d 17597/* Add an entry to LH's file name table. */
ae2de4f8 17598
debd256d
JB
17599static void
17600add_file_name (struct line_header *lh,
d521ce57 17601 const char *name,
debd256d
JB
17602 unsigned int dir_index,
17603 unsigned int mod_time,
17604 unsigned int length)
17605{
17606 struct file_entry *fe;
17607
27e0867f
DE
17608 if (dwarf_line_debug >= 2)
17609 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17610 lh->num_file_names + 1, name);
17611
debd256d
JB
17612 /* Grow the array if necessary. */
17613 if (lh->file_names_size == 0)
17614 {
17615 lh->file_names_size = 1; /* for testing */
8d749320 17616 lh->file_names = XNEWVEC (struct file_entry, lh->file_names_size);
debd256d
JB
17617 }
17618 else if (lh->num_file_names >= lh->file_names_size)
17619 {
17620 lh->file_names_size *= 2;
224c3ddb
SM
17621 lh->file_names
17622 = XRESIZEVEC (struct file_entry, lh->file_names, lh->file_names_size);
debd256d
JB
17623 }
17624
17625 fe = &lh->file_names[lh->num_file_names++];
17626 fe->name = name;
17627 fe->dir_index = dir_index;
17628 fe->mod_time = mod_time;
17629 fe->length = length;
aaa75496 17630 fe->included_p = 0;
cb1df416 17631 fe->symtab = NULL;
debd256d 17632}
6e70227d 17633
83769d0b 17634/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
17635
17636static struct dwarf2_section_info *
17637get_debug_line_section (struct dwarf2_cu *cu)
17638{
17639 struct dwarf2_section_info *section;
17640
17641 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17642 DWO file. */
17643 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17644 section = &cu->dwo_unit->dwo_file->sections.line;
17645 else if (cu->per_cu->is_dwz)
17646 {
17647 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17648
17649 section = &dwz->line;
17650 }
17651 else
17652 section = &dwarf2_per_objfile->line;
17653
17654 return section;
17655}
17656
43988095
JK
17657/* Forwarding function for read_formatted_entries. */
17658
17659static void
17660add_include_dir_stub (struct line_header *lh, const char *name,
17661 unsigned int dir_index, unsigned int mod_time,
17662 unsigned int length)
17663{
17664 add_include_dir (lh, name);
17665}
17666
17667/* Read directory or file name entry format, starting with byte of
17668 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17669 entries count and the entries themselves in the described entry
17670 format. */
17671
17672static void
17673read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17674 struct line_header *lh,
17675 const struct comp_unit_head *cu_header,
17676 void (*callback) (struct line_header *lh,
17677 const char *name,
17678 unsigned int dir_index,
17679 unsigned int mod_time,
17680 unsigned int length))
17681{
17682 gdb_byte format_count, formati;
17683 ULONGEST data_count, datai;
17684 const gdb_byte *buf = *bufp;
17685 const gdb_byte *format_header_data;
17686 int i;
17687 unsigned int bytes_read;
17688
17689 format_count = read_1_byte (abfd, buf);
17690 buf += 1;
17691 format_header_data = buf;
17692 for (formati = 0; formati < format_count; formati++)
17693 {
17694 read_unsigned_leb128 (abfd, buf, &bytes_read);
17695 buf += bytes_read;
17696 read_unsigned_leb128 (abfd, buf, &bytes_read);
17697 buf += bytes_read;
17698 }
17699
17700 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17701 buf += bytes_read;
17702 for (datai = 0; datai < data_count; datai++)
17703 {
17704 const gdb_byte *format = format_header_data;
17705 struct file_entry fe;
17706
17707 memset (&fe, 0, sizeof (fe));
17708
17709 for (formati = 0; formati < format_count; formati++)
17710 {
17711 ULONGEST content_type, form;
17712 const char *string_trash;
17713 const char **stringp = &string_trash;
17714 unsigned int uint_trash, *uintp = &uint_trash;
17715
17716 content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17717 format += bytes_read;
17718 switch (content_type)
17719 {
17720 case DW_LNCT_path:
17721 stringp = &fe.name;
17722 break;
17723 case DW_LNCT_directory_index:
17724 uintp = &fe.dir_index;
17725 break;
17726 case DW_LNCT_timestamp:
17727 uintp = &fe.mod_time;
17728 break;
17729 case DW_LNCT_size:
17730 uintp = &fe.length;
17731 break;
17732 case DW_LNCT_MD5:
17733 break;
17734 default:
17735 complaint (&symfile_complaints,
17736 _("Unknown format content type %s"),
17737 pulongest (content_type));
17738 }
17739
17740 form = read_unsigned_leb128 (abfd, format, &bytes_read);
17741 format += bytes_read;
17742 switch (form)
17743 {
17744 case DW_FORM_string:
17745 *stringp = read_direct_string (abfd, buf, &bytes_read);
17746 buf += bytes_read;
17747 break;
17748
17749 case DW_FORM_line_strp:
17750 *stringp = read_indirect_line_string (abfd, buf, cu_header, &bytes_read);
17751 buf += bytes_read;
17752 break;
17753
17754 case DW_FORM_data1:
17755 *uintp = read_1_byte (abfd, buf);
17756 buf += 1;
17757 break;
17758
17759 case DW_FORM_data2:
17760 *uintp = read_2_bytes (abfd, buf);
17761 buf += 2;
17762 break;
17763
17764 case DW_FORM_data4:
17765 *uintp = read_4_bytes (abfd, buf);
17766 buf += 4;
17767 break;
17768
17769 case DW_FORM_data8:
17770 *uintp = read_8_bytes (abfd, buf);
17771 buf += 8;
17772 break;
17773
17774 case DW_FORM_udata:
17775 *uintp = read_unsigned_leb128 (abfd, buf, &bytes_read);
17776 buf += bytes_read;
17777 break;
17778
17779 case DW_FORM_block:
17780 /* It is valid only for DW_LNCT_timestamp which is ignored by
17781 current GDB. */
17782 break;
17783 }
17784 }
17785
17786 callback (lh, fe.name, fe.dir_index, fe.mod_time, fe.length);
17787 }
17788
17789 *bufp = buf;
17790}
17791
debd256d 17792/* Read the statement program header starting at OFFSET in
3019eac3 17793 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 17794 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
17795 Returns NULL if there is a problem reading the header, e.g., if it
17796 has a version we don't understand.
debd256d
JB
17797
17798 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
17799 the returned object point into the dwarf line section buffer,
17800 and must not be freed. */
ae2de4f8 17801
debd256d 17802static struct line_header *
3019eac3 17803dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
17804{
17805 struct cleanup *back_to;
17806 struct line_header *lh;
d521ce57 17807 const gdb_byte *line_ptr;
c764a876 17808 unsigned int bytes_read, offset_size;
debd256d 17809 int i;
d521ce57 17810 const char *cur_dir, *cur_file;
3019eac3
DE
17811 struct dwarf2_section_info *section;
17812 bfd *abfd;
17813
36586728 17814 section = get_debug_line_section (cu);
3019eac3
DE
17815 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17816 if (section->buffer == NULL)
debd256d 17817 {
3019eac3
DE
17818 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17819 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17820 else
17821 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
17822 return 0;
17823 }
17824
fceca515
DE
17825 /* We can't do this until we know the section is non-empty.
17826 Only then do we know we have such a section. */
a32a8923 17827 abfd = get_section_bfd_owner (section);
fceca515 17828
a738430d
MK
17829 /* Make sure that at least there's room for the total_length field.
17830 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 17831 if (offset + 4 >= section->size)
debd256d 17832 {
4d3c2250 17833 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
17834 return 0;
17835 }
17836
8d749320 17837 lh = XNEW (struct line_header);
debd256d
JB
17838 memset (lh, 0, sizeof (*lh));
17839 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
17840 (void *) lh);
17841
527f3840
JK
17842 lh->offset.sect_off = offset;
17843 lh->offset_in_dwz = cu->per_cu->is_dwz;
17844
3019eac3 17845 line_ptr = section->buffer + offset;
debd256d 17846
a738430d 17847 /* Read in the header. */
6e70227d 17848 lh->total_length =
c764a876
DE
17849 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17850 &bytes_read, &offset_size);
debd256d 17851 line_ptr += bytes_read;
3019eac3 17852 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 17853 {
4d3c2250 17854 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 17855 do_cleanups (back_to);
debd256d
JB
17856 return 0;
17857 }
17858 lh->statement_program_end = line_ptr + lh->total_length;
17859 lh->version = read_2_bytes (abfd, line_ptr);
17860 line_ptr += 2;
43988095 17861 if (lh->version > 5)
cd366ee8
DE
17862 {
17863 /* This is a version we don't understand. The format could have
17864 changed in ways we don't handle properly so just punt. */
17865 complaint (&symfile_complaints,
17866 _("unsupported version in .debug_line section"));
17867 return NULL;
17868 }
43988095
JK
17869 if (lh->version >= 5)
17870 {
17871 gdb_byte segment_selector_size;
17872
17873 /* Skip address size. */
17874 read_1_byte (abfd, line_ptr);
17875 line_ptr += 1;
17876
17877 segment_selector_size = read_1_byte (abfd, line_ptr);
17878 line_ptr += 1;
17879 if (segment_selector_size != 0)
17880 {
17881 complaint (&symfile_complaints,
17882 _("unsupported segment selector size %u "
17883 "in .debug_line section"),
17884 segment_selector_size);
17885 return NULL;
17886 }
17887 }
c764a876
DE
17888 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17889 line_ptr += offset_size;
debd256d
JB
17890 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17891 line_ptr += 1;
2dc7f7b3
TT
17892 if (lh->version >= 4)
17893 {
17894 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17895 line_ptr += 1;
17896 }
17897 else
17898 lh->maximum_ops_per_instruction = 1;
17899
17900 if (lh->maximum_ops_per_instruction == 0)
17901 {
17902 lh->maximum_ops_per_instruction = 1;
17903 complaint (&symfile_complaints,
3e43a32a
MS
17904 _("invalid maximum_ops_per_instruction "
17905 "in `.debug_line' section"));
2dc7f7b3
TT
17906 }
17907
debd256d
JB
17908 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17909 line_ptr += 1;
17910 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17911 line_ptr += 1;
17912 lh->line_range = read_1_byte (abfd, line_ptr);
17913 line_ptr += 1;
17914 lh->opcode_base = read_1_byte (abfd, line_ptr);
17915 line_ptr += 1;
8d749320 17916 lh->standard_opcode_lengths = XNEWVEC (unsigned char, lh->opcode_base);
debd256d
JB
17917
17918 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17919 for (i = 1; i < lh->opcode_base; ++i)
17920 {
17921 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17922 line_ptr += 1;
17923 }
17924
43988095 17925 if (lh->version >= 5)
debd256d 17926 {
43988095
JK
17927 /* Read directory table. */
17928 read_formatted_entries (abfd, &line_ptr, lh, &cu->header,
17929 add_include_dir_stub);
debd256d 17930
43988095
JK
17931 /* Read file name table. */
17932 read_formatted_entries (abfd, &line_ptr, lh, &cu->header, add_file_name);
17933 }
17934 else
debd256d 17935 {
43988095
JK
17936 /* Read directory table. */
17937 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17938 {
17939 line_ptr += bytes_read;
17940 add_include_dir (lh, cur_dir);
17941 }
debd256d
JB
17942 line_ptr += bytes_read;
17943
43988095
JK
17944 /* Read file name table. */
17945 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17946 {
17947 unsigned int dir_index, mod_time, length;
17948
17949 line_ptr += bytes_read;
17950 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17951 line_ptr += bytes_read;
17952 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17953 line_ptr += bytes_read;
17954 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17955 line_ptr += bytes_read;
17956
17957 add_file_name (lh, cur_file, dir_index, mod_time, length);
17958 }
17959 line_ptr += bytes_read;
debd256d 17960 }
6e70227d 17961 lh->statement_program_start = line_ptr;
debd256d 17962
3019eac3 17963 if (line_ptr > (section->buffer + section->size))
4d3c2250 17964 complaint (&symfile_complaints,
3e43a32a
MS
17965 _("line number info header doesn't "
17966 "fit in `.debug_line' section"));
debd256d
JB
17967
17968 discard_cleanups (back_to);
17969 return lh;
17970}
c906108c 17971
c6da4cef
DE
17972/* Subroutine of dwarf_decode_lines to simplify it.
17973 Return the file name of the psymtab for included file FILE_INDEX
17974 in line header LH of PST.
17975 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17976 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
17977 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17978
17979 The function creates dangling cleanup registration. */
c6da4cef 17980
d521ce57 17981static const char *
c6da4cef
DE
17982psymtab_include_file_name (const struct line_header *lh, int file_index,
17983 const struct partial_symtab *pst,
17984 const char *comp_dir)
17985{
17986 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
17987 const char *include_name = fe.name;
17988 const char *include_name_to_compare = include_name;
17989 const char *dir_name = NULL;
72b9f47f
TT
17990 const char *pst_filename;
17991 char *copied_name = NULL;
c6da4cef
DE
17992 int file_is_pst;
17993
568c1b9f
PB
17994 if (fe.dir_index && lh->include_dirs != NULL
17995 && (fe.dir_index - 1) < lh->num_include_dirs)
c6da4cef
DE
17996 dir_name = lh->include_dirs[fe.dir_index - 1];
17997
17998 if (!IS_ABSOLUTE_PATH (include_name)
17999 && (dir_name != NULL || comp_dir != NULL))
18000 {
18001 /* Avoid creating a duplicate psymtab for PST.
18002 We do this by comparing INCLUDE_NAME and PST_FILENAME.
18003 Before we do the comparison, however, we need to account
18004 for DIR_NAME and COMP_DIR.
18005 First prepend dir_name (if non-NULL). If we still don't
18006 have an absolute path prepend comp_dir (if non-NULL).
18007 However, the directory we record in the include-file's
18008 psymtab does not contain COMP_DIR (to match the
18009 corresponding symtab(s)).
18010
18011 Example:
18012
18013 bash$ cd /tmp
18014 bash$ gcc -g ./hello.c
18015 include_name = "hello.c"
18016 dir_name = "."
18017 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
18018 DW_AT_name = "./hello.c"
18019
18020 */
c6da4cef
DE
18021
18022 if (dir_name != NULL)
18023 {
d521ce57
TT
18024 char *tem = concat (dir_name, SLASH_STRING,
18025 include_name, (char *)NULL);
18026
18027 make_cleanup (xfree, tem);
18028 include_name = tem;
c6da4cef 18029 include_name_to_compare = include_name;
c6da4cef
DE
18030 }
18031 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18032 {
d521ce57
TT
18033 char *tem = concat (comp_dir, SLASH_STRING,
18034 include_name, (char *)NULL);
18035
18036 make_cleanup (xfree, tem);
18037 include_name_to_compare = tem;
c6da4cef
DE
18038 }
18039 }
18040
18041 pst_filename = pst->filename;
18042 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18043 {
72b9f47f
TT
18044 copied_name = concat (pst->dirname, SLASH_STRING,
18045 pst_filename, (char *)NULL);
18046 pst_filename = copied_name;
c6da4cef
DE
18047 }
18048
1e3fad37 18049 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 18050
72b9f47f
TT
18051 if (copied_name != NULL)
18052 xfree (copied_name);
c6da4cef
DE
18053
18054 if (file_is_pst)
18055 return NULL;
18056 return include_name;
18057}
18058
d9b3de22
DE
18059/* State machine to track the state of the line number program. */
18060
18061typedef struct
18062{
18063 /* These are part of the standard DWARF line number state machine. */
18064
18065 unsigned char op_index;
18066 unsigned int file;
18067 unsigned int line;
18068 CORE_ADDR address;
18069 int is_stmt;
18070 unsigned int discriminator;
18071
18072 /* Additional bits of state we need to track. */
18073
18074 /* The last file that we called dwarf2_start_subfile for.
18075 This is only used for TLLs. */
18076 unsigned int last_file;
18077 /* The last file a line number was recorded for. */
18078 struct subfile *last_subfile;
18079
18080 /* The function to call to record a line. */
18081 record_line_ftype *record_line;
18082
18083 /* The last line number that was recorded, used to coalesce
18084 consecutive entries for the same line. This can happen, for
18085 example, when discriminators are present. PR 17276. */
18086 unsigned int last_line;
18087 int line_has_non_zero_discriminator;
18088} lnp_state_machine;
18089
18090/* There's a lot of static state to pass to dwarf_record_line.
18091 This keeps it all together. */
18092
18093typedef struct
18094{
18095 /* The gdbarch. */
18096 struct gdbarch *gdbarch;
18097
18098 /* The line number header. */
18099 struct line_header *line_header;
18100
18101 /* Non-zero if we're recording lines.
18102 Otherwise we're building partial symtabs and are just interested in
18103 finding include files mentioned by the line number program. */
18104 int record_lines_p;
18105} lnp_reader_state;
18106
c91513d8
PP
18107/* Ignore this record_line request. */
18108
18109static void
18110noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18111{
18112 return;
18113}
18114
a05a36a5
DE
18115/* Return non-zero if we should add LINE to the line number table.
18116 LINE is the line to add, LAST_LINE is the last line that was added,
18117 LAST_SUBFILE is the subfile for LAST_LINE.
18118 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18119 had a non-zero discriminator.
18120
18121 We have to be careful in the presence of discriminators.
18122 E.g., for this line:
18123
18124 for (i = 0; i < 100000; i++);
18125
18126 clang can emit four line number entries for that one line,
18127 each with a different discriminator.
18128 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18129
18130 However, we want gdb to coalesce all four entries into one.
18131 Otherwise the user could stepi into the middle of the line and
18132 gdb would get confused about whether the pc really was in the
18133 middle of the line.
18134
18135 Things are further complicated by the fact that two consecutive
18136 line number entries for the same line is a heuristic used by gcc
18137 to denote the end of the prologue. So we can't just discard duplicate
18138 entries, we have to be selective about it. The heuristic we use is
18139 that we only collapse consecutive entries for the same line if at least
18140 one of those entries has a non-zero discriminator. PR 17276.
18141
18142 Note: Addresses in the line number state machine can never go backwards
18143 within one sequence, thus this coalescing is ok. */
18144
18145static int
18146dwarf_record_line_p (unsigned int line, unsigned int last_line,
18147 int line_has_non_zero_discriminator,
18148 struct subfile *last_subfile)
18149{
18150 if (current_subfile != last_subfile)
18151 return 1;
18152 if (line != last_line)
18153 return 1;
18154 /* Same line for the same file that we've seen already.
18155 As a last check, for pr 17276, only record the line if the line
18156 has never had a non-zero discriminator. */
18157 if (!line_has_non_zero_discriminator)
18158 return 1;
18159 return 0;
18160}
18161
252a6764
DE
18162/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18163 in the line table of subfile SUBFILE. */
18164
18165static void
d9b3de22
DE
18166dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18167 unsigned int line, CORE_ADDR address,
18168 record_line_ftype p_record_line)
252a6764
DE
18169{
18170 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18171
27e0867f
DE
18172 if (dwarf_line_debug)
18173 {
18174 fprintf_unfiltered (gdb_stdlog,
18175 "Recording line %u, file %s, address %s\n",
18176 line, lbasename (subfile->name),
18177 paddress (gdbarch, address));
18178 }
18179
d5962de5 18180 (*p_record_line) (subfile, line, addr);
252a6764
DE
18181}
18182
18183/* Subroutine of dwarf_decode_lines_1 to simplify it.
18184 Mark the end of a set of line number records.
d9b3de22 18185 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
18186 If SUBFILE is NULL the request is ignored. */
18187
18188static void
18189dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18190 CORE_ADDR address, record_line_ftype p_record_line)
18191{
27e0867f
DE
18192 if (subfile == NULL)
18193 return;
18194
18195 if (dwarf_line_debug)
18196 {
18197 fprintf_unfiltered (gdb_stdlog,
18198 "Finishing current line, file %s, address %s\n",
18199 lbasename (subfile->name),
18200 paddress (gdbarch, address));
18201 }
18202
d9b3de22
DE
18203 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18204}
18205
18206/* Record the line in STATE.
18207 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18208
18209static void
18210dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18211 int end_sequence)
18212{
18213 const struct line_header *lh = reader->line_header;
18214 unsigned int file, line, discriminator;
18215 int is_stmt;
18216
18217 file = state->file;
18218 line = state->line;
18219 is_stmt = state->is_stmt;
18220 discriminator = state->discriminator;
18221
18222 if (dwarf_line_debug)
18223 {
18224 fprintf_unfiltered (gdb_stdlog,
18225 "Processing actual line %u: file %u,"
18226 " address %s, is_stmt %u, discrim %u\n",
18227 line, file,
18228 paddress (reader->gdbarch, state->address),
18229 is_stmt, discriminator);
18230 }
18231
18232 if (file == 0 || file - 1 >= lh->num_file_names)
18233 dwarf2_debug_line_missing_file_complaint ();
18234 /* For now we ignore lines not starting on an instruction boundary.
18235 But not when processing end_sequence for compatibility with the
18236 previous version of the code. */
18237 else if (state->op_index == 0 || end_sequence)
18238 {
18239 lh->file_names[file - 1].included_p = 1;
18240 if (reader->record_lines_p && is_stmt)
18241 {
e815d2d2 18242 if (state->last_subfile != current_subfile || end_sequence)
d9b3de22
DE
18243 {
18244 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18245 state->address, state->record_line);
18246 }
18247
18248 if (!end_sequence)
18249 {
18250 if (dwarf_record_line_p (line, state->last_line,
18251 state->line_has_non_zero_discriminator,
18252 state->last_subfile))
18253 {
18254 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18255 line, state->address,
18256 state->record_line);
18257 }
18258 state->last_subfile = current_subfile;
18259 state->last_line = line;
18260 }
18261 }
18262 }
18263}
18264
18265/* Initialize STATE for the start of a line number program. */
18266
18267static void
18268init_lnp_state_machine (lnp_state_machine *state,
18269 const lnp_reader_state *reader)
18270{
18271 memset (state, 0, sizeof (*state));
18272
18273 /* Just starting, there is no "last file". */
18274 state->last_file = 0;
18275 state->last_subfile = NULL;
18276
18277 state->record_line = record_line;
18278
18279 state->last_line = 0;
18280 state->line_has_non_zero_discriminator = 0;
18281
18282 /* Initialize these according to the DWARF spec. */
18283 state->op_index = 0;
18284 state->file = 1;
18285 state->line = 1;
18286 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18287 was a line entry for it so that the backend has a chance to adjust it
18288 and also record it in case it needs it. This is currently used by MIPS
18289 code, cf. `mips_adjust_dwarf2_line'. */
18290 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18291 state->is_stmt = reader->line_header->default_is_stmt;
18292 state->discriminator = 0;
252a6764
DE
18293}
18294
924c2928
DE
18295/* Check address and if invalid nop-out the rest of the lines in this
18296 sequence. */
18297
18298static void
d9b3de22 18299check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
924c2928
DE
18300 const gdb_byte *line_ptr,
18301 CORE_ADDR lowpc, CORE_ADDR address)
18302{
18303 /* If address < lowpc then it's not a usable value, it's outside the
18304 pc range of the CU. However, we restrict the test to only address
18305 values of zero to preserve GDB's previous behaviour which is to
18306 handle the specific case of a function being GC'd by the linker. */
18307
18308 if (address == 0 && address < lowpc)
18309 {
18310 /* This line table is for a function which has been
18311 GCd by the linker. Ignore it. PR gdb/12528 */
18312
18313 struct objfile *objfile = cu->objfile;
18314 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18315
18316 complaint (&symfile_complaints,
18317 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18318 line_offset, objfile_name (objfile));
d9b3de22
DE
18319 state->record_line = noop_record_line;
18320 /* Note: sm.record_line is left as noop_record_line
924c2928
DE
18321 until we see DW_LNE_end_sequence. */
18322 }
18323}
18324
f3f5162e 18325/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
18326 Process the line number information in LH.
18327 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18328 program in order to set included_p for every referenced header. */
debd256d 18329
c906108c 18330static void
43f3e411
DE
18331dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18332 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 18333{
d521ce57
TT
18334 const gdb_byte *line_ptr, *extended_end;
18335 const gdb_byte *line_end;
a8c50c1f 18336 unsigned int bytes_read, extended_len;
699ca60a 18337 unsigned char op_code, extended_op;
e142c38c
DJ
18338 CORE_ADDR baseaddr;
18339 struct objfile *objfile = cu->objfile;
f3f5162e 18340 bfd *abfd = objfile->obfd;
fbf65064 18341 struct gdbarch *gdbarch = get_objfile_arch (objfile);
d9b3de22
DE
18342 /* Non-zero if we're recording line info (as opposed to building partial
18343 symtabs). */
18344 int record_lines_p = !decode_for_pst_p;
18345 /* A collection of things we need to pass to dwarf_record_line. */
18346 lnp_reader_state reader_state;
e142c38c
DJ
18347
18348 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18349
debd256d
JB
18350 line_ptr = lh->statement_program_start;
18351 line_end = lh->statement_program_end;
c906108c 18352
d9b3de22
DE
18353 reader_state.gdbarch = gdbarch;
18354 reader_state.line_header = lh;
18355 reader_state.record_lines_p = record_lines_p;
18356
c906108c
SS
18357 /* Read the statement sequences until there's nothing left. */
18358 while (line_ptr < line_end)
18359 {
d9b3de22
DE
18360 /* The DWARF line number program state machine. */
18361 lnp_state_machine state_machine;
c906108c 18362 int end_sequence = 0;
d9b3de22
DE
18363
18364 /* Reset the state machine at the start of each sequence. */
18365 init_lnp_state_machine (&state_machine, &reader_state);
18366
18367 if (record_lines_p && lh->num_file_names >= state_machine.file)
c906108c 18368 {
aaa75496 18369 /* Start a subfile for the current file of the state machine. */
debd256d
JB
18370 /* lh->include_dirs and lh->file_names are 0-based, but the
18371 directory and file name numbers in the statement program
18372 are 1-based. */
d9b3de22 18373 struct file_entry *fe = &lh->file_names[state_machine.file - 1];
d521ce57 18374 const char *dir = NULL;
a738430d 18375
568c1b9f
PB
18376 if (fe->dir_index && lh->include_dirs != NULL
18377 && (fe->dir_index - 1) < lh->num_include_dirs)
debd256d 18378 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb 18379
4d663531 18380 dwarf2_start_subfile (fe->name, dir);
c906108c
SS
18381 }
18382
a738430d 18383 /* Decode the table. */
d9b3de22 18384 while (line_ptr < line_end && !end_sequence)
c906108c
SS
18385 {
18386 op_code = read_1_byte (abfd, line_ptr);
18387 line_ptr += 1;
9aa1fe7e 18388
debd256d 18389 if (op_code >= lh->opcode_base)
6e70227d 18390 {
8e07a239 18391 /* Special opcode. */
699ca60a 18392 unsigned char adj_opcode;
3e29f34a 18393 CORE_ADDR addr_adj;
a05a36a5 18394 int line_delta;
8e07a239 18395
debd256d 18396 adj_opcode = op_code - lh->opcode_base;
d9b3de22
DE
18397 addr_adj = (((state_machine.op_index
18398 + (adj_opcode / lh->line_range))
2dc7f7b3
TT
18399 / lh->maximum_ops_per_instruction)
18400 * lh->minimum_instruction_length);
d9b3de22
DE
18401 state_machine.address
18402 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18403 state_machine.op_index = ((state_machine.op_index
18404 + (adj_opcode / lh->line_range))
18405 % lh->maximum_ops_per_instruction);
a05a36a5 18406 line_delta = lh->line_base + (adj_opcode % lh->line_range);
d9b3de22 18407 state_machine.line += line_delta;
a05a36a5 18408 if (line_delta != 0)
d9b3de22
DE
18409 state_machine.line_has_non_zero_discriminator
18410 = state_machine.discriminator != 0;
18411
18412 dwarf_record_line (&reader_state, &state_machine, 0);
18413 state_machine.discriminator = 0;
9aa1fe7e
GK
18414 }
18415 else switch (op_code)
c906108c
SS
18416 {
18417 case DW_LNS_extended_op:
3e43a32a
MS
18418 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18419 &bytes_read);
473b7be6 18420 line_ptr += bytes_read;
a8c50c1f 18421 extended_end = line_ptr + extended_len;
c906108c
SS
18422 extended_op = read_1_byte (abfd, line_ptr);
18423 line_ptr += 1;
18424 switch (extended_op)
18425 {
18426 case DW_LNE_end_sequence:
d9b3de22 18427 state_machine.record_line = record_line;
c906108c 18428 end_sequence = 1;
c906108c
SS
18429 break;
18430 case DW_LNE_set_address:
d9b3de22
DE
18431 {
18432 CORE_ADDR address
18433 = read_address (abfd, line_ptr, cu, &bytes_read);
18434
18435 line_ptr += bytes_read;
18436 check_line_address (cu, &state_machine, line_ptr,
18437 lowpc, address);
18438 state_machine.op_index = 0;
18439 address += baseaddr;
18440 state_machine.address
18441 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18442 }
c906108c
SS
18443 break;
18444 case DW_LNE_define_file:
debd256d 18445 {
d521ce57 18446 const char *cur_file;
debd256d 18447 unsigned int dir_index, mod_time, length;
6e70227d 18448
3e43a32a
MS
18449 cur_file = read_direct_string (abfd, line_ptr,
18450 &bytes_read);
debd256d
JB
18451 line_ptr += bytes_read;
18452 dir_index =
18453 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18454 line_ptr += bytes_read;
18455 mod_time =
18456 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18457 line_ptr += bytes_read;
18458 length =
18459 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18460 line_ptr += bytes_read;
18461 add_file_name (lh, cur_file, dir_index, mod_time, length);
18462 }
c906108c 18463 break;
d0c6ba3d
CC
18464 case DW_LNE_set_discriminator:
18465 /* The discriminator is not interesting to the debugger;
a05a36a5
DE
18466 just ignore it. We still need to check its value though:
18467 if there are consecutive entries for the same
18468 (non-prologue) line we want to coalesce them.
18469 PR 17276. */
d9b3de22
DE
18470 state_machine.discriminator
18471 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18472 state_machine.line_has_non_zero_discriminator
18473 |= state_machine.discriminator != 0;
a05a36a5 18474 line_ptr += bytes_read;
d0c6ba3d 18475 break;
c906108c 18476 default:
4d3c2250 18477 complaint (&symfile_complaints,
e2e0b3e5 18478 _("mangled .debug_line section"));
debd256d 18479 return;
c906108c 18480 }
a8c50c1f
DJ
18481 /* Make sure that we parsed the extended op correctly. If e.g.
18482 we expected a different address size than the producer used,
18483 we may have read the wrong number of bytes. */
18484 if (line_ptr != extended_end)
18485 {
18486 complaint (&symfile_complaints,
18487 _("mangled .debug_line section"));
18488 return;
18489 }
c906108c
SS
18490 break;
18491 case DW_LNS_copy:
d9b3de22
DE
18492 dwarf_record_line (&reader_state, &state_machine, 0);
18493 state_machine.discriminator = 0;
c906108c
SS
18494 break;
18495 case DW_LNS_advance_pc:
2dc7f7b3
TT
18496 {
18497 CORE_ADDR adjust
18498 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3e29f34a 18499 CORE_ADDR addr_adj;
2dc7f7b3 18500
d9b3de22 18501 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18502 / lh->maximum_ops_per_instruction)
18503 * lh->minimum_instruction_length);
d9b3de22
DE
18504 state_machine.address
18505 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18506 state_machine.op_index = ((state_machine.op_index + adjust)
18507 % lh->maximum_ops_per_instruction);
2dc7f7b3
TT
18508 line_ptr += bytes_read;
18509 }
c906108c
SS
18510 break;
18511 case DW_LNS_advance_line:
a05a36a5
DE
18512 {
18513 int line_delta
18514 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18515
d9b3de22 18516 state_machine.line += line_delta;
a05a36a5 18517 if (line_delta != 0)
d9b3de22
DE
18518 state_machine.line_has_non_zero_discriminator
18519 = state_machine.discriminator != 0;
a05a36a5
DE
18520 line_ptr += bytes_read;
18521 }
c906108c
SS
18522 break;
18523 case DW_LNS_set_file:
d9b3de22
DE
18524 {
18525 /* The arrays lh->include_dirs and lh->file_names are
18526 0-based, but the directory and file name numbers in
18527 the statement program are 1-based. */
18528 struct file_entry *fe;
18529 const char *dir = NULL;
18530
18531 state_machine.file = read_unsigned_leb128 (abfd, line_ptr,
18532 &bytes_read);
18533 line_ptr += bytes_read;
18534 if (state_machine.file == 0
18535 || state_machine.file - 1 >= lh->num_file_names)
18536 dwarf2_debug_line_missing_file_complaint ();
18537 else
18538 {
18539 fe = &lh->file_names[state_machine.file - 1];
568c1b9f
PB
18540 if (fe->dir_index && lh->include_dirs != NULL
18541 && (fe->dir_index - 1) < lh->num_include_dirs)
d9b3de22
DE
18542 dir = lh->include_dirs[fe->dir_index - 1];
18543 if (record_lines_p)
18544 {
18545 state_machine.last_subfile = current_subfile;
18546 state_machine.line_has_non_zero_discriminator
18547 = state_machine.discriminator != 0;
18548 dwarf2_start_subfile (fe->name, dir);
18549 }
18550 }
18551 }
c906108c
SS
18552 break;
18553 case DW_LNS_set_column:
0ad93d4f 18554 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
18555 line_ptr += bytes_read;
18556 break;
18557 case DW_LNS_negate_stmt:
d9b3de22 18558 state_machine.is_stmt = (!state_machine.is_stmt);
c906108c
SS
18559 break;
18560 case DW_LNS_set_basic_block:
c906108c 18561 break;
c2c6d25f
JM
18562 /* Add to the address register of the state machine the
18563 address increment value corresponding to special opcode
a738430d
MK
18564 255. I.e., this value is scaled by the minimum
18565 instruction length since special opcode 255 would have
b021a221 18566 scaled the increment. */
c906108c 18567 case DW_LNS_const_add_pc:
2dc7f7b3
TT
18568 {
18569 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
3e29f34a 18570 CORE_ADDR addr_adj;
2dc7f7b3 18571
d9b3de22 18572 addr_adj = (((state_machine.op_index + adjust)
2dc7f7b3
TT
18573 / lh->maximum_ops_per_instruction)
18574 * lh->minimum_instruction_length);
d9b3de22
DE
18575 state_machine.address
18576 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18577 state_machine.op_index = ((state_machine.op_index + adjust)
18578 % lh->maximum_ops_per_instruction);
2dc7f7b3 18579 }
c906108c
SS
18580 break;
18581 case DW_LNS_fixed_advance_pc:
3e29f34a
MR
18582 {
18583 CORE_ADDR addr_adj;
18584
18585 addr_adj = read_2_bytes (abfd, line_ptr);
d9b3de22
DE
18586 state_machine.address
18587 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18588 state_machine.op_index = 0;
3e29f34a
MR
18589 line_ptr += 2;
18590 }
c906108c 18591 break;
9aa1fe7e 18592 default:
a738430d
MK
18593 {
18594 /* Unknown standard opcode, ignore it. */
9aa1fe7e 18595 int i;
a738430d 18596
debd256d 18597 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
18598 {
18599 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18600 line_ptr += bytes_read;
18601 }
18602 }
c906108c
SS
18603 }
18604 }
d9b3de22
DE
18605
18606 if (!end_sequence)
18607 dwarf2_debug_line_missing_end_sequence_complaint ();
18608
18609 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18610 in which case we still finish recording the last line). */
18611 dwarf_record_line (&reader_state, &state_machine, 1);
c906108c 18612 }
f3f5162e
DE
18613}
18614
18615/* Decode the Line Number Program (LNP) for the given line_header
18616 structure and CU. The actual information extracted and the type
18617 of structures created from the LNP depends on the value of PST.
18618
18619 1. If PST is NULL, then this procedure uses the data from the program
18620 to create all necessary symbol tables, and their linetables.
18621
18622 2. If PST is not NULL, this procedure reads the program to determine
18623 the list of files included by the unit represented by PST, and
18624 builds all the associated partial symbol tables.
18625
18626 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18627 It is used for relative paths in the line table.
18628 NOTE: When processing partial symtabs (pst != NULL),
18629 comp_dir == pst->dirname.
18630
18631 NOTE: It is important that psymtabs have the same file name (via strcmp)
18632 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18633 symtab we don't use it in the name of the psymtabs we create.
18634 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
18635 A good testcase for this is mb-inline.exp.
18636
527f3840
JK
18637 LOWPC is the lowest address in CU (or 0 if not known).
18638
18639 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18640 for its PC<->lines mapping information. Otherwise only the filename
18641 table is read in. */
f3f5162e
DE
18642
18643static void
18644dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 18645 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 18646 CORE_ADDR lowpc, int decode_mapping)
f3f5162e
DE
18647{
18648 struct objfile *objfile = cu->objfile;
18649 const int decode_for_pst_p = (pst != NULL);
f3f5162e 18650
527f3840
JK
18651 if (decode_mapping)
18652 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
18653
18654 if (decode_for_pst_p)
18655 {
18656 int file_index;
18657
18658 /* Now that we're done scanning the Line Header Program, we can
18659 create the psymtab of each included file. */
18660 for (file_index = 0; file_index < lh->num_file_names; file_index++)
18661 if (lh->file_names[file_index].included_p == 1)
18662 {
d521ce57 18663 const char *include_name =
c6da4cef
DE
18664 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18665 if (include_name != NULL)
aaa75496
JB
18666 dwarf2_create_include_psymtab (include_name, pst, objfile);
18667 }
18668 }
cb1df416
DJ
18669 else
18670 {
18671 /* Make sure a symtab is created for every file, even files
18672 which contain only variables (i.e. no code with associated
18673 line numbers). */
43f3e411 18674 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 18675 int i;
cb1df416
DJ
18676
18677 for (i = 0; i < lh->num_file_names; i++)
18678 {
d521ce57 18679 const char *dir = NULL;
f3f5162e 18680 struct file_entry *fe;
9a619af0 18681
cb1df416 18682 fe = &lh->file_names[i];
568c1b9f
PB
18683 if (fe->dir_index && lh->include_dirs != NULL
18684 && (fe->dir_index - 1) < lh->num_include_dirs)
cb1df416 18685 dir = lh->include_dirs[fe->dir_index - 1];
4d663531 18686 dwarf2_start_subfile (fe->name, dir);
cb1df416 18687
cb1df416 18688 if (current_subfile->symtab == NULL)
43f3e411
DE
18689 {
18690 current_subfile->symtab
18691 = allocate_symtab (cust, current_subfile->name);
18692 }
cb1df416
DJ
18693 fe->symtab = current_subfile->symtab;
18694 }
18695 }
c906108c
SS
18696}
18697
18698/* Start a subfile for DWARF. FILENAME is the name of the file and
18699 DIRNAME the name of the source directory which contains FILENAME
4d663531 18700 or NULL if not known.
c906108c
SS
18701 This routine tries to keep line numbers from identical absolute and
18702 relative file names in a common subfile.
18703
18704 Using the `list' example from the GDB testsuite, which resides in
18705 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18706 of /srcdir/list0.c yields the following debugging information for list0.c:
18707
c5aa993b 18708 DW_AT_name: /srcdir/list0.c
4d663531 18709 DW_AT_comp_dir: /compdir
357e46e7 18710 files.files[0].name: list0.h
c5aa993b 18711 files.files[0].dir: /srcdir
357e46e7 18712 files.files[1].name: list0.c
c5aa993b 18713 files.files[1].dir: /srcdir
c906108c
SS
18714
18715 The line number information for list0.c has to end up in a single
4f1520fb
FR
18716 subfile, so that `break /srcdir/list0.c:1' works as expected.
18717 start_subfile will ensure that this happens provided that we pass the
18718 concatenation of files.files[1].dir and files.files[1].name as the
18719 subfile's name. */
c906108c
SS
18720
18721static void
4d663531 18722dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 18723{
d521ce57 18724 char *copy = NULL;
4f1520fb 18725
4d663531 18726 /* In order not to lose the line information directory,
4f1520fb
FR
18727 we concatenate it to the filename when it makes sense.
18728 Note that the Dwarf3 standard says (speaking of filenames in line
18729 information): ``The directory index is ignored for file names
18730 that represent full path names''. Thus ignoring dirname in the
18731 `else' branch below isn't an issue. */
c906108c 18732
d5166ae1 18733 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
18734 {
18735 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18736 filename = copy;
18737 }
c906108c 18738
4d663531 18739 start_subfile (filename);
4f1520fb 18740
d521ce57
TT
18741 if (copy != NULL)
18742 xfree (copy);
c906108c
SS
18743}
18744
f4dc4d17
DE
18745/* Start a symtab for DWARF.
18746 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18747
43f3e411 18748static struct compunit_symtab *
f4dc4d17 18749dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 18750 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 18751{
43f3e411
DE
18752 struct compunit_symtab *cust
18753 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18754
f4dc4d17
DE
18755 record_debugformat ("DWARF 2");
18756 record_producer (cu->producer);
18757
18758 /* We assume that we're processing GCC output. */
18759 processing_gcc_compilation = 2;
18760
4d4ec4e5 18761 cu->processing_has_namespace_info = 0;
43f3e411
DE
18762
18763 return cust;
f4dc4d17
DE
18764}
18765
4c2df51b
DJ
18766static void
18767var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 18768 struct dwarf2_cu *cu)
4c2df51b 18769{
e7c27a73
DJ
18770 struct objfile *objfile = cu->objfile;
18771 struct comp_unit_head *cu_header = &cu->header;
18772
4c2df51b
DJ
18773 /* NOTE drow/2003-01-30: There used to be a comment and some special
18774 code here to turn a symbol with DW_AT_external and a
18775 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18776 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18777 with some versions of binutils) where shared libraries could have
18778 relocations against symbols in their debug information - the
18779 minimal symbol would have the right address, but the debug info
18780 would not. It's no longer necessary, because we will explicitly
18781 apply relocations when we read in the debug information now. */
18782
18783 /* A DW_AT_location attribute with no contents indicates that a
18784 variable has been optimized away. */
18785 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18786 {
f1e6e072 18787 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
18788 return;
18789 }
18790
18791 /* Handle one degenerate form of location expression specially, to
18792 preserve GDB's previous behavior when section offsets are
3019eac3
DE
18793 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18794 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
18795
18796 if (attr_form_is_block (attr)
3019eac3
DE
18797 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18798 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18799 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18800 && (DW_BLOCK (attr)->size
18801 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 18802 {
891d2f0b 18803 unsigned int dummy;
4c2df51b 18804
3019eac3
DE
18805 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18806 SYMBOL_VALUE_ADDRESS (sym) =
18807 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18808 else
18809 SYMBOL_VALUE_ADDRESS (sym) =
18810 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 18811 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
18812 fixup_symbol_section (sym, objfile);
18813 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18814 SYMBOL_SECTION (sym));
4c2df51b
DJ
18815 return;
18816 }
18817
18818 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18819 expression evaluator, and use LOC_COMPUTED only when necessary
18820 (i.e. when the value of a register or memory location is
18821 referenced, or a thread-local block, etc.). Then again, it might
18822 not be worthwhile. I'm assuming that it isn't unless performance
18823 or memory numbers show me otherwise. */
18824
f1e6e072 18825 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 18826
f1e6e072 18827 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 18828 cu->has_loclist = 1;
4c2df51b
DJ
18829}
18830
c906108c
SS
18831/* Given a pointer to a DWARF information entry, figure out if we need
18832 to make a symbol table entry for it, and if so, create a new entry
18833 and return a pointer to it.
18834 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
18835 used the passed type.
18836 If SPACE is not NULL, use it to hold the new symbol. If it is
18837 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
18838
18839static struct symbol *
34eaf542
TT
18840new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18841 struct symbol *space)
c906108c 18842{
e7c27a73 18843 struct objfile *objfile = cu->objfile;
3e29f34a 18844 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 18845 struct symbol *sym = NULL;
15d034d0 18846 const char *name;
c906108c
SS
18847 struct attribute *attr = NULL;
18848 struct attribute *attr2 = NULL;
e142c38c 18849 CORE_ADDR baseaddr;
e37fd15a
SW
18850 struct pending **list_to_add = NULL;
18851
edb3359d 18852 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
18853
18854 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 18855
94af9270 18856 name = dwarf2_name (die, cu);
c906108c
SS
18857 if (name)
18858 {
94af9270 18859 const char *linkagename;
34eaf542 18860 int suppress_add = 0;
94af9270 18861
34eaf542
TT
18862 if (space)
18863 sym = space;
18864 else
e623cf5d 18865 sym = allocate_symbol (objfile);
c906108c 18866 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
18867
18868 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 18869 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
18870 linkagename = dwarf2_physname (name, die, cu);
18871 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 18872
f55ee35c
JK
18873 /* Fortran does not have mangling standard and the mangling does differ
18874 between gfortran, iFort etc. */
18875 if (cu->language == language_fortran
b250c185 18876 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 18877 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 18878 dwarf2_full_name (name, die, cu),
29df156d 18879 NULL);
f55ee35c 18880
c906108c 18881 /* Default assumptions.
c5aa993b 18882 Use the passed type or decode it from the die. */
176620f1 18883 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 18884 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
18885 if (type != NULL)
18886 SYMBOL_TYPE (sym) = type;
18887 else
e7c27a73 18888 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
18889 attr = dwarf2_attr (die,
18890 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18891 cu);
c906108c
SS
18892 if (attr)
18893 {
18894 SYMBOL_LINE (sym) = DW_UNSND (attr);
18895 }
cb1df416 18896
edb3359d
DJ
18897 attr = dwarf2_attr (die,
18898 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18899 cu);
cb1df416
DJ
18900 if (attr)
18901 {
18902 int file_index = DW_UNSND (attr);
9a619af0 18903
cb1df416
DJ
18904 if (cu->line_header == NULL
18905 || file_index > cu->line_header->num_file_names)
18906 complaint (&symfile_complaints,
18907 _("file index out of range"));
1c3d648d 18908 else if (file_index > 0)
cb1df416
DJ
18909 {
18910 struct file_entry *fe;
9a619af0 18911
cb1df416 18912 fe = &cu->line_header->file_names[file_index - 1];
08be3fe3 18913 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
18914 }
18915 }
18916
c906108c
SS
18917 switch (die->tag)
18918 {
18919 case DW_TAG_label:
e142c38c 18920 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 18921 if (attr)
3e29f34a
MR
18922 {
18923 CORE_ADDR addr;
18924
18925 addr = attr_value_as_address (attr);
18926 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18927 SYMBOL_VALUE_ADDRESS (sym) = addr;
18928 }
0f5238ed
TT
18929 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18930 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 18931 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 18932 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
18933 break;
18934 case DW_TAG_subprogram:
18935 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18936 finish_block. */
f1e6e072 18937 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 18938 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
18939 if ((attr2 && (DW_UNSND (attr2) != 0))
18940 || cu->language == language_ada)
c906108c 18941 {
2cfa0c8d
JB
18942 /* Subprograms marked external are stored as a global symbol.
18943 Ada subprograms, whether marked external or not, are always
18944 stored as a global symbol, because we want to be able to
18945 access them globally. For instance, we want to be able
18946 to break on a nested subprogram without having to
18947 specify the context. */
e37fd15a 18948 list_to_add = &global_symbols;
c906108c
SS
18949 }
18950 else
18951 {
e37fd15a 18952 list_to_add = cu->list_in_scope;
c906108c
SS
18953 }
18954 break;
edb3359d
DJ
18955 case DW_TAG_inlined_subroutine:
18956 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18957 finish_block. */
f1e6e072 18958 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 18959 SYMBOL_INLINED (sym) = 1;
481860b3 18960 list_to_add = cu->list_in_scope;
edb3359d 18961 break;
34eaf542
TT
18962 case DW_TAG_template_value_param:
18963 suppress_add = 1;
18964 /* Fall through. */
72929c62 18965 case DW_TAG_constant:
c906108c 18966 case DW_TAG_variable:
254e6b9e 18967 case DW_TAG_member:
0963b4bd
MS
18968 /* Compilation with minimal debug info may result in
18969 variables with missing type entries. Change the
18970 misleading `void' type to something sensible. */
c906108c 18971 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 18972 SYMBOL_TYPE (sym)
46bf5051 18973 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 18974
e142c38c 18975 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
18976 /* In the case of DW_TAG_member, we should only be called for
18977 static const members. */
18978 if (die->tag == DW_TAG_member)
18979 {
3863f96c
DE
18980 /* dwarf2_add_field uses die_is_declaration,
18981 so we do the same. */
254e6b9e
DE
18982 gdb_assert (die_is_declaration (die, cu));
18983 gdb_assert (attr);
18984 }
c906108c
SS
18985 if (attr)
18986 {
e7c27a73 18987 dwarf2_const_value (attr, sym, cu);
e142c38c 18988 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 18989 if (!suppress_add)
34eaf542
TT
18990 {
18991 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 18992 list_to_add = &global_symbols;
34eaf542 18993 else
e37fd15a 18994 list_to_add = cu->list_in_scope;
34eaf542 18995 }
c906108c
SS
18996 break;
18997 }
e142c38c 18998 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
18999 if (attr)
19000 {
e7c27a73 19001 var_decode_location (attr, sym, cu);
e142c38c 19002 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
19003
19004 /* Fortran explicitly imports any global symbols to the local
19005 scope by DW_TAG_common_block. */
19006 if (cu->language == language_fortran && die->parent
19007 && die->parent->tag == DW_TAG_common_block)
19008 attr2 = NULL;
19009
caac4577
JG
19010 if (SYMBOL_CLASS (sym) == LOC_STATIC
19011 && SYMBOL_VALUE_ADDRESS (sym) == 0
19012 && !dwarf2_per_objfile->has_section_at_zero)
19013 {
19014 /* When a static variable is eliminated by the linker,
19015 the corresponding debug information is not stripped
19016 out, but the variable address is set to null;
19017 do not add such variables into symbol table. */
19018 }
19019 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 19020 {
f55ee35c
JK
19021 /* Workaround gfortran PR debug/40040 - it uses
19022 DW_AT_location for variables in -fPIC libraries which may
19023 get overriden by other libraries/executable and get
19024 a different address. Resolve it by the minimal symbol
19025 which may come from inferior's executable using copy
19026 relocation. Make this workaround only for gfortran as for
19027 other compilers GDB cannot guess the minimal symbol
19028 Fortran mangling kind. */
19029 if (cu->language == language_fortran && die->parent
19030 && die->parent->tag == DW_TAG_module
19031 && cu->producer
28586665 19032 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 19033 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 19034
1c809c68
TT
19035 /* A variable with DW_AT_external is never static,
19036 but it may be block-scoped. */
19037 list_to_add = (cu->list_in_scope == &file_symbols
19038 ? &global_symbols : cu->list_in_scope);
1c809c68 19039 }
c906108c 19040 else
e37fd15a 19041 list_to_add = cu->list_in_scope;
c906108c
SS
19042 }
19043 else
19044 {
19045 /* We do not know the address of this symbol.
c5aa993b
JM
19046 If it is an external symbol and we have type information
19047 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19048 The address of the variable will then be determined from
19049 the minimal symbol table whenever the variable is
19050 referenced. */
e142c38c 19051 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
19052
19053 /* Fortran explicitly imports any global symbols to the local
19054 scope by DW_TAG_common_block. */
19055 if (cu->language == language_fortran && die->parent
19056 && die->parent->tag == DW_TAG_common_block)
19057 {
19058 /* SYMBOL_CLASS doesn't matter here because
19059 read_common_block is going to reset it. */
19060 if (!suppress_add)
19061 list_to_add = cu->list_in_scope;
19062 }
19063 else if (attr2 && (DW_UNSND (attr2) != 0)
19064 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 19065 {
0fe7935b
DJ
19066 /* A variable with DW_AT_external is never static, but it
19067 may be block-scoped. */
19068 list_to_add = (cu->list_in_scope == &file_symbols
19069 ? &global_symbols : cu->list_in_scope);
19070
f1e6e072 19071 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 19072 }
442ddf59
JK
19073 else if (!die_is_declaration (die, cu))
19074 {
19075 /* Use the default LOC_OPTIMIZED_OUT class. */
19076 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
19077 if (!suppress_add)
19078 list_to_add = cu->list_in_scope;
442ddf59 19079 }
c906108c
SS
19080 }
19081 break;
19082 case DW_TAG_formal_parameter:
edb3359d
DJ
19083 /* If we are inside a function, mark this as an argument. If
19084 not, we might be looking at an argument to an inlined function
19085 when we do not have enough information to show inlined frames;
19086 pretend it's a local variable in that case so that the user can
19087 still see it. */
19088 if (context_stack_depth > 0
19089 && context_stack[context_stack_depth - 1].name != NULL)
19090 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 19091 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
19092 if (attr)
19093 {
e7c27a73 19094 var_decode_location (attr, sym, cu);
c906108c 19095 }
e142c38c 19096 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19097 if (attr)
19098 {
e7c27a73 19099 dwarf2_const_value (attr, sym, cu);
c906108c 19100 }
f346a30d 19101
e37fd15a 19102 list_to_add = cu->list_in_scope;
c906108c
SS
19103 break;
19104 case DW_TAG_unspecified_parameters:
19105 /* From varargs functions; gdb doesn't seem to have any
19106 interest in this information, so just ignore it for now.
19107 (FIXME?) */
19108 break;
34eaf542
TT
19109 case DW_TAG_template_type_param:
19110 suppress_add = 1;
19111 /* Fall through. */
c906108c 19112 case DW_TAG_class_type:
680b30c7 19113 case DW_TAG_interface_type:
c906108c
SS
19114 case DW_TAG_structure_type:
19115 case DW_TAG_union_type:
72019c9c 19116 case DW_TAG_set_type:
c906108c 19117 case DW_TAG_enumeration_type:
f1e6e072 19118 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19119 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 19120
63d06c5c 19121 {
9c37b5ae 19122 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
19123 really ever be static objects: otherwise, if you try
19124 to, say, break of a class's method and you're in a file
19125 which doesn't mention that class, it won't work unless
19126 the check for all static symbols in lookup_symbol_aux
19127 saves you. See the OtherFileClass tests in
19128 gdb.c++/namespace.exp. */
19129
e37fd15a 19130 if (!suppress_add)
34eaf542 19131 {
34eaf542 19132 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19133 && cu->language == language_cplus
34eaf542 19134 ? &global_symbols : cu->list_in_scope);
63d06c5c 19135
64382290 19136 /* The semantics of C++ state that "struct foo {
9c37b5ae 19137 ... }" also defines a typedef for "foo". */
64382290 19138 if (cu->language == language_cplus
45280282 19139 || cu->language == language_ada
c44af4eb
TT
19140 || cu->language == language_d
19141 || cu->language == language_rust)
64382290
TT
19142 {
19143 /* The symbol's name is already allocated along
19144 with this objfile, so we don't need to
19145 duplicate it for the type. */
19146 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19147 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19148 }
63d06c5c
DC
19149 }
19150 }
c906108c
SS
19151 break;
19152 case DW_TAG_typedef:
f1e6e072 19153 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 19154 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19155 list_to_add = cu->list_in_scope;
63d06c5c 19156 break;
c906108c 19157 case DW_TAG_base_type:
a02abb62 19158 case DW_TAG_subrange_type:
f1e6e072 19159 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 19160 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 19161 list_to_add = cu->list_in_scope;
c906108c
SS
19162 break;
19163 case DW_TAG_enumerator:
e142c38c 19164 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
19165 if (attr)
19166 {
e7c27a73 19167 dwarf2_const_value (attr, sym, cu);
c906108c 19168 }
63d06c5c
DC
19169 {
19170 /* NOTE: carlton/2003-11-10: See comment above in the
19171 DW_TAG_class_type, etc. block. */
19172
e142c38c 19173 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 19174 && cu->language == language_cplus
e142c38c 19175 ? &global_symbols : cu->list_in_scope);
63d06c5c 19176 }
c906108c 19177 break;
74921315 19178 case DW_TAG_imported_declaration:
5c4e30ca 19179 case DW_TAG_namespace:
f1e6e072 19180 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 19181 list_to_add = &global_symbols;
5c4e30ca 19182 break;
530e8392
KB
19183 case DW_TAG_module:
19184 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19185 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19186 list_to_add = &global_symbols;
19187 break;
4357ac6c 19188 case DW_TAG_common_block:
f1e6e072 19189 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
19190 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19191 add_symbol_to_list (sym, cu->list_in_scope);
19192 break;
c906108c
SS
19193 default:
19194 /* Not a tag we recognize. Hopefully we aren't processing
19195 trash data, but since we must specifically ignore things
19196 we don't recognize, there is nothing else we should do at
0963b4bd 19197 this point. */
e2e0b3e5 19198 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 19199 dwarf_tag_name (die->tag));
c906108c
SS
19200 break;
19201 }
df8a16a1 19202
e37fd15a
SW
19203 if (suppress_add)
19204 {
19205 sym->hash_next = objfile->template_symbols;
19206 objfile->template_symbols = sym;
19207 list_to_add = NULL;
19208 }
19209
19210 if (list_to_add != NULL)
19211 add_symbol_to_list (sym, list_to_add);
19212
df8a16a1
DJ
19213 /* For the benefit of old versions of GCC, check for anonymous
19214 namespaces based on the demangled name. */
4d4ec4e5 19215 if (!cu->processing_has_namespace_info
94af9270 19216 && cu->language == language_cplus)
a10964d1 19217 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
19218 }
19219 return (sym);
19220}
19221
34eaf542
TT
19222/* A wrapper for new_symbol_full that always allocates a new symbol. */
19223
19224static struct symbol *
19225new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19226{
19227 return new_symbol_full (die, type, cu, NULL);
19228}
19229
98bfdba5
PA
19230/* Given an attr with a DW_FORM_dataN value in host byte order,
19231 zero-extend it as appropriate for the symbol's type. The DWARF
19232 standard (v4) is not entirely clear about the meaning of using
19233 DW_FORM_dataN for a constant with a signed type, where the type is
19234 wider than the data. The conclusion of a discussion on the DWARF
19235 list was that this is unspecified. We choose to always zero-extend
19236 because that is the interpretation long in use by GCC. */
c906108c 19237
98bfdba5 19238static gdb_byte *
ff39bb5e 19239dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 19240 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 19241{
e7c27a73 19242 struct objfile *objfile = cu->objfile;
e17a4113
UW
19243 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19244 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
19245 LONGEST l = DW_UNSND (attr);
19246
19247 if (bits < sizeof (*value) * 8)
19248 {
19249 l &= ((LONGEST) 1 << bits) - 1;
19250 *value = l;
19251 }
19252 else if (bits == sizeof (*value) * 8)
19253 *value = l;
19254 else
19255 {
224c3ddb 19256 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
19257 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19258 return bytes;
19259 }
19260
19261 return NULL;
19262}
19263
19264/* Read a constant value from an attribute. Either set *VALUE, or if
19265 the value does not fit in *VALUE, set *BYTES - either already
19266 allocated on the objfile obstack, or newly allocated on OBSTACK,
19267 or, set *BATON, if we translated the constant to a location
19268 expression. */
19269
19270static void
ff39bb5e 19271dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
19272 const char *name, struct obstack *obstack,
19273 struct dwarf2_cu *cu,
d521ce57 19274 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
19275 struct dwarf2_locexpr_baton **baton)
19276{
19277 struct objfile *objfile = cu->objfile;
19278 struct comp_unit_head *cu_header = &cu->header;
c906108c 19279 struct dwarf_block *blk;
98bfdba5
PA
19280 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19281 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19282
19283 *value = 0;
19284 *bytes = NULL;
19285 *baton = NULL;
c906108c
SS
19286
19287 switch (attr->form)
19288 {
19289 case DW_FORM_addr:
3019eac3 19290 case DW_FORM_GNU_addr_index:
ac56253d 19291 {
ac56253d
TT
19292 gdb_byte *data;
19293
98bfdba5
PA
19294 if (TYPE_LENGTH (type) != cu_header->addr_size)
19295 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 19296 cu_header->addr_size,
98bfdba5 19297 TYPE_LENGTH (type));
ac56253d
TT
19298 /* Symbols of this form are reasonably rare, so we just
19299 piggyback on the existing location code rather than writing
19300 a new implementation of symbol_computed_ops. */
8d749320 19301 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
19302 (*baton)->per_cu = cu->per_cu;
19303 gdb_assert ((*baton)->per_cu);
ac56253d 19304
98bfdba5 19305 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 19306 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 19307 (*baton)->data = data;
ac56253d
TT
19308
19309 data[0] = DW_OP_addr;
19310 store_unsigned_integer (&data[1], cu_header->addr_size,
19311 byte_order, DW_ADDR (attr));
19312 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 19313 }
c906108c 19314 break;
4ac36638 19315 case DW_FORM_string:
93b5768b 19316 case DW_FORM_strp:
3019eac3 19317 case DW_FORM_GNU_str_index:
36586728 19318 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
19319 /* DW_STRING is already allocated on the objfile obstack, point
19320 directly to it. */
d521ce57 19321 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 19322 break;
c906108c
SS
19323 case DW_FORM_block1:
19324 case DW_FORM_block2:
19325 case DW_FORM_block4:
19326 case DW_FORM_block:
2dc7f7b3 19327 case DW_FORM_exprloc:
0224619f 19328 case DW_FORM_data16:
c906108c 19329 blk = DW_BLOCK (attr);
98bfdba5
PA
19330 if (TYPE_LENGTH (type) != blk->size)
19331 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19332 TYPE_LENGTH (type));
19333 *bytes = blk->data;
c906108c 19334 break;
2df3850c
JM
19335
19336 /* The DW_AT_const_value attributes are supposed to carry the
19337 symbol's value "represented as it would be on the target
19338 architecture." By the time we get here, it's already been
19339 converted to host endianness, so we just need to sign- or
19340 zero-extend it as appropriate. */
19341 case DW_FORM_data1:
3aef2284 19342 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 19343 break;
c906108c 19344 case DW_FORM_data2:
3aef2284 19345 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 19346 break;
c906108c 19347 case DW_FORM_data4:
3aef2284 19348 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 19349 break;
c906108c 19350 case DW_FORM_data8:
3aef2284 19351 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
19352 break;
19353
c906108c 19354 case DW_FORM_sdata:
98bfdba5 19355 *value = DW_SND (attr);
2df3850c
JM
19356 break;
19357
c906108c 19358 case DW_FORM_udata:
98bfdba5 19359 *value = DW_UNSND (attr);
c906108c 19360 break;
2df3850c 19361
c906108c 19362 default:
4d3c2250 19363 complaint (&symfile_complaints,
e2e0b3e5 19364 _("unsupported const value attribute form: '%s'"),
4d3c2250 19365 dwarf_form_name (attr->form));
98bfdba5 19366 *value = 0;
c906108c
SS
19367 break;
19368 }
19369}
19370
2df3850c 19371
98bfdba5
PA
19372/* Copy constant value from an attribute to a symbol. */
19373
2df3850c 19374static void
ff39bb5e 19375dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 19376 struct dwarf2_cu *cu)
2df3850c 19377{
98bfdba5 19378 struct objfile *objfile = cu->objfile;
12df843f 19379 LONGEST value;
d521ce57 19380 const gdb_byte *bytes;
98bfdba5 19381 struct dwarf2_locexpr_baton *baton;
2df3850c 19382
98bfdba5
PA
19383 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19384 SYMBOL_PRINT_NAME (sym),
19385 &objfile->objfile_obstack, cu,
19386 &value, &bytes, &baton);
2df3850c 19387
98bfdba5
PA
19388 if (baton != NULL)
19389 {
98bfdba5 19390 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 19391 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
19392 }
19393 else if (bytes != NULL)
19394 {
19395 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 19396 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
19397 }
19398 else
19399 {
19400 SYMBOL_VALUE (sym) = value;
f1e6e072 19401 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 19402 }
2df3850c
JM
19403}
19404
c906108c
SS
19405/* Return the type of the die in question using its DW_AT_type attribute. */
19406
19407static struct type *
e7c27a73 19408die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19409{
c906108c 19410 struct attribute *type_attr;
c906108c 19411
e142c38c 19412 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
19413 if (!type_attr)
19414 {
19415 /* A missing DW_AT_type represents a void type. */
46bf5051 19416 return objfile_type (cu->objfile)->builtin_void;
c906108c 19417 }
348e048f 19418
673bfd45 19419 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19420}
19421
b4ba55a1
JB
19422/* True iff CU's producer generates GNAT Ada auxiliary information
19423 that allows to find parallel types through that information instead
19424 of having to do expensive parallel lookups by type name. */
19425
19426static int
19427need_gnat_info (struct dwarf2_cu *cu)
19428{
19429 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19430 of GNAT produces this auxiliary information, without any indication
19431 that it is produced. Part of enhancing the FSF version of GNAT
19432 to produce that information will be to put in place an indicator
19433 that we can use in order to determine whether the descriptive type
19434 info is available or not. One suggestion that has been made is
19435 to use a new attribute, attached to the CU die. For now, assume
19436 that the descriptive type info is not available. */
19437 return 0;
19438}
19439
b4ba55a1
JB
19440/* Return the auxiliary type of the die in question using its
19441 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19442 attribute is not present. */
19443
19444static struct type *
19445die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19446{
b4ba55a1 19447 struct attribute *type_attr;
b4ba55a1
JB
19448
19449 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19450 if (!type_attr)
19451 return NULL;
19452
673bfd45 19453 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
19454}
19455
19456/* If DIE has a descriptive_type attribute, then set the TYPE's
19457 descriptive type accordingly. */
19458
19459static void
19460set_descriptive_type (struct type *type, struct die_info *die,
19461 struct dwarf2_cu *cu)
19462{
19463 struct type *descriptive_type = die_descriptive_type (die, cu);
19464
19465 if (descriptive_type)
19466 {
19467 ALLOCATE_GNAT_AUX_TYPE (type);
19468 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19469 }
19470}
19471
c906108c
SS
19472/* Return the containing type of the die in question using its
19473 DW_AT_containing_type attribute. */
19474
19475static struct type *
e7c27a73 19476die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19477{
c906108c 19478 struct attribute *type_attr;
c906108c 19479
e142c38c 19480 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
19481 if (!type_attr)
19482 error (_("Dwarf Error: Problem turning containing type into gdb type "
4262abfb 19483 "[in module %s]"), objfile_name (cu->objfile));
33ac96f0 19484
673bfd45 19485 return lookup_die_type (die, type_attr, cu);
c906108c
SS
19486}
19487
ac9ec31b
DE
19488/* Return an error marker type to use for the ill formed type in DIE/CU. */
19489
19490static struct type *
19491build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19492{
19493 struct objfile *objfile = dwarf2_per_objfile->objfile;
19494 char *message, *saved;
19495
19496 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
4262abfb 19497 objfile_name (objfile),
ac9ec31b
DE
19498 cu->header.offset.sect_off,
19499 die->offset.sect_off);
224c3ddb
SM
19500 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19501 message, strlen (message));
ac9ec31b
DE
19502 xfree (message);
19503
19f392bc 19504 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
19505}
19506
673bfd45 19507/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
19508 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19509 DW_AT_containing_type.
673bfd45
DE
19510 If there is no type substitute an error marker. */
19511
c906108c 19512static struct type *
ff39bb5e 19513lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 19514 struct dwarf2_cu *cu)
c906108c 19515{
bb5ed363 19516 struct objfile *objfile = cu->objfile;
f792889a
DJ
19517 struct type *this_type;
19518
ac9ec31b
DE
19519 gdb_assert (attr->name == DW_AT_type
19520 || attr->name == DW_AT_GNAT_descriptive_type
19521 || attr->name == DW_AT_containing_type);
19522
673bfd45
DE
19523 /* First see if we have it cached. */
19524
36586728
TT
19525 if (attr->form == DW_FORM_GNU_ref_alt)
19526 {
19527 struct dwarf2_per_cu_data *per_cu;
19528 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19529
19530 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19531 this_type = get_die_type_at_offset (offset, per_cu);
19532 }
7771576e 19533 else if (attr_form_is_ref (attr))
673bfd45 19534 {
b64f50a1 19535 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
19536
19537 this_type = get_die_type_at_offset (offset, cu->per_cu);
19538 }
55f1336d 19539 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 19540 {
ac9ec31b 19541 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 19542
ac9ec31b 19543 return get_signatured_type (die, signature, cu);
673bfd45
DE
19544 }
19545 else
19546 {
ac9ec31b
DE
19547 complaint (&symfile_complaints,
19548 _("Dwarf Error: Bad type attribute %s in DIE"
19549 " at 0x%x [in module %s]"),
19550 dwarf_attr_name (attr->name), die->offset.sect_off,
4262abfb 19551 objfile_name (objfile));
ac9ec31b 19552 return build_error_marker_type (cu, die);
673bfd45
DE
19553 }
19554
19555 /* If not cached we need to read it in. */
19556
19557 if (this_type == NULL)
19558 {
ac9ec31b 19559 struct die_info *type_die = NULL;
673bfd45
DE
19560 struct dwarf2_cu *type_cu = cu;
19561
7771576e 19562 if (attr_form_is_ref (attr))
ac9ec31b
DE
19563 type_die = follow_die_ref (die, attr, &type_cu);
19564 if (type_die == NULL)
19565 return build_error_marker_type (cu, die);
19566 /* If we find the type now, it's probably because the type came
3019eac3
DE
19567 from an inter-CU reference and the type's CU got expanded before
19568 ours. */
ac9ec31b 19569 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
19570 }
19571
19572 /* If we still don't have a type use an error marker. */
19573
19574 if (this_type == NULL)
ac9ec31b 19575 return build_error_marker_type (cu, die);
673bfd45 19576
f792889a 19577 return this_type;
c906108c
SS
19578}
19579
673bfd45
DE
19580/* Return the type in DIE, CU.
19581 Returns NULL for invalid types.
19582
02142a6c 19583 This first does a lookup in die_type_hash,
673bfd45
DE
19584 and only reads the die in if necessary.
19585
19586 NOTE: This can be called when reading in partial or full symbols. */
19587
f792889a 19588static struct type *
e7c27a73 19589read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 19590{
f792889a
DJ
19591 struct type *this_type;
19592
19593 this_type = get_die_type (die, cu);
19594 if (this_type)
19595 return this_type;
19596
673bfd45
DE
19597 return read_type_die_1 (die, cu);
19598}
19599
19600/* Read the type in DIE, CU.
19601 Returns NULL for invalid types. */
19602
19603static struct type *
19604read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19605{
19606 struct type *this_type = NULL;
19607
c906108c
SS
19608 switch (die->tag)
19609 {
19610 case DW_TAG_class_type:
680b30c7 19611 case DW_TAG_interface_type:
c906108c
SS
19612 case DW_TAG_structure_type:
19613 case DW_TAG_union_type:
f792889a 19614 this_type = read_structure_type (die, cu);
c906108c
SS
19615 break;
19616 case DW_TAG_enumeration_type:
f792889a 19617 this_type = read_enumeration_type (die, cu);
c906108c
SS
19618 break;
19619 case DW_TAG_subprogram:
19620 case DW_TAG_subroutine_type:
edb3359d 19621 case DW_TAG_inlined_subroutine:
f792889a 19622 this_type = read_subroutine_type (die, cu);
c906108c
SS
19623 break;
19624 case DW_TAG_array_type:
f792889a 19625 this_type = read_array_type (die, cu);
c906108c 19626 break;
72019c9c 19627 case DW_TAG_set_type:
f792889a 19628 this_type = read_set_type (die, cu);
72019c9c 19629 break;
c906108c 19630 case DW_TAG_pointer_type:
f792889a 19631 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
19632 break;
19633 case DW_TAG_ptr_to_member_type:
f792889a 19634 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
19635 break;
19636 case DW_TAG_reference_type:
4297a3f0
AV
19637 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19638 break;
19639 case DW_TAG_rvalue_reference_type:
19640 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
19641 break;
19642 case DW_TAG_const_type:
f792889a 19643 this_type = read_tag_const_type (die, cu);
c906108c
SS
19644 break;
19645 case DW_TAG_volatile_type:
f792889a 19646 this_type = read_tag_volatile_type (die, cu);
c906108c 19647 break;
06d66ee9
TT
19648 case DW_TAG_restrict_type:
19649 this_type = read_tag_restrict_type (die, cu);
19650 break;
c906108c 19651 case DW_TAG_string_type:
f792889a 19652 this_type = read_tag_string_type (die, cu);
c906108c
SS
19653 break;
19654 case DW_TAG_typedef:
f792889a 19655 this_type = read_typedef (die, cu);
c906108c 19656 break;
a02abb62 19657 case DW_TAG_subrange_type:
f792889a 19658 this_type = read_subrange_type (die, cu);
a02abb62 19659 break;
c906108c 19660 case DW_TAG_base_type:
f792889a 19661 this_type = read_base_type (die, cu);
c906108c 19662 break;
81a17f79 19663 case DW_TAG_unspecified_type:
f792889a 19664 this_type = read_unspecified_type (die, cu);
81a17f79 19665 break;
0114d602
DJ
19666 case DW_TAG_namespace:
19667 this_type = read_namespace_type (die, cu);
19668 break;
f55ee35c
JK
19669 case DW_TAG_module:
19670 this_type = read_module_type (die, cu);
19671 break;
a2c2acaf
MW
19672 case DW_TAG_atomic_type:
19673 this_type = read_tag_atomic_type (die, cu);
19674 break;
c906108c 19675 default:
3e43a32a
MS
19676 complaint (&symfile_complaints,
19677 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 19678 dwarf_tag_name (die->tag));
c906108c
SS
19679 break;
19680 }
63d06c5c 19681
f792889a 19682 return this_type;
63d06c5c
DC
19683}
19684
abc72ce4
DE
19685/* See if we can figure out if the class lives in a namespace. We do
19686 this by looking for a member function; its demangled name will
19687 contain namespace info, if there is any.
19688 Return the computed name or NULL.
19689 Space for the result is allocated on the objfile's obstack.
19690 This is the full-die version of guess_partial_die_structure_name.
19691 In this case we know DIE has no useful parent. */
19692
19693static char *
19694guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19695{
19696 struct die_info *spec_die;
19697 struct dwarf2_cu *spec_cu;
19698 struct die_info *child;
19699
19700 spec_cu = cu;
19701 spec_die = die_specification (die, &spec_cu);
19702 if (spec_die != NULL)
19703 {
19704 die = spec_die;
19705 cu = spec_cu;
19706 }
19707
19708 for (child = die->child;
19709 child != NULL;
19710 child = child->sibling)
19711 {
19712 if (child->tag == DW_TAG_subprogram)
19713 {
7d45c7c3 19714 const char *linkage_name;
abc72ce4 19715
7d45c7c3
KB
19716 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19717 if (linkage_name == NULL)
19718 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19719 cu);
19720 if (linkage_name != NULL)
abc72ce4
DE
19721 {
19722 char *actual_name
19723 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 19724 linkage_name);
abc72ce4
DE
19725 char *name = NULL;
19726
19727 if (actual_name != NULL)
19728 {
15d034d0 19729 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
19730
19731 if (die_name != NULL
19732 && strcmp (die_name, actual_name) != 0)
19733 {
19734 /* Strip off the class name from the full name.
19735 We want the prefix. */
19736 int die_name_len = strlen (die_name);
19737 int actual_name_len = strlen (actual_name);
19738
19739 /* Test for '::' as a sanity check. */
19740 if (actual_name_len > die_name_len + 2
3e43a32a
MS
19741 && actual_name[actual_name_len
19742 - die_name_len - 1] == ':')
224c3ddb
SM
19743 name = (char *) obstack_copy0 (
19744 &cu->objfile->per_bfd->storage_obstack,
19745 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
19746 }
19747 }
19748 xfree (actual_name);
19749 return name;
19750 }
19751 }
19752 }
19753
19754 return NULL;
19755}
19756
96408a79
SA
19757/* GCC might emit a nameless typedef that has a linkage name. Determine the
19758 prefix part in such case. See
19759 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19760
19761static char *
19762anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19763{
19764 struct attribute *attr;
e6a959d6 19765 const char *base;
96408a79
SA
19766
19767 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19768 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19769 return NULL;
19770
7d45c7c3 19771 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
19772 return NULL;
19773
19774 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19775 if (attr == NULL)
19776 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19777 if (attr == NULL || DW_STRING (attr) == NULL)
19778 return NULL;
19779
19780 /* dwarf2_name had to be already called. */
19781 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19782
19783 /* Strip the base name, keep any leading namespaces/classes. */
19784 base = strrchr (DW_STRING (attr), ':');
19785 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19786 return "";
19787
224c3ddb
SM
19788 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19789 DW_STRING (attr),
19790 &base[-1] - DW_STRING (attr));
96408a79
SA
19791}
19792
fdde2d81 19793/* Return the name of the namespace/class that DIE is defined within,
0114d602 19794 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 19795
0114d602
DJ
19796 For example, if we're within the method foo() in the following
19797 code:
19798
19799 namespace N {
19800 class C {
19801 void foo () {
19802 }
19803 };
19804 }
19805
19806 then determine_prefix on foo's die will return "N::C". */
fdde2d81 19807
0d5cff50 19808static const char *
e142c38c 19809determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 19810{
0114d602
DJ
19811 struct die_info *parent, *spec_die;
19812 struct dwarf2_cu *spec_cu;
19813 struct type *parent_type;
96408a79 19814 char *retval;
63d06c5c 19815
9c37b5ae 19816 if (cu->language != language_cplus
c44af4eb
TT
19817 && cu->language != language_fortran && cu->language != language_d
19818 && cu->language != language_rust)
0114d602
DJ
19819 return "";
19820
96408a79
SA
19821 retval = anonymous_struct_prefix (die, cu);
19822 if (retval)
19823 return retval;
19824
0114d602
DJ
19825 /* We have to be careful in the presence of DW_AT_specification.
19826 For example, with GCC 3.4, given the code
19827
19828 namespace N {
19829 void foo() {
19830 // Definition of N::foo.
19831 }
19832 }
19833
19834 then we'll have a tree of DIEs like this:
19835
19836 1: DW_TAG_compile_unit
19837 2: DW_TAG_namespace // N
19838 3: DW_TAG_subprogram // declaration of N::foo
19839 4: DW_TAG_subprogram // definition of N::foo
19840 DW_AT_specification // refers to die #3
19841
19842 Thus, when processing die #4, we have to pretend that we're in
19843 the context of its DW_AT_specification, namely the contex of die
19844 #3. */
19845 spec_cu = cu;
19846 spec_die = die_specification (die, &spec_cu);
19847 if (spec_die == NULL)
19848 parent = die->parent;
19849 else
63d06c5c 19850 {
0114d602
DJ
19851 parent = spec_die->parent;
19852 cu = spec_cu;
63d06c5c 19853 }
0114d602
DJ
19854
19855 if (parent == NULL)
19856 return "";
98bfdba5
PA
19857 else if (parent->building_fullname)
19858 {
19859 const char *name;
19860 const char *parent_name;
19861
19862 /* It has been seen on RealView 2.2 built binaries,
19863 DW_TAG_template_type_param types actually _defined_ as
19864 children of the parent class:
19865
19866 enum E {};
19867 template class <class Enum> Class{};
19868 Class<enum E> class_e;
19869
19870 1: DW_TAG_class_type (Class)
19871 2: DW_TAG_enumeration_type (E)
19872 3: DW_TAG_enumerator (enum1:0)
19873 3: DW_TAG_enumerator (enum2:1)
19874 ...
19875 2: DW_TAG_template_type_param
19876 DW_AT_type DW_FORM_ref_udata (E)
19877
19878 Besides being broken debug info, it can put GDB into an
19879 infinite loop. Consider:
19880
19881 When we're building the full name for Class<E>, we'll start
19882 at Class, and go look over its template type parameters,
19883 finding E. We'll then try to build the full name of E, and
19884 reach here. We're now trying to build the full name of E,
19885 and look over the parent DIE for containing scope. In the
19886 broken case, if we followed the parent DIE of E, we'd again
19887 find Class, and once again go look at its template type
19888 arguments, etc., etc. Simply don't consider such parent die
19889 as source-level parent of this die (it can't be, the language
19890 doesn't allow it), and break the loop here. */
19891 name = dwarf2_name (die, cu);
19892 parent_name = dwarf2_name (parent, cu);
19893 complaint (&symfile_complaints,
19894 _("template param type '%s' defined within parent '%s'"),
19895 name ? name : "<unknown>",
19896 parent_name ? parent_name : "<unknown>");
19897 return "";
19898 }
63d06c5c 19899 else
0114d602
DJ
19900 switch (parent->tag)
19901 {
63d06c5c 19902 case DW_TAG_namespace:
0114d602 19903 parent_type = read_type_die (parent, cu);
acebe513
UW
19904 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19905 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19906 Work around this problem here. */
19907 if (cu->language == language_cplus
19908 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19909 return "";
0114d602
DJ
19910 /* We give a name to even anonymous namespaces. */
19911 return TYPE_TAG_NAME (parent_type);
63d06c5c 19912 case DW_TAG_class_type:
680b30c7 19913 case DW_TAG_interface_type:
63d06c5c 19914 case DW_TAG_structure_type:
0114d602 19915 case DW_TAG_union_type:
f55ee35c 19916 case DW_TAG_module:
0114d602
DJ
19917 parent_type = read_type_die (parent, cu);
19918 if (TYPE_TAG_NAME (parent_type) != NULL)
19919 return TYPE_TAG_NAME (parent_type);
19920 else
19921 /* An anonymous structure is only allowed non-static data
19922 members; no typedefs, no member functions, et cetera.
19923 So it does not need a prefix. */
19924 return "";
abc72ce4 19925 case DW_TAG_compile_unit:
95554aad 19926 case DW_TAG_partial_unit:
abc72ce4
DE
19927 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19928 if (cu->language == language_cplus
8b70b953 19929 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
19930 && die->child != NULL
19931 && (die->tag == DW_TAG_class_type
19932 || die->tag == DW_TAG_structure_type
19933 || die->tag == DW_TAG_union_type))
19934 {
19935 char *name = guess_full_die_structure_name (die, cu);
19936 if (name != NULL)
19937 return name;
19938 }
19939 return "";
3d567982
TT
19940 case DW_TAG_enumeration_type:
19941 parent_type = read_type_die (parent, cu);
19942 if (TYPE_DECLARED_CLASS (parent_type))
19943 {
19944 if (TYPE_TAG_NAME (parent_type) != NULL)
19945 return TYPE_TAG_NAME (parent_type);
19946 return "";
19947 }
19948 /* Fall through. */
63d06c5c 19949 default:
8176b9b8 19950 return determine_prefix (parent, cu);
63d06c5c 19951 }
63d06c5c
DC
19952}
19953
3e43a32a
MS
19954/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19955 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19956 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19957 an obconcat, otherwise allocate storage for the result. The CU argument is
19958 used to determine the language and hence, the appropriate separator. */
987504bb 19959
f55ee35c 19960#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
19961
19962static char *
f55ee35c
JK
19963typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19964 int physname, struct dwarf2_cu *cu)
63d06c5c 19965{
f55ee35c 19966 const char *lead = "";
5c315b68 19967 const char *sep;
63d06c5c 19968
3e43a32a
MS
19969 if (suffix == NULL || suffix[0] == '\0'
19970 || prefix == NULL || prefix[0] == '\0')
987504bb 19971 sep = "";
45280282
IB
19972 else if (cu->language == language_d)
19973 {
19974 /* For D, the 'main' function could be defined in any module, but it
19975 should never be prefixed. */
19976 if (strcmp (suffix, "D main") == 0)
19977 {
19978 prefix = "";
19979 sep = "";
19980 }
19981 else
19982 sep = ".";
19983 }
f55ee35c
JK
19984 else if (cu->language == language_fortran && physname)
19985 {
19986 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19987 DW_AT_MIPS_linkage_name is preferred and used instead. */
19988
19989 lead = "__";
19990 sep = "_MOD_";
19991 }
987504bb
JJ
19992 else
19993 sep = "::";
63d06c5c 19994
6dd47d34
DE
19995 if (prefix == NULL)
19996 prefix = "";
19997 if (suffix == NULL)
19998 suffix = "";
19999
987504bb
JJ
20000 if (obs == NULL)
20001 {
3e43a32a 20002 char *retval
224c3ddb
SM
20003 = ((char *)
20004 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 20005
f55ee35c
JK
20006 strcpy (retval, lead);
20007 strcat (retval, prefix);
6dd47d34
DE
20008 strcat (retval, sep);
20009 strcat (retval, suffix);
63d06c5c
DC
20010 return retval;
20011 }
987504bb
JJ
20012 else
20013 {
20014 /* We have an obstack. */
f55ee35c 20015 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 20016 }
63d06c5c
DC
20017}
20018
c906108c
SS
20019/* Return sibling of die, NULL if no sibling. */
20020
f9aca02d 20021static struct die_info *
fba45db2 20022sibling_die (struct die_info *die)
c906108c 20023{
639d11d3 20024 return die->sibling;
c906108c
SS
20025}
20026
71c25dea
TT
20027/* Get name of a die, return NULL if not found. */
20028
15d034d0
TT
20029static const char *
20030dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
20031 struct obstack *obstack)
20032{
20033 if (name && cu->language == language_cplus)
20034 {
2f408ecb 20035 std::string canon_name = cp_canonicalize_string (name);
71c25dea 20036
2f408ecb 20037 if (!canon_name.empty ())
71c25dea 20038 {
2f408ecb
PA
20039 if (canon_name != name)
20040 name = (const char *) obstack_copy0 (obstack,
20041 canon_name.c_str (),
20042 canon_name.length ());
71c25dea
TT
20043 }
20044 }
20045
20046 return name;
c906108c
SS
20047}
20048
96553a0c
DE
20049/* Get name of a die, return NULL if not found.
20050 Anonymous namespaces are converted to their magic string. */
9219021c 20051
15d034d0 20052static const char *
e142c38c 20053dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
20054{
20055 struct attribute *attr;
20056
e142c38c 20057 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 20058 if ((!attr || !DW_STRING (attr))
96553a0c 20059 && die->tag != DW_TAG_namespace
53832f31
TT
20060 && die->tag != DW_TAG_class_type
20061 && die->tag != DW_TAG_interface_type
20062 && die->tag != DW_TAG_structure_type
20063 && die->tag != DW_TAG_union_type)
71c25dea
TT
20064 return NULL;
20065
20066 switch (die->tag)
20067 {
20068 case DW_TAG_compile_unit:
95554aad 20069 case DW_TAG_partial_unit:
71c25dea
TT
20070 /* Compilation units have a DW_AT_name that is a filename, not
20071 a source language identifier. */
20072 case DW_TAG_enumeration_type:
20073 case DW_TAG_enumerator:
20074 /* These tags always have simple identifiers already; no need
20075 to canonicalize them. */
20076 return DW_STRING (attr);
907af001 20077
96553a0c
DE
20078 case DW_TAG_namespace:
20079 if (attr != NULL && DW_STRING (attr) != NULL)
20080 return DW_STRING (attr);
20081 return CP_ANONYMOUS_NAMESPACE_STR;
20082
907af001
UW
20083 case DW_TAG_class_type:
20084 case DW_TAG_interface_type:
20085 case DW_TAG_structure_type:
20086 case DW_TAG_union_type:
20087 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20088 structures or unions. These were of the form "._%d" in GCC 4.1,
20089 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20090 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 20091 if (attr && DW_STRING (attr)
61012eef
GB
20092 && (startswith (DW_STRING (attr), "._")
20093 || startswith (DW_STRING (attr), "<anonymous")))
907af001 20094 return NULL;
53832f31
TT
20095
20096 /* GCC might emit a nameless typedef that has a linkage name. See
20097 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20098 if (!attr || DW_STRING (attr) == NULL)
20099 {
df5c6c50 20100 char *demangled = NULL;
53832f31
TT
20101
20102 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20103 if (attr == NULL)
20104 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20105
20106 if (attr == NULL || DW_STRING (attr) == NULL)
20107 return NULL;
20108
df5c6c50
JK
20109 /* Avoid demangling DW_STRING (attr) the second time on a second
20110 call for the same DIE. */
20111 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 20112 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
20113
20114 if (demangled)
20115 {
e6a959d6 20116 const char *base;
96408a79 20117
53832f31 20118 /* FIXME: we already did this for the partial symbol... */
34a68019 20119 DW_STRING (attr)
224c3ddb
SM
20120 = ((const char *)
20121 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20122 demangled, strlen (demangled)));
53832f31
TT
20123 DW_STRING_IS_CANONICAL (attr) = 1;
20124 xfree (demangled);
96408a79
SA
20125
20126 /* Strip any leading namespaces/classes, keep only the base name.
20127 DW_AT_name for named DIEs does not contain the prefixes. */
20128 base = strrchr (DW_STRING (attr), ':');
20129 if (base && base > DW_STRING (attr) && base[-1] == ':')
20130 return &base[1];
20131 else
20132 return DW_STRING (attr);
53832f31
TT
20133 }
20134 }
907af001
UW
20135 break;
20136
71c25dea 20137 default:
907af001
UW
20138 break;
20139 }
20140
20141 if (!DW_STRING_IS_CANONICAL (attr))
20142 {
20143 DW_STRING (attr)
20144 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
34a68019 20145 &cu->objfile->per_bfd->storage_obstack);
907af001 20146 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 20147 }
907af001 20148 return DW_STRING (attr);
9219021c
DC
20149}
20150
20151/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
20152 is none. *EXT_CU is the CU containing DIE on input, and the CU
20153 containing the return value on output. */
9219021c
DC
20154
20155static struct die_info *
f2f0e013 20156dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
20157{
20158 struct attribute *attr;
9219021c 20159
f2f0e013 20160 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
20161 if (attr == NULL)
20162 return NULL;
20163
f2f0e013 20164 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
20165}
20166
c906108c
SS
20167/* Convert a DIE tag into its string name. */
20168
f39c6ffd 20169static const char *
aa1ee363 20170dwarf_tag_name (unsigned tag)
c906108c 20171{
f39c6ffd
TT
20172 const char *name = get_DW_TAG_name (tag);
20173
20174 if (name == NULL)
20175 return "DW_TAG_<unknown>";
20176
20177 return name;
c906108c
SS
20178}
20179
20180/* Convert a DWARF attribute code into its string name. */
20181
f39c6ffd 20182static const char *
aa1ee363 20183dwarf_attr_name (unsigned attr)
c906108c 20184{
f39c6ffd
TT
20185 const char *name;
20186
c764a876 20187#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
20188 if (attr == DW_AT_MIPS_fde)
20189 return "DW_AT_MIPS_fde";
20190#else
20191 if (attr == DW_AT_HP_block_index)
20192 return "DW_AT_HP_block_index";
c764a876 20193#endif
f39c6ffd
TT
20194
20195 name = get_DW_AT_name (attr);
20196
20197 if (name == NULL)
20198 return "DW_AT_<unknown>";
20199
20200 return name;
c906108c
SS
20201}
20202
20203/* Convert a DWARF value form code into its string name. */
20204
f39c6ffd 20205static const char *
aa1ee363 20206dwarf_form_name (unsigned form)
c906108c 20207{
f39c6ffd
TT
20208 const char *name = get_DW_FORM_name (form);
20209
20210 if (name == NULL)
20211 return "DW_FORM_<unknown>";
20212
20213 return name;
c906108c
SS
20214}
20215
20216static char *
fba45db2 20217dwarf_bool_name (unsigned mybool)
c906108c
SS
20218{
20219 if (mybool)
20220 return "TRUE";
20221 else
20222 return "FALSE";
20223}
20224
20225/* Convert a DWARF type code into its string name. */
20226
f39c6ffd 20227static const char *
aa1ee363 20228dwarf_type_encoding_name (unsigned enc)
c906108c 20229{
f39c6ffd 20230 const char *name = get_DW_ATE_name (enc);
c906108c 20231
f39c6ffd
TT
20232 if (name == NULL)
20233 return "DW_ATE_<unknown>";
c906108c 20234
f39c6ffd 20235 return name;
c906108c 20236}
c906108c 20237
f9aca02d 20238static void
d97bc12b 20239dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
20240{
20241 unsigned int i;
20242
d97bc12b
DE
20243 print_spaces (indent, f);
20244 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 20245 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
20246
20247 if (die->parent != NULL)
20248 {
20249 print_spaces (indent, f);
20250 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 20251 die->parent->offset.sect_off);
d97bc12b
DE
20252 }
20253
20254 print_spaces (indent, f);
20255 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 20256 dwarf_bool_name (die->child != NULL));
c906108c 20257
d97bc12b
DE
20258 print_spaces (indent, f);
20259 fprintf_unfiltered (f, " attributes:\n");
20260
c906108c
SS
20261 for (i = 0; i < die->num_attrs; ++i)
20262 {
d97bc12b
DE
20263 print_spaces (indent, f);
20264 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
20265 dwarf_attr_name (die->attrs[i].name),
20266 dwarf_form_name (die->attrs[i].form));
d97bc12b 20267
c906108c
SS
20268 switch (die->attrs[i].form)
20269 {
c906108c 20270 case DW_FORM_addr:
3019eac3 20271 case DW_FORM_GNU_addr_index:
d97bc12b 20272 fprintf_unfiltered (f, "address: ");
5af949e3 20273 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
20274 break;
20275 case DW_FORM_block2:
20276 case DW_FORM_block4:
20277 case DW_FORM_block:
20278 case DW_FORM_block1:
56eb65bd
SP
20279 fprintf_unfiltered (f, "block: size %s",
20280 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 20281 break;
2dc7f7b3 20282 case DW_FORM_exprloc:
56eb65bd
SP
20283 fprintf_unfiltered (f, "expression: size %s",
20284 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 20285 break;
0224619f
JK
20286 case DW_FORM_data16:
20287 fprintf_unfiltered (f, "constant of 16 bytes");
20288 break;
4568ecf9
DE
20289 case DW_FORM_ref_addr:
20290 fprintf_unfiltered (f, "ref address: ");
20291 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20292 break;
36586728
TT
20293 case DW_FORM_GNU_ref_alt:
20294 fprintf_unfiltered (f, "alt ref address: ");
20295 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20296 break;
10b3939b
DJ
20297 case DW_FORM_ref1:
20298 case DW_FORM_ref2:
20299 case DW_FORM_ref4:
4568ecf9
DE
20300 case DW_FORM_ref8:
20301 case DW_FORM_ref_udata:
d97bc12b 20302 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 20303 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 20304 break;
c906108c
SS
20305 case DW_FORM_data1:
20306 case DW_FORM_data2:
20307 case DW_FORM_data4:
ce5d95e1 20308 case DW_FORM_data8:
c906108c
SS
20309 case DW_FORM_udata:
20310 case DW_FORM_sdata:
43bbcdc2
PH
20311 fprintf_unfiltered (f, "constant: %s",
20312 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 20313 break;
2dc7f7b3
TT
20314 case DW_FORM_sec_offset:
20315 fprintf_unfiltered (f, "section offset: %s",
20316 pulongest (DW_UNSND (&die->attrs[i])));
20317 break;
55f1336d 20318 case DW_FORM_ref_sig8:
ac9ec31b
DE
20319 fprintf_unfiltered (f, "signature: %s",
20320 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 20321 break;
c906108c 20322 case DW_FORM_string:
4bdf3d34 20323 case DW_FORM_strp:
43988095 20324 case DW_FORM_line_strp:
3019eac3 20325 case DW_FORM_GNU_str_index:
36586728 20326 case DW_FORM_GNU_strp_alt:
8285870a 20327 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 20328 DW_STRING (&die->attrs[i])
8285870a
JK
20329 ? DW_STRING (&die->attrs[i]) : "",
20330 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
20331 break;
20332 case DW_FORM_flag:
20333 if (DW_UNSND (&die->attrs[i]))
d97bc12b 20334 fprintf_unfiltered (f, "flag: TRUE");
c906108c 20335 else
d97bc12b 20336 fprintf_unfiltered (f, "flag: FALSE");
c906108c 20337 break;
2dc7f7b3
TT
20338 case DW_FORM_flag_present:
20339 fprintf_unfiltered (f, "flag: TRUE");
20340 break;
a8329558 20341 case DW_FORM_indirect:
0963b4bd
MS
20342 /* The reader will have reduced the indirect form to
20343 the "base form" so this form should not occur. */
3e43a32a
MS
20344 fprintf_unfiltered (f,
20345 "unexpected attribute form: DW_FORM_indirect");
a8329558 20346 break;
c906108c 20347 default:
d97bc12b 20348 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 20349 die->attrs[i].form);
d97bc12b 20350 break;
c906108c 20351 }
d97bc12b 20352 fprintf_unfiltered (f, "\n");
c906108c
SS
20353 }
20354}
20355
f9aca02d 20356static void
d97bc12b 20357dump_die_for_error (struct die_info *die)
c906108c 20358{
d97bc12b
DE
20359 dump_die_shallow (gdb_stderr, 0, die);
20360}
20361
20362static void
20363dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20364{
20365 int indent = level * 4;
20366
20367 gdb_assert (die != NULL);
20368
20369 if (level >= max_level)
20370 return;
20371
20372 dump_die_shallow (f, indent, die);
20373
20374 if (die->child != NULL)
c906108c 20375 {
d97bc12b
DE
20376 print_spaces (indent, f);
20377 fprintf_unfiltered (f, " Children:");
20378 if (level + 1 < max_level)
20379 {
20380 fprintf_unfiltered (f, "\n");
20381 dump_die_1 (f, level + 1, max_level, die->child);
20382 }
20383 else
20384 {
3e43a32a
MS
20385 fprintf_unfiltered (f,
20386 " [not printed, max nesting level reached]\n");
d97bc12b
DE
20387 }
20388 }
20389
20390 if (die->sibling != NULL && level > 0)
20391 {
20392 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
20393 }
20394}
20395
d97bc12b
DE
20396/* This is called from the pdie macro in gdbinit.in.
20397 It's not static so gcc will keep a copy callable from gdb. */
20398
20399void
20400dump_die (struct die_info *die, int max_level)
20401{
20402 dump_die_1 (gdb_stdlog, 0, max_level, die);
20403}
20404
f9aca02d 20405static void
51545339 20406store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20407{
51545339 20408 void **slot;
c906108c 20409
b64f50a1
JK
20410 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20411 INSERT);
51545339
DJ
20412
20413 *slot = die;
c906108c
SS
20414}
20415
b64f50a1
JK
20416/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20417 required kind. */
20418
20419static sect_offset
ff39bb5e 20420dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 20421{
4568ecf9 20422 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 20423
7771576e 20424 if (attr_form_is_ref (attr))
b64f50a1 20425 return retval;
93311388 20426
b64f50a1 20427 retval.sect_off = 0;
93311388
DE
20428 complaint (&symfile_complaints,
20429 _("unsupported die ref attribute form: '%s'"),
20430 dwarf_form_name (attr->form));
b64f50a1 20431 return retval;
c906108c
SS
20432}
20433
43bbcdc2
PH
20434/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20435 * the value held by the attribute is not constant. */
a02abb62 20436
43bbcdc2 20437static LONGEST
ff39bb5e 20438dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
20439{
20440 if (attr->form == DW_FORM_sdata)
20441 return DW_SND (attr);
20442 else if (attr->form == DW_FORM_udata
20443 || attr->form == DW_FORM_data1
20444 || attr->form == DW_FORM_data2
20445 || attr->form == DW_FORM_data4
20446 || attr->form == DW_FORM_data8)
20447 return DW_UNSND (attr);
20448 else
20449 {
0224619f 20450 /* For DW_FORM_data16 see attr_form_is_constant. */
3e43a32a
MS
20451 complaint (&symfile_complaints,
20452 _("Attribute value is not a constant (%s)"),
a02abb62
JB
20453 dwarf_form_name (attr->form));
20454 return default_value;
20455 }
20456}
20457
348e048f
DE
20458/* Follow reference or signature attribute ATTR of SRC_DIE.
20459 On entry *REF_CU is the CU of SRC_DIE.
20460 On exit *REF_CU is the CU of the result. */
20461
20462static struct die_info *
ff39bb5e 20463follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
20464 struct dwarf2_cu **ref_cu)
20465{
20466 struct die_info *die;
20467
7771576e 20468 if (attr_form_is_ref (attr))
348e048f 20469 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 20470 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
20471 die = follow_die_sig (src_die, attr, ref_cu);
20472 else
20473 {
20474 dump_die_for_error (src_die);
20475 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
4262abfb 20476 objfile_name ((*ref_cu)->objfile));
348e048f
DE
20477 }
20478
20479 return die;
03dd20cc
DJ
20480}
20481
5c631832 20482/* Follow reference OFFSET.
673bfd45
DE
20483 On entry *REF_CU is the CU of the source die referencing OFFSET.
20484 On exit *REF_CU is the CU of the result.
20485 Returns NULL if OFFSET is invalid. */
f504f079 20486
f9aca02d 20487static struct die_info *
36586728
TT
20488follow_die_offset (sect_offset offset, int offset_in_dwz,
20489 struct dwarf2_cu **ref_cu)
c906108c 20490{
10b3939b 20491 struct die_info temp_die;
f2f0e013 20492 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 20493
348e048f
DE
20494 gdb_assert (cu->per_cu != NULL);
20495
98bfdba5
PA
20496 target_cu = cu;
20497
3019eac3 20498 if (cu->per_cu->is_debug_types)
348e048f
DE
20499 {
20500 /* .debug_types CUs cannot reference anything outside their CU.
20501 If they need to, they have to reference a signatured type via
55f1336d 20502 DW_FORM_ref_sig8. */
348e048f 20503 if (! offset_in_cu_p (&cu->header, offset))
5c631832 20504 return NULL;
348e048f 20505 }
36586728
TT
20506 else if (offset_in_dwz != cu->per_cu->is_dwz
20507 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
20508 {
20509 struct dwarf2_per_cu_data *per_cu;
9a619af0 20510
36586728
TT
20511 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20512 cu->objfile);
03dd20cc
DJ
20513
20514 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
20515 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20516 load_full_comp_unit (per_cu, cu->language);
03dd20cc 20517
10b3939b
DJ
20518 target_cu = per_cu->cu;
20519 }
98bfdba5
PA
20520 else if (cu->dies == NULL)
20521 {
20522 /* We're loading full DIEs during partial symbol reading. */
20523 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 20524 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 20525 }
c906108c 20526
f2f0e013 20527 *ref_cu = target_cu;
51545339 20528 temp_die.offset = offset;
9a3c8263
SM
20529 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20530 &temp_die, offset.sect_off);
5c631832 20531}
10b3939b 20532
5c631832
JK
20533/* Follow reference attribute ATTR of SRC_DIE.
20534 On entry *REF_CU is the CU of SRC_DIE.
20535 On exit *REF_CU is the CU of the result. */
20536
20537static struct die_info *
ff39bb5e 20538follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
20539 struct dwarf2_cu **ref_cu)
20540{
b64f50a1 20541 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
20542 struct dwarf2_cu *cu = *ref_cu;
20543 struct die_info *die;
20544
36586728
TT
20545 die = follow_die_offset (offset,
20546 (attr->form == DW_FORM_GNU_ref_alt
20547 || cu->per_cu->is_dwz),
20548 ref_cu);
5c631832
JK
20549 if (!die)
20550 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20551 "at 0x%x [in module %s]"),
4262abfb
JK
20552 offset.sect_off, src_die->offset.sect_off,
20553 objfile_name (cu->objfile));
348e048f 20554
5c631832
JK
20555 return die;
20556}
20557
d83e736b
JK
20558/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20559 Returned value is intended for DW_OP_call*. Returned
20560 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
20561
20562struct dwarf2_locexpr_baton
8b9737bf
TT
20563dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20564 struct dwarf2_per_cu_data *per_cu,
20565 CORE_ADDR (*get_frame_pc) (void *baton),
20566 void *baton)
5c631832 20567{
918dd910 20568 struct dwarf2_cu *cu;
5c631832
JK
20569 struct die_info *die;
20570 struct attribute *attr;
20571 struct dwarf2_locexpr_baton retval;
20572
8cf6f0b1
TT
20573 dw2_setup (per_cu->objfile);
20574
918dd910
JK
20575 if (per_cu->cu == NULL)
20576 load_cu (per_cu);
20577 cu = per_cu->cu;
cc12ce38
DE
20578 if (cu == NULL)
20579 {
20580 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20581 Instead just throw an error, not much else we can do. */
20582 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20583 offset.sect_off, objfile_name (per_cu->objfile));
20584 }
918dd910 20585
36586728 20586 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
20587 if (!die)
20588 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20589 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20590
20591 attr = dwarf2_attr (die, DW_AT_location, cu);
20592 if (!attr)
20593 {
e103e986
JK
20594 /* DWARF: "If there is no such attribute, then there is no effect.".
20595 DATA is ignored if SIZE is 0. */
5c631832 20596
e103e986 20597 retval.data = NULL;
5c631832
JK
20598 retval.size = 0;
20599 }
8cf6f0b1
TT
20600 else if (attr_form_is_section_offset (attr))
20601 {
20602 struct dwarf2_loclist_baton loclist_baton;
20603 CORE_ADDR pc = (*get_frame_pc) (baton);
20604 size_t size;
20605
20606 fill_in_loclist_baton (cu, &loclist_baton, attr);
20607
20608 retval.data = dwarf2_find_location_expression (&loclist_baton,
20609 &size, pc);
20610 retval.size = size;
20611 }
5c631832
JK
20612 else
20613 {
20614 if (!attr_form_is_block (attr))
20615 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20616 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
4262abfb 20617 offset.sect_off, objfile_name (per_cu->objfile));
5c631832
JK
20618
20619 retval.data = DW_BLOCK (attr)->data;
20620 retval.size = DW_BLOCK (attr)->size;
20621 }
20622 retval.per_cu = cu->per_cu;
918dd910 20623
918dd910
JK
20624 age_cached_comp_units ();
20625
5c631832 20626 return retval;
348e048f
DE
20627}
20628
8b9737bf
TT
20629/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20630 offset. */
20631
20632struct dwarf2_locexpr_baton
20633dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20634 struct dwarf2_per_cu_data *per_cu,
20635 CORE_ADDR (*get_frame_pc) (void *baton),
20636 void *baton)
20637{
20638 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20639
20640 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20641}
20642
b6807d98
TT
20643/* Write a constant of a given type as target-ordered bytes into
20644 OBSTACK. */
20645
20646static const gdb_byte *
20647write_constant_as_bytes (struct obstack *obstack,
20648 enum bfd_endian byte_order,
20649 struct type *type,
20650 ULONGEST value,
20651 LONGEST *len)
20652{
20653 gdb_byte *result;
20654
20655 *len = TYPE_LENGTH (type);
224c3ddb 20656 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20657 store_unsigned_integer (result, *len, byte_order, value);
20658
20659 return result;
20660}
20661
20662/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20663 pointer to the constant bytes and set LEN to the length of the
20664 data. If memory is needed, allocate it on OBSTACK. If the DIE
20665 does not have a DW_AT_const_value, return NULL. */
20666
20667const gdb_byte *
20668dwarf2_fetch_constant_bytes (sect_offset offset,
20669 struct dwarf2_per_cu_data *per_cu,
20670 struct obstack *obstack,
20671 LONGEST *len)
20672{
20673 struct dwarf2_cu *cu;
20674 struct die_info *die;
20675 struct attribute *attr;
20676 const gdb_byte *result = NULL;
20677 struct type *type;
20678 LONGEST value;
20679 enum bfd_endian byte_order;
20680
20681 dw2_setup (per_cu->objfile);
20682
20683 if (per_cu->cu == NULL)
20684 load_cu (per_cu);
20685 cu = per_cu->cu;
cc12ce38
DE
20686 if (cu == NULL)
20687 {
20688 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20689 Instead just throw an error, not much else we can do. */
20690 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20691 offset.sect_off, objfile_name (per_cu->objfile));
20692 }
b6807d98
TT
20693
20694 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20695 if (!die)
20696 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
4262abfb 20697 offset.sect_off, objfile_name (per_cu->objfile));
b6807d98
TT
20698
20699
20700 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20701 if (attr == NULL)
20702 return NULL;
20703
20704 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20705 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20706
20707 switch (attr->form)
20708 {
20709 case DW_FORM_addr:
20710 case DW_FORM_GNU_addr_index:
20711 {
20712 gdb_byte *tem;
20713
20714 *len = cu->header.addr_size;
224c3ddb 20715 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
20716 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20717 result = tem;
20718 }
20719 break;
20720 case DW_FORM_string:
20721 case DW_FORM_strp:
20722 case DW_FORM_GNU_str_index:
20723 case DW_FORM_GNU_strp_alt:
20724 /* DW_STRING is already allocated on the objfile obstack, point
20725 directly to it. */
20726 result = (const gdb_byte *) DW_STRING (attr);
20727 *len = strlen (DW_STRING (attr));
20728 break;
20729 case DW_FORM_block1:
20730 case DW_FORM_block2:
20731 case DW_FORM_block4:
20732 case DW_FORM_block:
20733 case DW_FORM_exprloc:
0224619f 20734 case DW_FORM_data16:
b6807d98
TT
20735 result = DW_BLOCK (attr)->data;
20736 *len = DW_BLOCK (attr)->size;
20737 break;
20738
20739 /* The DW_AT_const_value attributes are supposed to carry the
20740 symbol's value "represented as it would be on the target
20741 architecture." By the time we get here, it's already been
20742 converted to host endianness, so we just need to sign- or
20743 zero-extend it as appropriate. */
20744 case DW_FORM_data1:
20745 type = die_type (die, cu);
20746 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20747 if (result == NULL)
20748 result = write_constant_as_bytes (obstack, byte_order,
20749 type, value, len);
20750 break;
20751 case DW_FORM_data2:
20752 type = die_type (die, cu);
20753 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20754 if (result == NULL)
20755 result = write_constant_as_bytes (obstack, byte_order,
20756 type, value, len);
20757 break;
20758 case DW_FORM_data4:
20759 type = die_type (die, cu);
20760 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20761 if (result == NULL)
20762 result = write_constant_as_bytes (obstack, byte_order,
20763 type, value, len);
20764 break;
20765 case DW_FORM_data8:
20766 type = die_type (die, cu);
20767 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20768 if (result == NULL)
20769 result = write_constant_as_bytes (obstack, byte_order,
20770 type, value, len);
20771 break;
20772
20773 case DW_FORM_sdata:
20774 type = die_type (die, cu);
20775 result = write_constant_as_bytes (obstack, byte_order,
20776 type, DW_SND (attr), len);
20777 break;
20778
20779 case DW_FORM_udata:
20780 type = die_type (die, cu);
20781 result = write_constant_as_bytes (obstack, byte_order,
20782 type, DW_UNSND (attr), len);
20783 break;
20784
20785 default:
20786 complaint (&symfile_complaints,
20787 _("unsupported const value attribute form: '%s'"),
20788 dwarf_form_name (attr->form));
20789 break;
20790 }
20791
20792 return result;
20793}
20794
7942e96e
AA
20795/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20796 valid type for this die is found. */
20797
20798struct type *
20799dwarf2_fetch_die_type_sect_off (sect_offset offset,
20800 struct dwarf2_per_cu_data *per_cu)
20801{
20802 struct dwarf2_cu *cu;
20803 struct die_info *die;
20804
20805 dw2_setup (per_cu->objfile);
20806
20807 if (per_cu->cu == NULL)
20808 load_cu (per_cu);
20809 cu = per_cu->cu;
20810 if (!cu)
20811 return NULL;
20812
20813 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20814 if (!die)
20815 return NULL;
20816
20817 return die_type (die, cu);
20818}
20819
8a9b8146
TT
20820/* Return the type of the DIE at DIE_OFFSET in the CU named by
20821 PER_CU. */
20822
20823struct type *
b64f50a1 20824dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
20825 struct dwarf2_per_cu_data *per_cu)
20826{
b64f50a1
JK
20827 sect_offset die_offset_sect;
20828
8a9b8146 20829 dw2_setup (per_cu->objfile);
b64f50a1
JK
20830
20831 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20832 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
20833}
20834
ac9ec31b 20835/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 20836 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
20837 On exit *REF_CU is the CU of the result.
20838 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
20839
20840static struct die_info *
ac9ec31b
DE
20841follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20842 struct dwarf2_cu **ref_cu)
348e048f 20843{
348e048f 20844 struct die_info temp_die;
348e048f
DE
20845 struct dwarf2_cu *sig_cu;
20846 struct die_info *die;
20847
ac9ec31b
DE
20848 /* While it might be nice to assert sig_type->type == NULL here,
20849 we can get here for DW_AT_imported_declaration where we need
20850 the DIE not the type. */
348e048f
DE
20851
20852 /* If necessary, add it to the queue and load its DIEs. */
20853
95554aad 20854 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 20855 read_signatured_type (sig_type);
348e048f 20856
348e048f 20857 sig_cu = sig_type->per_cu.cu;
69d751e3 20858 gdb_assert (sig_cu != NULL);
3019eac3
DE
20859 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20860 temp_die.offset = sig_type->type_offset_in_section;
9a3c8263
SM
20861 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20862 temp_die.offset.sect_off);
348e048f
DE
20863 if (die)
20864 {
796a7ff8
DE
20865 /* For .gdb_index version 7 keep track of included TUs.
20866 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20867 if (dwarf2_per_objfile->index_table != NULL
20868 && dwarf2_per_objfile->index_table->version <= 7)
20869 {
20870 VEC_safe_push (dwarf2_per_cu_ptr,
20871 (*ref_cu)->per_cu->imported_symtabs,
20872 sig_cu->per_cu);
20873 }
20874
348e048f
DE
20875 *ref_cu = sig_cu;
20876 return die;
20877 }
20878
ac9ec31b
DE
20879 return NULL;
20880}
20881
20882/* Follow signatured type referenced by ATTR in SRC_DIE.
20883 On entry *REF_CU is the CU of SRC_DIE.
20884 On exit *REF_CU is the CU of the result.
20885 The result is the DIE of the type.
20886 If the referenced type cannot be found an error is thrown. */
20887
20888static struct die_info *
ff39bb5e 20889follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
20890 struct dwarf2_cu **ref_cu)
20891{
20892 ULONGEST signature = DW_SIGNATURE (attr);
20893 struct signatured_type *sig_type;
20894 struct die_info *die;
20895
20896 gdb_assert (attr->form == DW_FORM_ref_sig8);
20897
a2ce51a0 20898 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
20899 /* sig_type will be NULL if the signatured type is missing from
20900 the debug info. */
20901 if (sig_type == NULL)
20902 {
20903 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20904 " from DIE at 0x%x [in module %s]"),
20905 hex_string (signature), src_die->offset.sect_off,
4262abfb 20906 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20907 }
20908
20909 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20910 if (die == NULL)
20911 {
20912 dump_die_for_error (src_die);
20913 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20914 " from DIE at 0x%x [in module %s]"),
20915 hex_string (signature), src_die->offset.sect_off,
4262abfb 20916 objfile_name ((*ref_cu)->objfile));
ac9ec31b
DE
20917 }
20918
20919 return die;
20920}
20921
20922/* Get the type specified by SIGNATURE referenced in DIE/CU,
20923 reading in and processing the type unit if necessary. */
20924
20925static struct type *
20926get_signatured_type (struct die_info *die, ULONGEST signature,
20927 struct dwarf2_cu *cu)
20928{
20929 struct signatured_type *sig_type;
20930 struct dwarf2_cu *type_cu;
20931 struct die_info *type_die;
20932 struct type *type;
20933
a2ce51a0 20934 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
20935 /* sig_type will be NULL if the signatured type is missing from
20936 the debug info. */
20937 if (sig_type == NULL)
20938 {
20939 complaint (&symfile_complaints,
20940 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20941 " from DIE at 0x%x [in module %s]"),
20942 hex_string (signature), die->offset.sect_off,
4262abfb 20943 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20944 return build_error_marker_type (cu, die);
20945 }
20946
20947 /* If we already know the type we're done. */
20948 if (sig_type->type != NULL)
20949 return sig_type->type;
20950
20951 type_cu = cu;
20952 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20953 if (type_die != NULL)
20954 {
20955 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20956 is created. This is important, for example, because for c++ classes
20957 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20958 type = read_type_die (type_die, type_cu);
20959 if (type == NULL)
20960 {
20961 complaint (&symfile_complaints,
20962 _("Dwarf Error: Cannot build signatured type %s"
20963 " referenced from DIE at 0x%x [in module %s]"),
20964 hex_string (signature), die->offset.sect_off,
4262abfb 20965 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20966 type = build_error_marker_type (cu, die);
20967 }
20968 }
20969 else
20970 {
20971 complaint (&symfile_complaints,
20972 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20973 " from DIE at 0x%x [in module %s]"),
20974 hex_string (signature), die->offset.sect_off,
4262abfb 20975 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
20976 type = build_error_marker_type (cu, die);
20977 }
20978 sig_type->type = type;
20979
20980 return type;
20981}
20982
20983/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20984 reading in and processing the type unit if necessary. */
20985
20986static struct type *
ff39bb5e 20987get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 20988 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
20989{
20990 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 20991 if (attr_form_is_ref (attr))
ac9ec31b
DE
20992 {
20993 struct dwarf2_cu *type_cu = cu;
20994 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20995
20996 return read_type_die (type_die, type_cu);
20997 }
20998 else if (attr->form == DW_FORM_ref_sig8)
20999 {
21000 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
21001 }
21002 else
21003 {
21004 complaint (&symfile_complaints,
21005 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21006 " at 0x%x [in module %s]"),
21007 dwarf_form_name (attr->form), die->offset.sect_off,
4262abfb 21008 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
21009 return build_error_marker_type (cu, die);
21010 }
348e048f
DE
21011}
21012
e5fe5e75 21013/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
21014
21015static void
e5fe5e75 21016load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 21017{
52dc124a 21018 struct signatured_type *sig_type;
348e048f 21019
f4dc4d17
DE
21020 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21021 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21022
6721b2ec
DE
21023 /* We have the per_cu, but we need the signatured_type.
21024 Fortunately this is an easy translation. */
21025 gdb_assert (per_cu->is_debug_types);
21026 sig_type = (struct signatured_type *) per_cu;
348e048f 21027
6721b2ec 21028 gdb_assert (per_cu->cu == NULL);
348e048f 21029
52dc124a 21030 read_signatured_type (sig_type);
348e048f 21031
6721b2ec 21032 gdb_assert (per_cu->cu != NULL);
348e048f
DE
21033}
21034
dee91e82
DE
21035/* die_reader_func for read_signatured_type.
21036 This is identical to load_full_comp_unit_reader,
21037 but is kept separate for now. */
348e048f
DE
21038
21039static void
dee91e82 21040read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 21041 const gdb_byte *info_ptr,
dee91e82
DE
21042 struct die_info *comp_unit_die,
21043 int has_children,
21044 void *data)
348e048f 21045{
dee91e82 21046 struct dwarf2_cu *cu = reader->cu;
348e048f 21047
dee91e82
DE
21048 gdb_assert (cu->die_hash == NULL);
21049 cu->die_hash =
21050 htab_create_alloc_ex (cu->header.length / 12,
21051 die_hash,
21052 die_eq,
21053 NULL,
21054 &cu->comp_unit_obstack,
21055 hashtab_obstack_allocate,
21056 dummy_obstack_deallocate);
348e048f 21057
dee91e82
DE
21058 if (has_children)
21059 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21060 &info_ptr, comp_unit_die);
21061 cu->dies = comp_unit_die;
21062 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
21063
21064 /* We try not to read any attributes in this function, because not
9cdd5dbd 21065 all CUs needed for references have been loaded yet, and symbol
348e048f 21066 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
21067 or we won't be able to build types correctly.
21068 Similarly, if we do not read the producer, we can not apply
21069 producer-specific interpretation. */
95554aad 21070 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 21071}
348e048f 21072
3019eac3
DE
21073/* Read in a signatured type and build its CU and DIEs.
21074 If the type is a stub for the real type in a DWO file,
21075 read in the real type from the DWO file as well. */
dee91e82
DE
21076
21077static void
21078read_signatured_type (struct signatured_type *sig_type)
21079{
21080 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 21081
3019eac3 21082 gdb_assert (per_cu->is_debug_types);
dee91e82 21083 gdb_assert (per_cu->cu == NULL);
348e048f 21084
f4dc4d17
DE
21085 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21086 read_signatured_type_reader, NULL);
7ee85ab1 21087 sig_type->per_cu.tu_read = 1;
c906108c
SS
21088}
21089
c906108c
SS
21090/* Decode simple location descriptions.
21091 Given a pointer to a dwarf block that defines a location, compute
21092 the location and return the value.
21093
4cecd739
DJ
21094 NOTE drow/2003-11-18: This function is called in two situations
21095 now: for the address of static or global variables (partial symbols
21096 only) and for offsets into structures which are expected to be
21097 (more or less) constant. The partial symbol case should go away,
21098 and only the constant case should remain. That will let this
21099 function complain more accurately. A few special modes are allowed
21100 without complaint for global variables (for instance, global
21101 register values and thread-local values).
c906108c
SS
21102
21103 A location description containing no operations indicates that the
4cecd739 21104 object is optimized out. The return value is 0 for that case.
6b992462
DJ
21105 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21106 callers will only want a very basic result and this can become a
21ae7a4d
JK
21107 complaint.
21108
21109 Note that stack[0] is unused except as a default error return. */
c906108c
SS
21110
21111static CORE_ADDR
e7c27a73 21112decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 21113{
e7c27a73 21114 struct objfile *objfile = cu->objfile;
56eb65bd
SP
21115 size_t i;
21116 size_t size = blk->size;
d521ce57 21117 const gdb_byte *data = blk->data;
21ae7a4d
JK
21118 CORE_ADDR stack[64];
21119 int stacki;
21120 unsigned int bytes_read, unsnd;
21121 gdb_byte op;
c906108c 21122
21ae7a4d
JK
21123 i = 0;
21124 stacki = 0;
21125 stack[stacki] = 0;
21126 stack[++stacki] = 0;
21127
21128 while (i < size)
21129 {
21130 op = data[i++];
21131 switch (op)
21132 {
21133 case DW_OP_lit0:
21134 case DW_OP_lit1:
21135 case DW_OP_lit2:
21136 case DW_OP_lit3:
21137 case DW_OP_lit4:
21138 case DW_OP_lit5:
21139 case DW_OP_lit6:
21140 case DW_OP_lit7:
21141 case DW_OP_lit8:
21142 case DW_OP_lit9:
21143 case DW_OP_lit10:
21144 case DW_OP_lit11:
21145 case DW_OP_lit12:
21146 case DW_OP_lit13:
21147 case DW_OP_lit14:
21148 case DW_OP_lit15:
21149 case DW_OP_lit16:
21150 case DW_OP_lit17:
21151 case DW_OP_lit18:
21152 case DW_OP_lit19:
21153 case DW_OP_lit20:
21154 case DW_OP_lit21:
21155 case DW_OP_lit22:
21156 case DW_OP_lit23:
21157 case DW_OP_lit24:
21158 case DW_OP_lit25:
21159 case DW_OP_lit26:
21160 case DW_OP_lit27:
21161 case DW_OP_lit28:
21162 case DW_OP_lit29:
21163 case DW_OP_lit30:
21164 case DW_OP_lit31:
21165 stack[++stacki] = op - DW_OP_lit0;
21166 break;
f1bea926 21167
21ae7a4d
JK
21168 case DW_OP_reg0:
21169 case DW_OP_reg1:
21170 case DW_OP_reg2:
21171 case DW_OP_reg3:
21172 case DW_OP_reg4:
21173 case DW_OP_reg5:
21174 case DW_OP_reg6:
21175 case DW_OP_reg7:
21176 case DW_OP_reg8:
21177 case DW_OP_reg9:
21178 case DW_OP_reg10:
21179 case DW_OP_reg11:
21180 case DW_OP_reg12:
21181 case DW_OP_reg13:
21182 case DW_OP_reg14:
21183 case DW_OP_reg15:
21184 case DW_OP_reg16:
21185 case DW_OP_reg17:
21186 case DW_OP_reg18:
21187 case DW_OP_reg19:
21188 case DW_OP_reg20:
21189 case DW_OP_reg21:
21190 case DW_OP_reg22:
21191 case DW_OP_reg23:
21192 case DW_OP_reg24:
21193 case DW_OP_reg25:
21194 case DW_OP_reg26:
21195 case DW_OP_reg27:
21196 case DW_OP_reg28:
21197 case DW_OP_reg29:
21198 case DW_OP_reg30:
21199 case DW_OP_reg31:
21200 stack[++stacki] = op - DW_OP_reg0;
21201 if (i < size)
21202 dwarf2_complex_location_expr_complaint ();
21203 break;
c906108c 21204
21ae7a4d
JK
21205 case DW_OP_regx:
21206 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21207 i += bytes_read;
21208 stack[++stacki] = unsnd;
21209 if (i < size)
21210 dwarf2_complex_location_expr_complaint ();
21211 break;
c906108c 21212
21ae7a4d
JK
21213 case DW_OP_addr:
21214 stack[++stacki] = read_address (objfile->obfd, &data[i],
21215 cu, &bytes_read);
21216 i += bytes_read;
21217 break;
d53d4ac5 21218
21ae7a4d
JK
21219 case DW_OP_const1u:
21220 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21221 i += 1;
21222 break;
21223
21224 case DW_OP_const1s:
21225 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21226 i += 1;
21227 break;
21228
21229 case DW_OP_const2u:
21230 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21231 i += 2;
21232 break;
21233
21234 case DW_OP_const2s:
21235 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21236 i += 2;
21237 break;
d53d4ac5 21238
21ae7a4d
JK
21239 case DW_OP_const4u:
21240 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21241 i += 4;
21242 break;
21243
21244 case DW_OP_const4s:
21245 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21246 i += 4;
21247 break;
21248
585861ea
JK
21249 case DW_OP_const8u:
21250 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21251 i += 8;
21252 break;
21253
21ae7a4d
JK
21254 case DW_OP_constu:
21255 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21256 &bytes_read);
21257 i += bytes_read;
21258 break;
21259
21260 case DW_OP_consts:
21261 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21262 i += bytes_read;
21263 break;
21264
21265 case DW_OP_dup:
21266 stack[stacki + 1] = stack[stacki];
21267 stacki++;
21268 break;
21269
21270 case DW_OP_plus:
21271 stack[stacki - 1] += stack[stacki];
21272 stacki--;
21273 break;
21274
21275 case DW_OP_plus_uconst:
21276 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21277 &bytes_read);
21278 i += bytes_read;
21279 break;
21280
21281 case DW_OP_minus:
21282 stack[stacki - 1] -= stack[stacki];
21283 stacki--;
21284 break;
21285
21286 case DW_OP_deref:
21287 /* If we're not the last op, then we definitely can't encode
21288 this using GDB's address_class enum. This is valid for partial
21289 global symbols, although the variable's address will be bogus
21290 in the psymtab. */
21291 if (i < size)
21292 dwarf2_complex_location_expr_complaint ();
21293 break;
21294
21295 case DW_OP_GNU_push_tls_address:
4aa4e28b 21296 case DW_OP_form_tls_address:
21ae7a4d
JK
21297 /* The top of the stack has the offset from the beginning
21298 of the thread control block at which the variable is located. */
21299 /* Nothing should follow this operator, so the top of stack would
21300 be returned. */
21301 /* This is valid for partial global symbols, but the variable's
585861ea
JK
21302 address will be bogus in the psymtab. Make it always at least
21303 non-zero to not look as a variable garbage collected by linker
21304 which have DW_OP_addr 0. */
21ae7a4d
JK
21305 if (i < size)
21306 dwarf2_complex_location_expr_complaint ();
585861ea 21307 stack[stacki]++;
21ae7a4d
JK
21308 break;
21309
21310 case DW_OP_GNU_uninit:
21311 break;
21312
3019eac3 21313 case DW_OP_GNU_addr_index:
49f6c839 21314 case DW_OP_GNU_const_index:
3019eac3
DE
21315 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21316 &bytes_read);
21317 i += bytes_read;
21318 break;
21319
21ae7a4d
JK
21320 default:
21321 {
f39c6ffd 21322 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
21323
21324 if (name)
21325 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21326 name);
21327 else
21328 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21329 op);
21330 }
21331
21332 return (stack[stacki]);
d53d4ac5 21333 }
3c6e0cb3 21334
21ae7a4d
JK
21335 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21336 outside of the allocated space. Also enforce minimum>0. */
21337 if (stacki >= ARRAY_SIZE (stack) - 1)
21338 {
21339 complaint (&symfile_complaints,
21340 _("location description stack overflow"));
21341 return 0;
21342 }
21343
21344 if (stacki <= 0)
21345 {
21346 complaint (&symfile_complaints,
21347 _("location description stack underflow"));
21348 return 0;
21349 }
21350 }
21351 return (stack[stacki]);
c906108c
SS
21352}
21353
21354/* memory allocation interface */
21355
c906108c 21356static struct dwarf_block *
7b5a2f43 21357dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 21358{
8d749320 21359 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
21360}
21361
c906108c 21362static struct die_info *
b60c80d6 21363dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
21364{
21365 struct die_info *die;
b60c80d6
DJ
21366 size_t size = sizeof (struct die_info);
21367
21368 if (num_attrs > 1)
21369 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 21370
b60c80d6 21371 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
21372 memset (die, 0, sizeof (struct die_info));
21373 return (die);
21374}
2e276125
JB
21375
21376\f
21377/* Macro support. */
21378
233d95b5
JK
21379/* Return file name relative to the compilation directory of file number I in
21380 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 21381 responsible for freeing it. */
233d95b5 21382
2e276125 21383static char *
233d95b5 21384file_file_name (int file, struct line_header *lh)
2e276125 21385{
6a83a1e6
EZ
21386 /* Is the file number a valid index into the line header's file name
21387 table? Remember that file numbers start with one, not zero. */
21388 if (1 <= file && file <= lh->num_file_names)
21389 {
21390 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 21391
afa6c9ab 21392 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0
568c1b9f
PB
21393 || lh->include_dirs == NULL
21394 || (fe->dir_index - 1) >= lh->num_include_dirs)
6a83a1e6 21395 return xstrdup (fe->name);
233d95b5 21396 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
b36cec19 21397 fe->name, (char *) NULL);
6a83a1e6 21398 }
2e276125
JB
21399 else
21400 {
6a83a1e6
EZ
21401 /* The compiler produced a bogus file number. We can at least
21402 record the macro definitions made in the file, even if we
21403 won't be able to find the file by name. */
21404 char fake_name[80];
9a619af0 21405
8c042590
PM
21406 xsnprintf (fake_name, sizeof (fake_name),
21407 "<bad macro file number %d>", file);
2e276125 21408
6e70227d 21409 complaint (&symfile_complaints,
6a83a1e6
EZ
21410 _("bad file number in macro information (%d)"),
21411 file);
2e276125 21412
6a83a1e6 21413 return xstrdup (fake_name);
2e276125
JB
21414 }
21415}
21416
233d95b5
JK
21417/* Return the full name of file number I in *LH's file name table.
21418 Use COMP_DIR as the name of the current directory of the
21419 compilation. The result is allocated using xmalloc; the caller is
21420 responsible for freeing it. */
21421static char *
21422file_full_name (int file, struct line_header *lh, const char *comp_dir)
21423{
21424 /* Is the file number a valid index into the line header's file name
21425 table? Remember that file numbers start with one, not zero. */
21426 if (1 <= file && file <= lh->num_file_names)
21427 {
21428 char *relative = file_file_name (file, lh);
21429
21430 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21431 return relative;
b36cec19
PA
21432 return reconcat (relative, comp_dir, SLASH_STRING,
21433 relative, (char *) NULL);
233d95b5
JK
21434 }
21435 else
21436 return file_file_name (file, lh);
21437}
21438
2e276125
JB
21439
21440static struct macro_source_file *
21441macro_start_file (int file, int line,
21442 struct macro_source_file *current_file,
43f3e411 21443 struct line_header *lh)
2e276125 21444{
233d95b5
JK
21445 /* File name relative to the compilation directory of this source file. */
21446 char *file_name = file_file_name (file, lh);
2e276125 21447
2e276125 21448 if (! current_file)
abc9d0dc 21449 {
fc474241
DE
21450 /* Note: We don't create a macro table for this compilation unit
21451 at all until we actually get a filename. */
43f3e411 21452 struct macro_table *macro_table = get_macro_table ();
fc474241 21453
abc9d0dc
TT
21454 /* If we have no current file, then this must be the start_file
21455 directive for the compilation unit's main source file. */
fc474241
DE
21456 current_file = macro_set_main (macro_table, file_name);
21457 macro_define_special (macro_table);
abc9d0dc 21458 }
2e276125 21459 else
233d95b5 21460 current_file = macro_include (current_file, line, file_name);
2e276125 21461
233d95b5 21462 xfree (file_name);
6e70227d 21463
2e276125
JB
21464 return current_file;
21465}
21466
21467
21468/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21469 followed by a null byte. */
21470static char *
21471copy_string (const char *buf, int len)
21472{
224c3ddb 21473 char *s = (char *) xmalloc (len + 1);
9a619af0 21474
2e276125
JB
21475 memcpy (s, buf, len);
21476 s[len] = '\0';
2e276125
JB
21477 return s;
21478}
21479
21480
21481static const char *
21482consume_improper_spaces (const char *p, const char *body)
21483{
21484 if (*p == ' ')
21485 {
4d3c2250 21486 complaint (&symfile_complaints,
3e43a32a
MS
21487 _("macro definition contains spaces "
21488 "in formal argument list:\n`%s'"),
4d3c2250 21489 body);
2e276125
JB
21490
21491 while (*p == ' ')
21492 p++;
21493 }
21494
21495 return p;
21496}
21497
21498
21499static void
21500parse_macro_definition (struct macro_source_file *file, int line,
21501 const char *body)
21502{
21503 const char *p;
21504
21505 /* The body string takes one of two forms. For object-like macro
21506 definitions, it should be:
21507
21508 <macro name> " " <definition>
21509
21510 For function-like macro definitions, it should be:
21511
21512 <macro name> "() " <definition>
21513 or
21514 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21515
21516 Spaces may appear only where explicitly indicated, and in the
21517 <definition>.
21518
21519 The Dwarf 2 spec says that an object-like macro's name is always
21520 followed by a space, but versions of GCC around March 2002 omit
6e70227d 21521 the space when the macro's definition is the empty string.
2e276125
JB
21522
21523 The Dwarf 2 spec says that there should be no spaces between the
21524 formal arguments in a function-like macro's formal argument list,
21525 but versions of GCC around March 2002 include spaces after the
21526 commas. */
21527
21528
21529 /* Find the extent of the macro name. The macro name is terminated
21530 by either a space or null character (for an object-like macro) or
21531 an opening paren (for a function-like macro). */
21532 for (p = body; *p; p++)
21533 if (*p == ' ' || *p == '(')
21534 break;
21535
21536 if (*p == ' ' || *p == '\0')
21537 {
21538 /* It's an object-like macro. */
21539 int name_len = p - body;
21540 char *name = copy_string (body, name_len);
21541 const char *replacement;
21542
21543 if (*p == ' ')
21544 replacement = body + name_len + 1;
21545 else
21546 {
4d3c2250 21547 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21548 replacement = body + name_len;
21549 }
6e70227d 21550
2e276125
JB
21551 macro_define_object (file, line, name, replacement);
21552
21553 xfree (name);
21554 }
21555 else if (*p == '(')
21556 {
21557 /* It's a function-like macro. */
21558 char *name = copy_string (body, p - body);
21559 int argc = 0;
21560 int argv_size = 1;
8d749320 21561 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
21562
21563 p++;
21564
21565 p = consume_improper_spaces (p, body);
21566
21567 /* Parse the formal argument list. */
21568 while (*p && *p != ')')
21569 {
21570 /* Find the extent of the current argument name. */
21571 const char *arg_start = p;
21572
21573 while (*p && *p != ',' && *p != ')' && *p != ' ')
21574 p++;
21575
21576 if (! *p || p == arg_start)
4d3c2250 21577 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21578 else
21579 {
21580 /* Make sure argv has room for the new argument. */
21581 if (argc >= argv_size)
21582 {
21583 argv_size *= 2;
224c3ddb 21584 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
21585 }
21586
21587 argv[argc++] = copy_string (arg_start, p - arg_start);
21588 }
21589
21590 p = consume_improper_spaces (p, body);
21591
21592 /* Consume the comma, if present. */
21593 if (*p == ',')
21594 {
21595 p++;
21596
21597 p = consume_improper_spaces (p, body);
21598 }
21599 }
21600
21601 if (*p == ')')
21602 {
21603 p++;
21604
21605 if (*p == ' ')
21606 /* Perfectly formed definition, no complaints. */
21607 macro_define_function (file, line, name,
6e70227d 21608 argc, (const char **) argv,
2e276125
JB
21609 p + 1);
21610 else if (*p == '\0')
21611 {
21612 /* Complain, but do define it. */
4d3c2250 21613 dwarf2_macro_malformed_definition_complaint (body);
2e276125 21614 macro_define_function (file, line, name,
6e70227d 21615 argc, (const char **) argv,
2e276125
JB
21616 p);
21617 }
21618 else
21619 /* Just complain. */
4d3c2250 21620 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21621 }
21622 else
21623 /* Just complain. */
4d3c2250 21624 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21625
21626 xfree (name);
21627 {
21628 int i;
21629
21630 for (i = 0; i < argc; i++)
21631 xfree (argv[i]);
21632 }
21633 xfree (argv);
21634 }
21635 else
4d3c2250 21636 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
21637}
21638
cf2c3c16
TT
21639/* Skip some bytes from BYTES according to the form given in FORM.
21640 Returns the new pointer. */
2e276125 21641
d521ce57
TT
21642static const gdb_byte *
21643skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
21644 enum dwarf_form form,
21645 unsigned int offset_size,
21646 struct dwarf2_section_info *section)
2e276125 21647{
cf2c3c16 21648 unsigned int bytes_read;
2e276125 21649
cf2c3c16 21650 switch (form)
2e276125 21651 {
cf2c3c16
TT
21652 case DW_FORM_data1:
21653 case DW_FORM_flag:
21654 ++bytes;
21655 break;
21656
21657 case DW_FORM_data2:
21658 bytes += 2;
21659 break;
21660
21661 case DW_FORM_data4:
21662 bytes += 4;
21663 break;
21664
21665 case DW_FORM_data8:
21666 bytes += 8;
21667 break;
21668
0224619f
JK
21669 case DW_FORM_data16:
21670 bytes += 16;
21671 break;
21672
cf2c3c16
TT
21673 case DW_FORM_string:
21674 read_direct_string (abfd, bytes, &bytes_read);
21675 bytes += bytes_read;
21676 break;
21677
21678 case DW_FORM_sec_offset:
21679 case DW_FORM_strp:
36586728 21680 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
21681 bytes += offset_size;
21682 break;
21683
21684 case DW_FORM_block:
21685 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21686 bytes += bytes_read;
21687 break;
21688
21689 case DW_FORM_block1:
21690 bytes += 1 + read_1_byte (abfd, bytes);
21691 break;
21692 case DW_FORM_block2:
21693 bytes += 2 + read_2_bytes (abfd, bytes);
21694 break;
21695 case DW_FORM_block4:
21696 bytes += 4 + read_4_bytes (abfd, bytes);
21697 break;
21698
21699 case DW_FORM_sdata:
21700 case DW_FORM_udata:
3019eac3
DE
21701 case DW_FORM_GNU_addr_index:
21702 case DW_FORM_GNU_str_index:
d521ce57 21703 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
21704 if (bytes == NULL)
21705 {
21706 dwarf2_section_buffer_overflow_complaint (section);
21707 return NULL;
21708 }
cf2c3c16
TT
21709 break;
21710
21711 default:
21712 {
21713 complain:
21714 complaint (&symfile_complaints,
21715 _("invalid form 0x%x in `%s'"),
a32a8923 21716 form, get_section_name (section));
cf2c3c16
TT
21717 return NULL;
21718 }
2e276125
JB
21719 }
21720
cf2c3c16
TT
21721 return bytes;
21722}
757a13d0 21723
cf2c3c16
TT
21724/* A helper for dwarf_decode_macros that handles skipping an unknown
21725 opcode. Returns an updated pointer to the macro data buffer; or,
21726 on error, issues a complaint and returns NULL. */
757a13d0 21727
d521ce57 21728static const gdb_byte *
cf2c3c16 21729skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
21730 const gdb_byte **opcode_definitions,
21731 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
21732 bfd *abfd,
21733 unsigned int offset_size,
21734 struct dwarf2_section_info *section)
21735{
21736 unsigned int bytes_read, i;
21737 unsigned long arg;
d521ce57 21738 const gdb_byte *defn;
2e276125 21739
cf2c3c16 21740 if (opcode_definitions[opcode] == NULL)
2e276125 21741 {
cf2c3c16
TT
21742 complaint (&symfile_complaints,
21743 _("unrecognized DW_MACFINO opcode 0x%x"),
21744 opcode);
21745 return NULL;
21746 }
2e276125 21747
cf2c3c16
TT
21748 defn = opcode_definitions[opcode];
21749 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21750 defn += bytes_read;
2e276125 21751
cf2c3c16
TT
21752 for (i = 0; i < arg; ++i)
21753 {
aead7601
SM
21754 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21755 (enum dwarf_form) defn[i], offset_size,
f664829e 21756 section);
cf2c3c16
TT
21757 if (mac_ptr == NULL)
21758 {
21759 /* skip_form_bytes already issued the complaint. */
21760 return NULL;
21761 }
21762 }
757a13d0 21763
cf2c3c16
TT
21764 return mac_ptr;
21765}
757a13d0 21766
cf2c3c16
TT
21767/* A helper function which parses the header of a macro section.
21768 If the macro section is the extended (for now called "GNU") type,
21769 then this updates *OFFSET_SIZE. Returns a pointer to just after
21770 the header, or issues a complaint and returns NULL on error. */
757a13d0 21771
d521ce57
TT
21772static const gdb_byte *
21773dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 21774 bfd *abfd,
d521ce57 21775 const gdb_byte *mac_ptr,
cf2c3c16
TT
21776 unsigned int *offset_size,
21777 int section_is_gnu)
21778{
21779 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 21780
cf2c3c16
TT
21781 if (section_is_gnu)
21782 {
21783 unsigned int version, flags;
757a13d0 21784
cf2c3c16 21785 version = read_2_bytes (abfd, mac_ptr);
0af92d60 21786 if (version != 4 && version != 5)
cf2c3c16
TT
21787 {
21788 complaint (&symfile_complaints,
21789 _("unrecognized version `%d' in .debug_macro section"),
21790 version);
21791 return NULL;
21792 }
21793 mac_ptr += 2;
757a13d0 21794
cf2c3c16
TT
21795 flags = read_1_byte (abfd, mac_ptr);
21796 ++mac_ptr;
21797 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 21798
cf2c3c16
TT
21799 if ((flags & 2) != 0)
21800 /* We don't need the line table offset. */
21801 mac_ptr += *offset_size;
757a13d0 21802
cf2c3c16
TT
21803 /* Vendor opcode descriptions. */
21804 if ((flags & 4) != 0)
21805 {
21806 unsigned int i, count;
757a13d0 21807
cf2c3c16
TT
21808 count = read_1_byte (abfd, mac_ptr);
21809 ++mac_ptr;
21810 for (i = 0; i < count; ++i)
21811 {
21812 unsigned int opcode, bytes_read;
21813 unsigned long arg;
21814
21815 opcode = read_1_byte (abfd, mac_ptr);
21816 ++mac_ptr;
21817 opcode_definitions[opcode] = mac_ptr;
21818 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21819 mac_ptr += bytes_read;
21820 mac_ptr += arg;
21821 }
757a13d0 21822 }
cf2c3c16 21823 }
757a13d0 21824
cf2c3c16
TT
21825 return mac_ptr;
21826}
757a13d0 21827
cf2c3c16 21828/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 21829 including DW_MACRO_import. */
cf2c3c16
TT
21830
21831static void
d521ce57
TT
21832dwarf_decode_macro_bytes (bfd *abfd,
21833 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 21834 struct macro_source_file *current_file,
43f3e411 21835 struct line_header *lh,
cf2c3c16 21836 struct dwarf2_section_info *section,
36586728 21837 int section_is_gnu, int section_is_dwz,
cf2c3c16 21838 unsigned int offset_size,
8fc3fc34 21839 htab_t include_hash)
cf2c3c16 21840{
4d663531 21841 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
21842 enum dwarf_macro_record_type macinfo_type;
21843 int at_commandline;
d521ce57 21844 const gdb_byte *opcode_definitions[256];
757a13d0 21845
cf2c3c16
TT
21846 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21847 &offset_size, section_is_gnu);
21848 if (mac_ptr == NULL)
21849 {
21850 /* We already issued a complaint. */
21851 return;
21852 }
757a13d0
JK
21853
21854 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21855 GDB is still reading the definitions from command line. First
21856 DW_MACINFO_start_file will need to be ignored as it was already executed
21857 to create CURRENT_FILE for the main source holding also the command line
21858 definitions. On first met DW_MACINFO_start_file this flag is reset to
21859 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21860
21861 at_commandline = 1;
21862
21863 do
21864 {
21865 /* Do we at least have room for a macinfo type byte? */
21866 if (mac_ptr >= mac_end)
21867 {
f664829e 21868 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
21869 break;
21870 }
21871
aead7601 21872 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
21873 mac_ptr++;
21874
cf2c3c16
TT
21875 /* Note that we rely on the fact that the corresponding GNU and
21876 DWARF constants are the same. */
757a13d0
JK
21877 switch (macinfo_type)
21878 {
21879 /* A zero macinfo type indicates the end of the macro
21880 information. */
21881 case 0:
21882 break;
2e276125 21883
0af92d60
JK
21884 case DW_MACRO_define:
21885 case DW_MACRO_undef:
21886 case DW_MACRO_define_strp:
21887 case DW_MACRO_undef_strp:
21888 case DW_MACRO_define_sup:
21889 case DW_MACRO_undef_sup:
2e276125 21890 {
891d2f0b 21891 unsigned int bytes_read;
2e276125 21892 int line;
d521ce57 21893 const char *body;
cf2c3c16 21894 int is_define;
2e276125 21895
cf2c3c16
TT
21896 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21897 mac_ptr += bytes_read;
21898
0af92d60
JK
21899 if (macinfo_type == DW_MACRO_define
21900 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
21901 {
21902 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21903 mac_ptr += bytes_read;
21904 }
21905 else
21906 {
21907 LONGEST str_offset;
21908
21909 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21910 mac_ptr += offset_size;
2e276125 21911
0af92d60
JK
21912 if (macinfo_type == DW_MACRO_define_sup
21913 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 21914 || section_is_dwz)
36586728
TT
21915 {
21916 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21917
21918 body = read_indirect_string_from_dwz (dwz, str_offset);
21919 }
21920 else
21921 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
21922 }
21923
0af92d60
JK
21924 is_define = (macinfo_type == DW_MACRO_define
21925 || macinfo_type == DW_MACRO_define_strp
21926 || macinfo_type == DW_MACRO_define_sup);
2e276125 21927 if (! current_file)
757a13d0
JK
21928 {
21929 /* DWARF violation as no main source is present. */
21930 complaint (&symfile_complaints,
21931 _("debug info with no main source gives macro %s "
21932 "on line %d: %s"),
cf2c3c16
TT
21933 is_define ? _("definition") : _("undefinition"),
21934 line, body);
757a13d0
JK
21935 break;
21936 }
3e43a32a
MS
21937 if ((line == 0 && !at_commandline)
21938 || (line != 0 && at_commandline))
4d3c2250 21939 complaint (&symfile_complaints,
757a13d0
JK
21940 _("debug info gives %s macro %s with %s line %d: %s"),
21941 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 21942 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
21943 line == 0 ? _("zero") : _("non-zero"), line, body);
21944
cf2c3c16 21945 if (is_define)
757a13d0 21946 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
21947 else
21948 {
0af92d60
JK
21949 gdb_assert (macinfo_type == DW_MACRO_undef
21950 || macinfo_type == DW_MACRO_undef_strp
21951 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
21952 macro_undef (current_file, line, body);
21953 }
2e276125
JB
21954 }
21955 break;
21956
0af92d60 21957 case DW_MACRO_start_file:
2e276125 21958 {
891d2f0b 21959 unsigned int bytes_read;
2e276125
JB
21960 int line, file;
21961
21962 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21963 mac_ptr += bytes_read;
21964 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21965 mac_ptr += bytes_read;
21966
3e43a32a
MS
21967 if ((line == 0 && !at_commandline)
21968 || (line != 0 && at_commandline))
757a13d0
JK
21969 complaint (&symfile_complaints,
21970 _("debug info gives source %d included "
21971 "from %s at %s line %d"),
21972 file, at_commandline ? _("command-line") : _("file"),
21973 line == 0 ? _("zero") : _("non-zero"), line);
21974
21975 if (at_commandline)
21976 {
0af92d60 21977 /* This DW_MACRO_start_file was executed in the
cf2c3c16 21978 pass one. */
757a13d0
JK
21979 at_commandline = 0;
21980 }
21981 else
43f3e411 21982 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
21983 }
21984 break;
21985
0af92d60 21986 case DW_MACRO_end_file:
2e276125 21987 if (! current_file)
4d3c2250 21988 complaint (&symfile_complaints,
3e43a32a
MS
21989 _("macro debug info has an unmatched "
21990 "`close_file' directive"));
2e276125
JB
21991 else
21992 {
21993 current_file = current_file->included_by;
21994 if (! current_file)
21995 {
cf2c3c16 21996 enum dwarf_macro_record_type next_type;
2e276125
JB
21997
21998 /* GCC circa March 2002 doesn't produce the zero
21999 type byte marking the end of the compilation
22000 unit. Complain if it's not there, but exit no
22001 matter what. */
22002
22003 /* Do we at least have room for a macinfo type byte? */
22004 if (mac_ptr >= mac_end)
22005 {
f664829e 22006 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
22007 return;
22008 }
22009
22010 /* We don't increment mac_ptr here, so this is just
22011 a look-ahead. */
aead7601
SM
22012 next_type
22013 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22014 mac_ptr);
2e276125 22015 if (next_type != 0)
4d3c2250 22016 complaint (&symfile_complaints,
3e43a32a
MS
22017 _("no terminating 0-type entry for "
22018 "macros in `.debug_macinfo' section"));
2e276125
JB
22019
22020 return;
22021 }
22022 }
22023 break;
22024
0af92d60
JK
22025 case DW_MACRO_import:
22026 case DW_MACRO_import_sup:
cf2c3c16
TT
22027 {
22028 LONGEST offset;
8fc3fc34 22029 void **slot;
a036ba48
TT
22030 bfd *include_bfd = abfd;
22031 struct dwarf2_section_info *include_section = section;
d521ce57 22032 const gdb_byte *include_mac_end = mac_end;
a036ba48 22033 int is_dwz = section_is_dwz;
d521ce57 22034 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
22035
22036 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22037 mac_ptr += offset_size;
22038
0af92d60 22039 if (macinfo_type == DW_MACRO_import_sup)
a036ba48
TT
22040 {
22041 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22042
4d663531 22043 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 22044
a036ba48 22045 include_section = &dwz->macro;
a32a8923 22046 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
22047 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22048 is_dwz = 1;
22049 }
22050
22051 new_mac_ptr = include_section->buffer + offset;
22052 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22053
8fc3fc34
TT
22054 if (*slot != NULL)
22055 {
22056 /* This has actually happened; see
22057 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22058 complaint (&symfile_complaints,
0af92d60 22059 _("recursive DW_MACRO_import in "
8fc3fc34
TT
22060 ".debug_macro section"));
22061 }
22062 else
22063 {
d521ce57 22064 *slot = (void *) new_mac_ptr;
36586728 22065
a036ba48 22066 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
43f3e411 22067 include_mac_end, current_file, lh,
36586728 22068 section, section_is_gnu, is_dwz,
4d663531 22069 offset_size, include_hash);
8fc3fc34 22070
d521ce57 22071 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 22072 }
cf2c3c16
TT
22073 }
22074 break;
22075
2e276125 22076 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
22077 if (!section_is_gnu)
22078 {
22079 unsigned int bytes_read;
2e276125 22080
ac298888
TT
22081 /* This reads the constant, but since we don't recognize
22082 any vendor extensions, we ignore it. */
22083 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
22084 mac_ptr += bytes_read;
22085 read_direct_string (abfd, mac_ptr, &bytes_read);
22086 mac_ptr += bytes_read;
2e276125 22087
cf2c3c16
TT
22088 /* We don't recognize any vendor extensions. */
22089 break;
22090 }
22091 /* FALLTHROUGH */
22092
22093 default:
22094 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22095 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22096 section);
22097 if (mac_ptr == NULL)
22098 return;
22099 break;
2e276125 22100 }
757a13d0 22101 } while (macinfo_type != 0);
2e276125 22102}
8e19ed76 22103
cf2c3c16 22104static void
09262596 22105dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 22106 int section_is_gnu)
cf2c3c16 22107{
bb5ed363 22108 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
22109 struct line_header *lh = cu->line_header;
22110 bfd *abfd;
d521ce57 22111 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
22112 struct macro_source_file *current_file = 0;
22113 enum dwarf_macro_record_type macinfo_type;
22114 unsigned int offset_size = cu->header.offset_size;
d521ce57 22115 const gdb_byte *opcode_definitions[256];
8fc3fc34 22116 struct cleanup *cleanup;
8fc3fc34 22117 void **slot;
09262596
DE
22118 struct dwarf2_section_info *section;
22119 const char *section_name;
22120
22121 if (cu->dwo_unit != NULL)
22122 {
22123 if (section_is_gnu)
22124 {
22125 section = &cu->dwo_unit->dwo_file->sections.macro;
22126 section_name = ".debug_macro.dwo";
22127 }
22128 else
22129 {
22130 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22131 section_name = ".debug_macinfo.dwo";
22132 }
22133 }
22134 else
22135 {
22136 if (section_is_gnu)
22137 {
22138 section = &dwarf2_per_objfile->macro;
22139 section_name = ".debug_macro";
22140 }
22141 else
22142 {
22143 section = &dwarf2_per_objfile->macinfo;
22144 section_name = ".debug_macinfo";
22145 }
22146 }
cf2c3c16 22147
bb5ed363 22148 dwarf2_read_section (objfile, section);
cf2c3c16
TT
22149 if (section->buffer == NULL)
22150 {
fceca515 22151 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
22152 return;
22153 }
a32a8923 22154 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
22155
22156 /* First pass: Find the name of the base filename.
22157 This filename is needed in order to process all macros whose definition
22158 (or undefinition) comes from the command line. These macros are defined
22159 before the first DW_MACINFO_start_file entry, and yet still need to be
22160 associated to the base file.
22161
22162 To determine the base file name, we scan the macro definitions until we
22163 reach the first DW_MACINFO_start_file entry. We then initialize
22164 CURRENT_FILE accordingly so that any macro definition found before the
22165 first DW_MACINFO_start_file can still be associated to the base file. */
22166
22167 mac_ptr = section->buffer + offset;
22168 mac_end = section->buffer + section->size;
22169
22170 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22171 &offset_size, section_is_gnu);
22172 if (mac_ptr == NULL)
22173 {
22174 /* We already issued a complaint. */
22175 return;
22176 }
22177
22178 do
22179 {
22180 /* Do we at least have room for a macinfo type byte? */
22181 if (mac_ptr >= mac_end)
22182 {
22183 /* Complaint is printed during the second pass as GDB will probably
22184 stop the first pass earlier upon finding
22185 DW_MACINFO_start_file. */
22186 break;
22187 }
22188
aead7601 22189 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
22190 mac_ptr++;
22191
22192 /* Note that we rely on the fact that the corresponding GNU and
22193 DWARF constants are the same. */
22194 switch (macinfo_type)
22195 {
22196 /* A zero macinfo type indicates the end of the macro
22197 information. */
22198 case 0:
22199 break;
22200
0af92d60
JK
22201 case DW_MACRO_define:
22202 case DW_MACRO_undef:
cf2c3c16
TT
22203 /* Only skip the data by MAC_PTR. */
22204 {
22205 unsigned int bytes_read;
22206
22207 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22208 mac_ptr += bytes_read;
22209 read_direct_string (abfd, mac_ptr, &bytes_read);
22210 mac_ptr += bytes_read;
22211 }
22212 break;
22213
0af92d60 22214 case DW_MACRO_start_file:
cf2c3c16
TT
22215 {
22216 unsigned int bytes_read;
22217 int line, file;
22218
22219 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22220 mac_ptr += bytes_read;
22221 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22222 mac_ptr += bytes_read;
22223
43f3e411 22224 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
22225 }
22226 break;
22227
0af92d60 22228 case DW_MACRO_end_file:
cf2c3c16
TT
22229 /* No data to skip by MAC_PTR. */
22230 break;
22231
0af92d60
JK
22232 case DW_MACRO_define_strp:
22233 case DW_MACRO_undef_strp:
22234 case DW_MACRO_define_sup:
22235 case DW_MACRO_undef_sup:
cf2c3c16
TT
22236 {
22237 unsigned int bytes_read;
22238
22239 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22240 mac_ptr += bytes_read;
22241 mac_ptr += offset_size;
22242 }
22243 break;
22244
0af92d60
JK
22245 case DW_MACRO_import:
22246 case DW_MACRO_import_sup:
cf2c3c16 22247 /* Note that, according to the spec, a transparent include
0af92d60 22248 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
22249 skip this opcode. */
22250 mac_ptr += offset_size;
22251 break;
22252
22253 case DW_MACINFO_vendor_ext:
22254 /* Only skip the data by MAC_PTR. */
22255 if (!section_is_gnu)
22256 {
22257 unsigned int bytes_read;
22258
22259 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22260 mac_ptr += bytes_read;
22261 read_direct_string (abfd, mac_ptr, &bytes_read);
22262 mac_ptr += bytes_read;
22263 }
22264 /* FALLTHROUGH */
22265
22266 default:
22267 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 22268 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
22269 section);
22270 if (mac_ptr == NULL)
22271 return;
22272 break;
22273 }
22274 } while (macinfo_type != 0 && current_file == NULL);
22275
22276 /* Second pass: Process all entries.
22277
22278 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22279 command-line macro definitions/undefinitions. This flag is unset when we
22280 reach the first DW_MACINFO_start_file entry. */
22281
fc4007c9
TT
22282 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22283 htab_eq_pointer,
22284 NULL, xcalloc, xfree));
8fc3fc34 22285 mac_ptr = section->buffer + offset;
fc4007c9 22286 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 22287 *slot = (void *) mac_ptr;
8fc3fc34 22288 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
43f3e411 22289 current_file, lh, section,
fc4007c9
TT
22290 section_is_gnu, 0, offset_size,
22291 include_hash.get ());
cf2c3c16
TT
22292}
22293
8e19ed76 22294/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 22295 if so return true else false. */
380bca97 22296
8e19ed76 22297static int
6e5a29e1 22298attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
22299{
22300 return (attr == NULL ? 0 :
22301 attr->form == DW_FORM_block1
22302 || attr->form == DW_FORM_block2
22303 || attr->form == DW_FORM_block4
2dc7f7b3
TT
22304 || attr->form == DW_FORM_block
22305 || attr->form == DW_FORM_exprloc);
8e19ed76 22306}
4c2df51b 22307
c6a0999f
JB
22308/* Return non-zero if ATTR's value is a section offset --- classes
22309 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22310 You may use DW_UNSND (attr) to retrieve such offsets.
22311
22312 Section 7.5.4, "Attribute Encodings", explains that no attribute
22313 may have a value that belongs to more than one of these classes; it
22314 would be ambiguous if we did, because we use the same forms for all
22315 of them. */
380bca97 22316
3690dd37 22317static int
6e5a29e1 22318attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
22319{
22320 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
22321 || attr->form == DW_FORM_data8
22322 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
22323}
22324
3690dd37
JB
22325/* Return non-zero if ATTR's value falls in the 'constant' class, or
22326 zero otherwise. When this function returns true, you can apply
22327 dwarf2_get_attr_constant_value to it.
22328
22329 However, note that for some attributes you must check
22330 attr_form_is_section_offset before using this test. DW_FORM_data4
22331 and DW_FORM_data8 are members of both the constant class, and of
22332 the classes that contain offsets into other debug sections
22333 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22334 that, if an attribute's can be either a constant or one of the
22335 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
22336 taken as section offsets, not constants.
22337
22338 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22339 cannot handle that. */
380bca97 22340
3690dd37 22341static int
6e5a29e1 22342attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
22343{
22344 switch (attr->form)
22345 {
22346 case DW_FORM_sdata:
22347 case DW_FORM_udata:
22348 case DW_FORM_data1:
22349 case DW_FORM_data2:
22350 case DW_FORM_data4:
22351 case DW_FORM_data8:
22352 return 1;
22353 default:
22354 return 0;
22355 }
22356}
22357
7771576e
SA
22358
22359/* DW_ADDR is always stored already as sect_offset; despite for the forms
22360 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22361
22362static int
6e5a29e1 22363attr_form_is_ref (const struct attribute *attr)
7771576e
SA
22364{
22365 switch (attr->form)
22366 {
22367 case DW_FORM_ref_addr:
22368 case DW_FORM_ref1:
22369 case DW_FORM_ref2:
22370 case DW_FORM_ref4:
22371 case DW_FORM_ref8:
22372 case DW_FORM_ref_udata:
22373 case DW_FORM_GNU_ref_alt:
22374 return 1;
22375 default:
22376 return 0;
22377 }
22378}
22379
3019eac3
DE
22380/* Return the .debug_loc section to use for CU.
22381 For DWO files use .debug_loc.dwo. */
22382
22383static struct dwarf2_section_info *
22384cu_debug_loc_section (struct dwarf2_cu *cu)
22385{
22386 if (cu->dwo_unit)
43988095
JK
22387 {
22388 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22389
22390 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22391 }
22392 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22393 : &dwarf2_per_objfile->loc);
3019eac3
DE
22394}
22395
8cf6f0b1
TT
22396/* A helper function that fills in a dwarf2_loclist_baton. */
22397
22398static void
22399fill_in_loclist_baton (struct dwarf2_cu *cu,
22400 struct dwarf2_loclist_baton *baton,
ff39bb5e 22401 const struct attribute *attr)
8cf6f0b1 22402{
3019eac3
DE
22403 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22404
22405 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
22406
22407 baton->per_cu = cu->per_cu;
22408 gdb_assert (baton->per_cu);
22409 /* We don't know how long the location list is, but make sure we
22410 don't run off the edge of the section. */
3019eac3
DE
22411 baton->size = section->size - DW_UNSND (attr);
22412 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 22413 baton->base_address = cu->base_address;
f664829e 22414 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22415}
22416
4c2df51b 22417static void
ff39bb5e 22418dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22419 struct dwarf2_cu *cu, int is_block)
4c2df51b 22420{
bb5ed363 22421 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22422 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22423
3690dd37 22424 if (attr_form_is_section_offset (attr)
3019eac3 22425 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22426 the section. If so, fall through to the complaint in the
22427 other branch. */
3019eac3 22428 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 22429 {
0d53c4c4 22430 struct dwarf2_loclist_baton *baton;
4c2df51b 22431
8d749320 22432 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22433
8cf6f0b1 22434 fill_in_loclist_baton (cu, baton, attr);
be391dca 22435
d00adf39 22436 if (cu->base_known == 0)
0d53c4c4 22437 complaint (&symfile_complaints,
3e43a32a
MS
22438 _("Location list used without "
22439 "specifying the CU base address."));
4c2df51b 22440
f1e6e072
TT
22441 SYMBOL_ACLASS_INDEX (sym) = (is_block
22442 ? dwarf2_loclist_block_index
22443 : dwarf2_loclist_index);
0d53c4c4
DJ
22444 SYMBOL_LOCATION_BATON (sym) = baton;
22445 }
22446 else
22447 {
22448 struct dwarf2_locexpr_baton *baton;
22449
8d749320 22450 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22451 baton->per_cu = cu->per_cu;
22452 gdb_assert (baton->per_cu);
0d53c4c4
DJ
22453
22454 if (attr_form_is_block (attr))
22455 {
22456 /* Note that we're just copying the block's data pointer
22457 here, not the actual data. We're still pointing into the
6502dd73
DJ
22458 info_buffer for SYM's objfile; right now we never release
22459 that buffer, but when we do clean up properly this may
22460 need to change. */
0d53c4c4
DJ
22461 baton->size = DW_BLOCK (attr)->size;
22462 baton->data = DW_BLOCK (attr)->data;
22463 }
22464 else
22465 {
22466 dwarf2_invalid_attrib_class_complaint ("location description",
22467 SYMBOL_NATURAL_NAME (sym));
22468 baton->size = 0;
0d53c4c4 22469 }
6e70227d 22470
f1e6e072
TT
22471 SYMBOL_ACLASS_INDEX (sym) = (is_block
22472 ? dwarf2_locexpr_block_index
22473 : dwarf2_locexpr_index);
0d53c4c4
DJ
22474 SYMBOL_LOCATION_BATON (sym) = baton;
22475 }
4c2df51b 22476}
6502dd73 22477
9aa1f1e3
TT
22478/* Return the OBJFILE associated with the compilation unit CU. If CU
22479 came from a separate debuginfo file, then the master objfile is
22480 returned. */
ae0d2f24
UW
22481
22482struct objfile *
22483dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22484{
9291a0cd 22485 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
22486
22487 /* Return the master objfile, so that we can report and look up the
22488 correct file containing this variable. */
22489 if (objfile->separate_debug_objfile_backlink)
22490 objfile = objfile->separate_debug_objfile_backlink;
22491
22492 return objfile;
22493}
22494
96408a79
SA
22495/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22496 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22497 CU_HEADERP first. */
22498
22499static const struct comp_unit_head *
22500per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22501 struct dwarf2_per_cu_data *per_cu)
22502{
d521ce57 22503 const gdb_byte *info_ptr;
96408a79
SA
22504
22505 if (per_cu->cu)
22506 return &per_cu->cu->header;
22507
8a0459fd 22508 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
22509
22510 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22511 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22512 rcuh_kind::COMPILE);
96408a79
SA
22513
22514 return cu_headerp;
22515}
22516
ae0d2f24
UW
22517/* Return the address size given in the compilation unit header for CU. */
22518
98714339 22519int
ae0d2f24
UW
22520dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22521{
96408a79
SA
22522 struct comp_unit_head cu_header_local;
22523 const struct comp_unit_head *cu_headerp;
c471e790 22524
96408a79
SA
22525 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22526
22527 return cu_headerp->addr_size;
ae0d2f24
UW
22528}
22529
9eae7c52
TT
22530/* Return the offset size given in the compilation unit header for CU. */
22531
22532int
22533dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22534{
96408a79
SA
22535 struct comp_unit_head cu_header_local;
22536 const struct comp_unit_head *cu_headerp;
9c6c53f7 22537
96408a79
SA
22538 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22539
22540 return cu_headerp->offset_size;
22541}
22542
22543/* See its dwarf2loc.h declaration. */
22544
22545int
22546dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22547{
22548 struct comp_unit_head cu_header_local;
22549 const struct comp_unit_head *cu_headerp;
22550
22551 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22552
22553 if (cu_headerp->version == 2)
22554 return cu_headerp->addr_size;
22555 else
22556 return cu_headerp->offset_size;
181cebd4
JK
22557}
22558
9aa1f1e3
TT
22559/* Return the text offset of the CU. The returned offset comes from
22560 this CU's objfile. If this objfile came from a separate debuginfo
22561 file, then the offset may be different from the corresponding
22562 offset in the parent objfile. */
22563
22564CORE_ADDR
22565dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22566{
bb3fa9d0 22567 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
22568
22569 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22570}
22571
43988095
JK
22572/* Return DWARF version number of PER_CU. */
22573
22574short
22575dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22576{
22577 return per_cu->dwarf_version;
22578}
22579
348e048f
DE
22580/* Locate the .debug_info compilation unit from CU's objfile which contains
22581 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
22582
22583static struct dwarf2_per_cu_data *
b64f50a1 22584dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 22585 unsigned int offset_in_dwz,
ae038cb0
DJ
22586 struct objfile *objfile)
22587{
22588 struct dwarf2_per_cu_data *this_cu;
22589 int low, high;
36586728 22590 const sect_offset *cu_off;
ae038cb0 22591
ae038cb0
DJ
22592 low = 0;
22593 high = dwarf2_per_objfile->n_comp_units - 1;
22594 while (high > low)
22595 {
36586728 22596 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22597 int mid = low + (high - low) / 2;
9a619af0 22598
36586728
TT
22599 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22600 cu_off = &mid_cu->offset;
22601 if (mid_cu->is_dwz > offset_in_dwz
22602 || (mid_cu->is_dwz == offset_in_dwz
22603 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
22604 high = mid;
22605 else
22606 low = mid + 1;
22607 }
22608 gdb_assert (low == high);
36586728
TT
22609 this_cu = dwarf2_per_objfile->all_comp_units[low];
22610 cu_off = &this_cu->offset;
22611 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 22612 {
36586728 22613 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
22614 error (_("Dwarf Error: could not find partial DIE containing "
22615 "offset 0x%lx [in module %s]"),
b64f50a1 22616 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 22617
b64f50a1
JK
22618 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22619 <= offset.sect_off);
ae038cb0
DJ
22620 return dwarf2_per_objfile->all_comp_units[low-1];
22621 }
22622 else
22623 {
22624 this_cu = dwarf2_per_objfile->all_comp_units[low];
22625 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
22626 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22627 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22628 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
22629 return this_cu;
22630 }
22631}
22632
23745b47 22633/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22634
9816fde3 22635static void
23745b47 22636init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 22637{
9816fde3 22638 memset (cu, 0, sizeof (*cu));
23745b47
DE
22639 per_cu->cu = cu;
22640 cu->per_cu = per_cu;
22641 cu->objfile = per_cu->objfile;
93311388 22642 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
22643}
22644
22645/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22646
22647static void
95554aad
TT
22648prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22649 enum language pretend_language)
9816fde3
JK
22650{
22651 struct attribute *attr;
22652
22653 /* Set the language we're debugging. */
22654 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22655 if (attr)
22656 set_cu_language (DW_UNSND (attr), cu);
22657 else
9cded63f 22658 {
95554aad 22659 cu->language = pretend_language;
9cded63f
TT
22660 cu->language_defn = language_def (cu->language);
22661 }
dee91e82 22662
7d45c7c3 22663 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22664}
22665
ae038cb0
DJ
22666/* Release one cached compilation unit, CU. We unlink it from the tree
22667 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
22668 the caller is responsible for that.
22669 NOTE: DATA is a void * because this function is also used as a
22670 cleanup routine. */
ae038cb0
DJ
22671
22672static void
68dc6402 22673free_heap_comp_unit (void *data)
ae038cb0 22674{
9a3c8263 22675 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
ae038cb0 22676
23745b47
DE
22677 gdb_assert (cu->per_cu != NULL);
22678 cu->per_cu->cu = NULL;
ae038cb0
DJ
22679 cu->per_cu = NULL;
22680
22681 obstack_free (&cu->comp_unit_obstack, NULL);
22682
22683 xfree (cu);
22684}
22685
72bf9492 22686/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 22687 when we're finished with it. We can't free the pointer itself, but be
dee91e82 22688 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
22689
22690static void
22691free_stack_comp_unit (void *data)
22692{
9a3c8263 22693 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
72bf9492 22694
23745b47
DE
22695 gdb_assert (cu->per_cu != NULL);
22696 cu->per_cu->cu = NULL;
22697 cu->per_cu = NULL;
22698
72bf9492
DJ
22699 obstack_free (&cu->comp_unit_obstack, NULL);
22700 cu->partial_dies = NULL;
ae038cb0
DJ
22701}
22702
22703/* Free all cached compilation units. */
22704
22705static void
22706free_cached_comp_units (void *data)
22707{
22708 struct dwarf2_per_cu_data *per_cu, **last_chain;
22709
22710 per_cu = dwarf2_per_objfile->read_in_chain;
22711 last_chain = &dwarf2_per_objfile->read_in_chain;
22712 while (per_cu != NULL)
22713 {
22714 struct dwarf2_per_cu_data *next_cu;
22715
22716 next_cu = per_cu->cu->read_in_chain;
22717
68dc6402 22718 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22719 *last_chain = next_cu;
22720
22721 per_cu = next_cu;
22722 }
22723}
22724
22725/* Increase the age counter on each cached compilation unit, and free
22726 any that are too old. */
22727
22728static void
22729age_cached_comp_units (void)
22730{
22731 struct dwarf2_per_cu_data *per_cu, **last_chain;
22732
22733 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22734 per_cu = dwarf2_per_objfile->read_in_chain;
22735 while (per_cu != NULL)
22736 {
22737 per_cu->cu->last_used ++;
b4f54984 22738 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22739 dwarf2_mark (per_cu->cu);
22740 per_cu = per_cu->cu->read_in_chain;
22741 }
22742
22743 per_cu = dwarf2_per_objfile->read_in_chain;
22744 last_chain = &dwarf2_per_objfile->read_in_chain;
22745 while (per_cu != NULL)
22746 {
22747 struct dwarf2_per_cu_data *next_cu;
22748
22749 next_cu = per_cu->cu->read_in_chain;
22750
22751 if (!per_cu->cu->mark)
22752 {
68dc6402 22753 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
22754 *last_chain = next_cu;
22755 }
22756 else
22757 last_chain = &per_cu->cu->read_in_chain;
22758
22759 per_cu = next_cu;
22760 }
22761}
22762
22763/* Remove a single compilation unit from the cache. */
22764
22765static void
dee91e82 22766free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
22767{
22768 struct dwarf2_per_cu_data *per_cu, **last_chain;
22769
22770 per_cu = dwarf2_per_objfile->read_in_chain;
22771 last_chain = &dwarf2_per_objfile->read_in_chain;
22772 while (per_cu != NULL)
22773 {
22774 struct dwarf2_per_cu_data *next_cu;
22775
22776 next_cu = per_cu->cu->read_in_chain;
22777
dee91e82 22778 if (per_cu == target_per_cu)
ae038cb0 22779 {
68dc6402 22780 free_heap_comp_unit (per_cu->cu);
dee91e82 22781 per_cu->cu = NULL;
ae038cb0
DJ
22782 *last_chain = next_cu;
22783 break;
22784 }
22785 else
22786 last_chain = &per_cu->cu->read_in_chain;
22787
22788 per_cu = next_cu;
22789 }
22790}
22791
fe3e1990
DJ
22792/* Release all extra memory associated with OBJFILE. */
22793
22794void
22795dwarf2_free_objfile (struct objfile *objfile)
22796{
9a3c8263
SM
22797 dwarf2_per_objfile
22798 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22799 dwarf2_objfile_data_key);
fe3e1990
DJ
22800
22801 if (dwarf2_per_objfile == NULL)
22802 return;
22803
22804 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22805 free_cached_comp_units (NULL);
22806
7b9f3c50
DE
22807 if (dwarf2_per_objfile->quick_file_names_table)
22808 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 22809
527f3840
JK
22810 if (dwarf2_per_objfile->line_header_hash)
22811 htab_delete (dwarf2_per_objfile->line_header_hash);
22812
fe3e1990
DJ
22813 /* Everything else should be on the objfile obstack. */
22814}
22815
dee91e82
DE
22816/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22817 We store these in a hash table separate from the DIEs, and preserve them
22818 when the DIEs are flushed out of cache.
22819
22820 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 22821 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
22822 or the type may come from a DWO file. Furthermore, while it's more logical
22823 to use per_cu->section+offset, with Fission the section with the data is in
22824 the DWO file but we don't know that section at the point we need it.
22825 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22826 because we can enter the lookup routine, get_die_type_at_offset, from
22827 outside this file, and thus won't necessarily have PER_CU->cu.
22828 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 22829
dee91e82 22830struct dwarf2_per_cu_offset_and_type
1c379e20 22831{
dee91e82 22832 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 22833 sect_offset offset;
1c379e20
DJ
22834 struct type *type;
22835};
22836
dee91e82 22837/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22838
22839static hashval_t
dee91e82 22840per_cu_offset_and_type_hash (const void *item)
1c379e20 22841{
9a3c8263
SM
22842 const struct dwarf2_per_cu_offset_and_type *ofs
22843 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 22844
dee91e82 22845 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
22846}
22847
dee91e82 22848/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
22849
22850static int
dee91e82 22851per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 22852{
9a3c8263
SM
22853 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22854 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22855 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22856 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 22857
dee91e82
DE
22858 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22859 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
22860}
22861
22862/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
22863 table if necessary. For convenience, return TYPE.
22864
22865 The DIEs reading must have careful ordering to:
22866 * Not cause infite loops trying to read in DIEs as a prerequisite for
22867 reading current DIE.
22868 * Not trying to dereference contents of still incompletely read in types
22869 while reading in other DIEs.
22870 * Enable referencing still incompletely read in types just by a pointer to
22871 the type without accessing its fields.
22872
22873 Therefore caller should follow these rules:
22874 * Try to fetch any prerequisite types we may need to build this DIE type
22875 before building the type and calling set_die_type.
e71ec853 22876 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
22877 possible before fetching more types to complete the current type.
22878 * Make the type as complete as possible before fetching more types. */
1c379e20 22879
f792889a 22880static struct type *
1c379e20
DJ
22881set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22882{
dee91e82 22883 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 22884 struct objfile *objfile = cu->objfile;
3cdcd0ce
JB
22885 struct attribute *attr;
22886 struct dynamic_prop prop;
1c379e20 22887
b4ba55a1
JB
22888 /* For Ada types, make sure that the gnat-specific data is always
22889 initialized (if not already set). There are a few types where
22890 we should not be doing so, because the type-specific area is
22891 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22892 where the type-specific area is used to store the floatformat).
22893 But this is not a problem, because the gnat-specific information
22894 is actually not needed for these types. */
22895 if (need_gnat_info (cu)
22896 && TYPE_CODE (type) != TYPE_CODE_FUNC
22897 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
22898 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22899 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22900 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
22901 && !HAVE_GNAT_AUX_INFO (type))
22902 INIT_GNAT_SPECIFIC (type);
22903
3f2f83dd
KB
22904 /* Read DW_AT_allocated and set in type. */
22905 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22906 if (attr_form_is_block (attr))
22907 {
22908 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22909 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22910 }
22911 else if (attr != NULL)
22912 {
22913 complaint (&symfile_complaints,
22914 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22915 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22916 die->offset.sect_off);
22917 }
22918
22919 /* Read DW_AT_associated and set in type. */
22920 attr = dwarf2_attr (die, DW_AT_associated, cu);
22921 if (attr_form_is_block (attr))
22922 {
22923 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22924 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22925 }
22926 else if (attr != NULL)
22927 {
22928 complaint (&symfile_complaints,
22929 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22930 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22931 die->offset.sect_off);
22932 }
22933
3cdcd0ce
JB
22934 /* Read DW_AT_data_location and set in type. */
22935 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22936 if (attr_to_dynamic_prop (attr, die, cu, &prop))
93a8e227 22937 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
3cdcd0ce 22938
dee91e82 22939 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22940 {
dee91e82
DE
22941 dwarf2_per_objfile->die_type_hash =
22942 htab_create_alloc_ex (127,
22943 per_cu_offset_and_type_hash,
22944 per_cu_offset_and_type_eq,
22945 NULL,
22946 &objfile->objfile_obstack,
22947 hashtab_obstack_allocate,
22948 dummy_obstack_deallocate);
f792889a 22949 }
1c379e20 22950
dee91e82 22951 ofs.per_cu = cu->per_cu;
1c379e20
DJ
22952 ofs.offset = die->offset;
22953 ofs.type = type;
dee91e82
DE
22954 slot = (struct dwarf2_per_cu_offset_and_type **)
22955 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
22956 if (*slot)
22957 complaint (&symfile_complaints,
22958 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 22959 die->offset.sect_off);
8d749320
SM
22960 *slot = XOBNEW (&objfile->objfile_obstack,
22961 struct dwarf2_per_cu_offset_and_type);
1c379e20 22962 **slot = ofs;
f792889a 22963 return type;
1c379e20
DJ
22964}
22965
02142a6c
DE
22966/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22967 or return NULL if the die does not have a saved type. */
1c379e20
DJ
22968
22969static struct type *
b64f50a1 22970get_die_type_at_offset (sect_offset offset,
673bfd45 22971 struct dwarf2_per_cu_data *per_cu)
1c379e20 22972{
dee91e82 22973 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 22974
dee91e82 22975 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 22976 return NULL;
1c379e20 22977
dee91e82 22978 ofs.per_cu = per_cu;
673bfd45 22979 ofs.offset = offset;
9a3c8263
SM
22980 slot = ((struct dwarf2_per_cu_offset_and_type *)
22981 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
22982 if (slot)
22983 return slot->type;
22984 else
22985 return NULL;
22986}
22987
02142a6c 22988/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
22989 or return NULL if DIE does not have a saved type. */
22990
22991static struct type *
22992get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22993{
22994 return get_die_type_at_offset (die->offset, cu->per_cu);
22995}
22996
10b3939b
DJ
22997/* Add a dependence relationship from CU to REF_PER_CU. */
22998
22999static void
23000dwarf2_add_dependence (struct dwarf2_cu *cu,
23001 struct dwarf2_per_cu_data *ref_per_cu)
23002{
23003 void **slot;
23004
23005 if (cu->dependencies == NULL)
23006 cu->dependencies
23007 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23008 NULL, &cu->comp_unit_obstack,
23009 hashtab_obstack_allocate,
23010 dummy_obstack_deallocate);
23011
23012 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23013 if (*slot == NULL)
23014 *slot = ref_per_cu;
23015}
1c379e20 23016
f504f079
DE
23017/* Subroutine of dwarf2_mark to pass to htab_traverse.
23018 Set the mark field in every compilation unit in the
ae038cb0
DJ
23019 cache that we must keep because we are keeping CU. */
23020
10b3939b
DJ
23021static int
23022dwarf2_mark_helper (void **slot, void *data)
23023{
23024 struct dwarf2_per_cu_data *per_cu;
23025
23026 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23027
23028 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23029 reading of the chain. As such dependencies remain valid it is not much
23030 useful to track and undo them during QUIT cleanups. */
23031 if (per_cu->cu == NULL)
23032 return 1;
23033
10b3939b
DJ
23034 if (per_cu->cu->mark)
23035 return 1;
23036 per_cu->cu->mark = 1;
23037
23038 if (per_cu->cu->dependencies != NULL)
23039 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23040
23041 return 1;
23042}
23043
f504f079
DE
23044/* Set the mark field in CU and in every other compilation unit in the
23045 cache that we must keep because we are keeping CU. */
23046
ae038cb0
DJ
23047static void
23048dwarf2_mark (struct dwarf2_cu *cu)
23049{
23050 if (cu->mark)
23051 return;
23052 cu->mark = 1;
10b3939b
DJ
23053 if (cu->dependencies != NULL)
23054 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23055}
23056
23057static void
23058dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23059{
23060 while (per_cu)
23061 {
23062 per_cu->cu->mark = 0;
23063 per_cu = per_cu->cu->read_in_chain;
23064 }
72bf9492
DJ
23065}
23066
72bf9492
DJ
23067/* Trivial hash function for partial_die_info: the hash value of a DIE
23068 is its offset in .debug_info for this objfile. */
23069
23070static hashval_t
23071partial_die_hash (const void *item)
23072{
9a3c8263
SM
23073 const struct partial_die_info *part_die
23074 = (const struct partial_die_info *) item;
9a619af0 23075
b64f50a1 23076 return part_die->offset.sect_off;
72bf9492
DJ
23077}
23078
23079/* Trivial comparison function for partial_die_info structures: two DIEs
23080 are equal if they have the same offset. */
23081
23082static int
23083partial_die_eq (const void *item_lhs, const void *item_rhs)
23084{
9a3c8263
SM
23085 const struct partial_die_info *part_die_lhs
23086 = (const struct partial_die_info *) item_lhs;
23087 const struct partial_die_info *part_die_rhs
23088 = (const struct partial_die_info *) item_rhs;
9a619af0 23089
b64f50a1 23090 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
23091}
23092
b4f54984
DE
23093static struct cmd_list_element *set_dwarf_cmdlist;
23094static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23095
23096static void
b4f54984 23097set_dwarf_cmd (char *args, int from_tty)
ae038cb0 23098{
b4f54984 23099 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23100 gdb_stdout);
ae038cb0
DJ
23101}
23102
23103static void
b4f54984 23104show_dwarf_cmd (char *args, int from_tty)
6e70227d 23105{
b4f54984 23106 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23107}
23108
4bf44c1c 23109/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
23110
23111static void
c1bd65d0 23112dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc 23113{
9a3c8263 23114 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
8b70b953 23115 int ix;
8b70b953 23116
626f2d1c
TT
23117 /* Make sure we don't accidentally use dwarf2_per_objfile while
23118 cleaning up. */
23119 dwarf2_per_objfile = NULL;
23120
59b0c7c1
JB
23121 for (ix = 0; ix < data->n_comp_units; ++ix)
23122 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 23123
59b0c7c1 23124 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 23125 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
23126 data->all_type_units[ix]->per_cu.imported_symtabs);
23127 xfree (data->all_type_units);
95554aad 23128
8b70b953 23129 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
23130
23131 if (data->dwo_files)
23132 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
23133 if (data->dwp_file)
23134 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
23135
23136 if (data->dwz_file && data->dwz_file->dwz_bfd)
23137 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
23138}
23139
23140\f
ae2de4f8 23141/* The "save gdb-index" command. */
9291a0cd
TT
23142
23143/* The contents of the hash table we create when building the string
23144 table. */
23145struct strtab_entry
23146{
23147 offset_type offset;
23148 const char *str;
23149};
23150
559a7a62
JK
23151/* Hash function for a strtab_entry.
23152
23153 Function is used only during write_hash_table so no index format backward
23154 compatibility is needed. */
b89be57b 23155
9291a0cd
TT
23156static hashval_t
23157hash_strtab_entry (const void *e)
23158{
9a3c8263 23159 const struct strtab_entry *entry = (const struct strtab_entry *) e;
559a7a62 23160 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
23161}
23162
23163/* Equality function for a strtab_entry. */
b89be57b 23164
9291a0cd
TT
23165static int
23166eq_strtab_entry (const void *a, const void *b)
23167{
9a3c8263
SM
23168 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23169 const struct strtab_entry *eb = (const struct strtab_entry *) b;
9291a0cd
TT
23170 return !strcmp (ea->str, eb->str);
23171}
23172
23173/* Create a strtab_entry hash table. */
b89be57b 23174
9291a0cd
TT
23175static htab_t
23176create_strtab (void)
23177{
23178 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23179 xfree, xcalloc, xfree);
23180}
23181
23182/* Add a string to the constant pool. Return the string's offset in
23183 host order. */
b89be57b 23184
9291a0cd
TT
23185static offset_type
23186add_string (htab_t table, struct obstack *cpool, const char *str)
23187{
23188 void **slot;
23189 struct strtab_entry entry;
23190 struct strtab_entry *result;
23191
23192 entry.str = str;
23193 slot = htab_find_slot (table, &entry, INSERT);
23194 if (*slot)
9a3c8263 23195 result = (struct strtab_entry *) *slot;
9291a0cd
TT
23196 else
23197 {
23198 result = XNEW (struct strtab_entry);
23199 result->offset = obstack_object_size (cpool);
23200 result->str = str;
23201 obstack_grow_str0 (cpool, str);
23202 *slot = result;
23203 }
23204 return result->offset;
23205}
23206
23207/* An entry in the symbol table. */
23208struct symtab_index_entry
23209{
23210 /* The name of the symbol. */
23211 const char *name;
23212 /* The offset of the name in the constant pool. */
23213 offset_type index_offset;
23214 /* A sorted vector of the indices of all the CUs that hold an object
23215 of this name. */
23216 VEC (offset_type) *cu_indices;
23217};
23218
23219/* The symbol table. This is a power-of-2-sized hash table. */
23220struct mapped_symtab
23221{
23222 offset_type n_elements;
23223 offset_type size;
23224 struct symtab_index_entry **data;
23225};
23226
23227/* Hash function for a symtab_index_entry. */
b89be57b 23228
9291a0cd
TT
23229static hashval_t
23230hash_symtab_entry (const void *e)
23231{
9a3c8263
SM
23232 const struct symtab_index_entry *entry
23233 = (const struct symtab_index_entry *) e;
9291a0cd
TT
23234 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23235 sizeof (offset_type) * VEC_length (offset_type,
23236 entry->cu_indices),
23237 0);
23238}
23239
23240/* Equality function for a symtab_index_entry. */
b89be57b 23241
9291a0cd
TT
23242static int
23243eq_symtab_entry (const void *a, const void *b)
23244{
9a3c8263
SM
23245 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23246 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
9291a0cd
TT
23247 int len = VEC_length (offset_type, ea->cu_indices);
23248 if (len != VEC_length (offset_type, eb->cu_indices))
23249 return 0;
23250 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23251 VEC_address (offset_type, eb->cu_indices),
23252 sizeof (offset_type) * len);
23253}
23254
23255/* Destroy a symtab_index_entry. */
b89be57b 23256
9291a0cd
TT
23257static void
23258delete_symtab_entry (void *p)
23259{
9a3c8263 23260 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
9291a0cd
TT
23261 VEC_free (offset_type, entry->cu_indices);
23262 xfree (entry);
23263}
23264
23265/* Create a hash table holding symtab_index_entry objects. */
b89be57b 23266
9291a0cd 23267static htab_t
3876f04e 23268create_symbol_hash_table (void)
9291a0cd
TT
23269{
23270 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23271 delete_symtab_entry, xcalloc, xfree);
23272}
23273
23274/* Create a new mapped symtab object. */
b89be57b 23275
9291a0cd
TT
23276static struct mapped_symtab *
23277create_mapped_symtab (void)
23278{
23279 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23280 symtab->n_elements = 0;
23281 symtab->size = 1024;
23282 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23283 return symtab;
23284}
23285
23286/* Destroy a mapped_symtab. */
b89be57b 23287
9291a0cd
TT
23288static void
23289cleanup_mapped_symtab (void *p)
23290{
9a3c8263 23291 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
9291a0cd
TT
23292 /* The contents of the array are freed when the other hash table is
23293 destroyed. */
23294 xfree (symtab->data);
23295 xfree (symtab);
23296}
23297
23298/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
23299 the slot.
23300
23301 Function is used only during write_hash_table so no index format backward
23302 compatibility is needed. */
b89be57b 23303
9291a0cd
TT
23304static struct symtab_index_entry **
23305find_slot (struct mapped_symtab *symtab, const char *name)
23306{
559a7a62 23307 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
23308
23309 index = hash & (symtab->size - 1);
23310 step = ((hash * 17) & (symtab->size - 1)) | 1;
23311
23312 for (;;)
23313 {
23314 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23315 return &symtab->data[index];
23316 index = (index + step) & (symtab->size - 1);
23317 }
23318}
23319
23320/* Expand SYMTAB's hash table. */
b89be57b 23321
9291a0cd
TT
23322static void
23323hash_expand (struct mapped_symtab *symtab)
23324{
23325 offset_type old_size = symtab->size;
23326 offset_type i;
23327 struct symtab_index_entry **old_entries = symtab->data;
23328
23329 symtab->size *= 2;
23330 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23331
23332 for (i = 0; i < old_size; ++i)
23333 {
23334 if (old_entries[i])
23335 {
23336 struct symtab_index_entry **slot = find_slot (symtab,
23337 old_entries[i]->name);
23338 *slot = old_entries[i];
23339 }
23340 }
23341
23342 xfree (old_entries);
23343}
23344
156942c7
DE
23345/* Add an entry to SYMTAB. NAME is the name of the symbol.
23346 CU_INDEX is the index of the CU in which the symbol appears.
23347 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 23348
9291a0cd
TT
23349static void
23350add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 23351 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
23352 offset_type cu_index)
23353{
23354 struct symtab_index_entry **slot;
156942c7 23355 offset_type cu_index_and_attrs;
9291a0cd
TT
23356
23357 ++symtab->n_elements;
23358 if (4 * symtab->n_elements / 3 >= symtab->size)
23359 hash_expand (symtab);
23360
23361 slot = find_slot (symtab, name);
23362 if (!*slot)
23363 {
23364 *slot = XNEW (struct symtab_index_entry);
23365 (*slot)->name = name;
156942c7 23366 /* index_offset is set later. */
9291a0cd
TT
23367 (*slot)->cu_indices = NULL;
23368 }
156942c7
DE
23369
23370 cu_index_and_attrs = 0;
23371 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23372 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23373 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23374
23375 /* We don't want to record an index value twice as we want to avoid the
23376 duplication.
23377 We process all global symbols and then all static symbols
23378 (which would allow us to avoid the duplication by only having to check
23379 the last entry pushed), but a symbol could have multiple kinds in one CU.
23380 To keep things simple we don't worry about the duplication here and
23381 sort and uniqufy the list after we've processed all symbols. */
23382 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23383}
23384
23385/* qsort helper routine for uniquify_cu_indices. */
23386
23387static int
23388offset_type_compare (const void *ap, const void *bp)
23389{
23390 offset_type a = *(offset_type *) ap;
23391 offset_type b = *(offset_type *) bp;
23392
23393 return (a > b) - (b > a);
23394}
23395
23396/* Sort and remove duplicates of all symbols' cu_indices lists. */
23397
23398static void
23399uniquify_cu_indices (struct mapped_symtab *symtab)
23400{
23401 int i;
23402
23403 for (i = 0; i < symtab->size; ++i)
23404 {
23405 struct symtab_index_entry *entry = symtab->data[i];
23406
23407 if (entry
23408 && entry->cu_indices != NULL)
23409 {
23410 unsigned int next_to_insert, next_to_check;
23411 offset_type last_value;
23412
23413 qsort (VEC_address (offset_type, entry->cu_indices),
23414 VEC_length (offset_type, entry->cu_indices),
23415 sizeof (offset_type), offset_type_compare);
23416
23417 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23418 next_to_insert = 1;
23419 for (next_to_check = 1;
23420 next_to_check < VEC_length (offset_type, entry->cu_indices);
23421 ++next_to_check)
23422 {
23423 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23424 != last_value)
23425 {
23426 last_value = VEC_index (offset_type, entry->cu_indices,
23427 next_to_check);
23428 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23429 last_value);
23430 ++next_to_insert;
23431 }
23432 }
23433 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23434 }
23435 }
9291a0cd
TT
23436}
23437
23438/* Add a vector of indices to the constant pool. */
b89be57b 23439
9291a0cd 23440static offset_type
3876f04e 23441add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
23442 struct symtab_index_entry *entry)
23443{
23444 void **slot;
23445
3876f04e 23446 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
23447 if (!*slot)
23448 {
23449 offset_type len = VEC_length (offset_type, entry->cu_indices);
23450 offset_type val = MAYBE_SWAP (len);
23451 offset_type iter;
23452 int i;
23453
23454 *slot = entry;
23455 entry->index_offset = obstack_object_size (cpool);
23456
23457 obstack_grow (cpool, &val, sizeof (val));
23458 for (i = 0;
23459 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23460 ++i)
23461 {
23462 val = MAYBE_SWAP (iter);
23463 obstack_grow (cpool, &val, sizeof (val));
23464 }
23465 }
23466 else
23467 {
9a3c8263
SM
23468 struct symtab_index_entry *old_entry
23469 = (struct symtab_index_entry *) *slot;
9291a0cd
TT
23470 entry->index_offset = old_entry->index_offset;
23471 entry = old_entry;
23472 }
23473 return entry->index_offset;
23474}
23475
23476/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23477 constant pool entries going into the obstack CPOOL. */
b89be57b 23478
9291a0cd
TT
23479static void
23480write_hash_table (struct mapped_symtab *symtab,
23481 struct obstack *output, struct obstack *cpool)
23482{
23483 offset_type i;
3876f04e 23484 htab_t symbol_hash_table;
9291a0cd
TT
23485 htab_t str_table;
23486
3876f04e 23487 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 23488 str_table = create_strtab ();
3876f04e 23489
9291a0cd
TT
23490 /* We add all the index vectors to the constant pool first, to
23491 ensure alignment is ok. */
23492 for (i = 0; i < symtab->size; ++i)
23493 {
23494 if (symtab->data[i])
3876f04e 23495 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
23496 }
23497
23498 /* Now write out the hash table. */
23499 for (i = 0; i < symtab->size; ++i)
23500 {
23501 offset_type str_off, vec_off;
23502
23503 if (symtab->data[i])
23504 {
23505 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23506 vec_off = symtab->data[i]->index_offset;
23507 }
23508 else
23509 {
23510 /* While 0 is a valid constant pool index, it is not valid
23511 to have 0 for both offsets. */
23512 str_off = 0;
23513 vec_off = 0;
23514 }
23515
23516 str_off = MAYBE_SWAP (str_off);
23517 vec_off = MAYBE_SWAP (vec_off);
23518
23519 obstack_grow (output, &str_off, sizeof (str_off));
23520 obstack_grow (output, &vec_off, sizeof (vec_off));
23521 }
23522
23523 htab_delete (str_table);
3876f04e 23524 htab_delete (symbol_hash_table);
9291a0cd
TT
23525}
23526
0a5429f6
DE
23527/* Struct to map psymtab to CU index in the index file. */
23528struct psymtab_cu_index_map
23529{
23530 struct partial_symtab *psymtab;
23531 unsigned int cu_index;
23532};
23533
23534static hashval_t
23535hash_psymtab_cu_index (const void *item)
23536{
9a3c8263
SM
23537 const struct psymtab_cu_index_map *map
23538 = (const struct psymtab_cu_index_map *) item;
0a5429f6
DE
23539
23540 return htab_hash_pointer (map->psymtab);
23541}
23542
23543static int
23544eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23545{
9a3c8263
SM
23546 const struct psymtab_cu_index_map *lhs
23547 = (const struct psymtab_cu_index_map *) item_lhs;
23548 const struct psymtab_cu_index_map *rhs
23549 = (const struct psymtab_cu_index_map *) item_rhs;
0a5429f6
DE
23550
23551 return lhs->psymtab == rhs->psymtab;
23552}
23553
23554/* Helper struct for building the address table. */
23555struct addrmap_index_data
23556{
23557 struct objfile *objfile;
23558 struct obstack *addr_obstack;
23559 htab_t cu_index_htab;
23560
23561 /* Non-zero if the previous_* fields are valid.
23562 We can't write an entry until we see the next entry (since it is only then
23563 that we know the end of the entry). */
23564 int previous_valid;
23565 /* Index of the CU in the table of all CUs in the index file. */
23566 unsigned int previous_cu_index;
0963b4bd 23567 /* Start address of the CU. */
0a5429f6
DE
23568 CORE_ADDR previous_cu_start;
23569};
23570
23571/* Write an address entry to OBSTACK. */
b89be57b 23572
9291a0cd 23573static void
0a5429f6
DE
23574add_address_entry (struct objfile *objfile, struct obstack *obstack,
23575 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 23576{
0a5429f6 23577 offset_type cu_index_to_write;
948f8e3d 23578 gdb_byte addr[8];
9291a0cd
TT
23579 CORE_ADDR baseaddr;
23580
23581 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23582
0a5429f6
DE
23583 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23584 obstack_grow (obstack, addr, 8);
23585 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23586 obstack_grow (obstack, addr, 8);
23587 cu_index_to_write = MAYBE_SWAP (cu_index);
23588 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23589}
23590
23591/* Worker function for traversing an addrmap to build the address table. */
23592
23593static int
23594add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23595{
9a3c8263
SM
23596 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23597 struct partial_symtab *pst = (struct partial_symtab *) obj;
0a5429f6
DE
23598
23599 if (data->previous_valid)
23600 add_address_entry (data->objfile, data->addr_obstack,
23601 data->previous_cu_start, start_addr,
23602 data->previous_cu_index);
23603
23604 data->previous_cu_start = start_addr;
23605 if (pst != NULL)
23606 {
23607 struct psymtab_cu_index_map find_map, *map;
23608 find_map.psymtab = pst;
9a3c8263
SM
23609 map = ((struct psymtab_cu_index_map *)
23610 htab_find (data->cu_index_htab, &find_map));
0a5429f6
DE
23611 gdb_assert (map != NULL);
23612 data->previous_cu_index = map->cu_index;
23613 data->previous_valid = 1;
23614 }
23615 else
23616 data->previous_valid = 0;
23617
23618 return 0;
23619}
23620
23621/* Write OBJFILE's address map to OBSTACK.
23622 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23623 in the index file. */
23624
23625static void
23626write_address_map (struct objfile *objfile, struct obstack *obstack,
23627 htab_t cu_index_htab)
23628{
23629 struct addrmap_index_data addrmap_index_data;
23630
23631 /* When writing the address table, we have to cope with the fact that
23632 the addrmap iterator only provides the start of a region; we have to
23633 wait until the next invocation to get the start of the next region. */
23634
23635 addrmap_index_data.objfile = objfile;
23636 addrmap_index_data.addr_obstack = obstack;
23637 addrmap_index_data.cu_index_htab = cu_index_htab;
23638 addrmap_index_data.previous_valid = 0;
23639
23640 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23641 &addrmap_index_data);
23642
23643 /* It's highly unlikely the last entry (end address = 0xff...ff)
23644 is valid, but we should still handle it.
23645 The end address is recorded as the start of the next region, but that
23646 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23647 anyway. */
23648 if (addrmap_index_data.previous_valid)
23649 add_address_entry (objfile, obstack,
23650 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23651 addrmap_index_data.previous_cu_index);
9291a0cd
TT
23652}
23653
156942c7
DE
23654/* Return the symbol kind of PSYM. */
23655
23656static gdb_index_symbol_kind
23657symbol_kind (struct partial_symbol *psym)
23658{
23659 domain_enum domain = PSYMBOL_DOMAIN (psym);
23660 enum address_class aclass = PSYMBOL_CLASS (psym);
23661
23662 switch (domain)
23663 {
23664 case VAR_DOMAIN:
23665 switch (aclass)
23666 {
23667 case LOC_BLOCK:
23668 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23669 case LOC_TYPEDEF:
23670 return GDB_INDEX_SYMBOL_KIND_TYPE;
23671 case LOC_COMPUTED:
23672 case LOC_CONST_BYTES:
23673 case LOC_OPTIMIZED_OUT:
23674 case LOC_STATIC:
23675 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23676 case LOC_CONST:
23677 /* Note: It's currently impossible to recognize psyms as enum values
23678 short of reading the type info. For now punt. */
23679 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23680 default:
23681 /* There are other LOC_FOO values that one might want to classify
23682 as variables, but dwarf2read.c doesn't currently use them. */
23683 return GDB_INDEX_SYMBOL_KIND_OTHER;
23684 }
23685 case STRUCT_DOMAIN:
23686 return GDB_INDEX_SYMBOL_KIND_TYPE;
23687 default:
23688 return GDB_INDEX_SYMBOL_KIND_OTHER;
23689 }
23690}
23691
9291a0cd 23692/* Add a list of partial symbols to SYMTAB. */
b89be57b 23693
9291a0cd
TT
23694static void
23695write_psymbols (struct mapped_symtab *symtab,
987d643c 23696 htab_t psyms_seen,
9291a0cd
TT
23697 struct partial_symbol **psymp,
23698 int count,
987d643c
TT
23699 offset_type cu_index,
23700 int is_static)
9291a0cd
TT
23701{
23702 for (; count-- > 0; ++psymp)
23703 {
156942c7
DE
23704 struct partial_symbol *psym = *psymp;
23705 void **slot;
987d643c 23706
156942c7 23707 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 23708 error (_("Ada is not currently supported by the index"));
987d643c 23709
987d643c 23710 /* Only add a given psymbol once. */
156942c7 23711 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
23712 if (!*slot)
23713 {
156942c7
DE
23714 gdb_index_symbol_kind kind = symbol_kind (psym);
23715
23716 *slot = psym;
23717 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23718 is_static, kind, cu_index);
987d643c 23719 }
9291a0cd
TT
23720 }
23721}
23722
23723/* Write the contents of an ("unfinished") obstack to FILE. Throw an
23724 exception if there is an error. */
b89be57b 23725
9291a0cd
TT
23726static void
23727write_obstack (FILE *file, struct obstack *obstack)
23728{
23729 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23730 file)
23731 != obstack_object_size (obstack))
23732 error (_("couldn't data write to file"));
23733}
23734
1fd400ff
TT
23735/* A helper struct used when iterating over debug_types. */
23736struct signatured_type_index_data
23737{
23738 struct objfile *objfile;
23739 struct mapped_symtab *symtab;
23740 struct obstack *types_list;
987d643c 23741 htab_t psyms_seen;
1fd400ff
TT
23742 int cu_index;
23743};
23744
23745/* A helper function that writes a single signatured_type to an
23746 obstack. */
b89be57b 23747
1fd400ff
TT
23748static int
23749write_one_signatured_type (void **slot, void *d)
23750{
9a3c8263
SM
23751 struct signatured_type_index_data *info
23752 = (struct signatured_type_index_data *) d;
1fd400ff 23753 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 23754 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
23755 gdb_byte val[8];
23756
23757 write_psymbols (info->symtab,
987d643c 23758 info->psyms_seen,
3e43a32a
MS
23759 info->objfile->global_psymbols.list
23760 + psymtab->globals_offset,
987d643c
TT
23761 psymtab->n_global_syms, info->cu_index,
23762 0);
1fd400ff 23763 write_psymbols (info->symtab,
987d643c 23764 info->psyms_seen,
3e43a32a
MS
23765 info->objfile->static_psymbols.list
23766 + psymtab->statics_offset,
987d643c
TT
23767 psymtab->n_static_syms, info->cu_index,
23768 1);
1fd400ff 23769
b64f50a1
JK
23770 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23771 entry->per_cu.offset.sect_off);
1fd400ff 23772 obstack_grow (info->types_list, val, 8);
3019eac3
DE
23773 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23774 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
23775 obstack_grow (info->types_list, val, 8);
23776 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23777 obstack_grow (info->types_list, val, 8);
23778
23779 ++info->cu_index;
23780
23781 return 1;
23782}
23783
95554aad
TT
23784/* Recurse into all "included" dependencies and write their symbols as
23785 if they appeared in this psymtab. */
23786
23787static void
23788recursively_write_psymbols (struct objfile *objfile,
23789 struct partial_symtab *psymtab,
23790 struct mapped_symtab *symtab,
23791 htab_t psyms_seen,
23792 offset_type cu_index)
23793{
23794 int i;
23795
23796 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23797 if (psymtab->dependencies[i]->user != NULL)
23798 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23799 symtab, psyms_seen, cu_index);
23800
23801 write_psymbols (symtab,
23802 psyms_seen,
23803 objfile->global_psymbols.list + psymtab->globals_offset,
23804 psymtab->n_global_syms, cu_index,
23805 0);
23806 write_psymbols (symtab,
23807 psyms_seen,
23808 objfile->static_psymbols.list + psymtab->statics_offset,
23809 psymtab->n_static_syms, cu_index,
23810 1);
23811}
23812
9291a0cd 23813/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 23814
9291a0cd
TT
23815static void
23816write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23817{
23818 struct cleanup *cleanup;
bef155c3 23819 char *filename;
1fd400ff
TT
23820 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23821 struct obstack cu_list, types_cu_list;
9291a0cd
TT
23822 int i;
23823 FILE *out_file;
23824 struct mapped_symtab *symtab;
23825 offset_type val, size_of_contents, total_len;
23826 struct stat st;
0a5429f6 23827 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 23828
9291a0cd
TT
23829 if (dwarf2_per_objfile->using_index)
23830 error (_("Cannot use an index to create the index"));
23831
8b70b953
TT
23832 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23833 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23834
260b681b
DE
23835 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23836 return;
23837
4262abfb
JK
23838 if (stat (objfile_name (objfile), &st) < 0)
23839 perror_with_name (objfile_name (objfile));
9291a0cd 23840
4262abfb 23841 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
9291a0cd
TT
23842 INDEX_SUFFIX, (char *) NULL);
23843 cleanup = make_cleanup (xfree, filename);
23844
614c279d 23845 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
23846 if (!out_file)
23847 error (_("Can't open `%s' for writing"), filename);
23848
bef155c3 23849 gdb::unlinker unlink_file (filename);
9291a0cd
TT
23850
23851 symtab = create_mapped_symtab ();
23852 make_cleanup (cleanup_mapped_symtab, symtab);
23853
23854 obstack_init (&addr_obstack);
23855 make_cleanup_obstack_free (&addr_obstack);
23856
23857 obstack_init (&cu_list);
23858 make_cleanup_obstack_free (&cu_list);
23859
1fd400ff
TT
23860 obstack_init (&types_cu_list);
23861 make_cleanup_obstack_free (&types_cu_list);
23862
fc4007c9
TT
23863 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23864 htab_eq_pointer,
23865 NULL, xcalloc, xfree));
987d643c 23866
0a5429f6
DE
23867 /* While we're scanning CU's create a table that maps a psymtab pointer
23868 (which is what addrmap records) to its index (which is what is recorded
23869 in the index file). This will later be needed to write the address
23870 table. */
fc4007c9
TT
23871 htab_up cu_index_htab (htab_create_alloc (100,
23872 hash_psymtab_cu_index,
23873 eq_psymtab_cu_index,
23874 NULL, xcalloc, xfree));
8d749320
SM
23875 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23876 dwarf2_per_objfile->n_comp_units);
0a5429f6
DE
23877 make_cleanup (xfree, psymtab_cu_index_map);
23878
23879 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
23880 work here. Also, the debug_types entries do not appear in
23881 all_comp_units, but only in their own hash table. */
9291a0cd
TT
23882 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23883 {
3e43a32a
MS
23884 struct dwarf2_per_cu_data *per_cu
23885 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 23886 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 23887 gdb_byte val[8];
0a5429f6
DE
23888 struct psymtab_cu_index_map *map;
23889 void **slot;
9291a0cd 23890
92fac807
JK
23891 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23892 It may be referenced from a local scope but in such case it does not
23893 need to be present in .gdb_index. */
23894 if (psymtab == NULL)
23895 continue;
23896
95554aad 23897 if (psymtab->user == NULL)
fc4007c9
TT
23898 recursively_write_psymbols (objfile, psymtab, symtab,
23899 psyms_seen.get (), i);
9291a0cd 23900
0a5429f6
DE
23901 map = &psymtab_cu_index_map[i];
23902 map->psymtab = psymtab;
23903 map->cu_index = i;
fc4007c9 23904 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
0a5429f6
DE
23905 gdb_assert (slot != NULL);
23906 gdb_assert (*slot == NULL);
23907 *slot = map;
9291a0cd 23908
b64f50a1
JK
23909 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23910 per_cu->offset.sect_off);
9291a0cd 23911 obstack_grow (&cu_list, val, 8);
e254ef6a 23912 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
23913 obstack_grow (&cu_list, val, 8);
23914 }
23915
0a5429f6 23916 /* Dump the address map. */
fc4007c9 23917 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
0a5429f6 23918
1fd400ff
TT
23919 /* Write out the .debug_type entries, if any. */
23920 if (dwarf2_per_objfile->signatured_types)
23921 {
23922 struct signatured_type_index_data sig_data;
23923
23924 sig_data.objfile = objfile;
23925 sig_data.symtab = symtab;
23926 sig_data.types_list = &types_cu_list;
fc4007c9 23927 sig_data.psyms_seen = psyms_seen.get ();
1fd400ff
TT
23928 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23929 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23930 write_one_signatured_type, &sig_data);
23931 }
23932
156942c7
DE
23933 /* Now that we've processed all symbols we can shrink their cu_indices
23934 lists. */
23935 uniquify_cu_indices (symtab);
23936
9291a0cd
TT
23937 obstack_init (&constant_pool);
23938 make_cleanup_obstack_free (&constant_pool);
23939 obstack_init (&symtab_obstack);
23940 make_cleanup_obstack_free (&symtab_obstack);
23941 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23942
23943 obstack_init (&contents);
23944 make_cleanup_obstack_free (&contents);
1fd400ff 23945 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
23946 total_len = size_of_contents;
23947
23948 /* The version number. */
796a7ff8 23949 val = MAYBE_SWAP (8);
9291a0cd
TT
23950 obstack_grow (&contents, &val, sizeof (val));
23951
23952 /* The offset of the CU list from the start of the file. */
23953 val = MAYBE_SWAP (total_len);
23954 obstack_grow (&contents, &val, sizeof (val));
23955 total_len += obstack_object_size (&cu_list);
23956
1fd400ff
TT
23957 /* The offset of the types CU list from the start of the file. */
23958 val = MAYBE_SWAP (total_len);
23959 obstack_grow (&contents, &val, sizeof (val));
23960 total_len += obstack_object_size (&types_cu_list);
23961
9291a0cd
TT
23962 /* The offset of the address table from the start of the file. */
23963 val = MAYBE_SWAP (total_len);
23964 obstack_grow (&contents, &val, sizeof (val));
23965 total_len += obstack_object_size (&addr_obstack);
23966
23967 /* The offset of the symbol table from the start of the file. */
23968 val = MAYBE_SWAP (total_len);
23969 obstack_grow (&contents, &val, sizeof (val));
23970 total_len += obstack_object_size (&symtab_obstack);
23971
23972 /* The offset of the constant pool from the start of the file. */
23973 val = MAYBE_SWAP (total_len);
23974 obstack_grow (&contents, &val, sizeof (val));
23975 total_len += obstack_object_size (&constant_pool);
23976
23977 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23978
23979 write_obstack (out_file, &contents);
23980 write_obstack (out_file, &cu_list);
1fd400ff 23981 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
23982 write_obstack (out_file, &addr_obstack);
23983 write_obstack (out_file, &symtab_obstack);
23984 write_obstack (out_file, &constant_pool);
23985
23986 fclose (out_file);
23987
bef155c3
TT
23988 /* We want to keep the file. */
23989 unlink_file.keep ();
9291a0cd
TT
23990
23991 do_cleanups (cleanup);
23992}
23993
90476074
TT
23994/* Implementation of the `save gdb-index' command.
23995
23996 Note that the file format used by this command is documented in the
23997 GDB manual. Any changes here must be documented there. */
11570e71 23998
9291a0cd
TT
23999static void
24000save_gdb_index_command (char *arg, int from_tty)
24001{
24002 struct objfile *objfile;
24003
24004 if (!arg || !*arg)
96d19272 24005 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
24006
24007 ALL_OBJFILES (objfile)
24008 {
24009 struct stat st;
24010
24011 /* If the objfile does not correspond to an actual file, skip it. */
4262abfb 24012 if (stat (objfile_name (objfile), &st) < 0)
9291a0cd
TT
24013 continue;
24014
9a3c8263
SM
24015 dwarf2_per_objfile
24016 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24017 dwarf2_objfile_data_key);
9291a0cd
TT
24018 if (dwarf2_per_objfile)
24019 {
9291a0cd 24020
492d29ea 24021 TRY
9291a0cd
TT
24022 {
24023 write_psymtabs_to_index (objfile, arg);
24024 }
492d29ea
PA
24025 CATCH (except, RETURN_MASK_ERROR)
24026 {
24027 exception_fprintf (gdb_stderr, except,
24028 _("Error while writing index for `%s': "),
24029 objfile_name (objfile));
24030 }
24031 END_CATCH
9291a0cd
TT
24032 }
24033 }
dce234bc
PP
24034}
24035
9291a0cd
TT
24036\f
24037
b4f54984 24038int dwarf_always_disassemble;
9eae7c52
TT
24039
24040static void
b4f54984
DE
24041show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24042 struct cmd_list_element *c, const char *value)
9eae7c52 24043{
3e43a32a
MS
24044 fprintf_filtered (file,
24045 _("Whether to always disassemble "
24046 "DWARF expressions is %s.\n"),
9eae7c52
TT
24047 value);
24048}
24049
900e11f9
JK
24050static void
24051show_check_physname (struct ui_file *file, int from_tty,
24052 struct cmd_list_element *c, const char *value)
24053{
24054 fprintf_filtered (file,
24055 _("Whether to check \"physname\" is %s.\n"),
24056 value);
24057}
24058
6502dd73
DJ
24059void _initialize_dwarf2_read (void);
24060
24061void
24062_initialize_dwarf2_read (void)
24063{
96d19272
JK
24064 struct cmd_list_element *c;
24065
dce234bc 24066 dwarf2_objfile_data_key
c1bd65d0 24067 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 24068
b4f54984
DE
24069 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24070Set DWARF specific variables.\n\
24071Configure DWARF variables such as the cache size"),
24072 &set_dwarf_cmdlist, "maintenance set dwarf ",
ae038cb0
DJ
24073 0/*allow-unknown*/, &maintenance_set_cmdlist);
24074
b4f54984
DE
24075 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24076Show DWARF specific variables\n\
24077Show DWARF variables such as the cache size"),
24078 &show_dwarf_cmdlist, "maintenance show dwarf ",
ae038cb0
DJ
24079 0/*allow-unknown*/, &maintenance_show_cmdlist);
24080
24081 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
b4f54984
DE
24082 &dwarf_max_cache_age, _("\
24083Set the upper bound on the age of cached DWARF compilation units."), _("\
24084Show the upper bound on the age of cached DWARF compilation units."), _("\
7915a72c
AC
24085A higher limit means that cached compilation units will be stored\n\
24086in memory longer, and more total memory will be used. Zero disables\n\
24087caching, which can slow down startup."),
2c5b56ce 24088 NULL,
b4f54984
DE
24089 show_dwarf_max_cache_age,
24090 &set_dwarf_cmdlist,
24091 &show_dwarf_cmdlist);
d97bc12b 24092
9eae7c52 24093 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
b4f54984 24094 &dwarf_always_disassemble, _("\
9eae7c52
TT
24095Set whether `info address' always disassembles DWARF expressions."), _("\
24096Show whether `info address' always disassembles DWARF expressions."), _("\
24097When enabled, DWARF expressions are always printed in an assembly-like\n\
24098syntax. When disabled, expressions will be printed in a more\n\
24099conversational style, when possible."),
24100 NULL,
b4f54984
DE
24101 show_dwarf_always_disassemble,
24102 &set_dwarf_cmdlist,
24103 &show_dwarf_cmdlist);
24104
24105 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24106Set debugging of the DWARF reader."), _("\
24107Show debugging of the DWARF reader."), _("\
24108When enabled (non-zero), debugging messages are printed during DWARF\n\
73be47f5
DE
24109reading and symtab expansion. A value of 1 (one) provides basic\n\
24110information. A value greater than 1 provides more verbose information."),
45cfd468
DE
24111 NULL,
24112 NULL,
24113 &setdebuglist, &showdebuglist);
24114
b4f54984
DE
24115 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24116Set debugging of the DWARF DIE reader."), _("\
24117Show debugging of the DWARF DIE reader."), _("\
d97bc12b
DE
24118When enabled (non-zero), DIEs are dumped after they are read in.\n\
24119The value is the maximum depth to print."),
ccce17b0
YQ
24120 NULL,
24121 NULL,
24122 &setdebuglist, &showdebuglist);
9291a0cd 24123
27e0867f
DE
24124 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24125Set debugging of the dwarf line reader."), _("\
24126Show debugging of the dwarf line reader."), _("\
24127When enabled (non-zero), line number entries are dumped as they are read in.\n\
24128A value of 1 (one) provides basic information.\n\
24129A value greater than 1 provides more verbose information."),
24130 NULL,
24131 NULL,
24132 &setdebuglist, &showdebuglist);
24133
900e11f9
JK
24134 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24135Set cross-checking of \"physname\" code against demangler."), _("\
24136Show cross-checking of \"physname\" code against demangler."), _("\
24137When enabled, GDB's internal \"physname\" code is checked against\n\
24138the demangler."),
24139 NULL, show_check_physname,
24140 &setdebuglist, &showdebuglist);
24141
e615022a
DE
24142 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24143 no_class, &use_deprecated_index_sections, _("\
24144Set whether to use deprecated gdb_index sections."), _("\
24145Show whether to use deprecated gdb_index sections."), _("\
24146When enabled, deprecated .gdb_index sections are used anyway.\n\
24147Normally they are ignored either because of a missing feature or\n\
24148performance issue.\n\
24149Warning: This option must be enabled before gdb reads the file."),
24150 NULL,
24151 NULL,
24152 &setlist, &showlist);
24153
96d19272 24154 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 24155 _("\
fc1a9d6e 24156Save a gdb-index file.\n\
11570e71 24157Usage: save gdb-index DIRECTORY"),
96d19272
JK
24158 &save_cmdlist);
24159 set_cmd_completer (c, filename_completer);
f1e6e072
TT
24160
24161 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24162 &dwarf2_locexpr_funcs);
24163 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24164 &dwarf2_loclist_funcs);
24165
24166 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24167 &dwarf2_block_frame_base_locexpr_funcs);
24168 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24169 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 24170}