* progspace.c (save_current_space_and_thread): Remove unnecessary
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 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
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
4c2df51b 72
c906108c
SS
73#include <fcntl.h>
74#include "gdb_string.h"
4bdf3d34 75#include "gdb_assert.h"
c906108c 76#include <sys/types.h>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
45cfd468
DE
81/* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83static int dwarf2_read_debug = 0;
84
d97bc12b 85/* When non-zero, dump DIEs after they are read in. */
ccce17b0 86static unsigned int dwarf2_die_debug = 0;
d97bc12b 87
900e11f9
JK
88/* When non-zero, cross-check physname against demangler. */
89static int check_physname = 0;
90
481860b3 91/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 92static int use_deprecated_index_sections = 0;
481860b3 93
6502dd73
DJ
94static const struct objfile_data *dwarf2_objfile_data_key;
95
f1e6e072
TT
96/* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98static int dwarf2_locexpr_index;
99static int dwarf2_loclist_index;
100static int dwarf2_locexpr_block_index;
101static int dwarf2_loclist_block_index;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
d521ce57 106 const gdb_byte *buffer;
dce234bc 107 bfd_size_type size;
be391dca
TT
108 /* True if we have tried to read this section. */
109 int readin;
dce234bc
PP
110};
111
8b70b953
TT
112typedef struct dwarf2_section_info dwarf2_section_info_def;
113DEF_VEC_O (dwarf2_section_info_def);
114
9291a0cd
TT
115/* All offsets in the index are of this type. It must be
116 architecture-independent. */
117typedef uint32_t offset_type;
118
119DEF_VEC_I (offset_type);
120
156942c7
DE
121/* Ensure only legit values are used. */
122#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure only legit values are used. */
129#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136/* Ensure we don't use more than the alloted nuber of bits for the CU. */
137#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
9291a0cd
TT
143/* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145struct mapped_index
146{
559a7a62
JK
147 /* Index data format version. */
148 int version;
149
9291a0cd
TT
150 /* The total length of the buffer. */
151 off_t total_size;
b11b1f88 152
9291a0cd
TT
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
b11b1f88 155
9291a0cd
TT
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
b11b1f88 158
3876f04e
DE
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
b11b1f88 161
9291a0cd 162 /* Size in slots, each slot is 2 offset_types. */
3876f04e 163 offset_type symbol_table_slots;
b11b1f88 164
9291a0cd
TT
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167};
168
95554aad
TT
169typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170DEF_VEC_P (dwarf2_per_cu_ptr);
171
9cdd5dbd
DE
172/* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
6502dd73
DJ
175struct dwarf2_per_objfile
176{
dce234bc
PP
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
dce234bc
PP
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
cf2c3c16 182 struct dwarf2_section_info macro;
dce234bc
PP
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
3019eac3 185 struct dwarf2_section_info addr;
dce234bc
PP
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
9291a0cd 188 struct dwarf2_section_info gdb_index;
ae038cb0 189
8b70b953
TT
190 VEC (dwarf2_section_info_def) *types;
191
be391dca
TT
192 /* Back link. */
193 struct objfile *objfile;
194
d467dd73 195 /* Table of all the compilation units. This is used to locate
10b3939b 196 the target compilation unit of a particular reference. */
ae038cb0
DJ
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
1fd400ff 202 /* The number of .debug_types-related CUs. */
d467dd73 203 int n_type_units;
1fd400ff 204
a2ce51a0
DE
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
b4dd5633 207 struct signatured_type **all_type_units;
1fd400ff 208
f4dc4d17
DE
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
f4dc4d17
DE
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
3019eac3
DE
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
80626a55
DE
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
36586728
TT
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
72dca2f5
FR
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
9291a0cd 255
ae2de4f8
DE
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
258 unsigned char using_index;
259
ae2de4f8 260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 261 struct mapped_index *index_table;
98bfdba5 262
7b9f3c50 263 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
7b9f3c50
DE
270 htab_t quick_file_names_table;
271
98bfdba5
PA
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
673bfd45 275
dee91e82 276 /* Table mapping type DIEs to their struct type *.
673bfd45 277 This is NULL if not allocated yet.
02142a6c 278 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 279 htab_t die_type_hash;
95554aad
TT
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
283};
284
285static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 299 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
3019eac3 303 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
24d3216f
TT
306 { ".gdb_index", ".zgdb_index" },
307 23
251d32d9 308};
c906108c 309
80626a55 310/* List of DWO/DWP sections. */
3019eac3 311
80626a55 312static const struct dwop_section_names
3019eac3
DE
313{
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
09262596
DE
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
3019eac3
DE
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
80626a55
DE
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
3019eac3 325}
80626a55 326dwop_section_names =
3019eac3
DE
327{
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
339};
340
c906108c
SS
341/* local data types */
342
107d2387
AC
343/* The data in a compilation unit header, after target2host
344 translation, looks like this. */
c906108c 345struct comp_unit_head
a738430d 346{
c764a876 347 unsigned int length;
a738430d 348 short version;
a738430d
MK
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
b64f50a1 351 sect_offset abbrev_offset;
57349743 352
a738430d
MK
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
57349743 355
a738430d
MK
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
57349743 358
a738430d
MK
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
b64f50a1 361 sect_offset offset;
57349743 362
d00adf39
DE
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
b64f50a1 365 cu_offset first_die_offset;
a738430d 366};
c906108c 367
3da10d80
KS
368/* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370struct delayed_method_info
371{
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386};
387
388typedef struct delayed_method_info delayed_method_info;
389DEF_VEC_O (delayed_method_info);
390
e7c27a73
DJ
391/* Internal state when decoding a particular compilation unit. */
392struct dwarf2_cu
393{
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
d00adf39 397 /* The header of the compilation unit. */
e7c27a73 398 struct comp_unit_head header;
e142c38c 399
d00adf39
DE
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
e142c38c
DJ
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
b0f35d58
DL
410 const char *producer;
411
e142c38c
DJ
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
433df2d4
DE
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
72bf9492 427
b64f50a1
JK
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
ae038cb0
DJ
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
69d751e3 442 /* Backlink to our per_cu entry. */
ae038cb0
DJ
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
b64f50a1
JK
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
51545339 450 htab_t die_hash;
10b3939b
DJ
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
cb1df416
DJ
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
96408a79
SA
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
3019eac3
DE
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
2e3cf129
DE
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
496 ULONGEST ranges_base;
497
ae038cb0
DJ
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
8be455d7
JK
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 505 unsigned int has_loclist : 1;
ba919b58 506
1b80a9fa
JK
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
ba919b58
TT
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 513 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 514 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
521};
522
10b3939b
DJ
523/* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
28dee7f5 525 read_symtab_private field of the psymtab. */
10b3939b 526
ae038cb0
DJ
527struct dwarf2_per_cu_data
528{
36586728 529 /* The start offset and length of this compilation unit.
45452591 530 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
b64f50a1 534 sect_offset offset;
36586728 535 unsigned int length;
ae038cb0
DJ
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
c764a876 539 unsigned int queued : 1;
ae038cb0 540
0d99eb77
DE
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
0186c6a7
DE
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
3019eac3
DE
550 unsigned int is_debug_types : 1;
551
36586728
TT
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
a2ce51a0
DE
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
7ee85ab1
DE
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
567 "midflight").
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
570
3019eac3
DE
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
573 not the DWO file. */
8a0459fd 574 struct dwarf2_section_info *section;
348e048f 575
17ea53c3
JK
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
ae038cb0 578 struct dwarf2_cu *cu;
1c379e20 579
9cdd5dbd
DE
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
583 struct objfile *objfile;
584
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
587 union
588 {
589 /* The partial symbol table associated with this compilation unit,
95554aad 590 or NULL for unread partial units. */
9291a0cd
TT
591 struct partial_symtab *psymtab;
592
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
595 } v;
95554aad 596
796a7ff8
DE
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
601
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
613 indices so we only pay a price for gold generated indices.
614 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 615 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
616};
617
348e048f
DE
618/* Entry in the signatured_types hash table. */
619
620struct signatured_type
621{
42e7ad6c 622 /* The "per_cu" object of this type.
ac9ec31b 623 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
624 N.B.: This is the first member so that it's easy to convert pointers
625 between them. */
626 struct dwarf2_per_cu_data per_cu;
627
3019eac3 628 /* The type's signature. */
348e048f
DE
629 ULONGEST signature;
630
3019eac3 631 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
632 If this TU is a DWO stub and the definition lives in a DWO file
633 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
634 cu_offset type_offset_in_tu;
635
636 /* Offset in the section of the type's DIE.
637 If the definition lives in a DWO file, this is the offset in the
638 .debug_types.dwo section.
639 The value is zero until the actual value is known.
640 Zero is otherwise not a valid section offset. */
641 sect_offset type_offset_in_section;
0186c6a7
DE
642
643 /* Type units are grouped by their DW_AT_stmt_list entry so that they
644 can share them. This points to the containing symtab. */
645 struct type_unit_group *type_unit_group;
ac9ec31b
DE
646
647 /* The type.
648 The first time we encounter this type we fully read it in and install it
649 in the symbol tables. Subsequent times we only need the type. */
650 struct type *type;
a2ce51a0
DE
651
652 /* Containing DWO unit.
653 This field is valid iff per_cu.reading_dwo_directly. */
654 struct dwo_unit *dwo_unit;
348e048f
DE
655};
656
0186c6a7
DE
657typedef struct signatured_type *sig_type_ptr;
658DEF_VEC_P (sig_type_ptr);
659
094b34ac
DE
660/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
661 This includes type_unit_group and quick_file_names. */
662
663struct stmt_list_hash
664{
665 /* The DWO unit this table is from or NULL if there is none. */
666 struct dwo_unit *dwo_unit;
667
668 /* Offset in .debug_line or .debug_line.dwo. */
669 sect_offset line_offset;
670};
671
f4dc4d17
DE
672/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
673 an object of this type. */
674
675struct type_unit_group
676{
0186c6a7 677 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
678 To simplify things we create an artificial CU that "includes" all the
679 type units using this stmt_list so that the rest of the code still has
680 a "per_cu" handle on the symtab.
681 This PER_CU is recognized by having no section. */
8a0459fd 682#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
683 struct dwarf2_per_cu_data per_cu;
684
0186c6a7
DE
685 /* The TUs that share this DW_AT_stmt_list entry.
686 This is added to while parsing type units to build partial symtabs,
687 and is deleted afterwards and not used again. */
688 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
689
690 /* The primary symtab.
094b34ac
DE
691 Type units in a group needn't all be defined in the same source file,
692 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
693 struct symtab *primary_symtab;
694
094b34ac
DE
695 /* The data used to construct the hash key. */
696 struct stmt_list_hash hash;
f4dc4d17
DE
697
698 /* The number of symtabs from the line header.
699 The value here must match line_header.num_file_names. */
700 unsigned int num_symtabs;
701
702 /* The symbol tables for this TU (obtained from the files listed in
703 DW_AT_stmt_list).
704 WARNING: The order of entries here must match the order of entries
705 in the line header. After the first TU using this type_unit_group, the
706 line header for the subsequent TUs is recreated from this. This is done
707 because we need to use the same symtabs for each TU using the same
708 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
709 there's no guarantee the line header doesn't have duplicate entries. */
710 struct symtab **symtabs;
711};
712
80626a55 713/* These sections are what may appear in a DWO file. */
3019eac3
DE
714
715struct dwo_sections
716{
717 struct dwarf2_section_info abbrev;
3019eac3
DE
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
09262596
DE
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
3019eac3
DE
722 struct dwarf2_section_info str;
723 struct dwarf2_section_info str_offsets;
80626a55
DE
724 /* In the case of a virtual DWO file, these two are unused. */
725 struct dwarf2_section_info info;
3019eac3
DE
726 VEC (dwarf2_section_info_def) *types;
727};
728
c88ee1f0 729/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
730
731struct dwo_unit
732{
733 /* Backlink to the containing struct dwo_file. */
734 struct dwo_file *dwo_file;
735
736 /* The "id" that distinguishes this CU/TU.
737 .debug_info calls this "dwo_id", .debug_types calls this "signature".
738 Since signatures came first, we stick with it for consistency. */
739 ULONGEST signature;
740
741 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 742 struct dwarf2_section_info *section;
3019eac3
DE
743
744 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
745 sect_offset offset;
746 unsigned int length;
747
748 /* For types, offset in the type's DIE of the type defined by this TU. */
749 cu_offset type_offset_in_tu;
750};
751
80626a55
DE
752/* Data for one DWO file.
753 This includes virtual DWO files that have been packaged into a
754 DWP file. */
3019eac3
DE
755
756struct dwo_file
757{
0ac5b59e 758 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
759 For virtual DWO files the name is constructed from the section offsets
760 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
761 from related CU+TUs. */
0ac5b59e
DE
762 const char *dwo_name;
763
764 /* The DW_AT_comp_dir attribute. */
765 const char *comp_dir;
3019eac3 766
80626a55
DE
767 /* The bfd, when the file is open. Otherwise this is NULL.
768 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
769 bfd *dbfd;
3019eac3
DE
770
771 /* Section info for this file. */
772 struct dwo_sections sections;
773
19c3d4c9
DE
774 /* The CU in the file.
775 We only support one because having more than one requires hacking the
776 dwo_name of each to match, which is highly unlikely to happen.
777 Doing this means all TUs can share comp_dir: We also assume that
778 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
779 struct dwo_unit *cu;
3019eac3
DE
780
781 /* Table of TUs in the file.
782 Each element is a struct dwo_unit. */
783 htab_t tus;
784};
785
80626a55
DE
786/* These sections are what may appear in a DWP file. */
787
788struct dwp_sections
789{
790 struct dwarf2_section_info str;
791 struct dwarf2_section_info cu_index;
792 struct dwarf2_section_info tu_index;
793 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
794 by section number. We don't need to record them here. */
795};
796
797/* These sections are what may appear in a virtual DWO file. */
798
799struct virtual_dwo_sections
800{
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info line;
803 struct dwarf2_section_info loc;
804 struct dwarf2_section_info macinfo;
805 struct dwarf2_section_info macro;
806 struct dwarf2_section_info str_offsets;
807 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 808 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
809 struct dwarf2_section_info info_or_types;
810};
811
812/* Contents of DWP hash tables. */
813
814struct dwp_hash_table
815{
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table, *section_pool;
818};
819
820/* Data for one DWP file. */
821
822struct dwp_file
823{
824 /* Name of the file. */
825 const char *name;
826
93417882 827 /* The bfd. */
80626a55
DE
828 bfd *dbfd;
829
830 /* Section info for this file. */
831 struct dwp_sections sections;
832
833 /* Table of CUs in the file. */
834 const struct dwp_hash_table *cus;
835
836 /* Table of TUs in the file. */
837 const struct dwp_hash_table *tus;
838
839 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
840 htab_t loaded_cutus;
841
842 /* Table to map ELF section numbers to their sections. */
843 unsigned int num_sections;
844 asection **elf_sections;
845};
846
36586728
TT
847/* This represents a '.dwz' file. */
848
849struct dwz_file
850{
851 /* A dwz file can only contain a few sections. */
852 struct dwarf2_section_info abbrev;
853 struct dwarf2_section_info info;
854 struct dwarf2_section_info str;
855 struct dwarf2_section_info line;
856 struct dwarf2_section_info macro;
2ec9a5e0 857 struct dwarf2_section_info gdb_index;
36586728
TT
858
859 /* The dwz's BFD. */
860 bfd *dwz_bfd;
861};
862
0963b4bd
MS
863/* Struct used to pass misc. parameters to read_die_and_children, et
864 al. which are used for both .debug_info and .debug_types dies.
865 All parameters here are unchanging for the life of the call. This
dee91e82 866 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
867
868struct die_reader_specs
869{
dee91e82 870 /* die_section->asection->owner. */
93311388
DE
871 bfd* abfd;
872
873 /* The CU of the DIE we are parsing. */
874 struct dwarf2_cu *cu;
875
80626a55 876 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
877 struct dwo_file *dwo_file;
878
dee91e82 879 /* The section the die comes from.
3019eac3 880 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
881 struct dwarf2_section_info *die_section;
882
883 /* die_section->buffer. */
d521ce57 884 const gdb_byte *buffer;
f664829e
DE
885
886 /* The end of the buffer. */
887 const gdb_byte *buffer_end;
a2ce51a0
DE
888
889 /* The value of the DW_AT_comp_dir attribute. */
890 const char *comp_dir;
93311388
DE
891};
892
fd820528 893/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 894typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 895 const gdb_byte *info_ptr,
dee91e82
DE
896 struct die_info *comp_unit_die,
897 int has_children,
898 void *data);
899
debd256d
JB
900/* The line number information for a compilation unit (found in the
901 .debug_line section) begins with a "statement program header",
902 which contains the following information. */
903struct line_header
904{
905 unsigned int total_length;
906 unsigned short version;
907 unsigned int header_length;
908 unsigned char minimum_instruction_length;
2dc7f7b3 909 unsigned char maximum_ops_per_instruction;
debd256d
JB
910 unsigned char default_is_stmt;
911 int line_base;
912 unsigned char line_range;
913 unsigned char opcode_base;
914
915 /* standard_opcode_lengths[i] is the number of operands for the
916 standard opcode whose value is i. This means that
917 standard_opcode_lengths[0] is unused, and the last meaningful
918 element is standard_opcode_lengths[opcode_base - 1]. */
919 unsigned char *standard_opcode_lengths;
920
921 /* The include_directories table. NOTE! These strings are not
922 allocated with xmalloc; instead, they are pointers into
923 debug_line_buffer. If you try to free them, `free' will get
924 indigestion. */
925 unsigned int num_include_dirs, include_dirs_size;
d521ce57 926 const char **include_dirs;
debd256d
JB
927
928 /* The file_names table. NOTE! These strings are not allocated
929 with xmalloc; instead, they are pointers into debug_line_buffer.
930 Don't try to free them directly. */
931 unsigned int num_file_names, file_names_size;
932 struct file_entry
c906108c 933 {
d521ce57 934 const char *name;
debd256d
JB
935 unsigned int dir_index;
936 unsigned int mod_time;
937 unsigned int length;
aaa75496 938 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 939 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
940 } *file_names;
941
942 /* The start and end of the statement program following this
6502dd73 943 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 944 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 945};
c906108c
SS
946
947/* When we construct a partial symbol table entry we only
0963b4bd 948 need this much information. */
c906108c
SS
949struct partial_die_info
950 {
72bf9492 951 /* Offset of this DIE. */
b64f50a1 952 sect_offset offset;
72bf9492
DJ
953
954 /* DWARF-2 tag for this DIE. */
955 ENUM_BITFIELD(dwarf_tag) tag : 16;
956
72bf9492
DJ
957 /* Assorted flags describing the data found in this DIE. */
958 unsigned int has_children : 1;
959 unsigned int is_external : 1;
960 unsigned int is_declaration : 1;
961 unsigned int has_type : 1;
962 unsigned int has_specification : 1;
963 unsigned int has_pc_info : 1;
481860b3 964 unsigned int may_be_inlined : 1;
72bf9492
DJ
965
966 /* Flag set if the SCOPE field of this structure has been
967 computed. */
968 unsigned int scope_set : 1;
969
fa4028e9
JB
970 /* Flag set if the DIE has a byte_size attribute. */
971 unsigned int has_byte_size : 1;
972
98bfdba5
PA
973 /* Flag set if any of the DIE's children are template arguments. */
974 unsigned int has_template_arguments : 1;
975
abc72ce4
DE
976 /* Flag set if fixup_partial_die has been called on this die. */
977 unsigned int fixup_called : 1;
978
36586728
TT
979 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
980 unsigned int is_dwz : 1;
981
982 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
983 unsigned int spec_is_dwz : 1;
984
72bf9492 985 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 986 sometimes a default name for unnamed DIEs. */
15d034d0 987 const char *name;
72bf9492 988
abc72ce4
DE
989 /* The linkage name, if present. */
990 const char *linkage_name;
991
72bf9492
DJ
992 /* The scope to prepend to our children. This is generally
993 allocated on the comp_unit_obstack, so will disappear
994 when this compilation unit leaves the cache. */
15d034d0 995 const char *scope;
72bf9492 996
95554aad
TT
997 /* Some data associated with the partial DIE. The tag determines
998 which field is live. */
999 union
1000 {
1001 /* The location description associated with this DIE, if any. */
1002 struct dwarf_block *locdesc;
1003 /* The offset of an import, for DW_TAG_imported_unit. */
1004 sect_offset offset;
1005 } d;
72bf9492
DJ
1006
1007 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1008 CORE_ADDR lowpc;
1009 CORE_ADDR highpc;
72bf9492 1010
93311388 1011 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1012 DW_AT_sibling, if any. */
abc72ce4
DE
1013 /* NOTE: This member isn't strictly necessary, read_partial_die could
1014 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1015 const gdb_byte *sibling;
72bf9492
DJ
1016
1017 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1018 DW_AT_specification (or DW_AT_abstract_origin or
1019 DW_AT_extension). */
b64f50a1 1020 sect_offset spec_offset;
72bf9492
DJ
1021
1022 /* Pointers to this DIE's parent, first child, and next sibling,
1023 if any. */
1024 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1025 };
1026
0963b4bd 1027/* This data structure holds the information of an abbrev. */
c906108c
SS
1028struct abbrev_info
1029 {
1030 unsigned int number; /* number identifying abbrev */
1031 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1032 unsigned short has_children; /* boolean */
1033 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1034 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1035 struct abbrev_info *next; /* next in chain */
1036 };
1037
1038struct attr_abbrev
1039 {
9d25dd43
DE
1040 ENUM_BITFIELD(dwarf_attribute) name : 16;
1041 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1042 };
1043
433df2d4
DE
1044/* Size of abbrev_table.abbrev_hash_table. */
1045#define ABBREV_HASH_SIZE 121
1046
1047/* Top level data structure to contain an abbreviation table. */
1048
1049struct abbrev_table
1050{
f4dc4d17
DE
1051 /* Where the abbrev table came from.
1052 This is used as a sanity check when the table is used. */
433df2d4
DE
1053 sect_offset offset;
1054
1055 /* Storage for the abbrev table. */
1056 struct obstack abbrev_obstack;
1057
1058 /* Hash table of abbrevs.
1059 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1060 It could be statically allocated, but the previous code didn't so we
1061 don't either. */
1062 struct abbrev_info **abbrevs;
1063};
1064
0963b4bd 1065/* Attributes have a name and a value. */
b60c80d6
DJ
1066struct attribute
1067 {
9d25dd43 1068 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1069 ENUM_BITFIELD(dwarf_form) form : 15;
1070
1071 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1072 field should be in u.str (existing only for DW_STRING) but it is kept
1073 here for better struct attribute alignment. */
1074 unsigned int string_is_canonical : 1;
1075
b60c80d6
DJ
1076 union
1077 {
15d034d0 1078 const char *str;
b60c80d6 1079 struct dwarf_block *blk;
43bbcdc2
PH
1080 ULONGEST unsnd;
1081 LONGEST snd;
b60c80d6 1082 CORE_ADDR addr;
ac9ec31b 1083 ULONGEST signature;
b60c80d6
DJ
1084 }
1085 u;
1086 };
1087
0963b4bd 1088/* This data structure holds a complete die structure. */
c906108c
SS
1089struct die_info
1090 {
76815b17
DE
1091 /* DWARF-2 tag for this DIE. */
1092 ENUM_BITFIELD(dwarf_tag) tag : 16;
1093
1094 /* Number of attributes */
98bfdba5
PA
1095 unsigned char num_attrs;
1096
1097 /* True if we're presently building the full type name for the
1098 type derived from this DIE. */
1099 unsigned char building_fullname : 1;
76815b17
DE
1100
1101 /* Abbrev number */
1102 unsigned int abbrev;
1103
93311388 1104 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1105 sect_offset offset;
78ba4af6
JB
1106
1107 /* The dies in a compilation unit form an n-ary tree. PARENT
1108 points to this die's parent; CHILD points to the first child of
1109 this node; and all the children of a given node are chained
4950bc1c 1110 together via their SIBLING fields. */
639d11d3
DC
1111 struct die_info *child; /* Its first child, if any. */
1112 struct die_info *sibling; /* Its next sibling, if any. */
1113 struct die_info *parent; /* Its parent, if any. */
c906108c 1114
b60c80d6
DJ
1115 /* An array of attributes, with NUM_ATTRS elements. There may be
1116 zero, but it's not common and zero-sized arrays are not
1117 sufficiently portable C. */
1118 struct attribute attrs[1];
c906108c
SS
1119 };
1120
0963b4bd 1121/* Get at parts of an attribute structure. */
c906108c
SS
1122
1123#define DW_STRING(attr) ((attr)->u.str)
8285870a 1124#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1125#define DW_UNSND(attr) ((attr)->u.unsnd)
1126#define DW_BLOCK(attr) ((attr)->u.blk)
1127#define DW_SND(attr) ((attr)->u.snd)
1128#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1129#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1130
0963b4bd 1131/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1132struct dwarf_block
1133 {
56eb65bd 1134 size_t size;
1d6edc3c
JK
1135
1136 /* Valid only if SIZE is not zero. */
d521ce57 1137 const gdb_byte *data;
c906108c
SS
1138 };
1139
c906108c
SS
1140#ifndef ATTR_ALLOC_CHUNK
1141#define ATTR_ALLOC_CHUNK 4
1142#endif
1143
c906108c
SS
1144/* Allocate fields for structs, unions and enums in this size. */
1145#ifndef DW_FIELD_ALLOC_CHUNK
1146#define DW_FIELD_ALLOC_CHUNK 4
1147#endif
1148
c906108c
SS
1149/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1150 but this would require a corresponding change in unpack_field_as_long
1151 and friends. */
1152static int bits_per_byte = 8;
1153
1154/* The routines that read and process dies for a C struct or C++ class
1155 pass lists of data member fields and lists of member function fields
1156 in an instance of a field_info structure, as defined below. */
1157struct field_info
c5aa993b 1158 {
0963b4bd 1159 /* List of data member and baseclasses fields. */
c5aa993b
JM
1160 struct nextfield
1161 {
1162 struct nextfield *next;
1163 int accessibility;
1164 int virtuality;
1165 struct field field;
1166 }
7d0ccb61 1167 *fields, *baseclasses;
c906108c 1168
7d0ccb61 1169 /* Number of fields (including baseclasses). */
c5aa993b 1170 int nfields;
c906108c 1171
c5aa993b
JM
1172 /* Number of baseclasses. */
1173 int nbaseclasses;
c906108c 1174
c5aa993b
JM
1175 /* Set if the accesibility of one of the fields is not public. */
1176 int non_public_fields;
c906108c 1177
c5aa993b
JM
1178 /* Member function fields array, entries are allocated in the order they
1179 are encountered in the object file. */
1180 struct nextfnfield
1181 {
1182 struct nextfnfield *next;
1183 struct fn_field fnfield;
1184 }
1185 *fnfields;
c906108c 1186
c5aa993b
JM
1187 /* Member function fieldlist array, contains name of possibly overloaded
1188 member function, number of overloaded member functions and a pointer
1189 to the head of the member function field chain. */
1190 struct fnfieldlist
1191 {
15d034d0 1192 const char *name;
c5aa993b
JM
1193 int length;
1194 struct nextfnfield *head;
1195 }
1196 *fnfieldlists;
c906108c 1197
c5aa993b
JM
1198 /* Number of entries in the fnfieldlists array. */
1199 int nfnfields;
98751a41
JK
1200
1201 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1202 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1203 struct typedef_field_list
1204 {
1205 struct typedef_field field;
1206 struct typedef_field_list *next;
1207 }
1208 *typedef_field_list;
1209 unsigned typedef_field_list_count;
c5aa993b 1210 };
c906108c 1211
10b3939b
DJ
1212/* One item on the queue of compilation units to read in full symbols
1213 for. */
1214struct dwarf2_queue_item
1215{
1216 struct dwarf2_per_cu_data *per_cu;
95554aad 1217 enum language pretend_language;
10b3939b
DJ
1218 struct dwarf2_queue_item *next;
1219};
1220
1221/* The current queue. */
1222static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1223
ae038cb0
DJ
1224/* Loaded secondary compilation units are kept in memory until they
1225 have not been referenced for the processing of this many
1226 compilation units. Set this to zero to disable caching. Cache
1227 sizes of up to at least twenty will improve startup time for
1228 typical inter-CU-reference binaries, at an obvious memory cost. */
1229static int dwarf2_max_cache_age = 5;
920d2a44
AC
1230static void
1231show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1232 struct cmd_list_element *c, const char *value)
1233{
3e43a32a
MS
1234 fprintf_filtered (file, _("The upper bound on the age of cached "
1235 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1236 value);
1237}
1238
ae038cb0 1239
0963b4bd 1240/* Various complaints about symbol reading that don't abort the process. */
c906108c 1241
4d3c2250
KB
1242static void
1243dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1244{
4d3c2250 1245 complaint (&symfile_complaints,
e2e0b3e5 1246 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1247}
1248
25e43795
DJ
1249static void
1250dwarf2_debug_line_missing_file_complaint (void)
1251{
1252 complaint (&symfile_complaints,
1253 _(".debug_line section has line data without a file"));
1254}
1255
59205f5a
JB
1256static void
1257dwarf2_debug_line_missing_end_sequence_complaint (void)
1258{
1259 complaint (&symfile_complaints,
3e43a32a
MS
1260 _(".debug_line section has line "
1261 "program sequence without an end"));
59205f5a
JB
1262}
1263
4d3c2250
KB
1264static void
1265dwarf2_complex_location_expr_complaint (void)
2e276125 1266{
e2e0b3e5 1267 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1268}
1269
4d3c2250
KB
1270static void
1271dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1272 int arg3)
2e276125 1273{
4d3c2250 1274 complaint (&symfile_complaints,
3e43a32a
MS
1275 _("const value length mismatch for '%s', got %d, expected %d"),
1276 arg1, arg2, arg3);
4d3c2250
KB
1277}
1278
1279static void
f664829e 1280dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1281{
4d3c2250 1282 complaint (&symfile_complaints,
f664829e
DE
1283 _("debug info runs off end of %s section"
1284 " [in module %s]"),
1285 section->asection->name,
1286 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1287}
1288
1289static void
1290dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1291{
4d3c2250 1292 complaint (&symfile_complaints,
3e43a32a
MS
1293 _("macro debug info contains a "
1294 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1295 arg1);
1296}
1297
1298static void
1299dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1300{
4d3c2250 1301 complaint (&symfile_complaints,
3e43a32a
MS
1302 _("invalid attribute class or form for '%s' in '%s'"),
1303 arg1, arg2);
4d3c2250 1304}
c906108c 1305
c906108c
SS
1306/* local function prototypes */
1307
4efb68b1 1308static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1309
918dd910
JK
1310static void dwarf2_find_base_address (struct die_info *die,
1311 struct dwarf2_cu *cu);
1312
0018ea6f
DE
1313static struct partial_symtab *create_partial_symtab
1314 (struct dwarf2_per_cu_data *per_cu, const char *name);
1315
c67a9c90 1316static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1317
72bf9492
DJ
1318static void scan_partial_symbols (struct partial_die_info *,
1319 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1320 int, struct dwarf2_cu *);
c906108c 1321
72bf9492
DJ
1322static void add_partial_symbol (struct partial_die_info *,
1323 struct dwarf2_cu *);
63d06c5c 1324
72bf9492
DJ
1325static void add_partial_namespace (struct partial_die_info *pdi,
1326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1327 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1328
5d7cb8df
JK
1329static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1330 CORE_ADDR *highpc, int need_pc,
1331 struct dwarf2_cu *cu);
1332
72bf9492
DJ
1333static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1334 struct dwarf2_cu *cu);
91c24f0a 1335
bc30ff58
JB
1336static void add_partial_subprogram (struct partial_die_info *pdi,
1337 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1338 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1339
257e7a09
YQ
1340static void dwarf2_read_symtab (struct partial_symtab *,
1341 struct objfile *);
c906108c 1342
a14ed312 1343static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1344
433df2d4
DE
1345static struct abbrev_info *abbrev_table_lookup_abbrev
1346 (const struct abbrev_table *, unsigned int);
1347
1348static struct abbrev_table *abbrev_table_read_table
1349 (struct dwarf2_section_info *, sect_offset);
1350
1351static void abbrev_table_free (struct abbrev_table *);
1352
f4dc4d17
DE
1353static void abbrev_table_free_cleanup (void *);
1354
dee91e82
DE
1355static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1356 struct dwarf2_section_info *);
c906108c 1357
f3dd6933 1358static void dwarf2_free_abbrev_table (void *);
c906108c 1359
d521ce57 1360static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1361
dee91e82 1362static struct partial_die_info *load_partial_dies
d521ce57 1363 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1364
d521ce57
TT
1365static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1366 struct partial_die_info *,
1367 struct abbrev_info *,
1368 unsigned int,
1369 const gdb_byte *);
c906108c 1370
36586728 1371static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1372 struct dwarf2_cu *);
72bf9492
DJ
1373
1374static void fixup_partial_die (struct partial_die_info *,
1375 struct dwarf2_cu *);
1376
d521ce57
TT
1377static const gdb_byte *read_attribute (const struct die_reader_specs *,
1378 struct attribute *, struct attr_abbrev *,
1379 const gdb_byte *);
a8329558 1380
a1855c1d 1381static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1382
a1855c1d 1383static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1384
a1855c1d 1385static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1386
a1855c1d 1387static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1388
a1855c1d 1389static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1390
d521ce57 1391static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1392 unsigned int *);
c906108c 1393
d521ce57 1394static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1395
1396static LONGEST read_checked_initial_length_and_offset
d521ce57 1397 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1398 unsigned int *, unsigned int *);
613e1657 1399
d521ce57
TT
1400static LONGEST read_offset (bfd *, const gdb_byte *,
1401 const struct comp_unit_head *,
c764a876
DE
1402 unsigned int *);
1403
d521ce57 1404static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1405
f4dc4d17
DE
1406static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1407 sect_offset);
1408
d521ce57 1409static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1410
d521ce57 1411static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1412
d521ce57
TT
1413static const char *read_indirect_string (bfd *, const gdb_byte *,
1414 const struct comp_unit_head *,
1415 unsigned int *);
4bdf3d34 1416
d521ce57 1417static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1418
d521ce57 1419static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1420
d521ce57 1421static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1422
d521ce57
TT
1423static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1424 const gdb_byte *,
3019eac3
DE
1425 unsigned int *);
1426
d521ce57
TT
1427static const char *read_str_index (const struct die_reader_specs *reader,
1428 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1429
e142c38c 1430static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1431
e142c38c
DJ
1432static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1433 struct dwarf2_cu *);
c906108c 1434
348e048f 1435static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1436 unsigned int);
348e048f 1437
05cf31d1
JB
1438static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1439 struct dwarf2_cu *cu);
1440
e142c38c 1441static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1442
e142c38c 1443static struct die_info *die_specification (struct die_info *die,
f2f0e013 1444 struct dwarf2_cu **);
63d06c5c 1445
debd256d
JB
1446static void free_line_header (struct line_header *lh);
1447
3019eac3
DE
1448static struct line_header *dwarf_decode_line_header (unsigned int offset,
1449 struct dwarf2_cu *cu);
debd256d 1450
f3f5162e
DE
1451static void dwarf_decode_lines (struct line_header *, const char *,
1452 struct dwarf2_cu *, struct partial_symtab *,
1453 int);
c906108c 1454
d521ce57 1455static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1456
f4dc4d17 1457static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1458 const char *, const char *, CORE_ADDR);
f4dc4d17 1459
a14ed312 1460static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1461 struct dwarf2_cu *);
c906108c 1462
34eaf542
TT
1463static struct symbol *new_symbol_full (struct die_info *, struct type *,
1464 struct dwarf2_cu *, struct symbol *);
1465
ff39bb5e 1466static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1467 struct dwarf2_cu *);
c906108c 1468
ff39bb5e 1469static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1470 struct type *type,
1471 const char *name,
1472 struct obstack *obstack,
12df843f 1473 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1474 const gdb_byte **bytes,
98bfdba5 1475 struct dwarf2_locexpr_baton **baton);
2df3850c 1476
e7c27a73 1477static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1478
b4ba55a1
JB
1479static int need_gnat_info (struct dwarf2_cu *);
1480
3e43a32a
MS
1481static struct type *die_descriptive_type (struct die_info *,
1482 struct dwarf2_cu *);
b4ba55a1
JB
1483
1484static void set_descriptive_type (struct type *, struct die_info *,
1485 struct dwarf2_cu *);
1486
e7c27a73
DJ
1487static struct type *die_containing_type (struct die_info *,
1488 struct dwarf2_cu *);
c906108c 1489
ff39bb5e 1490static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1491 struct dwarf2_cu *);
c906108c 1492
f792889a 1493static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1494
673bfd45
DE
1495static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1496
0d5cff50 1497static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1498
6e70227d 1499static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1500 const char *suffix, int physname,
1501 struct dwarf2_cu *cu);
63d06c5c 1502
e7c27a73 1503static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1504
348e048f
DE
1505static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1506
e7c27a73 1507static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1508
e7c27a73 1509static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1510
96408a79
SA
1511static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1512
ff013f42
JK
1513static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1514 struct dwarf2_cu *, struct partial_symtab *);
1515
a14ed312 1516static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1517 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1518 struct partial_symtab *);
c906108c 1519
fae299cd
DC
1520static void get_scope_pc_bounds (struct die_info *,
1521 CORE_ADDR *, CORE_ADDR *,
1522 struct dwarf2_cu *);
1523
801e3a5b
JB
1524static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1525 CORE_ADDR, struct dwarf2_cu *);
1526
a14ed312 1527static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1528 struct dwarf2_cu *);
c906108c 1529
a14ed312 1530static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1531 struct type *, struct dwarf2_cu *);
c906108c 1532
a14ed312 1533static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1534 struct die_info *, struct type *,
e7c27a73 1535 struct dwarf2_cu *);
c906108c 1536
a14ed312 1537static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1538 struct type *,
1539 struct dwarf2_cu *);
c906108c 1540
134d01f1 1541static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1542
e7c27a73 1543static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1544
e7c27a73 1545static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1546
5d7cb8df
JK
1547static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1548
27aa8d6a
SW
1549static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1550
f55ee35c
JK
1551static struct type *read_module_type (struct die_info *die,
1552 struct dwarf2_cu *cu);
1553
38d518c9 1554static const char *namespace_name (struct die_info *die,
e142c38c 1555 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1556
134d01f1 1557static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1558
e7c27a73 1559static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1560
6e70227d 1561static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1562 struct dwarf2_cu *);
1563
bf6af496 1564static struct die_info *read_die_and_siblings_1
d521ce57 1565 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1566 struct die_info *);
639d11d3 1567
dee91e82 1568static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1569 const gdb_byte *info_ptr,
1570 const gdb_byte **new_info_ptr,
639d11d3
DC
1571 struct die_info *parent);
1572
d521ce57
TT
1573static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1574 struct die_info **, const gdb_byte *,
1575 int *, int);
3019eac3 1576
d521ce57
TT
1577static const gdb_byte *read_full_die (const struct die_reader_specs *,
1578 struct die_info **, const gdb_byte *,
1579 int *);
93311388 1580
e7c27a73 1581static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1582
15d034d0
TT
1583static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1584 struct obstack *);
71c25dea 1585
15d034d0 1586static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1587
15d034d0 1588static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1589 struct die_info *die,
1590 struct dwarf2_cu *cu);
1591
ca69b9e6
DE
1592static const char *dwarf2_physname (const char *name, struct die_info *die,
1593 struct dwarf2_cu *cu);
1594
e142c38c 1595static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1596 struct dwarf2_cu **);
9219021c 1597
f39c6ffd 1598static const char *dwarf_tag_name (unsigned int);
c906108c 1599
f39c6ffd 1600static const char *dwarf_attr_name (unsigned int);
c906108c 1601
f39c6ffd 1602static const char *dwarf_form_name (unsigned int);
c906108c 1603
a14ed312 1604static char *dwarf_bool_name (unsigned int);
c906108c 1605
f39c6ffd 1606static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1607
f9aca02d 1608static struct die_info *sibling_die (struct die_info *);
c906108c 1609
d97bc12b
DE
1610static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1611
1612static void dump_die_for_error (struct die_info *);
1613
1614static void dump_die_1 (struct ui_file *, int level, int max_level,
1615 struct die_info *);
c906108c 1616
d97bc12b 1617/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1618
51545339 1619static void store_in_ref_table (struct die_info *,
10b3939b 1620 struct dwarf2_cu *);
c906108c 1621
ff39bb5e 1622static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1623
ff39bb5e 1624static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1625
348e048f 1626static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1627 const struct attribute *,
348e048f
DE
1628 struct dwarf2_cu **);
1629
10b3939b 1630static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1631 const struct attribute *,
f2f0e013 1632 struct dwarf2_cu **);
c906108c 1633
348e048f 1634static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1635 const struct attribute *,
348e048f
DE
1636 struct dwarf2_cu **);
1637
ac9ec31b
DE
1638static struct type *get_signatured_type (struct die_info *, ULONGEST,
1639 struct dwarf2_cu *);
1640
1641static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1642 const struct attribute *,
ac9ec31b
DE
1643 struct dwarf2_cu *);
1644
e5fe5e75 1645static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1646
52dc124a 1647static void read_signatured_type (struct signatured_type *);
348e048f 1648
f4dc4d17 1649static struct type_unit_group *get_type_unit_group
ff39bb5e 1650 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1651
1652static void build_type_unit_groups (die_reader_func_ftype *, void *);
1653
c906108c
SS
1654/* memory allocation interface */
1655
7b5a2f43 1656static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1657
b60c80d6 1658static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1659
09262596 1660static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1661 const char *, int);
2e276125 1662
6e5a29e1 1663static int attr_form_is_block (const struct attribute *);
8e19ed76 1664
6e5a29e1 1665static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1666
6e5a29e1 1667static int attr_form_is_constant (const struct attribute *);
3690dd37 1668
6e5a29e1 1669static int attr_form_is_ref (const struct attribute *);
7771576e 1670
8cf6f0b1
TT
1671static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1672 struct dwarf2_loclist_baton *baton,
ff39bb5e 1673 const struct attribute *attr);
8cf6f0b1 1674
ff39bb5e 1675static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1676 struct symbol *sym,
f1e6e072
TT
1677 struct dwarf2_cu *cu,
1678 int is_block);
4c2df51b 1679
d521ce57
TT
1680static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1681 const gdb_byte *info_ptr,
1682 struct abbrev_info *abbrev);
4bb7a0a7 1683
72bf9492
DJ
1684static void free_stack_comp_unit (void *);
1685
72bf9492
DJ
1686static hashval_t partial_die_hash (const void *item);
1687
1688static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1689
ae038cb0 1690static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1691 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1692
9816fde3 1693static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1694 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1695
1696static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1697 struct die_info *comp_unit_die,
1698 enum language pretend_language);
93311388 1699
68dc6402 1700static void free_heap_comp_unit (void *);
ae038cb0
DJ
1701
1702static void free_cached_comp_units (void *);
1703
1704static void age_cached_comp_units (void);
1705
dee91e82 1706static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1707
f792889a
DJ
1708static struct type *set_die_type (struct die_info *, struct type *,
1709 struct dwarf2_cu *);
1c379e20 1710
ae038cb0
DJ
1711static void create_all_comp_units (struct objfile *);
1712
0e50663e 1713static int create_all_type_units (struct objfile *);
1fd400ff 1714
95554aad
TT
1715static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1716 enum language);
10b3939b 1717
95554aad
TT
1718static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1719 enum language);
10b3939b 1720
f4dc4d17
DE
1721static void process_full_type_unit (struct dwarf2_per_cu_data *,
1722 enum language);
1723
10b3939b
DJ
1724static void dwarf2_add_dependence (struct dwarf2_cu *,
1725 struct dwarf2_per_cu_data *);
1726
ae038cb0
DJ
1727static void dwarf2_mark (struct dwarf2_cu *);
1728
1729static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1730
b64f50a1 1731static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1732 struct dwarf2_per_cu_data *);
673bfd45 1733
f792889a 1734static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1735
9291a0cd
TT
1736static void dwarf2_release_queue (void *dummy);
1737
95554aad
TT
1738static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1739 enum language pretend_language);
1740
a0f42c21 1741static void process_queue (void);
9291a0cd
TT
1742
1743static void find_file_and_directory (struct die_info *die,
1744 struct dwarf2_cu *cu,
15d034d0 1745 const char **name, const char **comp_dir);
9291a0cd
TT
1746
1747static char *file_full_name (int file, struct line_header *lh,
1748 const char *comp_dir);
1749
d521ce57 1750static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1751 (struct comp_unit_head *header,
1752 struct dwarf2_section_info *section,
d521ce57 1753 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1754 int is_debug_types_section);
1755
fd820528 1756static void init_cutu_and_read_dies
f4dc4d17
DE
1757 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1758 int use_existing_cu, int keep,
3019eac3
DE
1759 die_reader_func_ftype *die_reader_func, void *data);
1760
dee91e82
DE
1761static void init_cutu_and_read_dies_simple
1762 (struct dwarf2_per_cu_data *this_cu,
1763 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1764
673bfd45 1765static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1766
3019eac3
DE
1767static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1768
a2ce51a0
DE
1769static struct dwo_unit *lookup_dwo_in_dwp
1770 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1771 const char *comp_dir, ULONGEST signature, int is_debug_types);
1772
1773static struct dwp_file *get_dwp_file (void);
1774
3019eac3 1775static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1776 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1777
1778static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1779 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1780
1781static void free_dwo_file_cleanup (void *);
1782
95554aad
TT
1783static void process_cu_includes (void);
1784
1b80a9fa
JK
1785static void check_producer (struct dwarf2_cu *cu);
1786
9291a0cd
TT
1787#if WORDS_BIGENDIAN
1788
1789/* Convert VALUE between big- and little-endian. */
1790static offset_type
1791byte_swap (offset_type value)
1792{
1793 offset_type result;
1794
1795 result = (value & 0xff) << 24;
1796 result |= (value & 0xff00) << 8;
1797 result |= (value & 0xff0000) >> 8;
1798 result |= (value & 0xff000000) >> 24;
1799 return result;
1800}
1801
1802#define MAYBE_SWAP(V) byte_swap (V)
1803
1804#else
1805#define MAYBE_SWAP(V) (V)
1806#endif /* WORDS_BIGENDIAN */
1807
1808/* The suffix for an index file. */
1809#define INDEX_SUFFIX ".gdb-index"
1810
c906108c 1811/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1812 information and return true if we have enough to do something.
1813 NAMES points to the dwarf2 section names, or is NULL if the standard
1814 ELF names are used. */
c906108c
SS
1815
1816int
251d32d9
TG
1817dwarf2_has_info (struct objfile *objfile,
1818 const struct dwarf2_debug_sections *names)
c906108c 1819{
be391dca
TT
1820 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1821 if (!dwarf2_per_objfile)
1822 {
1823 /* Initialize per-objfile state. */
1824 struct dwarf2_per_objfile *data
1825 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1826
be391dca
TT
1827 memset (data, 0, sizeof (*data));
1828 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1829 dwarf2_per_objfile = data;
6502dd73 1830
251d32d9
TG
1831 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1832 (void *) names);
be391dca
TT
1833 dwarf2_per_objfile->objfile = objfile;
1834 }
1835 return (dwarf2_per_objfile->info.asection != NULL
1836 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1837}
1838
251d32d9
TG
1839/* When loading sections, we look either for uncompressed section or for
1840 compressed section names. */
233a11ab
CS
1841
1842static int
251d32d9
TG
1843section_is_p (const char *section_name,
1844 const struct dwarf2_section_names *names)
233a11ab 1845{
251d32d9
TG
1846 if (names->normal != NULL
1847 && strcmp (section_name, names->normal) == 0)
1848 return 1;
1849 if (names->compressed != NULL
1850 && strcmp (section_name, names->compressed) == 0)
1851 return 1;
1852 return 0;
233a11ab
CS
1853}
1854
c906108c
SS
1855/* This function is mapped across the sections and remembers the
1856 offset and size of each of the debugging sections we are interested
1857 in. */
1858
1859static void
251d32d9 1860dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1861{
251d32d9 1862 const struct dwarf2_debug_sections *names;
dc7650b8 1863 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1864
1865 if (vnames == NULL)
1866 names = &dwarf2_elf_names;
1867 else
1868 names = (const struct dwarf2_debug_sections *) vnames;
1869
dc7650b8
JK
1870 if ((aflag & SEC_HAS_CONTENTS) == 0)
1871 {
1872 }
1873 else if (section_is_p (sectp->name, &names->info))
c906108c 1874 {
dce234bc
PP
1875 dwarf2_per_objfile->info.asection = sectp;
1876 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1877 }
251d32d9 1878 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1879 {
dce234bc
PP
1880 dwarf2_per_objfile->abbrev.asection = sectp;
1881 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1882 }
251d32d9 1883 else if (section_is_p (sectp->name, &names->line))
c906108c 1884 {
dce234bc
PP
1885 dwarf2_per_objfile->line.asection = sectp;
1886 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1887 }
251d32d9 1888 else if (section_is_p (sectp->name, &names->loc))
c906108c 1889 {
dce234bc
PP
1890 dwarf2_per_objfile->loc.asection = sectp;
1891 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1892 }
251d32d9 1893 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1894 {
dce234bc
PP
1895 dwarf2_per_objfile->macinfo.asection = sectp;
1896 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1897 }
cf2c3c16
TT
1898 else if (section_is_p (sectp->name, &names->macro))
1899 {
1900 dwarf2_per_objfile->macro.asection = sectp;
1901 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1902 }
251d32d9 1903 else if (section_is_p (sectp->name, &names->str))
c906108c 1904 {
dce234bc
PP
1905 dwarf2_per_objfile->str.asection = sectp;
1906 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1907 }
3019eac3
DE
1908 else if (section_is_p (sectp->name, &names->addr))
1909 {
1910 dwarf2_per_objfile->addr.asection = sectp;
1911 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1912 }
251d32d9 1913 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1914 {
dce234bc
PP
1915 dwarf2_per_objfile->frame.asection = sectp;
1916 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1917 }
251d32d9 1918 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1919 {
dc7650b8
JK
1920 dwarf2_per_objfile->eh_frame.asection = sectp;
1921 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1922 }
251d32d9 1923 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1924 {
dce234bc
PP
1925 dwarf2_per_objfile->ranges.asection = sectp;
1926 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1927 }
251d32d9 1928 else if (section_is_p (sectp->name, &names->types))
348e048f 1929 {
8b70b953
TT
1930 struct dwarf2_section_info type_section;
1931
1932 memset (&type_section, 0, sizeof (type_section));
1933 type_section.asection = sectp;
1934 type_section.size = bfd_get_section_size (sectp);
1935
1936 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1937 &type_section);
348e048f 1938 }
251d32d9 1939 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1940 {
1941 dwarf2_per_objfile->gdb_index.asection = sectp;
1942 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1943 }
dce234bc 1944
72dca2f5
FR
1945 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1946 && bfd_section_vma (abfd, sectp) == 0)
1947 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1948}
1949
fceca515
DE
1950/* A helper function that decides whether a section is empty,
1951 or not present. */
9e0ac564
TT
1952
1953static int
1954dwarf2_section_empty_p (struct dwarf2_section_info *info)
1955{
1956 return info->asection == NULL || info->size == 0;
1957}
1958
3019eac3
DE
1959/* Read the contents of the section INFO.
1960 OBJFILE is the main object file, but not necessarily the file where
1961 the section comes from. E.g., for DWO files INFO->asection->owner
1962 is the bfd of the DWO file.
dce234bc 1963 If the section is compressed, uncompress it before returning. */
c906108c 1964
dce234bc
PP
1965static void
1966dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1967{
dce234bc 1968 asection *sectp = info->asection;
3019eac3 1969 bfd *abfd;
dce234bc
PP
1970 gdb_byte *buf, *retbuf;
1971 unsigned char header[4];
c906108c 1972
be391dca
TT
1973 if (info->readin)
1974 return;
dce234bc 1975 info->buffer = NULL;
be391dca 1976 info->readin = 1;
188dd5d6 1977
9e0ac564 1978 if (dwarf2_section_empty_p (info))
dce234bc 1979 return;
c906108c 1980
3019eac3
DE
1981 abfd = sectp->owner;
1982
4bf44c1c
TT
1983 /* If the section has relocations, we must read it ourselves.
1984 Otherwise we attach it to the BFD. */
1985 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1986 {
d521ce57 1987 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1988 return;
dce234bc 1989 }
dce234bc 1990
4bf44c1c
TT
1991 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1992 info->buffer = buf;
dce234bc
PP
1993
1994 /* When debugging .o files, we may need to apply relocations; see
1995 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1996 We never compress sections in .o files, so we only need to
1997 try this when the section is not compressed. */
ac8035ab 1998 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1999 if (retbuf != NULL)
2000 {
2001 info->buffer = retbuf;
2002 return;
2003 }
2004
2005 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2006 || bfd_bread (buf, info->size, abfd) != info->size)
2007 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2008 bfd_get_filename (abfd));
2009}
2010
9e0ac564
TT
2011/* A helper function that returns the size of a section in a safe way.
2012 If you are positive that the section has been read before using the
2013 size, then it is safe to refer to the dwarf2_section_info object's
2014 "size" field directly. In other cases, you must call this
2015 function, because for compressed sections the size field is not set
2016 correctly until the section has been read. */
2017
2018static bfd_size_type
2019dwarf2_section_size (struct objfile *objfile,
2020 struct dwarf2_section_info *info)
2021{
2022 if (!info->readin)
2023 dwarf2_read_section (objfile, info);
2024 return info->size;
2025}
2026
dce234bc 2027/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2028 SECTION_NAME. */
af34e669 2029
dce234bc 2030void
3017a003
TG
2031dwarf2_get_section_info (struct objfile *objfile,
2032 enum dwarf2_section_enum sect,
d521ce57 2033 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2034 bfd_size_type *sizep)
2035{
2036 struct dwarf2_per_objfile *data
2037 = objfile_data (objfile, dwarf2_objfile_data_key);
2038 struct dwarf2_section_info *info;
a3b2a86b
TT
2039
2040 /* We may see an objfile without any DWARF, in which case we just
2041 return nothing. */
2042 if (data == NULL)
2043 {
2044 *sectp = NULL;
2045 *bufp = NULL;
2046 *sizep = 0;
2047 return;
2048 }
3017a003
TG
2049 switch (sect)
2050 {
2051 case DWARF2_DEBUG_FRAME:
2052 info = &data->frame;
2053 break;
2054 case DWARF2_EH_FRAME:
2055 info = &data->eh_frame;
2056 break;
2057 default:
2058 gdb_assert_not_reached ("unexpected section");
2059 }
dce234bc 2060
9e0ac564 2061 dwarf2_read_section (objfile, info);
dce234bc
PP
2062
2063 *sectp = info->asection;
2064 *bufp = info->buffer;
2065 *sizep = info->size;
2066}
2067
36586728
TT
2068/* A helper function to find the sections for a .dwz file. */
2069
2070static void
2071locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2072{
2073 struct dwz_file *dwz_file = arg;
2074
2075 /* Note that we only support the standard ELF names, because .dwz
2076 is ELF-only (at the time of writing). */
2077 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2078 {
2079 dwz_file->abbrev.asection = sectp;
2080 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2081 }
2082 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2083 {
2084 dwz_file->info.asection = sectp;
2085 dwz_file->info.size = bfd_get_section_size (sectp);
2086 }
2087 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2088 {
2089 dwz_file->str.asection = sectp;
2090 dwz_file->str.size = bfd_get_section_size (sectp);
2091 }
2092 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2093 {
2094 dwz_file->line.asection = sectp;
2095 dwz_file->line.size = bfd_get_section_size (sectp);
2096 }
2097 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2098 {
2099 dwz_file->macro.asection = sectp;
2100 dwz_file->macro.size = bfd_get_section_size (sectp);
2101 }
2ec9a5e0
TT
2102 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2103 {
2104 dwz_file->gdb_index.asection = sectp;
2105 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2106 }
36586728
TT
2107}
2108
4db1a1dc
TT
2109/* Open the separate '.dwz' debug file, if needed. Return NULL if
2110 there is no .gnu_debugaltlink section in the file. Error if there
2111 is such a section but the file cannot be found. */
36586728
TT
2112
2113static struct dwz_file *
2114dwarf2_get_dwz_file (void)
2115{
4db1a1dc
TT
2116 bfd *dwz_bfd;
2117 char *data;
36586728
TT
2118 struct cleanup *cleanup;
2119 const char *filename;
2120 struct dwz_file *result;
4db1a1dc 2121 unsigned long buildid;
36586728
TT
2122
2123 if (dwarf2_per_objfile->dwz_file != NULL)
2124 return dwarf2_per_objfile->dwz_file;
2125
4db1a1dc
TT
2126 bfd_set_error (bfd_error_no_error);
2127 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2128 &buildid);
2129 if (data == NULL)
2130 {
2131 if (bfd_get_error () == bfd_error_no_error)
2132 return NULL;
2133 error (_("could not read '.gnu_debugaltlink' section: %s"),
2134 bfd_errmsg (bfd_get_error ()));
2135 }
36586728
TT
2136 cleanup = make_cleanup (xfree, data);
2137
f9d83a0b 2138 filename = (const char *) data;
36586728
TT
2139 if (!IS_ABSOLUTE_PATH (filename))
2140 {
2141 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2142 char *rel;
2143
2144 make_cleanup (xfree, abs);
2145 abs = ldirname (abs);
2146 make_cleanup (xfree, abs);
2147
2148 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2149 make_cleanup (xfree, rel);
2150 filename = rel;
2151 }
2152
2153 /* The format is just a NUL-terminated file name, followed by the
2154 build-id. For now, though, we ignore the build-id. */
2155 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2156 if (dwz_bfd == NULL)
2157 error (_("could not read '%s': %s"), filename,
2158 bfd_errmsg (bfd_get_error ()));
2159
2160 if (!bfd_check_format (dwz_bfd, bfd_object))
2161 {
2162 gdb_bfd_unref (dwz_bfd);
2163 error (_("file '%s' was not usable: %s"), filename,
2164 bfd_errmsg (bfd_get_error ()));
2165 }
2166
2167 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2168 struct dwz_file);
2169 result->dwz_bfd = dwz_bfd;
2170
2171 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2172
2173 do_cleanups (cleanup);
2174
8d2cc612 2175 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2176 return result;
2177}
9291a0cd 2178\f
7b9f3c50
DE
2179/* DWARF quick_symbols_functions support. */
2180
2181/* TUs can share .debug_line entries, and there can be a lot more TUs than
2182 unique line tables, so we maintain a separate table of all .debug_line
2183 derived entries to support the sharing.
2184 All the quick functions need is the list of file names. We discard the
2185 line_header when we're done and don't need to record it here. */
2186struct quick_file_names
2187{
094b34ac
DE
2188 /* The data used to construct the hash key. */
2189 struct stmt_list_hash hash;
7b9f3c50
DE
2190
2191 /* The number of entries in file_names, real_names. */
2192 unsigned int num_file_names;
2193
2194 /* The file names from the line table, after being run through
2195 file_full_name. */
2196 const char **file_names;
2197
2198 /* The file names from the line table after being run through
2199 gdb_realpath. These are computed lazily. */
2200 const char **real_names;
2201};
2202
2203/* When using the index (and thus not using psymtabs), each CU has an
2204 object of this type. This is used to hold information needed by
2205 the various "quick" methods. */
2206struct dwarf2_per_cu_quick_data
2207{
2208 /* The file table. This can be NULL if there was no file table
2209 or it's currently not read in.
2210 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2211 struct quick_file_names *file_names;
2212
2213 /* The corresponding symbol table. This is NULL if symbols for this
2214 CU have not yet been read. */
2215 struct symtab *symtab;
2216
2217 /* A temporary mark bit used when iterating over all CUs in
2218 expand_symtabs_matching. */
2219 unsigned int mark : 1;
2220
2221 /* True if we've tried to read the file table and found there isn't one.
2222 There will be no point in trying to read it again next time. */
2223 unsigned int no_file_data : 1;
2224};
2225
094b34ac
DE
2226/* Utility hash function for a stmt_list_hash. */
2227
2228static hashval_t
2229hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2230{
2231 hashval_t v = 0;
2232
2233 if (stmt_list_hash->dwo_unit != NULL)
2234 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2235 v += stmt_list_hash->line_offset.sect_off;
2236 return v;
2237}
2238
2239/* Utility equality function for a stmt_list_hash. */
2240
2241static int
2242eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2243 const struct stmt_list_hash *rhs)
2244{
2245 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2246 return 0;
2247 if (lhs->dwo_unit != NULL
2248 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2249 return 0;
2250
2251 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2252}
2253
7b9f3c50
DE
2254/* Hash function for a quick_file_names. */
2255
2256static hashval_t
2257hash_file_name_entry (const void *e)
2258{
2259 const struct quick_file_names *file_data = e;
2260
094b34ac 2261 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2262}
2263
2264/* Equality function for a quick_file_names. */
2265
2266static int
2267eq_file_name_entry (const void *a, const void *b)
2268{
2269 const struct quick_file_names *ea = a;
2270 const struct quick_file_names *eb = b;
2271
094b34ac 2272 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2273}
2274
2275/* Delete function for a quick_file_names. */
2276
2277static void
2278delete_file_name_entry (void *e)
2279{
2280 struct quick_file_names *file_data = e;
2281 int i;
2282
2283 for (i = 0; i < file_data->num_file_names; ++i)
2284 {
2285 xfree ((void*) file_data->file_names[i]);
2286 if (file_data->real_names)
2287 xfree ((void*) file_data->real_names[i]);
2288 }
2289
2290 /* The space for the struct itself lives on objfile_obstack,
2291 so we don't free it here. */
2292}
2293
2294/* Create a quick_file_names hash table. */
2295
2296static htab_t
2297create_quick_file_names_table (unsigned int nr_initial_entries)
2298{
2299 return htab_create_alloc (nr_initial_entries,
2300 hash_file_name_entry, eq_file_name_entry,
2301 delete_file_name_entry, xcalloc, xfree);
2302}
9291a0cd 2303
918dd910
JK
2304/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2305 have to be created afterwards. You should call age_cached_comp_units after
2306 processing PER_CU->CU. dw2_setup must have been already called. */
2307
2308static void
2309load_cu (struct dwarf2_per_cu_data *per_cu)
2310{
3019eac3 2311 if (per_cu->is_debug_types)
e5fe5e75 2312 load_full_type_unit (per_cu);
918dd910 2313 else
95554aad 2314 load_full_comp_unit (per_cu, language_minimal);
918dd910 2315
918dd910 2316 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2317
2318 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2319}
2320
a0f42c21 2321/* Read in the symbols for PER_CU. */
2fdf6df6 2322
9291a0cd 2323static void
a0f42c21 2324dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2325{
2326 struct cleanup *back_to;
2327
f4dc4d17
DE
2328 /* Skip type_unit_groups, reading the type units they contain
2329 is handled elsewhere. */
2330 if (IS_TYPE_UNIT_GROUP (per_cu))
2331 return;
2332
9291a0cd
TT
2333 back_to = make_cleanup (dwarf2_release_queue, NULL);
2334
95554aad
TT
2335 if (dwarf2_per_objfile->using_index
2336 ? per_cu->v.quick->symtab == NULL
2337 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2338 {
2339 queue_comp_unit (per_cu, language_minimal);
2340 load_cu (per_cu);
2341 }
9291a0cd 2342
a0f42c21 2343 process_queue ();
9291a0cd
TT
2344
2345 /* Age the cache, releasing compilation units that have not
2346 been used recently. */
2347 age_cached_comp_units ();
2348
2349 do_cleanups (back_to);
2350}
2351
2352/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2353 the objfile from which this CU came. Returns the resulting symbol
2354 table. */
2fdf6df6 2355
9291a0cd 2356static struct symtab *
a0f42c21 2357dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2358{
95554aad 2359 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2360 if (!per_cu->v.quick->symtab)
2361 {
2362 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2363 increment_reading_symtab ();
a0f42c21 2364 dw2_do_instantiate_symtab (per_cu);
95554aad 2365 process_cu_includes ();
9291a0cd
TT
2366 do_cleanups (back_to);
2367 }
2368 return per_cu->v.quick->symtab;
2369}
2370
f4dc4d17
DE
2371/* Return the CU given its index.
2372
2373 This is intended for loops like:
2374
2375 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2376 + dwarf2_per_objfile->n_type_units); ++i)
2377 {
2378 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2379
2380 ...;
2381 }
2382*/
2fdf6df6 2383
1fd400ff
TT
2384static struct dwarf2_per_cu_data *
2385dw2_get_cu (int index)
2386{
2387 if (index >= dwarf2_per_objfile->n_comp_units)
2388 {
f4dc4d17 2389 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2390 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2391 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2392 }
2393
2394 return dwarf2_per_objfile->all_comp_units[index];
2395}
2396
2397/* Return the primary CU given its index.
2398 The difference between this function and dw2_get_cu is in the handling
2399 of type units (TUs). Here we return the type_unit_group object.
2400
2401 This is intended for loops like:
2402
2403 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2404 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2405 {
2406 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2407
2408 ...;
2409 }
2410*/
2411
2412static struct dwarf2_per_cu_data *
2413dw2_get_primary_cu (int index)
2414{
2415 if (index >= dwarf2_per_objfile->n_comp_units)
2416 {
1fd400ff 2417 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2418 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2419 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2420 }
f4dc4d17 2421
1fd400ff
TT
2422 return dwarf2_per_objfile->all_comp_units[index];
2423}
2424
2ec9a5e0
TT
2425/* A helper for create_cus_from_index that handles a given list of
2426 CUs. */
2fdf6df6 2427
74a0d9f6 2428static void
2ec9a5e0
TT
2429create_cus_from_index_list (struct objfile *objfile,
2430 const gdb_byte *cu_list, offset_type n_elements,
2431 struct dwarf2_section_info *section,
2432 int is_dwz,
2433 int base_offset)
9291a0cd
TT
2434{
2435 offset_type i;
9291a0cd 2436
2ec9a5e0 2437 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2438 {
2439 struct dwarf2_per_cu_data *the_cu;
2440 ULONGEST offset, length;
2441
74a0d9f6
JK
2442 gdb_static_assert (sizeof (ULONGEST) >= 8);
2443 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2444 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2445 cu_list += 2 * 8;
2446
2447 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2448 struct dwarf2_per_cu_data);
b64f50a1 2449 the_cu->offset.sect_off = offset;
9291a0cd
TT
2450 the_cu->length = length;
2451 the_cu->objfile = objfile;
8a0459fd 2452 the_cu->section = section;
9291a0cd
TT
2453 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2454 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2455 the_cu->is_dwz = is_dwz;
2456 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2457 }
9291a0cd
TT
2458}
2459
2ec9a5e0 2460/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2461 the CU objects for this objfile. */
2ec9a5e0 2462
74a0d9f6 2463static void
2ec9a5e0
TT
2464create_cus_from_index (struct objfile *objfile,
2465 const gdb_byte *cu_list, offset_type cu_list_elements,
2466 const gdb_byte *dwz_list, offset_type dwz_elements)
2467{
2468 struct dwz_file *dwz;
2469
2470 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2471 dwarf2_per_objfile->all_comp_units
2472 = obstack_alloc (&objfile->objfile_obstack,
2473 dwarf2_per_objfile->n_comp_units
2474 * sizeof (struct dwarf2_per_cu_data *));
2475
74a0d9f6
JK
2476 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2477 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2478
2479 if (dwz_elements == 0)
74a0d9f6 2480 return;
2ec9a5e0
TT
2481
2482 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2483 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2484 cu_list_elements / 2);
2ec9a5e0
TT
2485}
2486
1fd400ff 2487/* Create the signatured type hash table from the index. */
673bfd45 2488
74a0d9f6 2489static void
673bfd45 2490create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2491 struct dwarf2_section_info *section,
673bfd45
DE
2492 const gdb_byte *bytes,
2493 offset_type elements)
1fd400ff
TT
2494{
2495 offset_type i;
673bfd45 2496 htab_t sig_types_hash;
1fd400ff 2497
d467dd73
DE
2498 dwarf2_per_objfile->n_type_units = elements / 3;
2499 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2500 = xmalloc (dwarf2_per_objfile->n_type_units
2501 * sizeof (struct signatured_type *));
1fd400ff 2502
673bfd45 2503 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2504
2505 for (i = 0; i < elements; i += 3)
2506 {
52dc124a
DE
2507 struct signatured_type *sig_type;
2508 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2509 void **slot;
2510
74a0d9f6
JK
2511 gdb_static_assert (sizeof (ULONGEST) >= 8);
2512 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2513 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2514 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2515 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2516 bytes += 3 * 8;
2517
52dc124a 2518 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2519 struct signatured_type);
52dc124a 2520 sig_type->signature = signature;
3019eac3
DE
2521 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2522 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2523 sig_type->per_cu.section = section;
52dc124a
DE
2524 sig_type->per_cu.offset.sect_off = offset;
2525 sig_type->per_cu.objfile = objfile;
2526 sig_type->per_cu.v.quick
1fd400ff
TT
2527 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2528 struct dwarf2_per_cu_quick_data);
2529
52dc124a
DE
2530 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2531 *slot = sig_type;
1fd400ff 2532
b4dd5633 2533 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2534 }
2535
673bfd45 2536 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2537}
2538
9291a0cd
TT
2539/* Read the address map data from the mapped index, and use it to
2540 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2541
9291a0cd
TT
2542static void
2543create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2544{
2545 const gdb_byte *iter, *end;
2546 struct obstack temp_obstack;
2547 struct addrmap *mutable_map;
2548 struct cleanup *cleanup;
2549 CORE_ADDR baseaddr;
2550
2551 obstack_init (&temp_obstack);
2552 cleanup = make_cleanup_obstack_free (&temp_obstack);
2553 mutable_map = addrmap_create_mutable (&temp_obstack);
2554
2555 iter = index->address_table;
2556 end = iter + index->address_table_size;
2557
2558 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2559
2560 while (iter < end)
2561 {
2562 ULONGEST hi, lo, cu_index;
2563 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2564 iter += 8;
2565 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2566 iter += 8;
2567 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2568 iter += 4;
f652bce2 2569
24a55014 2570 if (lo > hi)
f652bce2 2571 {
24a55014
DE
2572 complaint (&symfile_complaints,
2573 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2574 hex_string (lo), hex_string (hi));
24a55014 2575 continue;
f652bce2 2576 }
24a55014
DE
2577
2578 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2579 {
2580 complaint (&symfile_complaints,
2581 _(".gdb_index address table has invalid CU number %u"),
2582 (unsigned) cu_index);
24a55014 2583 continue;
f652bce2 2584 }
24a55014
DE
2585
2586 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2587 dw2_get_cu (cu_index));
9291a0cd
TT
2588 }
2589
2590 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2591 &objfile->objfile_obstack);
2592 do_cleanups (cleanup);
2593}
2594
59d7bcaf
JK
2595/* The hash function for strings in the mapped index. This is the same as
2596 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2597 implementation. This is necessary because the hash function is tied to the
2598 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2599 SYMBOL_HASH_NEXT.
2600
2601 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2602
9291a0cd 2603static hashval_t
559a7a62 2604mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2605{
2606 const unsigned char *str = (const unsigned char *) p;
2607 hashval_t r = 0;
2608 unsigned char c;
2609
2610 while ((c = *str++) != 0)
559a7a62
JK
2611 {
2612 if (index_version >= 5)
2613 c = tolower (c);
2614 r = r * 67 + c - 113;
2615 }
9291a0cd
TT
2616
2617 return r;
2618}
2619
2620/* Find a slot in the mapped index INDEX for the object named NAME.
2621 If NAME is found, set *VEC_OUT to point to the CU vector in the
2622 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2623
9291a0cd
TT
2624static int
2625find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2626 offset_type **vec_out)
2627{
0cf03b49
JK
2628 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2629 offset_type hash;
9291a0cd 2630 offset_type slot, step;
559a7a62 2631 int (*cmp) (const char *, const char *);
9291a0cd 2632
0cf03b49
JK
2633 if (current_language->la_language == language_cplus
2634 || current_language->la_language == language_java
2635 || current_language->la_language == language_fortran)
2636 {
2637 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2638 not contain any. */
2639 const char *paren = strchr (name, '(');
2640
2641 if (paren)
2642 {
2643 char *dup;
2644
2645 dup = xmalloc (paren - name + 1);
2646 memcpy (dup, name, paren - name);
2647 dup[paren - name] = 0;
2648
2649 make_cleanup (xfree, dup);
2650 name = dup;
2651 }
2652 }
2653
559a7a62 2654 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2655 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2656 simulate our NAME being searched is also lowercased. */
2657 hash = mapped_index_string_hash ((index->version == 4
2658 && case_sensitivity == case_sensitive_off
2659 ? 5 : index->version),
2660 name);
2661
3876f04e
DE
2662 slot = hash & (index->symbol_table_slots - 1);
2663 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2664 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2665
2666 for (;;)
2667 {
2668 /* Convert a slot number to an offset into the table. */
2669 offset_type i = 2 * slot;
2670 const char *str;
3876f04e 2671 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2672 {
2673 do_cleanups (back_to);
2674 return 0;
2675 }
9291a0cd 2676
3876f04e 2677 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2678 if (!cmp (name, str))
9291a0cd
TT
2679 {
2680 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2681 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2682 do_cleanups (back_to);
9291a0cd
TT
2683 return 1;
2684 }
2685
3876f04e 2686 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2687 }
2688}
2689
2ec9a5e0
TT
2690/* A helper function that reads the .gdb_index from SECTION and fills
2691 in MAP. FILENAME is the name of the file containing the section;
2692 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2693 ok to use deprecated sections.
2694
2695 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2696 out parameters that are filled in with information about the CU and
2697 TU lists in the section.
2698
2699 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2700
9291a0cd 2701static int
2ec9a5e0
TT
2702read_index_from_section (struct objfile *objfile,
2703 const char *filename,
2704 int deprecated_ok,
2705 struct dwarf2_section_info *section,
2706 struct mapped_index *map,
2707 const gdb_byte **cu_list,
2708 offset_type *cu_list_elements,
2709 const gdb_byte **types_list,
2710 offset_type *types_list_elements)
9291a0cd 2711{
948f8e3d 2712 const gdb_byte *addr;
2ec9a5e0 2713 offset_type version;
b3b272e1 2714 offset_type *metadata;
1fd400ff 2715 int i;
9291a0cd 2716
2ec9a5e0 2717 if (dwarf2_section_empty_p (section))
9291a0cd 2718 return 0;
82430852
JK
2719
2720 /* Older elfutils strip versions could keep the section in the main
2721 executable while splitting it for the separate debug info file. */
2ec9a5e0 2722 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2723 return 0;
2724
2ec9a5e0 2725 dwarf2_read_section (objfile, section);
9291a0cd 2726
2ec9a5e0 2727 addr = section->buffer;
9291a0cd 2728 /* Version check. */
1fd400ff 2729 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2730 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2731 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2732 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2733 indices. */
831adc1f 2734 if (version < 4)
481860b3
GB
2735 {
2736 static int warning_printed = 0;
2737 if (!warning_printed)
2738 {
2739 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2740 filename);
481860b3
GB
2741 warning_printed = 1;
2742 }
2743 return 0;
2744 }
2745 /* Index version 4 uses a different hash function than index version
2746 5 and later.
2747
2748 Versions earlier than 6 did not emit psymbols for inlined
2749 functions. Using these files will cause GDB not to be able to
2750 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2751 indices unless the user has done
2752 "set use-deprecated-index-sections on". */
2ec9a5e0 2753 if (version < 6 && !deprecated_ok)
481860b3
GB
2754 {
2755 static int warning_printed = 0;
2756 if (!warning_printed)
2757 {
e615022a
DE
2758 warning (_("\
2759Skipping deprecated .gdb_index section in %s.\n\
2760Do \"set use-deprecated-index-sections on\" before the file is read\n\
2761to use the section anyway."),
2ec9a5e0 2762 filename);
481860b3
GB
2763 warning_printed = 1;
2764 }
2765 return 0;
2766 }
796a7ff8
DE
2767 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2768 of the TU (for symbols coming from TUs). It's just a performance bug, and
2769 we can't distinguish gdb-generated indices from gold-generated ones, so
2770 nothing to do here. */
2771
481860b3 2772 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2773 longer backward compatible. */
796a7ff8 2774 if (version > 8)
594e8718 2775 return 0;
9291a0cd 2776
559a7a62 2777 map->version = version;
2ec9a5e0 2778 map->total_size = section->size;
9291a0cd
TT
2779
2780 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2781
2782 i = 0;
2ec9a5e0
TT
2783 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2784 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2785 / 8);
1fd400ff
TT
2786 ++i;
2787
2ec9a5e0
TT
2788 *types_list = addr + MAYBE_SWAP (metadata[i]);
2789 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2790 - MAYBE_SWAP (metadata[i]))
2791 / 8);
987d643c 2792 ++i;
1fd400ff
TT
2793
2794 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2795 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2796 - MAYBE_SWAP (metadata[i]));
2797 ++i;
2798
3876f04e
DE
2799 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2800 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2801 - MAYBE_SWAP (metadata[i]))
2802 / (2 * sizeof (offset_type)));
1fd400ff 2803 ++i;
9291a0cd 2804
f9d83a0b 2805 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2806
2ec9a5e0
TT
2807 return 1;
2808}
2809
2810
2811/* Read the index file. If everything went ok, initialize the "quick"
2812 elements of all the CUs and return 1. Otherwise, return 0. */
2813
2814static int
2815dwarf2_read_index (struct objfile *objfile)
2816{
2817 struct mapped_index local_map, *map;
2818 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2819 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2820 struct dwz_file *dwz;
2ec9a5e0
TT
2821
2822 if (!read_index_from_section (objfile, objfile->name,
2823 use_deprecated_index_sections,
2824 &dwarf2_per_objfile->gdb_index, &local_map,
2825 &cu_list, &cu_list_elements,
2826 &types_list, &types_list_elements))
2827 return 0;
2828
0fefef59 2829 /* Don't use the index if it's empty. */
2ec9a5e0 2830 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2831 return 0;
2832
2ec9a5e0
TT
2833 /* If there is a .dwz file, read it so we can get its CU list as
2834 well. */
4db1a1dc
TT
2835 dwz = dwarf2_get_dwz_file ();
2836 if (dwz != NULL)
2ec9a5e0 2837 {
2ec9a5e0
TT
2838 struct mapped_index dwz_map;
2839 const gdb_byte *dwz_types_ignore;
2840 offset_type dwz_types_elements_ignore;
2841
2842 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2843 1,
2844 &dwz->gdb_index, &dwz_map,
2845 &dwz_list, &dwz_list_elements,
2846 &dwz_types_ignore,
2847 &dwz_types_elements_ignore))
2848 {
2849 warning (_("could not read '.gdb_index' section from %s; skipping"),
2850 bfd_get_filename (dwz->dwz_bfd));
2851 return 0;
2852 }
2853 }
2854
74a0d9f6
JK
2855 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2856 dwz_list_elements);
1fd400ff 2857
8b70b953
TT
2858 if (types_list_elements)
2859 {
2860 struct dwarf2_section_info *section;
2861
2862 /* We can only handle a single .debug_types when we have an
2863 index. */
2864 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2865 return 0;
2866
2867 section = VEC_index (dwarf2_section_info_def,
2868 dwarf2_per_objfile->types, 0);
2869
74a0d9f6
JK
2870 create_signatured_type_table_from_index (objfile, section, types_list,
2871 types_list_elements);
8b70b953 2872 }
9291a0cd 2873
2ec9a5e0
TT
2874 create_addrmap_from_index (objfile, &local_map);
2875
2876 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2877 *map = local_map;
9291a0cd
TT
2878
2879 dwarf2_per_objfile->index_table = map;
2880 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2881 dwarf2_per_objfile->quick_file_names_table =
2882 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2883
2884 return 1;
2885}
2886
2887/* A helper for the "quick" functions which sets the global
2888 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2889
9291a0cd
TT
2890static void
2891dw2_setup (struct objfile *objfile)
2892{
2893 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2894 gdb_assert (dwarf2_per_objfile);
2895}
2896
dee91e82 2897/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2898
dee91e82
DE
2899static void
2900dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2901 const gdb_byte *info_ptr,
dee91e82
DE
2902 struct die_info *comp_unit_die,
2903 int has_children,
2904 void *data)
9291a0cd 2905{
dee91e82
DE
2906 struct dwarf2_cu *cu = reader->cu;
2907 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2908 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2909 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2910 struct line_header *lh;
9291a0cd 2911 struct attribute *attr;
dee91e82 2912 int i;
15d034d0 2913 const char *name, *comp_dir;
7b9f3c50
DE
2914 void **slot;
2915 struct quick_file_names *qfn;
2916 unsigned int line_offset;
9291a0cd 2917
0186c6a7
DE
2918 gdb_assert (! this_cu->is_debug_types);
2919
07261596
TT
2920 /* Our callers never want to match partial units -- instead they
2921 will match the enclosing full CU. */
2922 if (comp_unit_die->tag == DW_TAG_partial_unit)
2923 {
2924 this_cu->v.quick->no_file_data = 1;
2925 return;
2926 }
2927
0186c6a7 2928 lh_cu = this_cu;
7b9f3c50
DE
2929 lh = NULL;
2930 slot = NULL;
2931 line_offset = 0;
dee91e82
DE
2932
2933 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2934 if (attr)
2935 {
7b9f3c50
DE
2936 struct quick_file_names find_entry;
2937
2938 line_offset = DW_UNSND (attr);
2939
2940 /* We may have already read in this line header (TU line header sharing).
2941 If we have we're done. */
094b34ac
DE
2942 find_entry.hash.dwo_unit = cu->dwo_unit;
2943 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2944 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2945 &find_entry, INSERT);
2946 if (*slot != NULL)
2947 {
094b34ac 2948 lh_cu->v.quick->file_names = *slot;
dee91e82 2949 return;
7b9f3c50
DE
2950 }
2951
3019eac3 2952 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2953 }
2954 if (lh == NULL)
2955 {
094b34ac 2956 lh_cu->v.quick->no_file_data = 1;
dee91e82 2957 return;
9291a0cd
TT
2958 }
2959
7b9f3c50 2960 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2961 qfn->hash.dwo_unit = cu->dwo_unit;
2962 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2963 gdb_assert (slot != NULL);
2964 *slot = qfn;
9291a0cd 2965
dee91e82 2966 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2967
7b9f3c50
DE
2968 qfn->num_file_names = lh->num_file_names;
2969 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2970 lh->num_file_names * sizeof (char *));
9291a0cd 2971 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2972 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2973 qfn->real_names = NULL;
9291a0cd 2974
7b9f3c50 2975 free_line_header (lh);
7b9f3c50 2976
094b34ac 2977 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2978}
2979
2980/* A helper for the "quick" functions which attempts to read the line
2981 table for THIS_CU. */
2982
2983static struct quick_file_names *
e4a48d9d 2984dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2985{
0186c6a7
DE
2986 /* This should never be called for TUs. */
2987 gdb_assert (! this_cu->is_debug_types);
2988 /* Nor type unit groups. */
2989 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2990
dee91e82
DE
2991 if (this_cu->v.quick->file_names != NULL)
2992 return this_cu->v.quick->file_names;
2993 /* If we know there is no line data, no point in looking again. */
2994 if (this_cu->v.quick->no_file_data)
2995 return NULL;
2996
0186c6a7 2997 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2998
2999 if (this_cu->v.quick->no_file_data)
3000 return NULL;
3001 return this_cu->v.quick->file_names;
9291a0cd
TT
3002}
3003
3004/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3005 real path for a given file name from the line table. */
2fdf6df6 3006
9291a0cd 3007static const char *
7b9f3c50
DE
3008dw2_get_real_path (struct objfile *objfile,
3009 struct quick_file_names *qfn, int index)
9291a0cd 3010{
7b9f3c50
DE
3011 if (qfn->real_names == NULL)
3012 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3013 qfn->num_file_names, sizeof (char *));
9291a0cd 3014
7b9f3c50
DE
3015 if (qfn->real_names[index] == NULL)
3016 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3017
7b9f3c50 3018 return qfn->real_names[index];
9291a0cd
TT
3019}
3020
3021static struct symtab *
3022dw2_find_last_source_symtab (struct objfile *objfile)
3023{
3024 int index;
ae2de4f8 3025
9291a0cd
TT
3026 dw2_setup (objfile);
3027 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3028 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3029}
3030
7b9f3c50
DE
3031/* Traversal function for dw2_forget_cached_source_info. */
3032
3033static int
3034dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3035{
7b9f3c50 3036 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3037
7b9f3c50 3038 if (file_data->real_names)
9291a0cd 3039 {
7b9f3c50 3040 int i;
9291a0cd 3041
7b9f3c50 3042 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3043 {
7b9f3c50
DE
3044 xfree ((void*) file_data->real_names[i]);
3045 file_data->real_names[i] = NULL;
9291a0cd
TT
3046 }
3047 }
7b9f3c50
DE
3048
3049 return 1;
3050}
3051
3052static void
3053dw2_forget_cached_source_info (struct objfile *objfile)
3054{
3055 dw2_setup (objfile);
3056
3057 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3058 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3059}
3060
f8eba3c6
TT
3061/* Helper function for dw2_map_symtabs_matching_filename that expands
3062 the symtabs and calls the iterator. */
3063
3064static int
3065dw2_map_expand_apply (struct objfile *objfile,
3066 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3067 const char *name, const char *real_path,
f8eba3c6
TT
3068 int (*callback) (struct symtab *, void *),
3069 void *data)
3070{
3071 struct symtab *last_made = objfile->symtabs;
3072
3073 /* Don't visit already-expanded CUs. */
3074 if (per_cu->v.quick->symtab)
3075 return 0;
3076
3077 /* This may expand more than one symtab, and we want to iterate over
3078 all of them. */
a0f42c21 3079 dw2_instantiate_symtab (per_cu);
f8eba3c6 3080
f5b95b50 3081 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3082 objfile->symtabs, last_made);
3083}
3084
3085/* Implementation of the map_symtabs_matching_filename method. */
3086
9291a0cd 3087static int
f8eba3c6 3088dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3089 const char *real_path,
f8eba3c6
TT
3090 int (*callback) (struct symtab *, void *),
3091 void *data)
9291a0cd
TT
3092{
3093 int i;
c011a4f4 3094 const char *name_basename = lbasename (name);
9291a0cd
TT
3095
3096 dw2_setup (objfile);
ae2de4f8 3097
848e3e78
DE
3098 /* The rule is CUs specify all the files, including those used by
3099 any TU, so there's no need to scan TUs here. */
f4dc4d17 3100
848e3e78 3101 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3102 {
3103 int j;
f4dc4d17 3104 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3105 struct quick_file_names *file_data;
9291a0cd 3106
3d7bb9d9 3107 /* We only need to look at symtabs not already expanded. */
e254ef6a 3108 if (per_cu->v.quick->symtab)
9291a0cd
TT
3109 continue;
3110
e4a48d9d 3111 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3112 if (file_data == NULL)
9291a0cd
TT
3113 continue;
3114
7b9f3c50 3115 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3116 {
7b9f3c50 3117 const char *this_name = file_data->file_names[j];
da235a7c 3118 const char *this_real_name;
9291a0cd 3119
af529f8f 3120 if (compare_filenames_for_search (this_name, name))
9291a0cd 3121 {
f5b95b50 3122 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3123 callback, data))
3124 return 1;
288e77a7 3125 continue;
4aac40c8 3126 }
9291a0cd 3127
c011a4f4
DE
3128 /* Before we invoke realpath, which can get expensive when many
3129 files are involved, do a quick comparison of the basenames. */
3130 if (! basenames_may_differ
3131 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3132 continue;
3133
da235a7c
JK
3134 this_real_name = dw2_get_real_path (objfile, file_data, j);
3135 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3136 {
da235a7c
JK
3137 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3138 callback, data))
3139 return 1;
288e77a7 3140 continue;
da235a7c 3141 }
9291a0cd 3142
da235a7c
JK
3143 if (real_path != NULL)
3144 {
af529f8f
JK
3145 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3146 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3147 if (this_real_name != NULL
af529f8f 3148 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3149 {
f5b95b50 3150 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3151 callback, data))
3152 return 1;
288e77a7 3153 continue;
9291a0cd
TT
3154 }
3155 }
3156 }
3157 }
3158
9291a0cd
TT
3159 return 0;
3160}
3161
da51c347
DE
3162/* Struct used to manage iterating over all CUs looking for a symbol. */
3163
3164struct dw2_symtab_iterator
9291a0cd 3165{
da51c347
DE
3166 /* The internalized form of .gdb_index. */
3167 struct mapped_index *index;
3168 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3169 int want_specific_block;
3170 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3171 Unused if !WANT_SPECIFIC_BLOCK. */
3172 int block_index;
3173 /* The kind of symbol we're looking for. */
3174 domain_enum domain;
3175 /* The list of CUs from the index entry of the symbol,
3176 or NULL if not found. */
3177 offset_type *vec;
3178 /* The next element in VEC to look at. */
3179 int next;
3180 /* The number of elements in VEC, or zero if there is no match. */
3181 int length;
3182};
9291a0cd 3183
da51c347
DE
3184/* Initialize the index symtab iterator ITER.
3185 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3186 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3187
9291a0cd 3188static void
da51c347
DE
3189dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3190 struct mapped_index *index,
3191 int want_specific_block,
3192 int block_index,
3193 domain_enum domain,
3194 const char *name)
3195{
3196 iter->index = index;
3197 iter->want_specific_block = want_specific_block;
3198 iter->block_index = block_index;
3199 iter->domain = domain;
3200 iter->next = 0;
3201
3202 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3203 iter->length = MAYBE_SWAP (*iter->vec);
3204 else
3205 {
3206 iter->vec = NULL;
3207 iter->length = 0;
3208 }
3209}
3210
3211/* Return the next matching CU or NULL if there are no more. */
3212
3213static struct dwarf2_per_cu_data *
3214dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3215{
3216 for ( ; iter->next < iter->length; ++iter->next)
3217 {
3218 offset_type cu_index_and_attrs =
3219 MAYBE_SWAP (iter->vec[iter->next + 1]);
3220 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3221 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3222 int want_static = iter->block_index != GLOBAL_BLOCK;
3223 /* This value is only valid for index versions >= 7. */
3224 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3225 gdb_index_symbol_kind symbol_kind =
3226 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3227 /* Only check the symbol attributes if they're present.
3228 Indices prior to version 7 don't record them,
3229 and indices >= 7 may elide them for certain symbols
3230 (gold does this). */
3231 int attrs_valid =
3232 (iter->index->version >= 7
3233 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3234
3190f0c6
DE
3235 /* Don't crash on bad data. */
3236 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3237 + dwarf2_per_objfile->n_type_units))
3238 {
3239 complaint (&symfile_complaints,
3240 _(".gdb_index entry has bad CU index"
3241 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3242 continue;
3243 }
3244
3245 per_cu = dw2_get_cu (cu_index);
3246
da51c347
DE
3247 /* Skip if already read in. */
3248 if (per_cu->v.quick->symtab)
3249 continue;
3250
3251 if (attrs_valid
3252 && iter->want_specific_block
3253 && want_static != is_static)
3254 continue;
3255
3256 /* Only check the symbol's kind if it has one. */
3257 if (attrs_valid)
3258 {
3259 switch (iter->domain)
3260 {
3261 case VAR_DOMAIN:
3262 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3263 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3264 /* Some types are also in VAR_DOMAIN. */
3265 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3266 continue;
3267 break;
3268 case STRUCT_DOMAIN:
3269 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3270 continue;
3271 break;
3272 case LABEL_DOMAIN:
3273 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3274 continue;
3275 break;
3276 default:
3277 break;
3278 }
3279 }
3280
3281 ++iter->next;
3282 return per_cu;
3283 }
3284
3285 return NULL;
3286}
3287
3288static struct symtab *
3289dw2_lookup_symbol (struct objfile *objfile, int block_index,
3290 const char *name, domain_enum domain)
9291a0cd 3291{
da51c347 3292 struct symtab *stab_best = NULL;
156942c7
DE
3293 struct mapped_index *index;
3294
9291a0cd
TT
3295 dw2_setup (objfile);
3296
156942c7
DE
3297 index = dwarf2_per_objfile->index_table;
3298
da51c347 3299 /* index is NULL if OBJF_READNOW. */
156942c7 3300 if (index)
9291a0cd 3301 {
da51c347
DE
3302 struct dw2_symtab_iterator iter;
3303 struct dwarf2_per_cu_data *per_cu;
3304
3305 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3306
da51c347 3307 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3308 {
da51c347
DE
3309 struct symbol *sym = NULL;
3310 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3311
3312 /* Some caution must be observed with overloaded functions
3313 and methods, since the index will not contain any overload
3314 information (but NAME might contain it). */
3315 if (stab->primary)
9291a0cd 3316 {
da51c347
DE
3317 struct blockvector *bv = BLOCKVECTOR (stab);
3318 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3319
da51c347
DE
3320 sym = lookup_block_symbol (block, name, domain);
3321 }
1fd400ff 3322
da51c347
DE
3323 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3324 {
3325 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3326 return stab;
3327
3328 stab_best = stab;
9291a0cd 3329 }
da51c347
DE
3330
3331 /* Keep looking through other CUs. */
9291a0cd
TT
3332 }
3333 }
9291a0cd 3334
da51c347 3335 return stab_best;
9291a0cd
TT
3336}
3337
3338static void
3339dw2_print_stats (struct objfile *objfile)
3340{
e4a48d9d 3341 int i, total, count;
9291a0cd
TT
3342
3343 dw2_setup (objfile);
e4a48d9d 3344 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3345 count = 0;
e4a48d9d 3346 for (i = 0; i < total; ++i)
9291a0cd 3347 {
e254ef6a 3348 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3349
e254ef6a 3350 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3351 ++count;
3352 }
e4a48d9d 3353 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3354 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3355}
3356
779bd270
DE
3357/* This dumps minimal information about the index.
3358 It is called via "mt print objfiles".
3359 One use is to verify .gdb_index has been loaded by the
3360 gdb.dwarf2/gdb-index.exp testcase. */
3361
9291a0cd
TT
3362static void
3363dw2_dump (struct objfile *objfile)
3364{
779bd270
DE
3365 dw2_setup (objfile);
3366 gdb_assert (dwarf2_per_objfile->using_index);
3367 printf_filtered (".gdb_index:");
3368 if (dwarf2_per_objfile->index_table != NULL)
3369 {
3370 printf_filtered (" version %d\n",
3371 dwarf2_per_objfile->index_table->version);
3372 }
3373 else
3374 printf_filtered (" faked for \"readnow\"\n");
3375 printf_filtered ("\n");
9291a0cd
TT
3376}
3377
3378static void
3189cb12
DE
3379dw2_relocate (struct objfile *objfile,
3380 const struct section_offsets *new_offsets,
3381 const struct section_offsets *delta)
9291a0cd
TT
3382{
3383 /* There's nothing to relocate here. */
3384}
3385
3386static void
3387dw2_expand_symtabs_for_function (struct objfile *objfile,
3388 const char *func_name)
3389{
da51c347
DE
3390 struct mapped_index *index;
3391
3392 dw2_setup (objfile);
3393
3394 index = dwarf2_per_objfile->index_table;
3395
3396 /* index is NULL if OBJF_READNOW. */
3397 if (index)
3398 {
3399 struct dw2_symtab_iterator iter;
3400 struct dwarf2_per_cu_data *per_cu;
3401
3402 /* Note: It doesn't matter what we pass for block_index here. */
3403 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3404 func_name);
3405
3406 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3407 dw2_instantiate_symtab (per_cu);
3408 }
9291a0cd
TT
3409}
3410
3411static void
3412dw2_expand_all_symtabs (struct objfile *objfile)
3413{
3414 int i;
3415
3416 dw2_setup (objfile);
1fd400ff
TT
3417
3418 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3419 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3420 {
e254ef6a 3421 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3422
a0f42c21 3423 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3424 }
3425}
3426
3427static void
652a8996
JK
3428dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3429 const char *fullname)
9291a0cd
TT
3430{
3431 int i;
3432
3433 dw2_setup (objfile);
d4637a04
DE
3434
3435 /* We don't need to consider type units here.
3436 This is only called for examining code, e.g. expand_line_sal.
3437 There can be an order of magnitude (or more) more type units
3438 than comp units, and we avoid them if we can. */
3439
3440 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3441 {
3442 int j;
e254ef6a 3443 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3444 struct quick_file_names *file_data;
9291a0cd 3445
3d7bb9d9 3446 /* We only need to look at symtabs not already expanded. */
e254ef6a 3447 if (per_cu->v.quick->symtab)
9291a0cd
TT
3448 continue;
3449
e4a48d9d 3450 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3451 if (file_data == NULL)
9291a0cd
TT
3452 continue;
3453
7b9f3c50 3454 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3455 {
652a8996
JK
3456 const char *this_fullname = file_data->file_names[j];
3457
3458 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3459 {
a0f42c21 3460 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3461 break;
3462 }
3463 }
3464 }
3465}
3466
9291a0cd 3467static void
40658b94
PH
3468dw2_map_matching_symbols (const char * name, domain_enum namespace,
3469 struct objfile *objfile, int global,
3470 int (*callback) (struct block *,
3471 struct symbol *, void *),
2edb89d3
JK
3472 void *data, symbol_compare_ftype *match,
3473 symbol_compare_ftype *ordered_compare)
9291a0cd 3474{
40658b94 3475 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3476 current language is Ada for a non-Ada objfile using GNU index. As Ada
3477 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3478}
3479
3480static void
f8eba3c6
TT
3481dw2_expand_symtabs_matching
3482 (struct objfile *objfile,
fbd9ab74 3483 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3484 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3485 enum search_domain kind,
3486 void *data)
9291a0cd
TT
3487{
3488 int i;
3489 offset_type iter;
4b5246aa 3490 struct mapped_index *index;
9291a0cd
TT
3491
3492 dw2_setup (objfile);
ae2de4f8
DE
3493
3494 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3495 if (!dwarf2_per_objfile->index_table)
3496 return;
4b5246aa 3497 index = dwarf2_per_objfile->index_table;
9291a0cd 3498
7b08b9eb 3499 if (file_matcher != NULL)
24c79950
TT
3500 {
3501 struct cleanup *cleanup;
3502 htab_t visited_found, visited_not_found;
3503
3504 visited_found = htab_create_alloc (10,
3505 htab_hash_pointer, htab_eq_pointer,
3506 NULL, xcalloc, xfree);
3507 cleanup = make_cleanup_htab_delete (visited_found);
3508 visited_not_found = htab_create_alloc (10,
3509 htab_hash_pointer, htab_eq_pointer,
3510 NULL, xcalloc, xfree);
3511 make_cleanup_htab_delete (visited_not_found);
3512
848e3e78
DE
3513 /* The rule is CUs specify all the files, including those used by
3514 any TU, so there's no need to scan TUs here. */
3515
3516 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3517 {
3518 int j;
f4dc4d17 3519 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3520 struct quick_file_names *file_data;
3521 void **slot;
7b08b9eb 3522
24c79950 3523 per_cu->v.quick->mark = 0;
3d7bb9d9 3524
24c79950
TT
3525 /* We only need to look at symtabs not already expanded. */
3526 if (per_cu->v.quick->symtab)
3527 continue;
7b08b9eb 3528
e4a48d9d 3529 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3530 if (file_data == NULL)
3531 continue;
7b08b9eb 3532
24c79950
TT
3533 if (htab_find (visited_not_found, file_data) != NULL)
3534 continue;
3535 else if (htab_find (visited_found, file_data) != NULL)
3536 {
3537 per_cu->v.quick->mark = 1;
3538 continue;
3539 }
3540
3541 for (j = 0; j < file_data->num_file_names; ++j)
3542 {
da235a7c
JK
3543 const char *this_real_name;
3544
fbd9ab74 3545 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3546 {
3547 per_cu->v.quick->mark = 1;
3548 break;
3549 }
da235a7c
JK
3550
3551 /* Before we invoke realpath, which can get expensive when many
3552 files are involved, do a quick comparison of the basenames. */
3553 if (!basenames_may_differ
3554 && !file_matcher (lbasename (file_data->file_names[j]),
3555 data, 1))
3556 continue;
3557
3558 this_real_name = dw2_get_real_path (objfile, file_data, j);
3559 if (file_matcher (this_real_name, data, 0))
3560 {
3561 per_cu->v.quick->mark = 1;
3562 break;
3563 }
24c79950
TT
3564 }
3565
3566 slot = htab_find_slot (per_cu->v.quick->mark
3567 ? visited_found
3568 : visited_not_found,
3569 file_data, INSERT);
3570 *slot = file_data;
3571 }
3572
3573 do_cleanups (cleanup);
3574 }
9291a0cd 3575
3876f04e 3576 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3577 {
3578 offset_type idx = 2 * iter;
3579 const char *name;
3580 offset_type *vec, vec_len, vec_idx;
3581
3876f04e 3582 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3583 continue;
3584
3876f04e 3585 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3586
e078317b 3587 if (! (*name_matcher) (name, data))
9291a0cd
TT
3588 continue;
3589
3590 /* The name was matched, now expand corresponding CUs that were
3591 marked. */
4b5246aa 3592 vec = (offset_type *) (index->constant_pool
3876f04e 3593 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3594 vec_len = MAYBE_SWAP (vec[0]);
3595 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3596 {
e254ef6a 3597 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3598 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3599 gdb_index_symbol_kind symbol_kind =
3600 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3601 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3602 /* Only check the symbol attributes if they're present.
3603 Indices prior to version 7 don't record them,
3604 and indices >= 7 may elide them for certain symbols
3605 (gold does this). */
3606 int attrs_valid =
3607 (index->version >= 7
3608 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3609
3610 /* Only check the symbol's kind if it has one. */
3611 if (attrs_valid)
156942c7
DE
3612 {
3613 switch (kind)
3614 {
3615 case VARIABLES_DOMAIN:
3616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3617 continue;
3618 break;
3619 case FUNCTIONS_DOMAIN:
3620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3621 continue;
3622 break;
3623 case TYPES_DOMAIN:
3624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3625 continue;
3626 break;
3627 default:
3628 break;
3629 }
3630 }
3631
3190f0c6
DE
3632 /* Don't crash on bad data. */
3633 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3634 + dwarf2_per_objfile->n_type_units))
3635 {
3636 complaint (&symfile_complaints,
3637 _(".gdb_index entry has bad CU index"
3638 " [in module %s]"), objfile->name);
3639 continue;
3640 }
3641
156942c7 3642 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3643 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3644 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3645 }
3646 }
3647}
3648
9703b513
TT
3649/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3650 symtab. */
3651
3652static struct symtab *
3653recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3654{
3655 int i;
3656
3657 if (BLOCKVECTOR (symtab) != NULL
3658 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3659 return symtab;
3660
a3ec0bb1
DE
3661 if (symtab->includes == NULL)
3662 return NULL;
3663
9703b513
TT
3664 for (i = 0; symtab->includes[i]; ++i)
3665 {
a3ec0bb1 3666 struct symtab *s = symtab->includes[i];
9703b513
TT
3667
3668 s = recursively_find_pc_sect_symtab (s, pc);
3669 if (s != NULL)
3670 return s;
3671 }
3672
3673 return NULL;
3674}
3675
9291a0cd
TT
3676static struct symtab *
3677dw2_find_pc_sect_symtab (struct objfile *objfile,
3678 struct minimal_symbol *msymbol,
3679 CORE_ADDR pc,
3680 struct obj_section *section,
3681 int warn_if_readin)
3682{
3683 struct dwarf2_per_cu_data *data;
9703b513 3684 struct symtab *result;
9291a0cd
TT
3685
3686 dw2_setup (objfile);
3687
3688 if (!objfile->psymtabs_addrmap)
3689 return NULL;
3690
3691 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3692 if (!data)
3693 return NULL;
3694
3695 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3696 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3697 paddress (get_objfile_arch (objfile), pc));
3698
9703b513
TT
3699 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3700 gdb_assert (result != NULL);
3701 return result;
9291a0cd
TT
3702}
3703
9291a0cd 3704static void
44b13c5a 3705dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3706 void *data, int need_fullname)
9291a0cd
TT
3707{
3708 int i;
24c79950
TT
3709 struct cleanup *cleanup;
3710 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3711 NULL, xcalloc, xfree);
9291a0cd 3712
24c79950 3713 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3714 dw2_setup (objfile);
ae2de4f8 3715
848e3e78
DE
3716 /* The rule is CUs specify all the files, including those used by
3717 any TU, so there's no need to scan TUs here.
3718 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3719
848e3e78 3720 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3721 {
3722 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3723
3724 if (per_cu->v.quick->symtab)
3725 {
3726 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3727 INSERT);
3728
3729 *slot = per_cu->v.quick->file_names;
3730 }
3731 }
3732
848e3e78 3733 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3734 {
3735 int j;
f4dc4d17 3736 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3737 struct quick_file_names *file_data;
24c79950 3738 void **slot;
9291a0cd 3739
3d7bb9d9 3740 /* We only need to look at symtabs not already expanded. */
e254ef6a 3741 if (per_cu->v.quick->symtab)
9291a0cd
TT
3742 continue;
3743
e4a48d9d 3744 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3745 if (file_data == NULL)
9291a0cd
TT
3746 continue;
3747
24c79950
TT
3748 slot = htab_find_slot (visited, file_data, INSERT);
3749 if (*slot)
3750 {
3751 /* Already visited. */
3752 continue;
3753 }
3754 *slot = file_data;
3755
7b9f3c50 3756 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3757 {
74e2f255
DE
3758 const char *this_real_name;
3759
3760 if (need_fullname)
3761 this_real_name = dw2_get_real_path (objfile, file_data, j);
3762 else
3763 this_real_name = NULL;
7b9f3c50 3764 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3765 }
3766 }
24c79950
TT
3767
3768 do_cleanups (cleanup);
9291a0cd
TT
3769}
3770
3771static int
3772dw2_has_symbols (struct objfile *objfile)
3773{
3774 return 1;
3775}
3776
3777const struct quick_symbol_functions dwarf2_gdb_index_functions =
3778{
3779 dw2_has_symbols,
3780 dw2_find_last_source_symtab,
3781 dw2_forget_cached_source_info,
f8eba3c6 3782 dw2_map_symtabs_matching_filename,
9291a0cd 3783 dw2_lookup_symbol,
9291a0cd
TT
3784 dw2_print_stats,
3785 dw2_dump,
3786 dw2_relocate,
3787 dw2_expand_symtabs_for_function,
3788 dw2_expand_all_symtabs,
652a8996 3789 dw2_expand_symtabs_with_fullname,
40658b94 3790 dw2_map_matching_symbols,
9291a0cd
TT
3791 dw2_expand_symtabs_matching,
3792 dw2_find_pc_sect_symtab,
9291a0cd
TT
3793 dw2_map_symbol_filenames
3794};
3795
3796/* Initialize for reading DWARF for this objfile. Return 0 if this
3797 file will use psymtabs, or 1 if using the GNU index. */
3798
3799int
3800dwarf2_initialize_objfile (struct objfile *objfile)
3801{
3802 /* If we're about to read full symbols, don't bother with the
3803 indices. In this case we also don't care if some other debug
3804 format is making psymtabs, because they are all about to be
3805 expanded anyway. */
3806 if ((objfile->flags & OBJF_READNOW))
3807 {
3808 int i;
3809
3810 dwarf2_per_objfile->using_index = 1;
3811 create_all_comp_units (objfile);
0e50663e 3812 create_all_type_units (objfile);
7b9f3c50
DE
3813 dwarf2_per_objfile->quick_file_names_table =
3814 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3815
1fd400ff 3816 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3817 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3818 {
e254ef6a 3819 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3820
e254ef6a
DE
3821 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3822 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3823 }
3824
3825 /* Return 1 so that gdb sees the "quick" functions. However,
3826 these functions will be no-ops because we will have expanded
3827 all symtabs. */
3828 return 1;
3829 }
3830
3831 if (dwarf2_read_index (objfile))
3832 return 1;
3833
9291a0cd
TT
3834 return 0;
3835}
3836
3837\f
3838
dce234bc
PP
3839/* Build a partial symbol table. */
3840
3841void
f29dff0a 3842dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3843{
c9bf0622
TT
3844 volatile struct gdb_exception except;
3845
f29dff0a 3846 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3847 {
3848 init_psymbol_list (objfile, 1024);
3849 }
3850
c9bf0622
TT
3851 TRY_CATCH (except, RETURN_MASK_ERROR)
3852 {
3853 /* This isn't really ideal: all the data we allocate on the
3854 objfile's obstack is still uselessly kept around. However,
3855 freeing it seems unsafe. */
3856 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3857
3858 dwarf2_build_psymtabs_hard (objfile);
3859 discard_cleanups (cleanups);
3860 }
3861 if (except.reason < 0)
3862 exception_print (gdb_stderr, except);
c906108c 3863}
c906108c 3864
1ce1cefd
DE
3865/* Return the total length of the CU described by HEADER. */
3866
3867static unsigned int
3868get_cu_length (const struct comp_unit_head *header)
3869{
3870 return header->initial_length_size + header->length;
3871}
3872
45452591
DE
3873/* Return TRUE if OFFSET is within CU_HEADER. */
3874
3875static inline int
b64f50a1 3876offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3877{
b64f50a1 3878 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3879 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3880
b64f50a1 3881 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3882}
3883
3b80fe9b
DE
3884/* Find the base address of the compilation unit for range lists and
3885 location lists. It will normally be specified by DW_AT_low_pc.
3886 In DWARF-3 draft 4, the base address could be overridden by
3887 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3888 compilation units with discontinuous ranges. */
3889
3890static void
3891dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3892{
3893 struct attribute *attr;
3894
3895 cu->base_known = 0;
3896 cu->base_address = 0;
3897
3898 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3899 if (attr)
3900 {
3901 cu->base_address = DW_ADDR (attr);
3902 cu->base_known = 1;
3903 }
3904 else
3905 {
3906 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3907 if (attr)
3908 {
3909 cu->base_address = DW_ADDR (attr);
3910 cu->base_known = 1;
3911 }
3912 }
3913}
3914
93311388
DE
3915/* Read in the comp unit header information from the debug_info at info_ptr.
3916 NOTE: This leaves members offset, first_die_offset to be filled in
3917 by the caller. */
107d2387 3918
d521ce57 3919static const gdb_byte *
107d2387 3920read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3921 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3922{
3923 int signed_addr;
891d2f0b 3924 unsigned int bytes_read;
c764a876
DE
3925
3926 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3927 cu_header->initial_length_size = bytes_read;
3928 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3929 info_ptr += bytes_read;
107d2387
AC
3930 cu_header->version = read_2_bytes (abfd, info_ptr);
3931 info_ptr += 2;
b64f50a1
JK
3932 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3933 &bytes_read);
613e1657 3934 info_ptr += bytes_read;
107d2387
AC
3935 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3936 info_ptr += 1;
3937 signed_addr = bfd_get_sign_extend_vma (abfd);
3938 if (signed_addr < 0)
8e65ff28 3939 internal_error (__FILE__, __LINE__,
e2e0b3e5 3940 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3941 cu_header->signed_addr_p = signed_addr;
c764a876 3942
107d2387
AC
3943 return info_ptr;
3944}
3945
36586728
TT
3946/* Helper function that returns the proper abbrev section for
3947 THIS_CU. */
3948
3949static struct dwarf2_section_info *
3950get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3951{
3952 struct dwarf2_section_info *abbrev;
3953
3954 if (this_cu->is_dwz)
3955 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3956 else
3957 abbrev = &dwarf2_per_objfile->abbrev;
3958
3959 return abbrev;
3960}
3961
9ff913ba
DE
3962/* Subroutine of read_and_check_comp_unit_head and
3963 read_and_check_type_unit_head to simplify them.
3964 Perform various error checking on the header. */
3965
3966static void
3967error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3968 struct dwarf2_section_info *section,
3969 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3970{
3971 bfd *abfd = section->asection->owner;
3972 const char *filename = bfd_get_filename (abfd);
3973
3974 if (header->version != 2 && header->version != 3 && header->version != 4)
3975 error (_("Dwarf Error: wrong version in compilation unit header "
3976 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3977 filename);
3978
b64f50a1 3979 if (header->abbrev_offset.sect_off
36586728 3980 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3981 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3982 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3983 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3984 filename);
3985
3986 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3987 avoid potential 32-bit overflow. */
1ce1cefd 3988 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3989 > section->size)
3990 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3991 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3992 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3993 filename);
3994}
3995
3996/* Read in a CU/TU header and perform some basic error checking.
3997 The contents of the header are stored in HEADER.
3998 The result is a pointer to the start of the first DIE. */
adabb602 3999
d521ce57 4000static const gdb_byte *
9ff913ba
DE
4001read_and_check_comp_unit_head (struct comp_unit_head *header,
4002 struct dwarf2_section_info *section,
4bdcc0c1 4003 struct dwarf2_section_info *abbrev_section,
d521ce57 4004 const gdb_byte *info_ptr,
9ff913ba 4005 int is_debug_types_section)
72bf9492 4006{
d521ce57 4007 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4008 bfd *abfd = section->asection->owner;
72bf9492 4009
b64f50a1 4010 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4011
72bf9492
DJ
4012 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4013
460c1c54
CC
4014 /* If we're reading a type unit, skip over the signature and
4015 type_offset fields. */
b0df02fd 4016 if (is_debug_types_section)
460c1c54
CC
4017 info_ptr += 8 /*signature*/ + header->offset_size;
4018
b64f50a1 4019 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4020
4bdcc0c1 4021 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4022
4023 return info_ptr;
4024}
4025
348e048f
DE
4026/* Read in the types comp unit header information from .debug_types entry at
4027 types_ptr. The result is a pointer to one past the end of the header. */
4028
d521ce57 4029static const gdb_byte *
9ff913ba
DE
4030read_and_check_type_unit_head (struct comp_unit_head *header,
4031 struct dwarf2_section_info *section,
4bdcc0c1 4032 struct dwarf2_section_info *abbrev_section,
d521ce57 4033 const gdb_byte *info_ptr,
dee91e82
DE
4034 ULONGEST *signature,
4035 cu_offset *type_offset_in_tu)
348e048f 4036{
d521ce57 4037 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4038 bfd *abfd = section->asection->owner;
348e048f 4039
b64f50a1 4040 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4041
9ff913ba 4042 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4043
9ff913ba
DE
4044 /* If we're reading a type unit, skip over the signature and
4045 type_offset fields. */
4046 if (signature != NULL)
4047 *signature = read_8_bytes (abfd, info_ptr);
4048 info_ptr += 8;
dee91e82
DE
4049 if (type_offset_in_tu != NULL)
4050 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4051 header->offset_size);
9ff913ba
DE
4052 info_ptr += header->offset_size;
4053
b64f50a1 4054 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4055
4bdcc0c1 4056 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4057
4058 return info_ptr;
348e048f
DE
4059}
4060
f4dc4d17
DE
4061/* Fetch the abbreviation table offset from a comp or type unit header. */
4062
4063static sect_offset
4064read_abbrev_offset (struct dwarf2_section_info *section,
4065 sect_offset offset)
4066{
4067 bfd *abfd = section->asection->owner;
d521ce57 4068 const gdb_byte *info_ptr;
f4dc4d17
DE
4069 unsigned int length, initial_length_size, offset_size;
4070 sect_offset abbrev_offset;
4071
4072 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4073 info_ptr = section->buffer + offset.sect_off;
4074 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4075 offset_size = initial_length_size == 4 ? 4 : 8;
4076 info_ptr += initial_length_size + 2 /*version*/;
4077 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4078 return abbrev_offset;
4079}
4080
aaa75496
JB
4081/* Allocate a new partial symtab for file named NAME and mark this new
4082 partial symtab as being an include of PST. */
4083
4084static void
d521ce57 4085dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4086 struct objfile *objfile)
4087{
4088 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4089
fbd9ab74
JK
4090 if (!IS_ABSOLUTE_PATH (subpst->filename))
4091 {
4092 /* It shares objfile->objfile_obstack. */
4093 subpst->dirname = pst->dirname;
4094 }
4095
aaa75496
JB
4096 subpst->section_offsets = pst->section_offsets;
4097 subpst->textlow = 0;
4098 subpst->texthigh = 0;
4099
4100 subpst->dependencies = (struct partial_symtab **)
4101 obstack_alloc (&objfile->objfile_obstack,
4102 sizeof (struct partial_symtab *));
4103 subpst->dependencies[0] = pst;
4104 subpst->number_of_dependencies = 1;
4105
4106 subpst->globals_offset = 0;
4107 subpst->n_global_syms = 0;
4108 subpst->statics_offset = 0;
4109 subpst->n_static_syms = 0;
4110 subpst->symtab = NULL;
4111 subpst->read_symtab = pst->read_symtab;
4112 subpst->readin = 0;
4113
4114 /* No private part is necessary for include psymtabs. This property
4115 can be used to differentiate between such include psymtabs and
10b3939b 4116 the regular ones. */
58a9656e 4117 subpst->read_symtab_private = NULL;
aaa75496
JB
4118}
4119
4120/* Read the Line Number Program data and extract the list of files
4121 included by the source file represented by PST. Build an include
d85a05f0 4122 partial symtab for each of these included files. */
aaa75496
JB
4123
4124static void
4125dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4126 struct die_info *die,
4127 struct partial_symtab *pst)
aaa75496 4128{
d85a05f0
DJ
4129 struct line_header *lh = NULL;
4130 struct attribute *attr;
aaa75496 4131
d85a05f0
DJ
4132 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4133 if (attr)
3019eac3 4134 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4135 if (lh == NULL)
4136 return; /* No linetable, so no includes. */
4137
c6da4cef 4138 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4139 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4140
4141 free_line_header (lh);
4142}
4143
348e048f 4144static hashval_t
52dc124a 4145hash_signatured_type (const void *item)
348e048f 4146{
52dc124a 4147 const struct signatured_type *sig_type = item;
9a619af0 4148
348e048f 4149 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4150 return sig_type->signature;
348e048f
DE
4151}
4152
4153static int
52dc124a 4154eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4155{
4156 const struct signatured_type *lhs = item_lhs;
4157 const struct signatured_type *rhs = item_rhs;
9a619af0 4158
348e048f
DE
4159 return lhs->signature == rhs->signature;
4160}
4161
1fd400ff
TT
4162/* Allocate a hash table for signatured types. */
4163
4164static htab_t
673bfd45 4165allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4166{
4167 return htab_create_alloc_ex (41,
52dc124a
DE
4168 hash_signatured_type,
4169 eq_signatured_type,
1fd400ff
TT
4170 NULL,
4171 &objfile->objfile_obstack,
4172 hashtab_obstack_allocate,
4173 dummy_obstack_deallocate);
4174}
4175
d467dd73 4176/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4177
4178static int
d467dd73 4179add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4180{
4181 struct signatured_type *sigt = *slot;
b4dd5633 4182 struct signatured_type ***datap = datum;
1fd400ff 4183
b4dd5633 4184 **datap = sigt;
1fd400ff
TT
4185 ++*datap;
4186
4187 return 1;
4188}
4189
c88ee1f0
DE
4190/* Create the hash table of all entries in the .debug_types
4191 (or .debug_types.dwo) section(s).
4192 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4193 otherwise it is NULL.
4194
4195 The result is a pointer to the hash table or NULL if there are no types.
4196
4197 Note: This function processes DWO files only, not DWP files. */
348e048f 4198
3019eac3
DE
4199static htab_t
4200create_debug_types_hash_table (struct dwo_file *dwo_file,
4201 VEC (dwarf2_section_info_def) *types)
348e048f 4202{
3019eac3 4203 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4204 htab_t types_htab = NULL;
8b70b953
TT
4205 int ix;
4206 struct dwarf2_section_info *section;
4bdcc0c1 4207 struct dwarf2_section_info *abbrev_section;
348e048f 4208
3019eac3
DE
4209 if (VEC_empty (dwarf2_section_info_def, types))
4210 return NULL;
348e048f 4211
4bdcc0c1
DE
4212 abbrev_section = (dwo_file != NULL
4213 ? &dwo_file->sections.abbrev
4214 : &dwarf2_per_objfile->abbrev);
4215
09406207
DE
4216 if (dwarf2_read_debug)
4217 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4218 dwo_file ? ".dwo" : "",
4219 bfd_get_filename (abbrev_section->asection->owner));
4220
8b70b953 4221 for (ix = 0;
3019eac3 4222 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4223 ++ix)
4224 {
3019eac3 4225 bfd *abfd;
d521ce57 4226 const gdb_byte *info_ptr, *end_ptr;
36586728 4227 struct dwarf2_section_info *abbrev_section;
348e048f 4228
8b70b953
TT
4229 dwarf2_read_section (objfile, section);
4230 info_ptr = section->buffer;
348e048f 4231
8b70b953
TT
4232 if (info_ptr == NULL)
4233 continue;
348e048f 4234
3019eac3
DE
4235 /* We can't set abfd until now because the section may be empty or
4236 not present, in which case section->asection will be NULL. */
4237 abfd = section->asection->owner;
4238
36586728
TT
4239 if (dwo_file)
4240 abbrev_section = &dwo_file->sections.abbrev;
4241 else
4242 abbrev_section = &dwarf2_per_objfile->abbrev;
4243
dee91e82
DE
4244 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4245 because we don't need to read any dies: the signature is in the
4246 header. */
8b70b953
TT
4247
4248 end_ptr = info_ptr + section->size;
4249 while (info_ptr < end_ptr)
4250 {
b64f50a1 4251 sect_offset offset;
3019eac3 4252 cu_offset type_offset_in_tu;
8b70b953 4253 ULONGEST signature;
52dc124a 4254 struct signatured_type *sig_type;
3019eac3 4255 struct dwo_unit *dwo_tu;
8b70b953 4256 void **slot;
d521ce57 4257 const gdb_byte *ptr = info_ptr;
9ff913ba 4258 struct comp_unit_head header;
dee91e82 4259 unsigned int length;
348e048f 4260
b64f50a1 4261 offset.sect_off = ptr - section->buffer;
348e048f 4262
8b70b953 4263 /* We need to read the type's signature in order to build the hash
9ff913ba 4264 table, but we don't need anything else just yet. */
348e048f 4265
4bdcc0c1
DE
4266 ptr = read_and_check_type_unit_head (&header, section,
4267 abbrev_section, ptr,
3019eac3 4268 &signature, &type_offset_in_tu);
6caca83c 4269
1ce1cefd 4270 length = get_cu_length (&header);
dee91e82 4271
6caca83c 4272 /* Skip dummy type units. */
dee91e82
DE
4273 if (ptr >= info_ptr + length
4274 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4275 {
1ce1cefd 4276 info_ptr += length;
6caca83c
CC
4277 continue;
4278 }
8b70b953 4279
0349ea22
DE
4280 if (types_htab == NULL)
4281 {
4282 if (dwo_file)
4283 types_htab = allocate_dwo_unit_table (objfile);
4284 else
4285 types_htab = allocate_signatured_type_table (objfile);
4286 }
4287
3019eac3
DE
4288 if (dwo_file)
4289 {
4290 sig_type = NULL;
4291 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4292 struct dwo_unit);
4293 dwo_tu->dwo_file = dwo_file;
4294 dwo_tu->signature = signature;
4295 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4296 dwo_tu->section = section;
3019eac3
DE
4297 dwo_tu->offset = offset;
4298 dwo_tu->length = length;
4299 }
4300 else
4301 {
4302 /* N.B.: type_offset is not usable if this type uses a DWO file.
4303 The real type_offset is in the DWO file. */
4304 dwo_tu = NULL;
4305 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4306 struct signatured_type);
4307 sig_type->signature = signature;
4308 sig_type->type_offset_in_tu = type_offset_in_tu;
4309 sig_type->per_cu.objfile = objfile;
4310 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4311 sig_type->per_cu.section = section;
3019eac3
DE
4312 sig_type->per_cu.offset = offset;
4313 sig_type->per_cu.length = length;
4314 }
8b70b953 4315
3019eac3
DE
4316 slot = htab_find_slot (types_htab,
4317 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4318 INSERT);
8b70b953
TT
4319 gdb_assert (slot != NULL);
4320 if (*slot != NULL)
4321 {
3019eac3
DE
4322 sect_offset dup_offset;
4323
4324 if (dwo_file)
4325 {
4326 const struct dwo_unit *dup_tu = *slot;
4327
4328 dup_offset = dup_tu->offset;
4329 }
4330 else
4331 {
4332 const struct signatured_type *dup_tu = *slot;
4333
4334 dup_offset = dup_tu->per_cu.offset;
4335 }
b3c8eb43 4336
8b70b953 4337 complaint (&symfile_complaints,
c88ee1f0 4338 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4339 " the entry at offset 0x%x, signature %s"),
3019eac3 4340 offset.sect_off, dup_offset.sect_off,
4031ecc5 4341 hex_string (signature));
8b70b953 4342 }
3019eac3 4343 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4344
09406207 4345 if (dwarf2_read_debug)
4031ecc5 4346 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4347 offset.sect_off,
4031ecc5 4348 hex_string (signature));
348e048f 4349
dee91e82 4350 info_ptr += length;
8b70b953 4351 }
348e048f
DE
4352 }
4353
3019eac3
DE
4354 return types_htab;
4355}
4356
4357/* Create the hash table of all entries in the .debug_types section,
4358 and initialize all_type_units.
4359 The result is zero if there is an error (e.g. missing .debug_types section),
4360 otherwise non-zero. */
4361
4362static int
4363create_all_type_units (struct objfile *objfile)
4364{
4365 htab_t types_htab;
b4dd5633 4366 struct signatured_type **iter;
3019eac3
DE
4367
4368 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4369 if (types_htab == NULL)
4370 {
4371 dwarf2_per_objfile->signatured_types = NULL;
4372 return 0;
4373 }
4374
348e048f
DE
4375 dwarf2_per_objfile->signatured_types = types_htab;
4376
d467dd73
DE
4377 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4378 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4379 = xmalloc (dwarf2_per_objfile->n_type_units
4380 * sizeof (struct signatured_type *));
d467dd73
DE
4381 iter = &dwarf2_per_objfile->all_type_units[0];
4382 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4383 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4384 == dwarf2_per_objfile->n_type_units);
1fd400ff 4385
348e048f
DE
4386 return 1;
4387}
4388
a2ce51a0
DE
4389/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4390 Fill in SIG_ENTRY with DWO_ENTRY. */
4391
4392static void
4393fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4394 struct signatured_type *sig_entry,
4395 struct dwo_unit *dwo_entry)
4396{
7ee85ab1 4397 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4398 gdb_assert (! sig_entry->per_cu.queued);
4399 gdb_assert (sig_entry->per_cu.cu == NULL);
4400 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4401 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4402 gdb_assert (sig_entry->signature == dwo_entry->signature);
4403 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4404 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4405 gdb_assert (sig_entry->dwo_unit == NULL);
4406
4407 sig_entry->per_cu.section = dwo_entry->section;
4408 sig_entry->per_cu.offset = dwo_entry->offset;
4409 sig_entry->per_cu.length = dwo_entry->length;
4410 sig_entry->per_cu.reading_dwo_directly = 1;
4411 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4412 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4413 sig_entry->dwo_unit = dwo_entry;
4414}
4415
4416/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4417 If we haven't read the TU yet, create the signatured_type data structure
4418 for a TU to be read in directly from a DWO file, bypassing the stub.
4419 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4420 using .gdb_index, then when reading a CU we want to stay in the DWO file
4421 containing that CU. Otherwise we could end up reading several other DWO
4422 files (due to comdat folding) to process the transitive closure of all the
4423 mentioned TUs, and that can be slow. The current DWO file will have every
4424 type signature that it needs.
a2ce51a0
DE
4425 We only do this for .gdb_index because in the psymtab case we already have
4426 to read all the DWOs to build the type unit groups. */
4427
4428static struct signatured_type *
4429lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4430{
4431 struct objfile *objfile = dwarf2_per_objfile->objfile;
4432 struct dwo_file *dwo_file;
4433 struct dwo_unit find_dwo_entry, *dwo_entry;
4434 struct signatured_type find_sig_entry, *sig_entry;
4435
4436 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4437
4438 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4439 dwo_unit of the TU itself. */
4440 dwo_file = cu->dwo_unit->dwo_file;
4441
4442 /* We only ever need to read in one copy of a signatured type.
4443 Just use the global signatured_types array. If this is the first time
4444 we're reading this type, replace the recorded data from .gdb_index with
4445 this TU. */
4446
4447 if (dwarf2_per_objfile->signatured_types == NULL)
4448 return NULL;
4449 find_sig_entry.signature = sig;
4450 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4451 if (sig_entry == NULL)
4452 return NULL;
7ee85ab1
DE
4453
4454 /* We can get here with the TU already read, *or* in the process of being
4455 read. Don't reassign it if that's the case. Also note that if the TU is
4456 already being read, it may not have come from a DWO, the program may be
4457 a mix of Fission-compiled code and non-Fission-compiled code. */
a2ce51a0 4458 /* Have we already tried to read this TU? */
7ee85ab1 4459 if (sig_entry->per_cu.tu_read)
a2ce51a0
DE
4460 return sig_entry;
4461
4462 /* Ok, this is the first time we're reading this TU. */
4463 if (dwo_file->tus == NULL)
4464 return NULL;
4465 find_dwo_entry.signature = sig;
4466 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4467 if (dwo_entry == NULL)
4468 return NULL;
4469
4470 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4471 return sig_entry;
4472}
4473
4474/* Subroutine of lookup_dwp_signatured_type.
4475 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4476
4477static struct signatured_type *
4478add_type_unit (ULONGEST sig)
4479{
4480 struct objfile *objfile = dwarf2_per_objfile->objfile;
4481 int n_type_units = dwarf2_per_objfile->n_type_units;
4482 struct signatured_type *sig_type;
4483 void **slot;
4484
4485 ++n_type_units;
4486 dwarf2_per_objfile->all_type_units =
4487 xrealloc (dwarf2_per_objfile->all_type_units,
4488 n_type_units * sizeof (struct signatured_type *));
4489 dwarf2_per_objfile->n_type_units = n_type_units;
4490 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4491 struct signatured_type);
4492 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4493 sig_type->signature = sig;
4494 sig_type->per_cu.is_debug_types = 1;
4495 sig_type->per_cu.v.quick =
4496 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4497 struct dwarf2_per_cu_quick_data);
4498 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4499 sig_type, INSERT);
4500 gdb_assert (*slot == NULL);
4501 *slot = sig_type;
4502 /* The rest of sig_type must be filled in by the caller. */
4503 return sig_type;
4504}
4505
4506/* Subroutine of lookup_signatured_type.
4507 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4508 then try the DWP file.
4509 Normally this "can't happen", but if there's a bug in signature
4510 generation and/or the DWP file is built incorrectly, it can happen.
4511 Using the type directly from the DWP file means we don't have the stub
4512 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4513 not critical. [Eventually the stub may go away for type units anyway.] */
4514
4515static struct signatured_type *
4516lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4517{
4518 struct objfile *objfile = dwarf2_per_objfile->objfile;
4519 struct dwp_file *dwp_file = get_dwp_file ();
4520 struct dwo_unit *dwo_entry;
4521 struct signatured_type find_sig_entry, *sig_entry;
4522
4523 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4524 gdb_assert (dwp_file != NULL);
4525
4526 if (dwarf2_per_objfile->signatured_types != NULL)
4527 {
4528 find_sig_entry.signature = sig;
4529 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4530 &find_sig_entry);
4531 if (sig_entry != NULL)
4532 return sig_entry;
4533 }
4534
4535 /* This is the "shouldn't happen" case.
4536 Try the DWP file and hope for the best. */
4537 if (dwp_file->tus == NULL)
4538 return NULL;
4539 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4540 sig, 1 /* is_debug_types */);
4541 if (dwo_entry == NULL)
4542 return NULL;
4543
4544 sig_entry = add_type_unit (sig);
4545 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4546
4547 /* The caller will signal a complaint if we return NULL.
4548 Here we don't return NULL but we still want to complain. */
4549 complaint (&symfile_complaints,
4550 _("Bad type signature %s referenced by %s at 0x%x,"
4551 " coping by using copy in DWP [in module %s]"),
4552 hex_string (sig),
4553 cu->per_cu->is_debug_types ? "TU" : "CU",
4554 cu->per_cu->offset.sect_off,
4555 objfile->name);
4556
4557 return sig_entry;
4558}
4559
380bca97 4560/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4561 Returns NULL if signature SIG is not present in the table.
4562 It is up to the caller to complain about this. */
348e048f
DE
4563
4564static struct signatured_type *
a2ce51a0 4565lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4566{
a2ce51a0
DE
4567 if (cu->dwo_unit
4568 && dwarf2_per_objfile->using_index)
4569 {
4570 /* We're in a DWO/DWP file, and we're using .gdb_index.
4571 These cases require special processing. */
4572 if (get_dwp_file () == NULL)
4573 return lookup_dwo_signatured_type (cu, sig);
4574 else
4575 return lookup_dwp_signatured_type (cu, sig);
4576 }
4577 else
4578 {
4579 struct signatured_type find_entry, *entry;
348e048f 4580
a2ce51a0
DE
4581 if (dwarf2_per_objfile->signatured_types == NULL)
4582 return NULL;
4583 find_entry.signature = sig;
4584 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4585 return entry;
4586 }
348e048f 4587}
42e7ad6c
DE
4588\f
4589/* Low level DIE reading support. */
348e048f 4590
d85a05f0
DJ
4591/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4592
4593static void
4594init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4595 struct dwarf2_cu *cu,
3019eac3
DE
4596 struct dwarf2_section_info *section,
4597 struct dwo_file *dwo_file)
d85a05f0 4598{
fceca515 4599 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4600 reader->abfd = section->asection->owner;
d85a05f0 4601 reader->cu = cu;
3019eac3 4602 reader->dwo_file = dwo_file;
dee91e82
DE
4603 reader->die_section = section;
4604 reader->buffer = section->buffer;
f664829e 4605 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4606 reader->comp_dir = NULL;
d85a05f0
DJ
4607}
4608
b0c7bfa9
DE
4609/* Subroutine of init_cutu_and_read_dies to simplify it.
4610 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4611 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4612 already.
4613
4614 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4615 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4616 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4617 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4618 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4619 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4620 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4621 are filled in with the info of the DIE from the DWO file.
4622 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4623 provided an abbrev table to use.
4624 The result is non-zero if a valid (non-dummy) DIE was found. */
4625
4626static int
4627read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4628 struct dwo_unit *dwo_unit,
4629 int abbrev_table_provided,
4630 struct die_info *stub_comp_unit_die,
a2ce51a0 4631 const char *stub_comp_dir,
b0c7bfa9 4632 struct die_reader_specs *result_reader,
d521ce57 4633 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4634 struct die_info **result_comp_unit_die,
4635 int *result_has_children)
4636{
4637 struct objfile *objfile = dwarf2_per_objfile->objfile;
4638 struct dwarf2_cu *cu = this_cu->cu;
4639 struct dwarf2_section_info *section;
4640 bfd *abfd;
d521ce57 4641 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4642 const char *comp_dir_string;
4643 ULONGEST signature; /* Or dwo_id. */
4644 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4645 int i,num_extra_attrs;
4646 struct dwarf2_section_info *dwo_abbrev_section;
4647 struct attribute *attr;
a2ce51a0 4648 struct attribute comp_dir_attr;
b0c7bfa9
DE
4649 struct die_info *comp_unit_die;
4650
a2ce51a0
DE
4651 /* Both can't be provided. */
4652 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4653
b0c7bfa9
DE
4654 /* These attributes aren't processed until later:
4655 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4656 However, the attribute is found in the stub which we won't have later.
4657 In order to not impose this complication on the rest of the code,
4658 we read them here and copy them to the DWO CU/TU die. */
4659
4660 stmt_list = NULL;
4661 low_pc = NULL;
4662 high_pc = NULL;
4663 ranges = NULL;
4664 comp_dir = NULL;
4665
4666 if (stub_comp_unit_die != NULL)
4667 {
4668 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4669 DWO file. */
4670 if (! this_cu->is_debug_types)
4671 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4672 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4673 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4674 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4675 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4676
4677 /* There should be a DW_AT_addr_base attribute here (if needed).
4678 We need the value before we can process DW_FORM_GNU_addr_index. */
4679 cu->addr_base = 0;
4680 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4681 if (attr)
4682 cu->addr_base = DW_UNSND (attr);
4683
4684 /* There should be a DW_AT_ranges_base attribute here (if needed).
4685 We need the value before we can process DW_AT_ranges. */
4686 cu->ranges_base = 0;
4687 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4688 if (attr)
4689 cu->ranges_base = DW_UNSND (attr);
4690 }
a2ce51a0
DE
4691 else if (stub_comp_dir != NULL)
4692 {
4693 /* Reconstruct the comp_dir attribute to simplify the code below. */
4694 comp_dir = (struct attribute *)
4695 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4696 comp_dir->name = DW_AT_comp_dir;
4697 comp_dir->form = DW_FORM_string;
4698 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4699 DW_STRING (comp_dir) = stub_comp_dir;
4700 }
b0c7bfa9
DE
4701
4702 /* Set up for reading the DWO CU/TU. */
4703 cu->dwo_unit = dwo_unit;
4704 section = dwo_unit->section;
4705 dwarf2_read_section (objfile, section);
4706 abfd = section->asection->owner;
4707 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4708 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4709 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4710
4711 if (this_cu->is_debug_types)
4712 {
4713 ULONGEST header_signature;
4714 cu_offset type_offset_in_tu;
4715 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4716
4717 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4718 dwo_abbrev_section,
4719 info_ptr,
4720 &header_signature,
4721 &type_offset_in_tu);
a2ce51a0
DE
4722 /* This is not an assert because it can be caused by bad debug info. */
4723 if (sig_type->signature != header_signature)
4724 {
4725 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4726 " TU at offset 0x%x [in module %s]"),
4727 hex_string (sig_type->signature),
4728 hex_string (header_signature),
4729 dwo_unit->offset.sect_off,
4730 bfd_get_filename (abfd));
4731 }
b0c7bfa9
DE
4732 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4733 /* For DWOs coming from DWP files, we don't know the CU length
4734 nor the type's offset in the TU until now. */
4735 dwo_unit->length = get_cu_length (&cu->header);
4736 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4737
4738 /* Establish the type offset that can be used to lookup the type.
4739 For DWO files, we don't know it until now. */
4740 sig_type->type_offset_in_section.sect_off =
4741 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4742 }
4743 else
4744 {
4745 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4746 dwo_abbrev_section,
4747 info_ptr, 0);
4748 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4749 /* For DWOs coming from DWP files, we don't know the CU length
4750 until now. */
4751 dwo_unit->length = get_cu_length (&cu->header);
4752 }
4753
02142a6c
DE
4754 /* Replace the CU's original abbrev table with the DWO's.
4755 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4756 if (abbrev_table_provided)
4757 {
4758 /* Don't free the provided abbrev table, the caller of
4759 init_cutu_and_read_dies owns it. */
4760 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4761 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4762 make_cleanup (dwarf2_free_abbrev_table, cu);
4763 }
4764 else
4765 {
4766 dwarf2_free_abbrev_table (cu);
4767 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4768 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4769 }
4770
4771 /* Read in the die, but leave space to copy over the attributes
4772 from the stub. This has the benefit of simplifying the rest of
4773 the code - all the work to maintain the illusion of a single
4774 DW_TAG_{compile,type}_unit DIE is done here. */
4775 num_extra_attrs = ((stmt_list != NULL)
4776 + (low_pc != NULL)
4777 + (high_pc != NULL)
4778 + (ranges != NULL)
4779 + (comp_dir != NULL));
4780 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4781 result_has_children, num_extra_attrs);
4782
4783 /* Copy over the attributes from the stub to the DIE we just read in. */
4784 comp_unit_die = *result_comp_unit_die;
4785 i = comp_unit_die->num_attrs;
4786 if (stmt_list != NULL)
4787 comp_unit_die->attrs[i++] = *stmt_list;
4788 if (low_pc != NULL)
4789 comp_unit_die->attrs[i++] = *low_pc;
4790 if (high_pc != NULL)
4791 comp_unit_die->attrs[i++] = *high_pc;
4792 if (ranges != NULL)
4793 comp_unit_die->attrs[i++] = *ranges;
4794 if (comp_dir != NULL)
4795 comp_unit_die->attrs[i++] = *comp_dir;
4796 comp_unit_die->num_attrs += num_extra_attrs;
4797
bf6af496
DE
4798 if (dwarf2_die_debug)
4799 {
4800 fprintf_unfiltered (gdb_stdlog,
4801 "Read die from %s@0x%x of %s:\n",
4802 bfd_section_name (abfd, section->asection),
4803 (unsigned) (begin_info_ptr - section->buffer),
4804 bfd_get_filename (abfd));
4805 dump_die (comp_unit_die, dwarf2_die_debug);
4806 }
4807
a2ce51a0
DE
4808 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4809 TUs by skipping the stub and going directly to the entry in the DWO file.
4810 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4811 to get it via circuitous means. Blech. */
4812 if (comp_dir != NULL)
4813 result_reader->comp_dir = DW_STRING (comp_dir);
4814
b0c7bfa9
DE
4815 /* Skip dummy compilation units. */
4816 if (info_ptr >= begin_info_ptr + dwo_unit->length
4817 || peek_abbrev_code (abfd, info_ptr) == 0)
4818 return 0;
4819
4820 *result_info_ptr = info_ptr;
4821 return 1;
4822}
4823
4824/* Subroutine of init_cutu_and_read_dies to simplify it.
4825 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4826 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4827
4828static struct dwo_unit *
4829lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4830 struct die_info *comp_unit_die)
4831{
4832 struct dwarf2_cu *cu = this_cu->cu;
4833 struct attribute *attr;
4834 ULONGEST signature;
4835 struct dwo_unit *dwo_unit;
4836 const char *comp_dir, *dwo_name;
4837
a2ce51a0
DE
4838 gdb_assert (cu != NULL);
4839
b0c7bfa9
DE
4840 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4841 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4842 gdb_assert (attr != NULL);
4843 dwo_name = DW_STRING (attr);
4844 comp_dir = NULL;
4845 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4846 if (attr)
4847 comp_dir = DW_STRING (attr);
4848
4849 if (this_cu->is_debug_types)
4850 {
4851 struct signatured_type *sig_type;
4852
4853 /* Since this_cu is the first member of struct signatured_type,
4854 we can go from a pointer to one to a pointer to the other. */
4855 sig_type = (struct signatured_type *) this_cu;
4856 signature = sig_type->signature;
4857 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4858 }
4859 else
4860 {
4861 struct attribute *attr;
4862
4863 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4864 if (! attr)
4865 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4866 " [in module %s]"),
4867 dwo_name, this_cu->objfile->name);
4868 signature = DW_UNSND (attr);
4869 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4870 signature);
4871 }
4872
b0c7bfa9
DE
4873 return dwo_unit;
4874}
4875
a2ce51a0
DE
4876/* Subroutine of init_cutu_and_read_dies to simplify it.
4877 Read a TU directly from a DWO file, bypassing the stub. */
4878
4879static void
4880init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4881 die_reader_func_ftype *die_reader_func,
4882 void *data)
4883{
4884 struct dwarf2_cu *cu;
4885 struct signatured_type *sig_type;
4886 struct cleanup *cleanups, *free_cu_cleanup;
4887 struct die_reader_specs reader;
4888 const gdb_byte *info_ptr;
4889 struct die_info *comp_unit_die;
4890 int has_children;
4891
4892 /* Verify we can do the following downcast, and that we have the
4893 data we need. */
4894 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4895 sig_type = (struct signatured_type *) this_cu;
4896 gdb_assert (sig_type->dwo_unit != NULL);
4897
4898 cleanups = make_cleanup (null_cleanup, NULL);
4899
4900 gdb_assert (this_cu->cu == NULL);
4901 cu = xmalloc (sizeof (*cu));
4902 init_one_comp_unit (cu, this_cu);
4903 /* If an error occurs while loading, release our storage. */
4904 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4905
4906 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4907 0 /* abbrev_table_provided */,
4908 NULL /* stub_comp_unit_die */,
4909 sig_type->dwo_unit->dwo_file->comp_dir,
4910 &reader, &info_ptr,
4911 &comp_unit_die, &has_children) == 0)
4912 {
4913 /* Dummy die. */
4914 do_cleanups (cleanups);
4915 return;
4916 }
4917
4918 /* All the "real" work is done here. */
4919 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4920
4921 /* This duplicates some code in init_cutu_and_read_dies,
4922 but the alternative is making the latter more complex.
4923 This function is only for the special case of using DWO files directly:
4924 no point in overly complicating the general case just to handle this. */
4925 if (keep)
4926 {
4927 /* We've successfully allocated this compilation unit. Let our
4928 caller clean it up when finished with it. */
4929 discard_cleanups (free_cu_cleanup);
4930
4931 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4932 So we have to manually free the abbrev table. */
4933 dwarf2_free_abbrev_table (cu);
4934
4935 /* Link this CU into read_in_chain. */
4936 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4937 dwarf2_per_objfile->read_in_chain = this_cu;
4938 }
4939 else
4940 do_cleanups (free_cu_cleanup);
4941
4942 do_cleanups (cleanups);
4943}
4944
fd820528 4945/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4946 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4947
f4dc4d17
DE
4948 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4949 Otherwise the table specified in the comp unit header is read in and used.
4950 This is an optimization for when we already have the abbrev table.
4951
dee91e82
DE
4952 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4953 Otherwise, a new CU is allocated with xmalloc.
4954
4955 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4956 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4957
4958 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4959 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4960
70221824 4961static void
fd820528 4962init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4963 struct abbrev_table *abbrev_table,
fd820528
DE
4964 int use_existing_cu, int keep,
4965 die_reader_func_ftype *die_reader_func,
4966 void *data)
c906108c 4967{
dee91e82 4968 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4969 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4970 bfd *abfd = section->asection->owner;
dee91e82 4971 struct dwarf2_cu *cu;
d521ce57 4972 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4973 struct die_reader_specs reader;
d85a05f0 4974 struct die_info *comp_unit_die;
dee91e82 4975 int has_children;
d85a05f0 4976 struct attribute *attr;
365156ad 4977 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 4978 struct signatured_type *sig_type = NULL;
4bdcc0c1 4979 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4980 /* Non-zero if CU currently points to a DWO file and we need to
4981 reread it. When this happens we need to reread the skeleton die
a2ce51a0 4982 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 4983 int rereading_dwo_cu = 0;
c906108c 4984
09406207
DE
4985 if (dwarf2_die_debug)
4986 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4987 this_cu->is_debug_types ? "type" : "comp",
4988 this_cu->offset.sect_off);
4989
dee91e82
DE
4990 if (use_existing_cu)
4991 gdb_assert (keep);
23745b47 4992
a2ce51a0
DE
4993 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4994 file (instead of going through the stub), short-circuit all of this. */
4995 if (this_cu->reading_dwo_directly)
4996 {
4997 /* Narrow down the scope of possibilities to have to understand. */
4998 gdb_assert (this_cu->is_debug_types);
4999 gdb_assert (abbrev_table == NULL);
5000 gdb_assert (!use_existing_cu);
5001 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5002 return;
5003 }
5004
dee91e82
DE
5005 cleanups = make_cleanup (null_cleanup, NULL);
5006
5007 /* This is cheap if the section is already read in. */
5008 dwarf2_read_section (objfile, section);
5009
5010 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5011
5012 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5013
5014 if (use_existing_cu && this_cu->cu != NULL)
5015 {
5016 cu = this_cu->cu;
42e7ad6c
DE
5017
5018 /* If this CU is from a DWO file we need to start over, we need to
5019 refetch the attributes from the skeleton CU.
5020 This could be optimized by retrieving those attributes from when we
5021 were here the first time: the previous comp_unit_die was stored in
5022 comp_unit_obstack. But there's no data yet that we need this
5023 optimization. */
5024 if (cu->dwo_unit != NULL)
5025 rereading_dwo_cu = 1;
dee91e82
DE
5026 }
5027 else
5028 {
5029 /* If !use_existing_cu, this_cu->cu must be NULL. */
5030 gdb_assert (this_cu->cu == NULL);
5031
5032 cu = xmalloc (sizeof (*cu));
5033 init_one_comp_unit (cu, this_cu);
5034
5035 /* If an error occurs while loading, release our storage. */
365156ad 5036 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5037 }
dee91e82 5038
b0c7bfa9 5039 /* Get the header. */
42e7ad6c
DE
5040 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5041 {
5042 /* We already have the header, there's no need to read it in again. */
5043 info_ptr += cu->header.first_die_offset.cu_off;
5044 }
5045 else
5046 {
3019eac3 5047 if (this_cu->is_debug_types)
dee91e82
DE
5048 {
5049 ULONGEST signature;
42e7ad6c 5050 cu_offset type_offset_in_tu;
dee91e82 5051
4bdcc0c1
DE
5052 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5053 abbrev_section, info_ptr,
42e7ad6c
DE
5054 &signature,
5055 &type_offset_in_tu);
dee91e82 5056
42e7ad6c
DE
5057 /* Since per_cu is the first member of struct signatured_type,
5058 we can go from a pointer to one to a pointer to the other. */
5059 sig_type = (struct signatured_type *) this_cu;
5060 gdb_assert (sig_type->signature == signature);
5061 gdb_assert (sig_type->type_offset_in_tu.cu_off
5062 == type_offset_in_tu.cu_off);
dee91e82
DE
5063 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5064
42e7ad6c
DE
5065 /* LENGTH has not been set yet for type units if we're
5066 using .gdb_index. */
1ce1cefd 5067 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5068
5069 /* Establish the type offset that can be used to lookup the type. */
5070 sig_type->type_offset_in_section.sect_off =
5071 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5072 }
5073 else
5074 {
4bdcc0c1
DE
5075 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5076 abbrev_section,
5077 info_ptr, 0);
dee91e82
DE
5078
5079 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5080 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5081 }
5082 }
10b3939b 5083
6caca83c 5084 /* Skip dummy compilation units. */
dee91e82 5085 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5086 || peek_abbrev_code (abfd, info_ptr) == 0)
5087 {
dee91e82 5088 do_cleanups (cleanups);
21b2bd31 5089 return;
6caca83c
CC
5090 }
5091
433df2d4
DE
5092 /* If we don't have them yet, read the abbrevs for this compilation unit.
5093 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5094 done. Note that it's important that if the CU had an abbrev table
5095 on entry we don't free it when we're done: Somewhere up the call stack
5096 it may be in use. */
f4dc4d17
DE
5097 if (abbrev_table != NULL)
5098 {
5099 gdb_assert (cu->abbrev_table == NULL);
5100 gdb_assert (cu->header.abbrev_offset.sect_off
5101 == abbrev_table->offset.sect_off);
5102 cu->abbrev_table = abbrev_table;
5103 }
5104 else if (cu->abbrev_table == NULL)
dee91e82 5105 {
4bdcc0c1 5106 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5107 make_cleanup (dwarf2_free_abbrev_table, cu);
5108 }
42e7ad6c
DE
5109 else if (rereading_dwo_cu)
5110 {
5111 dwarf2_free_abbrev_table (cu);
5112 dwarf2_read_abbrevs (cu, abbrev_section);
5113 }
af703f96 5114
dee91e82 5115 /* Read the top level CU/TU die. */
3019eac3 5116 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5117 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5118
b0c7bfa9
DE
5119 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5120 from the DWO file.
5121 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5122 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5123 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5124 if (attr)
5125 {
3019eac3 5126 struct dwo_unit *dwo_unit;
b0c7bfa9 5127 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5128
5129 if (has_children)
6a506a2d
DE
5130 {
5131 complaint (&symfile_complaints,
5132 _("compilation unit with DW_AT_GNU_dwo_name"
5133 " has children (offset 0x%x) [in module %s]"),
5134 this_cu->offset.sect_off, bfd_get_filename (abfd));
5135 }
b0c7bfa9 5136 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5137 if (dwo_unit != NULL)
3019eac3 5138 {
6a506a2d
DE
5139 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5140 abbrev_table != NULL,
a2ce51a0 5141 comp_unit_die, NULL,
6a506a2d
DE
5142 &reader, &info_ptr,
5143 &dwo_comp_unit_die, &has_children) == 0)
5144 {
5145 /* Dummy die. */
5146 do_cleanups (cleanups);
5147 return;
5148 }
5149 comp_unit_die = dwo_comp_unit_die;
5150 }
5151 else
5152 {
5153 /* Yikes, we couldn't find the rest of the DIE, we only have
5154 the stub. A complaint has already been logged. There's
5155 not much more we can do except pass on the stub DIE to
5156 die_reader_func. We don't want to throw an error on bad
5157 debug info. */
3019eac3
DE
5158 }
5159 }
5160
b0c7bfa9 5161 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5162 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5163
b0c7bfa9 5164 /* Done, clean up. */
365156ad 5165 if (free_cu_cleanup != NULL)
348e048f 5166 {
365156ad
TT
5167 if (keep)
5168 {
5169 /* We've successfully allocated this compilation unit. Let our
5170 caller clean it up when finished with it. */
5171 discard_cleanups (free_cu_cleanup);
dee91e82 5172
365156ad
TT
5173 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5174 So we have to manually free the abbrev table. */
5175 dwarf2_free_abbrev_table (cu);
dee91e82 5176
365156ad
TT
5177 /* Link this CU into read_in_chain. */
5178 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5179 dwarf2_per_objfile->read_in_chain = this_cu;
5180 }
5181 else
5182 do_cleanups (free_cu_cleanup);
348e048f 5183 }
365156ad
TT
5184
5185 do_cleanups (cleanups);
dee91e82
DE
5186}
5187
3019eac3
DE
5188/* Read CU/TU THIS_CU in section SECTION,
5189 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5190 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5191 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5192
5193 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5194 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5195
5196 We fill in THIS_CU->length.
5197
5198 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5199 linker) then DIE_READER_FUNC will not get called.
5200
5201 THIS_CU->cu is always freed when done.
3019eac3
DE
5202 This is done in order to not leave THIS_CU->cu in a state where we have
5203 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5204
5205static void
5206init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5207 struct dwarf2_section_info *abbrev_section,
3019eac3 5208 struct dwo_file *dwo_file,
dee91e82
DE
5209 die_reader_func_ftype *die_reader_func,
5210 void *data)
5211{
5212 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5213 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5214 bfd *abfd = section->asection->owner;
dee91e82 5215 struct dwarf2_cu cu;
d521ce57 5216 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5217 struct die_reader_specs reader;
5218 struct cleanup *cleanups;
5219 struct die_info *comp_unit_die;
5220 int has_children;
5221
09406207
DE
5222 if (dwarf2_die_debug)
5223 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5224 this_cu->is_debug_types ? "type" : "comp",
5225 this_cu->offset.sect_off);
5226
dee91e82
DE
5227 gdb_assert (this_cu->cu == NULL);
5228
dee91e82
DE
5229 /* This is cheap if the section is already read in. */
5230 dwarf2_read_section (objfile, section);
5231
5232 init_one_comp_unit (&cu, this_cu);
5233
5234 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5235
5236 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5237 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5238 abbrev_section, info_ptr,
3019eac3 5239 this_cu->is_debug_types);
dee91e82 5240
1ce1cefd 5241 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5242
5243 /* Skip dummy compilation units. */
5244 if (info_ptr >= begin_info_ptr + this_cu->length
5245 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5246 {
dee91e82 5247 do_cleanups (cleanups);
21b2bd31 5248 return;
93311388 5249 }
72bf9492 5250
dee91e82
DE
5251 dwarf2_read_abbrevs (&cu, abbrev_section);
5252 make_cleanup (dwarf2_free_abbrev_table, &cu);
5253
3019eac3 5254 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5255 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5256
5257 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5258
5259 do_cleanups (cleanups);
5260}
5261
3019eac3
DE
5262/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5263 does not lookup the specified DWO file.
5264 This cannot be used to read DWO files.
dee91e82
DE
5265
5266 THIS_CU->cu is always freed when done.
3019eac3
DE
5267 This is done in order to not leave THIS_CU->cu in a state where we have
5268 to care whether it refers to the "main" CU or the DWO CU.
5269 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5270
5271static void
5272init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5273 die_reader_func_ftype *die_reader_func,
5274 void *data)
5275{
5276 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5277 get_abbrev_section_for_cu (this_cu),
3019eac3 5278 NULL,
dee91e82
DE
5279 die_reader_func, data);
5280}
0018ea6f
DE
5281\f
5282/* Type Unit Groups.
dee91e82 5283
0018ea6f
DE
5284 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5285 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5286 so that all types coming from the same compilation (.o file) are grouped
5287 together. A future step could be to put the types in the same symtab as
5288 the CU the types ultimately came from. */
ff013f42 5289
f4dc4d17
DE
5290static hashval_t
5291hash_type_unit_group (const void *item)
5292{
094b34ac 5293 const struct type_unit_group *tu_group = item;
f4dc4d17 5294
094b34ac 5295 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5296}
348e048f
DE
5297
5298static int
f4dc4d17 5299eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5300{
f4dc4d17
DE
5301 const struct type_unit_group *lhs = item_lhs;
5302 const struct type_unit_group *rhs = item_rhs;
348e048f 5303
094b34ac 5304 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5305}
348e048f 5306
f4dc4d17
DE
5307/* Allocate a hash table for type unit groups. */
5308
5309static htab_t
5310allocate_type_unit_groups_table (void)
5311{
5312 return htab_create_alloc_ex (3,
5313 hash_type_unit_group,
5314 eq_type_unit_group,
5315 NULL,
5316 &dwarf2_per_objfile->objfile->objfile_obstack,
5317 hashtab_obstack_allocate,
5318 dummy_obstack_deallocate);
5319}
dee91e82 5320
f4dc4d17
DE
5321/* Type units that don't have DW_AT_stmt_list are grouped into their own
5322 partial symtabs. We combine several TUs per psymtab to not let the size
5323 of any one psymtab grow too big. */
5324#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5325#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5326
094b34ac 5327/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5328 Create the type_unit_group object used to hold one or more TUs. */
5329
5330static struct type_unit_group *
094b34ac 5331create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5332{
5333 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5334 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5335 struct type_unit_group *tu_group;
f4dc4d17
DE
5336
5337 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5338 struct type_unit_group);
094b34ac 5339 per_cu = &tu_group->per_cu;
f4dc4d17 5340 per_cu->objfile = objfile;
f4dc4d17 5341
094b34ac
DE
5342 if (dwarf2_per_objfile->using_index)
5343 {
5344 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5345 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5346 }
5347 else
5348 {
5349 unsigned int line_offset = line_offset_struct.sect_off;
5350 struct partial_symtab *pst;
5351 char *name;
5352
5353 /* Give the symtab a useful name for debug purposes. */
5354 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5355 name = xstrprintf ("<type_units_%d>",
5356 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5357 else
5358 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5359
5360 pst = create_partial_symtab (per_cu, name);
5361 pst->anonymous = 1;
f4dc4d17 5362
094b34ac
DE
5363 xfree (name);
5364 }
f4dc4d17 5365
094b34ac
DE
5366 tu_group->hash.dwo_unit = cu->dwo_unit;
5367 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5368
5369 return tu_group;
5370}
5371
094b34ac
DE
5372/* Look up the type_unit_group for type unit CU, and create it if necessary.
5373 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5374
5375static struct type_unit_group *
ff39bb5e 5376get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5377{
5378 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5379 struct type_unit_group *tu_group;
5380 void **slot;
5381 unsigned int line_offset;
5382 struct type_unit_group type_unit_group_for_lookup;
5383
5384 if (dwarf2_per_objfile->type_unit_groups == NULL)
5385 {
5386 dwarf2_per_objfile->type_unit_groups =
5387 allocate_type_unit_groups_table ();
5388 }
5389
5390 /* Do we need to create a new group, or can we use an existing one? */
5391
5392 if (stmt_list)
5393 {
5394 line_offset = DW_UNSND (stmt_list);
5395 ++tu_stats->nr_symtab_sharers;
5396 }
5397 else
5398 {
5399 /* Ugh, no stmt_list. Rare, but we have to handle it.
5400 We can do various things here like create one group per TU or
5401 spread them over multiple groups to split up the expansion work.
5402 To avoid worst case scenarios (too many groups or too large groups)
5403 we, umm, group them in bunches. */
5404 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5405 | (tu_stats->nr_stmt_less_type_units
5406 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5407 ++tu_stats->nr_stmt_less_type_units;
5408 }
5409
094b34ac
DE
5410 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5411 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5412 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5413 &type_unit_group_for_lookup, INSERT);
5414 if (*slot != NULL)
5415 {
5416 tu_group = *slot;
5417 gdb_assert (tu_group != NULL);
5418 }
5419 else
5420 {
5421 sect_offset line_offset_struct;
5422
5423 line_offset_struct.sect_off = line_offset;
094b34ac 5424 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5425 *slot = tu_group;
5426 ++tu_stats->nr_symtabs;
5427 }
5428
5429 return tu_group;
5430}
5431
5432/* Struct used to sort TUs by their abbreviation table offset. */
5433
5434struct tu_abbrev_offset
5435{
5436 struct signatured_type *sig_type;
5437 sect_offset abbrev_offset;
5438};
5439
5440/* Helper routine for build_type_unit_groups, passed to qsort. */
5441
5442static int
5443sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5444{
5445 const struct tu_abbrev_offset * const *a = ap;
5446 const struct tu_abbrev_offset * const *b = bp;
5447 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5448 unsigned int boff = (*b)->abbrev_offset.sect_off;
5449
5450 return (aoff > boff) - (aoff < boff);
5451}
5452
5453/* A helper function to add a type_unit_group to a table. */
5454
5455static int
5456add_type_unit_group_to_table (void **slot, void *datum)
5457{
5458 struct type_unit_group *tu_group = *slot;
5459 struct type_unit_group ***datap = datum;
5460
5461 **datap = tu_group;
5462 ++*datap;
5463
5464 return 1;
5465}
5466
5467/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5468 each one passing FUNC,DATA.
5469
5470 The efficiency is because we sort TUs by the abbrev table they use and
5471 only read each abbrev table once. In one program there are 200K TUs
5472 sharing 8K abbrev tables.
5473
5474 The main purpose of this function is to support building the
5475 dwarf2_per_objfile->type_unit_groups table.
5476 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5477 can collapse the search space by grouping them by stmt_list.
5478 The savings can be significant, in the same program from above the 200K TUs
5479 share 8K stmt_list tables.
5480
5481 FUNC is expected to call get_type_unit_group, which will create the
5482 struct type_unit_group if necessary and add it to
5483 dwarf2_per_objfile->type_unit_groups. */
5484
5485static void
5486build_type_unit_groups (die_reader_func_ftype *func, void *data)
5487{
5488 struct objfile *objfile = dwarf2_per_objfile->objfile;
5489 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5490 struct cleanup *cleanups;
5491 struct abbrev_table *abbrev_table;
5492 sect_offset abbrev_offset;
5493 struct tu_abbrev_offset *sorted_by_abbrev;
5494 struct type_unit_group **iter;
5495 int i;
5496
5497 /* It's up to the caller to not call us multiple times. */
5498 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5499
5500 if (dwarf2_per_objfile->n_type_units == 0)
5501 return;
5502
5503 /* TUs typically share abbrev tables, and there can be way more TUs than
5504 abbrev tables. Sort by abbrev table to reduce the number of times we
5505 read each abbrev table in.
5506 Alternatives are to punt or to maintain a cache of abbrev tables.
5507 This is simpler and efficient enough for now.
5508
5509 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5510 symtab to use). Typically TUs with the same abbrev offset have the same
5511 stmt_list value too so in practice this should work well.
5512
5513 The basic algorithm here is:
5514
5515 sort TUs by abbrev table
5516 for each TU with same abbrev table:
5517 read abbrev table if first user
5518 read TU top level DIE
5519 [IWBN if DWO skeletons had DW_AT_stmt_list]
5520 call FUNC */
5521
5522 if (dwarf2_read_debug)
5523 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5524
5525 /* Sort in a separate table to maintain the order of all_type_units
5526 for .gdb_index: TU indices directly index all_type_units. */
5527 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5528 dwarf2_per_objfile->n_type_units);
5529 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5530 {
5531 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5532
5533 sorted_by_abbrev[i].sig_type = sig_type;
5534 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5535 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5536 sig_type->per_cu.offset);
5537 }
5538 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5539 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5540 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5541
094b34ac
DE
5542 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5543 called any number of times, so we don't reset tu_stats here. */
5544
f4dc4d17
DE
5545 abbrev_offset.sect_off = ~(unsigned) 0;
5546 abbrev_table = NULL;
5547 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5548
5549 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5550 {
5551 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5552
5553 /* Switch to the next abbrev table if necessary. */
5554 if (abbrev_table == NULL
5555 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5556 {
5557 if (abbrev_table != NULL)
5558 {
5559 abbrev_table_free (abbrev_table);
5560 /* Reset to NULL in case abbrev_table_read_table throws
5561 an error: abbrev_table_free_cleanup will get called. */
5562 abbrev_table = NULL;
5563 }
5564 abbrev_offset = tu->abbrev_offset;
5565 abbrev_table =
5566 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5567 abbrev_offset);
5568 ++tu_stats->nr_uniq_abbrev_tables;
5569 }
5570
5571 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5572 func, data);
5573 }
5574
a2ce51a0
DE
5575 /* type_unit_groups can be NULL if there is an error in the debug info.
5576 Just create an empty table so the rest of gdb doesn't have to watch
5577 for this error case. */
5578 if (dwarf2_per_objfile->type_unit_groups == NULL)
5579 {
5580 dwarf2_per_objfile->type_unit_groups =
5581 allocate_type_unit_groups_table ();
5582 dwarf2_per_objfile->n_type_unit_groups = 0;
5583 }
5584
f4dc4d17
DE
5585 /* Create a vector of pointers to primary type units to make it easy to
5586 iterate over them and CUs. See dw2_get_primary_cu. */
5587 dwarf2_per_objfile->n_type_unit_groups =
5588 htab_elements (dwarf2_per_objfile->type_unit_groups);
5589 dwarf2_per_objfile->all_type_unit_groups =
5590 obstack_alloc (&objfile->objfile_obstack,
5591 dwarf2_per_objfile->n_type_unit_groups
5592 * sizeof (struct type_unit_group *));
5593 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5594 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5595 add_type_unit_group_to_table, &iter);
5596 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5597 == dwarf2_per_objfile->n_type_unit_groups);
5598
5599 do_cleanups (cleanups);
5600
5601 if (dwarf2_read_debug)
5602 {
5603 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5604 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5605 dwarf2_per_objfile->n_type_units);
5606 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5607 tu_stats->nr_uniq_abbrev_tables);
5608 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5609 tu_stats->nr_symtabs);
5610 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5611 tu_stats->nr_symtab_sharers);
5612 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5613 tu_stats->nr_stmt_less_type_units);
5614 }
5615}
0018ea6f
DE
5616\f
5617/* Partial symbol tables. */
5618
5619/* Create a psymtab named NAME and assign it to PER_CU.
5620
5621 The caller must fill in the following details:
5622 dirname, textlow, texthigh. */
5623
5624static struct partial_symtab *
5625create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5626{
5627 struct objfile *objfile = per_cu->objfile;
5628 struct partial_symtab *pst;
5629
5630 pst = start_psymtab_common (objfile, objfile->section_offsets,
5631 name, 0,
5632 objfile->global_psymbols.next,
5633 objfile->static_psymbols.next);
5634
5635 pst->psymtabs_addrmap_supported = 1;
5636
5637 /* This is the glue that links PST into GDB's symbol API. */
5638 pst->read_symtab_private = per_cu;
5639 pst->read_symtab = dwarf2_read_symtab;
5640 per_cu->v.psymtab = pst;
5641
5642 return pst;
5643}
5644
b93601f3
TT
5645/* The DATA object passed to process_psymtab_comp_unit_reader has this
5646 type. */
5647
5648struct process_psymtab_comp_unit_data
5649{
5650 /* True if we are reading a DW_TAG_partial_unit. */
5651
5652 int want_partial_unit;
5653
5654 /* The "pretend" language that is used if the CU doesn't declare a
5655 language. */
5656
5657 enum language pretend_language;
5658};
5659
0018ea6f
DE
5660/* die_reader_func for process_psymtab_comp_unit. */
5661
5662static void
5663process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5664 const gdb_byte *info_ptr,
0018ea6f
DE
5665 struct die_info *comp_unit_die,
5666 int has_children,
5667 void *data)
5668{
5669 struct dwarf2_cu *cu = reader->cu;
5670 struct objfile *objfile = cu->objfile;
5671 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5672 struct attribute *attr;
5673 CORE_ADDR baseaddr;
5674 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5675 struct partial_symtab *pst;
5676 int has_pc_info;
5677 const char *filename;
b93601f3 5678 struct process_psymtab_comp_unit_data *info = data;
0018ea6f 5679
b93601f3 5680 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5681 return;
5682
5683 gdb_assert (! per_cu->is_debug_types);
5684
b93601f3 5685 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5686
5687 cu->list_in_scope = &file_symbols;
5688
5689 /* Allocate a new partial symbol table structure. */
5690 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5691 if (attr == NULL || !DW_STRING (attr))
5692 filename = "";
5693 else
5694 filename = DW_STRING (attr);
5695
5696 pst = create_partial_symtab (per_cu, filename);
5697
5698 /* This must be done before calling dwarf2_build_include_psymtabs. */
5699 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5700 if (attr != NULL)
5701 pst->dirname = DW_STRING (attr);
5702
5703 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5704
5705 dwarf2_find_base_address (comp_unit_die, cu);
5706
5707 /* Possibly set the default values of LOWPC and HIGHPC from
5708 `DW_AT_ranges'. */
5709 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5710 &best_highpc, cu, pst);
5711 if (has_pc_info == 1 && best_lowpc < best_highpc)
5712 /* Store the contiguous range if it is not empty; it can be empty for
5713 CUs with no code. */
5714 addrmap_set_empty (objfile->psymtabs_addrmap,
5715 best_lowpc + baseaddr,
5716 best_highpc + baseaddr - 1, pst);
5717
5718 /* Check if comp unit has_children.
5719 If so, read the rest of the partial symbols from this comp unit.
5720 If not, there's no more debug_info for this comp unit. */
5721 if (has_children)
5722 {
5723 struct partial_die_info *first_die;
5724 CORE_ADDR lowpc, highpc;
5725
5726 lowpc = ((CORE_ADDR) -1);
5727 highpc = ((CORE_ADDR) 0);
5728
5729 first_die = load_partial_dies (reader, info_ptr, 1);
5730
5731 scan_partial_symbols (first_die, &lowpc, &highpc,
5732 ! has_pc_info, cu);
5733
5734 /* If we didn't find a lowpc, set it to highpc to avoid
5735 complaints from `maint check'. */
5736 if (lowpc == ((CORE_ADDR) -1))
5737 lowpc = highpc;
5738
5739 /* If the compilation unit didn't have an explicit address range,
5740 then use the information extracted from its child dies. */
5741 if (! has_pc_info)
5742 {
5743 best_lowpc = lowpc;
5744 best_highpc = highpc;
5745 }
5746 }
5747 pst->textlow = best_lowpc + baseaddr;
5748 pst->texthigh = best_highpc + baseaddr;
5749
5750 pst->n_global_syms = objfile->global_psymbols.next -
5751 (objfile->global_psymbols.list + pst->globals_offset);
5752 pst->n_static_syms = objfile->static_psymbols.next -
5753 (objfile->static_psymbols.list + pst->statics_offset);
5754 sort_pst_symbols (objfile, pst);
5755
5756 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5757 {
5758 int i;
5759 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5760 struct dwarf2_per_cu_data *iter;
5761
5762 /* Fill in 'dependencies' here; we fill in 'users' in a
5763 post-pass. */
5764 pst->number_of_dependencies = len;
5765 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5766 len * sizeof (struct symtab *));
5767 for (i = 0;
5768 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5769 i, iter);
5770 ++i)
5771 pst->dependencies[i] = iter->v.psymtab;
5772
5773 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5774 }
5775
5776 /* Get the list of files included in the current compilation unit,
5777 and build a psymtab for each of them. */
5778 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5779
5780 if (dwarf2_read_debug)
5781 {
5782 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5783
5784 fprintf_unfiltered (gdb_stdlog,
5785 "Psymtab for %s unit @0x%x: %s - %s"
5786 ", %d global, %d static syms\n",
5787 per_cu->is_debug_types ? "type" : "comp",
5788 per_cu->offset.sect_off,
5789 paddress (gdbarch, pst->textlow),
5790 paddress (gdbarch, pst->texthigh),
5791 pst->n_global_syms, pst->n_static_syms);
5792 }
5793}
5794
5795/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5796 Process compilation unit THIS_CU for a psymtab. */
5797
5798static void
5799process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
5800 int want_partial_unit,
5801 enum language pretend_language)
0018ea6f 5802{
b93601f3
TT
5803 struct process_psymtab_comp_unit_data info;
5804
0018ea6f
DE
5805 /* If this compilation unit was already read in, free the
5806 cached copy in order to read it in again. This is
5807 necessary because we skipped some symbols when we first
5808 read in the compilation unit (see load_partial_dies).
5809 This problem could be avoided, but the benefit is unclear. */
5810 if (this_cu->cu != NULL)
5811 free_one_cached_comp_unit (this_cu);
5812
5813 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
5814 info.want_partial_unit = want_partial_unit;
5815 info.pretend_language = pretend_language;
0018ea6f
DE
5816 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5817 process_psymtab_comp_unit_reader,
b93601f3 5818 &info);
0018ea6f
DE
5819
5820 /* Age out any secondary CUs. */
5821 age_cached_comp_units ();
5822}
f4dc4d17
DE
5823
5824/* Reader function for build_type_psymtabs. */
5825
5826static void
5827build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5828 const gdb_byte *info_ptr,
f4dc4d17
DE
5829 struct die_info *type_unit_die,
5830 int has_children,
5831 void *data)
5832{
5833 struct objfile *objfile = dwarf2_per_objfile->objfile;
5834 struct dwarf2_cu *cu = reader->cu;
5835 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5836 struct signatured_type *sig_type;
f4dc4d17
DE
5837 struct type_unit_group *tu_group;
5838 struct attribute *attr;
5839 struct partial_die_info *first_die;
5840 CORE_ADDR lowpc, highpc;
5841 struct partial_symtab *pst;
5842
5843 gdb_assert (data == NULL);
0186c6a7
DE
5844 gdb_assert (per_cu->is_debug_types);
5845 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5846
5847 if (! has_children)
5848 return;
5849
5850 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5851 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5852
0186c6a7 5853 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5854
5855 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5856 cu->list_in_scope = &file_symbols;
5857 pst = create_partial_symtab (per_cu, "");
5858 pst->anonymous = 1;
5859
5860 first_die = load_partial_dies (reader, info_ptr, 1);
5861
5862 lowpc = (CORE_ADDR) -1;
5863 highpc = (CORE_ADDR) 0;
5864 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5865
5866 pst->n_global_syms = objfile->global_psymbols.next -
5867 (objfile->global_psymbols.list + pst->globals_offset);
5868 pst->n_static_syms = objfile->static_psymbols.next -
5869 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5870 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5871}
5872
5873/* Traversal function for build_type_psymtabs. */
5874
5875static int
5876build_type_psymtab_dependencies (void **slot, void *info)
5877{
5878 struct objfile *objfile = dwarf2_per_objfile->objfile;
5879 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5880 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5881 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5882 int len = VEC_length (sig_type_ptr, tu_group->tus);
5883 struct signatured_type *iter;
f4dc4d17
DE
5884 int i;
5885
5886 gdb_assert (len > 0);
0186c6a7 5887 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5888
5889 pst->number_of_dependencies = len;
5890 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5891 len * sizeof (struct psymtab *));
5892 for (i = 0;
0186c6a7 5893 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5894 ++i)
5895 {
0186c6a7
DE
5896 gdb_assert (iter->per_cu.is_debug_types);
5897 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5898 iter->type_unit_group = tu_group;
f4dc4d17
DE
5899 }
5900
0186c6a7 5901 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5902
5903 return 1;
5904}
5905
5906/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5907 Build partial symbol tables for the .debug_types comp-units. */
5908
5909static void
5910build_type_psymtabs (struct objfile *objfile)
5911{
0e50663e 5912 if (! create_all_type_units (objfile))
348e048f
DE
5913 return;
5914
f4dc4d17
DE
5915 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5916
5917 /* Now that all TUs have been processed we can fill in the dependencies. */
5918 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5919 build_type_psymtab_dependencies, NULL);
348e048f
DE
5920}
5921
60606b2c
TT
5922/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5923
5924static void
5925psymtabs_addrmap_cleanup (void *o)
5926{
5927 struct objfile *objfile = o;
ec61707d 5928
60606b2c
TT
5929 objfile->psymtabs_addrmap = NULL;
5930}
5931
95554aad
TT
5932/* Compute the 'user' field for each psymtab in OBJFILE. */
5933
5934static void
5935set_partial_user (struct objfile *objfile)
5936{
5937 int i;
5938
5939 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5940 {
5941 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5942 struct partial_symtab *pst = per_cu->v.psymtab;
5943 int j;
5944
36586728
TT
5945 if (pst == NULL)
5946 continue;
5947
95554aad
TT
5948 for (j = 0; j < pst->number_of_dependencies; ++j)
5949 {
5950 /* Set the 'user' field only if it is not already set. */
5951 if (pst->dependencies[j]->user == NULL)
5952 pst->dependencies[j]->user = pst;
5953 }
5954 }
5955}
5956
93311388
DE
5957/* Build the partial symbol table by doing a quick pass through the
5958 .debug_info and .debug_abbrev sections. */
72bf9492 5959
93311388 5960static void
c67a9c90 5961dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5962{
60606b2c
TT
5963 struct cleanup *back_to, *addrmap_cleanup;
5964 struct obstack temp_obstack;
21b2bd31 5965 int i;
93311388 5966
45cfd468
DE
5967 if (dwarf2_read_debug)
5968 {
5969 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5970 objfile->name);
5971 }
5972
98bfdba5
PA
5973 dwarf2_per_objfile->reading_partial_symbols = 1;
5974
be391dca 5975 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5976
93311388
DE
5977 /* Any cached compilation units will be linked by the per-objfile
5978 read_in_chain. Make sure to free them when we're done. */
5979 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5980
348e048f
DE
5981 build_type_psymtabs (objfile);
5982
93311388 5983 create_all_comp_units (objfile);
c906108c 5984
60606b2c
TT
5985 /* Create a temporary address map on a temporary obstack. We later
5986 copy this to the final obstack. */
5987 obstack_init (&temp_obstack);
5988 make_cleanup_obstack_free (&temp_obstack);
5989 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5990 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 5991
21b2bd31 5992 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 5993 {
21b2bd31 5994 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 5995
b93601f3 5996 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 5997 }
ff013f42 5998
95554aad
TT
5999 set_partial_user (objfile);
6000
ff013f42
JK
6001 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6002 &objfile->objfile_obstack);
60606b2c 6003 discard_cleanups (addrmap_cleanup);
ff013f42 6004
ae038cb0 6005 do_cleanups (back_to);
45cfd468
DE
6006
6007 if (dwarf2_read_debug)
6008 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6009 objfile->name);
ae038cb0
DJ
6010}
6011
3019eac3 6012/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6013
6014static void
dee91e82 6015load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6016 const gdb_byte *info_ptr,
dee91e82
DE
6017 struct die_info *comp_unit_die,
6018 int has_children,
6019 void *data)
ae038cb0 6020{
dee91e82 6021 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6022
95554aad 6023 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6024
ae038cb0
DJ
6025 /* Check if comp unit has_children.
6026 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6027 If not, there's no more debug_info for this comp unit. */
d85a05f0 6028 if (has_children)
dee91e82
DE
6029 load_partial_dies (reader, info_ptr, 0);
6030}
98bfdba5 6031
dee91e82
DE
6032/* Load the partial DIEs for a secondary CU into memory.
6033 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6034
dee91e82
DE
6035static void
6036load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6037{
f4dc4d17
DE
6038 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6039 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6040}
6041
ae038cb0 6042static void
36586728
TT
6043read_comp_units_from_section (struct objfile *objfile,
6044 struct dwarf2_section_info *section,
6045 unsigned int is_dwz,
6046 int *n_allocated,
6047 int *n_comp_units,
6048 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6049{
d521ce57 6050 const gdb_byte *info_ptr;
36586728 6051 bfd *abfd = section->asection->owner;
be391dca 6052
bf6af496
DE
6053 if (dwarf2_read_debug)
6054 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6055 section->asection->name, bfd_get_filename (abfd));
6056
36586728 6057 dwarf2_read_section (objfile, section);
ae038cb0 6058
36586728 6059 info_ptr = section->buffer;
6e70227d 6060
36586728 6061 while (info_ptr < section->buffer + section->size)
ae038cb0 6062 {
c764a876 6063 unsigned int length, initial_length_size;
ae038cb0 6064 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6065 sect_offset offset;
ae038cb0 6066
36586728 6067 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6068
6069 /* Read just enough information to find out where the next
6070 compilation unit is. */
36586728 6071 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6072
6073 /* Save the compilation unit for later lookup. */
6074 this_cu = obstack_alloc (&objfile->objfile_obstack,
6075 sizeof (struct dwarf2_per_cu_data));
6076 memset (this_cu, 0, sizeof (*this_cu));
6077 this_cu->offset = offset;
c764a876 6078 this_cu->length = length + initial_length_size;
36586728 6079 this_cu->is_dwz = is_dwz;
9291a0cd 6080 this_cu->objfile = objfile;
8a0459fd 6081 this_cu->section = section;
ae038cb0 6082
36586728 6083 if (*n_comp_units == *n_allocated)
ae038cb0 6084 {
36586728
TT
6085 *n_allocated *= 2;
6086 *all_comp_units = xrealloc (*all_comp_units,
6087 *n_allocated
6088 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6089 }
36586728
TT
6090 (*all_comp_units)[*n_comp_units] = this_cu;
6091 ++*n_comp_units;
ae038cb0
DJ
6092
6093 info_ptr = info_ptr + this_cu->length;
6094 }
36586728
TT
6095}
6096
6097/* Create a list of all compilation units in OBJFILE.
6098 This is only done for -readnow and building partial symtabs. */
6099
6100static void
6101create_all_comp_units (struct objfile *objfile)
6102{
6103 int n_allocated;
6104 int n_comp_units;
6105 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6106 struct dwz_file *dwz;
36586728
TT
6107
6108 n_comp_units = 0;
6109 n_allocated = 10;
6110 all_comp_units = xmalloc (n_allocated
6111 * sizeof (struct dwarf2_per_cu_data *));
6112
6113 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6114 &n_allocated, &n_comp_units, &all_comp_units);
6115
4db1a1dc
TT
6116 dwz = dwarf2_get_dwz_file ();
6117 if (dwz != NULL)
6118 read_comp_units_from_section (objfile, &dwz->info, 1,
6119 &n_allocated, &n_comp_units,
6120 &all_comp_units);
ae038cb0
DJ
6121
6122 dwarf2_per_objfile->all_comp_units
6123 = obstack_alloc (&objfile->objfile_obstack,
6124 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6125 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6126 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6127 xfree (all_comp_units);
6128 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6129}
6130
5734ee8b
DJ
6131/* Process all loaded DIEs for compilation unit CU, starting at
6132 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6133 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6134 DW_AT_ranges). If NEED_PC is set, then this function will set
6135 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6136 and record the covered ranges in the addrmap. */
c906108c 6137
72bf9492
DJ
6138static void
6139scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6140 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6141{
72bf9492 6142 struct partial_die_info *pdi;
c906108c 6143
91c24f0a
DC
6144 /* Now, march along the PDI's, descending into ones which have
6145 interesting children but skipping the children of the other ones,
6146 until we reach the end of the compilation unit. */
c906108c 6147
72bf9492 6148 pdi = first_die;
91c24f0a 6149
72bf9492
DJ
6150 while (pdi != NULL)
6151 {
6152 fixup_partial_die (pdi, cu);
c906108c 6153
f55ee35c 6154 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6155 children, so we need to look at them. Ditto for anonymous
6156 enums. */
933c6fe4 6157
72bf9492 6158 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6159 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6160 || pdi->tag == DW_TAG_imported_unit)
c906108c 6161 {
72bf9492 6162 switch (pdi->tag)
c906108c
SS
6163 {
6164 case DW_TAG_subprogram:
5734ee8b 6165 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6166 break;
72929c62 6167 case DW_TAG_constant:
c906108c
SS
6168 case DW_TAG_variable:
6169 case DW_TAG_typedef:
91c24f0a 6170 case DW_TAG_union_type:
72bf9492 6171 if (!pdi->is_declaration)
63d06c5c 6172 {
72bf9492 6173 add_partial_symbol (pdi, cu);
63d06c5c
DC
6174 }
6175 break;
c906108c 6176 case DW_TAG_class_type:
680b30c7 6177 case DW_TAG_interface_type:
c906108c 6178 case DW_TAG_structure_type:
72bf9492 6179 if (!pdi->is_declaration)
c906108c 6180 {
72bf9492 6181 add_partial_symbol (pdi, cu);
c906108c
SS
6182 }
6183 break;
91c24f0a 6184 case DW_TAG_enumeration_type:
72bf9492
DJ
6185 if (!pdi->is_declaration)
6186 add_partial_enumeration (pdi, cu);
c906108c
SS
6187 break;
6188 case DW_TAG_base_type:
a02abb62 6189 case DW_TAG_subrange_type:
c906108c 6190 /* File scope base type definitions are added to the partial
c5aa993b 6191 symbol table. */
72bf9492 6192 add_partial_symbol (pdi, cu);
c906108c 6193 break;
d9fa45fe 6194 case DW_TAG_namespace:
5734ee8b 6195 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6196 break;
5d7cb8df
JK
6197 case DW_TAG_module:
6198 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6199 break;
95554aad
TT
6200 case DW_TAG_imported_unit:
6201 {
6202 struct dwarf2_per_cu_data *per_cu;
6203
f4dc4d17
DE
6204 /* For now we don't handle imported units in type units. */
6205 if (cu->per_cu->is_debug_types)
6206 {
6207 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6208 " supported in type units [in module %s]"),
6209 cu->objfile->name);
6210 }
6211
95554aad 6212 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6213 pdi->is_dwz,
95554aad
TT
6214 cu->objfile);
6215
6216 /* Go read the partial unit, if needed. */
6217 if (per_cu->v.psymtab == NULL)
b93601f3 6218 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6219
f4dc4d17 6220 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6221 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6222 }
6223 break;
c906108c
SS
6224 default:
6225 break;
6226 }
6227 }
6228
72bf9492
DJ
6229 /* If the die has a sibling, skip to the sibling. */
6230
6231 pdi = pdi->die_sibling;
6232 }
6233}
6234
6235/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6236
72bf9492 6237 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6238 name is concatenated with "::" and the partial DIE's name. For
6239 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6240 Enumerators are an exception; they use the scope of their parent
6241 enumeration type, i.e. the name of the enumeration type is not
6242 prepended to the enumerator.
91c24f0a 6243
72bf9492
DJ
6244 There are two complexities. One is DW_AT_specification; in this
6245 case "parent" means the parent of the target of the specification,
6246 instead of the direct parent of the DIE. The other is compilers
6247 which do not emit DW_TAG_namespace; in this case we try to guess
6248 the fully qualified name of structure types from their members'
6249 linkage names. This must be done using the DIE's children rather
6250 than the children of any DW_AT_specification target. We only need
6251 to do this for structures at the top level, i.e. if the target of
6252 any DW_AT_specification (if any; otherwise the DIE itself) does not
6253 have a parent. */
6254
6255/* Compute the scope prefix associated with PDI's parent, in
6256 compilation unit CU. The result will be allocated on CU's
6257 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6258 field. NULL is returned if no prefix is necessary. */
15d034d0 6259static const char *
72bf9492
DJ
6260partial_die_parent_scope (struct partial_die_info *pdi,
6261 struct dwarf2_cu *cu)
6262{
15d034d0 6263 const char *grandparent_scope;
72bf9492 6264 struct partial_die_info *parent, *real_pdi;
91c24f0a 6265
72bf9492
DJ
6266 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6267 then this means the parent of the specification DIE. */
6268
6269 real_pdi = pdi;
72bf9492 6270 while (real_pdi->has_specification)
36586728
TT
6271 real_pdi = find_partial_die (real_pdi->spec_offset,
6272 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6273
6274 parent = real_pdi->die_parent;
6275 if (parent == NULL)
6276 return NULL;
6277
6278 if (parent->scope_set)
6279 return parent->scope;
6280
6281 fixup_partial_die (parent, cu);
6282
10b3939b 6283 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6284
acebe513
UW
6285 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6286 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6287 Work around this problem here. */
6288 if (cu->language == language_cplus
6e70227d 6289 && parent->tag == DW_TAG_namespace
acebe513
UW
6290 && strcmp (parent->name, "::") == 0
6291 && grandparent_scope == NULL)
6292 {
6293 parent->scope = NULL;
6294 parent->scope_set = 1;
6295 return NULL;
6296 }
6297
9c6c53f7
SA
6298 if (pdi->tag == DW_TAG_enumerator)
6299 /* Enumerators should not get the name of the enumeration as a prefix. */
6300 parent->scope = grandparent_scope;
6301 else if (parent->tag == DW_TAG_namespace
f55ee35c 6302 || parent->tag == DW_TAG_module
72bf9492
DJ
6303 || parent->tag == DW_TAG_structure_type
6304 || parent->tag == DW_TAG_class_type
680b30c7 6305 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6306 || parent->tag == DW_TAG_union_type
6307 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6308 {
6309 if (grandparent_scope == NULL)
6310 parent->scope = parent->name;
6311 else
3e43a32a
MS
6312 parent->scope = typename_concat (&cu->comp_unit_obstack,
6313 grandparent_scope,
f55ee35c 6314 parent->name, 0, cu);
72bf9492 6315 }
72bf9492
DJ
6316 else
6317 {
6318 /* FIXME drow/2004-04-01: What should we be doing with
6319 function-local names? For partial symbols, we should probably be
6320 ignoring them. */
6321 complaint (&symfile_complaints,
e2e0b3e5 6322 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6323 parent->tag, pdi->offset.sect_off);
72bf9492 6324 parent->scope = grandparent_scope;
c906108c
SS
6325 }
6326
72bf9492
DJ
6327 parent->scope_set = 1;
6328 return parent->scope;
6329}
6330
6331/* Return the fully scoped name associated with PDI, from compilation unit
6332 CU. The result will be allocated with malloc. */
4568ecf9 6333
72bf9492
DJ
6334static char *
6335partial_die_full_name (struct partial_die_info *pdi,
6336 struct dwarf2_cu *cu)
6337{
15d034d0 6338 const char *parent_scope;
72bf9492 6339
98bfdba5
PA
6340 /* If this is a template instantiation, we can not work out the
6341 template arguments from partial DIEs. So, unfortunately, we have
6342 to go through the full DIEs. At least any work we do building
6343 types here will be reused if full symbols are loaded later. */
6344 if (pdi->has_template_arguments)
6345 {
6346 fixup_partial_die (pdi, cu);
6347
6348 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6349 {
6350 struct die_info *die;
6351 struct attribute attr;
6352 struct dwarf2_cu *ref_cu = cu;
6353
b64f50a1 6354 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6355 attr.name = 0;
6356 attr.form = DW_FORM_ref_addr;
4568ecf9 6357 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6358 die = follow_die_ref (NULL, &attr, &ref_cu);
6359
6360 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6361 }
6362 }
6363
72bf9492
DJ
6364 parent_scope = partial_die_parent_scope (pdi, cu);
6365 if (parent_scope == NULL)
6366 return NULL;
6367 else
f55ee35c 6368 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6369}
6370
6371static void
72bf9492 6372add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6373{
e7c27a73 6374 struct objfile *objfile = cu->objfile;
c906108c 6375 CORE_ADDR addr = 0;
15d034d0 6376 const char *actual_name = NULL;
e142c38c 6377 CORE_ADDR baseaddr;
15d034d0 6378 char *built_actual_name;
e142c38c
DJ
6379
6380 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6381
15d034d0
TT
6382 built_actual_name = partial_die_full_name (pdi, cu);
6383 if (built_actual_name != NULL)
6384 actual_name = built_actual_name;
63d06c5c 6385
72bf9492
DJ
6386 if (actual_name == NULL)
6387 actual_name = pdi->name;
6388
c906108c
SS
6389 switch (pdi->tag)
6390 {
6391 case DW_TAG_subprogram:
2cfa0c8d 6392 if (pdi->is_external || cu->language == language_ada)
c906108c 6393 {
2cfa0c8d
JB
6394 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6395 of the global scope. But in Ada, we want to be able to access
6396 nested procedures globally. So all Ada subprograms are stored
6397 in the global scope. */
f47fb265 6398 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6399 mst_text, objfile); */
f47fb265 6400 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6401 built_actual_name != NULL,
f47fb265
MS
6402 VAR_DOMAIN, LOC_BLOCK,
6403 &objfile->global_psymbols,
6404 0, pdi->lowpc + baseaddr,
6405 cu->language, objfile);
c906108c
SS
6406 }
6407 else
6408 {
f47fb265 6409 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6410 mst_file_text, objfile); */
f47fb265 6411 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6412 built_actual_name != NULL,
f47fb265
MS
6413 VAR_DOMAIN, LOC_BLOCK,
6414 &objfile->static_psymbols,
6415 0, pdi->lowpc + baseaddr,
6416 cu->language, objfile);
c906108c
SS
6417 }
6418 break;
72929c62
JB
6419 case DW_TAG_constant:
6420 {
6421 struct psymbol_allocation_list *list;
6422
6423 if (pdi->is_external)
6424 list = &objfile->global_psymbols;
6425 else
6426 list = &objfile->static_psymbols;
f47fb265 6427 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6428 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6429 list, 0, 0, cu->language, objfile);
72929c62
JB
6430 }
6431 break;
c906108c 6432 case DW_TAG_variable:
95554aad
TT
6433 if (pdi->d.locdesc)
6434 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6435
95554aad 6436 if (pdi->d.locdesc
caac4577
JG
6437 && addr == 0
6438 && !dwarf2_per_objfile->has_section_at_zero)
6439 {
6440 /* A global or static variable may also have been stripped
6441 out by the linker if unused, in which case its address
6442 will be nullified; do not add such variables into partial
6443 symbol table then. */
6444 }
6445 else if (pdi->is_external)
c906108c
SS
6446 {
6447 /* Global Variable.
6448 Don't enter into the minimal symbol tables as there is
6449 a minimal symbol table entry from the ELF symbols already.
6450 Enter into partial symbol table if it has a location
6451 descriptor or a type.
6452 If the location descriptor is missing, new_symbol will create
6453 a LOC_UNRESOLVED symbol, the address of the variable will then
6454 be determined from the minimal symbol table whenever the variable
6455 is referenced.
6456 The address for the partial symbol table entry is not
6457 used by GDB, but it comes in handy for debugging partial symbol
6458 table building. */
6459
95554aad 6460 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6461 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6462 built_actual_name != NULL,
f47fb265
MS
6463 VAR_DOMAIN, LOC_STATIC,
6464 &objfile->global_psymbols,
6465 0, addr + baseaddr,
6466 cu->language, objfile);
c906108c
SS
6467 }
6468 else
6469 {
0963b4bd 6470 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6471 if (pdi->d.locdesc == NULL)
decbce07 6472 {
15d034d0 6473 xfree (built_actual_name);
decbce07
MS
6474 return;
6475 }
f47fb265 6476 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6477 mst_file_data, objfile); */
f47fb265 6478 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6479 built_actual_name != NULL,
f47fb265
MS
6480 VAR_DOMAIN, LOC_STATIC,
6481 &objfile->static_psymbols,
6482 0, addr + baseaddr,
6483 cu->language, objfile);
c906108c
SS
6484 }
6485 break;
6486 case DW_TAG_typedef:
6487 case DW_TAG_base_type:
a02abb62 6488 case DW_TAG_subrange_type:
38d518c9 6489 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6490 built_actual_name != NULL,
176620f1 6491 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6492 &objfile->static_psymbols,
e142c38c 6493 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6494 break;
72bf9492
DJ
6495 case DW_TAG_namespace:
6496 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6497 built_actual_name != NULL,
72bf9492
DJ
6498 VAR_DOMAIN, LOC_TYPEDEF,
6499 &objfile->global_psymbols,
6500 0, (CORE_ADDR) 0, cu->language, objfile);
6501 break;
c906108c 6502 case DW_TAG_class_type:
680b30c7 6503 case DW_TAG_interface_type:
c906108c
SS
6504 case DW_TAG_structure_type:
6505 case DW_TAG_union_type:
6506 case DW_TAG_enumeration_type:
fa4028e9
JB
6507 /* Skip external references. The DWARF standard says in the section
6508 about "Structure, Union, and Class Type Entries": "An incomplete
6509 structure, union or class type is represented by a structure,
6510 union or class entry that does not have a byte size attribute
6511 and that has a DW_AT_declaration attribute." */
6512 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6513 {
15d034d0 6514 xfree (built_actual_name);
decbce07
MS
6515 return;
6516 }
fa4028e9 6517
63d06c5c
DC
6518 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6519 static vs. global. */
38d518c9 6520 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6521 built_actual_name != NULL,
176620f1 6522 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6523 (cu->language == language_cplus
6524 || cu->language == language_java)
63d06c5c
DC
6525 ? &objfile->global_psymbols
6526 : &objfile->static_psymbols,
e142c38c 6527 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6528
c906108c
SS
6529 break;
6530 case DW_TAG_enumerator:
38d518c9 6531 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6532 built_actual_name != NULL,
176620f1 6533 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6534 (cu->language == language_cplus
6535 || cu->language == language_java)
f6fe98ef
DJ
6536 ? &objfile->global_psymbols
6537 : &objfile->static_psymbols,
e142c38c 6538 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6539 break;
6540 default:
6541 break;
6542 }
5c4e30ca 6543
15d034d0 6544 xfree (built_actual_name);
c906108c
SS
6545}
6546
5c4e30ca
DC
6547/* Read a partial die corresponding to a namespace; also, add a symbol
6548 corresponding to that namespace to the symbol table. NAMESPACE is
6549 the name of the enclosing namespace. */
91c24f0a 6550
72bf9492
DJ
6551static void
6552add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6553 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6554 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6555{
72bf9492 6556 /* Add a symbol for the namespace. */
e7c27a73 6557
72bf9492 6558 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6559
6560 /* Now scan partial symbols in that namespace. */
6561
91c24f0a 6562 if (pdi->has_children)
5734ee8b 6563 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6564}
6565
5d7cb8df
JK
6566/* Read a partial die corresponding to a Fortran module. */
6567
6568static void
6569add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6570 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6571{
f55ee35c 6572 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6573
6574 if (pdi->has_children)
6575 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6576}
6577
bc30ff58
JB
6578/* Read a partial die corresponding to a subprogram and create a partial
6579 symbol for that subprogram. When the CU language allows it, this
6580 routine also defines a partial symbol for each nested subprogram
6581 that this subprogram contains.
6e70227d 6582
bc30ff58
JB
6583 DIE my also be a lexical block, in which case we simply search
6584 recursively for suprograms defined inside that lexical block.
6585 Again, this is only performed when the CU language allows this
6586 type of definitions. */
6587
6588static void
6589add_partial_subprogram (struct partial_die_info *pdi,
6590 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6591 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6592{
6593 if (pdi->tag == DW_TAG_subprogram)
6594 {
6595 if (pdi->has_pc_info)
6596 {
6597 if (pdi->lowpc < *lowpc)
6598 *lowpc = pdi->lowpc;
6599 if (pdi->highpc > *highpc)
6600 *highpc = pdi->highpc;
5734ee8b
DJ
6601 if (need_pc)
6602 {
6603 CORE_ADDR baseaddr;
6604 struct objfile *objfile = cu->objfile;
6605
6606 baseaddr = ANOFFSET (objfile->section_offsets,
6607 SECT_OFF_TEXT (objfile));
6608 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6609 pdi->lowpc + baseaddr,
6610 pdi->highpc - 1 + baseaddr,
9291a0cd 6611 cu->per_cu->v.psymtab);
5734ee8b 6612 }
481860b3
GB
6613 }
6614
6615 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6616 {
bc30ff58 6617 if (!pdi->is_declaration)
e8d05480
JB
6618 /* Ignore subprogram DIEs that do not have a name, they are
6619 illegal. Do not emit a complaint at this point, we will
6620 do so when we convert this psymtab into a symtab. */
6621 if (pdi->name)
6622 add_partial_symbol (pdi, cu);
bc30ff58
JB
6623 }
6624 }
6e70227d 6625
bc30ff58
JB
6626 if (! pdi->has_children)
6627 return;
6628
6629 if (cu->language == language_ada)
6630 {
6631 pdi = pdi->die_child;
6632 while (pdi != NULL)
6633 {
6634 fixup_partial_die (pdi, cu);
6635 if (pdi->tag == DW_TAG_subprogram
6636 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6637 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6638 pdi = pdi->die_sibling;
6639 }
6640 }
6641}
6642
91c24f0a
DC
6643/* Read a partial die corresponding to an enumeration type. */
6644
72bf9492
DJ
6645static void
6646add_partial_enumeration (struct partial_die_info *enum_pdi,
6647 struct dwarf2_cu *cu)
91c24f0a 6648{
72bf9492 6649 struct partial_die_info *pdi;
91c24f0a
DC
6650
6651 if (enum_pdi->name != NULL)
72bf9492
DJ
6652 add_partial_symbol (enum_pdi, cu);
6653
6654 pdi = enum_pdi->die_child;
6655 while (pdi)
91c24f0a 6656 {
72bf9492 6657 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6658 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6659 else
72bf9492
DJ
6660 add_partial_symbol (pdi, cu);
6661 pdi = pdi->die_sibling;
91c24f0a 6662 }
91c24f0a
DC
6663}
6664
6caca83c
CC
6665/* Return the initial uleb128 in the die at INFO_PTR. */
6666
6667static unsigned int
d521ce57 6668peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6669{
6670 unsigned int bytes_read;
6671
6672 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6673}
6674
4bb7a0a7
DJ
6675/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6676 Return the corresponding abbrev, or NULL if the number is zero (indicating
6677 an empty DIE). In either case *BYTES_READ will be set to the length of
6678 the initial number. */
6679
6680static struct abbrev_info *
d521ce57 6681peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6682 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6683{
6684 bfd *abfd = cu->objfile->obfd;
6685 unsigned int abbrev_number;
6686 struct abbrev_info *abbrev;
6687
6688 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6689
6690 if (abbrev_number == 0)
6691 return NULL;
6692
433df2d4 6693 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6694 if (!abbrev)
6695 {
3e43a32a
MS
6696 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6697 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6698 }
6699
6700 return abbrev;
6701}
6702
93311388
DE
6703/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6704 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6705 DIE. Any children of the skipped DIEs will also be skipped. */
6706
d521ce57
TT
6707static const gdb_byte *
6708skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6709{
dee91e82 6710 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6711 struct abbrev_info *abbrev;
6712 unsigned int bytes_read;
6713
6714 while (1)
6715 {
6716 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6717 if (abbrev == NULL)
6718 return info_ptr + bytes_read;
6719 else
dee91e82 6720 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6721 }
6722}
6723
93311388
DE
6724/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6725 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6726 abbrev corresponding to that skipped uleb128 should be passed in
6727 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6728 children. */
6729
d521ce57
TT
6730static const gdb_byte *
6731skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6732 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6733{
6734 unsigned int bytes_read;
6735 struct attribute attr;
dee91e82
DE
6736 bfd *abfd = reader->abfd;
6737 struct dwarf2_cu *cu = reader->cu;
d521ce57 6738 const gdb_byte *buffer = reader->buffer;
f664829e 6739 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6740 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6741 unsigned int form, i;
6742
6743 for (i = 0; i < abbrev->num_attrs; i++)
6744 {
6745 /* The only abbrev we care about is DW_AT_sibling. */
6746 if (abbrev->attrs[i].name == DW_AT_sibling)
6747 {
dee91e82 6748 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6749 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6750 complaint (&symfile_complaints,
6751 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6752 else
b64f50a1 6753 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6754 }
6755
6756 /* If it isn't DW_AT_sibling, skip this attribute. */
6757 form = abbrev->attrs[i].form;
6758 skip_attribute:
6759 switch (form)
6760 {
4bb7a0a7 6761 case DW_FORM_ref_addr:
ae411497
TT
6762 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6763 and later it is offset sized. */
6764 if (cu->header.version == 2)
6765 info_ptr += cu->header.addr_size;
6766 else
6767 info_ptr += cu->header.offset_size;
6768 break;
36586728
TT
6769 case DW_FORM_GNU_ref_alt:
6770 info_ptr += cu->header.offset_size;
6771 break;
ae411497 6772 case DW_FORM_addr:
4bb7a0a7
DJ
6773 info_ptr += cu->header.addr_size;
6774 break;
6775 case DW_FORM_data1:
6776 case DW_FORM_ref1:
6777 case DW_FORM_flag:
6778 info_ptr += 1;
6779 break;
2dc7f7b3
TT
6780 case DW_FORM_flag_present:
6781 break;
4bb7a0a7
DJ
6782 case DW_FORM_data2:
6783 case DW_FORM_ref2:
6784 info_ptr += 2;
6785 break;
6786 case DW_FORM_data4:
6787 case DW_FORM_ref4:
6788 info_ptr += 4;
6789 break;
6790 case DW_FORM_data8:
6791 case DW_FORM_ref8:
55f1336d 6792 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6793 info_ptr += 8;
6794 break;
6795 case DW_FORM_string:
9b1c24c8 6796 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6797 info_ptr += bytes_read;
6798 break;
2dc7f7b3 6799 case DW_FORM_sec_offset:
4bb7a0a7 6800 case DW_FORM_strp:
36586728 6801 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6802 info_ptr += cu->header.offset_size;
6803 break;
2dc7f7b3 6804 case DW_FORM_exprloc:
4bb7a0a7
DJ
6805 case DW_FORM_block:
6806 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6807 info_ptr += bytes_read;
6808 break;
6809 case DW_FORM_block1:
6810 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6811 break;
6812 case DW_FORM_block2:
6813 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6814 break;
6815 case DW_FORM_block4:
6816 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6817 break;
6818 case DW_FORM_sdata:
6819 case DW_FORM_udata:
6820 case DW_FORM_ref_udata:
3019eac3
DE
6821 case DW_FORM_GNU_addr_index:
6822 case DW_FORM_GNU_str_index:
d521ce57 6823 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6824 break;
6825 case DW_FORM_indirect:
6826 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6827 info_ptr += bytes_read;
6828 /* We need to continue parsing from here, so just go back to
6829 the top. */
6830 goto skip_attribute;
6831
6832 default:
3e43a32a
MS
6833 error (_("Dwarf Error: Cannot handle %s "
6834 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6835 dwarf_form_name (form),
6836 bfd_get_filename (abfd));
6837 }
6838 }
6839
6840 if (abbrev->has_children)
dee91e82 6841 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6842 else
6843 return info_ptr;
6844}
6845
93311388 6846/* Locate ORIG_PDI's sibling.
dee91e82 6847 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6848
d521ce57 6849static const gdb_byte *
dee91e82
DE
6850locate_pdi_sibling (const struct die_reader_specs *reader,
6851 struct partial_die_info *orig_pdi,
d521ce57 6852 const gdb_byte *info_ptr)
91c24f0a
DC
6853{
6854 /* Do we know the sibling already? */
72bf9492 6855
91c24f0a
DC
6856 if (orig_pdi->sibling)
6857 return orig_pdi->sibling;
6858
6859 /* Are there any children to deal with? */
6860
6861 if (!orig_pdi->has_children)
6862 return info_ptr;
6863
4bb7a0a7 6864 /* Skip the children the long way. */
91c24f0a 6865
dee91e82 6866 return skip_children (reader, info_ptr);
91c24f0a
DC
6867}
6868
257e7a09 6869/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6870 not NULL. */
c906108c
SS
6871
6872static void
257e7a09
YQ
6873dwarf2_read_symtab (struct partial_symtab *self,
6874 struct objfile *objfile)
c906108c 6875{
257e7a09 6876 if (self->readin)
c906108c 6877 {
442e4d9c 6878 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6879 self->filename);
442e4d9c
YQ
6880 }
6881 else
6882 {
6883 if (info_verbose)
c906108c 6884 {
442e4d9c 6885 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6886 self->filename);
442e4d9c 6887 gdb_flush (gdb_stdout);
c906108c 6888 }
c906108c 6889
442e4d9c
YQ
6890 /* Restore our global data. */
6891 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6892
442e4d9c
YQ
6893 /* If this psymtab is constructed from a debug-only objfile, the
6894 has_section_at_zero flag will not necessarily be correct. We
6895 can get the correct value for this flag by looking at the data
6896 associated with the (presumably stripped) associated objfile. */
6897 if (objfile->separate_debug_objfile_backlink)
6898 {
6899 struct dwarf2_per_objfile *dpo_backlink
6900 = objfile_data (objfile->separate_debug_objfile_backlink,
6901 dwarf2_objfile_data_key);
9a619af0 6902
442e4d9c
YQ
6903 dwarf2_per_objfile->has_section_at_zero
6904 = dpo_backlink->has_section_at_zero;
6905 }
b2ab525c 6906
442e4d9c 6907 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6908
257e7a09 6909 psymtab_to_symtab_1 (self);
c906108c 6910
442e4d9c
YQ
6911 /* Finish up the debug error message. */
6912 if (info_verbose)
6913 printf_filtered (_("done.\n"));
c906108c 6914 }
95554aad
TT
6915
6916 process_cu_includes ();
c906108c 6917}
9cdd5dbd
DE
6918\f
6919/* Reading in full CUs. */
c906108c 6920
10b3939b
DJ
6921/* Add PER_CU to the queue. */
6922
6923static void
95554aad
TT
6924queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6925 enum language pretend_language)
10b3939b
DJ
6926{
6927 struct dwarf2_queue_item *item;
6928
6929 per_cu->queued = 1;
6930 item = xmalloc (sizeof (*item));
6931 item->per_cu = per_cu;
95554aad 6932 item->pretend_language = pretend_language;
10b3939b
DJ
6933 item->next = NULL;
6934
6935 if (dwarf2_queue == NULL)
6936 dwarf2_queue = item;
6937 else
6938 dwarf2_queue_tail->next = item;
6939
6940 dwarf2_queue_tail = item;
6941}
6942
0907af0c
DE
6943/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6944 unit and add it to our queue.
6945 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
6946 meaning either PER_CU is already queued or it is already loaded.
6947
6948 N.B. There is an invariant here that if a CU is queued then it is loaded.
6949 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
6950
6951static int
6952maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6953 struct dwarf2_per_cu_data *per_cu,
6954 enum language pretend_language)
6955{
6956 /* We may arrive here during partial symbol reading, if we need full
6957 DIEs to process an unusual case (e.g. template arguments). Do
6958 not queue PER_CU, just tell our caller to load its DIEs. */
6959 if (dwarf2_per_objfile->reading_partial_symbols)
6960 {
6961 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6962 return 1;
6963 return 0;
6964 }
6965
6966 /* Mark the dependence relation so that we don't flush PER_CU
6967 too early. */
6968 dwarf2_add_dependence (this_cu, per_cu);
6969
6970 /* If it's already on the queue, we have nothing to do. */
6971 if (per_cu->queued)
6972 return 0;
6973
6974 /* If the compilation unit is already loaded, just mark it as
6975 used. */
6976 if (per_cu->cu != NULL)
6977 {
6978 per_cu->cu->last_used = 0;
6979 return 0;
6980 }
6981
6982 /* Add it to the queue. */
6983 queue_comp_unit (per_cu, pretend_language);
6984
6985 return 1;
6986}
6987
10b3939b
DJ
6988/* Process the queue. */
6989
6990static void
a0f42c21 6991process_queue (void)
10b3939b
DJ
6992{
6993 struct dwarf2_queue_item *item, *next_item;
6994
45cfd468
DE
6995 if (dwarf2_read_debug)
6996 {
6997 fprintf_unfiltered (gdb_stdlog,
6998 "Expanding one or more symtabs of objfile %s ...\n",
6999 dwarf2_per_objfile->objfile->name);
7000 }
7001
03dd20cc
DJ
7002 /* The queue starts out with one item, but following a DIE reference
7003 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7004 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7005 {
9291a0cd
TT
7006 if (dwarf2_per_objfile->using_index
7007 ? !item->per_cu->v.quick->symtab
7008 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7009 {
7010 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 7011 char buf[100];
f4dc4d17 7012
247f5c4f 7013 if (per_cu->is_debug_types)
f4dc4d17 7014 {
247f5c4f
DE
7015 struct signatured_type *sig_type =
7016 (struct signatured_type *) per_cu;
7017
7018 sprintf (buf, "TU %s at offset 0x%x",
7019 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7020 }
247f5c4f
DE
7021 else
7022 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7023
7024 if (dwarf2_read_debug)
7025 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7026
7027 if (per_cu->is_debug_types)
7028 process_full_type_unit (per_cu, item->pretend_language);
7029 else
7030 process_full_comp_unit (per_cu, item->pretend_language);
7031
7032 if (dwarf2_read_debug)
247f5c4f 7033 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7034 }
10b3939b
DJ
7035
7036 item->per_cu->queued = 0;
7037 next_item = item->next;
7038 xfree (item);
7039 }
7040
7041 dwarf2_queue_tail = NULL;
45cfd468
DE
7042
7043 if (dwarf2_read_debug)
7044 {
7045 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7046 dwarf2_per_objfile->objfile->name);
7047 }
10b3939b
DJ
7048}
7049
7050/* Free all allocated queue entries. This function only releases anything if
7051 an error was thrown; if the queue was processed then it would have been
7052 freed as we went along. */
7053
7054static void
7055dwarf2_release_queue (void *dummy)
7056{
7057 struct dwarf2_queue_item *item, *last;
7058
7059 item = dwarf2_queue;
7060 while (item)
7061 {
7062 /* Anything still marked queued is likely to be in an
7063 inconsistent state, so discard it. */
7064 if (item->per_cu->queued)
7065 {
7066 if (item->per_cu->cu != NULL)
dee91e82 7067 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7068 item->per_cu->queued = 0;
7069 }
7070
7071 last = item;
7072 item = item->next;
7073 xfree (last);
7074 }
7075
7076 dwarf2_queue = dwarf2_queue_tail = NULL;
7077}
7078
7079/* Read in full symbols for PST, and anything it depends on. */
7080
c906108c 7081static void
fba45db2 7082psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7083{
10b3939b 7084 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7085 int i;
7086
95554aad
TT
7087 if (pst->readin)
7088 return;
7089
aaa75496 7090 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7091 if (!pst->dependencies[i]->readin
7092 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7093 {
7094 /* Inform about additional files that need to be read in. */
7095 if (info_verbose)
7096 {
a3f17187 7097 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7098 fputs_filtered (" ", gdb_stdout);
7099 wrap_here ("");
7100 fputs_filtered ("and ", gdb_stdout);
7101 wrap_here ("");
7102 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7103 wrap_here (""); /* Flush output. */
aaa75496
JB
7104 gdb_flush (gdb_stdout);
7105 }
7106 psymtab_to_symtab_1 (pst->dependencies[i]);
7107 }
7108
e38df1d0 7109 per_cu = pst->read_symtab_private;
10b3939b
DJ
7110
7111 if (per_cu == NULL)
aaa75496
JB
7112 {
7113 /* It's an include file, no symbols to read for it.
7114 Everything is in the parent symtab. */
7115 pst->readin = 1;
7116 return;
7117 }
c906108c 7118
a0f42c21 7119 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7120}
7121
dee91e82
DE
7122/* Trivial hash function for die_info: the hash value of a DIE
7123 is its offset in .debug_info for this objfile. */
10b3939b 7124
dee91e82
DE
7125static hashval_t
7126die_hash (const void *item)
10b3939b 7127{
dee91e82 7128 const struct die_info *die = item;
6502dd73 7129
dee91e82
DE
7130 return die->offset.sect_off;
7131}
63d06c5c 7132
dee91e82
DE
7133/* Trivial comparison function for die_info structures: two DIEs
7134 are equal if they have the same offset. */
98bfdba5 7135
dee91e82
DE
7136static int
7137die_eq (const void *item_lhs, const void *item_rhs)
7138{
7139 const struct die_info *die_lhs = item_lhs;
7140 const struct die_info *die_rhs = item_rhs;
c906108c 7141
dee91e82
DE
7142 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7143}
c906108c 7144
dee91e82
DE
7145/* die_reader_func for load_full_comp_unit.
7146 This is identical to read_signatured_type_reader,
7147 but is kept separate for now. */
c906108c 7148
dee91e82
DE
7149static void
7150load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7151 const gdb_byte *info_ptr,
dee91e82
DE
7152 struct die_info *comp_unit_die,
7153 int has_children,
7154 void *data)
7155{
7156 struct dwarf2_cu *cu = reader->cu;
95554aad 7157 enum language *language_ptr = data;
6caca83c 7158
dee91e82
DE
7159 gdb_assert (cu->die_hash == NULL);
7160 cu->die_hash =
7161 htab_create_alloc_ex (cu->header.length / 12,
7162 die_hash,
7163 die_eq,
7164 NULL,
7165 &cu->comp_unit_obstack,
7166 hashtab_obstack_allocate,
7167 dummy_obstack_deallocate);
e142c38c 7168
dee91e82
DE
7169 if (has_children)
7170 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7171 &info_ptr, comp_unit_die);
7172 cu->dies = comp_unit_die;
7173 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7174
7175 /* We try not to read any attributes in this function, because not
9cdd5dbd 7176 all CUs needed for references have been loaded yet, and symbol
10b3939b 7177 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7178 or we won't be able to build types correctly.
7179 Similarly, if we do not read the producer, we can not apply
7180 producer-specific interpretation. */
95554aad 7181 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7182}
10b3939b 7183
dee91e82 7184/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7185
dee91e82 7186static void
95554aad
TT
7187load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7188 enum language pretend_language)
dee91e82 7189{
3019eac3 7190 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7191
f4dc4d17
DE
7192 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7193 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7194}
7195
3da10d80
KS
7196/* Add a DIE to the delayed physname list. */
7197
7198static void
7199add_to_method_list (struct type *type, int fnfield_index, int index,
7200 const char *name, struct die_info *die,
7201 struct dwarf2_cu *cu)
7202{
7203 struct delayed_method_info mi;
7204 mi.type = type;
7205 mi.fnfield_index = fnfield_index;
7206 mi.index = index;
7207 mi.name = name;
7208 mi.die = die;
7209 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7210}
7211
7212/* A cleanup for freeing the delayed method list. */
7213
7214static void
7215free_delayed_list (void *ptr)
7216{
7217 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7218 if (cu->method_list != NULL)
7219 {
7220 VEC_free (delayed_method_info, cu->method_list);
7221 cu->method_list = NULL;
7222 }
7223}
7224
7225/* Compute the physnames of any methods on the CU's method list.
7226
7227 The computation of method physnames is delayed in order to avoid the
7228 (bad) condition that one of the method's formal parameters is of an as yet
7229 incomplete type. */
7230
7231static void
7232compute_delayed_physnames (struct dwarf2_cu *cu)
7233{
7234 int i;
7235 struct delayed_method_info *mi;
7236 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7237 {
1d06ead6 7238 const char *physname;
3da10d80
KS
7239 struct fn_fieldlist *fn_flp
7240 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7241 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7242 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7243 }
7244}
7245
a766d390
DE
7246/* Go objects should be embedded in a DW_TAG_module DIE,
7247 and it's not clear if/how imported objects will appear.
7248 To keep Go support simple until that's worked out,
7249 go back through what we've read and create something usable.
7250 We could do this while processing each DIE, and feels kinda cleaner,
7251 but that way is more invasive.
7252 This is to, for example, allow the user to type "p var" or "b main"
7253 without having to specify the package name, and allow lookups
7254 of module.object to work in contexts that use the expression
7255 parser. */
7256
7257static void
7258fixup_go_packaging (struct dwarf2_cu *cu)
7259{
7260 char *package_name = NULL;
7261 struct pending *list;
7262 int i;
7263
7264 for (list = global_symbols; list != NULL; list = list->next)
7265 {
7266 for (i = 0; i < list->nsyms; ++i)
7267 {
7268 struct symbol *sym = list->symbol[i];
7269
7270 if (SYMBOL_LANGUAGE (sym) == language_go
7271 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7272 {
7273 char *this_package_name = go_symbol_package_name (sym);
7274
7275 if (this_package_name == NULL)
7276 continue;
7277 if (package_name == NULL)
7278 package_name = this_package_name;
7279 else
7280 {
7281 if (strcmp (package_name, this_package_name) != 0)
7282 complaint (&symfile_complaints,
7283 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7284 (SYMBOL_SYMTAB (sym)
05cba821 7285 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
7286 : cu->objfile->name),
7287 this_package_name, package_name);
7288 xfree (this_package_name);
7289 }
7290 }
7291 }
7292 }
7293
7294 if (package_name != NULL)
7295 {
7296 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7297 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7298 package_name,
7299 strlen (package_name));
a766d390 7300 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7301 saved_package_name, objfile);
a766d390
DE
7302 struct symbol *sym;
7303
7304 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7305
e623cf5d 7306 sym = allocate_symbol (objfile);
f85f34ed 7307 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7308 SYMBOL_SET_NAMES (sym, saved_package_name,
7309 strlen (saved_package_name), 0, objfile);
a766d390
DE
7310 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7311 e.g., "main" finds the "main" module and not C's main(). */
7312 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7313 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7314 SYMBOL_TYPE (sym) = type;
7315
7316 add_symbol_to_list (sym, &global_symbols);
7317
7318 xfree (package_name);
7319 }
7320}
7321
95554aad
TT
7322/* Return the symtab for PER_CU. This works properly regardless of
7323 whether we're using the index or psymtabs. */
7324
7325static struct symtab *
7326get_symtab (struct dwarf2_per_cu_data *per_cu)
7327{
7328 return (dwarf2_per_objfile->using_index
7329 ? per_cu->v.quick->symtab
7330 : per_cu->v.psymtab->symtab);
7331}
7332
7333/* A helper function for computing the list of all symbol tables
7334 included by PER_CU. */
7335
7336static void
ec94af83
DE
7337recursively_compute_inclusions (VEC (symtab_ptr) **result,
7338 htab_t all_children, htab_t all_type_symtabs,
f9125b6c
TT
7339 struct dwarf2_per_cu_data *per_cu,
7340 struct symtab *immediate_parent)
95554aad
TT
7341{
7342 void **slot;
7343 int ix;
ec94af83 7344 struct symtab *symtab;
95554aad
TT
7345 struct dwarf2_per_cu_data *iter;
7346
7347 slot = htab_find_slot (all_children, per_cu, INSERT);
7348 if (*slot != NULL)
7349 {
7350 /* This inclusion and its children have been processed. */
7351 return;
7352 }
7353
7354 *slot = per_cu;
7355 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7356 symtab = get_symtab (per_cu);
7357 if (symtab != NULL)
7358 {
7359 /* If this is a type unit only add its symbol table if we haven't
7360 seen it yet (type unit per_cu's can share symtabs). */
7361 if (per_cu->is_debug_types)
7362 {
7363 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7364 if (*slot == NULL)
7365 {
7366 *slot = symtab;
7367 VEC_safe_push (symtab_ptr, *result, symtab);
f9125b6c
TT
7368 if (symtab->user == NULL)
7369 symtab->user = immediate_parent;
ec94af83
DE
7370 }
7371 }
7372 else
f9125b6c
TT
7373 {
7374 VEC_safe_push (symtab_ptr, *result, symtab);
7375 if (symtab->user == NULL)
7376 symtab->user = immediate_parent;
7377 }
ec94af83 7378 }
95554aad
TT
7379
7380 for (ix = 0;
796a7ff8 7381 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7382 ++ix)
ec94af83
DE
7383 {
7384 recursively_compute_inclusions (result, all_children,
f9125b6c 7385 all_type_symtabs, iter, symtab);
ec94af83 7386 }
95554aad
TT
7387}
7388
7389/* Compute the symtab 'includes' fields for the symtab related to
7390 PER_CU. */
7391
7392static void
7393compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7394{
f4dc4d17
DE
7395 gdb_assert (! per_cu->is_debug_types);
7396
796a7ff8 7397 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7398 {
7399 int ix, len;
ec94af83
DE
7400 struct dwarf2_per_cu_data *per_cu_iter;
7401 struct symtab *symtab_iter;
7402 VEC (symtab_ptr) *result_symtabs = NULL;
7403 htab_t all_children, all_type_symtabs;
95554aad
TT
7404 struct symtab *symtab = get_symtab (per_cu);
7405
7406 /* If we don't have a symtab, we can just skip this case. */
7407 if (symtab == NULL)
7408 return;
7409
7410 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7411 NULL, xcalloc, xfree);
ec94af83
DE
7412 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7413 NULL, xcalloc, xfree);
95554aad
TT
7414
7415 for (ix = 0;
796a7ff8 7416 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7417 ix, per_cu_iter);
95554aad 7418 ++ix)
ec94af83
DE
7419 {
7420 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c
TT
7421 all_type_symtabs, per_cu_iter,
7422 symtab);
ec94af83 7423 }
95554aad 7424
ec94af83
DE
7425 /* Now we have a transitive closure of all the included symtabs. */
7426 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7427 symtab->includes
7428 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7429 (len + 1) * sizeof (struct symtab *));
7430 for (ix = 0;
ec94af83 7431 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7432 ++ix)
ec94af83 7433 symtab->includes[ix] = symtab_iter;
95554aad
TT
7434 symtab->includes[len] = NULL;
7435
ec94af83 7436 VEC_free (symtab_ptr, result_symtabs);
95554aad 7437 htab_delete (all_children);
ec94af83 7438 htab_delete (all_type_symtabs);
95554aad
TT
7439 }
7440}
7441
7442/* Compute the 'includes' field for the symtabs of all the CUs we just
7443 read. */
7444
7445static void
7446process_cu_includes (void)
7447{
7448 int ix;
7449 struct dwarf2_per_cu_data *iter;
7450
7451 for (ix = 0;
7452 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7453 ix, iter);
7454 ++ix)
f4dc4d17
DE
7455 {
7456 if (! iter->is_debug_types)
7457 compute_symtab_includes (iter);
7458 }
95554aad
TT
7459
7460 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7461}
7462
9cdd5dbd 7463/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7464 already been loaded into memory. */
7465
7466static void
95554aad
TT
7467process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7468 enum language pretend_language)
10b3939b 7469{
10b3939b 7470 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7471 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7472 CORE_ADDR lowpc, highpc;
7473 struct symtab *symtab;
3da10d80 7474 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7475 CORE_ADDR baseaddr;
4359dff1 7476 struct block *static_block;
10b3939b
DJ
7477
7478 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7479
10b3939b
DJ
7480 buildsym_init ();
7481 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7482 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7483
7484 cu->list_in_scope = &file_symbols;
c906108c 7485
95554aad
TT
7486 cu->language = pretend_language;
7487 cu->language_defn = language_def (cu->language);
7488
c906108c 7489 /* Do line number decoding in read_file_scope () */
10b3939b 7490 process_die (cu->dies, cu);
c906108c 7491
a766d390
DE
7492 /* For now fudge the Go package. */
7493 if (cu->language == language_go)
7494 fixup_go_packaging (cu);
7495
3da10d80
KS
7496 /* Now that we have processed all the DIEs in the CU, all the types
7497 should be complete, and it should now be safe to compute all of the
7498 physnames. */
7499 compute_delayed_physnames (cu);
7500 do_cleanups (delayed_list_cleanup);
7501
fae299cd
DC
7502 /* Some compilers don't define a DW_AT_high_pc attribute for the
7503 compilation unit. If the DW_AT_high_pc is missing, synthesize
7504 it, by scanning the DIE's below the compilation unit. */
10b3939b 7505 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7506
36586728 7507 static_block
ff546935 7508 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7509
7510 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7511 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7512 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7513 addrmap to help ensure it has an accurate map of pc values belonging to
7514 this comp unit. */
7515 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7516
7517 symtab = end_symtab_from_static_block (static_block, objfile,
7518 SECT_OFF_TEXT (objfile), 0);
c906108c 7519
8be455d7 7520 if (symtab != NULL)
c906108c 7521 {
df15bd07 7522 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7523
8be455d7
JK
7524 /* Set symtab language to language from DW_AT_language. If the
7525 compilation is from a C file generated by language preprocessors, do
7526 not set the language if it was already deduced by start_subfile. */
7527 if (!(cu->language == language_c && symtab->language != language_c))
7528 symtab->language = cu->language;
7529
7530 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7531 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7532 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7533 there were bugs in prologue debug info, fixed later in GCC-4.5
7534 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7535
7536 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7537 needed, it would be wrong due to missing DW_AT_producer there.
7538
7539 Still one can confuse GDB by using non-standard GCC compilation
7540 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7541 */
ab260dad 7542 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7543 symtab->locations_valid = 1;
e0d00bc7
JK
7544
7545 if (gcc_4_minor >= 5)
7546 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7547
7548 symtab->call_site_htab = cu->call_site_htab;
c906108c 7549 }
9291a0cd
TT
7550
7551 if (dwarf2_per_objfile->using_index)
7552 per_cu->v.quick->symtab = symtab;
7553 else
7554 {
7555 struct partial_symtab *pst = per_cu->v.psymtab;
7556 pst->symtab = symtab;
7557 pst->readin = 1;
7558 }
c906108c 7559
95554aad
TT
7560 /* Push it for inclusion processing later. */
7561 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7562
c906108c 7563 do_cleanups (back_to);
f4dc4d17 7564}
45cfd468 7565
f4dc4d17
DE
7566/* Generate full symbol information for type unit PER_CU, whose DIEs have
7567 already been loaded into memory. */
7568
7569static void
7570process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7571 enum language pretend_language)
7572{
7573 struct dwarf2_cu *cu = per_cu->cu;
7574 struct objfile *objfile = per_cu->objfile;
7575 struct symtab *symtab;
7576 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7577 struct signatured_type *sig_type;
7578
7579 gdb_assert (per_cu->is_debug_types);
7580 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7581
7582 buildsym_init ();
7583 back_to = make_cleanup (really_free_pendings, NULL);
7584 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7585
7586 cu->list_in_scope = &file_symbols;
7587
7588 cu->language = pretend_language;
7589 cu->language_defn = language_def (cu->language);
7590
7591 /* The symbol tables are set up in read_type_unit_scope. */
7592 process_die (cu->dies, cu);
7593
7594 /* For now fudge the Go package. */
7595 if (cu->language == language_go)
7596 fixup_go_packaging (cu);
7597
7598 /* Now that we have processed all the DIEs in the CU, all the types
7599 should be complete, and it should now be safe to compute all of the
7600 physnames. */
7601 compute_delayed_physnames (cu);
7602 do_cleanups (delayed_list_cleanup);
7603
7604 /* TUs share symbol tables.
7605 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7606 of it with end_expandable_symtab. Otherwise, complete the addition of
7607 this TU's symbols to the existing symtab. */
0186c6a7 7608 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7609 {
f4dc4d17 7610 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7611 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7612
7613 if (symtab != NULL)
7614 {
7615 /* Set symtab language to language from DW_AT_language. If the
7616 compilation is from a C file generated by language preprocessors,
7617 do not set the language if it was already deduced by
7618 start_subfile. */
7619 if (!(cu->language == language_c && symtab->language != language_c))
7620 symtab->language = cu->language;
7621 }
7622 }
7623 else
7624 {
7625 augment_type_symtab (objfile,
0186c6a7
DE
7626 sig_type->type_unit_group->primary_symtab);
7627 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7628 }
7629
7630 if (dwarf2_per_objfile->using_index)
7631 per_cu->v.quick->symtab = symtab;
7632 else
7633 {
7634 struct partial_symtab *pst = per_cu->v.psymtab;
7635 pst->symtab = symtab;
7636 pst->readin = 1;
45cfd468 7637 }
f4dc4d17
DE
7638
7639 do_cleanups (back_to);
c906108c
SS
7640}
7641
95554aad
TT
7642/* Process an imported unit DIE. */
7643
7644static void
7645process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7646{
7647 struct attribute *attr;
7648
f4dc4d17
DE
7649 /* For now we don't handle imported units in type units. */
7650 if (cu->per_cu->is_debug_types)
7651 {
7652 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7653 " supported in type units [in module %s]"),
7654 cu->objfile->name);
7655 }
7656
95554aad
TT
7657 attr = dwarf2_attr (die, DW_AT_import, cu);
7658 if (attr != NULL)
7659 {
7660 struct dwarf2_per_cu_data *per_cu;
7661 struct symtab *imported_symtab;
7662 sect_offset offset;
36586728 7663 int is_dwz;
95554aad
TT
7664
7665 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7666 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7667 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 7668
69d751e3 7669 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
7670 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7671 load_full_comp_unit (per_cu, cu->language);
7672
796a7ff8 7673 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7674 per_cu);
7675 }
7676}
7677
c906108c
SS
7678/* Process a die and its children. */
7679
7680static void
e7c27a73 7681process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7682{
7683 switch (die->tag)
7684 {
7685 case DW_TAG_padding:
7686 break;
7687 case DW_TAG_compile_unit:
95554aad 7688 case DW_TAG_partial_unit:
e7c27a73 7689 read_file_scope (die, cu);
c906108c 7690 break;
348e048f
DE
7691 case DW_TAG_type_unit:
7692 read_type_unit_scope (die, cu);
7693 break;
c906108c 7694 case DW_TAG_subprogram:
c906108c 7695 case DW_TAG_inlined_subroutine:
edb3359d 7696 read_func_scope (die, cu);
c906108c
SS
7697 break;
7698 case DW_TAG_lexical_block:
14898363
L
7699 case DW_TAG_try_block:
7700 case DW_TAG_catch_block:
e7c27a73 7701 read_lexical_block_scope (die, cu);
c906108c 7702 break;
96408a79
SA
7703 case DW_TAG_GNU_call_site:
7704 read_call_site_scope (die, cu);
7705 break;
c906108c 7706 case DW_TAG_class_type:
680b30c7 7707 case DW_TAG_interface_type:
c906108c
SS
7708 case DW_TAG_structure_type:
7709 case DW_TAG_union_type:
134d01f1 7710 process_structure_scope (die, cu);
c906108c
SS
7711 break;
7712 case DW_TAG_enumeration_type:
134d01f1 7713 process_enumeration_scope (die, cu);
c906108c 7714 break;
134d01f1 7715
f792889a
DJ
7716 /* These dies have a type, but processing them does not create
7717 a symbol or recurse to process the children. Therefore we can
7718 read them on-demand through read_type_die. */
c906108c 7719 case DW_TAG_subroutine_type:
72019c9c 7720 case DW_TAG_set_type:
c906108c 7721 case DW_TAG_array_type:
c906108c 7722 case DW_TAG_pointer_type:
c906108c 7723 case DW_TAG_ptr_to_member_type:
c906108c 7724 case DW_TAG_reference_type:
c906108c 7725 case DW_TAG_string_type:
c906108c 7726 break;
134d01f1 7727
c906108c 7728 case DW_TAG_base_type:
a02abb62 7729 case DW_TAG_subrange_type:
cb249c71 7730 case DW_TAG_typedef:
134d01f1
DJ
7731 /* Add a typedef symbol for the type definition, if it has a
7732 DW_AT_name. */
f792889a 7733 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7734 break;
c906108c 7735 case DW_TAG_common_block:
e7c27a73 7736 read_common_block (die, cu);
c906108c
SS
7737 break;
7738 case DW_TAG_common_inclusion:
7739 break;
d9fa45fe 7740 case DW_TAG_namespace:
4d4ec4e5 7741 cu->processing_has_namespace_info = 1;
e7c27a73 7742 read_namespace (die, cu);
d9fa45fe 7743 break;
5d7cb8df 7744 case DW_TAG_module:
4d4ec4e5 7745 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7746 read_module (die, cu);
7747 break;
d9fa45fe
DC
7748 case DW_TAG_imported_declaration:
7749 case DW_TAG_imported_module:
4d4ec4e5 7750 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7751 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7752 || cu->language != language_fortran))
7753 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7754 dwarf_tag_name (die->tag));
7755 read_import_statement (die, cu);
d9fa45fe 7756 break;
95554aad
TT
7757
7758 case DW_TAG_imported_unit:
7759 process_imported_unit_die (die, cu);
7760 break;
7761
c906108c 7762 default:
e7c27a73 7763 new_symbol (die, NULL, cu);
c906108c
SS
7764 break;
7765 }
7766}
ca69b9e6
DE
7767\f
7768/* DWARF name computation. */
c906108c 7769
94af9270
KS
7770/* A helper function for dwarf2_compute_name which determines whether DIE
7771 needs to have the name of the scope prepended to the name listed in the
7772 die. */
7773
7774static int
7775die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7776{
1c809c68
TT
7777 struct attribute *attr;
7778
94af9270
KS
7779 switch (die->tag)
7780 {
7781 case DW_TAG_namespace:
7782 case DW_TAG_typedef:
7783 case DW_TAG_class_type:
7784 case DW_TAG_interface_type:
7785 case DW_TAG_structure_type:
7786 case DW_TAG_union_type:
7787 case DW_TAG_enumeration_type:
7788 case DW_TAG_enumerator:
7789 case DW_TAG_subprogram:
7790 case DW_TAG_member:
7791 return 1;
7792
7793 case DW_TAG_variable:
c2b0a229 7794 case DW_TAG_constant:
94af9270
KS
7795 /* We only need to prefix "globally" visible variables. These include
7796 any variable marked with DW_AT_external or any variable that
7797 lives in a namespace. [Variables in anonymous namespaces
7798 require prefixing, but they are not DW_AT_external.] */
7799
7800 if (dwarf2_attr (die, DW_AT_specification, cu))
7801 {
7802 struct dwarf2_cu *spec_cu = cu;
9a619af0 7803
94af9270
KS
7804 return die_needs_namespace (die_specification (die, &spec_cu),
7805 spec_cu);
7806 }
7807
1c809c68 7808 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7809 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7810 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7811 return 0;
7812 /* A variable in a lexical block of some kind does not need a
7813 namespace, even though in C++ such variables may be external
7814 and have a mangled name. */
7815 if (die->parent->tag == DW_TAG_lexical_block
7816 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7817 || die->parent->tag == DW_TAG_catch_block
7818 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7819 return 0;
7820 return 1;
94af9270
KS
7821
7822 default:
7823 return 0;
7824 }
7825}
7826
98bfdba5
PA
7827/* Retrieve the last character from a mem_file. */
7828
7829static void
7830do_ui_file_peek_last (void *object, const char *buffer, long length)
7831{
7832 char *last_char_p = (char *) object;
7833
7834 if (length > 0)
7835 *last_char_p = buffer[length - 1];
7836}
7837
94af9270 7838/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7839 compute the physname for the object, which include a method's:
7840 - formal parameters (C++/Java),
7841 - receiver type (Go),
7842 - return type (Java).
7843
7844 The term "physname" is a bit confusing.
7845 For C++, for example, it is the demangled name.
7846 For Go, for example, it's the mangled name.
94af9270 7847
af6b7be1
JB
7848 For Ada, return the DIE's linkage name rather than the fully qualified
7849 name. PHYSNAME is ignored..
7850
94af9270
KS
7851 The result is allocated on the objfile_obstack and canonicalized. */
7852
7853static const char *
15d034d0
TT
7854dwarf2_compute_name (const char *name,
7855 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7856 int physname)
7857{
bb5ed363
DE
7858 struct objfile *objfile = cu->objfile;
7859
94af9270
KS
7860 if (name == NULL)
7861 name = dwarf2_name (die, cu);
7862
f55ee35c
JK
7863 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7864 compute it by typename_concat inside GDB. */
7865 if (cu->language == language_ada
7866 || (cu->language == language_fortran && physname))
7867 {
7868 /* For Ada unit, we prefer the linkage name over the name, as
7869 the former contains the exported name, which the user expects
7870 to be able to reference. Ideally, we want the user to be able
7871 to reference this entity using either natural or linkage name,
7872 but we haven't started looking at this enhancement yet. */
7873 struct attribute *attr;
7874
7875 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7876 if (attr == NULL)
7877 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7878 if (attr && DW_STRING (attr))
7879 return DW_STRING (attr);
7880 }
7881
94af9270
KS
7882 /* These are the only languages we know how to qualify names in. */
7883 if (name != NULL
f55ee35c
JK
7884 && (cu->language == language_cplus || cu->language == language_java
7885 || cu->language == language_fortran))
94af9270
KS
7886 {
7887 if (die_needs_namespace (die, cu))
7888 {
7889 long length;
0d5cff50 7890 const char *prefix;
94af9270
KS
7891 struct ui_file *buf;
7892
7893 prefix = determine_prefix (die, cu);
7894 buf = mem_fileopen ();
7895 if (*prefix != '\0')
7896 {
f55ee35c
JK
7897 char *prefixed_name = typename_concat (NULL, prefix, name,
7898 physname, cu);
9a619af0 7899
94af9270
KS
7900 fputs_unfiltered (prefixed_name, buf);
7901 xfree (prefixed_name);
7902 }
7903 else
62d5b8da 7904 fputs_unfiltered (name, buf);
94af9270 7905
98bfdba5
PA
7906 /* Template parameters may be specified in the DIE's DW_AT_name, or
7907 as children with DW_TAG_template_type_param or
7908 DW_TAG_value_type_param. If the latter, add them to the name
7909 here. If the name already has template parameters, then
7910 skip this step; some versions of GCC emit both, and
7911 it is more efficient to use the pre-computed name.
7912
7913 Something to keep in mind about this process: it is very
7914 unlikely, or in some cases downright impossible, to produce
7915 something that will match the mangled name of a function.
7916 If the definition of the function has the same debug info,
7917 we should be able to match up with it anyway. But fallbacks
7918 using the minimal symbol, for instance to find a method
7919 implemented in a stripped copy of libstdc++, will not work.
7920 If we do not have debug info for the definition, we will have to
7921 match them up some other way.
7922
7923 When we do name matching there is a related problem with function
7924 templates; two instantiated function templates are allowed to
7925 differ only by their return types, which we do not add here. */
7926
7927 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7928 {
7929 struct attribute *attr;
7930 struct die_info *child;
7931 int first = 1;
7932
7933 die->building_fullname = 1;
7934
7935 for (child = die->child; child != NULL; child = child->sibling)
7936 {
7937 struct type *type;
12df843f 7938 LONGEST value;
d521ce57 7939 const gdb_byte *bytes;
98bfdba5
PA
7940 struct dwarf2_locexpr_baton *baton;
7941 struct value *v;
7942
7943 if (child->tag != DW_TAG_template_type_param
7944 && child->tag != DW_TAG_template_value_param)
7945 continue;
7946
7947 if (first)
7948 {
7949 fputs_unfiltered ("<", buf);
7950 first = 0;
7951 }
7952 else
7953 fputs_unfiltered (", ", buf);
7954
7955 attr = dwarf2_attr (child, DW_AT_type, cu);
7956 if (attr == NULL)
7957 {
7958 complaint (&symfile_complaints,
7959 _("template parameter missing DW_AT_type"));
7960 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7961 continue;
7962 }
7963 type = die_type (child, cu);
7964
7965 if (child->tag == DW_TAG_template_type_param)
7966 {
79d43c61 7967 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7968 continue;
7969 }
7970
7971 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7972 if (attr == NULL)
7973 {
7974 complaint (&symfile_complaints,
3e43a32a
MS
7975 _("template parameter missing "
7976 "DW_AT_const_value"));
98bfdba5
PA
7977 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7978 continue;
7979 }
7980
7981 dwarf2_const_value_attr (attr, type, name,
7982 &cu->comp_unit_obstack, cu,
7983 &value, &bytes, &baton);
7984
7985 if (TYPE_NOSIGN (type))
7986 /* GDB prints characters as NUMBER 'CHAR'. If that's
7987 changed, this can use value_print instead. */
7988 c_printchar (value, type, buf);
7989 else
7990 {
7991 struct value_print_options opts;
7992
7993 if (baton != NULL)
7994 v = dwarf2_evaluate_loc_desc (type, NULL,
7995 baton->data,
7996 baton->size,
7997 baton->per_cu);
7998 else if (bytes != NULL)
7999 {
8000 v = allocate_value (type);
8001 memcpy (value_contents_writeable (v), bytes,
8002 TYPE_LENGTH (type));
8003 }
8004 else
8005 v = value_from_longest (type, value);
8006
3e43a32a
MS
8007 /* Specify decimal so that we do not depend on
8008 the radix. */
98bfdba5
PA
8009 get_formatted_print_options (&opts, 'd');
8010 opts.raw = 1;
8011 value_print (v, buf, &opts);
8012 release_value (v);
8013 value_free (v);
8014 }
8015 }
8016
8017 die->building_fullname = 0;
8018
8019 if (!first)
8020 {
8021 /* Close the argument list, with a space if necessary
8022 (nested templates). */
8023 char last_char = '\0';
8024 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8025 if (last_char == '>')
8026 fputs_unfiltered (" >", buf);
8027 else
8028 fputs_unfiltered (">", buf);
8029 }
8030 }
8031
94af9270
KS
8032 /* For Java and C++ methods, append formal parameter type
8033 information, if PHYSNAME. */
6e70227d 8034
94af9270
KS
8035 if (physname && die->tag == DW_TAG_subprogram
8036 && (cu->language == language_cplus
8037 || cu->language == language_java))
8038 {
8039 struct type *type = read_type_die (die, cu);
8040
79d43c61
TT
8041 c_type_print_args (type, buf, 1, cu->language,
8042 &type_print_raw_options);
94af9270
KS
8043
8044 if (cu->language == language_java)
8045 {
8046 /* For java, we must append the return type to method
0963b4bd 8047 names. */
94af9270
KS
8048 if (die->tag == DW_TAG_subprogram)
8049 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8050 0, 0, &type_print_raw_options);
94af9270
KS
8051 }
8052 else if (cu->language == language_cplus)
8053 {
60430eff
DJ
8054 /* Assume that an artificial first parameter is
8055 "this", but do not crash if it is not. RealView
8056 marks unnamed (and thus unused) parameters as
8057 artificial; there is no way to differentiate
8058 the two cases. */
94af9270
KS
8059 if (TYPE_NFIELDS (type) > 0
8060 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8061 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8062 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8063 0))))
94af9270
KS
8064 fputs_unfiltered (" const", buf);
8065 }
8066 }
8067
bb5ed363 8068 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8069 &length);
8070 ui_file_delete (buf);
8071
8072 if (cu->language == language_cplus)
8073 {
15d034d0 8074 const char *cname
94af9270 8075 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8076 &objfile->objfile_obstack);
9a619af0 8077
94af9270
KS
8078 if (cname != NULL)
8079 name = cname;
8080 }
8081 }
8082 }
8083
8084 return name;
8085}
8086
0114d602
DJ
8087/* Return the fully qualified name of DIE, based on its DW_AT_name.
8088 If scope qualifiers are appropriate they will be added. The result
8089 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8090 not have a name. NAME may either be from a previous call to
8091 dwarf2_name or NULL.
8092
0963b4bd 8093 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8094
8095static const char *
15d034d0 8096dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8097{
94af9270
KS
8098 return dwarf2_compute_name (name, die, cu, 0);
8099}
0114d602 8100
94af9270
KS
8101/* Construct a physname for the given DIE in CU. NAME may either be
8102 from a previous call to dwarf2_name or NULL. The result will be
8103 allocated on the objfile_objstack or NULL if the DIE does not have a
8104 name.
0114d602 8105
94af9270 8106 The output string will be canonicalized (if C++/Java). */
0114d602 8107
94af9270 8108static const char *
15d034d0 8109dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8110{
bb5ed363 8111 struct objfile *objfile = cu->objfile;
900e11f9
JK
8112 struct attribute *attr;
8113 const char *retval, *mangled = NULL, *canon = NULL;
8114 struct cleanup *back_to;
8115 int need_copy = 1;
8116
8117 /* In this case dwarf2_compute_name is just a shortcut not building anything
8118 on its own. */
8119 if (!die_needs_namespace (die, cu))
8120 return dwarf2_compute_name (name, die, cu, 1);
8121
8122 back_to = make_cleanup (null_cleanup, NULL);
8123
8124 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8125 if (!attr)
8126 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8127
8128 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8129 has computed. */
8130 if (attr && DW_STRING (attr))
8131 {
8132 char *demangled;
8133
8134 mangled = DW_STRING (attr);
8135
8136 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8137 type. It is easier for GDB users to search for such functions as
8138 `name(params)' than `long name(params)'. In such case the minimal
8139 symbol names do not match the full symbol names but for template
8140 functions there is never a need to look up their definition from their
8141 declaration so the only disadvantage remains the minimal symbol
8142 variant `long name(params)' does not have the proper inferior type.
8143 */
8144
a766d390
DE
8145 if (cu->language == language_go)
8146 {
8147 /* This is a lie, but we already lie to the caller new_symbol_full.
8148 new_symbol_full assumes we return the mangled name.
8149 This just undoes that lie until things are cleaned up. */
8150 demangled = NULL;
8151 }
8152 else
8153 {
8de20a37
TT
8154 demangled = gdb_demangle (mangled,
8155 (DMGL_PARAMS | DMGL_ANSI
8156 | (cu->language == language_java
8157 ? DMGL_JAVA | DMGL_RET_POSTFIX
8158 : DMGL_RET_DROP)));
a766d390 8159 }
900e11f9
JK
8160 if (demangled)
8161 {
8162 make_cleanup (xfree, demangled);
8163 canon = demangled;
8164 }
8165 else
8166 {
8167 canon = mangled;
8168 need_copy = 0;
8169 }
8170 }
8171
8172 if (canon == NULL || check_physname)
8173 {
8174 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8175
8176 if (canon != NULL && strcmp (physname, canon) != 0)
8177 {
8178 /* It may not mean a bug in GDB. The compiler could also
8179 compute DW_AT_linkage_name incorrectly. But in such case
8180 GDB would need to be bug-to-bug compatible. */
8181
8182 complaint (&symfile_complaints,
8183 _("Computed physname <%s> does not match demangled <%s> "
8184 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 8185 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
8186
8187 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8188 is available here - over computed PHYSNAME. It is safer
8189 against both buggy GDB and buggy compilers. */
8190
8191 retval = canon;
8192 }
8193 else
8194 {
8195 retval = physname;
8196 need_copy = 0;
8197 }
8198 }
8199 else
8200 retval = canon;
8201
8202 if (need_copy)
10f0c4bb 8203 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8204
8205 do_cleanups (back_to);
8206 return retval;
0114d602
DJ
8207}
8208
27aa8d6a
SW
8209/* Read the import statement specified by the given die and record it. */
8210
8211static void
8212read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8213{
bb5ed363 8214 struct objfile *objfile = cu->objfile;
27aa8d6a 8215 struct attribute *import_attr;
32019081 8216 struct die_info *imported_die, *child_die;
de4affc9 8217 struct dwarf2_cu *imported_cu;
27aa8d6a 8218 const char *imported_name;
794684b6 8219 const char *imported_name_prefix;
13387711
SW
8220 const char *canonical_name;
8221 const char *import_alias;
8222 const char *imported_declaration = NULL;
794684b6 8223 const char *import_prefix;
32019081
JK
8224 VEC (const_char_ptr) *excludes = NULL;
8225 struct cleanup *cleanups;
13387711 8226
27aa8d6a
SW
8227 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8228 if (import_attr == NULL)
8229 {
8230 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8231 dwarf_tag_name (die->tag));
8232 return;
8233 }
8234
de4affc9
CC
8235 imported_cu = cu;
8236 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8237 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8238 if (imported_name == NULL)
8239 {
8240 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8241
8242 The import in the following code:
8243 namespace A
8244 {
8245 typedef int B;
8246 }
8247
8248 int main ()
8249 {
8250 using A::B;
8251 B b;
8252 return b;
8253 }
8254
8255 ...
8256 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8257 <52> DW_AT_decl_file : 1
8258 <53> DW_AT_decl_line : 6
8259 <54> DW_AT_import : <0x75>
8260 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8261 <59> DW_AT_name : B
8262 <5b> DW_AT_decl_file : 1
8263 <5c> DW_AT_decl_line : 2
8264 <5d> DW_AT_type : <0x6e>
8265 ...
8266 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8267 <76> DW_AT_byte_size : 4
8268 <77> DW_AT_encoding : 5 (signed)
8269
8270 imports the wrong die ( 0x75 instead of 0x58 ).
8271 This case will be ignored until the gcc bug is fixed. */
8272 return;
8273 }
8274
82856980
SW
8275 /* Figure out the local name after import. */
8276 import_alias = dwarf2_name (die, cu);
27aa8d6a 8277
794684b6
SW
8278 /* Figure out where the statement is being imported to. */
8279 import_prefix = determine_prefix (die, cu);
8280
8281 /* Figure out what the scope of the imported die is and prepend it
8282 to the name of the imported die. */
de4affc9 8283 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8284
f55ee35c
JK
8285 if (imported_die->tag != DW_TAG_namespace
8286 && imported_die->tag != DW_TAG_module)
794684b6 8287 {
13387711
SW
8288 imported_declaration = imported_name;
8289 canonical_name = imported_name_prefix;
794684b6 8290 }
13387711 8291 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8292 canonical_name = obconcat (&objfile->objfile_obstack,
8293 imported_name_prefix, "::", imported_name,
8294 (char *) NULL);
13387711
SW
8295 else
8296 canonical_name = imported_name;
794684b6 8297
32019081
JK
8298 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8299
8300 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8301 for (child_die = die->child; child_die && child_die->tag;
8302 child_die = sibling_die (child_die))
8303 {
8304 /* DWARF-4: A Fortran use statement with a “rename list” may be
8305 represented by an imported module entry with an import attribute
8306 referring to the module and owned entries corresponding to those
8307 entities that are renamed as part of being imported. */
8308
8309 if (child_die->tag != DW_TAG_imported_declaration)
8310 {
8311 complaint (&symfile_complaints,
8312 _("child DW_TAG_imported_declaration expected "
8313 "- DIE at 0x%x [in module %s]"),
b64f50a1 8314 child_die->offset.sect_off, objfile->name);
32019081
JK
8315 continue;
8316 }
8317
8318 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8319 if (import_attr == NULL)
8320 {
8321 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8322 dwarf_tag_name (child_die->tag));
8323 continue;
8324 }
8325
8326 imported_cu = cu;
8327 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8328 &imported_cu);
8329 imported_name = dwarf2_name (imported_die, imported_cu);
8330 if (imported_name == NULL)
8331 {
8332 complaint (&symfile_complaints,
8333 _("child DW_TAG_imported_declaration has unknown "
8334 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 8335 child_die->offset.sect_off, objfile->name);
32019081
JK
8336 continue;
8337 }
8338
8339 VEC_safe_push (const_char_ptr, excludes, imported_name);
8340
8341 process_die (child_die, cu);
8342 }
8343
c0cc3a76
SW
8344 cp_add_using_directive (import_prefix,
8345 canonical_name,
8346 import_alias,
13387711 8347 imported_declaration,
32019081 8348 excludes,
12aaed36 8349 0,
bb5ed363 8350 &objfile->objfile_obstack);
32019081
JK
8351
8352 do_cleanups (cleanups);
27aa8d6a
SW
8353}
8354
f4dc4d17 8355/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8356
cb1df416
DJ
8357static void
8358free_cu_line_header (void *arg)
8359{
8360 struct dwarf2_cu *cu = arg;
8361
8362 free_line_header (cu->line_header);
8363 cu->line_header = NULL;
8364}
8365
1b80a9fa
JK
8366/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8367 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8368 this, it was first present in GCC release 4.3.0. */
8369
8370static int
8371producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8372{
8373 if (!cu->checked_producer)
8374 check_producer (cu);
8375
8376 return cu->producer_is_gcc_lt_4_3;
8377}
8378
9291a0cd
TT
8379static void
8380find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8381 const char **name, const char **comp_dir)
9291a0cd
TT
8382{
8383 struct attribute *attr;
8384
8385 *name = NULL;
8386 *comp_dir = NULL;
8387
8388 /* Find the filename. Do not use dwarf2_name here, since the filename
8389 is not a source language identifier. */
8390 attr = dwarf2_attr (die, DW_AT_name, cu);
8391 if (attr)
8392 {
8393 *name = DW_STRING (attr);
8394 }
8395
8396 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8397 if (attr)
8398 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8399 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8400 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8401 {
15d034d0
TT
8402 char *d = ldirname (*name);
8403
8404 *comp_dir = d;
8405 if (d != NULL)
8406 make_cleanup (xfree, d);
9291a0cd
TT
8407 }
8408 if (*comp_dir != NULL)
8409 {
8410 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8411 directory, get rid of it. */
8412 char *cp = strchr (*comp_dir, ':');
8413
8414 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8415 *comp_dir = cp + 1;
8416 }
8417
8418 if (*name == NULL)
8419 *name = "<unknown>";
8420}
8421
f4dc4d17
DE
8422/* Handle DW_AT_stmt_list for a compilation unit.
8423 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8424 COMP_DIR is the compilation directory.
8425 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8426
8427static void
8428handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8429 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8430{
8431 struct attribute *attr;
2ab95328 8432
f4dc4d17
DE
8433 gdb_assert (! cu->per_cu->is_debug_types);
8434
2ab95328
TT
8435 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8436 if (attr)
8437 {
8438 unsigned int line_offset = DW_UNSND (attr);
8439 struct line_header *line_header
3019eac3 8440 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8441
8442 if (line_header)
dee91e82
DE
8443 {
8444 cu->line_header = line_header;
8445 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8446 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8447 }
2ab95328
TT
8448 }
8449}
8450
95554aad 8451/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8452
c906108c 8453static void
e7c27a73 8454read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8455{
dee91e82 8456 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8457 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8458 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8459 CORE_ADDR highpc = ((CORE_ADDR) 0);
8460 struct attribute *attr;
15d034d0
TT
8461 const char *name = NULL;
8462 const char *comp_dir = NULL;
c906108c
SS
8463 struct die_info *child_die;
8464 bfd *abfd = objfile->obfd;
e142c38c 8465 CORE_ADDR baseaddr;
6e70227d 8466
e142c38c 8467 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8468
fae299cd 8469 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8470
8471 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8472 from finish_block. */
2acceee2 8473 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8474 lowpc = highpc;
8475 lowpc += baseaddr;
8476 highpc += baseaddr;
8477
9291a0cd 8478 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8479
95554aad 8480 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8481
f4b8a18d
KW
8482 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8483 standardised yet. As a workaround for the language detection we fall
8484 back to the DW_AT_producer string. */
8485 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8486 cu->language = language_opencl;
8487
3019eac3
DE
8488 /* Similar hack for Go. */
8489 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8490 set_cu_language (DW_LANG_Go, cu);
8491
f4dc4d17 8492 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8493
8494 /* Decode line number information if present. We do this before
8495 processing child DIEs, so that the line header table is available
8496 for DW_AT_decl_file. */
f4dc4d17 8497 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8498
8499 /* Process all dies in compilation unit. */
8500 if (die->child != NULL)
8501 {
8502 child_die = die->child;
8503 while (child_die && child_die->tag)
8504 {
8505 process_die (child_die, cu);
8506 child_die = sibling_die (child_die);
8507 }
8508 }
8509
8510 /* Decode macro information, if present. Dwarf 2 macro information
8511 refers to information in the line number info statement program
8512 header, so we can only read it if we've read the header
8513 successfully. */
8514 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8515 if (attr && cu->line_header)
8516 {
8517 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8518 complaint (&symfile_complaints,
8519 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8520
09262596 8521 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8522 }
8523 else
8524 {
8525 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8526 if (attr && cu->line_header)
8527 {
8528 unsigned int macro_offset = DW_UNSND (attr);
8529
09262596 8530 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8531 }
8532 }
8533
8534 do_cleanups (back_to);
8535}
8536
f4dc4d17
DE
8537/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8538 Create the set of symtabs used by this TU, or if this TU is sharing
8539 symtabs with another TU and the symtabs have already been created
8540 then restore those symtabs in the line header.
8541 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8542
8543static void
f4dc4d17 8544setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8545{
f4dc4d17
DE
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8548 struct type_unit_group *tu_group;
8549 int first_time;
8550 struct line_header *lh;
3019eac3 8551 struct attribute *attr;
f4dc4d17 8552 unsigned int i, line_offset;
0186c6a7 8553 struct signatured_type *sig_type;
3019eac3 8554
f4dc4d17 8555 gdb_assert (per_cu->is_debug_types);
0186c6a7 8556 sig_type = (struct signatured_type *) per_cu;
3019eac3 8557
f4dc4d17 8558 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8559
f4dc4d17 8560 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8561 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8562 if (sig_type->type_unit_group == NULL)
8563 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8564 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8565
8566 /* If we've already processed this stmt_list there's no real need to
8567 do it again, we could fake it and just recreate the part we need
8568 (file name,index -> symtab mapping). If data shows this optimization
8569 is useful we can do it then. */
8570 first_time = tu_group->primary_symtab == NULL;
8571
8572 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8573 debug info. */
8574 lh = NULL;
8575 if (attr != NULL)
3019eac3 8576 {
f4dc4d17
DE
8577 line_offset = DW_UNSND (attr);
8578 lh = dwarf_decode_line_header (line_offset, cu);
8579 }
8580 if (lh == NULL)
8581 {
8582 if (first_time)
8583 dwarf2_start_symtab (cu, "", NULL, 0);
8584 else
8585 {
8586 gdb_assert (tu_group->symtabs == NULL);
8587 restart_symtab (0);
8588 }
8589 /* Note: The primary symtab will get allocated at the end. */
8590 return;
3019eac3
DE
8591 }
8592
f4dc4d17
DE
8593 cu->line_header = lh;
8594 make_cleanup (free_cu_line_header, cu);
3019eac3 8595
f4dc4d17
DE
8596 if (first_time)
8597 {
8598 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8599
f4dc4d17
DE
8600 tu_group->num_symtabs = lh->num_file_names;
8601 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8602
f4dc4d17
DE
8603 for (i = 0; i < lh->num_file_names; ++i)
8604 {
d521ce57 8605 const char *dir = NULL;
f4dc4d17 8606 struct file_entry *fe = &lh->file_names[i];
3019eac3 8607
f4dc4d17
DE
8608 if (fe->dir_index)
8609 dir = lh->include_dirs[fe->dir_index - 1];
8610 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8611
f4dc4d17
DE
8612 /* Note: We don't have to watch for the main subfile here, type units
8613 don't have DW_AT_name. */
3019eac3 8614
f4dc4d17
DE
8615 if (current_subfile->symtab == NULL)
8616 {
8617 /* NOTE: start_subfile will recognize when it's been passed
8618 a file it has already seen. So we can't assume there's a
8619 simple mapping from lh->file_names to subfiles,
8620 lh->file_names may contain dups. */
8621 current_subfile->symtab = allocate_symtab (current_subfile->name,
8622 objfile);
8623 }
8624
8625 fe->symtab = current_subfile->symtab;
8626 tu_group->symtabs[i] = fe->symtab;
8627 }
8628 }
8629 else
3019eac3 8630 {
f4dc4d17
DE
8631 restart_symtab (0);
8632
8633 for (i = 0; i < lh->num_file_names; ++i)
8634 {
8635 struct file_entry *fe = &lh->file_names[i];
8636
8637 fe->symtab = tu_group->symtabs[i];
8638 }
3019eac3
DE
8639 }
8640
f4dc4d17
DE
8641 /* The main symtab is allocated last. Type units don't have DW_AT_name
8642 so they don't have a "real" (so to speak) symtab anyway.
8643 There is later code that will assign the main symtab to all symbols
8644 that don't have one. We need to handle the case of a symbol with a
8645 missing symtab (DW_AT_decl_file) anyway. */
8646}
3019eac3 8647
f4dc4d17
DE
8648/* Process DW_TAG_type_unit.
8649 For TUs we want to skip the first top level sibling if it's not the
8650 actual type being defined by this TU. In this case the first top
8651 level sibling is there to provide context only. */
3019eac3 8652
f4dc4d17
DE
8653static void
8654read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8655{
8656 struct die_info *child_die;
3019eac3 8657
f4dc4d17
DE
8658 prepare_one_comp_unit (cu, die, language_minimal);
8659
8660 /* Initialize (or reinitialize) the machinery for building symtabs.
8661 We do this before processing child DIEs, so that the line header table
8662 is available for DW_AT_decl_file. */
8663 setup_type_unit_groups (die, cu);
8664
8665 if (die->child != NULL)
8666 {
8667 child_die = die->child;
8668 while (child_die && child_die->tag)
8669 {
8670 process_die (child_die, cu);
8671 child_die = sibling_die (child_die);
8672 }
8673 }
3019eac3
DE
8674}
8675\f
80626a55
DE
8676/* DWO/DWP files.
8677
8678 http://gcc.gnu.org/wiki/DebugFission
8679 http://gcc.gnu.org/wiki/DebugFissionDWP
8680
8681 To simplify handling of both DWO files ("object" files with the DWARF info)
8682 and DWP files (a file with the DWOs packaged up into one file), we treat
8683 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8684
8685static hashval_t
8686hash_dwo_file (const void *item)
8687{
8688 const struct dwo_file *dwo_file = item;
a2ce51a0 8689 hashval_t hash;
3019eac3 8690
a2ce51a0
DE
8691 hash = htab_hash_string (dwo_file->dwo_name);
8692 if (dwo_file->comp_dir != NULL)
8693 hash += htab_hash_string (dwo_file->comp_dir);
8694 return hash;
3019eac3
DE
8695}
8696
8697static int
8698eq_dwo_file (const void *item_lhs, const void *item_rhs)
8699{
8700 const struct dwo_file *lhs = item_lhs;
8701 const struct dwo_file *rhs = item_rhs;
8702
a2ce51a0
DE
8703 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8704 return 0;
8705 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8706 return lhs->comp_dir == rhs->comp_dir;
8707 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8708}
8709
8710/* Allocate a hash table for DWO files. */
8711
8712static htab_t
8713allocate_dwo_file_hash_table (void)
8714{
8715 struct objfile *objfile = dwarf2_per_objfile->objfile;
8716
8717 return htab_create_alloc_ex (41,
8718 hash_dwo_file,
8719 eq_dwo_file,
8720 NULL,
8721 &objfile->objfile_obstack,
8722 hashtab_obstack_allocate,
8723 dummy_obstack_deallocate);
8724}
8725
80626a55
DE
8726/* Lookup DWO file DWO_NAME. */
8727
8728static void **
0ac5b59e 8729lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8730{
8731 struct dwo_file find_entry;
8732 void **slot;
8733
8734 if (dwarf2_per_objfile->dwo_files == NULL)
8735 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8736
8737 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8738 find_entry.dwo_name = dwo_name;
8739 find_entry.comp_dir = comp_dir;
80626a55
DE
8740 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8741
8742 return slot;
8743}
8744
3019eac3
DE
8745static hashval_t
8746hash_dwo_unit (const void *item)
8747{
8748 const struct dwo_unit *dwo_unit = item;
8749
8750 /* This drops the top 32 bits of the id, but is ok for a hash. */
8751 return dwo_unit->signature;
8752}
8753
8754static int
8755eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8756{
8757 const struct dwo_unit *lhs = item_lhs;
8758 const struct dwo_unit *rhs = item_rhs;
8759
8760 /* The signature is assumed to be unique within the DWO file.
8761 So while object file CU dwo_id's always have the value zero,
8762 that's OK, assuming each object file DWO file has only one CU,
8763 and that's the rule for now. */
8764 return lhs->signature == rhs->signature;
8765}
8766
8767/* Allocate a hash table for DWO CUs,TUs.
8768 There is one of these tables for each of CUs,TUs for each DWO file. */
8769
8770static htab_t
8771allocate_dwo_unit_table (struct objfile *objfile)
8772{
8773 /* Start out with a pretty small number.
8774 Generally DWO files contain only one CU and maybe some TUs. */
8775 return htab_create_alloc_ex (3,
8776 hash_dwo_unit,
8777 eq_dwo_unit,
8778 NULL,
8779 &objfile->objfile_obstack,
8780 hashtab_obstack_allocate,
8781 dummy_obstack_deallocate);
8782}
8783
80626a55 8784/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8785
19c3d4c9 8786struct create_dwo_cu_data
3019eac3
DE
8787{
8788 struct dwo_file *dwo_file;
19c3d4c9 8789 struct dwo_unit dwo_unit;
3019eac3
DE
8790};
8791
19c3d4c9 8792/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8793
8794static void
19c3d4c9
DE
8795create_dwo_cu_reader (const struct die_reader_specs *reader,
8796 const gdb_byte *info_ptr,
8797 struct die_info *comp_unit_die,
8798 int has_children,
8799 void *datap)
3019eac3
DE
8800{
8801 struct dwarf2_cu *cu = reader->cu;
8802 struct objfile *objfile = dwarf2_per_objfile->objfile;
8803 sect_offset offset = cu->per_cu->offset;
8a0459fd 8804 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8805 struct create_dwo_cu_data *data = datap;
3019eac3 8806 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8807 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8808 struct attribute *attr;
3019eac3
DE
8809
8810 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8811 if (attr == NULL)
8812 {
19c3d4c9
DE
8813 complaint (&symfile_complaints,
8814 _("Dwarf Error: debug entry at offset 0x%x is missing"
8815 " its dwo_id [in module %s]"),
8816 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8817 return;
8818 }
8819
3019eac3
DE
8820 dwo_unit->dwo_file = dwo_file;
8821 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8822 dwo_unit->section = section;
3019eac3
DE
8823 dwo_unit->offset = offset;
8824 dwo_unit->length = cu->per_cu->length;
8825
09406207 8826 if (dwarf2_read_debug)
4031ecc5
DE
8827 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8828 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8829}
8830
19c3d4c9
DE
8831/* Create the dwo_unit for the lone CU in DWO_FILE.
8832 Note: This function processes DWO files only, not DWP files. */
3019eac3 8833
19c3d4c9
DE
8834static struct dwo_unit *
8835create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8836{
8837 struct objfile *objfile = dwarf2_per_objfile->objfile;
8838 struct dwarf2_section_info *section = &dwo_file->sections.info;
8839 bfd *abfd;
8840 htab_t cu_htab;
d521ce57 8841 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8842 struct create_dwo_cu_data create_dwo_cu_data;
8843 struct dwo_unit *dwo_unit;
3019eac3
DE
8844
8845 dwarf2_read_section (objfile, section);
8846 info_ptr = section->buffer;
8847
8848 if (info_ptr == NULL)
8849 return NULL;
8850
8851 /* We can't set abfd until now because the section may be empty or
8852 not present, in which case section->asection will be NULL. */
8853 abfd = section->asection->owner;
8854
09406207 8855 if (dwarf2_read_debug)
19c3d4c9
DE
8856 {
8857 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8858 bfd_section_name (abfd, section->asection),
8859 bfd_get_filename (abfd));
8860 }
3019eac3 8861
19c3d4c9
DE
8862 create_dwo_cu_data.dwo_file = dwo_file;
8863 dwo_unit = NULL;
3019eac3
DE
8864
8865 end_ptr = info_ptr + section->size;
8866 while (info_ptr < end_ptr)
8867 {
8868 struct dwarf2_per_cu_data per_cu;
8869
19c3d4c9
DE
8870 memset (&create_dwo_cu_data.dwo_unit, 0,
8871 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8872 memset (&per_cu, 0, sizeof (per_cu));
8873 per_cu.objfile = objfile;
8874 per_cu.is_debug_types = 0;
8875 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8876 per_cu.section = section;
3019eac3
DE
8877
8878 init_cutu_and_read_dies_no_follow (&per_cu,
8879 &dwo_file->sections.abbrev,
8880 dwo_file,
19c3d4c9
DE
8881 create_dwo_cu_reader,
8882 &create_dwo_cu_data);
8883
8884 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8885 {
8886 /* If we've already found one, complain. We only support one
8887 because having more than one requires hacking the dwo_name of
8888 each to match, which is highly unlikely to happen. */
8889 if (dwo_unit != NULL)
8890 {
8891 complaint (&symfile_complaints,
8892 _("Multiple CUs in DWO file %s [in module %s]"),
8893 dwo_file->dwo_name, objfile->name);
8894 break;
8895 }
8896
8897 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8898 *dwo_unit = create_dwo_cu_data.dwo_unit;
8899 }
3019eac3
DE
8900
8901 info_ptr += per_cu.length;
8902 }
8903
19c3d4c9 8904 return dwo_unit;
3019eac3
DE
8905}
8906
80626a55
DE
8907/* DWP file .debug_{cu,tu}_index section format:
8908 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8909
d2415c6c
DE
8910 DWP Version 1:
8911
80626a55
DE
8912 Both index sections have the same format, and serve to map a 64-bit
8913 signature to a set of section numbers. Each section begins with a header,
8914 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8915 indexes, and a pool of 32-bit section numbers. The index sections will be
8916 aligned at 8-byte boundaries in the file.
8917
d2415c6c
DE
8918 The index section header consists of:
8919
8920 V, 32 bit version number
8921 -, 32 bits unused
8922 N, 32 bit number of compilation units or type units in the index
8923 M, 32 bit number of slots in the hash table
80626a55 8924
d2415c6c 8925 Numbers are recorded using the byte order of the application binary.
80626a55 8926
d2415c6c 8927 We assume that N and M will not exceed 2^32 - 1.
80626a55 8928
d2415c6c 8929 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8930
d2415c6c
DE
8931 The hash table begins at offset 16 in the section, and consists of an array
8932 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8933 order of the application binary). Unused slots in the hash table are 0.
8934 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8935
d2415c6c
DE
8936 The parallel table begins immediately after the hash table
8937 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8938 array of 32-bit indexes (using the byte order of the application binary),
8939 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8940 table contains a 32-bit index into the pool of section numbers. For unused
8941 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8942
d2415c6c
DE
8943 Given a 64-bit compilation unit signature or a type signature S, an entry
8944 in the hash table is located as follows:
80626a55 8945
d2415c6c
DE
8946 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8947 the low-order k bits all set to 1.
80626a55 8948
d2415c6c 8949 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8950
d2415c6c
DE
8951 3) If the hash table entry at index H matches the signature, use that
8952 entry. If the hash table entry at index H is unused (all zeroes),
8953 terminate the search: the signature is not present in the table.
80626a55 8954
d2415c6c 8955 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8956
d2415c6c
DE
8957 Because M > N and H' and M are relatively prime, the search is guaranteed
8958 to stop at an unused slot or find the match.
80626a55 8959
d2415c6c
DE
8960 The pool of section numbers begins immediately following the hash table
8961 (at offset 16 + 12 * M from the beginning of the section). The pool of
8962 section numbers consists of an array of 32-bit words (using the byte order
8963 of the application binary). Each item in the array is indexed starting
8964 from 0. The hash table entry provides the index of the first section
8965 number in the set. Additional section numbers in the set follow, and the
8966 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 8967
d2415c6c
DE
8968 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8969 section must be the first entry in the set, and the .debug_abbrev.dwo must
8970 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
8971
8972/* Create a hash table to map DWO IDs to their CU/TU entry in
8973 .debug_{info,types}.dwo in DWP_FILE.
8974 Returns NULL if there isn't one.
8975 Note: This function processes DWP files only, not DWO files. */
8976
8977static struct dwp_hash_table *
8978create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8979{
8980 struct objfile *objfile = dwarf2_per_objfile->objfile;
8981 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8982 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8983 struct dwarf2_section_info *index;
8984 uint32_t version, nr_units, nr_slots;
8985 struct dwp_hash_table *htab;
8986
8987 if (is_debug_types)
8988 index = &dwp_file->sections.tu_index;
8989 else
8990 index = &dwp_file->sections.cu_index;
8991
8992 if (dwarf2_section_empty_p (index))
8993 return NULL;
8994 dwarf2_read_section (objfile, index);
8995
8996 index_ptr = index->buffer;
8997 index_end = index_ptr + index->size;
8998
8999 version = read_4_bytes (dbfd, index_ptr);
9000 index_ptr += 8; /* Skip the unused word. */
9001 nr_units = read_4_bytes (dbfd, index_ptr);
9002 index_ptr += 4;
9003 nr_slots = read_4_bytes (dbfd, index_ptr);
9004 index_ptr += 4;
9005
9006 if (version != 1)
9007 {
21aa081e 9008 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9009 " [in module %s]"),
21aa081e 9010 pulongest (version), dwp_file->name);
80626a55
DE
9011 }
9012 if (nr_slots != (nr_slots & -nr_slots))
9013 {
21aa081e 9014 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9015 " is not power of 2 [in module %s]"),
21aa081e 9016 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9017 }
9018
9019 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9020 htab->nr_units = nr_units;
9021 htab->nr_slots = nr_slots;
9022 htab->hash_table = index_ptr;
9023 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9024 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9025
9026 return htab;
9027}
9028
9029/* Update SECTIONS with the data from SECTP.
9030
9031 This function is like the other "locate" section routines that are
9032 passed to bfd_map_over_sections, but in this context the sections to
9033 read comes from the DWP hash table, not the full ELF section table.
9034
9035 The result is non-zero for success, or zero if an error was found. */
9036
9037static int
9038locate_virtual_dwo_sections (asection *sectp,
9039 struct virtual_dwo_sections *sections)
9040{
9041 const struct dwop_section_names *names = &dwop_section_names;
9042
9043 if (section_is_p (sectp->name, &names->abbrev_dwo))
9044 {
9045 /* There can be only one. */
9046 if (sections->abbrev.asection != NULL)
9047 return 0;
9048 sections->abbrev.asection = sectp;
9049 sections->abbrev.size = bfd_get_section_size (sectp);
9050 }
9051 else if (section_is_p (sectp->name, &names->info_dwo)
9052 || section_is_p (sectp->name, &names->types_dwo))
9053 {
9054 /* There can be only one. */
9055 if (sections->info_or_types.asection != NULL)
9056 return 0;
9057 sections->info_or_types.asection = sectp;
9058 sections->info_or_types.size = bfd_get_section_size (sectp);
9059 }
9060 else if (section_is_p (sectp->name, &names->line_dwo))
9061 {
9062 /* There can be only one. */
9063 if (sections->line.asection != NULL)
9064 return 0;
9065 sections->line.asection = sectp;
9066 sections->line.size = bfd_get_section_size (sectp);
9067 }
9068 else if (section_is_p (sectp->name, &names->loc_dwo))
9069 {
9070 /* There can be only one. */
9071 if (sections->loc.asection != NULL)
9072 return 0;
9073 sections->loc.asection = sectp;
9074 sections->loc.size = bfd_get_section_size (sectp);
9075 }
9076 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9077 {
9078 /* There can be only one. */
9079 if (sections->macinfo.asection != NULL)
9080 return 0;
9081 sections->macinfo.asection = sectp;
9082 sections->macinfo.size = bfd_get_section_size (sectp);
9083 }
9084 else if (section_is_p (sectp->name, &names->macro_dwo))
9085 {
9086 /* There can be only one. */
9087 if (sections->macro.asection != NULL)
9088 return 0;
9089 sections->macro.asection = sectp;
9090 sections->macro.size = bfd_get_section_size (sectp);
9091 }
9092 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9093 {
9094 /* There can be only one. */
9095 if (sections->str_offsets.asection != NULL)
9096 return 0;
9097 sections->str_offsets.asection = sectp;
9098 sections->str_offsets.size = bfd_get_section_size (sectp);
9099 }
9100 else
9101 {
9102 /* No other kind of section is valid. */
9103 return 0;
9104 }
9105
9106 return 1;
9107}
9108
9109/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9110 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9111 SECTION_INDEX is the index of the DWO in HTAB.
9112 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9113
9114static struct dwo_unit *
9115create_dwo_in_dwp (struct dwp_file *dwp_file,
9116 const struct dwp_hash_table *htab,
9117 uint32_t section_index,
0ac5b59e 9118 const char *comp_dir,
80626a55
DE
9119 ULONGEST signature, int is_debug_types)
9120{
9121 struct objfile *objfile = dwarf2_per_objfile->objfile;
9122 bfd *dbfd = dwp_file->dbfd;
9123 const char *kind = is_debug_types ? "TU" : "CU";
9124 struct dwo_file *dwo_file;
9125 struct dwo_unit *dwo_unit;
9126 struct virtual_dwo_sections sections;
9127 void **dwo_file_slot;
9128 char *virtual_dwo_name;
9129 struct dwarf2_section_info *cutu;
9130 struct cleanup *cleanups;
9131 int i;
9132
9133 if (dwarf2_read_debug)
9134 {
21aa081e 9135 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9136 kind,
21aa081e 9137 pulongest (section_index), hex_string (signature),
80626a55
DE
9138 dwp_file->name);
9139 }
9140
9141 /* Fetch the sections of this DWO.
9142 Put a limit on the number of sections we look for so that bad data
9143 doesn't cause us to loop forever. */
9144
9145#define MAX_NR_DWO_SECTIONS \
9146 (1 /* .debug_info or .debug_types */ \
9147 + 1 /* .debug_abbrev */ \
9148 + 1 /* .debug_line */ \
9149 + 1 /* .debug_loc */ \
9150 + 1 /* .debug_str_offsets */ \
9151 + 1 /* .debug_macro */ \
9152 + 1 /* .debug_macinfo */ \
9153 + 1 /* trailing zero */)
9154
9155 memset (&sections, 0, sizeof (sections));
9156 cleanups = make_cleanup (null_cleanup, 0);
9157
9158 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9159 {
9160 asection *sectp;
9161 uint32_t section_nr =
9162 read_4_bytes (dbfd,
9163 htab->section_pool
9164 + (section_index + i) * sizeof (uint32_t));
9165
9166 if (section_nr == 0)
9167 break;
9168 if (section_nr >= dwp_file->num_sections)
9169 {
9170 error (_("Dwarf Error: bad DWP hash table, section number too large"
9171 " [in module %s]"),
9172 dwp_file->name);
9173 }
9174
9175 sectp = dwp_file->elf_sections[section_nr];
9176 if (! locate_virtual_dwo_sections (sectp, &sections))
9177 {
9178 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9179 " [in module %s]"),
9180 dwp_file->name);
9181 }
9182 }
9183
9184 if (i < 2
9185 || sections.info_or_types.asection == NULL
9186 || sections.abbrev.asection == NULL)
9187 {
9188 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9189 " [in module %s]"),
9190 dwp_file->name);
9191 }
9192 if (i == MAX_NR_DWO_SECTIONS)
9193 {
9194 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9195 " [in module %s]"),
9196 dwp_file->name);
9197 }
9198
9199 /* It's easier for the rest of the code if we fake a struct dwo_file and
9200 have dwo_unit "live" in that. At least for now.
9201
9202 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9203 However, for each CU + set of TUs that came from the same original DWO
9204 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9205 (fewer struct dwo_file objects to allocated). Remember that for really
9206 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9207
2792b94d
PM
9208 virtual_dwo_name =
9209 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9210 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9211 sections.line.asection ? sections.line.asection->id : 0,
9212 sections.loc.asection ? sections.loc.asection->id : 0,
9213 (sections.str_offsets.asection
9214 ? sections.str_offsets.asection->id
9215 : 0));
80626a55
DE
9216 make_cleanup (xfree, virtual_dwo_name);
9217 /* Can we use an existing virtual DWO file? */
0ac5b59e 9218 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9219 /* Create one if necessary. */
9220 if (*dwo_file_slot == NULL)
9221 {
9222 if (dwarf2_read_debug)
9223 {
9224 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9225 virtual_dwo_name);
9226 }
9227 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9228 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9229 virtual_dwo_name,
9230 strlen (virtual_dwo_name));
9231 dwo_file->comp_dir = comp_dir;
80626a55
DE
9232 dwo_file->sections.abbrev = sections.abbrev;
9233 dwo_file->sections.line = sections.line;
9234 dwo_file->sections.loc = sections.loc;
9235 dwo_file->sections.macinfo = sections.macinfo;
9236 dwo_file->sections.macro = sections.macro;
9237 dwo_file->sections.str_offsets = sections.str_offsets;
9238 /* The "str" section is global to the entire DWP file. */
9239 dwo_file->sections.str = dwp_file->sections.str;
9240 /* The info or types section is assigned later to dwo_unit,
9241 there's no need to record it in dwo_file.
9242 Also, we can't simply record type sections in dwo_file because
9243 we record a pointer into the vector in dwo_unit. As we collect more
9244 types we'll grow the vector and eventually have to reallocate space
9245 for it, invalidating all the pointers into the current copy. */
9246 *dwo_file_slot = dwo_file;
9247 }
9248 else
9249 {
9250 if (dwarf2_read_debug)
9251 {
9252 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9253 virtual_dwo_name);
9254 }
9255 dwo_file = *dwo_file_slot;
9256 }
9257 do_cleanups (cleanups);
9258
9259 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9260 dwo_unit->dwo_file = dwo_file;
9261 dwo_unit->signature = signature;
8a0459fd
DE
9262 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9263 sizeof (struct dwarf2_section_info));
9264 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9265 /* offset, length, type_offset_in_tu are set later. */
9266
9267 return dwo_unit;
9268}
9269
9270/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9271
9272static struct dwo_unit *
9273lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9274 const struct dwp_hash_table *htab,
0ac5b59e 9275 const char *comp_dir,
80626a55
DE
9276 ULONGEST signature, int is_debug_types)
9277{
9278 bfd *dbfd = dwp_file->dbfd;
9279 uint32_t mask = htab->nr_slots - 1;
9280 uint32_t hash = signature & mask;
9281 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9282 unsigned int i;
9283 void **slot;
9284 struct dwo_unit find_dwo_cu, *dwo_cu;
9285
9286 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9287 find_dwo_cu.signature = signature;
9288 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9289
9290 if (*slot != NULL)
9291 return *slot;
9292
9293 /* Use a for loop so that we don't loop forever on bad debug info. */
9294 for (i = 0; i < htab->nr_slots; ++i)
9295 {
9296 ULONGEST signature_in_table;
9297
9298 signature_in_table =
9299 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9300 if (signature_in_table == signature)
9301 {
9302 uint32_t section_index =
9303 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9304
9305 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9306 comp_dir, signature, is_debug_types);
80626a55
DE
9307 return *slot;
9308 }
9309 if (signature_in_table == 0)
9310 return NULL;
9311 hash = (hash + hash2) & mask;
9312 }
9313
9314 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9315 " [in module %s]"),
9316 dwp_file->name);
9317}
9318
ab5088bf 9319/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9320 Open the file specified by FILE_NAME and hand it off to BFD for
9321 preliminary analysis. Return a newly initialized bfd *, which
9322 includes a canonicalized copy of FILE_NAME.
80626a55 9323 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9324 SEARCH_CWD is true if the current directory is to be searched.
9325 It will be searched before debug-file-directory.
9326 If unable to find/open the file, return NULL.
3019eac3
DE
9327 NOTE: This function is derived from symfile_bfd_open. */
9328
9329static bfd *
6ac97d4c 9330try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9331{
9332 bfd *sym_bfd;
80626a55 9333 int desc, flags;
3019eac3 9334 char *absolute_name;
9c02c129
DE
9335 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9336 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9337 to debug_file_directory. */
9338 char *search_path;
9339 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9340
6ac97d4c
DE
9341 if (search_cwd)
9342 {
9343 if (*debug_file_directory != '\0')
9344 search_path = concat (".", dirname_separator_string,
9345 debug_file_directory, NULL);
9346 else
9347 search_path = xstrdup (".");
9348 }
9c02c129 9349 else
6ac97d4c 9350 search_path = xstrdup (debug_file_directory);
3019eac3 9351
9c02c129 9352 flags = 0;
80626a55
DE
9353 if (is_dwp)
9354 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9355 desc = openp (search_path, flags, file_name,
3019eac3 9356 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9357 xfree (search_path);
3019eac3
DE
9358 if (desc < 0)
9359 return NULL;
9360
bb397797 9361 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9362 xfree (absolute_name);
9c02c129
DE
9363 if (sym_bfd == NULL)
9364 return NULL;
3019eac3
DE
9365 bfd_set_cacheable (sym_bfd, 1);
9366
9367 if (!bfd_check_format (sym_bfd, bfd_object))
9368 {
cbb099e8 9369 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9370 return NULL;
9371 }
9372
3019eac3
DE
9373 return sym_bfd;
9374}
9375
ab5088bf 9376/* Try to open DWO file FILE_NAME.
3019eac3
DE
9377 COMP_DIR is the DW_AT_comp_dir attribute.
9378 The result is the bfd handle of the file.
9379 If there is a problem finding or opening the file, return NULL.
9380 Upon success, the canonicalized path of the file is stored in the bfd,
9381 same as symfile_bfd_open. */
9382
9383static bfd *
ab5088bf 9384open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9385{
9386 bfd *abfd;
3019eac3 9387
80626a55 9388 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9389 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9390
9391 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9392
9393 if (comp_dir != NULL)
9394 {
80626a55 9395 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9396
9397 /* NOTE: If comp_dir is a relative path, this will also try the
9398 search path, which seems useful. */
6ac97d4c 9399 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9400 xfree (path_to_try);
9401 if (abfd != NULL)
9402 return abfd;
9403 }
9404
9405 /* That didn't work, try debug-file-directory, which, despite its name,
9406 is a list of paths. */
9407
9408 if (*debug_file_directory == '\0')
9409 return NULL;
9410
6ac97d4c 9411 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9412}
9413
80626a55
DE
9414/* This function is mapped across the sections and remembers the offset and
9415 size of each of the DWO debugging sections we are interested in. */
9416
9417static void
9418dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9419{
9420 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9421 const struct dwop_section_names *names = &dwop_section_names;
9422
9423 if (section_is_p (sectp->name, &names->abbrev_dwo))
9424 {
9425 dwo_sections->abbrev.asection = sectp;
9426 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9427 }
9428 else if (section_is_p (sectp->name, &names->info_dwo))
9429 {
9430 dwo_sections->info.asection = sectp;
9431 dwo_sections->info.size = bfd_get_section_size (sectp);
9432 }
9433 else if (section_is_p (sectp->name, &names->line_dwo))
9434 {
9435 dwo_sections->line.asection = sectp;
9436 dwo_sections->line.size = bfd_get_section_size (sectp);
9437 }
9438 else if (section_is_p (sectp->name, &names->loc_dwo))
9439 {
9440 dwo_sections->loc.asection = sectp;
9441 dwo_sections->loc.size = bfd_get_section_size (sectp);
9442 }
9443 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9444 {
9445 dwo_sections->macinfo.asection = sectp;
9446 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9447 }
9448 else if (section_is_p (sectp->name, &names->macro_dwo))
9449 {
9450 dwo_sections->macro.asection = sectp;
9451 dwo_sections->macro.size = bfd_get_section_size (sectp);
9452 }
9453 else if (section_is_p (sectp->name, &names->str_dwo))
9454 {
9455 dwo_sections->str.asection = sectp;
9456 dwo_sections->str.size = bfd_get_section_size (sectp);
9457 }
9458 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9459 {
9460 dwo_sections->str_offsets.asection = sectp;
9461 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9462 }
9463 else if (section_is_p (sectp->name, &names->types_dwo))
9464 {
9465 struct dwarf2_section_info type_section;
9466
9467 memset (&type_section, 0, sizeof (type_section));
9468 type_section.asection = sectp;
9469 type_section.size = bfd_get_section_size (sectp);
9470 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9471 &type_section);
9472 }
9473}
9474
ab5088bf 9475/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9476 by PER_CU. This is for the non-DWP case.
80626a55 9477 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9478
9479static struct dwo_file *
0ac5b59e
DE
9480open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9481 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9482{
9483 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9484 struct dwo_file *dwo_file;
9485 bfd *dbfd;
3019eac3
DE
9486 struct cleanup *cleanups;
9487
ab5088bf 9488 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9489 if (dbfd == NULL)
9490 {
9491 if (dwarf2_read_debug)
9492 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9493 return NULL;
9494 }
9495 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9496 dwo_file->dwo_name = dwo_name;
9497 dwo_file->comp_dir = comp_dir;
80626a55 9498 dwo_file->dbfd = dbfd;
3019eac3
DE
9499
9500 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9501
80626a55 9502 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9503
19c3d4c9 9504 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9505
9506 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9507 dwo_file->sections.types);
9508
9509 discard_cleanups (cleanups);
9510
80626a55
DE
9511 if (dwarf2_read_debug)
9512 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9513
3019eac3
DE
9514 return dwo_file;
9515}
9516
80626a55
DE
9517/* This function is mapped across the sections and remembers the offset and
9518 size of each of the DWP debugging sections we are interested in. */
3019eac3 9519
80626a55
DE
9520static void
9521dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9522{
80626a55
DE
9523 struct dwp_file *dwp_file = dwp_file_ptr;
9524 const struct dwop_section_names *names = &dwop_section_names;
9525 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9526
80626a55
DE
9527 /* Record the ELF section number for later lookup: this is what the
9528 .debug_cu_index,.debug_tu_index tables use. */
9529 gdb_assert (elf_section_nr < dwp_file->num_sections);
9530 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9531
80626a55
DE
9532 /* Look for specific sections that we need. */
9533 if (section_is_p (sectp->name, &names->str_dwo))
9534 {
9535 dwp_file->sections.str.asection = sectp;
9536 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9537 }
9538 else if (section_is_p (sectp->name, &names->cu_index))
9539 {
9540 dwp_file->sections.cu_index.asection = sectp;
9541 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9542 }
9543 else if (section_is_p (sectp->name, &names->tu_index))
9544 {
9545 dwp_file->sections.tu_index.asection = sectp;
9546 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9547 }
9548}
3019eac3 9549
80626a55 9550/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9551
80626a55
DE
9552static hashval_t
9553hash_dwp_loaded_cutus (const void *item)
9554{
9555 const struct dwo_unit *dwo_unit = item;
3019eac3 9556
80626a55
DE
9557 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9558 return dwo_unit->signature;
3019eac3
DE
9559}
9560
80626a55 9561/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9562
80626a55
DE
9563static int
9564eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9565{
80626a55
DE
9566 const struct dwo_unit *dua = a;
9567 const struct dwo_unit *dub = b;
3019eac3 9568
80626a55
DE
9569 return dua->signature == dub->signature;
9570}
3019eac3 9571
80626a55 9572/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9573
80626a55
DE
9574static htab_t
9575allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9576{
9577 return htab_create_alloc_ex (3,
9578 hash_dwp_loaded_cutus,
9579 eq_dwp_loaded_cutus,
9580 NULL,
9581 &objfile->objfile_obstack,
9582 hashtab_obstack_allocate,
9583 dummy_obstack_deallocate);
9584}
3019eac3 9585
ab5088bf
DE
9586/* Try to open DWP file FILE_NAME.
9587 The result is the bfd handle of the file.
9588 If there is a problem finding or opening the file, return NULL.
9589 Upon success, the canonicalized path of the file is stored in the bfd,
9590 same as symfile_bfd_open. */
9591
9592static bfd *
9593open_dwp_file (const char *file_name)
9594{
6ac97d4c
DE
9595 bfd *abfd;
9596
9597 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9598 if (abfd != NULL)
9599 return abfd;
9600
9601 /* Work around upstream bug 15652.
9602 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9603 [Whether that's a "bug" is debatable, but it is getting in our way.]
9604 We have no real idea where the dwp file is, because gdb's realpath-ing
9605 of the executable's path may have discarded the needed info.
9606 [IWBN if the dwp file name was recorded in the executable, akin to
9607 .gnu_debuglink, but that doesn't exist yet.]
9608 Strip the directory from FILE_NAME and search again. */
9609 if (*debug_file_directory != '\0')
9610 {
9611 /* Don't implicitly search the current directory here.
9612 If the user wants to search "." to handle this case,
9613 it must be added to debug-file-directory. */
9614 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9615 0 /*search_cwd*/);
9616 }
9617
9618 return NULL;
ab5088bf
DE
9619}
9620
80626a55
DE
9621/* Initialize the use of the DWP file for the current objfile.
9622 By convention the name of the DWP file is ${objfile}.dwp.
9623 The result is NULL if it can't be found. */
a766d390 9624
80626a55 9625static struct dwp_file *
ab5088bf 9626open_and_init_dwp_file (void)
80626a55
DE
9627{
9628 struct objfile *objfile = dwarf2_per_objfile->objfile;
9629 struct dwp_file *dwp_file;
9630 char *dwp_name;
9631 bfd *dbfd;
9632 struct cleanup *cleanups;
9633
2792b94d 9634 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9635 cleanups = make_cleanup (xfree, dwp_name);
9636
ab5088bf 9637 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9638 if (dbfd == NULL)
9639 {
9640 if (dwarf2_read_debug)
9641 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9642 do_cleanups (cleanups);
9643 return NULL;
3019eac3 9644 }
80626a55 9645 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9646 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9647 dwp_file->dbfd = dbfd;
9648 do_cleanups (cleanups);
c906108c 9649
80626a55
DE
9650 /* +1: section 0 is unused */
9651 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9652 dwp_file->elf_sections =
9653 OBSTACK_CALLOC (&objfile->objfile_obstack,
9654 dwp_file->num_sections, asection *);
9655
9656 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9657
9658 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9659
9660 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9661
9662 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9663
80626a55
DE
9664 if (dwarf2_read_debug)
9665 {
9666 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9667 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9668 " %s CUs, %s TUs\n",
9669 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9670 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9671 }
9672
9673 return dwp_file;
3019eac3 9674}
c906108c 9675
ab5088bf
DE
9676/* Wrapper around open_and_init_dwp_file, only open it once. */
9677
9678static struct dwp_file *
9679get_dwp_file (void)
9680{
9681 if (! dwarf2_per_objfile->dwp_checked)
9682 {
9683 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9684 dwarf2_per_objfile->dwp_checked = 1;
9685 }
9686 return dwarf2_per_objfile->dwp_file;
9687}
9688
80626a55
DE
9689/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9690 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9691 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9692 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9693 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9694
9695 This is called, for example, when wanting to read a variable with a
9696 complex location. Therefore we don't want to do file i/o for every call.
9697 Therefore we don't want to look for a DWO file on every call.
9698 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9699 then we check if we've already seen DWO_NAME, and only THEN do we check
9700 for a DWO file.
9701
1c658ad5 9702 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9703 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9704
3019eac3 9705static struct dwo_unit *
80626a55
DE
9706lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9707 const char *dwo_name, const char *comp_dir,
9708 ULONGEST signature, int is_debug_types)
3019eac3
DE
9709{
9710 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9711 const char *kind = is_debug_types ? "TU" : "CU";
9712 void **dwo_file_slot;
3019eac3 9713 struct dwo_file *dwo_file;
80626a55 9714 struct dwp_file *dwp_file;
cb1df416 9715
6a506a2d
DE
9716 /* First see if there's a DWP file.
9717 If we have a DWP file but didn't find the DWO inside it, don't
9718 look for the original DWO file. It makes gdb behave differently
9719 depending on whether one is debugging in the build tree. */
cf2c3c16 9720
ab5088bf 9721 dwp_file = get_dwp_file ();
80626a55 9722 if (dwp_file != NULL)
cf2c3c16 9723 {
80626a55
DE
9724 const struct dwp_hash_table *dwp_htab =
9725 is_debug_types ? dwp_file->tus : dwp_file->cus;
9726
9727 if (dwp_htab != NULL)
9728 {
9729 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9730 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9731 signature, is_debug_types);
80626a55
DE
9732
9733 if (dwo_cutu != NULL)
9734 {
9735 if (dwarf2_read_debug)
9736 {
9737 fprintf_unfiltered (gdb_stdlog,
9738 "Virtual DWO %s %s found: @%s\n",
9739 kind, hex_string (signature),
9740 host_address_to_string (dwo_cutu));
9741 }
9742 return dwo_cutu;
9743 }
9744 }
9745 }
6a506a2d 9746 else
80626a55 9747 {
6a506a2d 9748 /* No DWP file, look for the DWO file. */
80626a55 9749
6a506a2d
DE
9750 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9751 if (*dwo_file_slot == NULL)
80626a55 9752 {
6a506a2d
DE
9753 /* Read in the file and build a table of the CUs/TUs it contains. */
9754 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9755 }
6a506a2d
DE
9756 /* NOTE: This will be NULL if unable to open the file. */
9757 dwo_file = *dwo_file_slot;
3019eac3 9758
6a506a2d 9759 if (dwo_file != NULL)
19c3d4c9 9760 {
6a506a2d
DE
9761 struct dwo_unit *dwo_cutu = NULL;
9762
9763 if (is_debug_types && dwo_file->tus)
9764 {
9765 struct dwo_unit find_dwo_cutu;
9766
9767 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9768 find_dwo_cutu.signature = signature;
9769 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9770 }
9771 else if (!is_debug_types && dwo_file->cu)
80626a55 9772 {
6a506a2d
DE
9773 if (signature == dwo_file->cu->signature)
9774 dwo_cutu = dwo_file->cu;
9775 }
9776
9777 if (dwo_cutu != NULL)
9778 {
9779 if (dwarf2_read_debug)
9780 {
9781 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9782 kind, dwo_name, hex_string (signature),
9783 host_address_to_string (dwo_cutu));
9784 }
9785 return dwo_cutu;
80626a55
DE
9786 }
9787 }
2e276125 9788 }
9cdd5dbd 9789
80626a55
DE
9790 /* We didn't find it. This could mean a dwo_id mismatch, or
9791 someone deleted the DWO/DWP file, or the search path isn't set up
9792 correctly to find the file. */
9793
9794 if (dwarf2_read_debug)
9795 {
9796 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9797 kind, dwo_name, hex_string (signature));
9798 }
3019eac3 9799
6656a72d
DE
9800 /* This is a warning and not a complaint because it can be caused by
9801 pilot error (e.g., user accidentally deleting the DWO). */
9802 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9803 " [in module %s]"),
9804 kind, dwo_name, hex_string (signature),
9805 this_unit->is_debug_types ? "TU" : "CU",
9806 this_unit->offset.sect_off, objfile->name);
3019eac3 9807 return NULL;
5fb290d7
DJ
9808}
9809
80626a55
DE
9810/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9811 See lookup_dwo_cutu_unit for details. */
9812
9813static struct dwo_unit *
9814lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9815 const char *dwo_name, const char *comp_dir,
9816 ULONGEST signature)
9817{
9818 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9819}
9820
9821/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9822 See lookup_dwo_cutu_unit for details. */
9823
9824static struct dwo_unit *
9825lookup_dwo_type_unit (struct signatured_type *this_tu,
9826 const char *dwo_name, const char *comp_dir)
9827{
9828 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9829}
9830
3019eac3
DE
9831/* Free all resources associated with DWO_FILE.
9832 Close the DWO file and munmap the sections.
9833 All memory should be on the objfile obstack. */
348e048f
DE
9834
9835static void
3019eac3 9836free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9837{
3019eac3
DE
9838 int ix;
9839 struct dwarf2_section_info *section;
348e048f 9840
5c6fa7ab 9841 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9842 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9843
3019eac3
DE
9844 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9845}
348e048f 9846
3019eac3 9847/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9848
3019eac3
DE
9849static void
9850free_dwo_file_cleanup (void *arg)
9851{
9852 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9853 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9854
3019eac3
DE
9855 free_dwo_file (dwo_file, objfile);
9856}
348e048f 9857
3019eac3 9858/* Traversal function for free_dwo_files. */
2ab95328 9859
3019eac3
DE
9860static int
9861free_dwo_file_from_slot (void **slot, void *info)
9862{
9863 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9864 struct objfile *objfile = (struct objfile *) info;
348e048f 9865
3019eac3 9866 free_dwo_file (dwo_file, objfile);
348e048f 9867
3019eac3
DE
9868 return 1;
9869}
348e048f 9870
3019eac3 9871/* Free all resources associated with DWO_FILES. */
348e048f 9872
3019eac3
DE
9873static void
9874free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9875{
9876 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9877}
3019eac3
DE
9878\f
9879/* Read in various DIEs. */
348e048f 9880
d389af10
JK
9881/* qsort helper for inherit_abstract_dies. */
9882
9883static int
9884unsigned_int_compar (const void *ap, const void *bp)
9885{
9886 unsigned int a = *(unsigned int *) ap;
9887 unsigned int b = *(unsigned int *) bp;
9888
9889 return (a > b) - (b > a);
9890}
9891
9892/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9893 Inherit only the children of the DW_AT_abstract_origin DIE not being
9894 already referenced by DW_AT_abstract_origin from the children of the
9895 current DIE. */
d389af10
JK
9896
9897static void
9898inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9899{
9900 struct die_info *child_die;
9901 unsigned die_children_count;
9902 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9903 sect_offset *offsets;
9904 sect_offset *offsets_end, *offsetp;
d389af10
JK
9905 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9906 struct die_info *origin_die;
9907 /* Iterator of the ORIGIN_DIE children. */
9908 struct die_info *origin_child_die;
9909 struct cleanup *cleanups;
9910 struct attribute *attr;
cd02d79d
PA
9911 struct dwarf2_cu *origin_cu;
9912 struct pending **origin_previous_list_in_scope;
d389af10
JK
9913
9914 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9915 if (!attr)
9916 return;
9917
cd02d79d
PA
9918 /* Note that following die references may follow to a die in a
9919 different cu. */
9920
9921 origin_cu = cu;
9922 origin_die = follow_die_ref (die, attr, &origin_cu);
9923
9924 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9925 symbols in. */
9926 origin_previous_list_in_scope = origin_cu->list_in_scope;
9927 origin_cu->list_in_scope = cu->list_in_scope;
9928
edb3359d
DJ
9929 if (die->tag != origin_die->tag
9930 && !(die->tag == DW_TAG_inlined_subroutine
9931 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9932 complaint (&symfile_complaints,
9933 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9934 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9935
9936 child_die = die->child;
9937 die_children_count = 0;
9938 while (child_die && child_die->tag)
9939 {
9940 child_die = sibling_die (child_die);
9941 die_children_count++;
9942 }
9943 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9944 cleanups = make_cleanup (xfree, offsets);
9945
9946 offsets_end = offsets;
9947 child_die = die->child;
9948 while (child_die && child_die->tag)
9949 {
c38f313d
DJ
9950 /* For each CHILD_DIE, find the corresponding child of
9951 ORIGIN_DIE. If there is more than one layer of
9952 DW_AT_abstract_origin, follow them all; there shouldn't be,
9953 but GCC versions at least through 4.4 generate this (GCC PR
9954 40573). */
9955 struct die_info *child_origin_die = child_die;
cd02d79d 9956 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9957
c38f313d
DJ
9958 while (1)
9959 {
cd02d79d
PA
9960 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9961 child_origin_cu);
c38f313d
DJ
9962 if (attr == NULL)
9963 break;
cd02d79d
PA
9964 child_origin_die = follow_die_ref (child_origin_die, attr,
9965 &child_origin_cu);
c38f313d
DJ
9966 }
9967
d389af10
JK
9968 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9969 counterpart may exist. */
c38f313d 9970 if (child_origin_die != child_die)
d389af10 9971 {
edb3359d
DJ
9972 if (child_die->tag != child_origin_die->tag
9973 && !(child_die->tag == DW_TAG_inlined_subroutine
9974 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9975 complaint (&symfile_complaints,
9976 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9977 "different tags"), child_die->offset.sect_off,
9978 child_origin_die->offset.sect_off);
c38f313d
DJ
9979 if (child_origin_die->parent != origin_die)
9980 complaint (&symfile_complaints,
9981 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9982 "different parents"), child_die->offset.sect_off,
9983 child_origin_die->offset.sect_off);
c38f313d
DJ
9984 else
9985 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9986 }
9987 child_die = sibling_die (child_die);
9988 }
9989 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9990 unsigned_int_compar);
9991 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 9992 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
9993 complaint (&symfile_complaints,
9994 _("Multiple children of DIE 0x%x refer "
9995 "to DIE 0x%x as their abstract origin"),
b64f50a1 9996 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
9997
9998 offsetp = offsets;
9999 origin_child_die = origin_die->child;
10000 while (origin_child_die && origin_child_die->tag)
10001 {
10002 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
10003 while (offsetp < offsets_end
10004 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 10005 offsetp++;
b64f50a1
JK
10006 if (offsetp >= offsets_end
10007 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
10008 {
10009 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 10010 process_die (origin_child_die, origin_cu);
d389af10
JK
10011 }
10012 origin_child_die = sibling_die (origin_child_die);
10013 }
cd02d79d 10014 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10015
10016 do_cleanups (cleanups);
10017}
10018
c906108c 10019static void
e7c27a73 10020read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10021{
e7c27a73 10022 struct objfile *objfile = cu->objfile;
52f0bd74 10023 struct context_stack *new;
c906108c
SS
10024 CORE_ADDR lowpc;
10025 CORE_ADDR highpc;
10026 struct die_info *child_die;
edb3359d 10027 struct attribute *attr, *call_line, *call_file;
15d034d0 10028 const char *name;
e142c38c 10029 CORE_ADDR baseaddr;
801e3a5b 10030 struct block *block;
edb3359d 10031 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10032 VEC (symbolp) *template_args = NULL;
10033 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10034
10035 if (inlined_func)
10036 {
10037 /* If we do not have call site information, we can't show the
10038 caller of this inlined function. That's too confusing, so
10039 only use the scope for local variables. */
10040 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10041 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10042 if (call_line == NULL || call_file == NULL)
10043 {
10044 read_lexical_block_scope (die, cu);
10045 return;
10046 }
10047 }
c906108c 10048
e142c38c
DJ
10049 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10050
94af9270 10051 name = dwarf2_name (die, cu);
c906108c 10052
e8d05480
JB
10053 /* Ignore functions with missing or empty names. These are actually
10054 illegal according to the DWARF standard. */
10055 if (name == NULL)
10056 {
10057 complaint (&symfile_complaints,
b64f50a1
JK
10058 _("missing name for subprogram DIE at %d"),
10059 die->offset.sect_off);
e8d05480
JB
10060 return;
10061 }
10062
10063 /* Ignore functions with missing or invalid low and high pc attributes. */
10064 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10065 {
ae4d0c03
PM
10066 attr = dwarf2_attr (die, DW_AT_external, cu);
10067 if (!attr || !DW_UNSND (attr))
10068 complaint (&symfile_complaints,
3e43a32a
MS
10069 _("cannot get low and high bounds "
10070 "for subprogram DIE at %d"),
b64f50a1 10071 die->offset.sect_off);
e8d05480
JB
10072 return;
10073 }
c906108c
SS
10074
10075 lowpc += baseaddr;
10076 highpc += baseaddr;
10077
34eaf542
TT
10078 /* If we have any template arguments, then we must allocate a
10079 different sort of symbol. */
10080 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10081 {
10082 if (child_die->tag == DW_TAG_template_type_param
10083 || child_die->tag == DW_TAG_template_value_param)
10084 {
e623cf5d 10085 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10086 templ_func->base.is_cplus_template_function = 1;
10087 break;
10088 }
10089 }
10090
c906108c 10091 new = push_context (0, lowpc);
34eaf542
TT
10092 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10093 (struct symbol *) templ_func);
4c2df51b 10094
4cecd739
DJ
10095 /* If there is a location expression for DW_AT_frame_base, record
10096 it. */
e142c38c 10097 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10098 if (attr)
f1e6e072 10099 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10100
e142c38c 10101 cu->list_in_scope = &local_symbols;
c906108c 10102
639d11d3 10103 if (die->child != NULL)
c906108c 10104 {
639d11d3 10105 child_die = die->child;
c906108c
SS
10106 while (child_die && child_die->tag)
10107 {
34eaf542
TT
10108 if (child_die->tag == DW_TAG_template_type_param
10109 || child_die->tag == DW_TAG_template_value_param)
10110 {
10111 struct symbol *arg = new_symbol (child_die, NULL, cu);
10112
f1078f66
DJ
10113 if (arg != NULL)
10114 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10115 }
10116 else
10117 process_die (child_die, cu);
c906108c
SS
10118 child_die = sibling_die (child_die);
10119 }
10120 }
10121
d389af10
JK
10122 inherit_abstract_dies (die, cu);
10123
4a811a97
UW
10124 /* If we have a DW_AT_specification, we might need to import using
10125 directives from the context of the specification DIE. See the
10126 comment in determine_prefix. */
10127 if (cu->language == language_cplus
10128 && dwarf2_attr (die, DW_AT_specification, cu))
10129 {
10130 struct dwarf2_cu *spec_cu = cu;
10131 struct die_info *spec_die = die_specification (die, &spec_cu);
10132
10133 while (spec_die)
10134 {
10135 child_die = spec_die->child;
10136 while (child_die && child_die->tag)
10137 {
10138 if (child_die->tag == DW_TAG_imported_module)
10139 process_die (child_die, spec_cu);
10140 child_die = sibling_die (child_die);
10141 }
10142
10143 /* In some cases, GCC generates specification DIEs that
10144 themselves contain DW_AT_specification attributes. */
10145 spec_die = die_specification (spec_die, &spec_cu);
10146 }
10147 }
10148
c906108c
SS
10149 new = pop_context ();
10150 /* Make a block for the local symbols within. */
801e3a5b
JB
10151 block = finish_block (new->name, &local_symbols, new->old_blocks,
10152 lowpc, highpc, objfile);
10153
df8a16a1 10154 /* For C++, set the block's scope. */
195a3f6c 10155 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10156 && cu->processing_has_namespace_info)
195a3f6c
TT
10157 block_set_scope (block, determine_prefix (die, cu),
10158 &objfile->objfile_obstack);
df8a16a1 10159
801e3a5b
JB
10160 /* If we have address ranges, record them. */
10161 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10162
34eaf542
TT
10163 /* Attach template arguments to function. */
10164 if (! VEC_empty (symbolp, template_args))
10165 {
10166 gdb_assert (templ_func != NULL);
10167
10168 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10169 templ_func->template_arguments
10170 = obstack_alloc (&objfile->objfile_obstack,
10171 (templ_func->n_template_arguments
10172 * sizeof (struct symbol *)));
10173 memcpy (templ_func->template_arguments,
10174 VEC_address (symbolp, template_args),
10175 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10176 VEC_free (symbolp, template_args);
10177 }
10178
208d8187
JB
10179 /* In C++, we can have functions nested inside functions (e.g., when
10180 a function declares a class that has methods). This means that
10181 when we finish processing a function scope, we may need to go
10182 back to building a containing block's symbol lists. */
10183 local_symbols = new->locals;
27aa8d6a 10184 using_directives = new->using_directives;
208d8187 10185
921e78cf
JB
10186 /* If we've finished processing a top-level function, subsequent
10187 symbols go in the file symbol list. */
10188 if (outermost_context_p ())
e142c38c 10189 cu->list_in_scope = &file_symbols;
c906108c
SS
10190}
10191
10192/* Process all the DIES contained within a lexical block scope. Start
10193 a new scope, process the dies, and then close the scope. */
10194
10195static void
e7c27a73 10196read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10197{
e7c27a73 10198 struct objfile *objfile = cu->objfile;
52f0bd74 10199 struct context_stack *new;
c906108c
SS
10200 CORE_ADDR lowpc, highpc;
10201 struct die_info *child_die;
e142c38c
DJ
10202 CORE_ADDR baseaddr;
10203
10204 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10205
10206 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10207 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10208 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10209 be nasty. Might be easier to properly extend generic blocks to
af34e669 10210 describe ranges. */
d85a05f0 10211 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10212 return;
10213 lowpc += baseaddr;
10214 highpc += baseaddr;
10215
10216 push_context (0, lowpc);
639d11d3 10217 if (die->child != NULL)
c906108c 10218 {
639d11d3 10219 child_die = die->child;
c906108c
SS
10220 while (child_die && child_die->tag)
10221 {
e7c27a73 10222 process_die (child_die, cu);
c906108c
SS
10223 child_die = sibling_die (child_die);
10224 }
10225 }
10226 new = pop_context ();
10227
8540c487 10228 if (local_symbols != NULL || using_directives != NULL)
c906108c 10229 {
801e3a5b
JB
10230 struct block *block
10231 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10232 highpc, objfile);
10233
10234 /* Note that recording ranges after traversing children, as we
10235 do here, means that recording a parent's ranges entails
10236 walking across all its children's ranges as they appear in
10237 the address map, which is quadratic behavior.
10238
10239 It would be nicer to record the parent's ranges before
10240 traversing its children, simply overriding whatever you find
10241 there. But since we don't even decide whether to create a
10242 block until after we've traversed its children, that's hard
10243 to do. */
10244 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10245 }
10246 local_symbols = new->locals;
27aa8d6a 10247 using_directives = new->using_directives;
c906108c
SS
10248}
10249
96408a79
SA
10250/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10251
10252static void
10253read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10254{
10255 struct objfile *objfile = cu->objfile;
10256 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10257 CORE_ADDR pc, baseaddr;
10258 struct attribute *attr;
10259 struct call_site *call_site, call_site_local;
10260 void **slot;
10261 int nparams;
10262 struct die_info *child_die;
10263
10264 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10265
10266 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10267 if (!attr)
10268 {
10269 complaint (&symfile_complaints,
10270 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10271 "DIE 0x%x [in module %s]"),
b64f50a1 10272 die->offset.sect_off, objfile->name);
96408a79
SA
10273 return;
10274 }
10275 pc = DW_ADDR (attr) + baseaddr;
10276
10277 if (cu->call_site_htab == NULL)
10278 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10279 NULL, &objfile->objfile_obstack,
10280 hashtab_obstack_allocate, NULL);
10281 call_site_local.pc = pc;
10282 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10283 if (*slot != NULL)
10284 {
10285 complaint (&symfile_complaints,
10286 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10287 "DIE 0x%x [in module %s]"),
b64f50a1 10288 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
10289 return;
10290 }
10291
10292 /* Count parameters at the caller. */
10293
10294 nparams = 0;
10295 for (child_die = die->child; child_die && child_die->tag;
10296 child_die = sibling_die (child_die))
10297 {
10298 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10299 {
10300 complaint (&symfile_complaints,
10301 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10302 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10303 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
10304 continue;
10305 }
10306
10307 nparams++;
10308 }
10309
10310 call_site = obstack_alloc (&objfile->objfile_obstack,
10311 (sizeof (*call_site)
10312 + (sizeof (*call_site->parameter)
10313 * (nparams - 1))));
10314 *slot = call_site;
10315 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10316 call_site->pc = pc;
10317
10318 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10319 {
10320 struct die_info *func_die;
10321
10322 /* Skip also over DW_TAG_inlined_subroutine. */
10323 for (func_die = die->parent;
10324 func_die && func_die->tag != DW_TAG_subprogram
10325 && func_die->tag != DW_TAG_subroutine_type;
10326 func_die = func_die->parent);
10327
10328 /* DW_AT_GNU_all_call_sites is a superset
10329 of DW_AT_GNU_all_tail_call_sites. */
10330 if (func_die
10331 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10332 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10333 {
10334 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10335 not complete. But keep CALL_SITE for look ups via call_site_htab,
10336 both the initial caller containing the real return address PC and
10337 the final callee containing the current PC of a chain of tail
10338 calls do not need to have the tail call list complete. But any
10339 function candidate for a virtual tail call frame searched via
10340 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10341 determined unambiguously. */
10342 }
10343 else
10344 {
10345 struct type *func_type = NULL;
10346
10347 if (func_die)
10348 func_type = get_die_type (func_die, cu);
10349 if (func_type != NULL)
10350 {
10351 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10352
10353 /* Enlist this call site to the function. */
10354 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10355 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10356 }
10357 else
10358 complaint (&symfile_complaints,
10359 _("Cannot find function owning DW_TAG_GNU_call_site "
10360 "DIE 0x%x [in module %s]"),
b64f50a1 10361 die->offset.sect_off, objfile->name);
96408a79
SA
10362 }
10363 }
10364
10365 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10366 if (attr == NULL)
10367 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10368 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10369 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10370 /* Keep NULL DWARF_BLOCK. */;
10371 else if (attr_form_is_block (attr))
10372 {
10373 struct dwarf2_locexpr_baton *dlbaton;
10374
10375 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10376 dlbaton->data = DW_BLOCK (attr)->data;
10377 dlbaton->size = DW_BLOCK (attr)->size;
10378 dlbaton->per_cu = cu->per_cu;
10379
10380 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10381 }
7771576e 10382 else if (attr_form_is_ref (attr))
96408a79 10383 {
96408a79
SA
10384 struct dwarf2_cu *target_cu = cu;
10385 struct die_info *target_die;
10386
ac9ec31b 10387 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10388 gdb_assert (target_cu->objfile == objfile);
10389 if (die_is_declaration (target_die, target_cu))
10390 {
9112db09
JK
10391 const char *target_physname = NULL;
10392 struct attribute *target_attr;
10393
10394 /* Prefer the mangled name; otherwise compute the demangled one. */
10395 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10396 if (target_attr == NULL)
10397 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10398 target_cu);
10399 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10400 target_physname = DW_STRING (target_attr);
10401 else
10402 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10403 if (target_physname == NULL)
10404 complaint (&symfile_complaints,
10405 _("DW_AT_GNU_call_site_target target DIE has invalid "
10406 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10407 die->offset.sect_off, objfile->name);
96408a79 10408 else
7d455152 10409 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10410 }
10411 else
10412 {
10413 CORE_ADDR lowpc;
10414
10415 /* DW_AT_entry_pc should be preferred. */
10416 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10417 complaint (&symfile_complaints,
10418 _("DW_AT_GNU_call_site_target target DIE has invalid "
10419 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10420 die->offset.sect_off, objfile->name);
96408a79
SA
10421 else
10422 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10423 }
10424 }
10425 else
10426 complaint (&symfile_complaints,
10427 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10428 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10429 die->offset.sect_off, objfile->name);
96408a79
SA
10430
10431 call_site->per_cu = cu->per_cu;
10432
10433 for (child_die = die->child;
10434 child_die && child_die->tag;
10435 child_die = sibling_die (child_die))
10436 {
96408a79 10437 struct call_site_parameter *parameter;
1788b2d3 10438 struct attribute *loc, *origin;
96408a79
SA
10439
10440 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10441 {
10442 /* Already printed the complaint above. */
10443 continue;
10444 }
10445
10446 gdb_assert (call_site->parameter_count < nparams);
10447 parameter = &call_site->parameter[call_site->parameter_count];
10448
1788b2d3
JK
10449 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10450 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10451 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10452
24c5c679 10453 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10454 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10455 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10456 {
10457 sect_offset offset;
10458
10459 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10460 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10461 if (!offset_in_cu_p (&cu->header, offset))
10462 {
10463 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10464 binding can be done only inside one CU. Such referenced DIE
10465 therefore cannot be even moved to DW_TAG_partial_unit. */
10466 complaint (&symfile_complaints,
10467 _("DW_AT_abstract_origin offset is not in CU for "
10468 "DW_TAG_GNU_call_site child DIE 0x%x "
10469 "[in module %s]"),
10470 child_die->offset.sect_off, objfile->name);
10471 continue;
10472 }
1788b2d3
JK
10473 parameter->u.param_offset.cu_off = (offset.sect_off
10474 - cu->header.offset.sect_off);
10475 }
10476 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10477 {
10478 complaint (&symfile_complaints,
10479 _("No DW_FORM_block* DW_AT_location for "
10480 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10481 child_die->offset.sect_off, objfile->name);
96408a79
SA
10482 continue;
10483 }
24c5c679 10484 else
96408a79 10485 {
24c5c679
JK
10486 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10487 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10488 if (parameter->u.dwarf_reg != -1)
10489 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10490 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10491 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10492 &parameter->u.fb_offset))
10493 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10494 else
10495 {
10496 complaint (&symfile_complaints,
10497 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10498 "for DW_FORM_block* DW_AT_location is supported for "
10499 "DW_TAG_GNU_call_site child DIE 0x%x "
10500 "[in module %s]"),
10501 child_die->offset.sect_off, objfile->name);
10502 continue;
10503 }
96408a79
SA
10504 }
10505
10506 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10507 if (!attr_form_is_block (attr))
10508 {
10509 complaint (&symfile_complaints,
10510 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10511 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10512 child_die->offset.sect_off, objfile->name);
96408a79
SA
10513 continue;
10514 }
10515 parameter->value = DW_BLOCK (attr)->data;
10516 parameter->value_size = DW_BLOCK (attr)->size;
10517
10518 /* Parameters are not pre-cleared by memset above. */
10519 parameter->data_value = NULL;
10520 parameter->data_value_size = 0;
10521 call_site->parameter_count++;
10522
10523 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10524 if (attr)
10525 {
10526 if (!attr_form_is_block (attr))
10527 complaint (&symfile_complaints,
10528 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10529 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10530 child_die->offset.sect_off, objfile->name);
96408a79
SA
10531 else
10532 {
10533 parameter->data_value = DW_BLOCK (attr)->data;
10534 parameter->data_value_size = DW_BLOCK (attr)->size;
10535 }
10536 }
10537 }
10538}
10539
43039443 10540/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10541 Return 1 if the attributes are present and valid, otherwise, return 0.
10542 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10543
10544static int
10545dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10546 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10547 struct partial_symtab *ranges_pst)
43039443
JK
10548{
10549 struct objfile *objfile = cu->objfile;
10550 struct comp_unit_head *cu_header = &cu->header;
10551 bfd *obfd = objfile->obfd;
10552 unsigned int addr_size = cu_header->addr_size;
10553 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10554 /* Base address selection entry. */
10555 CORE_ADDR base;
10556 int found_base;
10557 unsigned int dummy;
d521ce57 10558 const gdb_byte *buffer;
43039443
JK
10559 CORE_ADDR marker;
10560 int low_set;
10561 CORE_ADDR low = 0;
10562 CORE_ADDR high = 0;
ff013f42 10563 CORE_ADDR baseaddr;
43039443 10564
d00adf39
DE
10565 found_base = cu->base_known;
10566 base = cu->base_address;
43039443 10567
be391dca 10568 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10569 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10570 {
10571 complaint (&symfile_complaints,
10572 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10573 offset);
10574 return 0;
10575 }
dce234bc 10576 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10577
10578 /* Read in the largest possible address. */
10579 marker = read_address (obfd, buffer, cu, &dummy);
10580 if ((marker & mask) == mask)
10581 {
10582 /* If we found the largest possible address, then
10583 read the base address. */
10584 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10585 buffer += 2 * addr_size;
10586 offset += 2 * addr_size;
10587 found_base = 1;
10588 }
10589
10590 low_set = 0;
10591
e7030f15 10592 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10593
43039443
JK
10594 while (1)
10595 {
10596 CORE_ADDR range_beginning, range_end;
10597
10598 range_beginning = read_address (obfd, buffer, cu, &dummy);
10599 buffer += addr_size;
10600 range_end = read_address (obfd, buffer, cu, &dummy);
10601 buffer += addr_size;
10602 offset += 2 * addr_size;
10603
10604 /* An end of list marker is a pair of zero addresses. */
10605 if (range_beginning == 0 && range_end == 0)
10606 /* Found the end of list entry. */
10607 break;
10608
10609 /* Each base address selection entry is a pair of 2 values.
10610 The first is the largest possible address, the second is
10611 the base address. Check for a base address here. */
10612 if ((range_beginning & mask) == mask)
10613 {
10614 /* If we found the largest possible address, then
10615 read the base address. */
10616 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10617 found_base = 1;
10618 continue;
10619 }
10620
10621 if (!found_base)
10622 {
10623 /* We have no valid base address for the ranges
10624 data. */
10625 complaint (&symfile_complaints,
10626 _("Invalid .debug_ranges data (no base address)"));
10627 return 0;
10628 }
10629
9277c30c
UW
10630 if (range_beginning > range_end)
10631 {
10632 /* Inverted range entries are invalid. */
10633 complaint (&symfile_complaints,
10634 _("Invalid .debug_ranges data (inverted range)"));
10635 return 0;
10636 }
10637
10638 /* Empty range entries have no effect. */
10639 if (range_beginning == range_end)
10640 continue;
10641
43039443
JK
10642 range_beginning += base;
10643 range_end += base;
10644
01093045
DE
10645 /* A not-uncommon case of bad debug info.
10646 Don't pollute the addrmap with bad data. */
10647 if (range_beginning + baseaddr == 0
10648 && !dwarf2_per_objfile->has_section_at_zero)
10649 {
10650 complaint (&symfile_complaints,
10651 _(".debug_ranges entry has start address of zero"
10652 " [in module %s]"), objfile->name);
10653 continue;
10654 }
10655
9277c30c 10656 if (ranges_pst != NULL)
ff013f42 10657 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10658 range_beginning + baseaddr,
10659 range_end - 1 + baseaddr,
ff013f42
JK
10660 ranges_pst);
10661
43039443
JK
10662 /* FIXME: This is recording everything as a low-high
10663 segment of consecutive addresses. We should have a
10664 data structure for discontiguous block ranges
10665 instead. */
10666 if (! low_set)
10667 {
10668 low = range_beginning;
10669 high = range_end;
10670 low_set = 1;
10671 }
10672 else
10673 {
10674 if (range_beginning < low)
10675 low = range_beginning;
10676 if (range_end > high)
10677 high = range_end;
10678 }
10679 }
10680
10681 if (! low_set)
10682 /* If the first entry is an end-of-list marker, the range
10683 describes an empty scope, i.e. no instructions. */
10684 return 0;
10685
10686 if (low_return)
10687 *low_return = low;
10688 if (high_return)
10689 *high_return = high;
10690 return 1;
10691}
10692
af34e669
DJ
10693/* Get low and high pc attributes from a die. Return 1 if the attributes
10694 are present and valid, otherwise, return 0. Return -1 if the range is
10695 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10696
c906108c 10697static int
af34e669 10698dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10699 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10700 struct partial_symtab *pst)
c906108c
SS
10701{
10702 struct attribute *attr;
91da1414 10703 struct attribute *attr_high;
af34e669
DJ
10704 CORE_ADDR low = 0;
10705 CORE_ADDR high = 0;
10706 int ret = 0;
c906108c 10707
91da1414
MW
10708 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10709 if (attr_high)
af34e669 10710 {
e142c38c 10711 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10712 if (attr)
91da1414
MW
10713 {
10714 low = DW_ADDR (attr);
3019eac3
DE
10715 if (attr_high->form == DW_FORM_addr
10716 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10717 high = DW_ADDR (attr_high);
10718 else
10719 high = low + DW_UNSND (attr_high);
10720 }
af34e669
DJ
10721 else
10722 /* Found high w/o low attribute. */
10723 return 0;
10724
10725 /* Found consecutive range of addresses. */
10726 ret = 1;
10727 }
c906108c 10728 else
af34e669 10729 {
e142c38c 10730 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10731 if (attr != NULL)
10732 {
ab435259
DE
10733 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10734 We take advantage of the fact that DW_AT_ranges does not appear
10735 in DW_TAG_compile_unit of DWO files. */
10736 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10737 unsigned int ranges_offset = (DW_UNSND (attr)
10738 + (need_ranges_base
10739 ? cu->ranges_base
10740 : 0));
2e3cf129 10741
af34e669 10742 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10743 .debug_ranges section. */
2e3cf129 10744 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10745 return 0;
43039443 10746 /* Found discontinuous range of addresses. */
af34e669
DJ
10747 ret = -1;
10748 }
10749 }
c906108c 10750
9373cf26
JK
10751 /* read_partial_die has also the strict LOW < HIGH requirement. */
10752 if (high <= low)
c906108c
SS
10753 return 0;
10754
10755 /* When using the GNU linker, .gnu.linkonce. sections are used to
10756 eliminate duplicate copies of functions and vtables and such.
10757 The linker will arbitrarily choose one and discard the others.
10758 The AT_*_pc values for such functions refer to local labels in
10759 these sections. If the section from that file was discarded, the
10760 labels are not in the output, so the relocs get a value of 0.
10761 If this is a discarded function, mark the pc bounds as invalid,
10762 so that GDB will ignore it. */
72dca2f5 10763 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10764 return 0;
10765
10766 *lowpc = low;
96408a79
SA
10767 if (highpc)
10768 *highpc = high;
af34e669 10769 return ret;
c906108c
SS
10770}
10771
b084d499
JB
10772/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10773 its low and high PC addresses. Do nothing if these addresses could not
10774 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10775 and HIGHPC to the high address if greater than HIGHPC. */
10776
10777static void
10778dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10779 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10780 struct dwarf2_cu *cu)
10781{
10782 CORE_ADDR low, high;
10783 struct die_info *child = die->child;
10784
d85a05f0 10785 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10786 {
10787 *lowpc = min (*lowpc, low);
10788 *highpc = max (*highpc, high);
10789 }
10790
10791 /* If the language does not allow nested subprograms (either inside
10792 subprograms or lexical blocks), we're done. */
10793 if (cu->language != language_ada)
10794 return;
6e70227d 10795
b084d499
JB
10796 /* Check all the children of the given DIE. If it contains nested
10797 subprograms, then check their pc bounds. Likewise, we need to
10798 check lexical blocks as well, as they may also contain subprogram
10799 definitions. */
10800 while (child && child->tag)
10801 {
10802 if (child->tag == DW_TAG_subprogram
10803 || child->tag == DW_TAG_lexical_block)
10804 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10805 child = sibling_die (child);
10806 }
10807}
10808
fae299cd
DC
10809/* Get the low and high pc's represented by the scope DIE, and store
10810 them in *LOWPC and *HIGHPC. If the correct values can't be
10811 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10812
10813static void
10814get_scope_pc_bounds (struct die_info *die,
10815 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10816 struct dwarf2_cu *cu)
10817{
10818 CORE_ADDR best_low = (CORE_ADDR) -1;
10819 CORE_ADDR best_high = (CORE_ADDR) 0;
10820 CORE_ADDR current_low, current_high;
10821
d85a05f0 10822 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10823 {
10824 best_low = current_low;
10825 best_high = current_high;
10826 }
10827 else
10828 {
10829 struct die_info *child = die->child;
10830
10831 while (child && child->tag)
10832 {
10833 switch (child->tag) {
10834 case DW_TAG_subprogram:
b084d499 10835 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10836 break;
10837 case DW_TAG_namespace:
f55ee35c 10838 case DW_TAG_module:
fae299cd
DC
10839 /* FIXME: carlton/2004-01-16: Should we do this for
10840 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10841 that current GCC's always emit the DIEs corresponding
10842 to definitions of methods of classes as children of a
10843 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10844 the DIEs giving the declarations, which could be
10845 anywhere). But I don't see any reason why the
10846 standards says that they have to be there. */
10847 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10848
10849 if (current_low != ((CORE_ADDR) -1))
10850 {
10851 best_low = min (best_low, current_low);
10852 best_high = max (best_high, current_high);
10853 }
10854 break;
10855 default:
0963b4bd 10856 /* Ignore. */
fae299cd
DC
10857 break;
10858 }
10859
10860 child = sibling_die (child);
10861 }
10862 }
10863
10864 *lowpc = best_low;
10865 *highpc = best_high;
10866}
10867
801e3a5b
JB
10868/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10869 in DIE. */
380bca97 10870
801e3a5b
JB
10871static void
10872dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10873 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10874{
bb5ed363 10875 struct objfile *objfile = cu->objfile;
801e3a5b 10876 struct attribute *attr;
91da1414 10877 struct attribute *attr_high;
801e3a5b 10878
91da1414
MW
10879 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10880 if (attr_high)
801e3a5b 10881 {
801e3a5b
JB
10882 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10883 if (attr)
10884 {
10885 CORE_ADDR low = DW_ADDR (attr);
91da1414 10886 CORE_ADDR high;
3019eac3
DE
10887 if (attr_high->form == DW_FORM_addr
10888 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10889 high = DW_ADDR (attr_high);
10890 else
10891 high = low + DW_UNSND (attr_high);
9a619af0 10892
801e3a5b
JB
10893 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10894 }
10895 }
10896
10897 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10898 if (attr)
10899 {
bb5ed363 10900 bfd *obfd = objfile->obfd;
ab435259
DE
10901 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10902 We take advantage of the fact that DW_AT_ranges does not appear
10903 in DW_TAG_compile_unit of DWO files. */
10904 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10905
10906 /* The value of the DW_AT_ranges attribute is the offset of the
10907 address range list in the .debug_ranges section. */
ab435259
DE
10908 unsigned long offset = (DW_UNSND (attr)
10909 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10910 const gdb_byte *buffer;
801e3a5b
JB
10911
10912 /* For some target architectures, but not others, the
10913 read_address function sign-extends the addresses it returns.
10914 To recognize base address selection entries, we need a
10915 mask. */
10916 unsigned int addr_size = cu->header.addr_size;
10917 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10918
10919 /* The base address, to which the next pair is relative. Note
10920 that this 'base' is a DWARF concept: most entries in a range
10921 list are relative, to reduce the number of relocs against the
10922 debugging information. This is separate from this function's
10923 'baseaddr' argument, which GDB uses to relocate debugging
10924 information from a shared library based on the address at
10925 which the library was loaded. */
d00adf39
DE
10926 CORE_ADDR base = cu->base_address;
10927 int base_known = cu->base_known;
801e3a5b 10928
d62bfeaf 10929 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10930 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10931 {
10932 complaint (&symfile_complaints,
10933 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10934 offset);
10935 return;
10936 }
d62bfeaf 10937 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10938
10939 for (;;)
10940 {
10941 unsigned int bytes_read;
10942 CORE_ADDR start, end;
10943
10944 start = read_address (obfd, buffer, cu, &bytes_read);
10945 buffer += bytes_read;
10946 end = read_address (obfd, buffer, cu, &bytes_read);
10947 buffer += bytes_read;
10948
10949 /* Did we find the end of the range list? */
10950 if (start == 0 && end == 0)
10951 break;
10952
10953 /* Did we find a base address selection entry? */
10954 else if ((start & base_select_mask) == base_select_mask)
10955 {
10956 base = end;
10957 base_known = 1;
10958 }
10959
10960 /* We found an ordinary address range. */
10961 else
10962 {
10963 if (!base_known)
10964 {
10965 complaint (&symfile_complaints,
3e43a32a
MS
10966 _("Invalid .debug_ranges data "
10967 "(no base address)"));
801e3a5b
JB
10968 return;
10969 }
10970
9277c30c
UW
10971 if (start > end)
10972 {
10973 /* Inverted range entries are invalid. */
10974 complaint (&symfile_complaints,
10975 _("Invalid .debug_ranges data "
10976 "(inverted range)"));
10977 return;
10978 }
10979
10980 /* Empty range entries have no effect. */
10981 if (start == end)
10982 continue;
10983
01093045
DE
10984 start += base + baseaddr;
10985 end += base + baseaddr;
10986
10987 /* A not-uncommon case of bad debug info.
10988 Don't pollute the addrmap with bad data. */
10989 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10990 {
10991 complaint (&symfile_complaints,
10992 _(".debug_ranges entry has start address of zero"
10993 " [in module %s]"), objfile->name);
10994 continue;
10995 }
10996
10997 record_block_range (block, start, end - 1);
801e3a5b
JB
10998 }
10999 }
11000 }
11001}
11002
685b1105
JK
11003/* Check whether the producer field indicates either of GCC < 4.6, or the
11004 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 11005
685b1105
JK
11006static void
11007check_producer (struct dwarf2_cu *cu)
60d5a603
JK
11008{
11009 const char *cs;
11010 int major, minor, release;
11011
11012 if (cu->producer == NULL)
11013 {
11014 /* For unknown compilers expect their behavior is DWARF version
11015 compliant.
11016
11017 GCC started to support .debug_types sections by -gdwarf-4 since
11018 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11019 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11020 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11021 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11022 }
685b1105 11023 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11024 {
685b1105
JK
11025 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11026
ba919b58
TT
11027 cs = &cu->producer[strlen ("GNU ")];
11028 while (*cs && !isdigit (*cs))
11029 cs++;
11030 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11031 {
11032 /* Not recognized as GCC. */
11033 }
11034 else
1b80a9fa
JK
11035 {
11036 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11037 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11038 }
685b1105
JK
11039 }
11040 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11041 cu->producer_is_icc = 1;
11042 else
11043 {
11044 /* For other non-GCC compilers, expect their behavior is DWARF version
11045 compliant. */
60d5a603
JK
11046 }
11047
ba919b58 11048 cu->checked_producer = 1;
685b1105 11049}
ba919b58 11050
685b1105
JK
11051/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11052 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11053 during 4.6.0 experimental. */
11054
11055static int
11056producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11057{
11058 if (!cu->checked_producer)
11059 check_producer (cu);
11060
11061 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11062}
11063
11064/* Return the default accessibility type if it is not overriden by
11065 DW_AT_accessibility. */
11066
11067static enum dwarf_access_attribute
11068dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11069{
11070 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11071 {
11072 /* The default DWARF 2 accessibility for members is public, the default
11073 accessibility for inheritance is private. */
11074
11075 if (die->tag != DW_TAG_inheritance)
11076 return DW_ACCESS_public;
11077 else
11078 return DW_ACCESS_private;
11079 }
11080 else
11081 {
11082 /* DWARF 3+ defines the default accessibility a different way. The same
11083 rules apply now for DW_TAG_inheritance as for the members and it only
11084 depends on the container kind. */
11085
11086 if (die->parent->tag == DW_TAG_class_type)
11087 return DW_ACCESS_private;
11088 else
11089 return DW_ACCESS_public;
11090 }
11091}
11092
74ac6d43
TT
11093/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11094 offset. If the attribute was not found return 0, otherwise return
11095 1. If it was found but could not properly be handled, set *OFFSET
11096 to 0. */
11097
11098static int
11099handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11100 LONGEST *offset)
11101{
11102 struct attribute *attr;
11103
11104 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11105 if (attr != NULL)
11106 {
11107 *offset = 0;
11108
11109 /* Note that we do not check for a section offset first here.
11110 This is because DW_AT_data_member_location is new in DWARF 4,
11111 so if we see it, we can assume that a constant form is really
11112 a constant and not a section offset. */
11113 if (attr_form_is_constant (attr))
11114 *offset = dwarf2_get_attr_constant_value (attr, 0);
11115 else if (attr_form_is_section_offset (attr))
11116 dwarf2_complex_location_expr_complaint ();
11117 else if (attr_form_is_block (attr))
11118 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11119 else
11120 dwarf2_complex_location_expr_complaint ();
11121
11122 return 1;
11123 }
11124
11125 return 0;
11126}
11127
c906108c
SS
11128/* Add an aggregate field to the field list. */
11129
11130static void
107d2387 11131dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11132 struct dwarf2_cu *cu)
6e70227d 11133{
e7c27a73 11134 struct objfile *objfile = cu->objfile;
5e2b427d 11135 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11136 struct nextfield *new_field;
11137 struct attribute *attr;
11138 struct field *fp;
15d034d0 11139 const char *fieldname = "";
c906108c
SS
11140
11141 /* Allocate a new field list entry and link it in. */
11142 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11143 make_cleanup (xfree, new_field);
c906108c 11144 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11145
11146 if (die->tag == DW_TAG_inheritance)
11147 {
11148 new_field->next = fip->baseclasses;
11149 fip->baseclasses = new_field;
11150 }
11151 else
11152 {
11153 new_field->next = fip->fields;
11154 fip->fields = new_field;
11155 }
c906108c
SS
11156 fip->nfields++;
11157
e142c38c 11158 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11159 if (attr)
11160 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11161 else
11162 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11163 if (new_field->accessibility != DW_ACCESS_public)
11164 fip->non_public_fields = 1;
60d5a603 11165
e142c38c 11166 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11167 if (attr)
11168 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11169 else
11170 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11171
11172 fp = &new_field->field;
a9a9bd0f 11173
e142c38c 11174 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11175 {
74ac6d43
TT
11176 LONGEST offset;
11177
a9a9bd0f 11178 /* Data member other than a C++ static data member. */
6e70227d 11179
c906108c 11180 /* Get type of field. */
e7c27a73 11181 fp->type = die_type (die, cu);
c906108c 11182
d6a843b5 11183 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11184
c906108c 11185 /* Get bit size of field (zero if none). */
e142c38c 11186 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11187 if (attr)
11188 {
11189 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11190 }
11191 else
11192 {
11193 FIELD_BITSIZE (*fp) = 0;
11194 }
11195
11196 /* Get bit offset of field. */
74ac6d43
TT
11197 if (handle_data_member_location (die, cu, &offset))
11198 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11199 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11200 if (attr)
11201 {
5e2b427d 11202 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11203 {
11204 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11205 additional bit offset from the MSB of the containing
11206 anonymous object to the MSB of the field. We don't
11207 have to do anything special since we don't need to
11208 know the size of the anonymous object. */
f41f5e61 11209 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11210 }
11211 else
11212 {
11213 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11214 MSB of the anonymous object, subtract off the number of
11215 bits from the MSB of the field to the MSB of the
11216 object, and then subtract off the number of bits of
11217 the field itself. The result is the bit offset of
11218 the LSB of the field. */
c906108c
SS
11219 int anonymous_size;
11220 int bit_offset = DW_UNSND (attr);
11221
e142c38c 11222 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11223 if (attr)
11224 {
11225 /* The size of the anonymous object containing
11226 the bit field is explicit, so use the
11227 indicated size (in bytes). */
11228 anonymous_size = DW_UNSND (attr);
11229 }
11230 else
11231 {
11232 /* The size of the anonymous object containing
11233 the bit field must be inferred from the type
11234 attribute of the data member containing the
11235 bit field. */
11236 anonymous_size = TYPE_LENGTH (fp->type);
11237 }
f41f5e61
PA
11238 SET_FIELD_BITPOS (*fp,
11239 (FIELD_BITPOS (*fp)
11240 + anonymous_size * bits_per_byte
11241 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11242 }
11243 }
11244
11245 /* Get name of field. */
39cbfefa
DJ
11246 fieldname = dwarf2_name (die, cu);
11247 if (fieldname == NULL)
11248 fieldname = "";
d8151005
DJ
11249
11250 /* The name is already allocated along with this objfile, so we don't
11251 need to duplicate it for the type. */
11252 fp->name = fieldname;
c906108c
SS
11253
11254 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11255 pointer or virtual base class pointer) to private. */
e142c38c 11256 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11257 {
d48cc9dd 11258 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11259 new_field->accessibility = DW_ACCESS_private;
11260 fip->non_public_fields = 1;
11261 }
11262 }
a9a9bd0f 11263 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11264 {
a9a9bd0f
DC
11265 /* C++ static member. */
11266
11267 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11268 is a declaration, but all versions of G++ as of this writing
11269 (so through at least 3.2.1) incorrectly generate
11270 DW_TAG_variable tags. */
6e70227d 11271
ff355380 11272 const char *physname;
c906108c 11273
a9a9bd0f 11274 /* Get name of field. */
39cbfefa
DJ
11275 fieldname = dwarf2_name (die, cu);
11276 if (fieldname == NULL)
c906108c
SS
11277 return;
11278
254e6b9e 11279 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11280 if (attr
11281 /* Only create a symbol if this is an external value.
11282 new_symbol checks this and puts the value in the global symbol
11283 table, which we want. If it is not external, new_symbol
11284 will try to put the value in cu->list_in_scope which is wrong. */
11285 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11286 {
11287 /* A static const member, not much different than an enum as far as
11288 we're concerned, except that we can support more types. */
11289 new_symbol (die, NULL, cu);
11290 }
11291
2df3850c 11292 /* Get physical name. */
ff355380 11293 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11294
d8151005
DJ
11295 /* The name is already allocated along with this objfile, so we don't
11296 need to duplicate it for the type. */
11297 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11298 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11299 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11300 }
11301 else if (die->tag == DW_TAG_inheritance)
11302 {
74ac6d43 11303 LONGEST offset;
d4b96c9a 11304
74ac6d43
TT
11305 /* C++ base class field. */
11306 if (handle_data_member_location (die, cu, &offset))
11307 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11308 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11309 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11310 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11311 fip->nbaseclasses++;
11312 }
11313}
11314
98751a41
JK
11315/* Add a typedef defined in the scope of the FIP's class. */
11316
11317static void
11318dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11319 struct dwarf2_cu *cu)
6e70227d 11320{
98751a41 11321 struct objfile *objfile = cu->objfile;
98751a41
JK
11322 struct typedef_field_list *new_field;
11323 struct attribute *attr;
11324 struct typedef_field *fp;
11325 char *fieldname = "";
11326
11327 /* Allocate a new field list entry and link it in. */
11328 new_field = xzalloc (sizeof (*new_field));
11329 make_cleanup (xfree, new_field);
11330
11331 gdb_assert (die->tag == DW_TAG_typedef);
11332
11333 fp = &new_field->field;
11334
11335 /* Get name of field. */
11336 fp->name = dwarf2_name (die, cu);
11337 if (fp->name == NULL)
11338 return;
11339
11340 fp->type = read_type_die (die, cu);
11341
11342 new_field->next = fip->typedef_field_list;
11343 fip->typedef_field_list = new_field;
11344 fip->typedef_field_list_count++;
11345}
11346
c906108c
SS
11347/* Create the vector of fields, and attach it to the type. */
11348
11349static void
fba45db2 11350dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11351 struct dwarf2_cu *cu)
c906108c
SS
11352{
11353 int nfields = fip->nfields;
11354
11355 /* Record the field count, allocate space for the array of fields,
11356 and create blank accessibility bitfields if necessary. */
11357 TYPE_NFIELDS (type) = nfields;
11358 TYPE_FIELDS (type) = (struct field *)
11359 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11360 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11361
b4ba55a1 11362 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11363 {
11364 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11365
11366 TYPE_FIELD_PRIVATE_BITS (type) =
11367 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11368 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11369
11370 TYPE_FIELD_PROTECTED_BITS (type) =
11371 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11372 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11373
774b6a14
TT
11374 TYPE_FIELD_IGNORE_BITS (type) =
11375 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11376 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11377 }
11378
11379 /* If the type has baseclasses, allocate and clear a bit vector for
11380 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11381 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11382 {
11383 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11384 unsigned char *pointer;
c906108c
SS
11385
11386 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11387 pointer = TYPE_ALLOC (type, num_bytes);
11388 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11389 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11390 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11391 }
11392
3e43a32a
MS
11393 /* Copy the saved-up fields into the field vector. Start from the head of
11394 the list, adding to the tail of the field array, so that they end up in
11395 the same order in the array in which they were added to the list. */
c906108c
SS
11396 while (nfields-- > 0)
11397 {
7d0ccb61
DJ
11398 struct nextfield *fieldp;
11399
11400 if (fip->fields)
11401 {
11402 fieldp = fip->fields;
11403 fip->fields = fieldp->next;
11404 }
11405 else
11406 {
11407 fieldp = fip->baseclasses;
11408 fip->baseclasses = fieldp->next;
11409 }
11410
11411 TYPE_FIELD (type, nfields) = fieldp->field;
11412 switch (fieldp->accessibility)
c906108c 11413 {
c5aa993b 11414 case DW_ACCESS_private:
b4ba55a1
JB
11415 if (cu->language != language_ada)
11416 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11417 break;
c906108c 11418
c5aa993b 11419 case DW_ACCESS_protected:
b4ba55a1
JB
11420 if (cu->language != language_ada)
11421 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11422 break;
c906108c 11423
c5aa993b
JM
11424 case DW_ACCESS_public:
11425 break;
c906108c 11426
c5aa993b
JM
11427 default:
11428 /* Unknown accessibility. Complain and treat it as public. */
11429 {
e2e0b3e5 11430 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11431 fieldp->accessibility);
c5aa993b
JM
11432 }
11433 break;
c906108c
SS
11434 }
11435 if (nfields < fip->nbaseclasses)
11436 {
7d0ccb61 11437 switch (fieldp->virtuality)
c906108c 11438 {
c5aa993b
JM
11439 case DW_VIRTUALITY_virtual:
11440 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11441 if (cu->language == language_ada)
a73c6dcd 11442 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11443 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11444 break;
c906108c
SS
11445 }
11446 }
c906108c
SS
11447 }
11448}
11449
7d27a96d
TT
11450/* Return true if this member function is a constructor, false
11451 otherwise. */
11452
11453static int
11454dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11455{
11456 const char *fieldname;
11457 const char *typename;
11458 int len;
11459
11460 if (die->parent == NULL)
11461 return 0;
11462
11463 if (die->parent->tag != DW_TAG_structure_type
11464 && die->parent->tag != DW_TAG_union_type
11465 && die->parent->tag != DW_TAG_class_type)
11466 return 0;
11467
11468 fieldname = dwarf2_name (die, cu);
11469 typename = dwarf2_name (die->parent, cu);
11470 if (fieldname == NULL || typename == NULL)
11471 return 0;
11472
11473 len = strlen (fieldname);
11474 return (strncmp (fieldname, typename, len) == 0
11475 && (typename[len] == '\0' || typename[len] == '<'));
11476}
11477
c906108c
SS
11478/* Add a member function to the proper fieldlist. */
11479
11480static void
107d2387 11481dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11482 struct type *type, struct dwarf2_cu *cu)
c906108c 11483{
e7c27a73 11484 struct objfile *objfile = cu->objfile;
c906108c
SS
11485 struct attribute *attr;
11486 struct fnfieldlist *flp;
11487 int i;
11488 struct fn_field *fnp;
15d034d0 11489 const char *fieldname;
c906108c 11490 struct nextfnfield *new_fnfield;
f792889a 11491 struct type *this_type;
60d5a603 11492 enum dwarf_access_attribute accessibility;
c906108c 11493
b4ba55a1 11494 if (cu->language == language_ada)
a73c6dcd 11495 error (_("unexpected member function in Ada type"));
b4ba55a1 11496
2df3850c 11497 /* Get name of member function. */
39cbfefa
DJ
11498 fieldname = dwarf2_name (die, cu);
11499 if (fieldname == NULL)
2df3850c 11500 return;
c906108c 11501
c906108c
SS
11502 /* Look up member function name in fieldlist. */
11503 for (i = 0; i < fip->nfnfields; i++)
11504 {
27bfe10e 11505 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11506 break;
11507 }
11508
11509 /* Create new list element if necessary. */
11510 if (i < fip->nfnfields)
11511 flp = &fip->fnfieldlists[i];
11512 else
11513 {
11514 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11515 {
11516 fip->fnfieldlists = (struct fnfieldlist *)
11517 xrealloc (fip->fnfieldlists,
11518 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11519 * sizeof (struct fnfieldlist));
c906108c 11520 if (fip->nfnfields == 0)
c13c43fd 11521 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11522 }
11523 flp = &fip->fnfieldlists[fip->nfnfields];
11524 flp->name = fieldname;
11525 flp->length = 0;
11526 flp->head = NULL;
3da10d80 11527 i = fip->nfnfields++;
c906108c
SS
11528 }
11529
11530 /* Create a new member function field and chain it to the field list
0963b4bd 11531 entry. */
c906108c 11532 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11533 make_cleanup (xfree, new_fnfield);
c906108c
SS
11534 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11535 new_fnfield->next = flp->head;
11536 flp->head = new_fnfield;
11537 flp->length++;
11538
11539 /* Fill in the member function field info. */
11540 fnp = &new_fnfield->fnfield;
3da10d80
KS
11541
11542 /* Delay processing of the physname until later. */
11543 if (cu->language == language_cplus || cu->language == language_java)
11544 {
11545 add_to_method_list (type, i, flp->length - 1, fieldname,
11546 die, cu);
11547 }
11548 else
11549 {
1d06ead6 11550 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11551 fnp->physname = physname ? physname : "";
11552 }
11553
c906108c 11554 fnp->type = alloc_type (objfile);
f792889a
DJ
11555 this_type = read_type_die (die, cu);
11556 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11557 {
f792889a 11558 int nparams = TYPE_NFIELDS (this_type);
c906108c 11559
f792889a 11560 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11561 of the method itself (TYPE_CODE_METHOD). */
11562 smash_to_method_type (fnp->type, type,
f792889a
DJ
11563 TYPE_TARGET_TYPE (this_type),
11564 TYPE_FIELDS (this_type),
11565 TYPE_NFIELDS (this_type),
11566 TYPE_VARARGS (this_type));
c906108c
SS
11567
11568 /* Handle static member functions.
c5aa993b 11569 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11570 member functions. G++ helps GDB by marking the first
11571 parameter for non-static member functions (which is the this
11572 pointer) as artificial. We obtain this information from
11573 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11574 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11575 fnp->voffset = VOFFSET_STATIC;
11576 }
11577 else
e2e0b3e5 11578 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11579 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11580
11581 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11582 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11583 fnp->fcontext = die_containing_type (die, cu);
c906108c 11584
3e43a32a
MS
11585 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11586 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11587
11588 /* Get accessibility. */
e142c38c 11589 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11590 if (attr)
60d5a603
JK
11591 accessibility = DW_UNSND (attr);
11592 else
11593 accessibility = dwarf2_default_access_attribute (die, cu);
11594 switch (accessibility)
c906108c 11595 {
60d5a603
JK
11596 case DW_ACCESS_private:
11597 fnp->is_private = 1;
11598 break;
11599 case DW_ACCESS_protected:
11600 fnp->is_protected = 1;
11601 break;
c906108c
SS
11602 }
11603
b02dede2 11604 /* Check for artificial methods. */
e142c38c 11605 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11606 if (attr && DW_UNSND (attr) != 0)
11607 fnp->is_artificial = 1;
11608
7d27a96d
TT
11609 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11610
0d564a31 11611 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11612 function. For older versions of GCC, this is an offset in the
11613 appropriate virtual table, as specified by DW_AT_containing_type.
11614 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11615 to the object address. */
11616
e142c38c 11617 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11618 if (attr)
8e19ed76 11619 {
aec5aa8b 11620 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11621 {
aec5aa8b
TT
11622 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11623 {
11624 /* Old-style GCC. */
11625 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11626 }
11627 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11628 || (DW_BLOCK (attr)->size > 1
11629 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11630 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11631 {
11632 struct dwarf_block blk;
11633 int offset;
11634
11635 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11636 ? 1 : 2);
11637 blk.size = DW_BLOCK (attr)->size - offset;
11638 blk.data = DW_BLOCK (attr)->data + offset;
11639 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11640 if ((fnp->voffset % cu->header.addr_size) != 0)
11641 dwarf2_complex_location_expr_complaint ();
11642 else
11643 fnp->voffset /= cu->header.addr_size;
11644 fnp->voffset += 2;
11645 }
11646 else
11647 dwarf2_complex_location_expr_complaint ();
11648
11649 if (!fnp->fcontext)
11650 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11651 }
3690dd37 11652 else if (attr_form_is_section_offset (attr))
8e19ed76 11653 {
4d3c2250 11654 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11655 }
11656 else
11657 {
4d3c2250
KB
11658 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11659 fieldname);
8e19ed76 11660 }
0d564a31 11661 }
d48cc9dd
DJ
11662 else
11663 {
11664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11665 if (attr && DW_UNSND (attr))
11666 {
11667 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11668 complaint (&symfile_complaints,
3e43a32a
MS
11669 _("Member function \"%s\" (offset %d) is virtual "
11670 "but the vtable offset is not specified"),
b64f50a1 11671 fieldname, die->offset.sect_off);
9655fd1a 11672 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11673 TYPE_CPLUS_DYNAMIC (type) = 1;
11674 }
11675 }
c906108c
SS
11676}
11677
11678/* Create the vector of member function fields, and attach it to the type. */
11679
11680static void
fba45db2 11681dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11682 struct dwarf2_cu *cu)
c906108c
SS
11683{
11684 struct fnfieldlist *flp;
c906108c
SS
11685 int i;
11686
b4ba55a1 11687 if (cu->language == language_ada)
a73c6dcd 11688 error (_("unexpected member functions in Ada type"));
b4ba55a1 11689
c906108c
SS
11690 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11691 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11692 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11693
11694 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11695 {
11696 struct nextfnfield *nfp = flp->head;
11697 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11698 int k;
11699
11700 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11701 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11702 fn_flp->fn_fields = (struct fn_field *)
11703 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11704 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11705 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11706 }
11707
11708 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11709}
11710
1168df01
JB
11711/* Returns non-zero if NAME is the name of a vtable member in CU's
11712 language, zero otherwise. */
11713static int
11714is_vtable_name (const char *name, struct dwarf2_cu *cu)
11715{
11716 static const char vptr[] = "_vptr";
987504bb 11717 static const char vtable[] = "vtable";
1168df01 11718
987504bb
JJ
11719 /* Look for the C++ and Java forms of the vtable. */
11720 if ((cu->language == language_java
11721 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11722 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11723 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11724 return 1;
11725
11726 return 0;
11727}
11728
c0dd20ea 11729/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11730 functions, with the ABI-specified layout. If TYPE describes
11731 such a structure, smash it into a member function type.
61049d3b
DJ
11732
11733 GCC shouldn't do this; it should just output pointer to member DIEs.
11734 This is GCC PR debug/28767. */
c0dd20ea 11735
0b92b5bb
TT
11736static void
11737quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11738{
0b92b5bb 11739 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11740
11741 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11742 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11743 return;
c0dd20ea
DJ
11744
11745 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11746 if (TYPE_FIELD_NAME (type, 0) == NULL
11747 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11748 || TYPE_FIELD_NAME (type, 1) == NULL
11749 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11750 return;
c0dd20ea
DJ
11751
11752 /* Find the type of the method. */
0b92b5bb 11753 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11754 if (pfn_type == NULL
11755 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11756 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11757 return;
c0dd20ea
DJ
11758
11759 /* Look for the "this" argument. */
11760 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11761 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11762 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11763 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11764 return;
c0dd20ea
DJ
11765
11766 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11767 new_type = alloc_type (objfile);
11768 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11769 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11770 TYPE_VARARGS (pfn_type));
0b92b5bb 11771 smash_to_methodptr_type (type, new_type);
c0dd20ea 11772}
1168df01 11773
685b1105
JK
11774/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11775 (icc). */
11776
11777static int
11778producer_is_icc (struct dwarf2_cu *cu)
11779{
11780 if (!cu->checked_producer)
11781 check_producer (cu);
11782
11783 return cu->producer_is_icc;
11784}
11785
c906108c 11786/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11787 (definition) to create a type for the structure or union. Fill in
11788 the type's name and general properties; the members will not be
3d1d5ea3 11789 processed until process_structure_scope.
c906108c 11790
c767944b
DJ
11791 NOTE: we need to call these functions regardless of whether or not the
11792 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11793 structure or union. This gets the type entered into our set of
11794 user defined types.
11795
11796 However, if the structure is incomplete (an opaque struct/union)
11797 then suppress creating a symbol table entry for it since gdb only
11798 wants to find the one with the complete definition. Note that if
11799 it is complete, we just call new_symbol, which does it's own
11800 checking about whether the struct/union is anonymous or not (and
11801 suppresses creating a symbol table entry itself). */
11802
f792889a 11803static struct type *
134d01f1 11804read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11805{
e7c27a73 11806 struct objfile *objfile = cu->objfile;
c906108c
SS
11807 struct type *type;
11808 struct attribute *attr;
15d034d0 11809 const char *name;
c906108c 11810
348e048f
DE
11811 /* If the definition of this type lives in .debug_types, read that type.
11812 Don't follow DW_AT_specification though, that will take us back up
11813 the chain and we want to go down. */
45e58e77 11814 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11815 if (attr)
11816 {
ac9ec31b 11817 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11818
ac9ec31b 11819 /* The type's CU may not be the same as CU.
02142a6c 11820 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11821 return set_die_type (die, type, cu);
11822 }
11823
c0dd20ea 11824 type = alloc_type (objfile);
c906108c 11825 INIT_CPLUS_SPECIFIC (type);
93311388 11826
39cbfefa
DJ
11827 name = dwarf2_name (die, cu);
11828 if (name != NULL)
c906108c 11829 {
987504bb
JJ
11830 if (cu->language == language_cplus
11831 || cu->language == language_java)
63d06c5c 11832 {
15d034d0 11833 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11834
11835 /* dwarf2_full_name might have already finished building the DIE's
11836 type. If so, there is no need to continue. */
11837 if (get_die_type (die, cu) != NULL)
11838 return get_die_type (die, cu);
11839
11840 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11841 if (die->tag == DW_TAG_structure_type
11842 || die->tag == DW_TAG_class_type)
11843 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11844 }
11845 else
11846 {
d8151005
DJ
11847 /* The name is already allocated along with this objfile, so
11848 we don't need to duplicate it for the type. */
7d455152 11849 TYPE_TAG_NAME (type) = name;
94af9270
KS
11850 if (die->tag == DW_TAG_class_type)
11851 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11852 }
c906108c
SS
11853 }
11854
11855 if (die->tag == DW_TAG_structure_type)
11856 {
11857 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11858 }
11859 else if (die->tag == DW_TAG_union_type)
11860 {
11861 TYPE_CODE (type) = TYPE_CODE_UNION;
11862 }
11863 else
11864 {
c906108c
SS
11865 TYPE_CODE (type) = TYPE_CODE_CLASS;
11866 }
11867
0cc2414c
TT
11868 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11869 TYPE_DECLARED_CLASS (type) = 1;
11870
e142c38c 11871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11872 if (attr)
11873 {
11874 TYPE_LENGTH (type) = DW_UNSND (attr);
11875 }
11876 else
11877 {
11878 TYPE_LENGTH (type) = 0;
11879 }
11880
685b1105
JK
11881 if (producer_is_icc (cu))
11882 {
11883 /* ICC does not output the required DW_AT_declaration
11884 on incomplete types, but gives them a size of zero. */
11885 }
11886 else
11887 TYPE_STUB_SUPPORTED (type) = 1;
11888
dc718098 11889 if (die_is_declaration (die, cu))
876cecd0 11890 TYPE_STUB (type) = 1;
a6c727b2
DJ
11891 else if (attr == NULL && die->child == NULL
11892 && producer_is_realview (cu->producer))
11893 /* RealView does not output the required DW_AT_declaration
11894 on incomplete types. */
11895 TYPE_STUB (type) = 1;
dc718098 11896
c906108c
SS
11897 /* We need to add the type field to the die immediately so we don't
11898 infinitely recurse when dealing with pointers to the structure
0963b4bd 11899 type within the structure itself. */
1c379e20 11900 set_die_type (die, type, cu);
c906108c 11901
7e314c57
JK
11902 /* set_die_type should be already done. */
11903 set_descriptive_type (type, die, cu);
11904
c767944b
DJ
11905 return type;
11906}
11907
11908/* Finish creating a structure or union type, including filling in
11909 its members and creating a symbol for it. */
11910
11911static void
11912process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11913{
11914 struct objfile *objfile = cu->objfile;
11915 struct die_info *child_die = die->child;
11916 struct type *type;
11917
11918 type = get_die_type (die, cu);
11919 if (type == NULL)
11920 type = read_structure_type (die, cu);
11921
e142c38c 11922 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11923 {
11924 struct field_info fi;
11925 struct die_info *child_die;
34eaf542 11926 VEC (symbolp) *template_args = NULL;
c767944b 11927 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11928
11929 memset (&fi, 0, sizeof (struct field_info));
11930
639d11d3 11931 child_die = die->child;
c906108c
SS
11932
11933 while (child_die && child_die->tag)
11934 {
a9a9bd0f
DC
11935 if (child_die->tag == DW_TAG_member
11936 || child_die->tag == DW_TAG_variable)
c906108c 11937 {
a9a9bd0f
DC
11938 /* NOTE: carlton/2002-11-05: A C++ static data member
11939 should be a DW_TAG_member that is a declaration, but
11940 all versions of G++ as of this writing (so through at
11941 least 3.2.1) incorrectly generate DW_TAG_variable
11942 tags for them instead. */
e7c27a73 11943 dwarf2_add_field (&fi, child_die, cu);
c906108c 11944 }
8713b1b1 11945 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11946 {
0963b4bd 11947 /* C++ member function. */
e7c27a73 11948 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11949 }
11950 else if (child_die->tag == DW_TAG_inheritance)
11951 {
11952 /* C++ base class field. */
e7c27a73 11953 dwarf2_add_field (&fi, child_die, cu);
c906108c 11954 }
98751a41
JK
11955 else if (child_die->tag == DW_TAG_typedef)
11956 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11957 else if (child_die->tag == DW_TAG_template_type_param
11958 || child_die->tag == DW_TAG_template_value_param)
11959 {
11960 struct symbol *arg = new_symbol (child_die, NULL, cu);
11961
f1078f66
DJ
11962 if (arg != NULL)
11963 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11964 }
11965
c906108c
SS
11966 child_die = sibling_die (child_die);
11967 }
11968
34eaf542
TT
11969 /* Attach template arguments to type. */
11970 if (! VEC_empty (symbolp, template_args))
11971 {
11972 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11973 TYPE_N_TEMPLATE_ARGUMENTS (type)
11974 = VEC_length (symbolp, template_args);
11975 TYPE_TEMPLATE_ARGUMENTS (type)
11976 = obstack_alloc (&objfile->objfile_obstack,
11977 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11978 * sizeof (struct symbol *)));
11979 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11980 VEC_address (symbolp, template_args),
11981 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11982 * sizeof (struct symbol *)));
11983 VEC_free (symbolp, template_args);
11984 }
11985
c906108c
SS
11986 /* Attach fields and member functions to the type. */
11987 if (fi.nfields)
e7c27a73 11988 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
11989 if (fi.nfnfields)
11990 {
e7c27a73 11991 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 11992
c5aa993b 11993 /* Get the type which refers to the base class (possibly this
c906108c 11994 class itself) which contains the vtable pointer for the current
0d564a31
DJ
11995 class from the DW_AT_containing_type attribute. This use of
11996 DW_AT_containing_type is a GNU extension. */
c906108c 11997
e142c38c 11998 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 11999 {
e7c27a73 12000 struct type *t = die_containing_type (die, cu);
c906108c
SS
12001
12002 TYPE_VPTR_BASETYPE (type) = t;
12003 if (type == t)
12004 {
c906108c
SS
12005 int i;
12006
12007 /* Our own class provides vtbl ptr. */
12008 for (i = TYPE_NFIELDS (t) - 1;
12009 i >= TYPE_N_BASECLASSES (t);
12010 --i)
12011 {
0d5cff50 12012 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 12013
1168df01 12014 if (is_vtable_name (fieldname, cu))
c906108c
SS
12015 {
12016 TYPE_VPTR_FIELDNO (type) = i;
12017 break;
12018 }
12019 }
12020
12021 /* Complain if virtual function table field not found. */
12022 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12023 complaint (&symfile_complaints,
3e43a32a
MS
12024 _("virtual function table pointer "
12025 "not found when defining class '%s'"),
4d3c2250
KB
12026 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12027 "");
c906108c
SS
12028 }
12029 else
12030 {
12031 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12032 }
12033 }
f6235d4c
EZ
12034 else if (cu->producer
12035 && strncmp (cu->producer,
12036 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12037 {
12038 /* The IBM XLC compiler does not provide direct indication
12039 of the containing type, but the vtable pointer is
12040 always named __vfp. */
12041
12042 int i;
12043
12044 for (i = TYPE_NFIELDS (type) - 1;
12045 i >= TYPE_N_BASECLASSES (type);
12046 --i)
12047 {
12048 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12049 {
12050 TYPE_VPTR_FIELDNO (type) = i;
12051 TYPE_VPTR_BASETYPE (type) = type;
12052 break;
12053 }
12054 }
12055 }
c906108c 12056 }
98751a41
JK
12057
12058 /* Copy fi.typedef_field_list linked list elements content into the
12059 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12060 if (fi.typedef_field_list)
12061 {
12062 int i = fi.typedef_field_list_count;
12063
a0d7a4ff 12064 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12065 TYPE_TYPEDEF_FIELD_ARRAY (type)
12066 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12067 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12068
12069 /* Reverse the list order to keep the debug info elements order. */
12070 while (--i >= 0)
12071 {
12072 struct typedef_field *dest, *src;
6e70227d 12073
98751a41
JK
12074 dest = &TYPE_TYPEDEF_FIELD (type, i);
12075 src = &fi.typedef_field_list->field;
12076 fi.typedef_field_list = fi.typedef_field_list->next;
12077 *dest = *src;
12078 }
12079 }
c767944b
DJ
12080
12081 do_cleanups (back_to);
eb2a6f42
TT
12082
12083 if (HAVE_CPLUS_STRUCT (type))
12084 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12085 }
63d06c5c 12086
bb5ed363 12087 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12088
90aeadfc
DC
12089 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12090 snapshots) has been known to create a die giving a declaration
12091 for a class that has, as a child, a die giving a definition for a
12092 nested class. So we have to process our children even if the
12093 current die is a declaration. Normally, of course, a declaration
12094 won't have any children at all. */
134d01f1 12095
90aeadfc
DC
12096 while (child_die != NULL && child_die->tag)
12097 {
12098 if (child_die->tag == DW_TAG_member
12099 || child_die->tag == DW_TAG_variable
34eaf542
TT
12100 || child_die->tag == DW_TAG_inheritance
12101 || child_die->tag == DW_TAG_template_value_param
12102 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12103 {
90aeadfc 12104 /* Do nothing. */
134d01f1 12105 }
90aeadfc
DC
12106 else
12107 process_die (child_die, cu);
134d01f1 12108
90aeadfc 12109 child_die = sibling_die (child_die);
134d01f1
DJ
12110 }
12111
fa4028e9
JB
12112 /* Do not consider external references. According to the DWARF standard,
12113 these DIEs are identified by the fact that they have no byte_size
12114 attribute, and a declaration attribute. */
12115 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12116 || !die_is_declaration (die, cu))
c767944b 12117 new_symbol (die, type, cu);
134d01f1
DJ
12118}
12119
12120/* Given a DW_AT_enumeration_type die, set its type. We do not
12121 complete the type's fields yet, or create any symbols. */
c906108c 12122
f792889a 12123static struct type *
134d01f1 12124read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12125{
e7c27a73 12126 struct objfile *objfile = cu->objfile;
c906108c 12127 struct type *type;
c906108c 12128 struct attribute *attr;
0114d602 12129 const char *name;
134d01f1 12130
348e048f
DE
12131 /* If the definition of this type lives in .debug_types, read that type.
12132 Don't follow DW_AT_specification though, that will take us back up
12133 the chain and we want to go down. */
45e58e77 12134 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12135 if (attr)
12136 {
ac9ec31b 12137 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12138
ac9ec31b 12139 /* The type's CU may not be the same as CU.
02142a6c 12140 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12141 return set_die_type (die, type, cu);
12142 }
12143
c906108c
SS
12144 type = alloc_type (objfile);
12145
12146 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12147 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12148 if (name != NULL)
7d455152 12149 TYPE_TAG_NAME (type) = name;
c906108c 12150
e142c38c 12151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12152 if (attr)
12153 {
12154 TYPE_LENGTH (type) = DW_UNSND (attr);
12155 }
12156 else
12157 {
12158 TYPE_LENGTH (type) = 0;
12159 }
12160
137033e9
JB
12161 /* The enumeration DIE can be incomplete. In Ada, any type can be
12162 declared as private in the package spec, and then defined only
12163 inside the package body. Such types are known as Taft Amendment
12164 Types. When another package uses such a type, an incomplete DIE
12165 may be generated by the compiler. */
02eb380e 12166 if (die_is_declaration (die, cu))
876cecd0 12167 TYPE_STUB (type) = 1;
02eb380e 12168
f792889a 12169 return set_die_type (die, type, cu);
134d01f1
DJ
12170}
12171
12172/* Given a pointer to a die which begins an enumeration, process all
12173 the dies that define the members of the enumeration, and create the
12174 symbol for the enumeration type.
12175
12176 NOTE: We reverse the order of the element list. */
12177
12178static void
12179process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12180{
f792889a 12181 struct type *this_type;
134d01f1 12182
f792889a
DJ
12183 this_type = get_die_type (die, cu);
12184 if (this_type == NULL)
12185 this_type = read_enumeration_type (die, cu);
9dc481d3 12186
639d11d3 12187 if (die->child != NULL)
c906108c 12188 {
9dc481d3
DE
12189 struct die_info *child_die;
12190 struct symbol *sym;
12191 struct field *fields = NULL;
12192 int num_fields = 0;
12193 int unsigned_enum = 1;
15d034d0 12194 const char *name;
cafec441
TT
12195 int flag_enum = 1;
12196 ULONGEST mask = 0;
9dc481d3 12197
639d11d3 12198 child_die = die->child;
c906108c
SS
12199 while (child_die && child_die->tag)
12200 {
12201 if (child_die->tag != DW_TAG_enumerator)
12202 {
e7c27a73 12203 process_die (child_die, cu);
c906108c
SS
12204 }
12205 else
12206 {
39cbfefa
DJ
12207 name = dwarf2_name (child_die, cu);
12208 if (name)
c906108c 12209 {
f792889a 12210 sym = new_symbol (child_die, this_type, cu);
c906108c 12211 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12212 {
12213 unsigned_enum = 0;
12214 flag_enum = 0;
12215 }
12216 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12217 flag_enum = 0;
12218 else
12219 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12220
12221 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12222 {
12223 fields = (struct field *)
12224 xrealloc (fields,
12225 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12226 * sizeof (struct field));
c906108c
SS
12227 }
12228
3567439c 12229 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12230 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12231 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12232 FIELD_BITSIZE (fields[num_fields]) = 0;
12233
12234 num_fields++;
12235 }
12236 }
12237
12238 child_die = sibling_die (child_die);
12239 }
12240
12241 if (num_fields)
12242 {
f792889a
DJ
12243 TYPE_NFIELDS (this_type) = num_fields;
12244 TYPE_FIELDS (this_type) = (struct field *)
12245 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12246 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12247 sizeof (struct field) * num_fields);
b8c9b27d 12248 xfree (fields);
c906108c
SS
12249 }
12250 if (unsigned_enum)
876cecd0 12251 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12252 if (flag_enum)
12253 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12254 }
134d01f1 12255
6c83ed52
TT
12256 /* If we are reading an enum from a .debug_types unit, and the enum
12257 is a declaration, and the enum is not the signatured type in the
12258 unit, then we do not want to add a symbol for it. Adding a
12259 symbol would in some cases obscure the true definition of the
12260 enum, giving users an incomplete type when the definition is
12261 actually available. Note that we do not want to do this for all
12262 enums which are just declarations, because C++0x allows forward
12263 enum declarations. */
3019eac3 12264 if (cu->per_cu->is_debug_types
6c83ed52
TT
12265 && die_is_declaration (die, cu))
12266 {
52dc124a 12267 struct signatured_type *sig_type;
6c83ed52 12268
c0f78cd4 12269 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12270 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12271 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12272 return;
12273 }
12274
f792889a 12275 new_symbol (die, this_type, cu);
c906108c
SS
12276}
12277
12278/* Extract all information from a DW_TAG_array_type DIE and put it in
12279 the DIE's type field. For now, this only handles one dimensional
12280 arrays. */
12281
f792889a 12282static struct type *
e7c27a73 12283read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12284{
e7c27a73 12285 struct objfile *objfile = cu->objfile;
c906108c 12286 struct die_info *child_die;
7e314c57 12287 struct type *type;
c906108c
SS
12288 struct type *element_type, *range_type, *index_type;
12289 struct type **range_types = NULL;
12290 struct attribute *attr;
12291 int ndim = 0;
12292 struct cleanup *back_to;
15d034d0 12293 const char *name;
c906108c 12294
e7c27a73 12295 element_type = die_type (die, cu);
c906108c 12296
7e314c57
JK
12297 /* The die_type call above may have already set the type for this DIE. */
12298 type = get_die_type (die, cu);
12299 if (type)
12300 return type;
12301
c906108c
SS
12302 /* Irix 6.2 native cc creates array types without children for
12303 arrays with unspecified length. */
639d11d3 12304 if (die->child == NULL)
c906108c 12305 {
46bf5051 12306 index_type = objfile_type (objfile)->builtin_int;
c906108c 12307 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12308 type = create_array_type (NULL, element_type, range_type);
12309 return set_die_type (die, type, cu);
c906108c
SS
12310 }
12311
12312 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12313 child_die = die->child;
c906108c
SS
12314 while (child_die && child_die->tag)
12315 {
12316 if (child_die->tag == DW_TAG_subrange_type)
12317 {
f792889a 12318 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12319
f792889a 12320 if (child_type != NULL)
a02abb62 12321 {
0963b4bd
MS
12322 /* The range type was succesfully read. Save it for the
12323 array type creation. */
a02abb62
JB
12324 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12325 {
12326 range_types = (struct type **)
12327 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12328 * sizeof (struct type *));
12329 if (ndim == 0)
12330 make_cleanup (free_current_contents, &range_types);
12331 }
f792889a 12332 range_types[ndim++] = child_type;
a02abb62 12333 }
c906108c
SS
12334 }
12335 child_die = sibling_die (child_die);
12336 }
12337
12338 /* Dwarf2 dimensions are output from left to right, create the
12339 necessary array types in backwards order. */
7ca2d3a3 12340
c906108c 12341 type = element_type;
7ca2d3a3
DL
12342
12343 if (read_array_order (die, cu) == DW_ORD_col_major)
12344 {
12345 int i = 0;
9a619af0 12346
7ca2d3a3
DL
12347 while (i < ndim)
12348 type = create_array_type (NULL, type, range_types[i++]);
12349 }
12350 else
12351 {
12352 while (ndim-- > 0)
12353 type = create_array_type (NULL, type, range_types[ndim]);
12354 }
c906108c 12355
f5f8a009
EZ
12356 /* Understand Dwarf2 support for vector types (like they occur on
12357 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12358 array type. This is not part of the Dwarf2/3 standard yet, but a
12359 custom vendor extension. The main difference between a regular
12360 array and the vector variant is that vectors are passed by value
12361 to functions. */
e142c38c 12362 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12363 if (attr)
ea37ba09 12364 make_vector_type (type);
f5f8a009 12365
dbc98a8b
KW
12366 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12367 implementation may choose to implement triple vectors using this
12368 attribute. */
12369 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12370 if (attr)
12371 {
12372 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12373 TYPE_LENGTH (type) = DW_UNSND (attr);
12374 else
3e43a32a
MS
12375 complaint (&symfile_complaints,
12376 _("DW_AT_byte_size for array type smaller "
12377 "than the total size of elements"));
dbc98a8b
KW
12378 }
12379
39cbfefa
DJ
12380 name = dwarf2_name (die, cu);
12381 if (name)
12382 TYPE_NAME (type) = name;
6e70227d 12383
0963b4bd 12384 /* Install the type in the die. */
7e314c57
JK
12385 set_die_type (die, type, cu);
12386
12387 /* set_die_type should be already done. */
b4ba55a1
JB
12388 set_descriptive_type (type, die, cu);
12389
c906108c
SS
12390 do_cleanups (back_to);
12391
7e314c57 12392 return type;
c906108c
SS
12393}
12394
7ca2d3a3 12395static enum dwarf_array_dim_ordering
6e70227d 12396read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12397{
12398 struct attribute *attr;
12399
12400 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12401
12402 if (attr) return DW_SND (attr);
12403
0963b4bd
MS
12404 /* GNU F77 is a special case, as at 08/2004 array type info is the
12405 opposite order to the dwarf2 specification, but data is still
12406 laid out as per normal fortran.
7ca2d3a3 12407
0963b4bd
MS
12408 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12409 version checking. */
7ca2d3a3 12410
905e0470
PM
12411 if (cu->language == language_fortran
12412 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12413 {
12414 return DW_ORD_row_major;
12415 }
12416
6e70227d 12417 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12418 {
12419 case array_column_major:
12420 return DW_ORD_col_major;
12421 case array_row_major:
12422 default:
12423 return DW_ORD_row_major;
12424 };
12425}
12426
72019c9c 12427/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12428 the DIE's type field. */
72019c9c 12429
f792889a 12430static struct type *
72019c9c
GM
12431read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12432{
7e314c57
JK
12433 struct type *domain_type, *set_type;
12434 struct attribute *attr;
f792889a 12435
7e314c57
JK
12436 domain_type = die_type (die, cu);
12437
12438 /* The die_type call above may have already set the type for this DIE. */
12439 set_type = get_die_type (die, cu);
12440 if (set_type)
12441 return set_type;
12442
12443 set_type = create_set_type (NULL, domain_type);
12444
12445 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12446 if (attr)
12447 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12448
f792889a 12449 return set_die_type (die, set_type, cu);
72019c9c 12450}
7ca2d3a3 12451
0971de02
TT
12452/* A helper for read_common_block that creates a locexpr baton.
12453 SYM is the symbol which we are marking as computed.
12454 COMMON_DIE is the DIE for the common block.
12455 COMMON_LOC is the location expression attribute for the common
12456 block itself.
12457 MEMBER_LOC is the location expression attribute for the particular
12458 member of the common block that we are processing.
12459 CU is the CU from which the above come. */
12460
12461static void
12462mark_common_block_symbol_computed (struct symbol *sym,
12463 struct die_info *common_die,
12464 struct attribute *common_loc,
12465 struct attribute *member_loc,
12466 struct dwarf2_cu *cu)
12467{
12468 struct objfile *objfile = dwarf2_per_objfile->objfile;
12469 struct dwarf2_locexpr_baton *baton;
12470 gdb_byte *ptr;
12471 unsigned int cu_off;
12472 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12473 LONGEST offset = 0;
12474
12475 gdb_assert (common_loc && member_loc);
12476 gdb_assert (attr_form_is_block (common_loc));
12477 gdb_assert (attr_form_is_block (member_loc)
12478 || attr_form_is_constant (member_loc));
12479
12480 baton = obstack_alloc (&objfile->objfile_obstack,
12481 sizeof (struct dwarf2_locexpr_baton));
12482 baton->per_cu = cu->per_cu;
12483 gdb_assert (baton->per_cu);
12484
12485 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12486
12487 if (attr_form_is_constant (member_loc))
12488 {
12489 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12490 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12491 }
12492 else
12493 baton->size += DW_BLOCK (member_loc)->size;
12494
12495 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12496 baton->data = ptr;
12497
12498 *ptr++ = DW_OP_call4;
12499 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12500 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12501 ptr += 4;
12502
12503 if (attr_form_is_constant (member_loc))
12504 {
12505 *ptr++ = DW_OP_addr;
12506 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12507 ptr += cu->header.addr_size;
12508 }
12509 else
12510 {
12511 /* We have to copy the data here, because DW_OP_call4 will only
12512 use a DW_AT_location attribute. */
12513 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12514 ptr += DW_BLOCK (member_loc)->size;
12515 }
12516
12517 *ptr++ = DW_OP_plus;
12518 gdb_assert (ptr - baton->data == baton->size);
12519
0971de02 12520 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12521 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12522}
12523
4357ac6c
TT
12524/* Create appropriate locally-scoped variables for all the
12525 DW_TAG_common_block entries. Also create a struct common_block
12526 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12527 is used to sepate the common blocks name namespace from regular
12528 variable names. */
c906108c
SS
12529
12530static void
e7c27a73 12531read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12532{
0971de02
TT
12533 struct attribute *attr;
12534
12535 attr = dwarf2_attr (die, DW_AT_location, cu);
12536 if (attr)
12537 {
12538 /* Support the .debug_loc offsets. */
12539 if (attr_form_is_block (attr))
12540 {
12541 /* Ok. */
12542 }
12543 else if (attr_form_is_section_offset (attr))
12544 {
12545 dwarf2_complex_location_expr_complaint ();
12546 attr = NULL;
12547 }
12548 else
12549 {
12550 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12551 "common block member");
12552 attr = NULL;
12553 }
12554 }
12555
639d11d3 12556 if (die->child != NULL)
c906108c 12557 {
4357ac6c
TT
12558 struct objfile *objfile = cu->objfile;
12559 struct die_info *child_die;
12560 size_t n_entries = 0, size;
12561 struct common_block *common_block;
12562 struct symbol *sym;
74ac6d43 12563
4357ac6c
TT
12564 for (child_die = die->child;
12565 child_die && child_die->tag;
12566 child_die = sibling_die (child_die))
12567 ++n_entries;
12568
12569 size = (sizeof (struct common_block)
12570 + (n_entries - 1) * sizeof (struct symbol *));
12571 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12572 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12573 common_block->n_entries = 0;
12574
12575 for (child_die = die->child;
12576 child_die && child_die->tag;
12577 child_die = sibling_die (child_die))
12578 {
12579 /* Create the symbol in the DW_TAG_common_block block in the current
12580 symbol scope. */
e7c27a73 12581 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12582 if (sym != NULL)
12583 {
12584 struct attribute *member_loc;
12585
12586 common_block->contents[common_block->n_entries++] = sym;
12587
12588 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12589 cu);
12590 if (member_loc)
12591 {
12592 /* GDB has handled this for a long time, but it is
12593 not specified by DWARF. It seems to have been
12594 emitted by gfortran at least as recently as:
12595 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12596 complaint (&symfile_complaints,
12597 _("Variable in common block has "
12598 "DW_AT_data_member_location "
12599 "- DIE at 0x%x [in module %s]"),
12600 child_die->offset.sect_off, cu->objfile->name);
12601
12602 if (attr_form_is_section_offset (member_loc))
12603 dwarf2_complex_location_expr_complaint ();
12604 else if (attr_form_is_constant (member_loc)
12605 || attr_form_is_block (member_loc))
12606 {
12607 if (attr)
12608 mark_common_block_symbol_computed (sym, die, attr,
12609 member_loc, cu);
12610 }
12611 else
12612 dwarf2_complex_location_expr_complaint ();
12613 }
12614 }
c906108c 12615 }
4357ac6c
TT
12616
12617 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12618 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12619 }
12620}
12621
0114d602 12622/* Create a type for a C++ namespace. */
d9fa45fe 12623
0114d602
DJ
12624static struct type *
12625read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12626{
e7c27a73 12627 struct objfile *objfile = cu->objfile;
0114d602 12628 const char *previous_prefix, *name;
9219021c 12629 int is_anonymous;
0114d602
DJ
12630 struct type *type;
12631
12632 /* For extensions, reuse the type of the original namespace. */
12633 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12634 {
12635 struct die_info *ext_die;
12636 struct dwarf2_cu *ext_cu = cu;
9a619af0 12637
0114d602
DJ
12638 ext_die = dwarf2_extension (die, &ext_cu);
12639 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12640
12641 /* EXT_CU may not be the same as CU.
02142a6c 12642 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12643 return set_die_type (die, type, cu);
12644 }
9219021c 12645
e142c38c 12646 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12647
12648 /* Now build the name of the current namespace. */
12649
0114d602
DJ
12650 previous_prefix = determine_prefix (die, cu);
12651 if (previous_prefix[0] != '\0')
12652 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12653 previous_prefix, name, 0, cu);
0114d602
DJ
12654
12655 /* Create the type. */
12656 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12657 objfile);
abee88f2 12658 TYPE_NAME (type) = name;
0114d602
DJ
12659 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12660
60531b24 12661 return set_die_type (die, type, cu);
0114d602
DJ
12662}
12663
12664/* Read a C++ namespace. */
12665
12666static void
12667read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12668{
12669 struct objfile *objfile = cu->objfile;
0114d602 12670 int is_anonymous;
9219021c 12671
5c4e30ca
DC
12672 /* Add a symbol associated to this if we haven't seen the namespace
12673 before. Also, add a using directive if it's an anonymous
12674 namespace. */
9219021c 12675
f2f0e013 12676 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12677 {
12678 struct type *type;
12679
0114d602 12680 type = read_type_die (die, cu);
e7c27a73 12681 new_symbol (die, type, cu);
5c4e30ca 12682
e8e80198 12683 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12684 if (is_anonymous)
0114d602
DJ
12685 {
12686 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12687
c0cc3a76 12688 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12689 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12690 }
5c4e30ca 12691 }
9219021c 12692
639d11d3 12693 if (die->child != NULL)
d9fa45fe 12694 {
639d11d3 12695 struct die_info *child_die = die->child;
6e70227d 12696
d9fa45fe
DC
12697 while (child_die && child_die->tag)
12698 {
e7c27a73 12699 process_die (child_die, cu);
d9fa45fe
DC
12700 child_die = sibling_die (child_die);
12701 }
12702 }
38d518c9
EZ
12703}
12704
f55ee35c
JK
12705/* Read a Fortran module as type. This DIE can be only a declaration used for
12706 imported module. Still we need that type as local Fortran "use ... only"
12707 declaration imports depend on the created type in determine_prefix. */
12708
12709static struct type *
12710read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12711{
12712 struct objfile *objfile = cu->objfile;
15d034d0 12713 const char *module_name;
f55ee35c
JK
12714 struct type *type;
12715
12716 module_name = dwarf2_name (die, cu);
12717 if (!module_name)
3e43a32a
MS
12718 complaint (&symfile_complaints,
12719 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12720 die->offset.sect_off);
f55ee35c
JK
12721 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12722
12723 /* determine_prefix uses TYPE_TAG_NAME. */
12724 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12725
12726 return set_die_type (die, type, cu);
12727}
12728
5d7cb8df
JK
12729/* Read a Fortran module. */
12730
12731static void
12732read_module (struct die_info *die, struct dwarf2_cu *cu)
12733{
12734 struct die_info *child_die = die->child;
12735
5d7cb8df
JK
12736 while (child_die && child_die->tag)
12737 {
12738 process_die (child_die, cu);
12739 child_die = sibling_die (child_die);
12740 }
12741}
12742
38d518c9
EZ
12743/* Return the name of the namespace represented by DIE. Set
12744 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12745 namespace. */
12746
12747static const char *
e142c38c 12748namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12749{
12750 struct die_info *current_die;
12751 const char *name = NULL;
12752
12753 /* Loop through the extensions until we find a name. */
12754
12755 for (current_die = die;
12756 current_die != NULL;
f2f0e013 12757 current_die = dwarf2_extension (die, &cu))
38d518c9 12758 {
e142c38c 12759 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12760 if (name != NULL)
12761 break;
12762 }
12763
12764 /* Is it an anonymous namespace? */
12765
12766 *is_anonymous = (name == NULL);
12767 if (*is_anonymous)
2b1dbab0 12768 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12769
12770 return name;
d9fa45fe
DC
12771}
12772
c906108c
SS
12773/* Extract all information from a DW_TAG_pointer_type DIE and add to
12774 the user defined type vector. */
12775
f792889a 12776static struct type *
e7c27a73 12777read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12778{
5e2b427d 12779 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12780 struct comp_unit_head *cu_header = &cu->header;
c906108c 12781 struct type *type;
8b2dbe47
KB
12782 struct attribute *attr_byte_size;
12783 struct attribute *attr_address_class;
12784 int byte_size, addr_class;
7e314c57
JK
12785 struct type *target_type;
12786
12787 target_type = die_type (die, cu);
c906108c 12788
7e314c57
JK
12789 /* The die_type call above may have already set the type for this DIE. */
12790 type = get_die_type (die, cu);
12791 if (type)
12792 return type;
12793
12794 type = lookup_pointer_type (target_type);
8b2dbe47 12795
e142c38c 12796 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12797 if (attr_byte_size)
12798 byte_size = DW_UNSND (attr_byte_size);
c906108c 12799 else
8b2dbe47
KB
12800 byte_size = cu_header->addr_size;
12801
e142c38c 12802 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12803 if (attr_address_class)
12804 addr_class = DW_UNSND (attr_address_class);
12805 else
12806 addr_class = DW_ADDR_none;
12807
12808 /* If the pointer size or address class is different than the
12809 default, create a type variant marked as such and set the
12810 length accordingly. */
12811 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12812 {
5e2b427d 12813 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12814 {
12815 int type_flags;
12816
849957d9 12817 type_flags = gdbarch_address_class_type_flags
5e2b427d 12818 (gdbarch, byte_size, addr_class);
876cecd0
TT
12819 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12820 == 0);
8b2dbe47
KB
12821 type = make_type_with_address_space (type, type_flags);
12822 }
12823 else if (TYPE_LENGTH (type) != byte_size)
12824 {
3e43a32a
MS
12825 complaint (&symfile_complaints,
12826 _("invalid pointer size %d"), byte_size);
8b2dbe47 12827 }
6e70227d 12828 else
9a619af0
MS
12829 {
12830 /* Should we also complain about unhandled address classes? */
12831 }
c906108c 12832 }
8b2dbe47
KB
12833
12834 TYPE_LENGTH (type) = byte_size;
f792889a 12835 return set_die_type (die, type, cu);
c906108c
SS
12836}
12837
12838/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12839 the user defined type vector. */
12840
f792889a 12841static struct type *
e7c27a73 12842read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12843{
12844 struct type *type;
12845 struct type *to_type;
12846 struct type *domain;
12847
e7c27a73
DJ
12848 to_type = die_type (die, cu);
12849 domain = die_containing_type (die, cu);
0d5de010 12850
7e314c57
JK
12851 /* The calls above may have already set the type for this DIE. */
12852 type = get_die_type (die, cu);
12853 if (type)
12854 return type;
12855
0d5de010
DJ
12856 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12857 type = lookup_methodptr_type (to_type);
7078baeb
TT
12858 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12859 {
12860 struct type *new_type = alloc_type (cu->objfile);
12861
12862 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12863 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12864 TYPE_VARARGS (to_type));
12865 type = lookup_methodptr_type (new_type);
12866 }
0d5de010
DJ
12867 else
12868 type = lookup_memberptr_type (to_type, domain);
c906108c 12869
f792889a 12870 return set_die_type (die, type, cu);
c906108c
SS
12871}
12872
12873/* Extract all information from a DW_TAG_reference_type DIE and add to
12874 the user defined type vector. */
12875
f792889a 12876static struct type *
e7c27a73 12877read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12878{
e7c27a73 12879 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12880 struct type *type, *target_type;
c906108c
SS
12881 struct attribute *attr;
12882
7e314c57
JK
12883 target_type = die_type (die, cu);
12884
12885 /* The die_type call above may have already set the type for this DIE. */
12886 type = get_die_type (die, cu);
12887 if (type)
12888 return type;
12889
12890 type = lookup_reference_type (target_type);
e142c38c 12891 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12892 if (attr)
12893 {
12894 TYPE_LENGTH (type) = DW_UNSND (attr);
12895 }
12896 else
12897 {
107d2387 12898 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12899 }
f792889a 12900 return set_die_type (die, type, cu);
c906108c
SS
12901}
12902
f792889a 12903static struct type *
e7c27a73 12904read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12905{
f792889a 12906 struct type *base_type, *cv_type;
c906108c 12907
e7c27a73 12908 base_type = die_type (die, cu);
7e314c57
JK
12909
12910 /* The die_type call above may have already set the type for this DIE. */
12911 cv_type = get_die_type (die, cu);
12912 if (cv_type)
12913 return cv_type;
12914
2f608a3a
KW
12915 /* In case the const qualifier is applied to an array type, the element type
12916 is so qualified, not the array type (section 6.7.3 of C99). */
12917 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12918 {
12919 struct type *el_type, *inner_array;
12920
12921 base_type = copy_type (base_type);
12922 inner_array = base_type;
12923
12924 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12925 {
12926 TYPE_TARGET_TYPE (inner_array) =
12927 copy_type (TYPE_TARGET_TYPE (inner_array));
12928 inner_array = TYPE_TARGET_TYPE (inner_array);
12929 }
12930
12931 el_type = TYPE_TARGET_TYPE (inner_array);
12932 TYPE_TARGET_TYPE (inner_array) =
12933 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12934
12935 return set_die_type (die, base_type, cu);
12936 }
12937
f792889a
DJ
12938 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12939 return set_die_type (die, cv_type, cu);
c906108c
SS
12940}
12941
f792889a 12942static struct type *
e7c27a73 12943read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12944{
f792889a 12945 struct type *base_type, *cv_type;
c906108c 12946
e7c27a73 12947 base_type = die_type (die, cu);
7e314c57
JK
12948
12949 /* The die_type call above may have already set the type for this DIE. */
12950 cv_type = get_die_type (die, cu);
12951 if (cv_type)
12952 return cv_type;
12953
f792889a
DJ
12954 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12955 return set_die_type (die, cv_type, cu);
c906108c
SS
12956}
12957
06d66ee9
TT
12958/* Handle DW_TAG_restrict_type. */
12959
12960static struct type *
12961read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12962{
12963 struct type *base_type, *cv_type;
12964
12965 base_type = die_type (die, cu);
12966
12967 /* The die_type call above may have already set the type for this DIE. */
12968 cv_type = get_die_type (die, cu);
12969 if (cv_type)
12970 return cv_type;
12971
12972 cv_type = make_restrict_type (base_type);
12973 return set_die_type (die, cv_type, cu);
12974}
12975
c906108c
SS
12976/* Extract all information from a DW_TAG_string_type DIE and add to
12977 the user defined type vector. It isn't really a user defined type,
12978 but it behaves like one, with other DIE's using an AT_user_def_type
12979 attribute to reference it. */
12980
f792889a 12981static struct type *
e7c27a73 12982read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12983{
e7c27a73 12984 struct objfile *objfile = cu->objfile;
3b7538c0 12985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12986 struct type *type, *range_type, *index_type, *char_type;
12987 struct attribute *attr;
12988 unsigned int length;
12989
e142c38c 12990 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
12991 if (attr)
12992 {
12993 length = DW_UNSND (attr);
12994 }
12995 else
12996 {
0963b4bd 12997 /* Check for the DW_AT_byte_size attribute. */
e142c38c 12998 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
12999 if (attr)
13000 {
13001 length = DW_UNSND (attr);
13002 }
13003 else
13004 {
13005 length = 1;
13006 }
c906108c 13007 }
6ccb9162 13008
46bf5051 13009 index_type = objfile_type (objfile)->builtin_int;
c906108c 13010 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
13011 char_type = language_string_char_type (cu->language_defn, gdbarch);
13012 type = create_string_type (NULL, char_type, range_type);
6ccb9162 13013
f792889a 13014 return set_die_type (die, type, cu);
c906108c
SS
13015}
13016
4d804846
JB
13017/* Assuming that DIE corresponds to a function, returns nonzero
13018 if the function is prototyped. */
13019
13020static int
13021prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13022{
13023 struct attribute *attr;
13024
13025 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13026 if (attr && (DW_UNSND (attr) != 0))
13027 return 1;
13028
13029 /* The DWARF standard implies that the DW_AT_prototyped attribute
13030 is only meaninful for C, but the concept also extends to other
13031 languages that allow unprototyped functions (Eg: Objective C).
13032 For all other languages, assume that functions are always
13033 prototyped. */
13034 if (cu->language != language_c
13035 && cu->language != language_objc
13036 && cu->language != language_opencl)
13037 return 1;
13038
13039 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13040 prototyped and unprototyped functions; default to prototyped,
13041 since that is more common in modern code (and RealView warns
13042 about unprototyped functions). */
13043 if (producer_is_realview (cu->producer))
13044 return 1;
13045
13046 return 0;
13047}
13048
c906108c
SS
13049/* Handle DIES due to C code like:
13050
13051 struct foo
c5aa993b
JM
13052 {
13053 int (*funcp)(int a, long l);
13054 int b;
13055 };
c906108c 13056
0963b4bd 13057 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13058
f792889a 13059static struct type *
e7c27a73 13060read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13061{
bb5ed363 13062 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13063 struct type *type; /* Type that this function returns. */
13064 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13065 struct attribute *attr;
13066
e7c27a73 13067 type = die_type (die, cu);
7e314c57
JK
13068
13069 /* The die_type call above may have already set the type for this DIE. */
13070 ftype = get_die_type (die, cu);
13071 if (ftype)
13072 return ftype;
13073
0c8b41f1 13074 ftype = lookup_function_type (type);
c906108c 13075
4d804846 13076 if (prototyped_function_p (die, cu))
a6c727b2 13077 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13078
c055b101
CV
13079 /* Store the calling convention in the type if it's available in
13080 the subroutine die. Otherwise set the calling convention to
13081 the default value DW_CC_normal. */
13082 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13083 if (attr)
13084 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13085 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13086 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13087 else
13088 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13089
13090 /* We need to add the subroutine type to the die immediately so
13091 we don't infinitely recurse when dealing with parameters
0963b4bd 13092 declared as the same subroutine type. */
76c10ea2 13093 set_die_type (die, ftype, cu);
6e70227d 13094
639d11d3 13095 if (die->child != NULL)
c906108c 13096 {
bb5ed363 13097 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13098 struct die_info *child_die;
8072405b 13099 int nparams, iparams;
c906108c
SS
13100
13101 /* Count the number of parameters.
13102 FIXME: GDB currently ignores vararg functions, but knows about
13103 vararg member functions. */
8072405b 13104 nparams = 0;
639d11d3 13105 child_die = die->child;
c906108c
SS
13106 while (child_die && child_die->tag)
13107 {
13108 if (child_die->tag == DW_TAG_formal_parameter)
13109 nparams++;
13110 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13111 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13112 child_die = sibling_die (child_die);
13113 }
13114
13115 /* Allocate storage for parameters and fill them in. */
13116 TYPE_NFIELDS (ftype) = nparams;
13117 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13118 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13119
8072405b
JK
13120 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13121 even if we error out during the parameters reading below. */
13122 for (iparams = 0; iparams < nparams; iparams++)
13123 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13124
13125 iparams = 0;
639d11d3 13126 child_die = die->child;
c906108c
SS
13127 while (child_die && child_die->tag)
13128 {
13129 if (child_die->tag == DW_TAG_formal_parameter)
13130 {
3ce3b1ba
PA
13131 struct type *arg_type;
13132
13133 /* DWARF version 2 has no clean way to discern C++
13134 static and non-static member functions. G++ helps
13135 GDB by marking the first parameter for non-static
13136 member functions (which is the this pointer) as
13137 artificial. We pass this information to
13138 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13139
13140 DWARF version 3 added DW_AT_object_pointer, which GCC
13141 4.5 does not yet generate. */
e142c38c 13142 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13143 if (attr)
13144 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13145 else
418835cc
KS
13146 {
13147 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13148
13149 /* GCC/43521: In java, the formal parameter
13150 "this" is sometimes not marked with DW_AT_artificial. */
13151 if (cu->language == language_java)
13152 {
13153 const char *name = dwarf2_name (child_die, cu);
9a619af0 13154
418835cc
KS
13155 if (name && !strcmp (name, "this"))
13156 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13157 }
13158 }
3ce3b1ba
PA
13159 arg_type = die_type (child_die, cu);
13160
13161 /* RealView does not mark THIS as const, which the testsuite
13162 expects. GCC marks THIS as const in method definitions,
13163 but not in the class specifications (GCC PR 43053). */
13164 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13165 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13166 {
13167 int is_this = 0;
13168 struct dwarf2_cu *arg_cu = cu;
13169 const char *name = dwarf2_name (child_die, cu);
13170
13171 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13172 if (attr)
13173 {
13174 /* If the compiler emits this, use it. */
13175 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13176 is_this = 1;
13177 }
13178 else if (name && strcmp (name, "this") == 0)
13179 /* Function definitions will have the argument names. */
13180 is_this = 1;
13181 else if (name == NULL && iparams == 0)
13182 /* Declarations may not have the names, so like
13183 elsewhere in GDB, assume an artificial first
13184 argument is "this". */
13185 is_this = 1;
13186
13187 if (is_this)
13188 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13189 arg_type, 0);
13190 }
13191
13192 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13193 iparams++;
13194 }
13195 child_die = sibling_die (child_die);
13196 }
13197 }
13198
76c10ea2 13199 return ftype;
c906108c
SS
13200}
13201
f792889a 13202static struct type *
e7c27a73 13203read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13204{
e7c27a73 13205 struct objfile *objfile = cu->objfile;
0114d602 13206 const char *name = NULL;
3c8e0968 13207 struct type *this_type, *target_type;
c906108c 13208
94af9270 13209 name = dwarf2_full_name (NULL, die, cu);
f792889a 13210 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13211 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13212 TYPE_NAME (this_type) = name;
f792889a 13213 set_die_type (die, this_type, cu);
3c8e0968
DE
13214 target_type = die_type (die, cu);
13215 if (target_type != this_type)
13216 TYPE_TARGET_TYPE (this_type) = target_type;
13217 else
13218 {
13219 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13220 spec and cause infinite loops in GDB. */
13221 complaint (&symfile_complaints,
13222 _("Self-referential DW_TAG_typedef "
13223 "- DIE at 0x%x [in module %s]"),
b64f50a1 13224 die->offset.sect_off, objfile->name);
3c8e0968
DE
13225 TYPE_TARGET_TYPE (this_type) = NULL;
13226 }
f792889a 13227 return this_type;
c906108c
SS
13228}
13229
13230/* Find a representation of a given base type and install
13231 it in the TYPE field of the die. */
13232
f792889a 13233static struct type *
e7c27a73 13234read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13235{
e7c27a73 13236 struct objfile *objfile = cu->objfile;
c906108c
SS
13237 struct type *type;
13238 struct attribute *attr;
13239 int encoding = 0, size = 0;
15d034d0 13240 const char *name;
6ccb9162
UW
13241 enum type_code code = TYPE_CODE_INT;
13242 int type_flags = 0;
13243 struct type *target_type = NULL;
c906108c 13244
e142c38c 13245 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13246 if (attr)
13247 {
13248 encoding = DW_UNSND (attr);
13249 }
e142c38c 13250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13251 if (attr)
13252 {
13253 size = DW_UNSND (attr);
13254 }
39cbfefa 13255 name = dwarf2_name (die, cu);
6ccb9162 13256 if (!name)
c906108c 13257 {
6ccb9162
UW
13258 complaint (&symfile_complaints,
13259 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13260 }
6ccb9162
UW
13261
13262 switch (encoding)
c906108c 13263 {
6ccb9162
UW
13264 case DW_ATE_address:
13265 /* Turn DW_ATE_address into a void * pointer. */
13266 code = TYPE_CODE_PTR;
13267 type_flags |= TYPE_FLAG_UNSIGNED;
13268 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13269 break;
13270 case DW_ATE_boolean:
13271 code = TYPE_CODE_BOOL;
13272 type_flags |= TYPE_FLAG_UNSIGNED;
13273 break;
13274 case DW_ATE_complex_float:
13275 code = TYPE_CODE_COMPLEX;
13276 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13277 break;
13278 case DW_ATE_decimal_float:
13279 code = TYPE_CODE_DECFLOAT;
13280 break;
13281 case DW_ATE_float:
13282 code = TYPE_CODE_FLT;
13283 break;
13284 case DW_ATE_signed:
13285 break;
13286 case DW_ATE_unsigned:
13287 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13288 if (cu->language == language_fortran
13289 && name
13290 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13291 code = TYPE_CODE_CHAR;
6ccb9162
UW
13292 break;
13293 case DW_ATE_signed_char:
6e70227d 13294 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13295 || cu->language == language_pascal
13296 || cu->language == language_fortran)
6ccb9162
UW
13297 code = TYPE_CODE_CHAR;
13298 break;
13299 case DW_ATE_unsigned_char:
868a0084 13300 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13301 || cu->language == language_pascal
13302 || cu->language == language_fortran)
6ccb9162
UW
13303 code = TYPE_CODE_CHAR;
13304 type_flags |= TYPE_FLAG_UNSIGNED;
13305 break;
75079b2b
TT
13306 case DW_ATE_UTF:
13307 /* We just treat this as an integer and then recognize the
13308 type by name elsewhere. */
13309 break;
13310
6ccb9162
UW
13311 default:
13312 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13313 dwarf_type_encoding_name (encoding));
13314 break;
c906108c 13315 }
6ccb9162 13316
0114d602
DJ
13317 type = init_type (code, size, type_flags, NULL, objfile);
13318 TYPE_NAME (type) = name;
6ccb9162
UW
13319 TYPE_TARGET_TYPE (type) = target_type;
13320
0114d602 13321 if (name && strcmp (name, "char") == 0)
876cecd0 13322 TYPE_NOSIGN (type) = 1;
0114d602 13323
f792889a 13324 return set_die_type (die, type, cu);
c906108c
SS
13325}
13326
a02abb62
JB
13327/* Read the given DW_AT_subrange DIE. */
13328
f792889a 13329static struct type *
a02abb62
JB
13330read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13331{
4c9ad8c2 13332 struct type *base_type, *orig_base_type;
a02abb62
JB
13333 struct type *range_type;
13334 struct attribute *attr;
4fae6e18
JK
13335 LONGEST low, high;
13336 int low_default_is_valid;
15d034d0 13337 const char *name;
43bbcdc2 13338 LONGEST negative_mask;
e77813c8 13339
4c9ad8c2
TT
13340 orig_base_type = die_type (die, cu);
13341 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13342 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13343 creating the range type, but we use the result of check_typedef
13344 when examining properties of the type. */
13345 base_type = check_typedef (orig_base_type);
a02abb62 13346
7e314c57
JK
13347 /* The die_type call above may have already set the type for this DIE. */
13348 range_type = get_die_type (die, cu);
13349 if (range_type)
13350 return range_type;
13351
4fae6e18
JK
13352 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13353 omitting DW_AT_lower_bound. */
13354 switch (cu->language)
6e70227d 13355 {
4fae6e18
JK
13356 case language_c:
13357 case language_cplus:
13358 low = 0;
13359 low_default_is_valid = 1;
13360 break;
13361 case language_fortran:
13362 low = 1;
13363 low_default_is_valid = 1;
13364 break;
13365 case language_d:
13366 case language_java:
13367 case language_objc:
13368 low = 0;
13369 low_default_is_valid = (cu->header.version >= 4);
13370 break;
13371 case language_ada:
13372 case language_m2:
13373 case language_pascal:
a02abb62 13374 low = 1;
4fae6e18
JK
13375 low_default_is_valid = (cu->header.version >= 4);
13376 break;
13377 default:
13378 low = 0;
13379 low_default_is_valid = 0;
13380 break;
a02abb62
JB
13381 }
13382
dd5e6932
DJ
13383 /* FIXME: For variable sized arrays either of these could be
13384 a variable rather than a constant value. We'll allow it,
13385 but we don't know how to handle it. */
e142c38c 13386 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13387 if (attr)
4fae6e18
JK
13388 low = dwarf2_get_attr_constant_value (attr, low);
13389 else if (!low_default_is_valid)
13390 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13391 "- DIE at 0x%x [in module %s]"),
13392 die->offset.sect_off, cu->objfile->name);
a02abb62 13393
e142c38c 13394 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13395 if (attr)
6e70227d 13396 {
7771576e 13397 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13398 {
13399 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13400 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13401 FIXME: GDB does not yet know how to handle dynamic
13402 arrays properly, treat them as arrays with unspecified
13403 length for now.
13404
13405 FIXME: jimb/2003-09-22: GDB does not really know
13406 how to handle arrays of unspecified length
13407 either; we just represent them as zero-length
13408 arrays. Choose an appropriate upper bound given
13409 the lower bound we've computed above. */
13410 high = low - 1;
13411 }
13412 else
13413 high = dwarf2_get_attr_constant_value (attr, 1);
13414 }
e77813c8
PM
13415 else
13416 {
13417 attr = dwarf2_attr (die, DW_AT_count, cu);
13418 if (attr)
13419 {
13420 int count = dwarf2_get_attr_constant_value (attr, 1);
13421 high = low + count - 1;
13422 }
c2ff108b
JK
13423 else
13424 {
13425 /* Unspecified array length. */
13426 high = low - 1;
13427 }
e77813c8
PM
13428 }
13429
13430 /* Dwarf-2 specifications explicitly allows to create subrange types
13431 without specifying a base type.
13432 In that case, the base type must be set to the type of
13433 the lower bound, upper bound or count, in that order, if any of these
13434 three attributes references an object that has a type.
13435 If no base type is found, the Dwarf-2 specifications say that
13436 a signed integer type of size equal to the size of an address should
13437 be used.
13438 For the following C code: `extern char gdb_int [];'
13439 GCC produces an empty range DIE.
13440 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13441 high bound or count are not yet handled by this code. */
e77813c8
PM
13442 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13443 {
13444 struct objfile *objfile = cu->objfile;
13445 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13446 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13447 struct type *int_type = objfile_type (objfile)->builtin_int;
13448
13449 /* Test "int", "long int", and "long long int" objfile types,
13450 and select the first one having a size above or equal to the
13451 architecture address size. */
13452 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13453 base_type = int_type;
13454 else
13455 {
13456 int_type = objfile_type (objfile)->builtin_long;
13457 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13458 base_type = int_type;
13459 else
13460 {
13461 int_type = objfile_type (objfile)->builtin_long_long;
13462 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13463 base_type = int_type;
13464 }
13465 }
13466 }
a02abb62 13467
6e70227d 13468 negative_mask =
43bbcdc2
PH
13469 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13470 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13471 low |= negative_mask;
13472 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13473 high |= negative_mask;
13474
4c9ad8c2 13475 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13476
bbb0eef6
JK
13477 /* Mark arrays with dynamic length at least as an array of unspecified
13478 length. GDB could check the boundary but before it gets implemented at
13479 least allow accessing the array elements. */
d48323d8 13480 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13481 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13482
c2ff108b
JK
13483 /* Ada expects an empty array on no boundary attributes. */
13484 if (attr == NULL && cu->language != language_ada)
13485 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13486
39cbfefa
DJ
13487 name = dwarf2_name (die, cu);
13488 if (name)
13489 TYPE_NAME (range_type) = name;
6e70227d 13490
e142c38c 13491 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13492 if (attr)
13493 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13494
7e314c57
JK
13495 set_die_type (die, range_type, cu);
13496
13497 /* set_die_type should be already done. */
b4ba55a1
JB
13498 set_descriptive_type (range_type, die, cu);
13499
7e314c57 13500 return range_type;
a02abb62 13501}
6e70227d 13502
f792889a 13503static struct type *
81a17f79
JB
13504read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13505{
13506 struct type *type;
81a17f79 13507
81a17f79
JB
13508 /* For now, we only support the C meaning of an unspecified type: void. */
13509
0114d602
DJ
13510 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13511 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13512
f792889a 13513 return set_die_type (die, type, cu);
81a17f79 13514}
a02abb62 13515
639d11d3
DC
13516/* Read a single die and all its descendents. Set the die's sibling
13517 field to NULL; set other fields in the die correctly, and set all
13518 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13519 location of the info_ptr after reading all of those dies. PARENT
13520 is the parent of the die in question. */
13521
13522static struct die_info *
dee91e82 13523read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13524 const gdb_byte *info_ptr,
13525 const gdb_byte **new_info_ptr,
dee91e82 13526 struct die_info *parent)
639d11d3
DC
13527{
13528 struct die_info *die;
d521ce57 13529 const gdb_byte *cur_ptr;
639d11d3
DC
13530 int has_children;
13531
bf6af496 13532 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13533 if (die == NULL)
13534 {
13535 *new_info_ptr = cur_ptr;
13536 return NULL;
13537 }
93311388 13538 store_in_ref_table (die, reader->cu);
639d11d3
DC
13539
13540 if (has_children)
bf6af496 13541 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13542 else
13543 {
13544 die->child = NULL;
13545 *new_info_ptr = cur_ptr;
13546 }
13547
13548 die->sibling = NULL;
13549 die->parent = parent;
13550 return die;
13551}
13552
13553/* Read a die, all of its descendents, and all of its siblings; set
13554 all of the fields of all of the dies correctly. Arguments are as
13555 in read_die_and_children. */
13556
13557static struct die_info *
bf6af496 13558read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13559 const gdb_byte *info_ptr,
13560 const gdb_byte **new_info_ptr,
bf6af496 13561 struct die_info *parent)
639d11d3
DC
13562{
13563 struct die_info *first_die, *last_sibling;
d521ce57 13564 const gdb_byte *cur_ptr;
639d11d3 13565
c906108c 13566 cur_ptr = info_ptr;
639d11d3
DC
13567 first_die = last_sibling = NULL;
13568
13569 while (1)
c906108c 13570 {
639d11d3 13571 struct die_info *die
dee91e82 13572 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13573
1d325ec1 13574 if (die == NULL)
c906108c 13575 {
639d11d3
DC
13576 *new_info_ptr = cur_ptr;
13577 return first_die;
c906108c 13578 }
1d325ec1
DJ
13579
13580 if (!first_die)
13581 first_die = die;
c906108c 13582 else
1d325ec1
DJ
13583 last_sibling->sibling = die;
13584
13585 last_sibling = die;
c906108c 13586 }
c906108c
SS
13587}
13588
bf6af496
DE
13589/* Read a die, all of its descendents, and all of its siblings; set
13590 all of the fields of all of the dies correctly. Arguments are as
13591 in read_die_and_children.
13592 This the main entry point for reading a DIE and all its children. */
13593
13594static struct die_info *
13595read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13596 const gdb_byte *info_ptr,
13597 const gdb_byte **new_info_ptr,
bf6af496
DE
13598 struct die_info *parent)
13599{
13600 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13601 new_info_ptr, parent);
13602
13603 if (dwarf2_die_debug)
13604 {
13605 fprintf_unfiltered (gdb_stdlog,
13606 "Read die from %s@0x%x of %s:\n",
13607 bfd_section_name (reader->abfd,
13608 reader->die_section->asection),
13609 (unsigned) (info_ptr - reader->die_section->buffer),
13610 bfd_get_filename (reader->abfd));
13611 dump_die (die, dwarf2_die_debug);
13612 }
13613
13614 return die;
13615}
13616
3019eac3
DE
13617/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13618 attributes.
13619 The caller is responsible for filling in the extra attributes
13620 and updating (*DIEP)->num_attrs.
13621 Set DIEP to point to a newly allocated die with its information,
13622 except for its child, sibling, and parent fields.
13623 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13624
d521ce57 13625static const gdb_byte *
3019eac3 13626read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13627 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13628 int *has_children, int num_extra_attrs)
93311388 13629{
b64f50a1
JK
13630 unsigned int abbrev_number, bytes_read, i;
13631 sect_offset offset;
93311388
DE
13632 struct abbrev_info *abbrev;
13633 struct die_info *die;
13634 struct dwarf2_cu *cu = reader->cu;
13635 bfd *abfd = reader->abfd;
13636
b64f50a1 13637 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13638 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13639 info_ptr += bytes_read;
13640 if (!abbrev_number)
13641 {
13642 *diep = NULL;
13643 *has_children = 0;
13644 return info_ptr;
13645 }
13646
433df2d4 13647 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13648 if (!abbrev)
348e048f
DE
13649 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13650 abbrev_number,
13651 bfd_get_filename (abfd));
13652
3019eac3 13653 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13654 die->offset = offset;
13655 die->tag = abbrev->tag;
13656 die->abbrev = abbrev_number;
13657
3019eac3
DE
13658 /* Make the result usable.
13659 The caller needs to update num_attrs after adding the extra
13660 attributes. */
93311388
DE
13661 die->num_attrs = abbrev->num_attrs;
13662
13663 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13664 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13665 info_ptr);
93311388
DE
13666
13667 *diep = die;
13668 *has_children = abbrev->has_children;
13669 return info_ptr;
13670}
13671
3019eac3
DE
13672/* Read a die and all its attributes.
13673 Set DIEP to point to a newly allocated die with its information,
13674 except for its child, sibling, and parent fields.
13675 Set HAS_CHILDREN to tell whether the die has children or not. */
13676
d521ce57 13677static const gdb_byte *
3019eac3 13678read_full_die (const struct die_reader_specs *reader,
d521ce57 13679 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13680 int *has_children)
13681{
d521ce57 13682 const gdb_byte *result;
bf6af496
DE
13683
13684 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13685
13686 if (dwarf2_die_debug)
13687 {
13688 fprintf_unfiltered (gdb_stdlog,
13689 "Read die from %s@0x%x of %s:\n",
13690 bfd_section_name (reader->abfd,
13691 reader->die_section->asection),
13692 (unsigned) (info_ptr - reader->die_section->buffer),
13693 bfd_get_filename (reader->abfd));
13694 dump_die (*diep, dwarf2_die_debug);
13695 }
13696
13697 return result;
3019eac3 13698}
433df2d4
DE
13699\f
13700/* Abbreviation tables.
3019eac3 13701
433df2d4 13702 In DWARF version 2, the description of the debugging information is
c906108c
SS
13703 stored in a separate .debug_abbrev section. Before we read any
13704 dies from a section we read in all abbreviations and install them
433df2d4
DE
13705 in a hash table. */
13706
13707/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13708
13709static struct abbrev_info *
13710abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13711{
13712 struct abbrev_info *abbrev;
13713
13714 abbrev = (struct abbrev_info *)
13715 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13716 memset (abbrev, 0, sizeof (struct abbrev_info));
13717 return abbrev;
13718}
13719
13720/* Add an abbreviation to the table. */
c906108c
SS
13721
13722static void
433df2d4
DE
13723abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13724 unsigned int abbrev_number,
13725 struct abbrev_info *abbrev)
13726{
13727 unsigned int hash_number;
13728
13729 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13730 abbrev->next = abbrev_table->abbrevs[hash_number];
13731 abbrev_table->abbrevs[hash_number] = abbrev;
13732}
dee91e82 13733
433df2d4
DE
13734/* Look up an abbrev in the table.
13735 Returns NULL if the abbrev is not found. */
13736
13737static struct abbrev_info *
13738abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13739 unsigned int abbrev_number)
c906108c 13740{
433df2d4
DE
13741 unsigned int hash_number;
13742 struct abbrev_info *abbrev;
13743
13744 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13745 abbrev = abbrev_table->abbrevs[hash_number];
13746
13747 while (abbrev)
13748 {
13749 if (abbrev->number == abbrev_number)
13750 return abbrev;
13751 abbrev = abbrev->next;
13752 }
13753 return NULL;
13754}
13755
13756/* Read in an abbrev table. */
13757
13758static struct abbrev_table *
13759abbrev_table_read_table (struct dwarf2_section_info *section,
13760 sect_offset offset)
13761{
13762 struct objfile *objfile = dwarf2_per_objfile->objfile;
13763 bfd *abfd = section->asection->owner;
13764 struct abbrev_table *abbrev_table;
d521ce57 13765 const gdb_byte *abbrev_ptr;
c906108c
SS
13766 struct abbrev_info *cur_abbrev;
13767 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13768 unsigned int abbrev_form;
f3dd6933
DJ
13769 struct attr_abbrev *cur_attrs;
13770 unsigned int allocated_attrs;
c906108c 13771
433df2d4 13772 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13773 abbrev_table->offset = offset;
433df2d4
DE
13774 obstack_init (&abbrev_table->abbrev_obstack);
13775 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13776 (ABBREV_HASH_SIZE
13777 * sizeof (struct abbrev_info *)));
13778 memset (abbrev_table->abbrevs, 0,
13779 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13780
433df2d4
DE
13781 dwarf2_read_section (objfile, section);
13782 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13783 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13784 abbrev_ptr += bytes_read;
13785
f3dd6933
DJ
13786 allocated_attrs = ATTR_ALLOC_CHUNK;
13787 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13788
0963b4bd 13789 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13790 while (abbrev_number)
13791 {
433df2d4 13792 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13793
13794 /* read in abbrev header */
13795 cur_abbrev->number = abbrev_number;
13796 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13797 abbrev_ptr += bytes_read;
13798 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13799 abbrev_ptr += 1;
13800
13801 /* now read in declarations */
13802 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13803 abbrev_ptr += bytes_read;
13804 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13805 abbrev_ptr += bytes_read;
13806 while (abbrev_name)
13807 {
f3dd6933 13808 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13809 {
f3dd6933
DJ
13810 allocated_attrs += ATTR_ALLOC_CHUNK;
13811 cur_attrs
13812 = xrealloc (cur_attrs, (allocated_attrs
13813 * sizeof (struct attr_abbrev)));
c906108c 13814 }
ae038cb0 13815
f3dd6933
DJ
13816 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13817 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13818 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13819 abbrev_ptr += bytes_read;
13820 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13821 abbrev_ptr += bytes_read;
13822 }
13823
433df2d4 13824 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13825 (cur_abbrev->num_attrs
13826 * sizeof (struct attr_abbrev)));
13827 memcpy (cur_abbrev->attrs, cur_attrs,
13828 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13829
433df2d4 13830 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13831
13832 /* Get next abbreviation.
13833 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13834 always properly terminated with an abbrev number of 0.
13835 Exit loop if we encounter an abbreviation which we have
13836 already read (which means we are about to read the abbreviations
13837 for the next compile unit) or if the end of the abbreviation
13838 table is reached. */
433df2d4 13839 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13840 break;
13841 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13842 abbrev_ptr += bytes_read;
433df2d4 13843 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13844 break;
13845 }
f3dd6933
DJ
13846
13847 xfree (cur_attrs);
433df2d4 13848 return abbrev_table;
c906108c
SS
13849}
13850
433df2d4 13851/* Free the resources held by ABBREV_TABLE. */
c906108c 13852
c906108c 13853static void
433df2d4 13854abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13855{
433df2d4
DE
13856 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13857 xfree (abbrev_table);
c906108c
SS
13858}
13859
f4dc4d17
DE
13860/* Same as abbrev_table_free but as a cleanup.
13861 We pass in a pointer to the pointer to the table so that we can
13862 set the pointer to NULL when we're done. It also simplifies
13863 build_type_unit_groups. */
13864
13865static void
13866abbrev_table_free_cleanup (void *table_ptr)
13867{
13868 struct abbrev_table **abbrev_table_ptr = table_ptr;
13869
13870 if (*abbrev_table_ptr != NULL)
13871 abbrev_table_free (*abbrev_table_ptr);
13872 *abbrev_table_ptr = NULL;
13873}
13874
433df2d4
DE
13875/* Read the abbrev table for CU from ABBREV_SECTION. */
13876
13877static void
13878dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13879 struct dwarf2_section_info *abbrev_section)
c906108c 13880{
433df2d4
DE
13881 cu->abbrev_table =
13882 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13883}
c906108c 13884
433df2d4 13885/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13886
433df2d4
DE
13887static void
13888dwarf2_free_abbrev_table (void *ptr_to_cu)
13889{
13890 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13891
a2ce51a0
DE
13892 if (cu->abbrev_table != NULL)
13893 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13894 /* Set this to NULL so that we SEGV if we try to read it later,
13895 and also because free_comp_unit verifies this is NULL. */
13896 cu->abbrev_table = NULL;
13897}
13898\f
72bf9492
DJ
13899/* Returns nonzero if TAG represents a type that we might generate a partial
13900 symbol for. */
13901
13902static int
13903is_type_tag_for_partial (int tag)
13904{
13905 switch (tag)
13906 {
13907#if 0
13908 /* Some types that would be reasonable to generate partial symbols for,
13909 that we don't at present. */
13910 case DW_TAG_array_type:
13911 case DW_TAG_file_type:
13912 case DW_TAG_ptr_to_member_type:
13913 case DW_TAG_set_type:
13914 case DW_TAG_string_type:
13915 case DW_TAG_subroutine_type:
13916#endif
13917 case DW_TAG_base_type:
13918 case DW_TAG_class_type:
680b30c7 13919 case DW_TAG_interface_type:
72bf9492
DJ
13920 case DW_TAG_enumeration_type:
13921 case DW_TAG_structure_type:
13922 case DW_TAG_subrange_type:
13923 case DW_TAG_typedef:
13924 case DW_TAG_union_type:
13925 return 1;
13926 default:
13927 return 0;
13928 }
13929}
13930
13931/* Load all DIEs that are interesting for partial symbols into memory. */
13932
13933static struct partial_die_info *
dee91e82 13934load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13935 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13936{
dee91e82 13937 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13938 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13939 struct partial_die_info *part_die;
13940 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13941 struct abbrev_info *abbrev;
13942 unsigned int bytes_read;
5afb4e99 13943 unsigned int load_all = 0;
72bf9492
DJ
13944 int nesting_level = 1;
13945
13946 parent_die = NULL;
13947 last_die = NULL;
13948
7adf1e79
DE
13949 gdb_assert (cu->per_cu != NULL);
13950 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13951 load_all = 1;
13952
72bf9492
DJ
13953 cu->partial_dies
13954 = htab_create_alloc_ex (cu->header.length / 12,
13955 partial_die_hash,
13956 partial_die_eq,
13957 NULL,
13958 &cu->comp_unit_obstack,
13959 hashtab_obstack_allocate,
13960 dummy_obstack_deallocate);
13961
13962 part_die = obstack_alloc (&cu->comp_unit_obstack,
13963 sizeof (struct partial_die_info));
13964
13965 while (1)
13966 {
13967 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13968
13969 /* A NULL abbrev means the end of a series of children. */
13970 if (abbrev == NULL)
13971 {
13972 if (--nesting_level == 0)
13973 {
13974 /* PART_DIE was probably the last thing allocated on the
13975 comp_unit_obstack, so we could call obstack_free
13976 here. We don't do that because the waste is small,
13977 and will be cleaned up when we're done with this
13978 compilation unit. This way, we're also more robust
13979 against other users of the comp_unit_obstack. */
13980 return first_die;
13981 }
13982 info_ptr += bytes_read;
13983 last_die = parent_die;
13984 parent_die = parent_die->die_parent;
13985 continue;
13986 }
13987
98bfdba5
PA
13988 /* Check for template arguments. We never save these; if
13989 they're seen, we just mark the parent, and go on our way. */
13990 if (parent_die != NULL
13991 && cu->language == language_cplus
13992 && (abbrev->tag == DW_TAG_template_type_param
13993 || abbrev->tag == DW_TAG_template_value_param))
13994 {
13995 parent_die->has_template_arguments = 1;
13996
13997 if (!load_all)
13998 {
13999 /* We don't need a partial DIE for the template argument. */
dee91e82 14000 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14001 continue;
14002 }
14003 }
14004
0d99eb77 14005 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
14006 Skip their other children. */
14007 if (!load_all
14008 && cu->language == language_cplus
14009 && parent_die != NULL
14010 && parent_die->tag == DW_TAG_subprogram)
14011 {
dee91e82 14012 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14013 continue;
14014 }
14015
5afb4e99
DJ
14016 /* Check whether this DIE is interesting enough to save. Normally
14017 we would not be interested in members here, but there may be
14018 later variables referencing them via DW_AT_specification (for
14019 static members). */
14020 if (!load_all
14021 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14022 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14023 && abbrev->tag != DW_TAG_enumerator
14024 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14025 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14026 && abbrev->tag != DW_TAG_variable
5afb4e99 14027 && abbrev->tag != DW_TAG_namespace
f55ee35c 14028 && abbrev->tag != DW_TAG_module
95554aad
TT
14029 && abbrev->tag != DW_TAG_member
14030 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14031 {
14032 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14033 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14034 continue;
14035 }
14036
dee91e82
DE
14037 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14038 info_ptr);
72bf9492
DJ
14039
14040 /* This two-pass algorithm for processing partial symbols has a
14041 high cost in cache pressure. Thus, handle some simple cases
14042 here which cover the majority of C partial symbols. DIEs
14043 which neither have specification tags in them, nor could have
14044 specification tags elsewhere pointing at them, can simply be
14045 processed and discarded.
14046
14047 This segment is also optional; scan_partial_symbols and
14048 add_partial_symbol will handle these DIEs if we chain
14049 them in normally. When compilers which do not emit large
14050 quantities of duplicate debug information are more common,
14051 this code can probably be removed. */
14052
14053 /* Any complete simple types at the top level (pretty much all
14054 of them, for a language without namespaces), can be processed
14055 directly. */
14056 if (parent_die == NULL
14057 && part_die->has_specification == 0
14058 && part_die->is_declaration == 0
d8228535 14059 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14060 || part_die->tag == DW_TAG_base_type
14061 || part_die->tag == DW_TAG_subrange_type))
14062 {
14063 if (building_psymtab && part_die->name != NULL)
04a679b8 14064 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14065 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14066 &objfile->static_psymbols,
14067 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14068 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14069 continue;
14070 }
14071
d8228535
JK
14072 /* The exception for DW_TAG_typedef with has_children above is
14073 a workaround of GCC PR debug/47510. In the case of this complaint
14074 type_name_no_tag_or_error will error on such types later.
14075
14076 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14077 it could not find the child DIEs referenced later, this is checked
14078 above. In correct DWARF DW_TAG_typedef should have no children. */
14079
14080 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14081 complaint (&symfile_complaints,
14082 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14083 "- DIE at 0x%x [in module %s]"),
b64f50a1 14084 part_die->offset.sect_off, objfile->name);
d8228535 14085
72bf9492
DJ
14086 /* If we're at the second level, and we're an enumerator, and
14087 our parent has no specification (meaning possibly lives in a
14088 namespace elsewhere), then we can add the partial symbol now
14089 instead of queueing it. */
14090 if (part_die->tag == DW_TAG_enumerator
14091 && parent_die != NULL
14092 && parent_die->die_parent == NULL
14093 && parent_die->tag == DW_TAG_enumeration_type
14094 && parent_die->has_specification == 0)
14095 {
14096 if (part_die->name == NULL)
3e43a32a
MS
14097 complaint (&symfile_complaints,
14098 _("malformed enumerator DIE ignored"));
72bf9492 14099 else if (building_psymtab)
04a679b8 14100 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14101 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14102 (cu->language == language_cplus
14103 || cu->language == language_java)
bb5ed363
DE
14104 ? &objfile->global_psymbols
14105 : &objfile->static_psymbols,
14106 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14107
dee91e82 14108 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14109 continue;
14110 }
14111
14112 /* We'll save this DIE so link it in. */
14113 part_die->die_parent = parent_die;
14114 part_die->die_sibling = NULL;
14115 part_die->die_child = NULL;
14116
14117 if (last_die && last_die == parent_die)
14118 last_die->die_child = part_die;
14119 else if (last_die)
14120 last_die->die_sibling = part_die;
14121
14122 last_die = part_die;
14123
14124 if (first_die == NULL)
14125 first_die = part_die;
14126
14127 /* Maybe add the DIE to the hash table. Not all DIEs that we
14128 find interesting need to be in the hash table, because we
14129 also have the parent/sibling/child chains; only those that we
14130 might refer to by offset later during partial symbol reading.
14131
14132 For now this means things that might have be the target of a
14133 DW_AT_specification, DW_AT_abstract_origin, or
14134 DW_AT_extension. DW_AT_extension will refer only to
14135 namespaces; DW_AT_abstract_origin refers to functions (and
14136 many things under the function DIE, but we do not recurse
14137 into function DIEs during partial symbol reading) and
14138 possibly variables as well; DW_AT_specification refers to
14139 declarations. Declarations ought to have the DW_AT_declaration
14140 flag. It happens that GCC forgets to put it in sometimes, but
14141 only for functions, not for types.
14142
14143 Adding more things than necessary to the hash table is harmless
14144 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14145 wasted time in find_partial_die, when we reread the compilation
14146 unit with load_all_dies set. */
72bf9492 14147
5afb4e99 14148 if (load_all
72929c62 14149 || abbrev->tag == DW_TAG_constant
5afb4e99 14150 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14151 || abbrev->tag == DW_TAG_variable
14152 || abbrev->tag == DW_TAG_namespace
14153 || part_die->is_declaration)
14154 {
14155 void **slot;
14156
14157 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14158 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14159 *slot = part_die;
14160 }
14161
14162 part_die = obstack_alloc (&cu->comp_unit_obstack,
14163 sizeof (struct partial_die_info));
14164
14165 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14166 we have no reason to follow the children of structures; for other
98bfdba5
PA
14167 languages we have to, so that we can get at method physnames
14168 to infer fully qualified class names, for DW_AT_specification,
14169 and for C++ template arguments. For C++, we also look one level
14170 inside functions to find template arguments (if the name of the
14171 function does not already contain the template arguments).
bc30ff58
JB
14172
14173 For Ada, we need to scan the children of subprograms and lexical
14174 blocks as well because Ada allows the definition of nested
14175 entities that could be interesting for the debugger, such as
14176 nested subprograms for instance. */
72bf9492 14177 if (last_die->has_children
5afb4e99
DJ
14178 && (load_all
14179 || last_die->tag == DW_TAG_namespace
f55ee35c 14180 || last_die->tag == DW_TAG_module
72bf9492 14181 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14182 || (cu->language == language_cplus
14183 && last_die->tag == DW_TAG_subprogram
14184 && (last_die->name == NULL
14185 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14186 || (cu->language != language_c
14187 && (last_die->tag == DW_TAG_class_type
680b30c7 14188 || last_die->tag == DW_TAG_interface_type
72bf9492 14189 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14190 || last_die->tag == DW_TAG_union_type))
14191 || (cu->language == language_ada
14192 && (last_die->tag == DW_TAG_subprogram
14193 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14194 {
14195 nesting_level++;
14196 parent_die = last_die;
14197 continue;
14198 }
14199
14200 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14201 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14202
14203 /* Back to the top, do it again. */
14204 }
14205}
14206
c906108c
SS
14207/* Read a minimal amount of information into the minimal die structure. */
14208
d521ce57 14209static const gdb_byte *
dee91e82
DE
14210read_partial_die (const struct die_reader_specs *reader,
14211 struct partial_die_info *part_die,
14212 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14213 const gdb_byte *info_ptr)
c906108c 14214{
dee91e82 14215 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14216 struct objfile *objfile = cu->objfile;
d521ce57 14217 const gdb_byte *buffer = reader->buffer;
fa238c03 14218 unsigned int i;
c906108c 14219 struct attribute attr;
c5aa993b 14220 int has_low_pc_attr = 0;
c906108c 14221 int has_high_pc_attr = 0;
91da1414 14222 int high_pc_relative = 0;
c906108c 14223
72bf9492 14224 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14225
b64f50a1 14226 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14227
14228 info_ptr += abbrev_len;
14229
14230 if (abbrev == NULL)
14231 return info_ptr;
14232
c906108c
SS
14233 part_die->tag = abbrev->tag;
14234 part_die->has_children = abbrev->has_children;
c906108c
SS
14235
14236 for (i = 0; i < abbrev->num_attrs; ++i)
14237 {
dee91e82 14238 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14239
14240 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14241 partial symbol table. */
c906108c
SS
14242 switch (attr.name)
14243 {
14244 case DW_AT_name:
71c25dea
TT
14245 switch (part_die->tag)
14246 {
14247 case DW_TAG_compile_unit:
95554aad 14248 case DW_TAG_partial_unit:
348e048f 14249 case DW_TAG_type_unit:
71c25dea
TT
14250 /* Compilation units have a DW_AT_name that is a filename, not
14251 a source language identifier. */
14252 case DW_TAG_enumeration_type:
14253 case DW_TAG_enumerator:
14254 /* These tags always have simple identifiers already; no need
14255 to canonicalize them. */
14256 part_die->name = DW_STRING (&attr);
14257 break;
14258 default:
14259 part_die->name
14260 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14261 &objfile->objfile_obstack);
71c25dea
TT
14262 break;
14263 }
c906108c 14264 break;
31ef98ae 14265 case DW_AT_linkage_name:
c906108c 14266 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14267 /* Note that both forms of linkage name might appear. We
14268 assume they will be the same, and we only store the last
14269 one we see. */
94af9270
KS
14270 if (cu->language == language_ada)
14271 part_die->name = DW_STRING (&attr);
abc72ce4 14272 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14273 break;
14274 case DW_AT_low_pc:
14275 has_low_pc_attr = 1;
14276 part_die->lowpc = DW_ADDR (&attr);
14277 break;
14278 case DW_AT_high_pc:
14279 has_high_pc_attr = 1;
3019eac3
DE
14280 if (attr.form == DW_FORM_addr
14281 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14282 part_die->highpc = DW_ADDR (&attr);
14283 else
14284 {
14285 high_pc_relative = 1;
14286 part_die->highpc = DW_UNSND (&attr);
14287 }
c906108c
SS
14288 break;
14289 case DW_AT_location:
0963b4bd 14290 /* Support the .debug_loc offsets. */
8e19ed76
PS
14291 if (attr_form_is_block (&attr))
14292 {
95554aad 14293 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14294 }
3690dd37 14295 else if (attr_form_is_section_offset (&attr))
8e19ed76 14296 {
4d3c2250 14297 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14298 }
14299 else
14300 {
4d3c2250
KB
14301 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14302 "partial symbol information");
8e19ed76 14303 }
c906108c 14304 break;
c906108c
SS
14305 case DW_AT_external:
14306 part_die->is_external = DW_UNSND (&attr);
14307 break;
14308 case DW_AT_declaration:
14309 part_die->is_declaration = DW_UNSND (&attr);
14310 break;
14311 case DW_AT_type:
14312 part_die->has_type = 1;
14313 break;
14314 case DW_AT_abstract_origin:
14315 case DW_AT_specification:
72bf9492
DJ
14316 case DW_AT_extension:
14317 part_die->has_specification = 1;
c764a876 14318 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14319 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14320 || cu->per_cu->is_dwz);
c906108c
SS
14321 break;
14322 case DW_AT_sibling:
14323 /* Ignore absolute siblings, they might point outside of
14324 the current compile unit. */
14325 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14326 complaint (&symfile_complaints,
14327 _("ignoring absolute DW_AT_sibling"));
c906108c 14328 else
b64f50a1 14329 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14330 break;
fa4028e9
JB
14331 case DW_AT_byte_size:
14332 part_die->has_byte_size = 1;
14333 break;
68511cec
CES
14334 case DW_AT_calling_convention:
14335 /* DWARF doesn't provide a way to identify a program's source-level
14336 entry point. DW_AT_calling_convention attributes are only meant
14337 to describe functions' calling conventions.
14338
14339 However, because it's a necessary piece of information in
14340 Fortran, and because DW_CC_program is the only piece of debugging
14341 information whose definition refers to a 'main program' at all,
14342 several compilers have begun marking Fortran main programs with
14343 DW_CC_program --- even when those functions use the standard
14344 calling conventions.
14345
14346 So until DWARF specifies a way to provide this information and
14347 compilers pick up the new representation, we'll support this
14348 practice. */
14349 if (DW_UNSND (&attr) == DW_CC_program
14350 && cu->language == language_fortran)
01f8c46d
JK
14351 {
14352 set_main_name (part_die->name);
14353
14354 /* As this DIE has a static linkage the name would be difficult
14355 to look up later. */
14356 language_of_main = language_fortran;
14357 }
68511cec 14358 break;
481860b3
GB
14359 case DW_AT_inline:
14360 if (DW_UNSND (&attr) == DW_INL_inlined
14361 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14362 part_die->may_be_inlined = 1;
14363 break;
95554aad
TT
14364
14365 case DW_AT_import:
14366 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14367 {
14368 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14369 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14370 || cu->per_cu->is_dwz);
14371 }
95554aad
TT
14372 break;
14373
c906108c
SS
14374 default:
14375 break;
14376 }
14377 }
14378
91da1414
MW
14379 if (high_pc_relative)
14380 part_die->highpc += part_die->lowpc;
14381
9373cf26
JK
14382 if (has_low_pc_attr && has_high_pc_attr)
14383 {
14384 /* When using the GNU linker, .gnu.linkonce. sections are used to
14385 eliminate duplicate copies of functions and vtables and such.
14386 The linker will arbitrarily choose one and discard the others.
14387 The AT_*_pc values for such functions refer to local labels in
14388 these sections. If the section from that file was discarded, the
14389 labels are not in the output, so the relocs get a value of 0.
14390 If this is a discarded function, mark the pc bounds as invalid,
14391 so that GDB will ignore it. */
14392 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14393 {
bb5ed363 14394 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14395
14396 complaint (&symfile_complaints,
14397 _("DW_AT_low_pc %s is zero "
14398 "for DIE at 0x%x [in module %s]"),
14399 paddress (gdbarch, part_die->lowpc),
b64f50a1 14400 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14401 }
14402 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14403 else if (part_die->lowpc >= part_die->highpc)
14404 {
bb5ed363 14405 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14406
14407 complaint (&symfile_complaints,
14408 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14409 "for DIE at 0x%x [in module %s]"),
14410 paddress (gdbarch, part_die->lowpc),
14411 paddress (gdbarch, part_die->highpc),
b64f50a1 14412 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14413 }
14414 else
14415 part_die->has_pc_info = 1;
14416 }
85cbf3d3 14417
c906108c
SS
14418 return info_ptr;
14419}
14420
72bf9492
DJ
14421/* Find a cached partial DIE at OFFSET in CU. */
14422
14423static struct partial_die_info *
b64f50a1 14424find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14425{
14426 struct partial_die_info *lookup_die = NULL;
14427 struct partial_die_info part_die;
14428
14429 part_die.offset = offset;
b64f50a1
JK
14430 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14431 offset.sect_off);
72bf9492 14432
72bf9492
DJ
14433 return lookup_die;
14434}
14435
348e048f
DE
14436/* Find a partial DIE at OFFSET, which may or may not be in CU,
14437 except in the case of .debug_types DIEs which do not reference
14438 outside their CU (they do however referencing other types via
55f1336d 14439 DW_FORM_ref_sig8). */
72bf9492
DJ
14440
14441static struct partial_die_info *
36586728 14442find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14443{
bb5ed363 14444 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14445 struct dwarf2_per_cu_data *per_cu = NULL;
14446 struct partial_die_info *pd = NULL;
72bf9492 14447
36586728
TT
14448 if (offset_in_dwz == cu->per_cu->is_dwz
14449 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14450 {
14451 pd = find_partial_die_in_comp_unit (offset, cu);
14452 if (pd != NULL)
14453 return pd;
0d99eb77
DE
14454 /* We missed recording what we needed.
14455 Load all dies and try again. */
14456 per_cu = cu->per_cu;
5afb4e99 14457 }
0d99eb77
DE
14458 else
14459 {
14460 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14461 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14462 {
14463 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14464 " external reference to offset 0x%lx [in module %s].\n"),
14465 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14466 bfd_get_filename (objfile->obfd));
14467 }
36586728
TT
14468 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14469 objfile);
72bf9492 14470
0d99eb77
DE
14471 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14472 load_partial_comp_unit (per_cu);
ae038cb0 14473
0d99eb77
DE
14474 per_cu->cu->last_used = 0;
14475 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14476 }
5afb4e99 14477
dee91e82
DE
14478 /* If we didn't find it, and not all dies have been loaded,
14479 load them all and try again. */
14480
5afb4e99
DJ
14481 if (pd == NULL && per_cu->load_all_dies == 0)
14482 {
5afb4e99 14483 per_cu->load_all_dies = 1;
fd820528
DE
14484
14485 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14486 THIS_CU->cu may already be in use. So we can't just free it and
14487 replace its DIEs with the ones we read in. Instead, we leave those
14488 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14489 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14490 set. */
dee91e82 14491 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14492
14493 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14494 }
14495
14496 if (pd == NULL)
14497 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14498 _("could not find partial DIE 0x%x "
14499 "in cache [from module %s]\n"),
b64f50a1 14500 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14501 return pd;
72bf9492
DJ
14502}
14503
abc72ce4
DE
14504/* See if we can figure out if the class lives in a namespace. We do
14505 this by looking for a member function; its demangled name will
14506 contain namespace info, if there is any. */
14507
14508static void
14509guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14510 struct dwarf2_cu *cu)
14511{
14512 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14513 what template types look like, because the demangler
14514 frequently doesn't give the same name as the debug info. We
14515 could fix this by only using the demangled name to get the
14516 prefix (but see comment in read_structure_type). */
14517
14518 struct partial_die_info *real_pdi;
14519 struct partial_die_info *child_pdi;
14520
14521 /* If this DIE (this DIE's specification, if any) has a parent, then
14522 we should not do this. We'll prepend the parent's fully qualified
14523 name when we create the partial symbol. */
14524
14525 real_pdi = struct_pdi;
14526 while (real_pdi->has_specification)
36586728
TT
14527 real_pdi = find_partial_die (real_pdi->spec_offset,
14528 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14529
14530 if (real_pdi->die_parent != NULL)
14531 return;
14532
14533 for (child_pdi = struct_pdi->die_child;
14534 child_pdi != NULL;
14535 child_pdi = child_pdi->die_sibling)
14536 {
14537 if (child_pdi->tag == DW_TAG_subprogram
14538 && child_pdi->linkage_name != NULL)
14539 {
14540 char *actual_class_name
14541 = language_class_name_from_physname (cu->language_defn,
14542 child_pdi->linkage_name);
14543 if (actual_class_name != NULL)
14544 {
14545 struct_pdi->name
10f0c4bb
TT
14546 = obstack_copy0 (&cu->objfile->objfile_obstack,
14547 actual_class_name,
14548 strlen (actual_class_name));
abc72ce4
DE
14549 xfree (actual_class_name);
14550 }
14551 break;
14552 }
14553 }
14554}
14555
72bf9492
DJ
14556/* Adjust PART_DIE before generating a symbol for it. This function
14557 may set the is_external flag or change the DIE's name. */
14558
14559static void
14560fixup_partial_die (struct partial_die_info *part_die,
14561 struct dwarf2_cu *cu)
14562{
abc72ce4
DE
14563 /* Once we've fixed up a die, there's no point in doing so again.
14564 This also avoids a memory leak if we were to call
14565 guess_partial_die_structure_name multiple times. */
14566 if (part_die->fixup_called)
14567 return;
14568
72bf9492
DJ
14569 /* If we found a reference attribute and the DIE has no name, try
14570 to find a name in the referred to DIE. */
14571
14572 if (part_die->name == NULL && part_die->has_specification)
14573 {
14574 struct partial_die_info *spec_die;
72bf9492 14575
36586728
TT
14576 spec_die = find_partial_die (part_die->spec_offset,
14577 part_die->spec_is_dwz, cu);
72bf9492 14578
10b3939b 14579 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14580
14581 if (spec_die->name)
14582 {
14583 part_die->name = spec_die->name;
14584
14585 /* Copy DW_AT_external attribute if it is set. */
14586 if (spec_die->is_external)
14587 part_die->is_external = spec_die->is_external;
14588 }
14589 }
14590
14591 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14592
14593 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14594 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14595
abc72ce4
DE
14596 /* If there is no parent die to provide a namespace, and there are
14597 children, see if we can determine the namespace from their linkage
122d1940 14598 name. */
abc72ce4 14599 if (cu->language == language_cplus
8b70b953 14600 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14601 && part_die->die_parent == NULL
14602 && part_die->has_children
14603 && (part_die->tag == DW_TAG_class_type
14604 || part_die->tag == DW_TAG_structure_type
14605 || part_die->tag == DW_TAG_union_type))
14606 guess_partial_die_structure_name (part_die, cu);
14607
53832f31
TT
14608 /* GCC might emit a nameless struct or union that has a linkage
14609 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14610 if (part_die->name == NULL
96408a79
SA
14611 && (part_die->tag == DW_TAG_class_type
14612 || part_die->tag == DW_TAG_interface_type
14613 || part_die->tag == DW_TAG_structure_type
14614 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14615 && part_die->linkage_name != NULL)
14616 {
14617 char *demangled;
14618
8de20a37 14619 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14620 if (demangled)
14621 {
96408a79
SA
14622 const char *base;
14623
14624 /* Strip any leading namespaces/classes, keep only the base name.
14625 DW_AT_name for named DIEs does not contain the prefixes. */
14626 base = strrchr (demangled, ':');
14627 if (base && base > demangled && base[-1] == ':')
14628 base++;
14629 else
14630 base = demangled;
14631
10f0c4bb
TT
14632 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14633 base, strlen (base));
53832f31
TT
14634 xfree (demangled);
14635 }
14636 }
14637
abc72ce4 14638 part_die->fixup_called = 1;
72bf9492
DJ
14639}
14640
a8329558 14641/* Read an attribute value described by an attribute form. */
c906108c 14642
d521ce57 14643static const gdb_byte *
dee91e82
DE
14644read_attribute_value (const struct die_reader_specs *reader,
14645 struct attribute *attr, unsigned form,
d521ce57 14646 const gdb_byte *info_ptr)
c906108c 14647{
dee91e82
DE
14648 struct dwarf2_cu *cu = reader->cu;
14649 bfd *abfd = reader->abfd;
e7c27a73 14650 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14651 unsigned int bytes_read;
14652 struct dwarf_block *blk;
14653
a8329558
KW
14654 attr->form = form;
14655 switch (form)
c906108c 14656 {
c906108c 14657 case DW_FORM_ref_addr:
ae411497 14658 if (cu->header.version == 2)
4568ecf9 14659 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14660 else
4568ecf9
DE
14661 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14662 &cu->header, &bytes_read);
ae411497
TT
14663 info_ptr += bytes_read;
14664 break;
36586728
TT
14665 case DW_FORM_GNU_ref_alt:
14666 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14667 info_ptr += bytes_read;
14668 break;
ae411497 14669 case DW_FORM_addr:
e7c27a73 14670 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14671 info_ptr += bytes_read;
c906108c
SS
14672 break;
14673 case DW_FORM_block2:
7b5a2f43 14674 blk = dwarf_alloc_block (cu);
c906108c
SS
14675 blk->size = read_2_bytes (abfd, info_ptr);
14676 info_ptr += 2;
14677 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14678 info_ptr += blk->size;
14679 DW_BLOCK (attr) = blk;
14680 break;
14681 case DW_FORM_block4:
7b5a2f43 14682 blk = dwarf_alloc_block (cu);
c906108c
SS
14683 blk->size = read_4_bytes (abfd, info_ptr);
14684 info_ptr += 4;
14685 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14686 info_ptr += blk->size;
14687 DW_BLOCK (attr) = blk;
14688 break;
14689 case DW_FORM_data2:
14690 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14691 info_ptr += 2;
14692 break;
14693 case DW_FORM_data4:
14694 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14695 info_ptr += 4;
14696 break;
14697 case DW_FORM_data8:
14698 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14699 info_ptr += 8;
14700 break;
2dc7f7b3
TT
14701 case DW_FORM_sec_offset:
14702 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14703 info_ptr += bytes_read;
14704 break;
c906108c 14705 case DW_FORM_string:
9b1c24c8 14706 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14707 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14708 info_ptr += bytes_read;
14709 break;
4bdf3d34 14710 case DW_FORM_strp:
36586728
TT
14711 if (!cu->per_cu->is_dwz)
14712 {
14713 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14714 &bytes_read);
14715 DW_STRING_IS_CANONICAL (attr) = 0;
14716 info_ptr += bytes_read;
14717 break;
14718 }
14719 /* FALLTHROUGH */
14720 case DW_FORM_GNU_strp_alt:
14721 {
14722 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14723 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14724 &bytes_read);
14725
14726 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14727 DW_STRING_IS_CANONICAL (attr) = 0;
14728 info_ptr += bytes_read;
14729 }
4bdf3d34 14730 break;
2dc7f7b3 14731 case DW_FORM_exprloc:
c906108c 14732 case DW_FORM_block:
7b5a2f43 14733 blk = dwarf_alloc_block (cu);
c906108c
SS
14734 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14735 info_ptr += bytes_read;
14736 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14737 info_ptr += blk->size;
14738 DW_BLOCK (attr) = blk;
14739 break;
14740 case DW_FORM_block1:
7b5a2f43 14741 blk = dwarf_alloc_block (cu);
c906108c
SS
14742 blk->size = read_1_byte (abfd, info_ptr);
14743 info_ptr += 1;
14744 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14745 info_ptr += blk->size;
14746 DW_BLOCK (attr) = blk;
14747 break;
14748 case DW_FORM_data1:
14749 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14750 info_ptr += 1;
14751 break;
14752 case DW_FORM_flag:
14753 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14754 info_ptr += 1;
14755 break;
2dc7f7b3
TT
14756 case DW_FORM_flag_present:
14757 DW_UNSND (attr) = 1;
14758 break;
c906108c
SS
14759 case DW_FORM_sdata:
14760 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14761 info_ptr += bytes_read;
14762 break;
14763 case DW_FORM_udata:
14764 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14765 info_ptr += bytes_read;
14766 break;
14767 case DW_FORM_ref1:
4568ecf9
DE
14768 DW_UNSND (attr) = (cu->header.offset.sect_off
14769 + read_1_byte (abfd, info_ptr));
c906108c
SS
14770 info_ptr += 1;
14771 break;
14772 case DW_FORM_ref2:
4568ecf9
DE
14773 DW_UNSND (attr) = (cu->header.offset.sect_off
14774 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14775 info_ptr += 2;
14776 break;
14777 case DW_FORM_ref4:
4568ecf9
DE
14778 DW_UNSND (attr) = (cu->header.offset.sect_off
14779 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14780 info_ptr += 4;
14781 break;
613e1657 14782 case DW_FORM_ref8:
4568ecf9
DE
14783 DW_UNSND (attr) = (cu->header.offset.sect_off
14784 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14785 info_ptr += 8;
14786 break;
55f1336d 14787 case DW_FORM_ref_sig8:
ac9ec31b 14788 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14789 info_ptr += 8;
14790 break;
c906108c 14791 case DW_FORM_ref_udata:
4568ecf9
DE
14792 DW_UNSND (attr) = (cu->header.offset.sect_off
14793 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14794 info_ptr += bytes_read;
14795 break;
c906108c 14796 case DW_FORM_indirect:
a8329558
KW
14797 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14798 info_ptr += bytes_read;
dee91e82 14799 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14800 break;
3019eac3
DE
14801 case DW_FORM_GNU_addr_index:
14802 if (reader->dwo_file == NULL)
14803 {
14804 /* For now flag a hard error.
14805 Later we can turn this into a complaint. */
14806 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14807 dwarf_form_name (form),
14808 bfd_get_filename (abfd));
14809 }
14810 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14811 info_ptr += bytes_read;
14812 break;
14813 case DW_FORM_GNU_str_index:
14814 if (reader->dwo_file == NULL)
14815 {
14816 /* For now flag a hard error.
14817 Later we can turn this into a complaint if warranted. */
14818 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14819 dwarf_form_name (form),
14820 bfd_get_filename (abfd));
14821 }
14822 {
14823 ULONGEST str_index =
14824 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14825
14826 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14827 DW_STRING_IS_CANONICAL (attr) = 0;
14828 info_ptr += bytes_read;
14829 }
14830 break;
c906108c 14831 default:
8a3fe4f8 14832 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14833 dwarf_form_name (form),
14834 bfd_get_filename (abfd));
c906108c 14835 }
28e94949 14836
36586728 14837 /* Super hack. */
7771576e 14838 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14839 attr->form = DW_FORM_GNU_ref_alt;
14840
28e94949
JB
14841 /* We have seen instances where the compiler tried to emit a byte
14842 size attribute of -1 which ended up being encoded as an unsigned
14843 0xffffffff. Although 0xffffffff is technically a valid size value,
14844 an object of this size seems pretty unlikely so we can relatively
14845 safely treat these cases as if the size attribute was invalid and
14846 treat them as zero by default. */
14847 if (attr->name == DW_AT_byte_size
14848 && form == DW_FORM_data4
14849 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14850 {
14851 complaint
14852 (&symfile_complaints,
43bbcdc2
PH
14853 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14854 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14855 DW_UNSND (attr) = 0;
14856 }
28e94949 14857
c906108c
SS
14858 return info_ptr;
14859}
14860
a8329558
KW
14861/* Read an attribute described by an abbreviated attribute. */
14862
d521ce57 14863static const gdb_byte *
dee91e82
DE
14864read_attribute (const struct die_reader_specs *reader,
14865 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14866 const gdb_byte *info_ptr)
a8329558
KW
14867{
14868 attr->name = abbrev->name;
dee91e82 14869 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14870}
14871
0963b4bd 14872/* Read dwarf information from a buffer. */
c906108c
SS
14873
14874static unsigned int
a1855c1d 14875read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14876{
fe1b8b76 14877 return bfd_get_8 (abfd, buf);
c906108c
SS
14878}
14879
14880static int
a1855c1d 14881read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14882{
fe1b8b76 14883 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14884}
14885
14886static unsigned int
a1855c1d 14887read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14888{
fe1b8b76 14889 return bfd_get_16 (abfd, buf);
c906108c
SS
14890}
14891
21ae7a4d 14892static int
a1855c1d 14893read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14894{
14895 return bfd_get_signed_16 (abfd, buf);
14896}
14897
c906108c 14898static unsigned int
a1855c1d 14899read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14900{
fe1b8b76 14901 return bfd_get_32 (abfd, buf);
c906108c
SS
14902}
14903
21ae7a4d 14904static int
a1855c1d 14905read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14906{
14907 return bfd_get_signed_32 (abfd, buf);
14908}
14909
93311388 14910static ULONGEST
a1855c1d 14911read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14912{
fe1b8b76 14913 return bfd_get_64 (abfd, buf);
c906108c
SS
14914}
14915
14916static CORE_ADDR
d521ce57 14917read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14918 unsigned int *bytes_read)
c906108c 14919{
e7c27a73 14920 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14921 CORE_ADDR retval = 0;
14922
107d2387 14923 if (cu_header->signed_addr_p)
c906108c 14924 {
107d2387
AC
14925 switch (cu_header->addr_size)
14926 {
14927 case 2:
fe1b8b76 14928 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14929 break;
14930 case 4:
fe1b8b76 14931 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14932 break;
14933 case 8:
fe1b8b76 14934 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14935 break;
14936 default:
8e65ff28 14937 internal_error (__FILE__, __LINE__,
e2e0b3e5 14938 _("read_address: bad switch, signed [in module %s]"),
659b0389 14939 bfd_get_filename (abfd));
107d2387
AC
14940 }
14941 }
14942 else
14943 {
14944 switch (cu_header->addr_size)
14945 {
14946 case 2:
fe1b8b76 14947 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14948 break;
14949 case 4:
fe1b8b76 14950 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14951 break;
14952 case 8:
fe1b8b76 14953 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14954 break;
14955 default:
8e65ff28 14956 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14957 _("read_address: bad switch, "
14958 "unsigned [in module %s]"),
659b0389 14959 bfd_get_filename (abfd));
107d2387 14960 }
c906108c 14961 }
64367e0a 14962
107d2387
AC
14963 *bytes_read = cu_header->addr_size;
14964 return retval;
c906108c
SS
14965}
14966
f7ef9339 14967/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14968 specification allows the initial length to take up either 4 bytes
14969 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14970 bytes describe the length and all offsets will be 8 bytes in length
14971 instead of 4.
14972
f7ef9339
KB
14973 An older, non-standard 64-bit format is also handled by this
14974 function. The older format in question stores the initial length
14975 as an 8-byte quantity without an escape value. Lengths greater
14976 than 2^32 aren't very common which means that the initial 4 bytes
14977 is almost always zero. Since a length value of zero doesn't make
14978 sense for the 32-bit format, this initial zero can be considered to
14979 be an escape value which indicates the presence of the older 64-bit
14980 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14981 greater than 4GB. If it becomes necessary to handle lengths
14982 somewhat larger than 4GB, we could allow other small values (such
14983 as the non-sensical values of 1, 2, and 3) to also be used as
14984 escape values indicating the presence of the old format.
f7ef9339 14985
917c78fc
MK
14986 The value returned via bytes_read should be used to increment the
14987 relevant pointer after calling read_initial_length().
c764a876 14988
613e1657
KB
14989 [ Note: read_initial_length() and read_offset() are based on the
14990 document entitled "DWARF Debugging Information Format", revision
f7ef9339 14991 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
14992 from:
14993
f7ef9339 14994 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 14995
613e1657
KB
14996 This document is only a draft and is subject to change. (So beware.)
14997
f7ef9339 14998 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
14999 determined empirically by examining 64-bit ELF files produced by
15000 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
15001
15002 - Kevin, July 16, 2002
613e1657
KB
15003 ] */
15004
15005static LONGEST
d521ce57 15006read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 15007{
fe1b8b76 15008 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 15009
dd373385 15010 if (length == 0xffffffff)
613e1657 15011 {
fe1b8b76 15012 length = bfd_get_64 (abfd, buf + 4);
613e1657 15013 *bytes_read = 12;
613e1657 15014 }
dd373385 15015 else if (length == 0)
f7ef9339 15016 {
dd373385 15017 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15018 length = bfd_get_64 (abfd, buf);
f7ef9339 15019 *bytes_read = 8;
f7ef9339 15020 }
613e1657
KB
15021 else
15022 {
15023 *bytes_read = 4;
613e1657
KB
15024 }
15025
c764a876
DE
15026 return length;
15027}
dd373385 15028
c764a876
DE
15029/* Cover function for read_initial_length.
15030 Returns the length of the object at BUF, and stores the size of the
15031 initial length in *BYTES_READ and stores the size that offsets will be in
15032 *OFFSET_SIZE.
15033 If the initial length size is not equivalent to that specified in
15034 CU_HEADER then issue a complaint.
15035 This is useful when reading non-comp-unit headers. */
dd373385 15036
c764a876 15037static LONGEST
d521ce57 15038read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15039 const struct comp_unit_head *cu_header,
15040 unsigned int *bytes_read,
15041 unsigned int *offset_size)
15042{
15043 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15044
15045 gdb_assert (cu_header->initial_length_size == 4
15046 || cu_header->initial_length_size == 8
15047 || cu_header->initial_length_size == 12);
15048
15049 if (cu_header->initial_length_size != *bytes_read)
15050 complaint (&symfile_complaints,
15051 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15052
c764a876 15053 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15054 return length;
613e1657
KB
15055}
15056
15057/* Read an offset from the data stream. The size of the offset is
917c78fc 15058 given by cu_header->offset_size. */
613e1657
KB
15059
15060static LONGEST
d521ce57
TT
15061read_offset (bfd *abfd, const gdb_byte *buf,
15062 const struct comp_unit_head *cu_header,
891d2f0b 15063 unsigned int *bytes_read)
c764a876
DE
15064{
15065 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15066
c764a876
DE
15067 *bytes_read = cu_header->offset_size;
15068 return offset;
15069}
15070
15071/* Read an offset from the data stream. */
15072
15073static LONGEST
d521ce57 15074read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15075{
15076 LONGEST retval = 0;
15077
c764a876 15078 switch (offset_size)
613e1657
KB
15079 {
15080 case 4:
fe1b8b76 15081 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15082 break;
15083 case 8:
fe1b8b76 15084 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15085 break;
15086 default:
8e65ff28 15087 internal_error (__FILE__, __LINE__,
c764a876 15088 _("read_offset_1: bad switch [in module %s]"),
659b0389 15089 bfd_get_filename (abfd));
613e1657
KB
15090 }
15091
917c78fc 15092 return retval;
613e1657
KB
15093}
15094
d521ce57
TT
15095static const gdb_byte *
15096read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15097{
15098 /* If the size of a host char is 8 bits, we can return a pointer
15099 to the buffer, otherwise we have to copy the data to a buffer
15100 allocated on the temporary obstack. */
4bdf3d34 15101 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15102 return buf;
c906108c
SS
15103}
15104
d521ce57
TT
15105static const char *
15106read_direct_string (bfd *abfd, const gdb_byte *buf,
15107 unsigned int *bytes_read_ptr)
c906108c
SS
15108{
15109 /* If the size of a host char is 8 bits, we can return a pointer
15110 to the string, otherwise we have to copy the string to a buffer
15111 allocated on the temporary obstack. */
4bdf3d34 15112 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15113 if (*buf == '\0')
15114 {
15115 *bytes_read_ptr = 1;
15116 return NULL;
15117 }
d521ce57
TT
15118 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15119 return (const char *) buf;
4bdf3d34
JJ
15120}
15121
d521ce57 15122static const char *
cf2c3c16 15123read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15124{
be391dca 15125 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15126 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15127 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15128 bfd_get_filename (abfd));
dce234bc 15129 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15130 error (_("DW_FORM_strp pointing outside of "
15131 ".debug_str section [in module %s]"),
15132 bfd_get_filename (abfd));
4bdf3d34 15133 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15134 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15135 return NULL;
d521ce57 15136 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15137}
15138
36586728
TT
15139/* Read a string at offset STR_OFFSET in the .debug_str section from
15140 the .dwz file DWZ. Throw an error if the offset is too large. If
15141 the string consists of a single NUL byte, return NULL; otherwise
15142 return a pointer to the string. */
15143
d521ce57 15144static const char *
36586728
TT
15145read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15146{
15147 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15148
15149 if (dwz->str.buffer == NULL)
15150 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15151 "section [in module %s]"),
15152 bfd_get_filename (dwz->dwz_bfd));
15153 if (str_offset >= dwz->str.size)
15154 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15155 ".debug_str section [in module %s]"),
15156 bfd_get_filename (dwz->dwz_bfd));
15157 gdb_assert (HOST_CHAR_BIT == 8);
15158 if (dwz->str.buffer[str_offset] == '\0')
15159 return NULL;
d521ce57 15160 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15161}
15162
d521ce57
TT
15163static const char *
15164read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15165 const struct comp_unit_head *cu_header,
15166 unsigned int *bytes_read_ptr)
15167{
15168 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15169
15170 return read_indirect_string_at_offset (abfd, str_offset);
15171}
15172
12df843f 15173static ULONGEST
d521ce57
TT
15174read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15175 unsigned int *bytes_read_ptr)
c906108c 15176{
12df843f 15177 ULONGEST result;
ce5d95e1 15178 unsigned int num_read;
c906108c
SS
15179 int i, shift;
15180 unsigned char byte;
15181
15182 result = 0;
15183 shift = 0;
15184 num_read = 0;
15185 i = 0;
15186 while (1)
15187 {
fe1b8b76 15188 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15189 buf++;
15190 num_read++;
12df843f 15191 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15192 if ((byte & 128) == 0)
15193 {
15194 break;
15195 }
15196 shift += 7;
15197 }
15198 *bytes_read_ptr = num_read;
15199 return result;
15200}
15201
12df843f 15202static LONGEST
d521ce57
TT
15203read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15204 unsigned int *bytes_read_ptr)
c906108c 15205{
12df843f 15206 LONGEST result;
77e0b926 15207 int i, shift, num_read;
c906108c
SS
15208 unsigned char byte;
15209
15210 result = 0;
15211 shift = 0;
c906108c
SS
15212 num_read = 0;
15213 i = 0;
15214 while (1)
15215 {
fe1b8b76 15216 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15217 buf++;
15218 num_read++;
12df843f 15219 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15220 shift += 7;
15221 if ((byte & 128) == 0)
15222 {
15223 break;
15224 }
15225 }
77e0b926 15226 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15227 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15228 *bytes_read_ptr = num_read;
15229 return result;
15230}
15231
3019eac3
DE
15232/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15233 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15234 ADDR_SIZE is the size of addresses from the CU header. */
15235
15236static CORE_ADDR
15237read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15238{
15239 struct objfile *objfile = dwarf2_per_objfile->objfile;
15240 bfd *abfd = objfile->obfd;
15241 const gdb_byte *info_ptr;
15242
15243 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15244 if (dwarf2_per_objfile->addr.buffer == NULL)
15245 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15246 objfile->name);
15247 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15248 error (_("DW_FORM_addr_index pointing outside of "
15249 ".debug_addr section [in module %s]"),
15250 objfile->name);
15251 info_ptr = (dwarf2_per_objfile->addr.buffer
15252 + addr_base + addr_index * addr_size);
15253 if (addr_size == 4)
15254 return bfd_get_32 (abfd, info_ptr);
15255 else
15256 return bfd_get_64 (abfd, info_ptr);
15257}
15258
15259/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15260
15261static CORE_ADDR
15262read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15263{
15264 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15265}
15266
15267/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15268
15269static CORE_ADDR
d521ce57 15270read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15271 unsigned int *bytes_read)
15272{
15273 bfd *abfd = cu->objfile->obfd;
15274 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15275
15276 return read_addr_index (cu, addr_index);
15277}
15278
15279/* Data structure to pass results from dwarf2_read_addr_index_reader
15280 back to dwarf2_read_addr_index. */
15281
15282struct dwarf2_read_addr_index_data
15283{
15284 ULONGEST addr_base;
15285 int addr_size;
15286};
15287
15288/* die_reader_func for dwarf2_read_addr_index. */
15289
15290static void
15291dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15292 const gdb_byte *info_ptr,
3019eac3
DE
15293 struct die_info *comp_unit_die,
15294 int has_children,
15295 void *data)
15296{
15297 struct dwarf2_cu *cu = reader->cu;
15298 struct dwarf2_read_addr_index_data *aidata =
15299 (struct dwarf2_read_addr_index_data *) data;
15300
15301 aidata->addr_base = cu->addr_base;
15302 aidata->addr_size = cu->header.addr_size;
15303}
15304
15305/* Given an index in .debug_addr, fetch the value.
15306 NOTE: This can be called during dwarf expression evaluation,
15307 long after the debug information has been read, and thus per_cu->cu
15308 may no longer exist. */
15309
15310CORE_ADDR
15311dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15312 unsigned int addr_index)
15313{
15314 struct objfile *objfile = per_cu->objfile;
15315 struct dwarf2_cu *cu = per_cu->cu;
15316 ULONGEST addr_base;
15317 int addr_size;
15318
15319 /* This is intended to be called from outside this file. */
15320 dw2_setup (objfile);
15321
15322 /* We need addr_base and addr_size.
15323 If we don't have PER_CU->cu, we have to get it.
15324 Nasty, but the alternative is storing the needed info in PER_CU,
15325 which at this point doesn't seem justified: it's not clear how frequently
15326 it would get used and it would increase the size of every PER_CU.
15327 Entry points like dwarf2_per_cu_addr_size do a similar thing
15328 so we're not in uncharted territory here.
15329 Alas we need to be a bit more complicated as addr_base is contained
15330 in the DIE.
15331
15332 We don't need to read the entire CU(/TU).
15333 We just need the header and top level die.
a1b64ce1 15334
3019eac3 15335 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15336 For now we skip this optimization. */
3019eac3
DE
15337
15338 if (cu != NULL)
15339 {
15340 addr_base = cu->addr_base;
15341 addr_size = cu->header.addr_size;
15342 }
15343 else
15344 {
15345 struct dwarf2_read_addr_index_data aidata;
15346
a1b64ce1
DE
15347 /* Note: We can't use init_cutu_and_read_dies_simple here,
15348 we need addr_base. */
15349 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15350 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15351 addr_base = aidata.addr_base;
15352 addr_size = aidata.addr_size;
15353 }
15354
15355 return read_addr_index_1 (addr_index, addr_base, addr_size);
15356}
15357
15358/* Given a DW_AT_str_index, fetch the string. */
15359
d521ce57 15360static const char *
3019eac3
DE
15361read_str_index (const struct die_reader_specs *reader,
15362 struct dwarf2_cu *cu, ULONGEST str_index)
15363{
15364 struct objfile *objfile = dwarf2_per_objfile->objfile;
15365 const char *dwo_name = objfile->name;
15366 bfd *abfd = objfile->obfd;
15367 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15368 const gdb_byte *info_ptr;
3019eac3
DE
15369 ULONGEST str_offset;
15370
15371 dwarf2_read_section (objfile, &sections->str);
15372 dwarf2_read_section (objfile, &sections->str_offsets);
15373 if (sections->str.buffer == NULL)
15374 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15375 " in CU at offset 0x%lx [in module %s]"),
15376 (long) cu->header.offset.sect_off, dwo_name);
15377 if (sections->str_offsets.buffer == NULL)
15378 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15379 " in CU at offset 0x%lx [in module %s]"),
15380 (long) cu->header.offset.sect_off, dwo_name);
15381 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15382 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15383 " section in CU at offset 0x%lx [in module %s]"),
15384 (long) cu->header.offset.sect_off, dwo_name);
15385 info_ptr = (sections->str_offsets.buffer
15386 + str_index * cu->header.offset_size);
15387 if (cu->header.offset_size == 4)
15388 str_offset = bfd_get_32 (abfd, info_ptr);
15389 else
15390 str_offset = bfd_get_64 (abfd, info_ptr);
15391 if (str_offset >= sections->str.size)
15392 error (_("Offset from DW_FORM_str_index pointing outside of"
15393 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15394 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15395 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15396}
15397
3019eac3
DE
15398/* Return the length of an LEB128 number in BUF. */
15399
15400static int
15401leb128_size (const gdb_byte *buf)
15402{
15403 const gdb_byte *begin = buf;
15404 gdb_byte byte;
15405
15406 while (1)
15407 {
15408 byte = *buf++;
15409 if ((byte & 128) == 0)
15410 return buf - begin;
15411 }
15412}
15413
c906108c 15414static void
e142c38c 15415set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15416{
15417 switch (lang)
15418 {
15419 case DW_LANG_C89:
76bee0cc 15420 case DW_LANG_C99:
c906108c 15421 case DW_LANG_C:
d1be3247 15422 case DW_LANG_UPC:
e142c38c 15423 cu->language = language_c;
c906108c
SS
15424 break;
15425 case DW_LANG_C_plus_plus:
e142c38c 15426 cu->language = language_cplus;
c906108c 15427 break;
6aecb9c2
JB
15428 case DW_LANG_D:
15429 cu->language = language_d;
15430 break;
c906108c
SS
15431 case DW_LANG_Fortran77:
15432 case DW_LANG_Fortran90:
b21b22e0 15433 case DW_LANG_Fortran95:
e142c38c 15434 cu->language = language_fortran;
c906108c 15435 break;
a766d390
DE
15436 case DW_LANG_Go:
15437 cu->language = language_go;
15438 break;
c906108c 15439 case DW_LANG_Mips_Assembler:
e142c38c 15440 cu->language = language_asm;
c906108c 15441 break;
bebd888e 15442 case DW_LANG_Java:
e142c38c 15443 cu->language = language_java;
bebd888e 15444 break;
c906108c 15445 case DW_LANG_Ada83:
8aaf0b47 15446 case DW_LANG_Ada95:
bc5f45f8
JB
15447 cu->language = language_ada;
15448 break;
72019c9c
GM
15449 case DW_LANG_Modula2:
15450 cu->language = language_m2;
15451 break;
fe8e67fd
PM
15452 case DW_LANG_Pascal83:
15453 cu->language = language_pascal;
15454 break;
22566fbd
DJ
15455 case DW_LANG_ObjC:
15456 cu->language = language_objc;
15457 break;
c906108c
SS
15458 case DW_LANG_Cobol74:
15459 case DW_LANG_Cobol85:
c906108c 15460 default:
e142c38c 15461 cu->language = language_minimal;
c906108c
SS
15462 break;
15463 }
e142c38c 15464 cu->language_defn = language_def (cu->language);
c906108c
SS
15465}
15466
15467/* Return the named attribute or NULL if not there. */
15468
15469static struct attribute *
e142c38c 15470dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15471{
a48e046c 15472 for (;;)
c906108c 15473 {
a48e046c
TT
15474 unsigned int i;
15475 struct attribute *spec = NULL;
15476
15477 for (i = 0; i < die->num_attrs; ++i)
15478 {
15479 if (die->attrs[i].name == name)
15480 return &die->attrs[i];
15481 if (die->attrs[i].name == DW_AT_specification
15482 || die->attrs[i].name == DW_AT_abstract_origin)
15483 spec = &die->attrs[i];
15484 }
15485
15486 if (!spec)
15487 break;
c906108c 15488
f2f0e013 15489 die = follow_die_ref (die, spec, &cu);
f2f0e013 15490 }
c5aa993b 15491
c906108c
SS
15492 return NULL;
15493}
15494
348e048f
DE
15495/* Return the named attribute or NULL if not there,
15496 but do not follow DW_AT_specification, etc.
15497 This is for use in contexts where we're reading .debug_types dies.
15498 Following DW_AT_specification, DW_AT_abstract_origin will take us
15499 back up the chain, and we want to go down. */
15500
15501static struct attribute *
45e58e77 15502dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15503{
15504 unsigned int i;
15505
15506 for (i = 0; i < die->num_attrs; ++i)
15507 if (die->attrs[i].name == name)
15508 return &die->attrs[i];
15509
15510 return NULL;
15511}
15512
05cf31d1
JB
15513/* Return non-zero iff the attribute NAME is defined for the given DIE,
15514 and holds a non-zero value. This function should only be used for
2dc7f7b3 15515 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15516
15517static int
15518dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15519{
15520 struct attribute *attr = dwarf2_attr (die, name, cu);
15521
15522 return (attr && DW_UNSND (attr));
15523}
15524
3ca72b44 15525static int
e142c38c 15526die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15527{
05cf31d1
JB
15528 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15529 which value is non-zero. However, we have to be careful with
15530 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15531 (via dwarf2_flag_true_p) follows this attribute. So we may
15532 end up accidently finding a declaration attribute that belongs
15533 to a different DIE referenced by the specification attribute,
15534 even though the given DIE does not have a declaration attribute. */
15535 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15536 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15537}
15538
63d06c5c 15539/* Return the die giving the specification for DIE, if there is
f2f0e013 15540 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15541 containing the return value on output. If there is no
15542 specification, but there is an abstract origin, that is
15543 returned. */
63d06c5c
DC
15544
15545static struct die_info *
f2f0e013 15546die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15547{
f2f0e013
DJ
15548 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15549 *spec_cu);
63d06c5c 15550
edb3359d
DJ
15551 if (spec_attr == NULL)
15552 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15553
63d06c5c
DC
15554 if (spec_attr == NULL)
15555 return NULL;
15556 else
f2f0e013 15557 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15558}
c906108c 15559
debd256d 15560/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15561 refers to.
15562 NOTE: This is also used as a "cleanup" function. */
15563
debd256d
JB
15564static void
15565free_line_header (struct line_header *lh)
15566{
15567 if (lh->standard_opcode_lengths)
a8bc7b56 15568 xfree (lh->standard_opcode_lengths);
debd256d
JB
15569
15570 /* Remember that all the lh->file_names[i].name pointers are
15571 pointers into debug_line_buffer, and don't need to be freed. */
15572 if (lh->file_names)
a8bc7b56 15573 xfree (lh->file_names);
debd256d
JB
15574
15575 /* Similarly for the include directory names. */
15576 if (lh->include_dirs)
a8bc7b56 15577 xfree (lh->include_dirs);
debd256d 15578
a8bc7b56 15579 xfree (lh);
debd256d
JB
15580}
15581
debd256d 15582/* Add an entry to LH's include directory table. */
ae2de4f8 15583
debd256d 15584static void
d521ce57 15585add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15586{
debd256d
JB
15587 /* Grow the array if necessary. */
15588 if (lh->include_dirs_size == 0)
c5aa993b 15589 {
debd256d
JB
15590 lh->include_dirs_size = 1; /* for testing */
15591 lh->include_dirs = xmalloc (lh->include_dirs_size
15592 * sizeof (*lh->include_dirs));
15593 }
15594 else if (lh->num_include_dirs >= lh->include_dirs_size)
15595 {
15596 lh->include_dirs_size *= 2;
15597 lh->include_dirs = xrealloc (lh->include_dirs,
15598 (lh->include_dirs_size
15599 * sizeof (*lh->include_dirs)));
c5aa993b 15600 }
c906108c 15601
debd256d
JB
15602 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15603}
6e70227d 15604
debd256d 15605/* Add an entry to LH's file name table. */
ae2de4f8 15606
debd256d
JB
15607static void
15608add_file_name (struct line_header *lh,
d521ce57 15609 const char *name,
debd256d
JB
15610 unsigned int dir_index,
15611 unsigned int mod_time,
15612 unsigned int length)
15613{
15614 struct file_entry *fe;
15615
15616 /* Grow the array if necessary. */
15617 if (lh->file_names_size == 0)
15618 {
15619 lh->file_names_size = 1; /* for testing */
15620 lh->file_names = xmalloc (lh->file_names_size
15621 * sizeof (*lh->file_names));
15622 }
15623 else if (lh->num_file_names >= lh->file_names_size)
15624 {
15625 lh->file_names_size *= 2;
15626 lh->file_names = xrealloc (lh->file_names,
15627 (lh->file_names_size
15628 * sizeof (*lh->file_names)));
15629 }
15630
15631 fe = &lh->file_names[lh->num_file_names++];
15632 fe->name = name;
15633 fe->dir_index = dir_index;
15634 fe->mod_time = mod_time;
15635 fe->length = length;
aaa75496 15636 fe->included_p = 0;
cb1df416 15637 fe->symtab = NULL;
debd256d 15638}
6e70227d 15639
36586728
TT
15640/* A convenience function to find the proper .debug_line section for a
15641 CU. */
15642
15643static struct dwarf2_section_info *
15644get_debug_line_section (struct dwarf2_cu *cu)
15645{
15646 struct dwarf2_section_info *section;
15647
15648 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15649 DWO file. */
15650 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15651 section = &cu->dwo_unit->dwo_file->sections.line;
15652 else if (cu->per_cu->is_dwz)
15653 {
15654 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15655
15656 section = &dwz->line;
15657 }
15658 else
15659 section = &dwarf2_per_objfile->line;
15660
15661 return section;
15662}
15663
debd256d 15664/* Read the statement program header starting at OFFSET in
3019eac3 15665 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15666 to a struct line_header, allocated using xmalloc.
debd256d
JB
15667
15668 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15669 the returned object point into the dwarf line section buffer,
15670 and must not be freed. */
ae2de4f8 15671
debd256d 15672static struct line_header *
3019eac3 15673dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15674{
15675 struct cleanup *back_to;
15676 struct line_header *lh;
d521ce57 15677 const gdb_byte *line_ptr;
c764a876 15678 unsigned int bytes_read, offset_size;
debd256d 15679 int i;
d521ce57 15680 const char *cur_dir, *cur_file;
3019eac3
DE
15681 struct dwarf2_section_info *section;
15682 bfd *abfd;
15683
36586728 15684 section = get_debug_line_section (cu);
3019eac3
DE
15685 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15686 if (section->buffer == NULL)
debd256d 15687 {
3019eac3
DE
15688 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15689 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15690 else
15691 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15692 return 0;
15693 }
15694
fceca515
DE
15695 /* We can't do this until we know the section is non-empty.
15696 Only then do we know we have such a section. */
15697 abfd = section->asection->owner;
15698
a738430d
MK
15699 /* Make sure that at least there's room for the total_length field.
15700 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15701 if (offset + 4 >= section->size)
debd256d 15702 {
4d3c2250 15703 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15704 return 0;
15705 }
15706
15707 lh = xmalloc (sizeof (*lh));
15708 memset (lh, 0, sizeof (*lh));
15709 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15710 (void *) lh);
15711
3019eac3 15712 line_ptr = section->buffer + offset;
debd256d 15713
a738430d 15714 /* Read in the header. */
6e70227d 15715 lh->total_length =
c764a876
DE
15716 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15717 &bytes_read, &offset_size);
debd256d 15718 line_ptr += bytes_read;
3019eac3 15719 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15720 {
4d3c2250 15721 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15722 do_cleanups (back_to);
debd256d
JB
15723 return 0;
15724 }
15725 lh->statement_program_end = line_ptr + lh->total_length;
15726 lh->version = read_2_bytes (abfd, line_ptr);
15727 line_ptr += 2;
c764a876
DE
15728 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15729 line_ptr += offset_size;
debd256d
JB
15730 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15731 line_ptr += 1;
2dc7f7b3
TT
15732 if (lh->version >= 4)
15733 {
15734 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15735 line_ptr += 1;
15736 }
15737 else
15738 lh->maximum_ops_per_instruction = 1;
15739
15740 if (lh->maximum_ops_per_instruction == 0)
15741 {
15742 lh->maximum_ops_per_instruction = 1;
15743 complaint (&symfile_complaints,
3e43a32a
MS
15744 _("invalid maximum_ops_per_instruction "
15745 "in `.debug_line' section"));
2dc7f7b3
TT
15746 }
15747
debd256d
JB
15748 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15749 line_ptr += 1;
15750 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15751 line_ptr += 1;
15752 lh->line_range = read_1_byte (abfd, line_ptr);
15753 line_ptr += 1;
15754 lh->opcode_base = read_1_byte (abfd, line_ptr);
15755 line_ptr += 1;
15756 lh->standard_opcode_lengths
fe1b8b76 15757 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15758
15759 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15760 for (i = 1; i < lh->opcode_base; ++i)
15761 {
15762 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15763 line_ptr += 1;
15764 }
15765
a738430d 15766 /* Read directory table. */
9b1c24c8 15767 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15768 {
15769 line_ptr += bytes_read;
15770 add_include_dir (lh, cur_dir);
15771 }
15772 line_ptr += bytes_read;
15773
a738430d 15774 /* Read file name table. */
9b1c24c8 15775 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15776 {
15777 unsigned int dir_index, mod_time, length;
15778
15779 line_ptr += bytes_read;
15780 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15781 line_ptr += bytes_read;
15782 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15783 line_ptr += bytes_read;
15784 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15785 line_ptr += bytes_read;
15786
15787 add_file_name (lh, cur_file, dir_index, mod_time, length);
15788 }
15789 line_ptr += bytes_read;
6e70227d 15790 lh->statement_program_start = line_ptr;
debd256d 15791
3019eac3 15792 if (line_ptr > (section->buffer + section->size))
4d3c2250 15793 complaint (&symfile_complaints,
3e43a32a
MS
15794 _("line number info header doesn't "
15795 "fit in `.debug_line' section"));
debd256d
JB
15796
15797 discard_cleanups (back_to);
15798 return lh;
15799}
c906108c 15800
c6da4cef
DE
15801/* Subroutine of dwarf_decode_lines to simplify it.
15802 Return the file name of the psymtab for included file FILE_INDEX
15803 in line header LH of PST.
15804 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15805 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15806 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15807
15808 The function creates dangling cleanup registration. */
c6da4cef 15809
d521ce57 15810static const char *
c6da4cef
DE
15811psymtab_include_file_name (const struct line_header *lh, int file_index,
15812 const struct partial_symtab *pst,
15813 const char *comp_dir)
15814{
15815 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15816 const char *include_name = fe.name;
15817 const char *include_name_to_compare = include_name;
15818 const char *dir_name = NULL;
72b9f47f
TT
15819 const char *pst_filename;
15820 char *copied_name = NULL;
c6da4cef
DE
15821 int file_is_pst;
15822
15823 if (fe.dir_index)
15824 dir_name = lh->include_dirs[fe.dir_index - 1];
15825
15826 if (!IS_ABSOLUTE_PATH (include_name)
15827 && (dir_name != NULL || comp_dir != NULL))
15828 {
15829 /* Avoid creating a duplicate psymtab for PST.
15830 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15831 Before we do the comparison, however, we need to account
15832 for DIR_NAME and COMP_DIR.
15833 First prepend dir_name (if non-NULL). If we still don't
15834 have an absolute path prepend comp_dir (if non-NULL).
15835 However, the directory we record in the include-file's
15836 psymtab does not contain COMP_DIR (to match the
15837 corresponding symtab(s)).
15838
15839 Example:
15840
15841 bash$ cd /tmp
15842 bash$ gcc -g ./hello.c
15843 include_name = "hello.c"
15844 dir_name = "."
15845 DW_AT_comp_dir = comp_dir = "/tmp"
15846 DW_AT_name = "./hello.c" */
15847
15848 if (dir_name != NULL)
15849 {
d521ce57
TT
15850 char *tem = concat (dir_name, SLASH_STRING,
15851 include_name, (char *)NULL);
15852
15853 make_cleanup (xfree, tem);
15854 include_name = tem;
c6da4cef 15855 include_name_to_compare = include_name;
c6da4cef
DE
15856 }
15857 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15858 {
d521ce57
TT
15859 char *tem = concat (comp_dir, SLASH_STRING,
15860 include_name, (char *)NULL);
15861
15862 make_cleanup (xfree, tem);
15863 include_name_to_compare = tem;
c6da4cef
DE
15864 }
15865 }
15866
15867 pst_filename = pst->filename;
15868 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15869 {
72b9f47f
TT
15870 copied_name = concat (pst->dirname, SLASH_STRING,
15871 pst_filename, (char *)NULL);
15872 pst_filename = copied_name;
c6da4cef
DE
15873 }
15874
1e3fad37 15875 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15876
72b9f47f
TT
15877 if (copied_name != NULL)
15878 xfree (copied_name);
c6da4cef
DE
15879
15880 if (file_is_pst)
15881 return NULL;
15882 return include_name;
15883}
15884
c91513d8
PP
15885/* Ignore this record_line request. */
15886
15887static void
15888noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15889{
15890 return;
15891}
15892
f3f5162e
DE
15893/* Subroutine of dwarf_decode_lines to simplify it.
15894 Process the line number information in LH. */
debd256d 15895
c906108c 15896static void
f3f5162e
DE
15897dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15898 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15899{
d521ce57
TT
15900 const gdb_byte *line_ptr, *extended_end;
15901 const gdb_byte *line_end;
a8c50c1f 15902 unsigned int bytes_read, extended_len;
c906108c 15903 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15904 CORE_ADDR baseaddr;
15905 struct objfile *objfile = cu->objfile;
f3f5162e 15906 bfd *abfd = objfile->obfd;
fbf65064 15907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15908 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15909 struct subfile *last_subfile = NULL;
c91513d8
PP
15910 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15911 = record_line;
e142c38c
DJ
15912
15913 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15914
debd256d
JB
15915 line_ptr = lh->statement_program_start;
15916 line_end = lh->statement_program_end;
c906108c
SS
15917
15918 /* Read the statement sequences until there's nothing left. */
15919 while (line_ptr < line_end)
15920 {
15921 /* state machine registers */
15922 CORE_ADDR address = 0;
15923 unsigned int file = 1;
15924 unsigned int line = 1;
15925 unsigned int column = 0;
debd256d 15926 int is_stmt = lh->default_is_stmt;
c906108c
SS
15927 int basic_block = 0;
15928 int end_sequence = 0;
fbf65064 15929 CORE_ADDR addr;
2dc7f7b3 15930 unsigned char op_index = 0;
c906108c 15931
aaa75496 15932 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15933 {
aaa75496 15934 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15935 /* lh->include_dirs and lh->file_names are 0-based, but the
15936 directory and file name numbers in the statement program
15937 are 1-based. */
15938 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15939 const char *dir = NULL;
a738430d 15940
debd256d
JB
15941 if (fe->dir_index)
15942 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15943
15944 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15945 }
15946
a738430d 15947 /* Decode the table. */
c5aa993b 15948 while (!end_sequence)
c906108c
SS
15949 {
15950 op_code = read_1_byte (abfd, line_ptr);
15951 line_ptr += 1;
59205f5a
JB
15952 if (line_ptr > line_end)
15953 {
15954 dwarf2_debug_line_missing_end_sequence_complaint ();
15955 break;
15956 }
9aa1fe7e 15957
debd256d 15958 if (op_code >= lh->opcode_base)
6e70227d 15959 {
a738430d 15960 /* Special operand. */
debd256d 15961 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15962 address += (((op_index + (adj_opcode / lh->line_range))
15963 / lh->maximum_ops_per_instruction)
15964 * lh->minimum_instruction_length);
15965 op_index = ((op_index + (adj_opcode / lh->line_range))
15966 % lh->maximum_ops_per_instruction);
debd256d 15967 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15968 if (lh->num_file_names < file || file == 0)
25e43795 15969 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15970 /* For now we ignore lines not starting on an
15971 instruction boundary. */
15972 else if (op_index == 0)
25e43795
DJ
15973 {
15974 lh->file_names[file - 1].included_p = 1;
ca5f395d 15975 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15976 {
15977 if (last_subfile != current_subfile)
15978 {
15979 addr = gdbarch_addr_bits_remove (gdbarch, address);
15980 if (last_subfile)
c91513d8 15981 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15982 last_subfile = current_subfile;
15983 }
25e43795 15984 /* Append row to matrix using current values. */
7019d805 15985 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15986 (*p_record_line) (current_subfile, line, addr);
366da635 15987 }
25e43795 15988 }
ca5f395d 15989 basic_block = 0;
9aa1fe7e
GK
15990 }
15991 else switch (op_code)
c906108c
SS
15992 {
15993 case DW_LNS_extended_op:
3e43a32a
MS
15994 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15995 &bytes_read);
473b7be6 15996 line_ptr += bytes_read;
a8c50c1f 15997 extended_end = line_ptr + extended_len;
c906108c
SS
15998 extended_op = read_1_byte (abfd, line_ptr);
15999 line_ptr += 1;
16000 switch (extended_op)
16001 {
16002 case DW_LNE_end_sequence:
c91513d8 16003 p_record_line = record_line;
c906108c 16004 end_sequence = 1;
c906108c
SS
16005 break;
16006 case DW_LNE_set_address:
e7c27a73 16007 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
16008
16009 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16010 {
16011 /* This line table is for a function which has been
16012 GCd by the linker. Ignore it. PR gdb/12528 */
16013
16014 long line_offset
36586728 16015 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16016
16017 complaint (&symfile_complaints,
16018 _(".debug_line address at offset 0x%lx is 0 "
16019 "[in module %s]"),
bb5ed363 16020 line_offset, objfile->name);
c91513d8
PP
16021 p_record_line = noop_record_line;
16022 }
16023
2dc7f7b3 16024 op_index = 0;
107d2387
AC
16025 line_ptr += bytes_read;
16026 address += baseaddr;
c906108c
SS
16027 break;
16028 case DW_LNE_define_file:
debd256d 16029 {
d521ce57 16030 const char *cur_file;
debd256d 16031 unsigned int dir_index, mod_time, length;
6e70227d 16032
3e43a32a
MS
16033 cur_file = read_direct_string (abfd, line_ptr,
16034 &bytes_read);
debd256d
JB
16035 line_ptr += bytes_read;
16036 dir_index =
16037 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16038 line_ptr += bytes_read;
16039 mod_time =
16040 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16041 line_ptr += bytes_read;
16042 length =
16043 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16044 line_ptr += bytes_read;
16045 add_file_name (lh, cur_file, dir_index, mod_time, length);
16046 }
c906108c 16047 break;
d0c6ba3d
CC
16048 case DW_LNE_set_discriminator:
16049 /* The discriminator is not interesting to the debugger;
16050 just ignore it. */
16051 line_ptr = extended_end;
16052 break;
c906108c 16053 default:
4d3c2250 16054 complaint (&symfile_complaints,
e2e0b3e5 16055 _("mangled .debug_line section"));
debd256d 16056 return;
c906108c 16057 }
a8c50c1f
DJ
16058 /* Make sure that we parsed the extended op correctly. If e.g.
16059 we expected a different address size than the producer used,
16060 we may have read the wrong number of bytes. */
16061 if (line_ptr != extended_end)
16062 {
16063 complaint (&symfile_complaints,
16064 _("mangled .debug_line section"));
16065 return;
16066 }
c906108c
SS
16067 break;
16068 case DW_LNS_copy:
59205f5a 16069 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16070 dwarf2_debug_line_missing_file_complaint ();
16071 else
366da635 16072 {
25e43795 16073 lh->file_names[file - 1].included_p = 1;
ca5f395d 16074 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16075 {
16076 if (last_subfile != current_subfile)
16077 {
16078 addr = gdbarch_addr_bits_remove (gdbarch, address);
16079 if (last_subfile)
c91513d8 16080 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16081 last_subfile = current_subfile;
16082 }
7019d805 16083 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16084 (*p_record_line) (current_subfile, line, addr);
fbf65064 16085 }
366da635 16086 }
c906108c
SS
16087 basic_block = 0;
16088 break;
16089 case DW_LNS_advance_pc:
2dc7f7b3
TT
16090 {
16091 CORE_ADDR adjust
16092 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16093
16094 address += (((op_index + adjust)
16095 / lh->maximum_ops_per_instruction)
16096 * lh->minimum_instruction_length);
16097 op_index = ((op_index + adjust)
16098 % lh->maximum_ops_per_instruction);
16099 line_ptr += bytes_read;
16100 }
c906108c
SS
16101 break;
16102 case DW_LNS_advance_line:
16103 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16104 line_ptr += bytes_read;
16105 break;
16106 case DW_LNS_set_file:
debd256d 16107 {
a738430d
MK
16108 /* The arrays lh->include_dirs and lh->file_names are
16109 0-based, but the directory and file name numbers in
16110 the statement program are 1-based. */
debd256d 16111 struct file_entry *fe;
d521ce57 16112 const char *dir = NULL;
a738430d 16113
debd256d
JB
16114 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16115 line_ptr += bytes_read;
59205f5a 16116 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16117 dwarf2_debug_line_missing_file_complaint ();
16118 else
16119 {
16120 fe = &lh->file_names[file - 1];
16121 if (fe->dir_index)
16122 dir = lh->include_dirs[fe->dir_index - 1];
16123 if (!decode_for_pst_p)
16124 {
16125 last_subfile = current_subfile;
16126 dwarf2_start_subfile (fe->name, dir, comp_dir);
16127 }
16128 }
debd256d 16129 }
c906108c
SS
16130 break;
16131 case DW_LNS_set_column:
16132 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16133 line_ptr += bytes_read;
16134 break;
16135 case DW_LNS_negate_stmt:
16136 is_stmt = (!is_stmt);
16137 break;
16138 case DW_LNS_set_basic_block:
16139 basic_block = 1;
16140 break;
c2c6d25f
JM
16141 /* Add to the address register of the state machine the
16142 address increment value corresponding to special opcode
a738430d
MK
16143 255. I.e., this value is scaled by the minimum
16144 instruction length since special opcode 255 would have
b021a221 16145 scaled the increment. */
c906108c 16146 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16147 {
16148 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16149
16150 address += (((op_index + adjust)
16151 / lh->maximum_ops_per_instruction)
16152 * lh->minimum_instruction_length);
16153 op_index = ((op_index + adjust)
16154 % lh->maximum_ops_per_instruction);
16155 }
c906108c
SS
16156 break;
16157 case DW_LNS_fixed_advance_pc:
16158 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16159 op_index = 0;
c906108c
SS
16160 line_ptr += 2;
16161 break;
9aa1fe7e 16162 default:
a738430d
MK
16163 {
16164 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16165 int i;
a738430d 16166
debd256d 16167 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16168 {
16169 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16170 line_ptr += bytes_read;
16171 }
16172 }
c906108c
SS
16173 }
16174 }
59205f5a
JB
16175 if (lh->num_file_names < file || file == 0)
16176 dwarf2_debug_line_missing_file_complaint ();
16177 else
16178 {
16179 lh->file_names[file - 1].included_p = 1;
16180 if (!decode_for_pst_p)
fbf65064
UW
16181 {
16182 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16183 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16184 }
59205f5a 16185 }
c906108c 16186 }
f3f5162e
DE
16187}
16188
16189/* Decode the Line Number Program (LNP) for the given line_header
16190 structure and CU. The actual information extracted and the type
16191 of structures created from the LNP depends on the value of PST.
16192
16193 1. If PST is NULL, then this procedure uses the data from the program
16194 to create all necessary symbol tables, and their linetables.
16195
16196 2. If PST is not NULL, this procedure reads the program to determine
16197 the list of files included by the unit represented by PST, and
16198 builds all the associated partial symbol tables.
16199
16200 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16201 It is used for relative paths in the line table.
16202 NOTE: When processing partial symtabs (pst != NULL),
16203 comp_dir == pst->dirname.
16204
16205 NOTE: It is important that psymtabs have the same file name (via strcmp)
16206 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16207 symtab we don't use it in the name of the psymtabs we create.
16208 E.g. expand_line_sal requires this when finding psymtabs to expand.
16209 A good testcase for this is mb-inline.exp. */
16210
16211static void
16212dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16213 struct dwarf2_cu *cu, struct partial_symtab *pst,
16214 int want_line_info)
16215{
16216 struct objfile *objfile = cu->objfile;
16217 const int decode_for_pst_p = (pst != NULL);
16218 struct subfile *first_subfile = current_subfile;
16219
16220 if (want_line_info)
16221 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16222
16223 if (decode_for_pst_p)
16224 {
16225 int file_index;
16226
16227 /* Now that we're done scanning the Line Header Program, we can
16228 create the psymtab of each included file. */
16229 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16230 if (lh->file_names[file_index].included_p == 1)
16231 {
d521ce57 16232 const char *include_name =
c6da4cef
DE
16233 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16234 if (include_name != NULL)
aaa75496
JB
16235 dwarf2_create_include_psymtab (include_name, pst, objfile);
16236 }
16237 }
cb1df416
DJ
16238 else
16239 {
16240 /* Make sure a symtab is created for every file, even files
16241 which contain only variables (i.e. no code with associated
16242 line numbers). */
cb1df416 16243 int i;
cb1df416
DJ
16244
16245 for (i = 0; i < lh->num_file_names; i++)
16246 {
d521ce57 16247 const char *dir = NULL;
f3f5162e 16248 struct file_entry *fe;
9a619af0 16249
cb1df416
DJ
16250 fe = &lh->file_names[i];
16251 if (fe->dir_index)
16252 dir = lh->include_dirs[fe->dir_index - 1];
16253 dwarf2_start_subfile (fe->name, dir, comp_dir);
16254
16255 /* Skip the main file; we don't need it, and it must be
16256 allocated last, so that it will show up before the
16257 non-primary symtabs in the objfile's symtab list. */
16258 if (current_subfile == first_subfile)
16259 continue;
16260
16261 if (current_subfile->symtab == NULL)
16262 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16263 objfile);
cb1df416
DJ
16264 fe->symtab = current_subfile->symtab;
16265 }
16266 }
c906108c
SS
16267}
16268
16269/* Start a subfile for DWARF. FILENAME is the name of the file and
16270 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16271 or NULL if not known. COMP_DIR is the compilation directory for the
16272 linetable's compilation unit or NULL if not known.
c906108c
SS
16273 This routine tries to keep line numbers from identical absolute and
16274 relative file names in a common subfile.
16275
16276 Using the `list' example from the GDB testsuite, which resides in
16277 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16278 of /srcdir/list0.c yields the following debugging information for list0.c:
16279
c5aa993b
JM
16280 DW_AT_name: /srcdir/list0.c
16281 DW_AT_comp_dir: /compdir
357e46e7 16282 files.files[0].name: list0.h
c5aa993b 16283 files.files[0].dir: /srcdir
357e46e7 16284 files.files[1].name: list0.c
c5aa993b 16285 files.files[1].dir: /srcdir
c906108c
SS
16286
16287 The line number information for list0.c has to end up in a single
4f1520fb
FR
16288 subfile, so that `break /srcdir/list0.c:1' works as expected.
16289 start_subfile will ensure that this happens provided that we pass the
16290 concatenation of files.files[1].dir and files.files[1].name as the
16291 subfile's name. */
c906108c
SS
16292
16293static void
d521ce57 16294dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16295 const char *comp_dir)
c906108c 16296{
d521ce57 16297 char *copy = NULL;
4f1520fb
FR
16298
16299 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16300 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16301 second argument to start_subfile. To be consistent, we do the
16302 same here. In order not to lose the line information directory,
16303 we concatenate it to the filename when it makes sense.
16304 Note that the Dwarf3 standard says (speaking of filenames in line
16305 information): ``The directory index is ignored for file names
16306 that represent full path names''. Thus ignoring dirname in the
16307 `else' branch below isn't an issue. */
c906108c 16308
d5166ae1 16309 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16310 {
16311 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16312 filename = copy;
16313 }
c906108c 16314
d521ce57 16315 start_subfile (filename, comp_dir);
4f1520fb 16316
d521ce57
TT
16317 if (copy != NULL)
16318 xfree (copy);
c906108c
SS
16319}
16320
f4dc4d17
DE
16321/* Start a symtab for DWARF.
16322 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16323
16324static void
16325dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16326 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16327{
16328 start_symtab (name, comp_dir, low_pc);
16329 record_debugformat ("DWARF 2");
16330 record_producer (cu->producer);
16331
16332 /* We assume that we're processing GCC output. */
16333 processing_gcc_compilation = 2;
16334
4d4ec4e5 16335 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16336}
16337
4c2df51b
DJ
16338static void
16339var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16340 struct dwarf2_cu *cu)
4c2df51b 16341{
e7c27a73
DJ
16342 struct objfile *objfile = cu->objfile;
16343 struct comp_unit_head *cu_header = &cu->header;
16344
4c2df51b
DJ
16345 /* NOTE drow/2003-01-30: There used to be a comment and some special
16346 code here to turn a symbol with DW_AT_external and a
16347 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16348 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16349 with some versions of binutils) where shared libraries could have
16350 relocations against symbols in their debug information - the
16351 minimal symbol would have the right address, but the debug info
16352 would not. It's no longer necessary, because we will explicitly
16353 apply relocations when we read in the debug information now. */
16354
16355 /* A DW_AT_location attribute with no contents indicates that a
16356 variable has been optimized away. */
16357 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16358 {
f1e6e072 16359 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16360 return;
16361 }
16362
16363 /* Handle one degenerate form of location expression specially, to
16364 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16365 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16366 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16367
16368 if (attr_form_is_block (attr)
3019eac3
DE
16369 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16370 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16371 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16372 && (DW_BLOCK (attr)->size
16373 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16374 {
891d2f0b 16375 unsigned int dummy;
4c2df51b 16376
3019eac3
DE
16377 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16378 SYMBOL_VALUE_ADDRESS (sym) =
16379 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16380 else
16381 SYMBOL_VALUE_ADDRESS (sym) =
16382 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16383 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16384 fixup_symbol_section (sym, objfile);
16385 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16386 SYMBOL_SECTION (sym));
4c2df51b
DJ
16387 return;
16388 }
16389
16390 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16391 expression evaluator, and use LOC_COMPUTED only when necessary
16392 (i.e. when the value of a register or memory location is
16393 referenced, or a thread-local block, etc.). Then again, it might
16394 not be worthwhile. I'm assuming that it isn't unless performance
16395 or memory numbers show me otherwise. */
16396
f1e6e072 16397 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16398
f1e6e072 16399 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16400 cu->has_loclist = 1;
4c2df51b
DJ
16401}
16402
c906108c
SS
16403/* Given a pointer to a DWARF information entry, figure out if we need
16404 to make a symbol table entry for it, and if so, create a new entry
16405 and return a pointer to it.
16406 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16407 used the passed type.
16408 If SPACE is not NULL, use it to hold the new symbol. If it is
16409 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16410
16411static struct symbol *
34eaf542
TT
16412new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16413 struct symbol *space)
c906108c 16414{
e7c27a73 16415 struct objfile *objfile = cu->objfile;
c906108c 16416 struct symbol *sym = NULL;
15d034d0 16417 const char *name;
c906108c
SS
16418 struct attribute *attr = NULL;
16419 struct attribute *attr2 = NULL;
e142c38c 16420 CORE_ADDR baseaddr;
e37fd15a
SW
16421 struct pending **list_to_add = NULL;
16422
edb3359d 16423 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16424
16425 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16426
94af9270 16427 name = dwarf2_name (die, cu);
c906108c
SS
16428 if (name)
16429 {
94af9270 16430 const char *linkagename;
34eaf542 16431 int suppress_add = 0;
94af9270 16432
34eaf542
TT
16433 if (space)
16434 sym = space;
16435 else
e623cf5d 16436 sym = allocate_symbol (objfile);
c906108c 16437 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16438
16439 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16440 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16441 linkagename = dwarf2_physname (name, die, cu);
16442 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16443
f55ee35c
JK
16444 /* Fortran does not have mangling standard and the mangling does differ
16445 between gfortran, iFort etc. */
16446 if (cu->language == language_fortran
b250c185 16447 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16448 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16449 dwarf2_full_name (name, die, cu),
29df156d 16450 NULL);
f55ee35c 16451
c906108c 16452 /* Default assumptions.
c5aa993b 16453 Use the passed type or decode it from the die. */
176620f1 16454 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16455 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16456 if (type != NULL)
16457 SYMBOL_TYPE (sym) = type;
16458 else
e7c27a73 16459 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16460 attr = dwarf2_attr (die,
16461 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16462 cu);
c906108c
SS
16463 if (attr)
16464 {
16465 SYMBOL_LINE (sym) = DW_UNSND (attr);
16466 }
cb1df416 16467
edb3359d
DJ
16468 attr = dwarf2_attr (die,
16469 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16470 cu);
cb1df416
DJ
16471 if (attr)
16472 {
16473 int file_index = DW_UNSND (attr);
9a619af0 16474
cb1df416
DJ
16475 if (cu->line_header == NULL
16476 || file_index > cu->line_header->num_file_names)
16477 complaint (&symfile_complaints,
16478 _("file index out of range"));
1c3d648d 16479 else if (file_index > 0)
cb1df416
DJ
16480 {
16481 struct file_entry *fe;
9a619af0 16482
cb1df416
DJ
16483 fe = &cu->line_header->file_names[file_index - 1];
16484 SYMBOL_SYMTAB (sym) = fe->symtab;
16485 }
16486 }
16487
c906108c
SS
16488 switch (die->tag)
16489 {
16490 case DW_TAG_label:
e142c38c 16491 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16492 if (attr)
16493 {
16494 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16495 }
0f5238ed
TT
16496 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16497 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16498 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16499 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16500 break;
16501 case DW_TAG_subprogram:
16502 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16503 finish_block. */
f1e6e072 16504 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16505 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16506 if ((attr2 && (DW_UNSND (attr2) != 0))
16507 || cu->language == language_ada)
c906108c 16508 {
2cfa0c8d
JB
16509 /* Subprograms marked external are stored as a global symbol.
16510 Ada subprograms, whether marked external or not, are always
16511 stored as a global symbol, because we want to be able to
16512 access them globally. For instance, we want to be able
16513 to break on a nested subprogram without having to
16514 specify the context. */
e37fd15a 16515 list_to_add = &global_symbols;
c906108c
SS
16516 }
16517 else
16518 {
e37fd15a 16519 list_to_add = cu->list_in_scope;
c906108c
SS
16520 }
16521 break;
edb3359d
DJ
16522 case DW_TAG_inlined_subroutine:
16523 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16524 finish_block. */
f1e6e072 16525 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16526 SYMBOL_INLINED (sym) = 1;
481860b3 16527 list_to_add = cu->list_in_scope;
edb3359d 16528 break;
34eaf542
TT
16529 case DW_TAG_template_value_param:
16530 suppress_add = 1;
16531 /* Fall through. */
72929c62 16532 case DW_TAG_constant:
c906108c 16533 case DW_TAG_variable:
254e6b9e 16534 case DW_TAG_member:
0963b4bd
MS
16535 /* Compilation with minimal debug info may result in
16536 variables with missing type entries. Change the
16537 misleading `void' type to something sensible. */
c906108c 16538 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16539 SYMBOL_TYPE (sym)
46bf5051 16540 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16541
e142c38c 16542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16543 /* In the case of DW_TAG_member, we should only be called for
16544 static const members. */
16545 if (die->tag == DW_TAG_member)
16546 {
3863f96c
DE
16547 /* dwarf2_add_field uses die_is_declaration,
16548 so we do the same. */
254e6b9e
DE
16549 gdb_assert (die_is_declaration (die, cu));
16550 gdb_assert (attr);
16551 }
c906108c
SS
16552 if (attr)
16553 {
e7c27a73 16554 dwarf2_const_value (attr, sym, cu);
e142c38c 16555 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16556 if (!suppress_add)
34eaf542
TT
16557 {
16558 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16559 list_to_add = &global_symbols;
34eaf542 16560 else
e37fd15a 16561 list_to_add = cu->list_in_scope;
34eaf542 16562 }
c906108c
SS
16563 break;
16564 }
e142c38c 16565 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16566 if (attr)
16567 {
e7c27a73 16568 var_decode_location (attr, sym, cu);
e142c38c 16569 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16570
16571 /* Fortran explicitly imports any global symbols to the local
16572 scope by DW_TAG_common_block. */
16573 if (cu->language == language_fortran && die->parent
16574 && die->parent->tag == DW_TAG_common_block)
16575 attr2 = NULL;
16576
caac4577
JG
16577 if (SYMBOL_CLASS (sym) == LOC_STATIC
16578 && SYMBOL_VALUE_ADDRESS (sym) == 0
16579 && !dwarf2_per_objfile->has_section_at_zero)
16580 {
16581 /* When a static variable is eliminated by the linker,
16582 the corresponding debug information is not stripped
16583 out, but the variable address is set to null;
16584 do not add such variables into symbol table. */
16585 }
16586 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16587 {
f55ee35c
JK
16588 /* Workaround gfortran PR debug/40040 - it uses
16589 DW_AT_location for variables in -fPIC libraries which may
16590 get overriden by other libraries/executable and get
16591 a different address. Resolve it by the minimal symbol
16592 which may come from inferior's executable using copy
16593 relocation. Make this workaround only for gfortran as for
16594 other compilers GDB cannot guess the minimal symbol
16595 Fortran mangling kind. */
16596 if (cu->language == language_fortran && die->parent
16597 && die->parent->tag == DW_TAG_module
16598 && cu->producer
16599 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16600 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16601
1c809c68
TT
16602 /* A variable with DW_AT_external is never static,
16603 but it may be block-scoped. */
16604 list_to_add = (cu->list_in_scope == &file_symbols
16605 ? &global_symbols : cu->list_in_scope);
1c809c68 16606 }
c906108c 16607 else
e37fd15a 16608 list_to_add = cu->list_in_scope;
c906108c
SS
16609 }
16610 else
16611 {
16612 /* We do not know the address of this symbol.
c5aa993b
JM
16613 If it is an external symbol and we have type information
16614 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16615 The address of the variable will then be determined from
16616 the minimal symbol table whenever the variable is
16617 referenced. */
e142c38c 16618 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16619
16620 /* Fortran explicitly imports any global symbols to the local
16621 scope by DW_TAG_common_block. */
16622 if (cu->language == language_fortran && die->parent
16623 && die->parent->tag == DW_TAG_common_block)
16624 {
16625 /* SYMBOL_CLASS doesn't matter here because
16626 read_common_block is going to reset it. */
16627 if (!suppress_add)
16628 list_to_add = cu->list_in_scope;
16629 }
16630 else if (attr2 && (DW_UNSND (attr2) != 0)
16631 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16632 {
0fe7935b
DJ
16633 /* A variable with DW_AT_external is never static, but it
16634 may be block-scoped. */
16635 list_to_add = (cu->list_in_scope == &file_symbols
16636 ? &global_symbols : cu->list_in_scope);
16637
f1e6e072 16638 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16639 }
442ddf59
JK
16640 else if (!die_is_declaration (die, cu))
16641 {
16642 /* Use the default LOC_OPTIMIZED_OUT class. */
16643 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16644 if (!suppress_add)
16645 list_to_add = cu->list_in_scope;
442ddf59 16646 }
c906108c
SS
16647 }
16648 break;
16649 case DW_TAG_formal_parameter:
edb3359d
DJ
16650 /* If we are inside a function, mark this as an argument. If
16651 not, we might be looking at an argument to an inlined function
16652 when we do not have enough information to show inlined frames;
16653 pretend it's a local variable in that case so that the user can
16654 still see it. */
16655 if (context_stack_depth > 0
16656 && context_stack[context_stack_depth - 1].name != NULL)
16657 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16658 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16659 if (attr)
16660 {
e7c27a73 16661 var_decode_location (attr, sym, cu);
c906108c 16662 }
e142c38c 16663 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16664 if (attr)
16665 {
e7c27a73 16666 dwarf2_const_value (attr, sym, cu);
c906108c 16667 }
f346a30d 16668
e37fd15a 16669 list_to_add = cu->list_in_scope;
c906108c
SS
16670 break;
16671 case DW_TAG_unspecified_parameters:
16672 /* From varargs functions; gdb doesn't seem to have any
16673 interest in this information, so just ignore it for now.
16674 (FIXME?) */
16675 break;
34eaf542
TT
16676 case DW_TAG_template_type_param:
16677 suppress_add = 1;
16678 /* Fall through. */
c906108c 16679 case DW_TAG_class_type:
680b30c7 16680 case DW_TAG_interface_type:
c906108c
SS
16681 case DW_TAG_structure_type:
16682 case DW_TAG_union_type:
72019c9c 16683 case DW_TAG_set_type:
c906108c 16684 case DW_TAG_enumeration_type:
f1e6e072 16685 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16686 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16687
63d06c5c 16688 {
987504bb 16689 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16690 really ever be static objects: otherwise, if you try
16691 to, say, break of a class's method and you're in a file
16692 which doesn't mention that class, it won't work unless
16693 the check for all static symbols in lookup_symbol_aux
16694 saves you. See the OtherFileClass tests in
16695 gdb.c++/namespace.exp. */
16696
e37fd15a 16697 if (!suppress_add)
34eaf542 16698 {
34eaf542
TT
16699 list_to_add = (cu->list_in_scope == &file_symbols
16700 && (cu->language == language_cplus
16701 || cu->language == language_java)
16702 ? &global_symbols : cu->list_in_scope);
63d06c5c 16703
64382290
TT
16704 /* The semantics of C++ state that "struct foo {
16705 ... }" also defines a typedef for "foo". A Java
16706 class declaration also defines a typedef for the
16707 class. */
16708 if (cu->language == language_cplus
16709 || cu->language == language_java
16710 || cu->language == language_ada)
16711 {
16712 /* The symbol's name is already allocated along
16713 with this objfile, so we don't need to
16714 duplicate it for the type. */
16715 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16716 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16717 }
63d06c5c
DC
16718 }
16719 }
c906108c
SS
16720 break;
16721 case DW_TAG_typedef:
f1e6e072 16722 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16723 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16724 list_to_add = cu->list_in_scope;
63d06c5c 16725 break;
c906108c 16726 case DW_TAG_base_type:
a02abb62 16727 case DW_TAG_subrange_type:
f1e6e072 16728 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16729 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16730 list_to_add = cu->list_in_scope;
c906108c
SS
16731 break;
16732 case DW_TAG_enumerator:
e142c38c 16733 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16734 if (attr)
16735 {
e7c27a73 16736 dwarf2_const_value (attr, sym, cu);
c906108c 16737 }
63d06c5c
DC
16738 {
16739 /* NOTE: carlton/2003-11-10: See comment above in the
16740 DW_TAG_class_type, etc. block. */
16741
e142c38c 16742 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16743 && (cu->language == language_cplus
16744 || cu->language == language_java)
e142c38c 16745 ? &global_symbols : cu->list_in_scope);
63d06c5c 16746 }
c906108c 16747 break;
5c4e30ca 16748 case DW_TAG_namespace:
f1e6e072 16749 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16750 list_to_add = &global_symbols;
5c4e30ca 16751 break;
4357ac6c 16752 case DW_TAG_common_block:
f1e6e072 16753 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16754 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16755 add_symbol_to_list (sym, cu->list_in_scope);
16756 break;
c906108c
SS
16757 default:
16758 /* Not a tag we recognize. Hopefully we aren't processing
16759 trash data, but since we must specifically ignore things
16760 we don't recognize, there is nothing else we should do at
0963b4bd 16761 this point. */
e2e0b3e5 16762 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16763 dwarf_tag_name (die->tag));
c906108c
SS
16764 break;
16765 }
df8a16a1 16766
e37fd15a
SW
16767 if (suppress_add)
16768 {
16769 sym->hash_next = objfile->template_symbols;
16770 objfile->template_symbols = sym;
16771 list_to_add = NULL;
16772 }
16773
16774 if (list_to_add != NULL)
16775 add_symbol_to_list (sym, list_to_add);
16776
df8a16a1
DJ
16777 /* For the benefit of old versions of GCC, check for anonymous
16778 namespaces based on the demangled name. */
4d4ec4e5 16779 if (!cu->processing_has_namespace_info
94af9270 16780 && cu->language == language_cplus)
a10964d1 16781 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16782 }
16783 return (sym);
16784}
16785
34eaf542
TT
16786/* A wrapper for new_symbol_full that always allocates a new symbol. */
16787
16788static struct symbol *
16789new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16790{
16791 return new_symbol_full (die, type, cu, NULL);
16792}
16793
98bfdba5
PA
16794/* Given an attr with a DW_FORM_dataN value in host byte order,
16795 zero-extend it as appropriate for the symbol's type. The DWARF
16796 standard (v4) is not entirely clear about the meaning of using
16797 DW_FORM_dataN for a constant with a signed type, where the type is
16798 wider than the data. The conclusion of a discussion on the DWARF
16799 list was that this is unspecified. We choose to always zero-extend
16800 because that is the interpretation long in use by GCC. */
c906108c 16801
98bfdba5 16802static gdb_byte *
ff39bb5e 16803dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16804 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16805{
e7c27a73 16806 struct objfile *objfile = cu->objfile;
e17a4113
UW
16807 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16808 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16809 LONGEST l = DW_UNSND (attr);
16810
16811 if (bits < sizeof (*value) * 8)
16812 {
16813 l &= ((LONGEST) 1 << bits) - 1;
16814 *value = l;
16815 }
16816 else if (bits == sizeof (*value) * 8)
16817 *value = l;
16818 else
16819 {
16820 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16821 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16822 return bytes;
16823 }
16824
16825 return NULL;
16826}
16827
16828/* Read a constant value from an attribute. Either set *VALUE, or if
16829 the value does not fit in *VALUE, set *BYTES - either already
16830 allocated on the objfile obstack, or newly allocated on OBSTACK,
16831 or, set *BATON, if we translated the constant to a location
16832 expression. */
16833
16834static void
ff39bb5e 16835dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16836 const char *name, struct obstack *obstack,
16837 struct dwarf2_cu *cu,
d521ce57 16838 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16839 struct dwarf2_locexpr_baton **baton)
16840{
16841 struct objfile *objfile = cu->objfile;
16842 struct comp_unit_head *cu_header = &cu->header;
c906108c 16843 struct dwarf_block *blk;
98bfdba5
PA
16844 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16845 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16846
16847 *value = 0;
16848 *bytes = NULL;
16849 *baton = NULL;
c906108c
SS
16850
16851 switch (attr->form)
16852 {
16853 case DW_FORM_addr:
3019eac3 16854 case DW_FORM_GNU_addr_index:
ac56253d 16855 {
ac56253d
TT
16856 gdb_byte *data;
16857
98bfdba5
PA
16858 if (TYPE_LENGTH (type) != cu_header->addr_size)
16859 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16860 cu_header->addr_size,
98bfdba5 16861 TYPE_LENGTH (type));
ac56253d
TT
16862 /* Symbols of this form are reasonably rare, so we just
16863 piggyback on the existing location code rather than writing
16864 a new implementation of symbol_computed_ops. */
7919a973 16865 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16866 (*baton)->per_cu = cu->per_cu;
16867 gdb_assert ((*baton)->per_cu);
ac56253d 16868
98bfdba5 16869 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16870 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16871 (*baton)->data = data;
ac56253d
TT
16872
16873 data[0] = DW_OP_addr;
16874 store_unsigned_integer (&data[1], cu_header->addr_size,
16875 byte_order, DW_ADDR (attr));
16876 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16877 }
c906108c 16878 break;
4ac36638 16879 case DW_FORM_string:
93b5768b 16880 case DW_FORM_strp:
3019eac3 16881 case DW_FORM_GNU_str_index:
36586728 16882 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16883 /* DW_STRING is already allocated on the objfile obstack, point
16884 directly to it. */
d521ce57 16885 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16886 break;
c906108c
SS
16887 case DW_FORM_block1:
16888 case DW_FORM_block2:
16889 case DW_FORM_block4:
16890 case DW_FORM_block:
2dc7f7b3 16891 case DW_FORM_exprloc:
c906108c 16892 blk = DW_BLOCK (attr);
98bfdba5
PA
16893 if (TYPE_LENGTH (type) != blk->size)
16894 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16895 TYPE_LENGTH (type));
16896 *bytes = blk->data;
c906108c 16897 break;
2df3850c
JM
16898
16899 /* The DW_AT_const_value attributes are supposed to carry the
16900 symbol's value "represented as it would be on the target
16901 architecture." By the time we get here, it's already been
16902 converted to host endianness, so we just need to sign- or
16903 zero-extend it as appropriate. */
16904 case DW_FORM_data1:
3aef2284 16905 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16906 break;
c906108c 16907 case DW_FORM_data2:
3aef2284 16908 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16909 break;
c906108c 16910 case DW_FORM_data4:
3aef2284 16911 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16912 break;
c906108c 16913 case DW_FORM_data8:
3aef2284 16914 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16915 break;
16916
c906108c 16917 case DW_FORM_sdata:
98bfdba5 16918 *value = DW_SND (attr);
2df3850c
JM
16919 break;
16920
c906108c 16921 case DW_FORM_udata:
98bfdba5 16922 *value = DW_UNSND (attr);
c906108c 16923 break;
2df3850c 16924
c906108c 16925 default:
4d3c2250 16926 complaint (&symfile_complaints,
e2e0b3e5 16927 _("unsupported const value attribute form: '%s'"),
4d3c2250 16928 dwarf_form_name (attr->form));
98bfdba5 16929 *value = 0;
c906108c
SS
16930 break;
16931 }
16932}
16933
2df3850c 16934
98bfdba5
PA
16935/* Copy constant value from an attribute to a symbol. */
16936
2df3850c 16937static void
ff39bb5e 16938dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 16939 struct dwarf2_cu *cu)
2df3850c 16940{
98bfdba5
PA
16941 struct objfile *objfile = cu->objfile;
16942 struct comp_unit_head *cu_header = &cu->header;
12df843f 16943 LONGEST value;
d521ce57 16944 const gdb_byte *bytes;
98bfdba5 16945 struct dwarf2_locexpr_baton *baton;
2df3850c 16946
98bfdba5
PA
16947 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16948 SYMBOL_PRINT_NAME (sym),
16949 &objfile->objfile_obstack, cu,
16950 &value, &bytes, &baton);
2df3850c 16951
98bfdba5
PA
16952 if (baton != NULL)
16953 {
98bfdba5 16954 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16955 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16956 }
16957 else if (bytes != NULL)
16958 {
16959 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16960 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16961 }
16962 else
16963 {
16964 SYMBOL_VALUE (sym) = value;
f1e6e072 16965 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16966 }
2df3850c
JM
16967}
16968
c906108c
SS
16969/* Return the type of the die in question using its DW_AT_type attribute. */
16970
16971static struct type *
e7c27a73 16972die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16973{
c906108c 16974 struct attribute *type_attr;
c906108c 16975
e142c38c 16976 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16977 if (!type_attr)
16978 {
16979 /* A missing DW_AT_type represents a void type. */
46bf5051 16980 return objfile_type (cu->objfile)->builtin_void;
c906108c 16981 }
348e048f 16982
673bfd45 16983 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16984}
16985
b4ba55a1
JB
16986/* True iff CU's producer generates GNAT Ada auxiliary information
16987 that allows to find parallel types through that information instead
16988 of having to do expensive parallel lookups by type name. */
16989
16990static int
16991need_gnat_info (struct dwarf2_cu *cu)
16992{
16993 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16994 of GNAT produces this auxiliary information, without any indication
16995 that it is produced. Part of enhancing the FSF version of GNAT
16996 to produce that information will be to put in place an indicator
16997 that we can use in order to determine whether the descriptive type
16998 info is available or not. One suggestion that has been made is
16999 to use a new attribute, attached to the CU die. For now, assume
17000 that the descriptive type info is not available. */
17001 return 0;
17002}
17003
b4ba55a1
JB
17004/* Return the auxiliary type of the die in question using its
17005 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17006 attribute is not present. */
17007
17008static struct type *
17009die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17010{
b4ba55a1 17011 struct attribute *type_attr;
b4ba55a1
JB
17012
17013 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17014 if (!type_attr)
17015 return NULL;
17016
673bfd45 17017 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17018}
17019
17020/* If DIE has a descriptive_type attribute, then set the TYPE's
17021 descriptive type accordingly. */
17022
17023static void
17024set_descriptive_type (struct type *type, struct die_info *die,
17025 struct dwarf2_cu *cu)
17026{
17027 struct type *descriptive_type = die_descriptive_type (die, cu);
17028
17029 if (descriptive_type)
17030 {
17031 ALLOCATE_GNAT_AUX_TYPE (type);
17032 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17033 }
17034}
17035
c906108c
SS
17036/* Return the containing type of the die in question using its
17037 DW_AT_containing_type attribute. */
17038
17039static struct type *
e7c27a73 17040die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17041{
c906108c 17042 struct attribute *type_attr;
c906108c 17043
e142c38c 17044 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17045 if (!type_attr)
17046 error (_("Dwarf Error: Problem turning containing type into gdb type "
17047 "[in module %s]"), cu->objfile->name);
17048
673bfd45 17049 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17050}
17051
ac9ec31b
DE
17052/* Return an error marker type to use for the ill formed type in DIE/CU. */
17053
17054static struct type *
17055build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17056{
17057 struct objfile *objfile = dwarf2_per_objfile->objfile;
17058 char *message, *saved;
17059
17060 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17061 objfile->name,
17062 cu->header.offset.sect_off,
17063 die->offset.sect_off);
17064 saved = obstack_copy0 (&objfile->objfile_obstack,
17065 message, strlen (message));
17066 xfree (message);
17067
17068 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17069}
17070
673bfd45 17071/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17072 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17073 DW_AT_containing_type.
673bfd45
DE
17074 If there is no type substitute an error marker. */
17075
c906108c 17076static struct type *
ff39bb5e 17077lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17078 struct dwarf2_cu *cu)
c906108c 17079{
bb5ed363 17080 struct objfile *objfile = cu->objfile;
f792889a
DJ
17081 struct type *this_type;
17082
ac9ec31b
DE
17083 gdb_assert (attr->name == DW_AT_type
17084 || attr->name == DW_AT_GNAT_descriptive_type
17085 || attr->name == DW_AT_containing_type);
17086
673bfd45
DE
17087 /* First see if we have it cached. */
17088
36586728
TT
17089 if (attr->form == DW_FORM_GNU_ref_alt)
17090 {
17091 struct dwarf2_per_cu_data *per_cu;
17092 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17093
17094 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17095 this_type = get_die_type_at_offset (offset, per_cu);
17096 }
7771576e 17097 else if (attr_form_is_ref (attr))
673bfd45 17098 {
b64f50a1 17099 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17100
17101 this_type = get_die_type_at_offset (offset, cu->per_cu);
17102 }
55f1336d 17103 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17104 {
ac9ec31b 17105 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17106
ac9ec31b 17107 return get_signatured_type (die, signature, cu);
673bfd45
DE
17108 }
17109 else
17110 {
ac9ec31b
DE
17111 complaint (&symfile_complaints,
17112 _("Dwarf Error: Bad type attribute %s in DIE"
17113 " at 0x%x [in module %s]"),
17114 dwarf_attr_name (attr->name), die->offset.sect_off,
17115 objfile->name);
17116 return build_error_marker_type (cu, die);
673bfd45
DE
17117 }
17118
17119 /* If not cached we need to read it in. */
17120
17121 if (this_type == NULL)
17122 {
ac9ec31b 17123 struct die_info *type_die = NULL;
673bfd45
DE
17124 struct dwarf2_cu *type_cu = cu;
17125
7771576e 17126 if (attr_form_is_ref (attr))
ac9ec31b
DE
17127 type_die = follow_die_ref (die, attr, &type_cu);
17128 if (type_die == NULL)
17129 return build_error_marker_type (cu, die);
17130 /* If we find the type now, it's probably because the type came
3019eac3
DE
17131 from an inter-CU reference and the type's CU got expanded before
17132 ours. */
ac9ec31b 17133 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17134 }
17135
17136 /* If we still don't have a type use an error marker. */
17137
17138 if (this_type == NULL)
ac9ec31b 17139 return build_error_marker_type (cu, die);
673bfd45 17140
f792889a 17141 return this_type;
c906108c
SS
17142}
17143
673bfd45
DE
17144/* Return the type in DIE, CU.
17145 Returns NULL for invalid types.
17146
02142a6c 17147 This first does a lookup in die_type_hash,
673bfd45
DE
17148 and only reads the die in if necessary.
17149
17150 NOTE: This can be called when reading in partial or full symbols. */
17151
f792889a 17152static struct type *
e7c27a73 17153read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17154{
f792889a
DJ
17155 struct type *this_type;
17156
17157 this_type = get_die_type (die, cu);
17158 if (this_type)
17159 return this_type;
17160
673bfd45
DE
17161 return read_type_die_1 (die, cu);
17162}
17163
17164/* Read the type in DIE, CU.
17165 Returns NULL for invalid types. */
17166
17167static struct type *
17168read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17169{
17170 struct type *this_type = NULL;
17171
c906108c
SS
17172 switch (die->tag)
17173 {
17174 case DW_TAG_class_type:
680b30c7 17175 case DW_TAG_interface_type:
c906108c
SS
17176 case DW_TAG_structure_type:
17177 case DW_TAG_union_type:
f792889a 17178 this_type = read_structure_type (die, cu);
c906108c
SS
17179 break;
17180 case DW_TAG_enumeration_type:
f792889a 17181 this_type = read_enumeration_type (die, cu);
c906108c
SS
17182 break;
17183 case DW_TAG_subprogram:
17184 case DW_TAG_subroutine_type:
edb3359d 17185 case DW_TAG_inlined_subroutine:
f792889a 17186 this_type = read_subroutine_type (die, cu);
c906108c
SS
17187 break;
17188 case DW_TAG_array_type:
f792889a 17189 this_type = read_array_type (die, cu);
c906108c 17190 break;
72019c9c 17191 case DW_TAG_set_type:
f792889a 17192 this_type = read_set_type (die, cu);
72019c9c 17193 break;
c906108c 17194 case DW_TAG_pointer_type:
f792889a 17195 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17196 break;
17197 case DW_TAG_ptr_to_member_type:
f792889a 17198 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17199 break;
17200 case DW_TAG_reference_type:
f792889a 17201 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17202 break;
17203 case DW_TAG_const_type:
f792889a 17204 this_type = read_tag_const_type (die, cu);
c906108c
SS
17205 break;
17206 case DW_TAG_volatile_type:
f792889a 17207 this_type = read_tag_volatile_type (die, cu);
c906108c 17208 break;
06d66ee9
TT
17209 case DW_TAG_restrict_type:
17210 this_type = read_tag_restrict_type (die, cu);
17211 break;
c906108c 17212 case DW_TAG_string_type:
f792889a 17213 this_type = read_tag_string_type (die, cu);
c906108c
SS
17214 break;
17215 case DW_TAG_typedef:
f792889a 17216 this_type = read_typedef (die, cu);
c906108c 17217 break;
a02abb62 17218 case DW_TAG_subrange_type:
f792889a 17219 this_type = read_subrange_type (die, cu);
a02abb62 17220 break;
c906108c 17221 case DW_TAG_base_type:
f792889a 17222 this_type = read_base_type (die, cu);
c906108c 17223 break;
81a17f79 17224 case DW_TAG_unspecified_type:
f792889a 17225 this_type = read_unspecified_type (die, cu);
81a17f79 17226 break;
0114d602
DJ
17227 case DW_TAG_namespace:
17228 this_type = read_namespace_type (die, cu);
17229 break;
f55ee35c
JK
17230 case DW_TAG_module:
17231 this_type = read_module_type (die, cu);
17232 break;
c906108c 17233 default:
3e43a32a
MS
17234 complaint (&symfile_complaints,
17235 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17236 dwarf_tag_name (die->tag));
c906108c
SS
17237 break;
17238 }
63d06c5c 17239
f792889a 17240 return this_type;
63d06c5c
DC
17241}
17242
abc72ce4
DE
17243/* See if we can figure out if the class lives in a namespace. We do
17244 this by looking for a member function; its demangled name will
17245 contain namespace info, if there is any.
17246 Return the computed name or NULL.
17247 Space for the result is allocated on the objfile's obstack.
17248 This is the full-die version of guess_partial_die_structure_name.
17249 In this case we know DIE has no useful parent. */
17250
17251static char *
17252guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17253{
17254 struct die_info *spec_die;
17255 struct dwarf2_cu *spec_cu;
17256 struct die_info *child;
17257
17258 spec_cu = cu;
17259 spec_die = die_specification (die, &spec_cu);
17260 if (spec_die != NULL)
17261 {
17262 die = spec_die;
17263 cu = spec_cu;
17264 }
17265
17266 for (child = die->child;
17267 child != NULL;
17268 child = child->sibling)
17269 {
17270 if (child->tag == DW_TAG_subprogram)
17271 {
17272 struct attribute *attr;
17273
17274 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17275 if (attr == NULL)
17276 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17277 if (attr != NULL)
17278 {
17279 char *actual_name
17280 = language_class_name_from_physname (cu->language_defn,
17281 DW_STRING (attr));
17282 char *name = NULL;
17283
17284 if (actual_name != NULL)
17285 {
15d034d0 17286 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17287
17288 if (die_name != NULL
17289 && strcmp (die_name, actual_name) != 0)
17290 {
17291 /* Strip off the class name from the full name.
17292 We want the prefix. */
17293 int die_name_len = strlen (die_name);
17294 int actual_name_len = strlen (actual_name);
17295
17296 /* Test for '::' as a sanity check. */
17297 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17298 && actual_name[actual_name_len
17299 - die_name_len - 1] == ':')
abc72ce4 17300 name =
10f0c4bb
TT
17301 obstack_copy0 (&cu->objfile->objfile_obstack,
17302 actual_name,
17303 actual_name_len - die_name_len - 2);
abc72ce4
DE
17304 }
17305 }
17306 xfree (actual_name);
17307 return name;
17308 }
17309 }
17310 }
17311
17312 return NULL;
17313}
17314
96408a79
SA
17315/* GCC might emit a nameless typedef that has a linkage name. Determine the
17316 prefix part in such case. See
17317 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17318
17319static char *
17320anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17321{
17322 struct attribute *attr;
17323 char *base;
17324
17325 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17326 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17327 return NULL;
17328
17329 attr = dwarf2_attr (die, DW_AT_name, cu);
17330 if (attr != NULL && DW_STRING (attr) != NULL)
17331 return NULL;
17332
17333 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17334 if (attr == NULL)
17335 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17336 if (attr == NULL || DW_STRING (attr) == NULL)
17337 return NULL;
17338
17339 /* dwarf2_name had to be already called. */
17340 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17341
17342 /* Strip the base name, keep any leading namespaces/classes. */
17343 base = strrchr (DW_STRING (attr), ':');
17344 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17345 return "";
17346
10f0c4bb
TT
17347 return obstack_copy0 (&cu->objfile->objfile_obstack,
17348 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17349}
17350
fdde2d81 17351/* Return the name of the namespace/class that DIE is defined within,
0114d602 17352 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17353
0114d602
DJ
17354 For example, if we're within the method foo() in the following
17355 code:
17356
17357 namespace N {
17358 class C {
17359 void foo () {
17360 }
17361 };
17362 }
17363
17364 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17365
0d5cff50 17366static const char *
e142c38c 17367determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17368{
0114d602
DJ
17369 struct die_info *parent, *spec_die;
17370 struct dwarf2_cu *spec_cu;
17371 struct type *parent_type;
96408a79 17372 char *retval;
63d06c5c 17373
f55ee35c
JK
17374 if (cu->language != language_cplus && cu->language != language_java
17375 && cu->language != language_fortran)
0114d602
DJ
17376 return "";
17377
96408a79
SA
17378 retval = anonymous_struct_prefix (die, cu);
17379 if (retval)
17380 return retval;
17381
0114d602
DJ
17382 /* We have to be careful in the presence of DW_AT_specification.
17383 For example, with GCC 3.4, given the code
17384
17385 namespace N {
17386 void foo() {
17387 // Definition of N::foo.
17388 }
17389 }
17390
17391 then we'll have a tree of DIEs like this:
17392
17393 1: DW_TAG_compile_unit
17394 2: DW_TAG_namespace // N
17395 3: DW_TAG_subprogram // declaration of N::foo
17396 4: DW_TAG_subprogram // definition of N::foo
17397 DW_AT_specification // refers to die #3
17398
17399 Thus, when processing die #4, we have to pretend that we're in
17400 the context of its DW_AT_specification, namely the contex of die
17401 #3. */
17402 spec_cu = cu;
17403 spec_die = die_specification (die, &spec_cu);
17404 if (spec_die == NULL)
17405 parent = die->parent;
17406 else
63d06c5c 17407 {
0114d602
DJ
17408 parent = spec_die->parent;
17409 cu = spec_cu;
63d06c5c 17410 }
0114d602
DJ
17411
17412 if (parent == NULL)
17413 return "";
98bfdba5
PA
17414 else if (parent->building_fullname)
17415 {
17416 const char *name;
17417 const char *parent_name;
17418
17419 /* It has been seen on RealView 2.2 built binaries,
17420 DW_TAG_template_type_param types actually _defined_ as
17421 children of the parent class:
17422
17423 enum E {};
17424 template class <class Enum> Class{};
17425 Class<enum E> class_e;
17426
17427 1: DW_TAG_class_type (Class)
17428 2: DW_TAG_enumeration_type (E)
17429 3: DW_TAG_enumerator (enum1:0)
17430 3: DW_TAG_enumerator (enum2:1)
17431 ...
17432 2: DW_TAG_template_type_param
17433 DW_AT_type DW_FORM_ref_udata (E)
17434
17435 Besides being broken debug info, it can put GDB into an
17436 infinite loop. Consider:
17437
17438 When we're building the full name for Class<E>, we'll start
17439 at Class, and go look over its template type parameters,
17440 finding E. We'll then try to build the full name of E, and
17441 reach here. We're now trying to build the full name of E,
17442 and look over the parent DIE for containing scope. In the
17443 broken case, if we followed the parent DIE of E, we'd again
17444 find Class, and once again go look at its template type
17445 arguments, etc., etc. Simply don't consider such parent die
17446 as source-level parent of this die (it can't be, the language
17447 doesn't allow it), and break the loop here. */
17448 name = dwarf2_name (die, cu);
17449 parent_name = dwarf2_name (parent, cu);
17450 complaint (&symfile_complaints,
17451 _("template param type '%s' defined within parent '%s'"),
17452 name ? name : "<unknown>",
17453 parent_name ? parent_name : "<unknown>");
17454 return "";
17455 }
63d06c5c 17456 else
0114d602
DJ
17457 switch (parent->tag)
17458 {
63d06c5c 17459 case DW_TAG_namespace:
0114d602 17460 parent_type = read_type_die (parent, cu);
acebe513
UW
17461 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17462 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17463 Work around this problem here. */
17464 if (cu->language == language_cplus
17465 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17466 return "";
0114d602
DJ
17467 /* We give a name to even anonymous namespaces. */
17468 return TYPE_TAG_NAME (parent_type);
63d06c5c 17469 case DW_TAG_class_type:
680b30c7 17470 case DW_TAG_interface_type:
63d06c5c 17471 case DW_TAG_structure_type:
0114d602 17472 case DW_TAG_union_type:
f55ee35c 17473 case DW_TAG_module:
0114d602
DJ
17474 parent_type = read_type_die (parent, cu);
17475 if (TYPE_TAG_NAME (parent_type) != NULL)
17476 return TYPE_TAG_NAME (parent_type);
17477 else
17478 /* An anonymous structure is only allowed non-static data
17479 members; no typedefs, no member functions, et cetera.
17480 So it does not need a prefix. */
17481 return "";
abc72ce4 17482 case DW_TAG_compile_unit:
95554aad 17483 case DW_TAG_partial_unit:
abc72ce4
DE
17484 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17485 if (cu->language == language_cplus
8b70b953 17486 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17487 && die->child != NULL
17488 && (die->tag == DW_TAG_class_type
17489 || die->tag == DW_TAG_structure_type
17490 || die->tag == DW_TAG_union_type))
17491 {
17492 char *name = guess_full_die_structure_name (die, cu);
17493 if (name != NULL)
17494 return name;
17495 }
17496 return "";
63d06c5c 17497 default:
8176b9b8 17498 return determine_prefix (parent, cu);
63d06c5c 17499 }
63d06c5c
DC
17500}
17501
3e43a32a
MS
17502/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17503 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17504 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17505 an obconcat, otherwise allocate storage for the result. The CU argument is
17506 used to determine the language and hence, the appropriate separator. */
987504bb 17507
f55ee35c 17508#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17509
17510static char *
f55ee35c
JK
17511typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17512 int physname, struct dwarf2_cu *cu)
63d06c5c 17513{
f55ee35c 17514 const char *lead = "";
5c315b68 17515 const char *sep;
63d06c5c 17516
3e43a32a
MS
17517 if (suffix == NULL || suffix[0] == '\0'
17518 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17519 sep = "";
17520 else if (cu->language == language_java)
17521 sep = ".";
f55ee35c
JK
17522 else if (cu->language == language_fortran && physname)
17523 {
17524 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17525 DW_AT_MIPS_linkage_name is preferred and used instead. */
17526
17527 lead = "__";
17528 sep = "_MOD_";
17529 }
987504bb
JJ
17530 else
17531 sep = "::";
63d06c5c 17532
6dd47d34
DE
17533 if (prefix == NULL)
17534 prefix = "";
17535 if (suffix == NULL)
17536 suffix = "";
17537
987504bb
JJ
17538 if (obs == NULL)
17539 {
3e43a32a
MS
17540 char *retval
17541 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17542
f55ee35c
JK
17543 strcpy (retval, lead);
17544 strcat (retval, prefix);
6dd47d34
DE
17545 strcat (retval, sep);
17546 strcat (retval, suffix);
63d06c5c
DC
17547 return retval;
17548 }
987504bb
JJ
17549 else
17550 {
17551 /* We have an obstack. */
f55ee35c 17552 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17553 }
63d06c5c
DC
17554}
17555
c906108c
SS
17556/* Return sibling of die, NULL if no sibling. */
17557
f9aca02d 17558static struct die_info *
fba45db2 17559sibling_die (struct die_info *die)
c906108c 17560{
639d11d3 17561 return die->sibling;
c906108c
SS
17562}
17563
71c25dea
TT
17564/* Get name of a die, return NULL if not found. */
17565
15d034d0
TT
17566static const char *
17567dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17568 struct obstack *obstack)
17569{
17570 if (name && cu->language == language_cplus)
17571 {
17572 char *canon_name = cp_canonicalize_string (name);
17573
17574 if (canon_name != NULL)
17575 {
17576 if (strcmp (canon_name, name) != 0)
10f0c4bb 17577 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17578 xfree (canon_name);
17579 }
17580 }
17581
17582 return name;
c906108c
SS
17583}
17584
9219021c
DC
17585/* Get name of a die, return NULL if not found. */
17586
15d034d0 17587static const char *
e142c38c 17588dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17589{
17590 struct attribute *attr;
17591
e142c38c 17592 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17593 if ((!attr || !DW_STRING (attr))
17594 && die->tag != DW_TAG_class_type
17595 && die->tag != DW_TAG_interface_type
17596 && die->tag != DW_TAG_structure_type
17597 && die->tag != DW_TAG_union_type)
71c25dea
TT
17598 return NULL;
17599
17600 switch (die->tag)
17601 {
17602 case DW_TAG_compile_unit:
95554aad 17603 case DW_TAG_partial_unit:
71c25dea
TT
17604 /* Compilation units have a DW_AT_name that is a filename, not
17605 a source language identifier. */
17606 case DW_TAG_enumeration_type:
17607 case DW_TAG_enumerator:
17608 /* These tags always have simple identifiers already; no need
17609 to canonicalize them. */
17610 return DW_STRING (attr);
907af001 17611
418835cc
KS
17612 case DW_TAG_subprogram:
17613 /* Java constructors will all be named "<init>", so return
17614 the class name when we see this special case. */
17615 if (cu->language == language_java
17616 && DW_STRING (attr) != NULL
17617 && strcmp (DW_STRING (attr), "<init>") == 0)
17618 {
17619 struct dwarf2_cu *spec_cu = cu;
17620 struct die_info *spec_die;
17621
17622 /* GCJ will output '<init>' for Java constructor names.
17623 For this special case, return the name of the parent class. */
17624
17625 /* GCJ may output suprogram DIEs with AT_specification set.
17626 If so, use the name of the specified DIE. */
17627 spec_die = die_specification (die, &spec_cu);
17628 if (spec_die != NULL)
17629 return dwarf2_name (spec_die, spec_cu);
17630
17631 do
17632 {
17633 die = die->parent;
17634 if (die->tag == DW_TAG_class_type)
17635 return dwarf2_name (die, cu);
17636 }
95554aad
TT
17637 while (die->tag != DW_TAG_compile_unit
17638 && die->tag != DW_TAG_partial_unit);
418835cc 17639 }
907af001
UW
17640 break;
17641
17642 case DW_TAG_class_type:
17643 case DW_TAG_interface_type:
17644 case DW_TAG_structure_type:
17645 case DW_TAG_union_type:
17646 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17647 structures or unions. These were of the form "._%d" in GCC 4.1,
17648 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17649 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17650 if (attr && DW_STRING (attr)
17651 && (strncmp (DW_STRING (attr), "._", 2) == 0
17652 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17653 return NULL;
53832f31
TT
17654
17655 /* GCC might emit a nameless typedef that has a linkage name. See
17656 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17657 if (!attr || DW_STRING (attr) == NULL)
17658 {
df5c6c50 17659 char *demangled = NULL;
53832f31
TT
17660
17661 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17662 if (attr == NULL)
17663 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17664
17665 if (attr == NULL || DW_STRING (attr) == NULL)
17666 return NULL;
17667
df5c6c50
JK
17668 /* Avoid demangling DW_STRING (attr) the second time on a second
17669 call for the same DIE. */
17670 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17671 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17672
17673 if (demangled)
17674 {
96408a79
SA
17675 char *base;
17676
53832f31 17677 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17678 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17679 demangled, strlen (demangled));
53832f31
TT
17680 DW_STRING_IS_CANONICAL (attr) = 1;
17681 xfree (demangled);
96408a79
SA
17682
17683 /* Strip any leading namespaces/classes, keep only the base name.
17684 DW_AT_name for named DIEs does not contain the prefixes. */
17685 base = strrchr (DW_STRING (attr), ':');
17686 if (base && base > DW_STRING (attr) && base[-1] == ':')
17687 return &base[1];
17688 else
17689 return DW_STRING (attr);
53832f31
TT
17690 }
17691 }
907af001
UW
17692 break;
17693
71c25dea 17694 default:
907af001
UW
17695 break;
17696 }
17697
17698 if (!DW_STRING_IS_CANONICAL (attr))
17699 {
17700 DW_STRING (attr)
17701 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17702 &cu->objfile->objfile_obstack);
17703 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17704 }
907af001 17705 return DW_STRING (attr);
9219021c
DC
17706}
17707
17708/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17709 is none. *EXT_CU is the CU containing DIE on input, and the CU
17710 containing the return value on output. */
9219021c
DC
17711
17712static struct die_info *
f2f0e013 17713dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17714{
17715 struct attribute *attr;
9219021c 17716
f2f0e013 17717 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17718 if (attr == NULL)
17719 return NULL;
17720
f2f0e013 17721 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17722}
17723
c906108c
SS
17724/* Convert a DIE tag into its string name. */
17725
f39c6ffd 17726static const char *
aa1ee363 17727dwarf_tag_name (unsigned tag)
c906108c 17728{
f39c6ffd
TT
17729 const char *name = get_DW_TAG_name (tag);
17730
17731 if (name == NULL)
17732 return "DW_TAG_<unknown>";
17733
17734 return name;
c906108c
SS
17735}
17736
17737/* Convert a DWARF attribute code into its string name. */
17738
f39c6ffd 17739static const char *
aa1ee363 17740dwarf_attr_name (unsigned attr)
c906108c 17741{
f39c6ffd
TT
17742 const char *name;
17743
c764a876 17744#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17745 if (attr == DW_AT_MIPS_fde)
17746 return "DW_AT_MIPS_fde";
17747#else
17748 if (attr == DW_AT_HP_block_index)
17749 return "DW_AT_HP_block_index";
c764a876 17750#endif
f39c6ffd
TT
17751
17752 name = get_DW_AT_name (attr);
17753
17754 if (name == NULL)
17755 return "DW_AT_<unknown>";
17756
17757 return name;
c906108c
SS
17758}
17759
17760/* Convert a DWARF value form code into its string name. */
17761
f39c6ffd 17762static const char *
aa1ee363 17763dwarf_form_name (unsigned form)
c906108c 17764{
f39c6ffd
TT
17765 const char *name = get_DW_FORM_name (form);
17766
17767 if (name == NULL)
17768 return "DW_FORM_<unknown>";
17769
17770 return name;
c906108c
SS
17771}
17772
17773static char *
fba45db2 17774dwarf_bool_name (unsigned mybool)
c906108c
SS
17775{
17776 if (mybool)
17777 return "TRUE";
17778 else
17779 return "FALSE";
17780}
17781
17782/* Convert a DWARF type code into its string name. */
17783
f39c6ffd 17784static const char *
aa1ee363 17785dwarf_type_encoding_name (unsigned enc)
c906108c 17786{
f39c6ffd 17787 const char *name = get_DW_ATE_name (enc);
c906108c 17788
f39c6ffd
TT
17789 if (name == NULL)
17790 return "DW_ATE_<unknown>";
c906108c 17791
f39c6ffd 17792 return name;
c906108c 17793}
c906108c 17794
f9aca02d 17795static void
d97bc12b 17796dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17797{
17798 unsigned int i;
17799
d97bc12b
DE
17800 print_spaces (indent, f);
17801 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17802 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17803
17804 if (die->parent != NULL)
17805 {
17806 print_spaces (indent, f);
17807 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17808 die->parent->offset.sect_off);
d97bc12b
DE
17809 }
17810
17811 print_spaces (indent, f);
17812 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17813 dwarf_bool_name (die->child != NULL));
c906108c 17814
d97bc12b
DE
17815 print_spaces (indent, f);
17816 fprintf_unfiltered (f, " attributes:\n");
17817
c906108c
SS
17818 for (i = 0; i < die->num_attrs; ++i)
17819 {
d97bc12b
DE
17820 print_spaces (indent, f);
17821 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17822 dwarf_attr_name (die->attrs[i].name),
17823 dwarf_form_name (die->attrs[i].form));
d97bc12b 17824
c906108c
SS
17825 switch (die->attrs[i].form)
17826 {
c906108c 17827 case DW_FORM_addr:
3019eac3 17828 case DW_FORM_GNU_addr_index:
d97bc12b 17829 fprintf_unfiltered (f, "address: ");
5af949e3 17830 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17831 break;
17832 case DW_FORM_block2:
17833 case DW_FORM_block4:
17834 case DW_FORM_block:
17835 case DW_FORM_block1:
56eb65bd
SP
17836 fprintf_unfiltered (f, "block: size %s",
17837 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17838 break;
2dc7f7b3 17839 case DW_FORM_exprloc:
56eb65bd
SP
17840 fprintf_unfiltered (f, "expression: size %s",
17841 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17842 break;
4568ecf9
DE
17843 case DW_FORM_ref_addr:
17844 fprintf_unfiltered (f, "ref address: ");
17845 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17846 break;
36586728
TT
17847 case DW_FORM_GNU_ref_alt:
17848 fprintf_unfiltered (f, "alt ref address: ");
17849 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17850 break;
10b3939b
DJ
17851 case DW_FORM_ref1:
17852 case DW_FORM_ref2:
17853 case DW_FORM_ref4:
4568ecf9
DE
17854 case DW_FORM_ref8:
17855 case DW_FORM_ref_udata:
d97bc12b 17856 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17857 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17858 break;
c906108c
SS
17859 case DW_FORM_data1:
17860 case DW_FORM_data2:
17861 case DW_FORM_data4:
ce5d95e1 17862 case DW_FORM_data8:
c906108c
SS
17863 case DW_FORM_udata:
17864 case DW_FORM_sdata:
43bbcdc2
PH
17865 fprintf_unfiltered (f, "constant: %s",
17866 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17867 break;
2dc7f7b3
TT
17868 case DW_FORM_sec_offset:
17869 fprintf_unfiltered (f, "section offset: %s",
17870 pulongest (DW_UNSND (&die->attrs[i])));
17871 break;
55f1336d 17872 case DW_FORM_ref_sig8:
ac9ec31b
DE
17873 fprintf_unfiltered (f, "signature: %s",
17874 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17875 break;
c906108c 17876 case DW_FORM_string:
4bdf3d34 17877 case DW_FORM_strp:
3019eac3 17878 case DW_FORM_GNU_str_index:
36586728 17879 case DW_FORM_GNU_strp_alt:
8285870a 17880 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17881 DW_STRING (&die->attrs[i])
8285870a
JK
17882 ? DW_STRING (&die->attrs[i]) : "",
17883 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17884 break;
17885 case DW_FORM_flag:
17886 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17887 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17888 else
d97bc12b 17889 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17890 break;
2dc7f7b3
TT
17891 case DW_FORM_flag_present:
17892 fprintf_unfiltered (f, "flag: TRUE");
17893 break;
a8329558 17894 case DW_FORM_indirect:
0963b4bd
MS
17895 /* The reader will have reduced the indirect form to
17896 the "base form" so this form should not occur. */
3e43a32a
MS
17897 fprintf_unfiltered (f,
17898 "unexpected attribute form: DW_FORM_indirect");
a8329558 17899 break;
c906108c 17900 default:
d97bc12b 17901 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17902 die->attrs[i].form);
d97bc12b 17903 break;
c906108c 17904 }
d97bc12b 17905 fprintf_unfiltered (f, "\n");
c906108c
SS
17906 }
17907}
17908
f9aca02d 17909static void
d97bc12b 17910dump_die_for_error (struct die_info *die)
c906108c 17911{
d97bc12b
DE
17912 dump_die_shallow (gdb_stderr, 0, die);
17913}
17914
17915static void
17916dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17917{
17918 int indent = level * 4;
17919
17920 gdb_assert (die != NULL);
17921
17922 if (level >= max_level)
17923 return;
17924
17925 dump_die_shallow (f, indent, die);
17926
17927 if (die->child != NULL)
c906108c 17928 {
d97bc12b
DE
17929 print_spaces (indent, f);
17930 fprintf_unfiltered (f, " Children:");
17931 if (level + 1 < max_level)
17932 {
17933 fprintf_unfiltered (f, "\n");
17934 dump_die_1 (f, level + 1, max_level, die->child);
17935 }
17936 else
17937 {
3e43a32a
MS
17938 fprintf_unfiltered (f,
17939 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17940 }
17941 }
17942
17943 if (die->sibling != NULL && level > 0)
17944 {
17945 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17946 }
17947}
17948
d97bc12b
DE
17949/* This is called from the pdie macro in gdbinit.in.
17950 It's not static so gcc will keep a copy callable from gdb. */
17951
17952void
17953dump_die (struct die_info *die, int max_level)
17954{
17955 dump_die_1 (gdb_stdlog, 0, max_level, die);
17956}
17957
f9aca02d 17958static void
51545339 17959store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17960{
51545339 17961 void **slot;
c906108c 17962
b64f50a1
JK
17963 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17964 INSERT);
51545339
DJ
17965
17966 *slot = die;
c906108c
SS
17967}
17968
b64f50a1
JK
17969/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17970 required kind. */
17971
17972static sect_offset
ff39bb5e 17973dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 17974{
4568ecf9 17975 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17976
7771576e 17977 if (attr_form_is_ref (attr))
b64f50a1 17978 return retval;
93311388 17979
b64f50a1 17980 retval.sect_off = 0;
93311388
DE
17981 complaint (&symfile_complaints,
17982 _("unsupported die ref attribute form: '%s'"),
17983 dwarf_form_name (attr->form));
b64f50a1 17984 return retval;
c906108c
SS
17985}
17986
43bbcdc2
PH
17987/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17988 * the value held by the attribute is not constant. */
a02abb62 17989
43bbcdc2 17990static LONGEST
ff39bb5e 17991dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
17992{
17993 if (attr->form == DW_FORM_sdata)
17994 return DW_SND (attr);
17995 else if (attr->form == DW_FORM_udata
17996 || attr->form == DW_FORM_data1
17997 || attr->form == DW_FORM_data2
17998 || attr->form == DW_FORM_data4
17999 || attr->form == DW_FORM_data8)
18000 return DW_UNSND (attr);
18001 else
18002 {
3e43a32a
MS
18003 complaint (&symfile_complaints,
18004 _("Attribute value is not a constant (%s)"),
a02abb62
JB
18005 dwarf_form_name (attr->form));
18006 return default_value;
18007 }
18008}
18009
348e048f
DE
18010/* Follow reference or signature attribute ATTR of SRC_DIE.
18011 On entry *REF_CU is the CU of SRC_DIE.
18012 On exit *REF_CU is the CU of the result. */
18013
18014static struct die_info *
ff39bb5e 18015follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18016 struct dwarf2_cu **ref_cu)
18017{
18018 struct die_info *die;
18019
7771576e 18020 if (attr_form_is_ref (attr))
348e048f 18021 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18022 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18023 die = follow_die_sig (src_die, attr, ref_cu);
18024 else
18025 {
18026 dump_die_for_error (src_die);
18027 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18028 (*ref_cu)->objfile->name);
18029 }
18030
18031 return die;
03dd20cc
DJ
18032}
18033
5c631832 18034/* Follow reference OFFSET.
673bfd45
DE
18035 On entry *REF_CU is the CU of the source die referencing OFFSET.
18036 On exit *REF_CU is the CU of the result.
18037 Returns NULL if OFFSET is invalid. */
f504f079 18038
f9aca02d 18039static struct die_info *
36586728
TT
18040follow_die_offset (sect_offset offset, int offset_in_dwz,
18041 struct dwarf2_cu **ref_cu)
c906108c 18042{
10b3939b 18043 struct die_info temp_die;
f2f0e013 18044 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18045
348e048f
DE
18046 gdb_assert (cu->per_cu != NULL);
18047
98bfdba5
PA
18048 target_cu = cu;
18049
3019eac3 18050 if (cu->per_cu->is_debug_types)
348e048f
DE
18051 {
18052 /* .debug_types CUs cannot reference anything outside their CU.
18053 If they need to, they have to reference a signatured type via
55f1336d 18054 DW_FORM_ref_sig8. */
348e048f 18055 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18056 return NULL;
348e048f 18057 }
36586728
TT
18058 else if (offset_in_dwz != cu->per_cu->is_dwz
18059 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18060 {
18061 struct dwarf2_per_cu_data *per_cu;
9a619af0 18062
36586728
TT
18063 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18064 cu->objfile);
03dd20cc
DJ
18065
18066 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18067 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18068 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18069
10b3939b
DJ
18070 target_cu = per_cu->cu;
18071 }
98bfdba5
PA
18072 else if (cu->dies == NULL)
18073 {
18074 /* We're loading full DIEs during partial symbol reading. */
18075 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18076 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18077 }
c906108c 18078
f2f0e013 18079 *ref_cu = target_cu;
51545339 18080 temp_die.offset = offset;
b64f50a1 18081 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18082}
10b3939b 18083
5c631832
JK
18084/* Follow reference attribute ATTR of SRC_DIE.
18085 On entry *REF_CU is the CU of SRC_DIE.
18086 On exit *REF_CU is the CU of the result. */
18087
18088static struct die_info *
ff39bb5e 18089follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18090 struct dwarf2_cu **ref_cu)
18091{
b64f50a1 18092 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18093 struct dwarf2_cu *cu = *ref_cu;
18094 struct die_info *die;
18095
36586728
TT
18096 die = follow_die_offset (offset,
18097 (attr->form == DW_FORM_GNU_ref_alt
18098 || cu->per_cu->is_dwz),
18099 ref_cu);
5c631832
JK
18100 if (!die)
18101 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18102 "at 0x%x [in module %s]"),
b64f50a1 18103 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 18104
5c631832
JK
18105 return die;
18106}
18107
d83e736b
JK
18108/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18109 Returned value is intended for DW_OP_call*. Returned
18110 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18111
18112struct dwarf2_locexpr_baton
8b9737bf
TT
18113dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18114 struct dwarf2_per_cu_data *per_cu,
18115 CORE_ADDR (*get_frame_pc) (void *baton),
18116 void *baton)
5c631832 18117{
918dd910 18118 struct dwarf2_cu *cu;
5c631832
JK
18119 struct die_info *die;
18120 struct attribute *attr;
18121 struct dwarf2_locexpr_baton retval;
18122
8cf6f0b1
TT
18123 dw2_setup (per_cu->objfile);
18124
918dd910
JK
18125 if (per_cu->cu == NULL)
18126 load_cu (per_cu);
18127 cu = per_cu->cu;
18128
36586728 18129 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18130 if (!die)
18131 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 18132 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18133
18134 attr = dwarf2_attr (die, DW_AT_location, cu);
18135 if (!attr)
18136 {
e103e986
JK
18137 /* DWARF: "If there is no such attribute, then there is no effect.".
18138 DATA is ignored if SIZE is 0. */
5c631832 18139
e103e986 18140 retval.data = NULL;
5c631832
JK
18141 retval.size = 0;
18142 }
8cf6f0b1
TT
18143 else if (attr_form_is_section_offset (attr))
18144 {
18145 struct dwarf2_loclist_baton loclist_baton;
18146 CORE_ADDR pc = (*get_frame_pc) (baton);
18147 size_t size;
18148
18149 fill_in_loclist_baton (cu, &loclist_baton, attr);
18150
18151 retval.data = dwarf2_find_location_expression (&loclist_baton,
18152 &size, pc);
18153 retval.size = size;
18154 }
5c631832
JK
18155 else
18156 {
18157 if (!attr_form_is_block (attr))
18158 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18159 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 18160 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18161
18162 retval.data = DW_BLOCK (attr)->data;
18163 retval.size = DW_BLOCK (attr)->size;
18164 }
18165 retval.per_cu = cu->per_cu;
918dd910 18166
918dd910
JK
18167 age_cached_comp_units ();
18168
5c631832 18169 return retval;
348e048f
DE
18170}
18171
8b9737bf
TT
18172/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18173 offset. */
18174
18175struct dwarf2_locexpr_baton
18176dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18177 struct dwarf2_per_cu_data *per_cu,
18178 CORE_ADDR (*get_frame_pc) (void *baton),
18179 void *baton)
18180{
18181 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18182
18183 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18184}
18185
b6807d98
TT
18186/* Write a constant of a given type as target-ordered bytes into
18187 OBSTACK. */
18188
18189static const gdb_byte *
18190write_constant_as_bytes (struct obstack *obstack,
18191 enum bfd_endian byte_order,
18192 struct type *type,
18193 ULONGEST value,
18194 LONGEST *len)
18195{
18196 gdb_byte *result;
18197
18198 *len = TYPE_LENGTH (type);
18199 result = obstack_alloc (obstack, *len);
18200 store_unsigned_integer (result, *len, byte_order, value);
18201
18202 return result;
18203}
18204
18205/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18206 pointer to the constant bytes and set LEN to the length of the
18207 data. If memory is needed, allocate it on OBSTACK. If the DIE
18208 does not have a DW_AT_const_value, return NULL. */
18209
18210const gdb_byte *
18211dwarf2_fetch_constant_bytes (sect_offset offset,
18212 struct dwarf2_per_cu_data *per_cu,
18213 struct obstack *obstack,
18214 LONGEST *len)
18215{
18216 struct dwarf2_cu *cu;
18217 struct die_info *die;
18218 struct attribute *attr;
18219 const gdb_byte *result = NULL;
18220 struct type *type;
18221 LONGEST value;
18222 enum bfd_endian byte_order;
18223
18224 dw2_setup (per_cu->objfile);
18225
18226 if (per_cu->cu == NULL)
18227 load_cu (per_cu);
18228 cu = per_cu->cu;
18229
18230 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18231 if (!die)
18232 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18233 offset.sect_off, per_cu->objfile->name);
18234
18235
18236 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18237 if (attr == NULL)
18238 return NULL;
18239
18240 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18241 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18242
18243 switch (attr->form)
18244 {
18245 case DW_FORM_addr:
18246 case DW_FORM_GNU_addr_index:
18247 {
18248 gdb_byte *tem;
18249
18250 *len = cu->header.addr_size;
18251 tem = obstack_alloc (obstack, *len);
18252 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18253 result = tem;
18254 }
18255 break;
18256 case DW_FORM_string:
18257 case DW_FORM_strp:
18258 case DW_FORM_GNU_str_index:
18259 case DW_FORM_GNU_strp_alt:
18260 /* DW_STRING is already allocated on the objfile obstack, point
18261 directly to it. */
18262 result = (const gdb_byte *) DW_STRING (attr);
18263 *len = strlen (DW_STRING (attr));
18264 break;
18265 case DW_FORM_block1:
18266 case DW_FORM_block2:
18267 case DW_FORM_block4:
18268 case DW_FORM_block:
18269 case DW_FORM_exprloc:
18270 result = DW_BLOCK (attr)->data;
18271 *len = DW_BLOCK (attr)->size;
18272 break;
18273
18274 /* The DW_AT_const_value attributes are supposed to carry the
18275 symbol's value "represented as it would be on the target
18276 architecture." By the time we get here, it's already been
18277 converted to host endianness, so we just need to sign- or
18278 zero-extend it as appropriate. */
18279 case DW_FORM_data1:
18280 type = die_type (die, cu);
18281 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18282 if (result == NULL)
18283 result = write_constant_as_bytes (obstack, byte_order,
18284 type, value, len);
18285 break;
18286 case DW_FORM_data2:
18287 type = die_type (die, cu);
18288 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18289 if (result == NULL)
18290 result = write_constant_as_bytes (obstack, byte_order,
18291 type, value, len);
18292 break;
18293 case DW_FORM_data4:
18294 type = die_type (die, cu);
18295 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18296 if (result == NULL)
18297 result = write_constant_as_bytes (obstack, byte_order,
18298 type, value, len);
18299 break;
18300 case DW_FORM_data8:
18301 type = die_type (die, cu);
18302 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18303 if (result == NULL)
18304 result = write_constant_as_bytes (obstack, byte_order,
18305 type, value, len);
18306 break;
18307
18308 case DW_FORM_sdata:
18309 type = die_type (die, cu);
18310 result = write_constant_as_bytes (obstack, byte_order,
18311 type, DW_SND (attr), len);
18312 break;
18313
18314 case DW_FORM_udata:
18315 type = die_type (die, cu);
18316 result = write_constant_as_bytes (obstack, byte_order,
18317 type, DW_UNSND (attr), len);
18318 break;
18319
18320 default:
18321 complaint (&symfile_complaints,
18322 _("unsupported const value attribute form: '%s'"),
18323 dwarf_form_name (attr->form));
18324 break;
18325 }
18326
18327 return result;
18328}
18329
8a9b8146
TT
18330/* Return the type of the DIE at DIE_OFFSET in the CU named by
18331 PER_CU. */
18332
18333struct type *
b64f50a1 18334dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18335 struct dwarf2_per_cu_data *per_cu)
18336{
b64f50a1
JK
18337 sect_offset die_offset_sect;
18338
8a9b8146 18339 dw2_setup (per_cu->objfile);
b64f50a1
JK
18340
18341 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18342 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18343}
18344
ac9ec31b 18345/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18346 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18347 On exit *REF_CU is the CU of the result.
18348 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18349
18350static struct die_info *
ac9ec31b
DE
18351follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18352 struct dwarf2_cu **ref_cu)
348e048f
DE
18353{
18354 struct objfile *objfile = (*ref_cu)->objfile;
18355 struct die_info temp_die;
348e048f
DE
18356 struct dwarf2_cu *sig_cu;
18357 struct die_info *die;
18358
ac9ec31b
DE
18359 /* While it might be nice to assert sig_type->type == NULL here,
18360 we can get here for DW_AT_imported_declaration where we need
18361 the DIE not the type. */
348e048f
DE
18362
18363 /* If necessary, add it to the queue and load its DIEs. */
18364
95554aad 18365 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18366 read_signatured_type (sig_type);
348e048f 18367
348e048f 18368 sig_cu = sig_type->per_cu.cu;
69d751e3 18369 gdb_assert (sig_cu != NULL);
3019eac3
DE
18370 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18371 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18372 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18373 temp_die.offset.sect_off);
348e048f
DE
18374 if (die)
18375 {
796a7ff8
DE
18376 /* For .gdb_index version 7 keep track of included TUs.
18377 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18378 if (dwarf2_per_objfile->index_table != NULL
18379 && dwarf2_per_objfile->index_table->version <= 7)
18380 {
18381 VEC_safe_push (dwarf2_per_cu_ptr,
18382 (*ref_cu)->per_cu->imported_symtabs,
18383 sig_cu->per_cu);
18384 }
18385
348e048f
DE
18386 *ref_cu = sig_cu;
18387 return die;
18388 }
18389
ac9ec31b
DE
18390 return NULL;
18391}
18392
18393/* Follow signatured type referenced by ATTR in SRC_DIE.
18394 On entry *REF_CU is the CU of SRC_DIE.
18395 On exit *REF_CU is the CU of the result.
18396 The result is the DIE of the type.
18397 If the referenced type cannot be found an error is thrown. */
18398
18399static struct die_info *
ff39bb5e 18400follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18401 struct dwarf2_cu **ref_cu)
18402{
18403 ULONGEST signature = DW_SIGNATURE (attr);
18404 struct signatured_type *sig_type;
18405 struct die_info *die;
18406
18407 gdb_assert (attr->form == DW_FORM_ref_sig8);
18408
a2ce51a0 18409 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18410 /* sig_type will be NULL if the signatured type is missing from
18411 the debug info. */
18412 if (sig_type == NULL)
18413 {
18414 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18415 " from DIE at 0x%x [in module %s]"),
18416 hex_string (signature), src_die->offset.sect_off,
18417 (*ref_cu)->objfile->name);
18418 }
18419
18420 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18421 if (die == NULL)
18422 {
18423 dump_die_for_error (src_die);
18424 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18425 " from DIE at 0x%x [in module %s]"),
18426 hex_string (signature), src_die->offset.sect_off,
18427 (*ref_cu)->objfile->name);
18428 }
18429
18430 return die;
18431}
18432
18433/* Get the type specified by SIGNATURE referenced in DIE/CU,
18434 reading in and processing the type unit if necessary. */
18435
18436static struct type *
18437get_signatured_type (struct die_info *die, ULONGEST signature,
18438 struct dwarf2_cu *cu)
18439{
18440 struct signatured_type *sig_type;
18441 struct dwarf2_cu *type_cu;
18442 struct die_info *type_die;
18443 struct type *type;
18444
a2ce51a0 18445 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18446 /* sig_type will be NULL if the signatured type is missing from
18447 the debug info. */
18448 if (sig_type == NULL)
18449 {
18450 complaint (&symfile_complaints,
18451 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18452 " from DIE at 0x%x [in module %s]"),
18453 hex_string (signature), die->offset.sect_off,
18454 dwarf2_per_objfile->objfile->name);
18455 return build_error_marker_type (cu, die);
18456 }
18457
18458 /* If we already know the type we're done. */
18459 if (sig_type->type != NULL)
18460 return sig_type->type;
18461
18462 type_cu = cu;
18463 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18464 if (type_die != NULL)
18465 {
18466 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18467 is created. This is important, for example, because for c++ classes
18468 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18469 type = read_type_die (type_die, type_cu);
18470 if (type == NULL)
18471 {
18472 complaint (&symfile_complaints,
18473 _("Dwarf Error: Cannot build signatured type %s"
18474 " referenced from DIE at 0x%x [in module %s]"),
18475 hex_string (signature), die->offset.sect_off,
18476 dwarf2_per_objfile->objfile->name);
18477 type = build_error_marker_type (cu, die);
18478 }
18479 }
18480 else
18481 {
18482 complaint (&symfile_complaints,
18483 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18484 " from DIE at 0x%x [in module %s]"),
18485 hex_string (signature), die->offset.sect_off,
18486 dwarf2_per_objfile->objfile->name);
18487 type = build_error_marker_type (cu, die);
18488 }
18489 sig_type->type = type;
18490
18491 return type;
18492}
18493
18494/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18495 reading in and processing the type unit if necessary. */
18496
18497static struct type *
ff39bb5e 18498get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18499 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18500{
18501 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18502 if (attr_form_is_ref (attr))
ac9ec31b
DE
18503 {
18504 struct dwarf2_cu *type_cu = cu;
18505 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18506
18507 return read_type_die (type_die, type_cu);
18508 }
18509 else if (attr->form == DW_FORM_ref_sig8)
18510 {
18511 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18512 }
18513 else
18514 {
18515 complaint (&symfile_complaints,
18516 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18517 " at 0x%x [in module %s]"),
18518 dwarf_form_name (attr->form), die->offset.sect_off,
18519 dwarf2_per_objfile->objfile->name);
18520 return build_error_marker_type (cu, die);
18521 }
348e048f
DE
18522}
18523
e5fe5e75 18524/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18525
18526static void
e5fe5e75 18527load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18528{
52dc124a 18529 struct signatured_type *sig_type;
348e048f 18530
f4dc4d17
DE
18531 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18532 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18533
6721b2ec
DE
18534 /* We have the per_cu, but we need the signatured_type.
18535 Fortunately this is an easy translation. */
18536 gdb_assert (per_cu->is_debug_types);
18537 sig_type = (struct signatured_type *) per_cu;
348e048f 18538
6721b2ec 18539 gdb_assert (per_cu->cu == NULL);
348e048f 18540
52dc124a 18541 read_signatured_type (sig_type);
348e048f 18542
6721b2ec 18543 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18544}
18545
dee91e82
DE
18546/* die_reader_func for read_signatured_type.
18547 This is identical to load_full_comp_unit_reader,
18548 but is kept separate for now. */
348e048f
DE
18549
18550static void
dee91e82 18551read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18552 const gdb_byte *info_ptr,
dee91e82
DE
18553 struct die_info *comp_unit_die,
18554 int has_children,
18555 void *data)
348e048f 18556{
dee91e82 18557 struct dwarf2_cu *cu = reader->cu;
348e048f 18558
dee91e82
DE
18559 gdb_assert (cu->die_hash == NULL);
18560 cu->die_hash =
18561 htab_create_alloc_ex (cu->header.length / 12,
18562 die_hash,
18563 die_eq,
18564 NULL,
18565 &cu->comp_unit_obstack,
18566 hashtab_obstack_allocate,
18567 dummy_obstack_deallocate);
348e048f 18568
dee91e82
DE
18569 if (has_children)
18570 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18571 &info_ptr, comp_unit_die);
18572 cu->dies = comp_unit_die;
18573 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18574
18575 /* We try not to read any attributes in this function, because not
9cdd5dbd 18576 all CUs needed for references have been loaded yet, and symbol
348e048f 18577 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18578 or we won't be able to build types correctly.
18579 Similarly, if we do not read the producer, we can not apply
18580 producer-specific interpretation. */
95554aad 18581 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18582}
348e048f 18583
3019eac3
DE
18584/* Read in a signatured type and build its CU and DIEs.
18585 If the type is a stub for the real type in a DWO file,
18586 read in the real type from the DWO file as well. */
dee91e82
DE
18587
18588static void
18589read_signatured_type (struct signatured_type *sig_type)
18590{
18591 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18592
3019eac3 18593 gdb_assert (per_cu->is_debug_types);
dee91e82 18594 gdb_assert (per_cu->cu == NULL);
348e048f 18595
f4dc4d17
DE
18596 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18597 read_signatured_type_reader, NULL);
7ee85ab1 18598 sig_type->per_cu.tu_read = 1;
c906108c
SS
18599}
18600
c906108c
SS
18601/* Decode simple location descriptions.
18602 Given a pointer to a dwarf block that defines a location, compute
18603 the location and return the value.
18604
4cecd739
DJ
18605 NOTE drow/2003-11-18: This function is called in two situations
18606 now: for the address of static or global variables (partial symbols
18607 only) and for offsets into structures which are expected to be
18608 (more or less) constant. The partial symbol case should go away,
18609 and only the constant case should remain. That will let this
18610 function complain more accurately. A few special modes are allowed
18611 without complaint for global variables (for instance, global
18612 register values and thread-local values).
c906108c
SS
18613
18614 A location description containing no operations indicates that the
4cecd739 18615 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18616 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18617 callers will only want a very basic result and this can become a
21ae7a4d
JK
18618 complaint.
18619
18620 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18621
18622static CORE_ADDR
e7c27a73 18623decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18624{
e7c27a73 18625 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18626 size_t i;
18627 size_t size = blk->size;
d521ce57 18628 const gdb_byte *data = blk->data;
21ae7a4d
JK
18629 CORE_ADDR stack[64];
18630 int stacki;
18631 unsigned int bytes_read, unsnd;
18632 gdb_byte op;
c906108c 18633
21ae7a4d
JK
18634 i = 0;
18635 stacki = 0;
18636 stack[stacki] = 0;
18637 stack[++stacki] = 0;
18638
18639 while (i < size)
18640 {
18641 op = data[i++];
18642 switch (op)
18643 {
18644 case DW_OP_lit0:
18645 case DW_OP_lit1:
18646 case DW_OP_lit2:
18647 case DW_OP_lit3:
18648 case DW_OP_lit4:
18649 case DW_OP_lit5:
18650 case DW_OP_lit6:
18651 case DW_OP_lit7:
18652 case DW_OP_lit8:
18653 case DW_OP_lit9:
18654 case DW_OP_lit10:
18655 case DW_OP_lit11:
18656 case DW_OP_lit12:
18657 case DW_OP_lit13:
18658 case DW_OP_lit14:
18659 case DW_OP_lit15:
18660 case DW_OP_lit16:
18661 case DW_OP_lit17:
18662 case DW_OP_lit18:
18663 case DW_OP_lit19:
18664 case DW_OP_lit20:
18665 case DW_OP_lit21:
18666 case DW_OP_lit22:
18667 case DW_OP_lit23:
18668 case DW_OP_lit24:
18669 case DW_OP_lit25:
18670 case DW_OP_lit26:
18671 case DW_OP_lit27:
18672 case DW_OP_lit28:
18673 case DW_OP_lit29:
18674 case DW_OP_lit30:
18675 case DW_OP_lit31:
18676 stack[++stacki] = op - DW_OP_lit0;
18677 break;
f1bea926 18678
21ae7a4d
JK
18679 case DW_OP_reg0:
18680 case DW_OP_reg1:
18681 case DW_OP_reg2:
18682 case DW_OP_reg3:
18683 case DW_OP_reg4:
18684 case DW_OP_reg5:
18685 case DW_OP_reg6:
18686 case DW_OP_reg7:
18687 case DW_OP_reg8:
18688 case DW_OP_reg9:
18689 case DW_OP_reg10:
18690 case DW_OP_reg11:
18691 case DW_OP_reg12:
18692 case DW_OP_reg13:
18693 case DW_OP_reg14:
18694 case DW_OP_reg15:
18695 case DW_OP_reg16:
18696 case DW_OP_reg17:
18697 case DW_OP_reg18:
18698 case DW_OP_reg19:
18699 case DW_OP_reg20:
18700 case DW_OP_reg21:
18701 case DW_OP_reg22:
18702 case DW_OP_reg23:
18703 case DW_OP_reg24:
18704 case DW_OP_reg25:
18705 case DW_OP_reg26:
18706 case DW_OP_reg27:
18707 case DW_OP_reg28:
18708 case DW_OP_reg29:
18709 case DW_OP_reg30:
18710 case DW_OP_reg31:
18711 stack[++stacki] = op - DW_OP_reg0;
18712 if (i < size)
18713 dwarf2_complex_location_expr_complaint ();
18714 break;
c906108c 18715
21ae7a4d
JK
18716 case DW_OP_regx:
18717 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18718 i += bytes_read;
18719 stack[++stacki] = unsnd;
18720 if (i < size)
18721 dwarf2_complex_location_expr_complaint ();
18722 break;
c906108c 18723
21ae7a4d
JK
18724 case DW_OP_addr:
18725 stack[++stacki] = read_address (objfile->obfd, &data[i],
18726 cu, &bytes_read);
18727 i += bytes_read;
18728 break;
d53d4ac5 18729
21ae7a4d
JK
18730 case DW_OP_const1u:
18731 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18732 i += 1;
18733 break;
18734
18735 case DW_OP_const1s:
18736 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18737 i += 1;
18738 break;
18739
18740 case DW_OP_const2u:
18741 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18742 i += 2;
18743 break;
18744
18745 case DW_OP_const2s:
18746 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18747 i += 2;
18748 break;
d53d4ac5 18749
21ae7a4d
JK
18750 case DW_OP_const4u:
18751 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18752 i += 4;
18753 break;
18754
18755 case DW_OP_const4s:
18756 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18757 i += 4;
18758 break;
18759
585861ea
JK
18760 case DW_OP_const8u:
18761 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18762 i += 8;
18763 break;
18764
21ae7a4d
JK
18765 case DW_OP_constu:
18766 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18767 &bytes_read);
18768 i += bytes_read;
18769 break;
18770
18771 case DW_OP_consts:
18772 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18773 i += bytes_read;
18774 break;
18775
18776 case DW_OP_dup:
18777 stack[stacki + 1] = stack[stacki];
18778 stacki++;
18779 break;
18780
18781 case DW_OP_plus:
18782 stack[stacki - 1] += stack[stacki];
18783 stacki--;
18784 break;
18785
18786 case DW_OP_plus_uconst:
18787 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18788 &bytes_read);
18789 i += bytes_read;
18790 break;
18791
18792 case DW_OP_minus:
18793 stack[stacki - 1] -= stack[stacki];
18794 stacki--;
18795 break;
18796
18797 case DW_OP_deref:
18798 /* If we're not the last op, then we definitely can't encode
18799 this using GDB's address_class enum. This is valid for partial
18800 global symbols, although the variable's address will be bogus
18801 in the psymtab. */
18802 if (i < size)
18803 dwarf2_complex_location_expr_complaint ();
18804 break;
18805
18806 case DW_OP_GNU_push_tls_address:
18807 /* The top of the stack has the offset from the beginning
18808 of the thread control block at which the variable is located. */
18809 /* Nothing should follow this operator, so the top of stack would
18810 be returned. */
18811 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18812 address will be bogus in the psymtab. Make it always at least
18813 non-zero to not look as a variable garbage collected by linker
18814 which have DW_OP_addr 0. */
21ae7a4d
JK
18815 if (i < size)
18816 dwarf2_complex_location_expr_complaint ();
585861ea 18817 stack[stacki]++;
21ae7a4d
JK
18818 break;
18819
18820 case DW_OP_GNU_uninit:
18821 break;
18822
3019eac3 18823 case DW_OP_GNU_addr_index:
49f6c839 18824 case DW_OP_GNU_const_index:
3019eac3
DE
18825 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18826 &bytes_read);
18827 i += bytes_read;
18828 break;
18829
21ae7a4d
JK
18830 default:
18831 {
f39c6ffd 18832 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18833
18834 if (name)
18835 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18836 name);
18837 else
18838 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18839 op);
18840 }
18841
18842 return (stack[stacki]);
d53d4ac5 18843 }
3c6e0cb3 18844
21ae7a4d
JK
18845 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18846 outside of the allocated space. Also enforce minimum>0. */
18847 if (stacki >= ARRAY_SIZE (stack) - 1)
18848 {
18849 complaint (&symfile_complaints,
18850 _("location description stack overflow"));
18851 return 0;
18852 }
18853
18854 if (stacki <= 0)
18855 {
18856 complaint (&symfile_complaints,
18857 _("location description stack underflow"));
18858 return 0;
18859 }
18860 }
18861 return (stack[stacki]);
c906108c
SS
18862}
18863
18864/* memory allocation interface */
18865
c906108c 18866static struct dwarf_block *
7b5a2f43 18867dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18868{
18869 struct dwarf_block *blk;
18870
18871 blk = (struct dwarf_block *)
7b5a2f43 18872 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18873 return (blk);
18874}
18875
c906108c 18876static struct die_info *
b60c80d6 18877dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18878{
18879 struct die_info *die;
b60c80d6
DJ
18880 size_t size = sizeof (struct die_info);
18881
18882 if (num_attrs > 1)
18883 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18884
b60c80d6 18885 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18886 memset (die, 0, sizeof (struct die_info));
18887 return (die);
18888}
2e276125
JB
18889
18890\f
18891/* Macro support. */
18892
233d95b5
JK
18893/* Return file name relative to the compilation directory of file number I in
18894 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18895 responsible for freeing it. */
233d95b5 18896
2e276125 18897static char *
233d95b5 18898file_file_name (int file, struct line_header *lh)
2e276125 18899{
6a83a1e6
EZ
18900 /* Is the file number a valid index into the line header's file name
18901 table? Remember that file numbers start with one, not zero. */
18902 if (1 <= file && file <= lh->num_file_names)
18903 {
18904 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18905
233d95b5 18906 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18907 return xstrdup (fe->name);
233d95b5
JK
18908 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18909 fe->name, NULL);
6a83a1e6 18910 }
2e276125
JB
18911 else
18912 {
6a83a1e6
EZ
18913 /* The compiler produced a bogus file number. We can at least
18914 record the macro definitions made in the file, even if we
18915 won't be able to find the file by name. */
18916 char fake_name[80];
9a619af0 18917
8c042590
PM
18918 xsnprintf (fake_name, sizeof (fake_name),
18919 "<bad macro file number %d>", file);
2e276125 18920
6e70227d 18921 complaint (&symfile_complaints,
6a83a1e6
EZ
18922 _("bad file number in macro information (%d)"),
18923 file);
2e276125 18924
6a83a1e6 18925 return xstrdup (fake_name);
2e276125
JB
18926 }
18927}
18928
233d95b5
JK
18929/* Return the full name of file number I in *LH's file name table.
18930 Use COMP_DIR as the name of the current directory of the
18931 compilation. The result is allocated using xmalloc; the caller is
18932 responsible for freeing it. */
18933static char *
18934file_full_name (int file, struct line_header *lh, const char *comp_dir)
18935{
18936 /* Is the file number a valid index into the line header's file name
18937 table? Remember that file numbers start with one, not zero. */
18938 if (1 <= file && file <= lh->num_file_names)
18939 {
18940 char *relative = file_file_name (file, lh);
18941
18942 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18943 return relative;
18944 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18945 }
18946 else
18947 return file_file_name (file, lh);
18948}
18949
2e276125
JB
18950
18951static struct macro_source_file *
18952macro_start_file (int file, int line,
18953 struct macro_source_file *current_file,
18954 const char *comp_dir,
18955 struct line_header *lh, struct objfile *objfile)
18956{
233d95b5
JK
18957 /* File name relative to the compilation directory of this source file. */
18958 char *file_name = file_file_name (file, lh);
2e276125 18959
2e276125 18960 if (! current_file)
abc9d0dc 18961 {
fc474241
DE
18962 /* Note: We don't create a macro table for this compilation unit
18963 at all until we actually get a filename. */
18964 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
18965
abc9d0dc
TT
18966 /* If we have no current file, then this must be the start_file
18967 directive for the compilation unit's main source file. */
fc474241
DE
18968 current_file = macro_set_main (macro_table, file_name);
18969 macro_define_special (macro_table);
abc9d0dc 18970 }
2e276125 18971 else
233d95b5 18972 current_file = macro_include (current_file, line, file_name);
2e276125 18973
233d95b5 18974 xfree (file_name);
6e70227d 18975
2e276125
JB
18976 return current_file;
18977}
18978
18979
18980/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18981 followed by a null byte. */
18982static char *
18983copy_string (const char *buf, int len)
18984{
18985 char *s = xmalloc (len + 1);
9a619af0 18986
2e276125
JB
18987 memcpy (s, buf, len);
18988 s[len] = '\0';
2e276125
JB
18989 return s;
18990}
18991
18992
18993static const char *
18994consume_improper_spaces (const char *p, const char *body)
18995{
18996 if (*p == ' ')
18997 {
4d3c2250 18998 complaint (&symfile_complaints,
3e43a32a
MS
18999 _("macro definition contains spaces "
19000 "in formal argument list:\n`%s'"),
4d3c2250 19001 body);
2e276125
JB
19002
19003 while (*p == ' ')
19004 p++;
19005 }
19006
19007 return p;
19008}
19009
19010
19011static void
19012parse_macro_definition (struct macro_source_file *file, int line,
19013 const char *body)
19014{
19015 const char *p;
19016
19017 /* The body string takes one of two forms. For object-like macro
19018 definitions, it should be:
19019
19020 <macro name> " " <definition>
19021
19022 For function-like macro definitions, it should be:
19023
19024 <macro name> "() " <definition>
19025 or
19026 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19027
19028 Spaces may appear only where explicitly indicated, and in the
19029 <definition>.
19030
19031 The Dwarf 2 spec says that an object-like macro's name is always
19032 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19033 the space when the macro's definition is the empty string.
2e276125
JB
19034
19035 The Dwarf 2 spec says that there should be no spaces between the
19036 formal arguments in a function-like macro's formal argument list,
19037 but versions of GCC around March 2002 include spaces after the
19038 commas. */
19039
19040
19041 /* Find the extent of the macro name. The macro name is terminated
19042 by either a space or null character (for an object-like macro) or
19043 an opening paren (for a function-like macro). */
19044 for (p = body; *p; p++)
19045 if (*p == ' ' || *p == '(')
19046 break;
19047
19048 if (*p == ' ' || *p == '\0')
19049 {
19050 /* It's an object-like macro. */
19051 int name_len = p - body;
19052 char *name = copy_string (body, name_len);
19053 const char *replacement;
19054
19055 if (*p == ' ')
19056 replacement = body + name_len + 1;
19057 else
19058 {
4d3c2250 19059 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19060 replacement = body + name_len;
19061 }
6e70227d 19062
2e276125
JB
19063 macro_define_object (file, line, name, replacement);
19064
19065 xfree (name);
19066 }
19067 else if (*p == '(')
19068 {
19069 /* It's a function-like macro. */
19070 char *name = copy_string (body, p - body);
19071 int argc = 0;
19072 int argv_size = 1;
19073 char **argv = xmalloc (argv_size * sizeof (*argv));
19074
19075 p++;
19076
19077 p = consume_improper_spaces (p, body);
19078
19079 /* Parse the formal argument list. */
19080 while (*p && *p != ')')
19081 {
19082 /* Find the extent of the current argument name. */
19083 const char *arg_start = p;
19084
19085 while (*p && *p != ',' && *p != ')' && *p != ' ')
19086 p++;
19087
19088 if (! *p || p == arg_start)
4d3c2250 19089 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19090 else
19091 {
19092 /* Make sure argv has room for the new argument. */
19093 if (argc >= argv_size)
19094 {
19095 argv_size *= 2;
19096 argv = xrealloc (argv, argv_size * sizeof (*argv));
19097 }
19098
19099 argv[argc++] = copy_string (arg_start, p - arg_start);
19100 }
19101
19102 p = consume_improper_spaces (p, body);
19103
19104 /* Consume the comma, if present. */
19105 if (*p == ',')
19106 {
19107 p++;
19108
19109 p = consume_improper_spaces (p, body);
19110 }
19111 }
19112
19113 if (*p == ')')
19114 {
19115 p++;
19116
19117 if (*p == ' ')
19118 /* Perfectly formed definition, no complaints. */
19119 macro_define_function (file, line, name,
6e70227d 19120 argc, (const char **) argv,
2e276125
JB
19121 p + 1);
19122 else if (*p == '\0')
19123 {
19124 /* Complain, but do define it. */
4d3c2250 19125 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19126 macro_define_function (file, line, name,
6e70227d 19127 argc, (const char **) argv,
2e276125
JB
19128 p);
19129 }
19130 else
19131 /* Just complain. */
4d3c2250 19132 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19133 }
19134 else
19135 /* Just complain. */
4d3c2250 19136 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19137
19138 xfree (name);
19139 {
19140 int i;
19141
19142 for (i = 0; i < argc; i++)
19143 xfree (argv[i]);
19144 }
19145 xfree (argv);
19146 }
19147 else
4d3c2250 19148 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19149}
19150
cf2c3c16
TT
19151/* Skip some bytes from BYTES according to the form given in FORM.
19152 Returns the new pointer. */
2e276125 19153
d521ce57
TT
19154static const gdb_byte *
19155skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19156 enum dwarf_form form,
19157 unsigned int offset_size,
19158 struct dwarf2_section_info *section)
2e276125 19159{
cf2c3c16 19160 unsigned int bytes_read;
2e276125 19161
cf2c3c16 19162 switch (form)
2e276125 19163 {
cf2c3c16
TT
19164 case DW_FORM_data1:
19165 case DW_FORM_flag:
19166 ++bytes;
19167 break;
19168
19169 case DW_FORM_data2:
19170 bytes += 2;
19171 break;
19172
19173 case DW_FORM_data4:
19174 bytes += 4;
19175 break;
19176
19177 case DW_FORM_data8:
19178 bytes += 8;
19179 break;
19180
19181 case DW_FORM_string:
19182 read_direct_string (abfd, bytes, &bytes_read);
19183 bytes += bytes_read;
19184 break;
19185
19186 case DW_FORM_sec_offset:
19187 case DW_FORM_strp:
36586728 19188 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19189 bytes += offset_size;
19190 break;
19191
19192 case DW_FORM_block:
19193 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19194 bytes += bytes_read;
19195 break;
19196
19197 case DW_FORM_block1:
19198 bytes += 1 + read_1_byte (abfd, bytes);
19199 break;
19200 case DW_FORM_block2:
19201 bytes += 2 + read_2_bytes (abfd, bytes);
19202 break;
19203 case DW_FORM_block4:
19204 bytes += 4 + read_4_bytes (abfd, bytes);
19205 break;
19206
19207 case DW_FORM_sdata:
19208 case DW_FORM_udata:
3019eac3
DE
19209 case DW_FORM_GNU_addr_index:
19210 case DW_FORM_GNU_str_index:
d521ce57 19211 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19212 if (bytes == NULL)
19213 {
19214 dwarf2_section_buffer_overflow_complaint (section);
19215 return NULL;
19216 }
cf2c3c16
TT
19217 break;
19218
19219 default:
19220 {
19221 complain:
19222 complaint (&symfile_complaints,
19223 _("invalid form 0x%x in `%s'"),
19224 form,
19225 section->asection->name);
19226 return NULL;
19227 }
2e276125
JB
19228 }
19229
cf2c3c16
TT
19230 return bytes;
19231}
757a13d0 19232
cf2c3c16
TT
19233/* A helper for dwarf_decode_macros that handles skipping an unknown
19234 opcode. Returns an updated pointer to the macro data buffer; or,
19235 on error, issues a complaint and returns NULL. */
757a13d0 19236
d521ce57 19237static const gdb_byte *
cf2c3c16 19238skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19239 const gdb_byte **opcode_definitions,
19240 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19241 bfd *abfd,
19242 unsigned int offset_size,
19243 struct dwarf2_section_info *section)
19244{
19245 unsigned int bytes_read, i;
19246 unsigned long arg;
d521ce57 19247 const gdb_byte *defn;
2e276125 19248
cf2c3c16 19249 if (opcode_definitions[opcode] == NULL)
2e276125 19250 {
cf2c3c16
TT
19251 complaint (&symfile_complaints,
19252 _("unrecognized DW_MACFINO opcode 0x%x"),
19253 opcode);
19254 return NULL;
19255 }
2e276125 19256
cf2c3c16
TT
19257 defn = opcode_definitions[opcode];
19258 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19259 defn += bytes_read;
2e276125 19260
cf2c3c16
TT
19261 for (i = 0; i < arg; ++i)
19262 {
f664829e
DE
19263 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19264 section);
cf2c3c16
TT
19265 if (mac_ptr == NULL)
19266 {
19267 /* skip_form_bytes already issued the complaint. */
19268 return NULL;
19269 }
19270 }
757a13d0 19271
cf2c3c16
TT
19272 return mac_ptr;
19273}
757a13d0 19274
cf2c3c16
TT
19275/* A helper function which parses the header of a macro section.
19276 If the macro section is the extended (for now called "GNU") type,
19277 then this updates *OFFSET_SIZE. Returns a pointer to just after
19278 the header, or issues a complaint and returns NULL on error. */
757a13d0 19279
d521ce57
TT
19280static const gdb_byte *
19281dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19282 bfd *abfd,
d521ce57 19283 const gdb_byte *mac_ptr,
cf2c3c16
TT
19284 unsigned int *offset_size,
19285 int section_is_gnu)
19286{
19287 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19288
cf2c3c16
TT
19289 if (section_is_gnu)
19290 {
19291 unsigned int version, flags;
757a13d0 19292
cf2c3c16
TT
19293 version = read_2_bytes (abfd, mac_ptr);
19294 if (version != 4)
19295 {
19296 complaint (&symfile_complaints,
19297 _("unrecognized version `%d' in .debug_macro section"),
19298 version);
19299 return NULL;
19300 }
19301 mac_ptr += 2;
757a13d0 19302
cf2c3c16
TT
19303 flags = read_1_byte (abfd, mac_ptr);
19304 ++mac_ptr;
19305 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19306
cf2c3c16
TT
19307 if ((flags & 2) != 0)
19308 /* We don't need the line table offset. */
19309 mac_ptr += *offset_size;
757a13d0 19310
cf2c3c16
TT
19311 /* Vendor opcode descriptions. */
19312 if ((flags & 4) != 0)
19313 {
19314 unsigned int i, count;
757a13d0 19315
cf2c3c16
TT
19316 count = read_1_byte (abfd, mac_ptr);
19317 ++mac_ptr;
19318 for (i = 0; i < count; ++i)
19319 {
19320 unsigned int opcode, bytes_read;
19321 unsigned long arg;
19322
19323 opcode = read_1_byte (abfd, mac_ptr);
19324 ++mac_ptr;
19325 opcode_definitions[opcode] = mac_ptr;
19326 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19327 mac_ptr += bytes_read;
19328 mac_ptr += arg;
19329 }
757a13d0 19330 }
cf2c3c16 19331 }
757a13d0 19332
cf2c3c16
TT
19333 return mac_ptr;
19334}
757a13d0 19335
cf2c3c16 19336/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19337 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19338
19339static void
d521ce57
TT
19340dwarf_decode_macro_bytes (bfd *abfd,
19341 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19342 struct macro_source_file *current_file,
15d034d0 19343 struct line_header *lh, const char *comp_dir,
cf2c3c16 19344 struct dwarf2_section_info *section,
36586728 19345 int section_is_gnu, int section_is_dwz,
cf2c3c16 19346 unsigned int offset_size,
8fc3fc34
TT
19347 struct objfile *objfile,
19348 htab_t include_hash)
cf2c3c16
TT
19349{
19350 enum dwarf_macro_record_type macinfo_type;
19351 int at_commandline;
d521ce57 19352 const gdb_byte *opcode_definitions[256];
757a13d0 19353
cf2c3c16
TT
19354 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19355 &offset_size, section_is_gnu);
19356 if (mac_ptr == NULL)
19357 {
19358 /* We already issued a complaint. */
19359 return;
19360 }
757a13d0
JK
19361
19362 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19363 GDB is still reading the definitions from command line. First
19364 DW_MACINFO_start_file will need to be ignored as it was already executed
19365 to create CURRENT_FILE for the main source holding also the command line
19366 definitions. On first met DW_MACINFO_start_file this flag is reset to
19367 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19368
19369 at_commandline = 1;
19370
19371 do
19372 {
19373 /* Do we at least have room for a macinfo type byte? */
19374 if (mac_ptr >= mac_end)
19375 {
f664829e 19376 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19377 break;
19378 }
19379
19380 macinfo_type = read_1_byte (abfd, mac_ptr);
19381 mac_ptr++;
19382
cf2c3c16
TT
19383 /* Note that we rely on the fact that the corresponding GNU and
19384 DWARF constants are the same. */
757a13d0
JK
19385 switch (macinfo_type)
19386 {
19387 /* A zero macinfo type indicates the end of the macro
19388 information. */
19389 case 0:
19390 break;
2e276125 19391
cf2c3c16
TT
19392 case DW_MACRO_GNU_define:
19393 case DW_MACRO_GNU_undef:
19394 case DW_MACRO_GNU_define_indirect:
19395 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19396 case DW_MACRO_GNU_define_indirect_alt:
19397 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19398 {
891d2f0b 19399 unsigned int bytes_read;
2e276125 19400 int line;
d521ce57 19401 const char *body;
cf2c3c16 19402 int is_define;
2e276125 19403
cf2c3c16
TT
19404 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19405 mac_ptr += bytes_read;
19406
19407 if (macinfo_type == DW_MACRO_GNU_define
19408 || macinfo_type == DW_MACRO_GNU_undef)
19409 {
19410 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19411 mac_ptr += bytes_read;
19412 }
19413 else
19414 {
19415 LONGEST str_offset;
19416
19417 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19418 mac_ptr += offset_size;
2e276125 19419
36586728 19420 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19421 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19422 || section_is_dwz)
36586728
TT
19423 {
19424 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19425
19426 body = read_indirect_string_from_dwz (dwz, str_offset);
19427 }
19428 else
19429 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19430 }
19431
19432 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19433 || macinfo_type == DW_MACRO_GNU_define_indirect
19434 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19435 if (! current_file)
757a13d0
JK
19436 {
19437 /* DWARF violation as no main source is present. */
19438 complaint (&symfile_complaints,
19439 _("debug info with no main source gives macro %s "
19440 "on line %d: %s"),
cf2c3c16
TT
19441 is_define ? _("definition") : _("undefinition"),
19442 line, body);
757a13d0
JK
19443 break;
19444 }
3e43a32a
MS
19445 if ((line == 0 && !at_commandline)
19446 || (line != 0 && at_commandline))
4d3c2250 19447 complaint (&symfile_complaints,
757a13d0
JK
19448 _("debug info gives %s macro %s with %s line %d: %s"),
19449 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19450 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19451 line == 0 ? _("zero") : _("non-zero"), line, body);
19452
cf2c3c16 19453 if (is_define)
757a13d0 19454 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19455 else
19456 {
19457 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19458 || macinfo_type == DW_MACRO_GNU_undef_indirect
19459 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19460 macro_undef (current_file, line, body);
19461 }
2e276125
JB
19462 }
19463 break;
19464
cf2c3c16 19465 case DW_MACRO_GNU_start_file:
2e276125 19466 {
891d2f0b 19467 unsigned int bytes_read;
2e276125
JB
19468 int line, file;
19469
19470 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19471 mac_ptr += bytes_read;
19472 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19473 mac_ptr += bytes_read;
19474
3e43a32a
MS
19475 if ((line == 0 && !at_commandline)
19476 || (line != 0 && at_commandline))
757a13d0
JK
19477 complaint (&symfile_complaints,
19478 _("debug info gives source %d included "
19479 "from %s at %s line %d"),
19480 file, at_commandline ? _("command-line") : _("file"),
19481 line == 0 ? _("zero") : _("non-zero"), line);
19482
19483 if (at_commandline)
19484 {
cf2c3c16
TT
19485 /* This DW_MACRO_GNU_start_file was executed in the
19486 pass one. */
757a13d0
JK
19487 at_commandline = 0;
19488 }
19489 else
19490 current_file = macro_start_file (file, line,
19491 current_file, comp_dir,
cf2c3c16 19492 lh, objfile);
2e276125
JB
19493 }
19494 break;
19495
cf2c3c16 19496 case DW_MACRO_GNU_end_file:
2e276125 19497 if (! current_file)
4d3c2250 19498 complaint (&symfile_complaints,
3e43a32a
MS
19499 _("macro debug info has an unmatched "
19500 "`close_file' directive"));
2e276125
JB
19501 else
19502 {
19503 current_file = current_file->included_by;
19504 if (! current_file)
19505 {
cf2c3c16 19506 enum dwarf_macro_record_type next_type;
2e276125
JB
19507
19508 /* GCC circa March 2002 doesn't produce the zero
19509 type byte marking the end of the compilation
19510 unit. Complain if it's not there, but exit no
19511 matter what. */
19512
19513 /* Do we at least have room for a macinfo type byte? */
19514 if (mac_ptr >= mac_end)
19515 {
f664829e 19516 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19517 return;
19518 }
19519
19520 /* We don't increment mac_ptr here, so this is just
19521 a look-ahead. */
19522 next_type = read_1_byte (abfd, mac_ptr);
19523 if (next_type != 0)
4d3c2250 19524 complaint (&symfile_complaints,
3e43a32a
MS
19525 _("no terminating 0-type entry for "
19526 "macros in `.debug_macinfo' section"));
2e276125
JB
19527
19528 return;
19529 }
19530 }
19531 break;
19532
cf2c3c16 19533 case DW_MACRO_GNU_transparent_include:
36586728 19534 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19535 {
19536 LONGEST offset;
8fc3fc34 19537 void **slot;
a036ba48
TT
19538 bfd *include_bfd = abfd;
19539 struct dwarf2_section_info *include_section = section;
19540 struct dwarf2_section_info alt_section;
d521ce57 19541 const gdb_byte *include_mac_end = mac_end;
a036ba48 19542 int is_dwz = section_is_dwz;
d521ce57 19543 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19544
19545 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19546 mac_ptr += offset_size;
19547
a036ba48
TT
19548 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19549 {
19550 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19551
19552 dwarf2_read_section (dwarf2_per_objfile->objfile,
19553 &dwz->macro);
19554
19555 include_bfd = dwz->macro.asection->owner;
19556 include_section = &dwz->macro;
19557 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19558 is_dwz = 1;
19559 }
19560
19561 new_mac_ptr = include_section->buffer + offset;
19562 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19563
8fc3fc34
TT
19564 if (*slot != NULL)
19565 {
19566 /* This has actually happened; see
19567 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19568 complaint (&symfile_complaints,
19569 _("recursive DW_MACRO_GNU_transparent_include in "
19570 ".debug_macro section"));
19571 }
19572 else
19573 {
d521ce57 19574 *slot = (void *) new_mac_ptr;
36586728 19575
a036ba48 19576 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19577 include_mac_end, current_file,
8fc3fc34 19578 lh, comp_dir,
36586728 19579 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19580 offset_size, objfile, include_hash);
19581
d521ce57 19582 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19583 }
cf2c3c16
TT
19584 }
19585 break;
19586
2e276125 19587 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19588 if (!section_is_gnu)
19589 {
19590 unsigned int bytes_read;
19591 int constant;
2e276125 19592
cf2c3c16
TT
19593 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19594 mac_ptr += bytes_read;
19595 read_direct_string (abfd, mac_ptr, &bytes_read);
19596 mac_ptr += bytes_read;
2e276125 19597
cf2c3c16
TT
19598 /* We don't recognize any vendor extensions. */
19599 break;
19600 }
19601 /* FALLTHROUGH */
19602
19603 default:
19604 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19605 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19606 section);
19607 if (mac_ptr == NULL)
19608 return;
19609 break;
2e276125 19610 }
757a13d0 19611 } while (macinfo_type != 0);
2e276125 19612}
8e19ed76 19613
cf2c3c16 19614static void
09262596 19615dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19616 const char *comp_dir, int section_is_gnu)
cf2c3c16 19617{
bb5ed363 19618 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19619 struct line_header *lh = cu->line_header;
19620 bfd *abfd;
d521ce57 19621 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19622 struct macro_source_file *current_file = 0;
19623 enum dwarf_macro_record_type macinfo_type;
19624 unsigned int offset_size = cu->header.offset_size;
d521ce57 19625 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19626 struct cleanup *cleanup;
19627 htab_t include_hash;
19628 void **slot;
09262596
DE
19629 struct dwarf2_section_info *section;
19630 const char *section_name;
19631
19632 if (cu->dwo_unit != NULL)
19633 {
19634 if (section_is_gnu)
19635 {
19636 section = &cu->dwo_unit->dwo_file->sections.macro;
19637 section_name = ".debug_macro.dwo";
19638 }
19639 else
19640 {
19641 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19642 section_name = ".debug_macinfo.dwo";
19643 }
19644 }
19645 else
19646 {
19647 if (section_is_gnu)
19648 {
19649 section = &dwarf2_per_objfile->macro;
19650 section_name = ".debug_macro";
19651 }
19652 else
19653 {
19654 section = &dwarf2_per_objfile->macinfo;
19655 section_name = ".debug_macinfo";
19656 }
19657 }
cf2c3c16 19658
bb5ed363 19659 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19660 if (section->buffer == NULL)
19661 {
fceca515 19662 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19663 return;
19664 }
09262596 19665 abfd = section->asection->owner;
cf2c3c16
TT
19666
19667 /* First pass: Find the name of the base filename.
19668 This filename is needed in order to process all macros whose definition
19669 (or undefinition) comes from the command line. These macros are defined
19670 before the first DW_MACINFO_start_file entry, and yet still need to be
19671 associated to the base file.
19672
19673 To determine the base file name, we scan the macro definitions until we
19674 reach the first DW_MACINFO_start_file entry. We then initialize
19675 CURRENT_FILE accordingly so that any macro definition found before the
19676 first DW_MACINFO_start_file can still be associated to the base file. */
19677
19678 mac_ptr = section->buffer + offset;
19679 mac_end = section->buffer + section->size;
19680
19681 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19682 &offset_size, section_is_gnu);
19683 if (mac_ptr == NULL)
19684 {
19685 /* We already issued a complaint. */
19686 return;
19687 }
19688
19689 do
19690 {
19691 /* Do we at least have room for a macinfo type byte? */
19692 if (mac_ptr >= mac_end)
19693 {
19694 /* Complaint is printed during the second pass as GDB will probably
19695 stop the first pass earlier upon finding
19696 DW_MACINFO_start_file. */
19697 break;
19698 }
19699
19700 macinfo_type = read_1_byte (abfd, mac_ptr);
19701 mac_ptr++;
19702
19703 /* Note that we rely on the fact that the corresponding GNU and
19704 DWARF constants are the same. */
19705 switch (macinfo_type)
19706 {
19707 /* A zero macinfo type indicates the end of the macro
19708 information. */
19709 case 0:
19710 break;
19711
19712 case DW_MACRO_GNU_define:
19713 case DW_MACRO_GNU_undef:
19714 /* Only skip the data by MAC_PTR. */
19715 {
19716 unsigned int bytes_read;
19717
19718 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19719 mac_ptr += bytes_read;
19720 read_direct_string (abfd, mac_ptr, &bytes_read);
19721 mac_ptr += bytes_read;
19722 }
19723 break;
19724
19725 case DW_MACRO_GNU_start_file:
19726 {
19727 unsigned int bytes_read;
19728 int line, file;
19729
19730 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19731 mac_ptr += bytes_read;
19732 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19733 mac_ptr += bytes_read;
19734
19735 current_file = macro_start_file (file, line, current_file,
bb5ed363 19736 comp_dir, lh, objfile);
cf2c3c16
TT
19737 }
19738 break;
19739
19740 case DW_MACRO_GNU_end_file:
19741 /* No data to skip by MAC_PTR. */
19742 break;
19743
19744 case DW_MACRO_GNU_define_indirect:
19745 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19746 case DW_MACRO_GNU_define_indirect_alt:
19747 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19748 {
19749 unsigned int bytes_read;
19750
19751 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19752 mac_ptr += bytes_read;
19753 mac_ptr += offset_size;
19754 }
19755 break;
19756
19757 case DW_MACRO_GNU_transparent_include:
f7a35f02 19758 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19759 /* Note that, according to the spec, a transparent include
19760 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19761 skip this opcode. */
19762 mac_ptr += offset_size;
19763 break;
19764
19765 case DW_MACINFO_vendor_ext:
19766 /* Only skip the data by MAC_PTR. */
19767 if (!section_is_gnu)
19768 {
19769 unsigned int bytes_read;
19770
19771 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19772 mac_ptr += bytes_read;
19773 read_direct_string (abfd, mac_ptr, &bytes_read);
19774 mac_ptr += bytes_read;
19775 }
19776 /* FALLTHROUGH */
19777
19778 default:
19779 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19780 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19781 section);
19782 if (mac_ptr == NULL)
19783 return;
19784 break;
19785 }
19786 } while (macinfo_type != 0 && current_file == NULL);
19787
19788 /* Second pass: Process all entries.
19789
19790 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19791 command-line macro definitions/undefinitions. This flag is unset when we
19792 reach the first DW_MACINFO_start_file entry. */
19793
8fc3fc34
TT
19794 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19795 NULL, xcalloc, xfree);
19796 cleanup = make_cleanup_htab_delete (include_hash);
19797 mac_ptr = section->buffer + offset;
19798 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19799 *slot = (void *) mac_ptr;
8fc3fc34 19800 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19801 current_file, lh, comp_dir, section,
19802 section_is_gnu, 0,
8fc3fc34
TT
19803 offset_size, objfile, include_hash);
19804 do_cleanups (cleanup);
cf2c3c16
TT
19805}
19806
8e19ed76 19807/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19808 if so return true else false. */
380bca97 19809
8e19ed76 19810static int
6e5a29e1 19811attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
19812{
19813 return (attr == NULL ? 0 :
19814 attr->form == DW_FORM_block1
19815 || attr->form == DW_FORM_block2
19816 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19817 || attr->form == DW_FORM_block
19818 || attr->form == DW_FORM_exprloc);
8e19ed76 19819}
4c2df51b 19820
c6a0999f
JB
19821/* Return non-zero if ATTR's value is a section offset --- classes
19822 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19823 You may use DW_UNSND (attr) to retrieve such offsets.
19824
19825 Section 7.5.4, "Attribute Encodings", explains that no attribute
19826 may have a value that belongs to more than one of these classes; it
19827 would be ambiguous if we did, because we use the same forms for all
19828 of them. */
380bca97 19829
3690dd37 19830static int
6e5a29e1 19831attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
19832{
19833 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19834 || attr->form == DW_FORM_data8
19835 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19836}
19837
3690dd37
JB
19838/* Return non-zero if ATTR's value falls in the 'constant' class, or
19839 zero otherwise. When this function returns true, you can apply
19840 dwarf2_get_attr_constant_value to it.
19841
19842 However, note that for some attributes you must check
19843 attr_form_is_section_offset before using this test. DW_FORM_data4
19844 and DW_FORM_data8 are members of both the constant class, and of
19845 the classes that contain offsets into other debug sections
19846 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19847 that, if an attribute's can be either a constant or one of the
19848 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19849 taken as section offsets, not constants. */
380bca97 19850
3690dd37 19851static int
6e5a29e1 19852attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
19853{
19854 switch (attr->form)
19855 {
19856 case DW_FORM_sdata:
19857 case DW_FORM_udata:
19858 case DW_FORM_data1:
19859 case DW_FORM_data2:
19860 case DW_FORM_data4:
19861 case DW_FORM_data8:
19862 return 1;
19863 default:
19864 return 0;
19865 }
19866}
19867
7771576e
SA
19868
19869/* DW_ADDR is always stored already as sect_offset; despite for the forms
19870 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19871
19872static int
6e5a29e1 19873attr_form_is_ref (const struct attribute *attr)
7771576e
SA
19874{
19875 switch (attr->form)
19876 {
19877 case DW_FORM_ref_addr:
19878 case DW_FORM_ref1:
19879 case DW_FORM_ref2:
19880 case DW_FORM_ref4:
19881 case DW_FORM_ref8:
19882 case DW_FORM_ref_udata:
19883 case DW_FORM_GNU_ref_alt:
19884 return 1;
19885 default:
19886 return 0;
19887 }
19888}
19889
3019eac3
DE
19890/* Return the .debug_loc section to use for CU.
19891 For DWO files use .debug_loc.dwo. */
19892
19893static struct dwarf2_section_info *
19894cu_debug_loc_section (struct dwarf2_cu *cu)
19895{
19896 if (cu->dwo_unit)
19897 return &cu->dwo_unit->dwo_file->sections.loc;
19898 return &dwarf2_per_objfile->loc;
19899}
19900
8cf6f0b1
TT
19901/* A helper function that fills in a dwarf2_loclist_baton. */
19902
19903static void
19904fill_in_loclist_baton (struct dwarf2_cu *cu,
19905 struct dwarf2_loclist_baton *baton,
ff39bb5e 19906 const struct attribute *attr)
8cf6f0b1 19907{
3019eac3
DE
19908 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19909
19910 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19911
19912 baton->per_cu = cu->per_cu;
19913 gdb_assert (baton->per_cu);
19914 /* We don't know how long the location list is, but make sure we
19915 don't run off the edge of the section. */
3019eac3
DE
19916 baton->size = section->size - DW_UNSND (attr);
19917 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19918 baton->base_address = cu->base_address;
f664829e 19919 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19920}
19921
4c2df51b 19922static void
ff39bb5e 19923dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 19924 struct dwarf2_cu *cu, int is_block)
4c2df51b 19925{
bb5ed363 19926 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19927 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19928
3690dd37 19929 if (attr_form_is_section_offset (attr)
3019eac3 19930 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19931 the section. If so, fall through to the complaint in the
19932 other branch. */
3019eac3 19933 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19934 {
0d53c4c4 19935 struct dwarf2_loclist_baton *baton;
4c2df51b 19936
bb5ed363 19937 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19938 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19939
8cf6f0b1 19940 fill_in_loclist_baton (cu, baton, attr);
be391dca 19941
d00adf39 19942 if (cu->base_known == 0)
0d53c4c4 19943 complaint (&symfile_complaints,
3e43a32a
MS
19944 _("Location list used without "
19945 "specifying the CU base address."));
4c2df51b 19946
f1e6e072
TT
19947 SYMBOL_ACLASS_INDEX (sym) = (is_block
19948 ? dwarf2_loclist_block_index
19949 : dwarf2_loclist_index);
0d53c4c4
DJ
19950 SYMBOL_LOCATION_BATON (sym) = baton;
19951 }
19952 else
19953 {
19954 struct dwarf2_locexpr_baton *baton;
19955
bb5ed363 19956 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19957 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19958 baton->per_cu = cu->per_cu;
19959 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19960
19961 if (attr_form_is_block (attr))
19962 {
19963 /* Note that we're just copying the block's data pointer
19964 here, not the actual data. We're still pointing into the
6502dd73
DJ
19965 info_buffer for SYM's objfile; right now we never release
19966 that buffer, but when we do clean up properly this may
19967 need to change. */
0d53c4c4
DJ
19968 baton->size = DW_BLOCK (attr)->size;
19969 baton->data = DW_BLOCK (attr)->data;
19970 }
19971 else
19972 {
19973 dwarf2_invalid_attrib_class_complaint ("location description",
19974 SYMBOL_NATURAL_NAME (sym));
19975 baton->size = 0;
0d53c4c4 19976 }
6e70227d 19977
f1e6e072
TT
19978 SYMBOL_ACLASS_INDEX (sym) = (is_block
19979 ? dwarf2_locexpr_block_index
19980 : dwarf2_locexpr_index);
0d53c4c4
DJ
19981 SYMBOL_LOCATION_BATON (sym) = baton;
19982 }
4c2df51b 19983}
6502dd73 19984
9aa1f1e3
TT
19985/* Return the OBJFILE associated with the compilation unit CU. If CU
19986 came from a separate debuginfo file, then the master objfile is
19987 returned. */
ae0d2f24
UW
19988
19989struct objfile *
19990dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19991{
9291a0cd 19992 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
19993
19994 /* Return the master objfile, so that we can report and look up the
19995 correct file containing this variable. */
19996 if (objfile->separate_debug_objfile_backlink)
19997 objfile = objfile->separate_debug_objfile_backlink;
19998
19999 return objfile;
20000}
20001
96408a79
SA
20002/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20003 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20004 CU_HEADERP first. */
20005
20006static const struct comp_unit_head *
20007per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20008 struct dwarf2_per_cu_data *per_cu)
20009{
d521ce57 20010 const gdb_byte *info_ptr;
96408a79
SA
20011
20012 if (per_cu->cu)
20013 return &per_cu->cu->header;
20014
8a0459fd 20015 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20016
20017 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20018 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20019
20020 return cu_headerp;
20021}
20022
ae0d2f24
UW
20023/* Return the address size given in the compilation unit header for CU. */
20024
98714339 20025int
ae0d2f24
UW
20026dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20027{
96408a79
SA
20028 struct comp_unit_head cu_header_local;
20029 const struct comp_unit_head *cu_headerp;
c471e790 20030
96408a79
SA
20031 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20032
20033 return cu_headerp->addr_size;
ae0d2f24
UW
20034}
20035
9eae7c52
TT
20036/* Return the offset size given in the compilation unit header for CU. */
20037
20038int
20039dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20040{
96408a79
SA
20041 struct comp_unit_head cu_header_local;
20042 const struct comp_unit_head *cu_headerp;
9c6c53f7 20043
96408a79
SA
20044 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20045
20046 return cu_headerp->offset_size;
20047}
20048
20049/* See its dwarf2loc.h declaration. */
20050
20051int
20052dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20053{
20054 struct comp_unit_head cu_header_local;
20055 const struct comp_unit_head *cu_headerp;
20056
20057 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20058
20059 if (cu_headerp->version == 2)
20060 return cu_headerp->addr_size;
20061 else
20062 return cu_headerp->offset_size;
181cebd4
JK
20063}
20064
9aa1f1e3
TT
20065/* Return the text offset of the CU. The returned offset comes from
20066 this CU's objfile. If this objfile came from a separate debuginfo
20067 file, then the offset may be different from the corresponding
20068 offset in the parent objfile. */
20069
20070CORE_ADDR
20071dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20072{
bb3fa9d0 20073 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20074
20075 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20076}
20077
348e048f
DE
20078/* Locate the .debug_info compilation unit from CU's objfile which contains
20079 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20080
20081static struct dwarf2_per_cu_data *
b64f50a1 20082dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20083 unsigned int offset_in_dwz,
ae038cb0
DJ
20084 struct objfile *objfile)
20085{
20086 struct dwarf2_per_cu_data *this_cu;
20087 int low, high;
36586728 20088 const sect_offset *cu_off;
ae038cb0 20089
ae038cb0
DJ
20090 low = 0;
20091 high = dwarf2_per_objfile->n_comp_units - 1;
20092 while (high > low)
20093 {
36586728 20094 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20095 int mid = low + (high - low) / 2;
9a619af0 20096
36586728
TT
20097 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20098 cu_off = &mid_cu->offset;
20099 if (mid_cu->is_dwz > offset_in_dwz
20100 || (mid_cu->is_dwz == offset_in_dwz
20101 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20102 high = mid;
20103 else
20104 low = mid + 1;
20105 }
20106 gdb_assert (low == high);
36586728
TT
20107 this_cu = dwarf2_per_objfile->all_comp_units[low];
20108 cu_off = &this_cu->offset;
20109 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20110 {
36586728 20111 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20112 error (_("Dwarf Error: could not find partial DIE containing "
20113 "offset 0x%lx [in module %s]"),
b64f50a1 20114 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20115
b64f50a1
JK
20116 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20117 <= offset.sect_off);
ae038cb0
DJ
20118 return dwarf2_per_objfile->all_comp_units[low-1];
20119 }
20120 else
20121 {
20122 this_cu = dwarf2_per_objfile->all_comp_units[low];
20123 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20124 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20125 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20126 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20127 return this_cu;
20128 }
20129}
20130
23745b47 20131/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20132
9816fde3 20133static void
23745b47 20134init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20135{
9816fde3 20136 memset (cu, 0, sizeof (*cu));
23745b47
DE
20137 per_cu->cu = cu;
20138 cu->per_cu = per_cu;
20139 cu->objfile = per_cu->objfile;
93311388 20140 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20141}
20142
20143/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20144
20145static void
95554aad
TT
20146prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20147 enum language pretend_language)
9816fde3
JK
20148{
20149 struct attribute *attr;
20150
20151 /* Set the language we're debugging. */
20152 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20153 if (attr)
20154 set_cu_language (DW_UNSND (attr), cu);
20155 else
9cded63f 20156 {
95554aad 20157 cu->language = pretend_language;
9cded63f
TT
20158 cu->language_defn = language_def (cu->language);
20159 }
dee91e82
DE
20160
20161 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20162 if (attr)
20163 cu->producer = DW_STRING (attr);
93311388
DE
20164}
20165
ae038cb0
DJ
20166/* Release one cached compilation unit, CU. We unlink it from the tree
20167 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20168 the caller is responsible for that.
20169 NOTE: DATA is a void * because this function is also used as a
20170 cleanup routine. */
ae038cb0
DJ
20171
20172static void
68dc6402 20173free_heap_comp_unit (void *data)
ae038cb0
DJ
20174{
20175 struct dwarf2_cu *cu = data;
20176
23745b47
DE
20177 gdb_assert (cu->per_cu != NULL);
20178 cu->per_cu->cu = NULL;
ae038cb0
DJ
20179 cu->per_cu = NULL;
20180
20181 obstack_free (&cu->comp_unit_obstack, NULL);
20182
20183 xfree (cu);
20184}
20185
72bf9492 20186/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20187 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20188 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20189
20190static void
20191free_stack_comp_unit (void *data)
20192{
20193 struct dwarf2_cu *cu = data;
20194
23745b47
DE
20195 gdb_assert (cu->per_cu != NULL);
20196 cu->per_cu->cu = NULL;
20197 cu->per_cu = NULL;
20198
72bf9492
DJ
20199 obstack_free (&cu->comp_unit_obstack, NULL);
20200 cu->partial_dies = NULL;
ae038cb0
DJ
20201}
20202
20203/* Free all cached compilation units. */
20204
20205static void
20206free_cached_comp_units (void *data)
20207{
20208 struct dwarf2_per_cu_data *per_cu, **last_chain;
20209
20210 per_cu = dwarf2_per_objfile->read_in_chain;
20211 last_chain = &dwarf2_per_objfile->read_in_chain;
20212 while (per_cu != NULL)
20213 {
20214 struct dwarf2_per_cu_data *next_cu;
20215
20216 next_cu = per_cu->cu->read_in_chain;
20217
68dc6402 20218 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20219 *last_chain = next_cu;
20220
20221 per_cu = next_cu;
20222 }
20223}
20224
20225/* Increase the age counter on each cached compilation unit, and free
20226 any that are too old. */
20227
20228static void
20229age_cached_comp_units (void)
20230{
20231 struct dwarf2_per_cu_data *per_cu, **last_chain;
20232
20233 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20234 per_cu = dwarf2_per_objfile->read_in_chain;
20235 while (per_cu != NULL)
20236 {
20237 per_cu->cu->last_used ++;
20238 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20239 dwarf2_mark (per_cu->cu);
20240 per_cu = per_cu->cu->read_in_chain;
20241 }
20242
20243 per_cu = dwarf2_per_objfile->read_in_chain;
20244 last_chain = &dwarf2_per_objfile->read_in_chain;
20245 while (per_cu != NULL)
20246 {
20247 struct dwarf2_per_cu_data *next_cu;
20248
20249 next_cu = per_cu->cu->read_in_chain;
20250
20251 if (!per_cu->cu->mark)
20252 {
68dc6402 20253 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20254 *last_chain = next_cu;
20255 }
20256 else
20257 last_chain = &per_cu->cu->read_in_chain;
20258
20259 per_cu = next_cu;
20260 }
20261}
20262
20263/* Remove a single compilation unit from the cache. */
20264
20265static void
dee91e82 20266free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20267{
20268 struct dwarf2_per_cu_data *per_cu, **last_chain;
20269
20270 per_cu = dwarf2_per_objfile->read_in_chain;
20271 last_chain = &dwarf2_per_objfile->read_in_chain;
20272 while (per_cu != NULL)
20273 {
20274 struct dwarf2_per_cu_data *next_cu;
20275
20276 next_cu = per_cu->cu->read_in_chain;
20277
dee91e82 20278 if (per_cu == target_per_cu)
ae038cb0 20279 {
68dc6402 20280 free_heap_comp_unit (per_cu->cu);
dee91e82 20281 per_cu->cu = NULL;
ae038cb0
DJ
20282 *last_chain = next_cu;
20283 break;
20284 }
20285 else
20286 last_chain = &per_cu->cu->read_in_chain;
20287
20288 per_cu = next_cu;
20289 }
20290}
20291
fe3e1990
DJ
20292/* Release all extra memory associated with OBJFILE. */
20293
20294void
20295dwarf2_free_objfile (struct objfile *objfile)
20296{
20297 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20298
20299 if (dwarf2_per_objfile == NULL)
20300 return;
20301
20302 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20303 free_cached_comp_units (NULL);
20304
7b9f3c50
DE
20305 if (dwarf2_per_objfile->quick_file_names_table)
20306 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20307
fe3e1990
DJ
20308 /* Everything else should be on the objfile obstack. */
20309}
20310
dee91e82
DE
20311/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20312 We store these in a hash table separate from the DIEs, and preserve them
20313 when the DIEs are flushed out of cache.
20314
20315 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20316 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20317 or the type may come from a DWO file. Furthermore, while it's more logical
20318 to use per_cu->section+offset, with Fission the section with the data is in
20319 the DWO file but we don't know that section at the point we need it.
20320 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20321 because we can enter the lookup routine, get_die_type_at_offset, from
20322 outside this file, and thus won't necessarily have PER_CU->cu.
20323 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20324
dee91e82 20325struct dwarf2_per_cu_offset_and_type
1c379e20 20326{
dee91e82 20327 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20328 sect_offset offset;
1c379e20
DJ
20329 struct type *type;
20330};
20331
dee91e82 20332/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20333
20334static hashval_t
dee91e82 20335per_cu_offset_and_type_hash (const void *item)
1c379e20 20336{
dee91e82 20337 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20338
dee91e82 20339 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20340}
20341
dee91e82 20342/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20343
20344static int
dee91e82 20345per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20346{
dee91e82
DE
20347 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20348 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20349
dee91e82
DE
20350 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20351 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20352}
20353
20354/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20355 table if necessary. For convenience, return TYPE.
20356
20357 The DIEs reading must have careful ordering to:
20358 * Not cause infite loops trying to read in DIEs as a prerequisite for
20359 reading current DIE.
20360 * Not trying to dereference contents of still incompletely read in types
20361 while reading in other DIEs.
20362 * Enable referencing still incompletely read in types just by a pointer to
20363 the type without accessing its fields.
20364
20365 Therefore caller should follow these rules:
20366 * Try to fetch any prerequisite types we may need to build this DIE type
20367 before building the type and calling set_die_type.
e71ec853 20368 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20369 possible before fetching more types to complete the current type.
20370 * Make the type as complete as possible before fetching more types. */
1c379e20 20371
f792889a 20372static struct type *
1c379e20
DJ
20373set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20374{
dee91e82 20375 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20376 struct objfile *objfile = cu->objfile;
1c379e20 20377
b4ba55a1
JB
20378 /* For Ada types, make sure that the gnat-specific data is always
20379 initialized (if not already set). There are a few types where
20380 we should not be doing so, because the type-specific area is
20381 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20382 where the type-specific area is used to store the floatformat).
20383 But this is not a problem, because the gnat-specific information
20384 is actually not needed for these types. */
20385 if (need_gnat_info (cu)
20386 && TYPE_CODE (type) != TYPE_CODE_FUNC
20387 && TYPE_CODE (type) != TYPE_CODE_FLT
20388 && !HAVE_GNAT_AUX_INFO (type))
20389 INIT_GNAT_SPECIFIC (type);
20390
dee91e82 20391 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20392 {
dee91e82
DE
20393 dwarf2_per_objfile->die_type_hash =
20394 htab_create_alloc_ex (127,
20395 per_cu_offset_and_type_hash,
20396 per_cu_offset_and_type_eq,
20397 NULL,
20398 &objfile->objfile_obstack,
20399 hashtab_obstack_allocate,
20400 dummy_obstack_deallocate);
f792889a 20401 }
1c379e20 20402
dee91e82 20403 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20404 ofs.offset = die->offset;
20405 ofs.type = type;
dee91e82
DE
20406 slot = (struct dwarf2_per_cu_offset_and_type **)
20407 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20408 if (*slot)
20409 complaint (&symfile_complaints,
20410 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20411 die->offset.sect_off);
673bfd45 20412 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20413 **slot = ofs;
f792889a 20414 return type;
1c379e20
DJ
20415}
20416
02142a6c
DE
20417/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20418 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20419
20420static struct type *
b64f50a1 20421get_die_type_at_offset (sect_offset offset,
673bfd45 20422 struct dwarf2_per_cu_data *per_cu)
1c379e20 20423{
dee91e82 20424 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20425
dee91e82 20426 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20427 return NULL;
1c379e20 20428
dee91e82 20429 ofs.per_cu = per_cu;
673bfd45 20430 ofs.offset = offset;
dee91e82 20431 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20432 if (slot)
20433 return slot->type;
20434 else
20435 return NULL;
20436}
20437
02142a6c 20438/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20439 or return NULL if DIE does not have a saved type. */
20440
20441static struct type *
20442get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20443{
20444 return get_die_type_at_offset (die->offset, cu->per_cu);
20445}
20446
10b3939b
DJ
20447/* Add a dependence relationship from CU to REF_PER_CU. */
20448
20449static void
20450dwarf2_add_dependence (struct dwarf2_cu *cu,
20451 struct dwarf2_per_cu_data *ref_per_cu)
20452{
20453 void **slot;
20454
20455 if (cu->dependencies == NULL)
20456 cu->dependencies
20457 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20458 NULL, &cu->comp_unit_obstack,
20459 hashtab_obstack_allocate,
20460 dummy_obstack_deallocate);
20461
20462 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20463 if (*slot == NULL)
20464 *slot = ref_per_cu;
20465}
1c379e20 20466
f504f079
DE
20467/* Subroutine of dwarf2_mark to pass to htab_traverse.
20468 Set the mark field in every compilation unit in the
ae038cb0
DJ
20469 cache that we must keep because we are keeping CU. */
20470
10b3939b
DJ
20471static int
20472dwarf2_mark_helper (void **slot, void *data)
20473{
20474 struct dwarf2_per_cu_data *per_cu;
20475
20476 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20477
20478 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20479 reading of the chain. As such dependencies remain valid it is not much
20480 useful to track and undo them during QUIT cleanups. */
20481 if (per_cu->cu == NULL)
20482 return 1;
20483
10b3939b
DJ
20484 if (per_cu->cu->mark)
20485 return 1;
20486 per_cu->cu->mark = 1;
20487
20488 if (per_cu->cu->dependencies != NULL)
20489 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20490
20491 return 1;
20492}
20493
f504f079
DE
20494/* Set the mark field in CU and in every other compilation unit in the
20495 cache that we must keep because we are keeping CU. */
20496
ae038cb0
DJ
20497static void
20498dwarf2_mark (struct dwarf2_cu *cu)
20499{
20500 if (cu->mark)
20501 return;
20502 cu->mark = 1;
10b3939b
DJ
20503 if (cu->dependencies != NULL)
20504 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20505}
20506
20507static void
20508dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20509{
20510 while (per_cu)
20511 {
20512 per_cu->cu->mark = 0;
20513 per_cu = per_cu->cu->read_in_chain;
20514 }
72bf9492
DJ
20515}
20516
72bf9492
DJ
20517/* Trivial hash function for partial_die_info: the hash value of a DIE
20518 is its offset in .debug_info for this objfile. */
20519
20520static hashval_t
20521partial_die_hash (const void *item)
20522{
20523 const struct partial_die_info *part_die = item;
9a619af0 20524
b64f50a1 20525 return part_die->offset.sect_off;
72bf9492
DJ
20526}
20527
20528/* Trivial comparison function for partial_die_info structures: two DIEs
20529 are equal if they have the same offset. */
20530
20531static int
20532partial_die_eq (const void *item_lhs, const void *item_rhs)
20533{
20534 const struct partial_die_info *part_die_lhs = item_lhs;
20535 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20536
b64f50a1 20537 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20538}
20539
ae038cb0
DJ
20540static struct cmd_list_element *set_dwarf2_cmdlist;
20541static struct cmd_list_element *show_dwarf2_cmdlist;
20542
20543static void
20544set_dwarf2_cmd (char *args, int from_tty)
20545{
20546 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20547}
20548
20549static void
20550show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20551{
ae038cb0
DJ
20552 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20553}
20554
4bf44c1c 20555/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20556
20557static void
c1bd65d0 20558dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20559{
20560 struct dwarf2_per_objfile *data = d;
8b70b953 20561 int ix;
8b70b953 20562
626f2d1c
TT
20563 /* Make sure we don't accidentally use dwarf2_per_objfile while
20564 cleaning up. */
20565 dwarf2_per_objfile = NULL;
20566
59b0c7c1
JB
20567 for (ix = 0; ix < data->n_comp_units; ++ix)
20568 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 20569
59b0c7c1 20570 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 20571 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
20572 data->all_type_units[ix]->per_cu.imported_symtabs);
20573 xfree (data->all_type_units);
95554aad 20574
8b70b953 20575 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20576
20577 if (data->dwo_files)
20578 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20579 if (data->dwp_file)
20580 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20581
20582 if (data->dwz_file && data->dwz_file->dwz_bfd)
20583 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20584}
20585
20586\f
ae2de4f8 20587/* The "save gdb-index" command. */
9291a0cd
TT
20588
20589/* The contents of the hash table we create when building the string
20590 table. */
20591struct strtab_entry
20592{
20593 offset_type offset;
20594 const char *str;
20595};
20596
559a7a62
JK
20597/* Hash function for a strtab_entry.
20598
20599 Function is used only during write_hash_table so no index format backward
20600 compatibility is needed. */
b89be57b 20601
9291a0cd
TT
20602static hashval_t
20603hash_strtab_entry (const void *e)
20604{
20605 const struct strtab_entry *entry = e;
559a7a62 20606 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20607}
20608
20609/* Equality function for a strtab_entry. */
b89be57b 20610
9291a0cd
TT
20611static int
20612eq_strtab_entry (const void *a, const void *b)
20613{
20614 const struct strtab_entry *ea = a;
20615 const struct strtab_entry *eb = b;
20616 return !strcmp (ea->str, eb->str);
20617}
20618
20619/* Create a strtab_entry hash table. */
b89be57b 20620
9291a0cd
TT
20621static htab_t
20622create_strtab (void)
20623{
20624 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20625 xfree, xcalloc, xfree);
20626}
20627
20628/* Add a string to the constant pool. Return the string's offset in
20629 host order. */
b89be57b 20630
9291a0cd
TT
20631static offset_type
20632add_string (htab_t table, struct obstack *cpool, const char *str)
20633{
20634 void **slot;
20635 struct strtab_entry entry;
20636 struct strtab_entry *result;
20637
20638 entry.str = str;
20639 slot = htab_find_slot (table, &entry, INSERT);
20640 if (*slot)
20641 result = *slot;
20642 else
20643 {
20644 result = XNEW (struct strtab_entry);
20645 result->offset = obstack_object_size (cpool);
20646 result->str = str;
20647 obstack_grow_str0 (cpool, str);
20648 *slot = result;
20649 }
20650 return result->offset;
20651}
20652
20653/* An entry in the symbol table. */
20654struct symtab_index_entry
20655{
20656 /* The name of the symbol. */
20657 const char *name;
20658 /* The offset of the name in the constant pool. */
20659 offset_type index_offset;
20660 /* A sorted vector of the indices of all the CUs that hold an object
20661 of this name. */
20662 VEC (offset_type) *cu_indices;
20663};
20664
20665/* The symbol table. This is a power-of-2-sized hash table. */
20666struct mapped_symtab
20667{
20668 offset_type n_elements;
20669 offset_type size;
20670 struct symtab_index_entry **data;
20671};
20672
20673/* Hash function for a symtab_index_entry. */
b89be57b 20674
9291a0cd
TT
20675static hashval_t
20676hash_symtab_entry (const void *e)
20677{
20678 const struct symtab_index_entry *entry = e;
20679 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20680 sizeof (offset_type) * VEC_length (offset_type,
20681 entry->cu_indices),
20682 0);
20683}
20684
20685/* Equality function for a symtab_index_entry. */
b89be57b 20686
9291a0cd
TT
20687static int
20688eq_symtab_entry (const void *a, const void *b)
20689{
20690 const struct symtab_index_entry *ea = a;
20691 const struct symtab_index_entry *eb = b;
20692 int len = VEC_length (offset_type, ea->cu_indices);
20693 if (len != VEC_length (offset_type, eb->cu_indices))
20694 return 0;
20695 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20696 VEC_address (offset_type, eb->cu_indices),
20697 sizeof (offset_type) * len);
20698}
20699
20700/* Destroy a symtab_index_entry. */
b89be57b 20701
9291a0cd
TT
20702static void
20703delete_symtab_entry (void *p)
20704{
20705 struct symtab_index_entry *entry = p;
20706 VEC_free (offset_type, entry->cu_indices);
20707 xfree (entry);
20708}
20709
20710/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20711
9291a0cd 20712static htab_t
3876f04e 20713create_symbol_hash_table (void)
9291a0cd
TT
20714{
20715 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20716 delete_symtab_entry, xcalloc, xfree);
20717}
20718
20719/* Create a new mapped symtab object. */
b89be57b 20720
9291a0cd
TT
20721static struct mapped_symtab *
20722create_mapped_symtab (void)
20723{
20724 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20725 symtab->n_elements = 0;
20726 symtab->size = 1024;
20727 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20728 return symtab;
20729}
20730
20731/* Destroy a mapped_symtab. */
b89be57b 20732
9291a0cd
TT
20733static void
20734cleanup_mapped_symtab (void *p)
20735{
20736 struct mapped_symtab *symtab = p;
20737 /* The contents of the array are freed when the other hash table is
20738 destroyed. */
20739 xfree (symtab->data);
20740 xfree (symtab);
20741}
20742
20743/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20744 the slot.
20745
20746 Function is used only during write_hash_table so no index format backward
20747 compatibility is needed. */
b89be57b 20748
9291a0cd
TT
20749static struct symtab_index_entry **
20750find_slot (struct mapped_symtab *symtab, const char *name)
20751{
559a7a62 20752 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20753
20754 index = hash & (symtab->size - 1);
20755 step = ((hash * 17) & (symtab->size - 1)) | 1;
20756
20757 for (;;)
20758 {
20759 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20760 return &symtab->data[index];
20761 index = (index + step) & (symtab->size - 1);
20762 }
20763}
20764
20765/* Expand SYMTAB's hash table. */
b89be57b 20766
9291a0cd
TT
20767static void
20768hash_expand (struct mapped_symtab *symtab)
20769{
20770 offset_type old_size = symtab->size;
20771 offset_type i;
20772 struct symtab_index_entry **old_entries = symtab->data;
20773
20774 symtab->size *= 2;
20775 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20776
20777 for (i = 0; i < old_size; ++i)
20778 {
20779 if (old_entries[i])
20780 {
20781 struct symtab_index_entry **slot = find_slot (symtab,
20782 old_entries[i]->name);
20783 *slot = old_entries[i];
20784 }
20785 }
20786
20787 xfree (old_entries);
20788}
20789
156942c7
DE
20790/* Add an entry to SYMTAB. NAME is the name of the symbol.
20791 CU_INDEX is the index of the CU in which the symbol appears.
20792 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20793
9291a0cd
TT
20794static void
20795add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20796 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20797 offset_type cu_index)
20798{
20799 struct symtab_index_entry **slot;
156942c7 20800 offset_type cu_index_and_attrs;
9291a0cd
TT
20801
20802 ++symtab->n_elements;
20803 if (4 * symtab->n_elements / 3 >= symtab->size)
20804 hash_expand (symtab);
20805
20806 slot = find_slot (symtab, name);
20807 if (!*slot)
20808 {
20809 *slot = XNEW (struct symtab_index_entry);
20810 (*slot)->name = name;
156942c7 20811 /* index_offset is set later. */
9291a0cd
TT
20812 (*slot)->cu_indices = NULL;
20813 }
156942c7
DE
20814
20815 cu_index_and_attrs = 0;
20816 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20817 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20818 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20819
20820 /* We don't want to record an index value twice as we want to avoid the
20821 duplication.
20822 We process all global symbols and then all static symbols
20823 (which would allow us to avoid the duplication by only having to check
20824 the last entry pushed), but a symbol could have multiple kinds in one CU.
20825 To keep things simple we don't worry about the duplication here and
20826 sort and uniqufy the list after we've processed all symbols. */
20827 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20828}
20829
20830/* qsort helper routine for uniquify_cu_indices. */
20831
20832static int
20833offset_type_compare (const void *ap, const void *bp)
20834{
20835 offset_type a = *(offset_type *) ap;
20836 offset_type b = *(offset_type *) bp;
20837
20838 return (a > b) - (b > a);
20839}
20840
20841/* Sort and remove duplicates of all symbols' cu_indices lists. */
20842
20843static void
20844uniquify_cu_indices (struct mapped_symtab *symtab)
20845{
20846 int i;
20847
20848 for (i = 0; i < symtab->size; ++i)
20849 {
20850 struct symtab_index_entry *entry = symtab->data[i];
20851
20852 if (entry
20853 && entry->cu_indices != NULL)
20854 {
20855 unsigned int next_to_insert, next_to_check;
20856 offset_type last_value;
20857
20858 qsort (VEC_address (offset_type, entry->cu_indices),
20859 VEC_length (offset_type, entry->cu_indices),
20860 sizeof (offset_type), offset_type_compare);
20861
20862 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20863 next_to_insert = 1;
20864 for (next_to_check = 1;
20865 next_to_check < VEC_length (offset_type, entry->cu_indices);
20866 ++next_to_check)
20867 {
20868 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20869 != last_value)
20870 {
20871 last_value = VEC_index (offset_type, entry->cu_indices,
20872 next_to_check);
20873 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20874 last_value);
20875 ++next_to_insert;
20876 }
20877 }
20878 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20879 }
20880 }
9291a0cd
TT
20881}
20882
20883/* Add a vector of indices to the constant pool. */
b89be57b 20884
9291a0cd 20885static offset_type
3876f04e 20886add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20887 struct symtab_index_entry *entry)
20888{
20889 void **slot;
20890
3876f04e 20891 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20892 if (!*slot)
20893 {
20894 offset_type len = VEC_length (offset_type, entry->cu_indices);
20895 offset_type val = MAYBE_SWAP (len);
20896 offset_type iter;
20897 int i;
20898
20899 *slot = entry;
20900 entry->index_offset = obstack_object_size (cpool);
20901
20902 obstack_grow (cpool, &val, sizeof (val));
20903 for (i = 0;
20904 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20905 ++i)
20906 {
20907 val = MAYBE_SWAP (iter);
20908 obstack_grow (cpool, &val, sizeof (val));
20909 }
20910 }
20911 else
20912 {
20913 struct symtab_index_entry *old_entry = *slot;
20914 entry->index_offset = old_entry->index_offset;
20915 entry = old_entry;
20916 }
20917 return entry->index_offset;
20918}
20919
20920/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20921 constant pool entries going into the obstack CPOOL. */
b89be57b 20922
9291a0cd
TT
20923static void
20924write_hash_table (struct mapped_symtab *symtab,
20925 struct obstack *output, struct obstack *cpool)
20926{
20927 offset_type i;
3876f04e 20928 htab_t symbol_hash_table;
9291a0cd
TT
20929 htab_t str_table;
20930
3876f04e 20931 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20932 str_table = create_strtab ();
3876f04e 20933
9291a0cd
TT
20934 /* We add all the index vectors to the constant pool first, to
20935 ensure alignment is ok. */
20936 for (i = 0; i < symtab->size; ++i)
20937 {
20938 if (symtab->data[i])
3876f04e 20939 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20940 }
20941
20942 /* Now write out the hash table. */
20943 for (i = 0; i < symtab->size; ++i)
20944 {
20945 offset_type str_off, vec_off;
20946
20947 if (symtab->data[i])
20948 {
20949 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20950 vec_off = symtab->data[i]->index_offset;
20951 }
20952 else
20953 {
20954 /* While 0 is a valid constant pool index, it is not valid
20955 to have 0 for both offsets. */
20956 str_off = 0;
20957 vec_off = 0;
20958 }
20959
20960 str_off = MAYBE_SWAP (str_off);
20961 vec_off = MAYBE_SWAP (vec_off);
20962
20963 obstack_grow (output, &str_off, sizeof (str_off));
20964 obstack_grow (output, &vec_off, sizeof (vec_off));
20965 }
20966
20967 htab_delete (str_table);
3876f04e 20968 htab_delete (symbol_hash_table);
9291a0cd
TT
20969}
20970
0a5429f6
DE
20971/* Struct to map psymtab to CU index in the index file. */
20972struct psymtab_cu_index_map
20973{
20974 struct partial_symtab *psymtab;
20975 unsigned int cu_index;
20976};
20977
20978static hashval_t
20979hash_psymtab_cu_index (const void *item)
20980{
20981 const struct psymtab_cu_index_map *map = item;
20982
20983 return htab_hash_pointer (map->psymtab);
20984}
20985
20986static int
20987eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20988{
20989 const struct psymtab_cu_index_map *lhs = item_lhs;
20990 const struct psymtab_cu_index_map *rhs = item_rhs;
20991
20992 return lhs->psymtab == rhs->psymtab;
20993}
20994
20995/* Helper struct for building the address table. */
20996struct addrmap_index_data
20997{
20998 struct objfile *objfile;
20999 struct obstack *addr_obstack;
21000 htab_t cu_index_htab;
21001
21002 /* Non-zero if the previous_* fields are valid.
21003 We can't write an entry until we see the next entry (since it is only then
21004 that we know the end of the entry). */
21005 int previous_valid;
21006 /* Index of the CU in the table of all CUs in the index file. */
21007 unsigned int previous_cu_index;
0963b4bd 21008 /* Start address of the CU. */
0a5429f6
DE
21009 CORE_ADDR previous_cu_start;
21010};
21011
21012/* Write an address entry to OBSTACK. */
b89be57b 21013
9291a0cd 21014static void
0a5429f6
DE
21015add_address_entry (struct objfile *objfile, struct obstack *obstack,
21016 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21017{
0a5429f6 21018 offset_type cu_index_to_write;
948f8e3d 21019 gdb_byte addr[8];
9291a0cd
TT
21020 CORE_ADDR baseaddr;
21021
21022 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21023
0a5429f6
DE
21024 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21025 obstack_grow (obstack, addr, 8);
21026 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21027 obstack_grow (obstack, addr, 8);
21028 cu_index_to_write = MAYBE_SWAP (cu_index);
21029 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21030}
21031
21032/* Worker function for traversing an addrmap to build the address table. */
21033
21034static int
21035add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21036{
21037 struct addrmap_index_data *data = datap;
21038 struct partial_symtab *pst = obj;
0a5429f6
DE
21039
21040 if (data->previous_valid)
21041 add_address_entry (data->objfile, data->addr_obstack,
21042 data->previous_cu_start, start_addr,
21043 data->previous_cu_index);
21044
21045 data->previous_cu_start = start_addr;
21046 if (pst != NULL)
21047 {
21048 struct psymtab_cu_index_map find_map, *map;
21049 find_map.psymtab = pst;
21050 map = htab_find (data->cu_index_htab, &find_map);
21051 gdb_assert (map != NULL);
21052 data->previous_cu_index = map->cu_index;
21053 data->previous_valid = 1;
21054 }
21055 else
21056 data->previous_valid = 0;
21057
21058 return 0;
21059}
21060
21061/* Write OBJFILE's address map to OBSTACK.
21062 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21063 in the index file. */
21064
21065static void
21066write_address_map (struct objfile *objfile, struct obstack *obstack,
21067 htab_t cu_index_htab)
21068{
21069 struct addrmap_index_data addrmap_index_data;
21070
21071 /* When writing the address table, we have to cope with the fact that
21072 the addrmap iterator only provides the start of a region; we have to
21073 wait until the next invocation to get the start of the next region. */
21074
21075 addrmap_index_data.objfile = objfile;
21076 addrmap_index_data.addr_obstack = obstack;
21077 addrmap_index_data.cu_index_htab = cu_index_htab;
21078 addrmap_index_data.previous_valid = 0;
21079
21080 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21081 &addrmap_index_data);
21082
21083 /* It's highly unlikely the last entry (end address = 0xff...ff)
21084 is valid, but we should still handle it.
21085 The end address is recorded as the start of the next region, but that
21086 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21087 anyway. */
21088 if (addrmap_index_data.previous_valid)
21089 add_address_entry (objfile, obstack,
21090 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21091 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21092}
21093
156942c7
DE
21094/* Return the symbol kind of PSYM. */
21095
21096static gdb_index_symbol_kind
21097symbol_kind (struct partial_symbol *psym)
21098{
21099 domain_enum domain = PSYMBOL_DOMAIN (psym);
21100 enum address_class aclass = PSYMBOL_CLASS (psym);
21101
21102 switch (domain)
21103 {
21104 case VAR_DOMAIN:
21105 switch (aclass)
21106 {
21107 case LOC_BLOCK:
21108 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21109 case LOC_TYPEDEF:
21110 return GDB_INDEX_SYMBOL_KIND_TYPE;
21111 case LOC_COMPUTED:
21112 case LOC_CONST_BYTES:
21113 case LOC_OPTIMIZED_OUT:
21114 case LOC_STATIC:
21115 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21116 case LOC_CONST:
21117 /* Note: It's currently impossible to recognize psyms as enum values
21118 short of reading the type info. For now punt. */
21119 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21120 default:
21121 /* There are other LOC_FOO values that one might want to classify
21122 as variables, but dwarf2read.c doesn't currently use them. */
21123 return GDB_INDEX_SYMBOL_KIND_OTHER;
21124 }
21125 case STRUCT_DOMAIN:
21126 return GDB_INDEX_SYMBOL_KIND_TYPE;
21127 default:
21128 return GDB_INDEX_SYMBOL_KIND_OTHER;
21129 }
21130}
21131
9291a0cd 21132/* Add a list of partial symbols to SYMTAB. */
b89be57b 21133
9291a0cd
TT
21134static void
21135write_psymbols (struct mapped_symtab *symtab,
987d643c 21136 htab_t psyms_seen,
9291a0cd
TT
21137 struct partial_symbol **psymp,
21138 int count,
987d643c
TT
21139 offset_type cu_index,
21140 int is_static)
9291a0cd
TT
21141{
21142 for (; count-- > 0; ++psymp)
21143 {
156942c7
DE
21144 struct partial_symbol *psym = *psymp;
21145 void **slot;
987d643c 21146
156942c7 21147 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21148 error (_("Ada is not currently supported by the index"));
987d643c 21149
987d643c 21150 /* Only add a given psymbol once. */
156942c7 21151 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21152 if (!*slot)
21153 {
156942c7
DE
21154 gdb_index_symbol_kind kind = symbol_kind (psym);
21155
21156 *slot = psym;
21157 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21158 is_static, kind, cu_index);
987d643c 21159 }
9291a0cd
TT
21160 }
21161}
21162
21163/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21164 exception if there is an error. */
b89be57b 21165
9291a0cd
TT
21166static void
21167write_obstack (FILE *file, struct obstack *obstack)
21168{
21169 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21170 file)
21171 != obstack_object_size (obstack))
21172 error (_("couldn't data write to file"));
21173}
21174
21175/* Unlink a file if the argument is not NULL. */
b89be57b 21176
9291a0cd
TT
21177static void
21178unlink_if_set (void *p)
21179{
21180 char **filename = p;
21181 if (*filename)
21182 unlink (*filename);
21183}
21184
1fd400ff
TT
21185/* A helper struct used when iterating over debug_types. */
21186struct signatured_type_index_data
21187{
21188 struct objfile *objfile;
21189 struct mapped_symtab *symtab;
21190 struct obstack *types_list;
987d643c 21191 htab_t psyms_seen;
1fd400ff
TT
21192 int cu_index;
21193};
21194
21195/* A helper function that writes a single signatured_type to an
21196 obstack. */
b89be57b 21197
1fd400ff
TT
21198static int
21199write_one_signatured_type (void **slot, void *d)
21200{
21201 struct signatured_type_index_data *info = d;
21202 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21203 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21204 gdb_byte val[8];
21205
21206 write_psymbols (info->symtab,
987d643c 21207 info->psyms_seen,
3e43a32a
MS
21208 info->objfile->global_psymbols.list
21209 + psymtab->globals_offset,
987d643c
TT
21210 psymtab->n_global_syms, info->cu_index,
21211 0);
1fd400ff 21212 write_psymbols (info->symtab,
987d643c 21213 info->psyms_seen,
3e43a32a
MS
21214 info->objfile->static_psymbols.list
21215 + psymtab->statics_offset,
987d643c
TT
21216 psymtab->n_static_syms, info->cu_index,
21217 1);
1fd400ff 21218
b64f50a1
JK
21219 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21220 entry->per_cu.offset.sect_off);
1fd400ff 21221 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21222 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21223 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21224 obstack_grow (info->types_list, val, 8);
21225 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21226 obstack_grow (info->types_list, val, 8);
21227
21228 ++info->cu_index;
21229
21230 return 1;
21231}
21232
95554aad
TT
21233/* Recurse into all "included" dependencies and write their symbols as
21234 if they appeared in this psymtab. */
21235
21236static void
21237recursively_write_psymbols (struct objfile *objfile,
21238 struct partial_symtab *psymtab,
21239 struct mapped_symtab *symtab,
21240 htab_t psyms_seen,
21241 offset_type cu_index)
21242{
21243 int i;
21244
21245 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21246 if (psymtab->dependencies[i]->user != NULL)
21247 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21248 symtab, psyms_seen, cu_index);
21249
21250 write_psymbols (symtab,
21251 psyms_seen,
21252 objfile->global_psymbols.list + psymtab->globals_offset,
21253 psymtab->n_global_syms, cu_index,
21254 0);
21255 write_psymbols (symtab,
21256 psyms_seen,
21257 objfile->static_psymbols.list + psymtab->statics_offset,
21258 psymtab->n_static_syms, cu_index,
21259 1);
21260}
21261
9291a0cd 21262/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21263
9291a0cd
TT
21264static void
21265write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21266{
21267 struct cleanup *cleanup;
21268 char *filename, *cleanup_filename;
1fd400ff
TT
21269 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21270 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21271 int i;
21272 FILE *out_file;
21273 struct mapped_symtab *symtab;
21274 offset_type val, size_of_contents, total_len;
21275 struct stat st;
987d643c 21276 htab_t psyms_seen;
0a5429f6
DE
21277 htab_t cu_index_htab;
21278 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21279
9291a0cd
TT
21280 if (dwarf2_per_objfile->using_index)
21281 error (_("Cannot use an index to create the index"));
21282
8b70b953
TT
21283 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21284 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21285
260b681b
DE
21286 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21287 return;
21288
9291a0cd 21289 if (stat (objfile->name, &st) < 0)
7e17e088 21290 perror_with_name (objfile->name);
9291a0cd
TT
21291
21292 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21293 INDEX_SUFFIX, (char *) NULL);
21294 cleanup = make_cleanup (xfree, filename);
21295
614c279d 21296 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21297 if (!out_file)
21298 error (_("Can't open `%s' for writing"), filename);
21299
21300 cleanup_filename = filename;
21301 make_cleanup (unlink_if_set, &cleanup_filename);
21302
21303 symtab = create_mapped_symtab ();
21304 make_cleanup (cleanup_mapped_symtab, symtab);
21305
21306 obstack_init (&addr_obstack);
21307 make_cleanup_obstack_free (&addr_obstack);
21308
21309 obstack_init (&cu_list);
21310 make_cleanup_obstack_free (&cu_list);
21311
1fd400ff
TT
21312 obstack_init (&types_cu_list);
21313 make_cleanup_obstack_free (&types_cu_list);
21314
987d643c
TT
21315 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21316 NULL, xcalloc, xfree);
96408a79 21317 make_cleanup_htab_delete (psyms_seen);
987d643c 21318
0a5429f6
DE
21319 /* While we're scanning CU's create a table that maps a psymtab pointer
21320 (which is what addrmap records) to its index (which is what is recorded
21321 in the index file). This will later be needed to write the address
21322 table. */
21323 cu_index_htab = htab_create_alloc (100,
21324 hash_psymtab_cu_index,
21325 eq_psymtab_cu_index,
21326 NULL, xcalloc, xfree);
96408a79 21327 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21328 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21329 xmalloc (sizeof (struct psymtab_cu_index_map)
21330 * dwarf2_per_objfile->n_comp_units);
21331 make_cleanup (xfree, psymtab_cu_index_map);
21332
21333 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21334 work here. Also, the debug_types entries do not appear in
21335 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21336 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21337 {
3e43a32a
MS
21338 struct dwarf2_per_cu_data *per_cu
21339 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21340 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21341 gdb_byte val[8];
0a5429f6
DE
21342 struct psymtab_cu_index_map *map;
21343 void **slot;
9291a0cd 21344
92fac807
JK
21345 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21346 It may be referenced from a local scope but in such case it does not
21347 need to be present in .gdb_index. */
21348 if (psymtab == NULL)
21349 continue;
21350
95554aad
TT
21351 if (psymtab->user == NULL)
21352 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21353
0a5429f6
DE
21354 map = &psymtab_cu_index_map[i];
21355 map->psymtab = psymtab;
21356 map->cu_index = i;
21357 slot = htab_find_slot (cu_index_htab, map, INSERT);
21358 gdb_assert (slot != NULL);
21359 gdb_assert (*slot == NULL);
21360 *slot = map;
9291a0cd 21361
b64f50a1
JK
21362 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21363 per_cu->offset.sect_off);
9291a0cd 21364 obstack_grow (&cu_list, val, 8);
e254ef6a 21365 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21366 obstack_grow (&cu_list, val, 8);
21367 }
21368
0a5429f6
DE
21369 /* Dump the address map. */
21370 write_address_map (objfile, &addr_obstack, cu_index_htab);
21371
1fd400ff
TT
21372 /* Write out the .debug_type entries, if any. */
21373 if (dwarf2_per_objfile->signatured_types)
21374 {
21375 struct signatured_type_index_data sig_data;
21376
21377 sig_data.objfile = objfile;
21378 sig_data.symtab = symtab;
21379 sig_data.types_list = &types_cu_list;
987d643c 21380 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21381 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21382 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21383 write_one_signatured_type, &sig_data);
21384 }
21385
156942c7
DE
21386 /* Now that we've processed all symbols we can shrink their cu_indices
21387 lists. */
21388 uniquify_cu_indices (symtab);
21389
9291a0cd
TT
21390 obstack_init (&constant_pool);
21391 make_cleanup_obstack_free (&constant_pool);
21392 obstack_init (&symtab_obstack);
21393 make_cleanup_obstack_free (&symtab_obstack);
21394 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21395
21396 obstack_init (&contents);
21397 make_cleanup_obstack_free (&contents);
1fd400ff 21398 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21399 total_len = size_of_contents;
21400
21401 /* The version number. */
796a7ff8 21402 val = MAYBE_SWAP (8);
9291a0cd
TT
21403 obstack_grow (&contents, &val, sizeof (val));
21404
21405 /* The offset of the CU list from the start of the file. */
21406 val = MAYBE_SWAP (total_len);
21407 obstack_grow (&contents, &val, sizeof (val));
21408 total_len += obstack_object_size (&cu_list);
21409
1fd400ff
TT
21410 /* The offset of the types CU list from the start of the file. */
21411 val = MAYBE_SWAP (total_len);
21412 obstack_grow (&contents, &val, sizeof (val));
21413 total_len += obstack_object_size (&types_cu_list);
21414
9291a0cd
TT
21415 /* The offset of the address table from the start of the file. */
21416 val = MAYBE_SWAP (total_len);
21417 obstack_grow (&contents, &val, sizeof (val));
21418 total_len += obstack_object_size (&addr_obstack);
21419
21420 /* The offset of the symbol table from the start of the file. */
21421 val = MAYBE_SWAP (total_len);
21422 obstack_grow (&contents, &val, sizeof (val));
21423 total_len += obstack_object_size (&symtab_obstack);
21424
21425 /* The offset of the constant pool from the start of the file. */
21426 val = MAYBE_SWAP (total_len);
21427 obstack_grow (&contents, &val, sizeof (val));
21428 total_len += obstack_object_size (&constant_pool);
21429
21430 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21431
21432 write_obstack (out_file, &contents);
21433 write_obstack (out_file, &cu_list);
1fd400ff 21434 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21435 write_obstack (out_file, &addr_obstack);
21436 write_obstack (out_file, &symtab_obstack);
21437 write_obstack (out_file, &constant_pool);
21438
21439 fclose (out_file);
21440
21441 /* We want to keep the file, so we set cleanup_filename to NULL
21442 here. See unlink_if_set. */
21443 cleanup_filename = NULL;
21444
21445 do_cleanups (cleanup);
21446}
21447
90476074
TT
21448/* Implementation of the `save gdb-index' command.
21449
21450 Note that the file format used by this command is documented in the
21451 GDB manual. Any changes here must be documented there. */
11570e71 21452
9291a0cd
TT
21453static void
21454save_gdb_index_command (char *arg, int from_tty)
21455{
21456 struct objfile *objfile;
21457
21458 if (!arg || !*arg)
96d19272 21459 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21460
21461 ALL_OBJFILES (objfile)
21462 {
21463 struct stat st;
21464
21465 /* If the objfile does not correspond to an actual file, skip it. */
21466 if (stat (objfile->name, &st) < 0)
21467 continue;
21468
21469 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21470 if (dwarf2_per_objfile)
21471 {
21472 volatile struct gdb_exception except;
21473
21474 TRY_CATCH (except, RETURN_MASK_ERROR)
21475 {
21476 write_psymtabs_to_index (objfile, arg);
21477 }
21478 if (except.reason < 0)
21479 exception_fprintf (gdb_stderr, except,
21480 _("Error while writing index for `%s': "),
21481 objfile->name);
21482 }
21483 }
dce234bc
PP
21484}
21485
9291a0cd
TT
21486\f
21487
9eae7c52
TT
21488int dwarf2_always_disassemble;
21489
21490static void
21491show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21492 struct cmd_list_element *c, const char *value)
21493{
3e43a32a
MS
21494 fprintf_filtered (file,
21495 _("Whether to always disassemble "
21496 "DWARF expressions is %s.\n"),
9eae7c52
TT
21497 value);
21498}
21499
900e11f9
JK
21500static void
21501show_check_physname (struct ui_file *file, int from_tty,
21502 struct cmd_list_element *c, const char *value)
21503{
21504 fprintf_filtered (file,
21505 _("Whether to check \"physname\" is %s.\n"),
21506 value);
21507}
21508
6502dd73
DJ
21509void _initialize_dwarf2_read (void);
21510
21511void
21512_initialize_dwarf2_read (void)
21513{
96d19272
JK
21514 struct cmd_list_element *c;
21515
dce234bc 21516 dwarf2_objfile_data_key
c1bd65d0 21517 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21518
1bedd215
AC
21519 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21520Set DWARF 2 specific variables.\n\
21521Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21522 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21523 0/*allow-unknown*/, &maintenance_set_cmdlist);
21524
1bedd215
AC
21525 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21526Show DWARF 2 specific variables\n\
21527Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21528 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21529 0/*allow-unknown*/, &maintenance_show_cmdlist);
21530
21531 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21532 &dwarf2_max_cache_age, _("\
21533Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21534Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21535A higher limit means that cached compilation units will be stored\n\
21536in memory longer, and more total memory will be used. Zero disables\n\
21537caching, which can slow down startup."),
2c5b56ce 21538 NULL,
920d2a44 21539 show_dwarf2_max_cache_age,
2c5b56ce 21540 &set_dwarf2_cmdlist,
ae038cb0 21541 &show_dwarf2_cmdlist);
d97bc12b 21542
9eae7c52
TT
21543 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21544 &dwarf2_always_disassemble, _("\
21545Set whether `info address' always disassembles DWARF expressions."), _("\
21546Show whether `info address' always disassembles DWARF expressions."), _("\
21547When enabled, DWARF expressions are always printed in an assembly-like\n\
21548syntax. When disabled, expressions will be printed in a more\n\
21549conversational style, when possible."),
21550 NULL,
21551 show_dwarf2_always_disassemble,
21552 &set_dwarf2_cmdlist,
21553 &show_dwarf2_cmdlist);
21554
45cfd468
DE
21555 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21556Set debugging of the dwarf2 reader."), _("\
21557Show debugging of the dwarf2 reader."), _("\
21558When enabled, debugging messages are printed during dwarf2 reading\n\
21559and symtab expansion."),
21560 NULL,
21561 NULL,
21562 &setdebuglist, &showdebuglist);
21563
ccce17b0 21564 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21565Set debugging of the dwarf2 DIE reader."), _("\
21566Show debugging of the dwarf2 DIE reader."), _("\
21567When enabled (non-zero), DIEs are dumped after they are read in.\n\
21568The value is the maximum depth to print."),
ccce17b0
YQ
21569 NULL,
21570 NULL,
21571 &setdebuglist, &showdebuglist);
9291a0cd 21572
900e11f9
JK
21573 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21574Set cross-checking of \"physname\" code against demangler."), _("\
21575Show cross-checking of \"physname\" code against demangler."), _("\
21576When enabled, GDB's internal \"physname\" code is checked against\n\
21577the demangler."),
21578 NULL, show_check_physname,
21579 &setdebuglist, &showdebuglist);
21580
e615022a
DE
21581 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21582 no_class, &use_deprecated_index_sections, _("\
21583Set whether to use deprecated gdb_index sections."), _("\
21584Show whether to use deprecated gdb_index sections."), _("\
21585When enabled, deprecated .gdb_index sections are used anyway.\n\
21586Normally they are ignored either because of a missing feature or\n\
21587performance issue.\n\
21588Warning: This option must be enabled before gdb reads the file."),
21589 NULL,
21590 NULL,
21591 &setlist, &showlist);
21592
96d19272 21593 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21594 _("\
fc1a9d6e 21595Save a gdb-index file.\n\
11570e71 21596Usage: save gdb-index DIRECTORY"),
96d19272
JK
21597 &save_cmdlist);
21598 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21599
21600 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21601 &dwarf2_locexpr_funcs);
21602 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21603 &dwarf2_loclist_funcs);
21604
21605 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21606 &dwarf2_block_frame_base_locexpr_funcs);
21607 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21608 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21609}
This page took 3.261777 seconds and 4 git commands to generate.