daily update
[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
442 /* Backchain to our per_cu entry if the tree has been built. */
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
3019eac3
DE
561 /* The section this CU/TU lives in.
562 If the DIE refers to a DWO file, this is always the original die,
563 not the DWO file. */
8a0459fd 564 struct dwarf2_section_info *section;
348e048f 565
17ea53c3
JK
566 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
567 of the CU cache it gets reset to NULL again. */
ae038cb0 568 struct dwarf2_cu *cu;
1c379e20 569
9cdd5dbd
DE
570 /* The corresponding objfile.
571 Normally we can get the objfile from dwarf2_per_objfile.
572 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
573 struct objfile *objfile;
574
575 /* When using partial symbol tables, the 'psymtab' field is active.
576 Otherwise the 'quick' field is active. */
577 union
578 {
579 /* The partial symbol table associated with this compilation unit,
95554aad 580 or NULL for unread partial units. */
9291a0cd
TT
581 struct partial_symtab *psymtab;
582
583 /* Data needed by the "quick" functions. */
584 struct dwarf2_per_cu_quick_data *quick;
585 } v;
95554aad 586
796a7ff8
DE
587 /* The CUs we import using DW_TAG_imported_unit. This is filled in
588 while reading psymtabs, used to compute the psymtab dependencies,
589 and then cleared. Then it is filled in again while reading full
590 symbols, and only deleted when the objfile is destroyed.
591
592 This is also used to work around a difference between the way gold
593 generates .gdb_index version <=7 and the way gdb does. Arguably this
594 is a gold bug. For symbols coming from TUs, gold records in the index
595 the CU that includes the TU instead of the TU itself. This breaks
596 dw2_lookup_symbol: It assumes that if the index says symbol X lives
597 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
598 will find X. Alas TUs live in their own symtab, so after expanding CU Y
599 we need to look in TU Z to find X. Fortunately, this is akin to
600 DW_TAG_imported_unit, so we just use the same mechanism: For
601 .gdb_index version <=7 this also records the TUs that the CU referred
602 to. Concurrently with this change gdb was modified to emit version 8
603 indices so we only pay a price for gold generated indices. */
604 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
605};
606
348e048f
DE
607/* Entry in the signatured_types hash table. */
608
609struct signatured_type
610{
42e7ad6c 611 /* The "per_cu" object of this type.
ac9ec31b 612 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
613 N.B.: This is the first member so that it's easy to convert pointers
614 between them. */
615 struct dwarf2_per_cu_data per_cu;
616
3019eac3 617 /* The type's signature. */
348e048f
DE
618 ULONGEST signature;
619
3019eac3 620 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
621 If this TU is a DWO stub and the definition lives in a DWO file
622 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
623 cu_offset type_offset_in_tu;
624
625 /* Offset in the section of the type's DIE.
626 If the definition lives in a DWO file, this is the offset in the
627 .debug_types.dwo section.
628 The value is zero until the actual value is known.
629 Zero is otherwise not a valid section offset. */
630 sect_offset type_offset_in_section;
0186c6a7
DE
631
632 /* Type units are grouped by their DW_AT_stmt_list entry so that they
633 can share them. This points to the containing symtab. */
634 struct type_unit_group *type_unit_group;
ac9ec31b
DE
635
636 /* The type.
637 The first time we encounter this type we fully read it in and install it
638 in the symbol tables. Subsequent times we only need the type. */
639 struct type *type;
a2ce51a0
DE
640
641 /* Containing DWO unit.
642 This field is valid iff per_cu.reading_dwo_directly. */
643 struct dwo_unit *dwo_unit;
348e048f
DE
644};
645
0186c6a7
DE
646typedef struct signatured_type *sig_type_ptr;
647DEF_VEC_P (sig_type_ptr);
648
094b34ac
DE
649/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
650 This includes type_unit_group and quick_file_names. */
651
652struct stmt_list_hash
653{
654 /* The DWO unit this table is from or NULL if there is none. */
655 struct dwo_unit *dwo_unit;
656
657 /* Offset in .debug_line or .debug_line.dwo. */
658 sect_offset line_offset;
659};
660
f4dc4d17
DE
661/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
662 an object of this type. */
663
664struct type_unit_group
665{
0186c6a7 666 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
667 To simplify things we create an artificial CU that "includes" all the
668 type units using this stmt_list so that the rest of the code still has
669 a "per_cu" handle on the symtab.
670 This PER_CU is recognized by having no section. */
8a0459fd 671#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
672 struct dwarf2_per_cu_data per_cu;
673
0186c6a7
DE
674 /* The TUs that share this DW_AT_stmt_list entry.
675 This is added to while parsing type units to build partial symtabs,
676 and is deleted afterwards and not used again. */
677 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
678
679 /* The primary symtab.
094b34ac
DE
680 Type units in a group needn't all be defined in the same source file,
681 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
682 struct symtab *primary_symtab;
683
094b34ac
DE
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
f4dc4d17
DE
686
687 /* The number of symtabs from the line header.
688 The value here must match line_header.num_file_names. */
689 unsigned int num_symtabs;
690
691 /* The symbol tables for this TU (obtained from the files listed in
692 DW_AT_stmt_list).
693 WARNING: The order of entries here must match the order of entries
694 in the line header. After the first TU using this type_unit_group, the
695 line header for the subsequent TUs is recreated from this. This is done
696 because we need to use the same symtabs for each TU using the same
697 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
698 there's no guarantee the line header doesn't have duplicate entries. */
699 struct symtab **symtabs;
700};
701
80626a55 702/* These sections are what may appear in a DWO file. */
3019eac3
DE
703
704struct dwo_sections
705{
706 struct dwarf2_section_info abbrev;
3019eac3
DE
707 struct dwarf2_section_info line;
708 struct dwarf2_section_info loc;
09262596
DE
709 struct dwarf2_section_info macinfo;
710 struct dwarf2_section_info macro;
3019eac3
DE
711 struct dwarf2_section_info str;
712 struct dwarf2_section_info str_offsets;
80626a55
DE
713 /* In the case of a virtual DWO file, these two are unused. */
714 struct dwarf2_section_info info;
3019eac3
DE
715 VEC (dwarf2_section_info_def) *types;
716};
717
c88ee1f0 718/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
719
720struct dwo_unit
721{
722 /* Backlink to the containing struct dwo_file. */
723 struct dwo_file *dwo_file;
724
725 /* The "id" that distinguishes this CU/TU.
726 .debug_info calls this "dwo_id", .debug_types calls this "signature".
727 Since signatures came first, we stick with it for consistency. */
728 ULONGEST signature;
729
730 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 731 struct dwarf2_section_info *section;
3019eac3
DE
732
733 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
734 sect_offset offset;
735 unsigned int length;
736
737 /* For types, offset in the type's DIE of the type defined by this TU. */
738 cu_offset type_offset_in_tu;
739};
740
80626a55
DE
741/* Data for one DWO file.
742 This includes virtual DWO files that have been packaged into a
743 DWP file. */
3019eac3
DE
744
745struct dwo_file
746{
0ac5b59e 747 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
748 For virtual DWO files the name is constructed from the section offsets
749 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
750 from related CU+TUs. */
0ac5b59e
DE
751 const char *dwo_name;
752
753 /* The DW_AT_comp_dir attribute. */
754 const char *comp_dir;
3019eac3 755
80626a55
DE
756 /* The bfd, when the file is open. Otherwise this is NULL.
757 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
758 bfd *dbfd;
3019eac3
DE
759
760 /* Section info for this file. */
761 struct dwo_sections sections;
762
19c3d4c9
DE
763 /* The CU in the file.
764 We only support one because having more than one requires hacking the
765 dwo_name of each to match, which is highly unlikely to happen.
766 Doing this means all TUs can share comp_dir: We also assume that
767 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
768 struct dwo_unit *cu;
3019eac3
DE
769
770 /* Table of TUs in the file.
771 Each element is a struct dwo_unit. */
772 htab_t tus;
773};
774
80626a55
DE
775/* These sections are what may appear in a DWP file. */
776
777struct dwp_sections
778{
779 struct dwarf2_section_info str;
780 struct dwarf2_section_info cu_index;
781 struct dwarf2_section_info tu_index;
782 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
783 by section number. We don't need to record them here. */
784};
785
786/* These sections are what may appear in a virtual DWO file. */
787
788struct virtual_dwo_sections
789{
790 struct dwarf2_section_info abbrev;
791 struct dwarf2_section_info line;
792 struct dwarf2_section_info loc;
793 struct dwarf2_section_info macinfo;
794 struct dwarf2_section_info macro;
795 struct dwarf2_section_info str_offsets;
796 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 797 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
798 struct dwarf2_section_info info_or_types;
799};
800
801/* Contents of DWP hash tables. */
802
803struct dwp_hash_table
804{
805 uint32_t nr_units, nr_slots;
806 const gdb_byte *hash_table, *unit_table, *section_pool;
807};
808
809/* Data for one DWP file. */
810
811struct dwp_file
812{
813 /* Name of the file. */
814 const char *name;
815
93417882 816 /* The bfd. */
80626a55
DE
817 bfd *dbfd;
818
819 /* Section info for this file. */
820 struct dwp_sections sections;
821
822 /* Table of CUs in the file. */
823 const struct dwp_hash_table *cus;
824
825 /* Table of TUs in the file. */
826 const struct dwp_hash_table *tus;
827
828 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
829 htab_t loaded_cutus;
830
831 /* Table to map ELF section numbers to their sections. */
832 unsigned int num_sections;
833 asection **elf_sections;
834};
835
36586728
TT
836/* This represents a '.dwz' file. */
837
838struct dwz_file
839{
840 /* A dwz file can only contain a few sections. */
841 struct dwarf2_section_info abbrev;
842 struct dwarf2_section_info info;
843 struct dwarf2_section_info str;
844 struct dwarf2_section_info line;
845 struct dwarf2_section_info macro;
2ec9a5e0 846 struct dwarf2_section_info gdb_index;
36586728
TT
847
848 /* The dwz's BFD. */
849 bfd *dwz_bfd;
850};
851
0963b4bd
MS
852/* Struct used to pass misc. parameters to read_die_and_children, et
853 al. which are used for both .debug_info and .debug_types dies.
854 All parameters here are unchanging for the life of the call. This
dee91e82 855 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
856
857struct die_reader_specs
858{
dee91e82 859 /* die_section->asection->owner. */
93311388
DE
860 bfd* abfd;
861
862 /* The CU of the DIE we are parsing. */
863 struct dwarf2_cu *cu;
864
80626a55 865 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
866 struct dwo_file *dwo_file;
867
dee91e82 868 /* The section the die comes from.
3019eac3 869 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
870 struct dwarf2_section_info *die_section;
871
872 /* die_section->buffer. */
d521ce57 873 const gdb_byte *buffer;
f664829e
DE
874
875 /* The end of the buffer. */
876 const gdb_byte *buffer_end;
a2ce51a0
DE
877
878 /* The value of the DW_AT_comp_dir attribute. */
879 const char *comp_dir;
93311388
DE
880};
881
fd820528 882/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 883typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 884 const gdb_byte *info_ptr,
dee91e82
DE
885 struct die_info *comp_unit_die,
886 int has_children,
887 void *data);
888
debd256d
JB
889/* The line number information for a compilation unit (found in the
890 .debug_line section) begins with a "statement program header",
891 which contains the following information. */
892struct line_header
893{
894 unsigned int total_length;
895 unsigned short version;
896 unsigned int header_length;
897 unsigned char minimum_instruction_length;
2dc7f7b3 898 unsigned char maximum_ops_per_instruction;
debd256d
JB
899 unsigned char default_is_stmt;
900 int line_base;
901 unsigned char line_range;
902 unsigned char opcode_base;
903
904 /* standard_opcode_lengths[i] is the number of operands for the
905 standard opcode whose value is i. This means that
906 standard_opcode_lengths[0] is unused, and the last meaningful
907 element is standard_opcode_lengths[opcode_base - 1]. */
908 unsigned char *standard_opcode_lengths;
909
910 /* The include_directories table. NOTE! These strings are not
911 allocated with xmalloc; instead, they are pointers into
912 debug_line_buffer. If you try to free them, `free' will get
913 indigestion. */
914 unsigned int num_include_dirs, include_dirs_size;
d521ce57 915 const char **include_dirs;
debd256d
JB
916
917 /* The file_names table. NOTE! These strings are not allocated
918 with xmalloc; instead, they are pointers into debug_line_buffer.
919 Don't try to free them directly. */
920 unsigned int num_file_names, file_names_size;
921 struct file_entry
c906108c 922 {
d521ce57 923 const char *name;
debd256d
JB
924 unsigned int dir_index;
925 unsigned int mod_time;
926 unsigned int length;
aaa75496 927 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 928 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
929 } *file_names;
930
931 /* The start and end of the statement program following this
6502dd73 932 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 933 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 934};
c906108c
SS
935
936/* When we construct a partial symbol table entry we only
0963b4bd 937 need this much information. */
c906108c
SS
938struct partial_die_info
939 {
72bf9492 940 /* Offset of this DIE. */
b64f50a1 941 sect_offset offset;
72bf9492
DJ
942
943 /* DWARF-2 tag for this DIE. */
944 ENUM_BITFIELD(dwarf_tag) tag : 16;
945
72bf9492
DJ
946 /* Assorted flags describing the data found in this DIE. */
947 unsigned int has_children : 1;
948 unsigned int is_external : 1;
949 unsigned int is_declaration : 1;
950 unsigned int has_type : 1;
951 unsigned int has_specification : 1;
952 unsigned int has_pc_info : 1;
481860b3 953 unsigned int may_be_inlined : 1;
72bf9492
DJ
954
955 /* Flag set if the SCOPE field of this structure has been
956 computed. */
957 unsigned int scope_set : 1;
958
fa4028e9
JB
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
961
98bfdba5
PA
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
abc72ce4
DE
965 /* Flag set if fixup_partial_die has been called on this die. */
966 unsigned int fixup_called : 1;
967
36586728
TT
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
72bf9492 974 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 975 sometimes a default name for unnamed DIEs. */
15d034d0 976 const char *name;
72bf9492 977
abc72ce4
DE
978 /* The linkage name, if present. */
979 const char *linkage_name;
980
72bf9492
DJ
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
15d034d0 984 const char *scope;
72bf9492 985
95554aad
TT
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset offset;
994 } d;
72bf9492
DJ
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
997 CORE_ADDR lowpc;
998 CORE_ADDR highpc;
72bf9492 999
93311388 1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1001 DW_AT_sibling, if any. */
abc72ce4
DE
1002 /* NOTE: This member isn't strictly necessary, read_partial_die could
1003 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1004 const gdb_byte *sibling;
72bf9492
DJ
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
b64f50a1 1009 sect_offset spec_offset;
72bf9492
DJ
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1014 };
1015
0963b4bd 1016/* This data structure holds the information of an abbrev. */
c906108c
SS
1017struct abbrev_info
1018 {
1019 unsigned int number; /* number identifying abbrev */
1020 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1021 unsigned short has_children; /* boolean */
1022 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1023 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1024 struct abbrev_info *next; /* next in chain */
1025 };
1026
1027struct attr_abbrev
1028 {
9d25dd43
DE
1029 ENUM_BITFIELD(dwarf_attribute) name : 16;
1030 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1031 };
1032
433df2d4
DE
1033/* Size of abbrev_table.abbrev_hash_table. */
1034#define ABBREV_HASH_SIZE 121
1035
1036/* Top level data structure to contain an abbreviation table. */
1037
1038struct abbrev_table
1039{
f4dc4d17
DE
1040 /* Where the abbrev table came from.
1041 This is used as a sanity check when the table is used. */
433df2d4
DE
1042 sect_offset offset;
1043
1044 /* Storage for the abbrev table. */
1045 struct obstack abbrev_obstack;
1046
1047 /* Hash table of abbrevs.
1048 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1049 It could be statically allocated, but the previous code didn't so we
1050 don't either. */
1051 struct abbrev_info **abbrevs;
1052};
1053
0963b4bd 1054/* Attributes have a name and a value. */
b60c80d6
DJ
1055struct attribute
1056 {
9d25dd43 1057 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1058 ENUM_BITFIELD(dwarf_form) form : 15;
1059
1060 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1061 field should be in u.str (existing only for DW_STRING) but it is kept
1062 here for better struct attribute alignment. */
1063 unsigned int string_is_canonical : 1;
1064
b60c80d6
DJ
1065 union
1066 {
15d034d0 1067 const char *str;
b60c80d6 1068 struct dwarf_block *blk;
43bbcdc2
PH
1069 ULONGEST unsnd;
1070 LONGEST snd;
b60c80d6 1071 CORE_ADDR addr;
ac9ec31b 1072 ULONGEST signature;
b60c80d6
DJ
1073 }
1074 u;
1075 };
1076
0963b4bd 1077/* This data structure holds a complete die structure. */
c906108c
SS
1078struct die_info
1079 {
76815b17
DE
1080 /* DWARF-2 tag for this DIE. */
1081 ENUM_BITFIELD(dwarf_tag) tag : 16;
1082
1083 /* Number of attributes */
98bfdba5
PA
1084 unsigned char num_attrs;
1085
1086 /* True if we're presently building the full type name for the
1087 type derived from this DIE. */
1088 unsigned char building_fullname : 1;
76815b17
DE
1089
1090 /* Abbrev number */
1091 unsigned int abbrev;
1092
93311388 1093 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1094 sect_offset offset;
78ba4af6
JB
1095
1096 /* The dies in a compilation unit form an n-ary tree. PARENT
1097 points to this die's parent; CHILD points to the first child of
1098 this node; and all the children of a given node are chained
4950bc1c 1099 together via their SIBLING fields. */
639d11d3
DC
1100 struct die_info *child; /* Its first child, if any. */
1101 struct die_info *sibling; /* Its next sibling, if any. */
1102 struct die_info *parent; /* Its parent, if any. */
c906108c 1103
b60c80d6
DJ
1104 /* An array of attributes, with NUM_ATTRS elements. There may be
1105 zero, but it's not common and zero-sized arrays are not
1106 sufficiently portable C. */
1107 struct attribute attrs[1];
c906108c
SS
1108 };
1109
0963b4bd 1110/* Get at parts of an attribute structure. */
c906108c
SS
1111
1112#define DW_STRING(attr) ((attr)->u.str)
8285870a 1113#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1114#define DW_UNSND(attr) ((attr)->u.unsnd)
1115#define DW_BLOCK(attr) ((attr)->u.blk)
1116#define DW_SND(attr) ((attr)->u.snd)
1117#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1118#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1119
0963b4bd 1120/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1121struct dwarf_block
1122 {
56eb65bd 1123 size_t size;
1d6edc3c
JK
1124
1125 /* Valid only if SIZE is not zero. */
d521ce57 1126 const gdb_byte *data;
c906108c
SS
1127 };
1128
c906108c
SS
1129#ifndef ATTR_ALLOC_CHUNK
1130#define ATTR_ALLOC_CHUNK 4
1131#endif
1132
c906108c
SS
1133/* Allocate fields for structs, unions and enums in this size. */
1134#ifndef DW_FIELD_ALLOC_CHUNK
1135#define DW_FIELD_ALLOC_CHUNK 4
1136#endif
1137
c906108c
SS
1138/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1139 but this would require a corresponding change in unpack_field_as_long
1140 and friends. */
1141static int bits_per_byte = 8;
1142
1143/* The routines that read and process dies for a C struct or C++ class
1144 pass lists of data member fields and lists of member function fields
1145 in an instance of a field_info structure, as defined below. */
1146struct field_info
c5aa993b 1147 {
0963b4bd 1148 /* List of data member and baseclasses fields. */
c5aa993b
JM
1149 struct nextfield
1150 {
1151 struct nextfield *next;
1152 int accessibility;
1153 int virtuality;
1154 struct field field;
1155 }
7d0ccb61 1156 *fields, *baseclasses;
c906108c 1157
7d0ccb61 1158 /* Number of fields (including baseclasses). */
c5aa993b 1159 int nfields;
c906108c 1160
c5aa993b
JM
1161 /* Number of baseclasses. */
1162 int nbaseclasses;
c906108c 1163
c5aa993b
JM
1164 /* Set if the accesibility of one of the fields is not public. */
1165 int non_public_fields;
c906108c 1166
c5aa993b
JM
1167 /* Member function fields array, entries are allocated in the order they
1168 are encountered in the object file. */
1169 struct nextfnfield
1170 {
1171 struct nextfnfield *next;
1172 struct fn_field fnfield;
1173 }
1174 *fnfields;
c906108c 1175
c5aa993b
JM
1176 /* Member function fieldlist array, contains name of possibly overloaded
1177 member function, number of overloaded member functions and a pointer
1178 to the head of the member function field chain. */
1179 struct fnfieldlist
1180 {
15d034d0 1181 const char *name;
c5aa993b
JM
1182 int length;
1183 struct nextfnfield *head;
1184 }
1185 *fnfieldlists;
c906108c 1186
c5aa993b
JM
1187 /* Number of entries in the fnfieldlists array. */
1188 int nfnfields;
98751a41
JK
1189
1190 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1191 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1192 struct typedef_field_list
1193 {
1194 struct typedef_field field;
1195 struct typedef_field_list *next;
1196 }
1197 *typedef_field_list;
1198 unsigned typedef_field_list_count;
c5aa993b 1199 };
c906108c 1200
10b3939b
DJ
1201/* One item on the queue of compilation units to read in full symbols
1202 for. */
1203struct dwarf2_queue_item
1204{
1205 struct dwarf2_per_cu_data *per_cu;
95554aad 1206 enum language pretend_language;
10b3939b
DJ
1207 struct dwarf2_queue_item *next;
1208};
1209
1210/* The current queue. */
1211static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1212
ae038cb0
DJ
1213/* Loaded secondary compilation units are kept in memory until they
1214 have not been referenced for the processing of this many
1215 compilation units. Set this to zero to disable caching. Cache
1216 sizes of up to at least twenty will improve startup time for
1217 typical inter-CU-reference binaries, at an obvious memory cost. */
1218static int dwarf2_max_cache_age = 5;
920d2a44
AC
1219static void
1220show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1221 struct cmd_list_element *c, const char *value)
1222{
3e43a32a
MS
1223 fprintf_filtered (file, _("The upper bound on the age of cached "
1224 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1225 value);
1226}
1227
ae038cb0 1228
0963b4bd 1229/* Various complaints about symbol reading that don't abort the process. */
c906108c 1230
4d3c2250
KB
1231static void
1232dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1233{
4d3c2250 1234 complaint (&symfile_complaints,
e2e0b3e5 1235 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1236}
1237
25e43795
DJ
1238static void
1239dwarf2_debug_line_missing_file_complaint (void)
1240{
1241 complaint (&symfile_complaints,
1242 _(".debug_line section has line data without a file"));
1243}
1244
59205f5a
JB
1245static void
1246dwarf2_debug_line_missing_end_sequence_complaint (void)
1247{
1248 complaint (&symfile_complaints,
3e43a32a
MS
1249 _(".debug_line section has line "
1250 "program sequence without an end"));
59205f5a
JB
1251}
1252
4d3c2250
KB
1253static void
1254dwarf2_complex_location_expr_complaint (void)
2e276125 1255{
e2e0b3e5 1256 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1257}
1258
4d3c2250
KB
1259static void
1260dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1261 int arg3)
2e276125 1262{
4d3c2250 1263 complaint (&symfile_complaints,
3e43a32a
MS
1264 _("const value length mismatch for '%s', got %d, expected %d"),
1265 arg1, arg2, arg3);
4d3c2250
KB
1266}
1267
1268static void
f664829e 1269dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1270{
4d3c2250 1271 complaint (&symfile_complaints,
f664829e
DE
1272 _("debug info runs off end of %s section"
1273 " [in module %s]"),
1274 section->asection->name,
1275 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1276}
1277
1278static void
1279dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1280{
4d3c2250 1281 complaint (&symfile_complaints,
3e43a32a
MS
1282 _("macro debug info contains a "
1283 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1284 arg1);
1285}
1286
1287static void
1288dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1289{
4d3c2250 1290 complaint (&symfile_complaints,
3e43a32a
MS
1291 _("invalid attribute class or form for '%s' in '%s'"),
1292 arg1, arg2);
4d3c2250 1293}
c906108c 1294
c906108c
SS
1295/* local function prototypes */
1296
4efb68b1 1297static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1298
918dd910
JK
1299static void dwarf2_find_base_address (struct die_info *die,
1300 struct dwarf2_cu *cu);
1301
0018ea6f
DE
1302static struct partial_symtab *create_partial_symtab
1303 (struct dwarf2_per_cu_data *per_cu, const char *name);
1304
c67a9c90 1305static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1306
72bf9492
DJ
1307static void scan_partial_symbols (struct partial_die_info *,
1308 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1309 int, struct dwarf2_cu *);
c906108c 1310
72bf9492
DJ
1311static void add_partial_symbol (struct partial_die_info *,
1312 struct dwarf2_cu *);
63d06c5c 1313
72bf9492
DJ
1314static void add_partial_namespace (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1316 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1317
5d7cb8df
JK
1318static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1319 CORE_ADDR *highpc, int need_pc,
1320 struct dwarf2_cu *cu);
1321
72bf9492
DJ
1322static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1323 struct dwarf2_cu *cu);
91c24f0a 1324
bc30ff58
JB
1325static void add_partial_subprogram (struct partial_die_info *pdi,
1326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1327 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1328
257e7a09
YQ
1329static void dwarf2_read_symtab (struct partial_symtab *,
1330 struct objfile *);
c906108c 1331
a14ed312 1332static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1333
433df2d4
DE
1334static struct abbrev_info *abbrev_table_lookup_abbrev
1335 (const struct abbrev_table *, unsigned int);
1336
1337static struct abbrev_table *abbrev_table_read_table
1338 (struct dwarf2_section_info *, sect_offset);
1339
1340static void abbrev_table_free (struct abbrev_table *);
1341
f4dc4d17
DE
1342static void abbrev_table_free_cleanup (void *);
1343
dee91e82
DE
1344static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1345 struct dwarf2_section_info *);
c906108c 1346
f3dd6933 1347static void dwarf2_free_abbrev_table (void *);
c906108c 1348
d521ce57 1349static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1350
dee91e82 1351static struct partial_die_info *load_partial_dies
d521ce57 1352 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1353
d521ce57
TT
1354static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1355 struct partial_die_info *,
1356 struct abbrev_info *,
1357 unsigned int,
1358 const gdb_byte *);
c906108c 1359
36586728 1360static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1361 struct dwarf2_cu *);
72bf9492
DJ
1362
1363static void fixup_partial_die (struct partial_die_info *,
1364 struct dwarf2_cu *);
1365
d521ce57
TT
1366static const gdb_byte *read_attribute (const struct die_reader_specs *,
1367 struct attribute *, struct attr_abbrev *,
1368 const gdb_byte *);
a8329558 1369
a1855c1d 1370static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1371
a1855c1d 1372static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1373
a1855c1d 1374static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1375
a1855c1d 1376static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1377
a1855c1d 1378static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1379
d521ce57 1380static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1381 unsigned int *);
c906108c 1382
d521ce57 1383static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1384
1385static LONGEST read_checked_initial_length_and_offset
d521ce57 1386 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1387 unsigned int *, unsigned int *);
613e1657 1388
d521ce57
TT
1389static LONGEST read_offset (bfd *, const gdb_byte *,
1390 const struct comp_unit_head *,
c764a876
DE
1391 unsigned int *);
1392
d521ce57 1393static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1394
f4dc4d17
DE
1395static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1396 sect_offset);
1397
d521ce57 1398static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1399
d521ce57 1400static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1401
d521ce57
TT
1402static const char *read_indirect_string (bfd *, const gdb_byte *,
1403 const struct comp_unit_head *,
1404 unsigned int *);
4bdf3d34 1405
d521ce57 1406static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1407
d521ce57 1408static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1409
d521ce57 1410static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1411
d521ce57
TT
1412static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1413 const gdb_byte *,
3019eac3
DE
1414 unsigned int *);
1415
d521ce57
TT
1416static const char *read_str_index (const struct die_reader_specs *reader,
1417 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1418
e142c38c 1419static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1420
e142c38c
DJ
1421static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1422 struct dwarf2_cu *);
c906108c 1423
348e048f 1424static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1425 unsigned int);
348e048f 1426
05cf31d1
JB
1427static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1428 struct dwarf2_cu *cu);
1429
e142c38c 1430static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1431
e142c38c 1432static struct die_info *die_specification (struct die_info *die,
f2f0e013 1433 struct dwarf2_cu **);
63d06c5c 1434
debd256d
JB
1435static void free_line_header (struct line_header *lh);
1436
3019eac3
DE
1437static struct line_header *dwarf_decode_line_header (unsigned int offset,
1438 struct dwarf2_cu *cu);
debd256d 1439
f3f5162e
DE
1440static void dwarf_decode_lines (struct line_header *, const char *,
1441 struct dwarf2_cu *, struct partial_symtab *,
1442 int);
c906108c 1443
d521ce57 1444static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1445
f4dc4d17 1446static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1447 const char *, const char *, CORE_ADDR);
f4dc4d17 1448
a14ed312 1449static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1450 struct dwarf2_cu *);
c906108c 1451
34eaf542
TT
1452static struct symbol *new_symbol_full (struct die_info *, struct type *,
1453 struct dwarf2_cu *, struct symbol *);
1454
ff39bb5e 1455static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1456 struct dwarf2_cu *);
c906108c 1457
ff39bb5e 1458static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1459 struct type *type,
1460 const char *name,
1461 struct obstack *obstack,
12df843f 1462 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1463 const gdb_byte **bytes,
98bfdba5 1464 struct dwarf2_locexpr_baton **baton);
2df3850c 1465
e7c27a73 1466static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1467
b4ba55a1
JB
1468static int need_gnat_info (struct dwarf2_cu *);
1469
3e43a32a
MS
1470static struct type *die_descriptive_type (struct die_info *,
1471 struct dwarf2_cu *);
b4ba55a1
JB
1472
1473static void set_descriptive_type (struct type *, struct die_info *,
1474 struct dwarf2_cu *);
1475
e7c27a73
DJ
1476static struct type *die_containing_type (struct die_info *,
1477 struct dwarf2_cu *);
c906108c 1478
ff39bb5e 1479static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1480 struct dwarf2_cu *);
c906108c 1481
f792889a 1482static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1483
673bfd45
DE
1484static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1485
0d5cff50 1486static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1487
6e70227d 1488static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1489 const char *suffix, int physname,
1490 struct dwarf2_cu *cu);
63d06c5c 1491
e7c27a73 1492static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1493
348e048f
DE
1494static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1495
e7c27a73 1496static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1497
e7c27a73 1498static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1499
96408a79
SA
1500static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1501
ff013f42
JK
1502static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1503 struct dwarf2_cu *, struct partial_symtab *);
1504
a14ed312 1505static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1506 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1507 struct partial_symtab *);
c906108c 1508
fae299cd
DC
1509static void get_scope_pc_bounds (struct die_info *,
1510 CORE_ADDR *, CORE_ADDR *,
1511 struct dwarf2_cu *);
1512
801e3a5b
JB
1513static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1514 CORE_ADDR, struct dwarf2_cu *);
1515
a14ed312 1516static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1517 struct dwarf2_cu *);
c906108c 1518
a14ed312 1519static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1520 struct type *, struct dwarf2_cu *);
c906108c 1521
a14ed312 1522static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1523 struct die_info *, struct type *,
e7c27a73 1524 struct dwarf2_cu *);
c906108c 1525
a14ed312 1526static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1527 struct type *,
1528 struct dwarf2_cu *);
c906108c 1529
134d01f1 1530static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1531
e7c27a73 1532static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1533
e7c27a73 1534static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1535
5d7cb8df
JK
1536static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1537
27aa8d6a
SW
1538static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1539
f55ee35c
JK
1540static struct type *read_module_type (struct die_info *die,
1541 struct dwarf2_cu *cu);
1542
38d518c9 1543static const char *namespace_name (struct die_info *die,
e142c38c 1544 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1545
134d01f1 1546static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1547
e7c27a73 1548static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1549
6e70227d 1550static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1551 struct dwarf2_cu *);
1552
bf6af496 1553static struct die_info *read_die_and_siblings_1
d521ce57 1554 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1555 struct die_info *);
639d11d3 1556
dee91e82 1557static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1558 const gdb_byte *info_ptr,
1559 const gdb_byte **new_info_ptr,
639d11d3
DC
1560 struct die_info *parent);
1561
d521ce57
TT
1562static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1563 struct die_info **, const gdb_byte *,
1564 int *, int);
3019eac3 1565
d521ce57
TT
1566static const gdb_byte *read_full_die (const struct die_reader_specs *,
1567 struct die_info **, const gdb_byte *,
1568 int *);
93311388 1569
e7c27a73 1570static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1571
15d034d0
TT
1572static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1573 struct obstack *);
71c25dea 1574
15d034d0 1575static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1576
15d034d0 1577static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1578 struct die_info *die,
1579 struct dwarf2_cu *cu);
1580
ca69b9e6
DE
1581static const char *dwarf2_physname (const char *name, struct die_info *die,
1582 struct dwarf2_cu *cu);
1583
e142c38c 1584static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1585 struct dwarf2_cu **);
9219021c 1586
f39c6ffd 1587static const char *dwarf_tag_name (unsigned int);
c906108c 1588
f39c6ffd 1589static const char *dwarf_attr_name (unsigned int);
c906108c 1590
f39c6ffd 1591static const char *dwarf_form_name (unsigned int);
c906108c 1592
a14ed312 1593static char *dwarf_bool_name (unsigned int);
c906108c 1594
f39c6ffd 1595static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1596
f9aca02d 1597static struct die_info *sibling_die (struct die_info *);
c906108c 1598
d97bc12b
DE
1599static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1600
1601static void dump_die_for_error (struct die_info *);
1602
1603static void dump_die_1 (struct ui_file *, int level, int max_level,
1604 struct die_info *);
c906108c 1605
d97bc12b 1606/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1607
51545339 1608static void store_in_ref_table (struct die_info *,
10b3939b 1609 struct dwarf2_cu *);
c906108c 1610
ff39bb5e 1611static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1612
ff39bb5e 1613static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1614
348e048f 1615static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1616 const struct attribute *,
348e048f
DE
1617 struct dwarf2_cu **);
1618
10b3939b 1619static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1620 const struct attribute *,
f2f0e013 1621 struct dwarf2_cu **);
c906108c 1622
348e048f 1623static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1624 const struct attribute *,
348e048f
DE
1625 struct dwarf2_cu **);
1626
ac9ec31b
DE
1627static struct type *get_signatured_type (struct die_info *, ULONGEST,
1628 struct dwarf2_cu *);
1629
1630static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1631 const struct attribute *,
ac9ec31b
DE
1632 struct dwarf2_cu *);
1633
e5fe5e75 1634static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1635
52dc124a 1636static void read_signatured_type (struct signatured_type *);
348e048f 1637
f4dc4d17 1638static struct type_unit_group *get_type_unit_group
ff39bb5e 1639 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1640
1641static void build_type_unit_groups (die_reader_func_ftype *, void *);
1642
c906108c
SS
1643/* memory allocation interface */
1644
7b5a2f43 1645static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1646
b60c80d6 1647static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1648
09262596 1649static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1650 const char *, int);
2e276125 1651
6e5a29e1 1652static int attr_form_is_block (const struct attribute *);
8e19ed76 1653
6e5a29e1 1654static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1655
6e5a29e1 1656static int attr_form_is_constant (const struct attribute *);
3690dd37 1657
6e5a29e1 1658static int attr_form_is_ref (const struct attribute *);
7771576e 1659
8cf6f0b1
TT
1660static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1661 struct dwarf2_loclist_baton *baton,
ff39bb5e 1662 const struct attribute *attr);
8cf6f0b1 1663
ff39bb5e 1664static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1665 struct symbol *sym,
f1e6e072
TT
1666 struct dwarf2_cu *cu,
1667 int is_block);
4c2df51b 1668
d521ce57
TT
1669static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1670 const gdb_byte *info_ptr,
1671 struct abbrev_info *abbrev);
4bb7a0a7 1672
72bf9492
DJ
1673static void free_stack_comp_unit (void *);
1674
72bf9492
DJ
1675static hashval_t partial_die_hash (const void *item);
1676
1677static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1678
ae038cb0 1679static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1680 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1681
9816fde3 1682static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1683 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1684
1685static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1686 struct die_info *comp_unit_die,
1687 enum language pretend_language);
93311388 1688
68dc6402 1689static void free_heap_comp_unit (void *);
ae038cb0
DJ
1690
1691static void free_cached_comp_units (void *);
1692
1693static void age_cached_comp_units (void);
1694
dee91e82 1695static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1696
f792889a
DJ
1697static struct type *set_die_type (struct die_info *, struct type *,
1698 struct dwarf2_cu *);
1c379e20 1699
ae038cb0
DJ
1700static void create_all_comp_units (struct objfile *);
1701
0e50663e 1702static int create_all_type_units (struct objfile *);
1fd400ff 1703
95554aad
TT
1704static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1705 enum language);
10b3939b 1706
95554aad
TT
1707static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1708 enum language);
10b3939b 1709
f4dc4d17
DE
1710static void process_full_type_unit (struct dwarf2_per_cu_data *,
1711 enum language);
1712
10b3939b
DJ
1713static void dwarf2_add_dependence (struct dwarf2_cu *,
1714 struct dwarf2_per_cu_data *);
1715
ae038cb0
DJ
1716static void dwarf2_mark (struct dwarf2_cu *);
1717
1718static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1719
b64f50a1 1720static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1721 struct dwarf2_per_cu_data *);
673bfd45 1722
f792889a 1723static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1724
9291a0cd
TT
1725static void dwarf2_release_queue (void *dummy);
1726
95554aad
TT
1727static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1728 enum language pretend_language);
1729
1730static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1731 struct dwarf2_per_cu_data *per_cu,
1732 enum language pretend_language);
9291a0cd 1733
a0f42c21 1734static void process_queue (void);
9291a0cd
TT
1735
1736static void find_file_and_directory (struct die_info *die,
1737 struct dwarf2_cu *cu,
15d034d0 1738 const char **name, const char **comp_dir);
9291a0cd
TT
1739
1740static char *file_full_name (int file, struct line_header *lh,
1741 const char *comp_dir);
1742
d521ce57 1743static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1744 (struct comp_unit_head *header,
1745 struct dwarf2_section_info *section,
d521ce57 1746 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1747 int is_debug_types_section);
1748
fd820528 1749static void init_cutu_and_read_dies
f4dc4d17
DE
1750 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1751 int use_existing_cu, int keep,
3019eac3
DE
1752 die_reader_func_ftype *die_reader_func, void *data);
1753
dee91e82
DE
1754static void init_cutu_and_read_dies_simple
1755 (struct dwarf2_per_cu_data *this_cu,
1756 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1757
673bfd45 1758static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1759
3019eac3
DE
1760static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1761
a2ce51a0
DE
1762static struct dwo_unit *lookup_dwo_in_dwp
1763 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1764 const char *comp_dir, ULONGEST signature, int is_debug_types);
1765
1766static struct dwp_file *get_dwp_file (void);
1767
3019eac3 1768static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1769 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1770
1771static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1772 (struct signatured_type *, const char *, const char *);
3019eac3
DE
1773
1774static void free_dwo_file_cleanup (void *);
1775
95554aad
TT
1776static void process_cu_includes (void);
1777
1b80a9fa
JK
1778static void check_producer (struct dwarf2_cu *cu);
1779
9291a0cd
TT
1780#if WORDS_BIGENDIAN
1781
1782/* Convert VALUE between big- and little-endian. */
1783static offset_type
1784byte_swap (offset_type value)
1785{
1786 offset_type result;
1787
1788 result = (value & 0xff) << 24;
1789 result |= (value & 0xff00) << 8;
1790 result |= (value & 0xff0000) >> 8;
1791 result |= (value & 0xff000000) >> 24;
1792 return result;
1793}
1794
1795#define MAYBE_SWAP(V) byte_swap (V)
1796
1797#else
1798#define MAYBE_SWAP(V) (V)
1799#endif /* WORDS_BIGENDIAN */
1800
1801/* The suffix for an index file. */
1802#define INDEX_SUFFIX ".gdb-index"
1803
c906108c 1804/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1805 information and return true if we have enough to do something.
1806 NAMES points to the dwarf2 section names, or is NULL if the standard
1807 ELF names are used. */
c906108c
SS
1808
1809int
251d32d9
TG
1810dwarf2_has_info (struct objfile *objfile,
1811 const struct dwarf2_debug_sections *names)
c906108c 1812{
be391dca
TT
1813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1814 if (!dwarf2_per_objfile)
1815 {
1816 /* Initialize per-objfile state. */
1817 struct dwarf2_per_objfile *data
1818 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1819
be391dca
TT
1820 memset (data, 0, sizeof (*data));
1821 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1822 dwarf2_per_objfile = data;
6502dd73 1823
251d32d9
TG
1824 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1825 (void *) names);
be391dca
TT
1826 dwarf2_per_objfile->objfile = objfile;
1827 }
1828 return (dwarf2_per_objfile->info.asection != NULL
1829 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1830}
1831
251d32d9
TG
1832/* When loading sections, we look either for uncompressed section or for
1833 compressed section names. */
233a11ab
CS
1834
1835static int
251d32d9
TG
1836section_is_p (const char *section_name,
1837 const struct dwarf2_section_names *names)
233a11ab 1838{
251d32d9
TG
1839 if (names->normal != NULL
1840 && strcmp (section_name, names->normal) == 0)
1841 return 1;
1842 if (names->compressed != NULL
1843 && strcmp (section_name, names->compressed) == 0)
1844 return 1;
1845 return 0;
233a11ab
CS
1846}
1847
c906108c
SS
1848/* This function is mapped across the sections and remembers the
1849 offset and size of each of the debugging sections we are interested
1850 in. */
1851
1852static void
251d32d9 1853dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1854{
251d32d9 1855 const struct dwarf2_debug_sections *names;
dc7650b8 1856 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1857
1858 if (vnames == NULL)
1859 names = &dwarf2_elf_names;
1860 else
1861 names = (const struct dwarf2_debug_sections *) vnames;
1862
dc7650b8
JK
1863 if ((aflag & SEC_HAS_CONTENTS) == 0)
1864 {
1865 }
1866 else if (section_is_p (sectp->name, &names->info))
c906108c 1867 {
dce234bc
PP
1868 dwarf2_per_objfile->info.asection = sectp;
1869 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1870 }
251d32d9 1871 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1872 {
dce234bc
PP
1873 dwarf2_per_objfile->abbrev.asection = sectp;
1874 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1875 }
251d32d9 1876 else if (section_is_p (sectp->name, &names->line))
c906108c 1877 {
dce234bc
PP
1878 dwarf2_per_objfile->line.asection = sectp;
1879 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1880 }
251d32d9 1881 else if (section_is_p (sectp->name, &names->loc))
c906108c 1882 {
dce234bc
PP
1883 dwarf2_per_objfile->loc.asection = sectp;
1884 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1885 }
251d32d9 1886 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1887 {
dce234bc
PP
1888 dwarf2_per_objfile->macinfo.asection = sectp;
1889 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1890 }
cf2c3c16
TT
1891 else if (section_is_p (sectp->name, &names->macro))
1892 {
1893 dwarf2_per_objfile->macro.asection = sectp;
1894 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1895 }
251d32d9 1896 else if (section_is_p (sectp->name, &names->str))
c906108c 1897 {
dce234bc
PP
1898 dwarf2_per_objfile->str.asection = sectp;
1899 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1900 }
3019eac3
DE
1901 else if (section_is_p (sectp->name, &names->addr))
1902 {
1903 dwarf2_per_objfile->addr.asection = sectp;
1904 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1905 }
251d32d9 1906 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1907 {
dce234bc
PP
1908 dwarf2_per_objfile->frame.asection = sectp;
1909 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1910 }
251d32d9 1911 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1912 {
dc7650b8
JK
1913 dwarf2_per_objfile->eh_frame.asection = sectp;
1914 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1915 }
251d32d9 1916 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1917 {
dce234bc
PP
1918 dwarf2_per_objfile->ranges.asection = sectp;
1919 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1920 }
251d32d9 1921 else if (section_is_p (sectp->name, &names->types))
348e048f 1922 {
8b70b953
TT
1923 struct dwarf2_section_info type_section;
1924
1925 memset (&type_section, 0, sizeof (type_section));
1926 type_section.asection = sectp;
1927 type_section.size = bfd_get_section_size (sectp);
1928
1929 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1930 &type_section);
348e048f 1931 }
251d32d9 1932 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1933 {
1934 dwarf2_per_objfile->gdb_index.asection = sectp;
1935 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1936 }
dce234bc 1937
72dca2f5
FR
1938 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1939 && bfd_section_vma (abfd, sectp) == 0)
1940 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1941}
1942
fceca515
DE
1943/* A helper function that decides whether a section is empty,
1944 or not present. */
9e0ac564
TT
1945
1946static int
1947dwarf2_section_empty_p (struct dwarf2_section_info *info)
1948{
1949 return info->asection == NULL || info->size == 0;
1950}
1951
3019eac3
DE
1952/* Read the contents of the section INFO.
1953 OBJFILE is the main object file, but not necessarily the file where
1954 the section comes from. E.g., for DWO files INFO->asection->owner
1955 is the bfd of the DWO file.
dce234bc 1956 If the section is compressed, uncompress it before returning. */
c906108c 1957
dce234bc
PP
1958static void
1959dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1960{
dce234bc 1961 asection *sectp = info->asection;
3019eac3 1962 bfd *abfd;
dce234bc
PP
1963 gdb_byte *buf, *retbuf;
1964 unsigned char header[4];
c906108c 1965
be391dca
TT
1966 if (info->readin)
1967 return;
dce234bc 1968 info->buffer = NULL;
be391dca 1969 info->readin = 1;
188dd5d6 1970
9e0ac564 1971 if (dwarf2_section_empty_p (info))
dce234bc 1972 return;
c906108c 1973
3019eac3
DE
1974 abfd = sectp->owner;
1975
4bf44c1c
TT
1976 /* If the section has relocations, we must read it ourselves.
1977 Otherwise we attach it to the BFD. */
1978 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1979 {
d521ce57 1980 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1981 return;
dce234bc 1982 }
dce234bc 1983
4bf44c1c
TT
1984 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1985 info->buffer = buf;
dce234bc
PP
1986
1987 /* When debugging .o files, we may need to apply relocations; see
1988 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1989 We never compress sections in .o files, so we only need to
1990 try this when the section is not compressed. */
ac8035ab 1991 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1992 if (retbuf != NULL)
1993 {
1994 info->buffer = retbuf;
1995 return;
1996 }
1997
1998 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1999 || bfd_bread (buf, info->size, abfd) != info->size)
2000 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2001 bfd_get_filename (abfd));
2002}
2003
9e0ac564
TT
2004/* A helper function that returns the size of a section in a safe way.
2005 If you are positive that the section has been read before using the
2006 size, then it is safe to refer to the dwarf2_section_info object's
2007 "size" field directly. In other cases, you must call this
2008 function, because for compressed sections the size field is not set
2009 correctly until the section has been read. */
2010
2011static bfd_size_type
2012dwarf2_section_size (struct objfile *objfile,
2013 struct dwarf2_section_info *info)
2014{
2015 if (!info->readin)
2016 dwarf2_read_section (objfile, info);
2017 return info->size;
2018}
2019
dce234bc 2020/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2021 SECTION_NAME. */
af34e669 2022
dce234bc 2023void
3017a003
TG
2024dwarf2_get_section_info (struct objfile *objfile,
2025 enum dwarf2_section_enum sect,
d521ce57 2026 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2027 bfd_size_type *sizep)
2028{
2029 struct dwarf2_per_objfile *data
2030 = objfile_data (objfile, dwarf2_objfile_data_key);
2031 struct dwarf2_section_info *info;
a3b2a86b
TT
2032
2033 /* We may see an objfile without any DWARF, in which case we just
2034 return nothing. */
2035 if (data == NULL)
2036 {
2037 *sectp = NULL;
2038 *bufp = NULL;
2039 *sizep = 0;
2040 return;
2041 }
3017a003
TG
2042 switch (sect)
2043 {
2044 case DWARF2_DEBUG_FRAME:
2045 info = &data->frame;
2046 break;
2047 case DWARF2_EH_FRAME:
2048 info = &data->eh_frame;
2049 break;
2050 default:
2051 gdb_assert_not_reached ("unexpected section");
2052 }
dce234bc 2053
9e0ac564 2054 dwarf2_read_section (objfile, info);
dce234bc
PP
2055
2056 *sectp = info->asection;
2057 *bufp = info->buffer;
2058 *sizep = info->size;
2059}
2060
36586728
TT
2061/* A helper function to find the sections for a .dwz file. */
2062
2063static void
2064locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2065{
2066 struct dwz_file *dwz_file = arg;
2067
2068 /* Note that we only support the standard ELF names, because .dwz
2069 is ELF-only (at the time of writing). */
2070 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2071 {
2072 dwz_file->abbrev.asection = sectp;
2073 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2076 {
2077 dwz_file->info.asection = sectp;
2078 dwz_file->info.size = bfd_get_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2081 {
2082 dwz_file->str.asection = sectp;
2083 dwz_file->str.size = bfd_get_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2086 {
2087 dwz_file->line.asection = sectp;
2088 dwz_file->line.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2091 {
2092 dwz_file->macro.asection = sectp;
2093 dwz_file->macro.size = bfd_get_section_size (sectp);
2094 }
2ec9a5e0
TT
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2096 {
2097 dwz_file->gdb_index.asection = sectp;
2098 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2099 }
36586728
TT
2100}
2101
4db1a1dc
TT
2102/* Open the separate '.dwz' debug file, if needed. Return NULL if
2103 there is no .gnu_debugaltlink section in the file. Error if there
2104 is such a section but the file cannot be found. */
36586728
TT
2105
2106static struct dwz_file *
2107dwarf2_get_dwz_file (void)
2108{
4db1a1dc
TT
2109 bfd *dwz_bfd;
2110 char *data;
36586728
TT
2111 struct cleanup *cleanup;
2112 const char *filename;
2113 struct dwz_file *result;
4db1a1dc 2114 unsigned long buildid;
36586728
TT
2115
2116 if (dwarf2_per_objfile->dwz_file != NULL)
2117 return dwarf2_per_objfile->dwz_file;
2118
4db1a1dc
TT
2119 bfd_set_error (bfd_error_no_error);
2120 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2121 &buildid);
2122 if (data == NULL)
2123 {
2124 if (bfd_get_error () == bfd_error_no_error)
2125 return NULL;
2126 error (_("could not read '.gnu_debugaltlink' section: %s"),
2127 bfd_errmsg (bfd_get_error ()));
2128 }
36586728
TT
2129 cleanup = make_cleanup (xfree, data);
2130
f9d83a0b 2131 filename = (const char *) data;
36586728
TT
2132 if (!IS_ABSOLUTE_PATH (filename))
2133 {
2134 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2135 char *rel;
2136
2137 make_cleanup (xfree, abs);
2138 abs = ldirname (abs);
2139 make_cleanup (xfree, abs);
2140
2141 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2142 make_cleanup (xfree, rel);
2143 filename = rel;
2144 }
2145
2146 /* The format is just a NUL-terminated file name, followed by the
2147 build-id. For now, though, we ignore the build-id. */
2148 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2149 if (dwz_bfd == NULL)
2150 error (_("could not read '%s': %s"), filename,
2151 bfd_errmsg (bfd_get_error ()));
2152
2153 if (!bfd_check_format (dwz_bfd, bfd_object))
2154 {
2155 gdb_bfd_unref (dwz_bfd);
2156 error (_("file '%s' was not usable: %s"), filename,
2157 bfd_errmsg (bfd_get_error ()));
2158 }
2159
2160 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2161 struct dwz_file);
2162 result->dwz_bfd = dwz_bfd;
2163
2164 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2165
2166 do_cleanups (cleanup);
2167
8d2cc612 2168 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2169 return result;
2170}
9291a0cd 2171\f
7b9f3c50
DE
2172/* DWARF quick_symbols_functions support. */
2173
2174/* TUs can share .debug_line entries, and there can be a lot more TUs than
2175 unique line tables, so we maintain a separate table of all .debug_line
2176 derived entries to support the sharing.
2177 All the quick functions need is the list of file names. We discard the
2178 line_header when we're done and don't need to record it here. */
2179struct quick_file_names
2180{
094b34ac
DE
2181 /* The data used to construct the hash key. */
2182 struct stmt_list_hash hash;
7b9f3c50
DE
2183
2184 /* The number of entries in file_names, real_names. */
2185 unsigned int num_file_names;
2186
2187 /* The file names from the line table, after being run through
2188 file_full_name. */
2189 const char **file_names;
2190
2191 /* The file names from the line table after being run through
2192 gdb_realpath. These are computed lazily. */
2193 const char **real_names;
2194};
2195
2196/* When using the index (and thus not using psymtabs), each CU has an
2197 object of this type. This is used to hold information needed by
2198 the various "quick" methods. */
2199struct dwarf2_per_cu_quick_data
2200{
2201 /* The file table. This can be NULL if there was no file table
2202 or it's currently not read in.
2203 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2204 struct quick_file_names *file_names;
2205
2206 /* The corresponding symbol table. This is NULL if symbols for this
2207 CU have not yet been read. */
2208 struct symtab *symtab;
2209
2210 /* A temporary mark bit used when iterating over all CUs in
2211 expand_symtabs_matching. */
2212 unsigned int mark : 1;
2213
2214 /* True if we've tried to read the file table and found there isn't one.
2215 There will be no point in trying to read it again next time. */
2216 unsigned int no_file_data : 1;
2217};
2218
094b34ac
DE
2219/* Utility hash function for a stmt_list_hash. */
2220
2221static hashval_t
2222hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2223{
2224 hashval_t v = 0;
2225
2226 if (stmt_list_hash->dwo_unit != NULL)
2227 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2228 v += stmt_list_hash->line_offset.sect_off;
2229 return v;
2230}
2231
2232/* Utility equality function for a stmt_list_hash. */
2233
2234static int
2235eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2236 const struct stmt_list_hash *rhs)
2237{
2238 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2239 return 0;
2240 if (lhs->dwo_unit != NULL
2241 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2242 return 0;
2243
2244 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2245}
2246
7b9f3c50
DE
2247/* Hash function for a quick_file_names. */
2248
2249static hashval_t
2250hash_file_name_entry (const void *e)
2251{
2252 const struct quick_file_names *file_data = e;
2253
094b34ac 2254 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2255}
2256
2257/* Equality function for a quick_file_names. */
2258
2259static int
2260eq_file_name_entry (const void *a, const void *b)
2261{
2262 const struct quick_file_names *ea = a;
2263 const struct quick_file_names *eb = b;
2264
094b34ac 2265 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2266}
2267
2268/* Delete function for a quick_file_names. */
2269
2270static void
2271delete_file_name_entry (void *e)
2272{
2273 struct quick_file_names *file_data = e;
2274 int i;
2275
2276 for (i = 0; i < file_data->num_file_names; ++i)
2277 {
2278 xfree ((void*) file_data->file_names[i]);
2279 if (file_data->real_names)
2280 xfree ((void*) file_data->real_names[i]);
2281 }
2282
2283 /* The space for the struct itself lives on objfile_obstack,
2284 so we don't free it here. */
2285}
2286
2287/* Create a quick_file_names hash table. */
2288
2289static htab_t
2290create_quick_file_names_table (unsigned int nr_initial_entries)
2291{
2292 return htab_create_alloc (nr_initial_entries,
2293 hash_file_name_entry, eq_file_name_entry,
2294 delete_file_name_entry, xcalloc, xfree);
2295}
9291a0cd 2296
918dd910
JK
2297/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2298 have to be created afterwards. You should call age_cached_comp_units after
2299 processing PER_CU->CU. dw2_setup must have been already called. */
2300
2301static void
2302load_cu (struct dwarf2_per_cu_data *per_cu)
2303{
3019eac3 2304 if (per_cu->is_debug_types)
e5fe5e75 2305 load_full_type_unit (per_cu);
918dd910 2306 else
95554aad 2307 load_full_comp_unit (per_cu, language_minimal);
918dd910 2308
918dd910 2309 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2310
2311 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2312}
2313
a0f42c21 2314/* Read in the symbols for PER_CU. */
2fdf6df6 2315
9291a0cd 2316static void
a0f42c21 2317dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2318{
2319 struct cleanup *back_to;
2320
f4dc4d17
DE
2321 /* Skip type_unit_groups, reading the type units they contain
2322 is handled elsewhere. */
2323 if (IS_TYPE_UNIT_GROUP (per_cu))
2324 return;
2325
9291a0cd
TT
2326 back_to = make_cleanup (dwarf2_release_queue, NULL);
2327
95554aad
TT
2328 if (dwarf2_per_objfile->using_index
2329 ? per_cu->v.quick->symtab == NULL
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
2333 load_cu (per_cu);
2334 }
9291a0cd 2335
a0f42c21 2336 process_queue ();
9291a0cd
TT
2337
2338 /* Age the cache, releasing compilation units that have not
2339 been used recently. */
2340 age_cached_comp_units ();
2341
2342 do_cleanups (back_to);
2343}
2344
2345/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2346 the objfile from which this CU came. Returns the resulting symbol
2347 table. */
2fdf6df6 2348
9291a0cd 2349static struct symtab *
a0f42c21 2350dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2351{
95554aad 2352 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2353 if (!per_cu->v.quick->symtab)
2354 {
2355 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2356 increment_reading_symtab ();
a0f42c21 2357 dw2_do_instantiate_symtab (per_cu);
95554aad 2358 process_cu_includes ();
9291a0cd
TT
2359 do_cleanups (back_to);
2360 }
2361 return per_cu->v.quick->symtab;
2362}
2363
f4dc4d17
DE
2364/* Return the CU given its index.
2365
2366 This is intended for loops like:
2367
2368 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2369 + dwarf2_per_objfile->n_type_units); ++i)
2370 {
2371 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2372
2373 ...;
2374 }
2375*/
2fdf6df6 2376
1fd400ff
TT
2377static struct dwarf2_per_cu_data *
2378dw2_get_cu (int index)
2379{
2380 if (index >= dwarf2_per_objfile->n_comp_units)
2381 {
f4dc4d17 2382 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2383 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2384 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2385 }
2386
2387 return dwarf2_per_objfile->all_comp_units[index];
2388}
2389
2390/* Return the primary CU given its index.
2391 The difference between this function and dw2_get_cu is in the handling
2392 of type units (TUs). Here we return the type_unit_group object.
2393
2394 This is intended for loops like:
2395
2396 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2397 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2398 {
2399 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2400
2401 ...;
2402 }
2403*/
2404
2405static struct dwarf2_per_cu_data *
2406dw2_get_primary_cu (int index)
2407{
2408 if (index >= dwarf2_per_objfile->n_comp_units)
2409 {
1fd400ff 2410 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2411 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2412 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2413 }
f4dc4d17 2414
1fd400ff
TT
2415 return dwarf2_per_objfile->all_comp_units[index];
2416}
2417
2ec9a5e0
TT
2418/* A helper for create_cus_from_index that handles a given list of
2419 CUs. */
2fdf6df6 2420
74a0d9f6 2421static void
2ec9a5e0
TT
2422create_cus_from_index_list (struct objfile *objfile,
2423 const gdb_byte *cu_list, offset_type n_elements,
2424 struct dwarf2_section_info *section,
2425 int is_dwz,
2426 int base_offset)
9291a0cd
TT
2427{
2428 offset_type i;
9291a0cd 2429
2ec9a5e0 2430 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2431 {
2432 struct dwarf2_per_cu_data *the_cu;
2433 ULONGEST offset, length;
2434
74a0d9f6
JK
2435 gdb_static_assert (sizeof (ULONGEST) >= 8);
2436 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2437 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2438 cu_list += 2 * 8;
2439
2440 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2441 struct dwarf2_per_cu_data);
b64f50a1 2442 the_cu->offset.sect_off = offset;
9291a0cd
TT
2443 the_cu->length = length;
2444 the_cu->objfile = objfile;
8a0459fd 2445 the_cu->section = section;
9291a0cd
TT
2446 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2447 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2448 the_cu->is_dwz = is_dwz;
2449 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2450 }
9291a0cd
TT
2451}
2452
2ec9a5e0 2453/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2454 the CU objects for this objfile. */
2ec9a5e0 2455
74a0d9f6 2456static void
2ec9a5e0
TT
2457create_cus_from_index (struct objfile *objfile,
2458 const gdb_byte *cu_list, offset_type cu_list_elements,
2459 const gdb_byte *dwz_list, offset_type dwz_elements)
2460{
2461 struct dwz_file *dwz;
2462
2463 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2464 dwarf2_per_objfile->all_comp_units
2465 = obstack_alloc (&objfile->objfile_obstack,
2466 dwarf2_per_objfile->n_comp_units
2467 * sizeof (struct dwarf2_per_cu_data *));
2468
74a0d9f6
JK
2469 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2470 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2471
2472 if (dwz_elements == 0)
74a0d9f6 2473 return;
2ec9a5e0
TT
2474
2475 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2476 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2477 cu_list_elements / 2);
2ec9a5e0
TT
2478}
2479
1fd400ff 2480/* Create the signatured type hash table from the index. */
673bfd45 2481
74a0d9f6 2482static void
673bfd45 2483create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2484 struct dwarf2_section_info *section,
673bfd45
DE
2485 const gdb_byte *bytes,
2486 offset_type elements)
1fd400ff
TT
2487{
2488 offset_type i;
673bfd45 2489 htab_t sig_types_hash;
1fd400ff 2490
d467dd73
DE
2491 dwarf2_per_objfile->n_type_units = elements / 3;
2492 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2493 = xmalloc (dwarf2_per_objfile->n_type_units
2494 * sizeof (struct signatured_type *));
1fd400ff 2495
673bfd45 2496 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2497
2498 for (i = 0; i < elements; i += 3)
2499 {
52dc124a
DE
2500 struct signatured_type *sig_type;
2501 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2502 void **slot;
2503
74a0d9f6
JK
2504 gdb_static_assert (sizeof (ULONGEST) >= 8);
2505 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2506 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2507 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2508 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2509 bytes += 3 * 8;
2510
52dc124a 2511 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2512 struct signatured_type);
52dc124a 2513 sig_type->signature = signature;
3019eac3
DE
2514 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2515 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2516 sig_type->per_cu.section = section;
52dc124a
DE
2517 sig_type->per_cu.offset.sect_off = offset;
2518 sig_type->per_cu.objfile = objfile;
2519 sig_type->per_cu.v.quick
1fd400ff
TT
2520 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2521 struct dwarf2_per_cu_quick_data);
2522
52dc124a
DE
2523 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2524 *slot = sig_type;
1fd400ff 2525
b4dd5633 2526 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2527 }
2528
673bfd45 2529 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2530}
2531
9291a0cd
TT
2532/* Read the address map data from the mapped index, and use it to
2533 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2534
9291a0cd
TT
2535static void
2536create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2537{
2538 const gdb_byte *iter, *end;
2539 struct obstack temp_obstack;
2540 struct addrmap *mutable_map;
2541 struct cleanup *cleanup;
2542 CORE_ADDR baseaddr;
2543
2544 obstack_init (&temp_obstack);
2545 cleanup = make_cleanup_obstack_free (&temp_obstack);
2546 mutable_map = addrmap_create_mutable (&temp_obstack);
2547
2548 iter = index->address_table;
2549 end = iter + index->address_table_size;
2550
2551 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2552
2553 while (iter < end)
2554 {
2555 ULONGEST hi, lo, cu_index;
2556 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2557 iter += 8;
2558 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2559 iter += 8;
2560 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2561 iter += 4;
f652bce2
DE
2562
2563 if (cu_index < dwarf2_per_objfile->n_comp_units)
2564 {
2565 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2566 dw2_get_cu (cu_index));
2567 }
2568 else
2569 {
2570 complaint (&symfile_complaints,
2571 _(".gdb_index address table has invalid CU number %u"),
2572 (unsigned) cu_index);
2573 }
9291a0cd
TT
2574 }
2575
2576 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2577 &objfile->objfile_obstack);
2578 do_cleanups (cleanup);
2579}
2580
59d7bcaf
JK
2581/* The hash function for strings in the mapped index. This is the same as
2582 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2583 implementation. This is necessary because the hash function is tied to the
2584 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2585 SYMBOL_HASH_NEXT.
2586
2587 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2588
9291a0cd 2589static hashval_t
559a7a62 2590mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2591{
2592 const unsigned char *str = (const unsigned char *) p;
2593 hashval_t r = 0;
2594 unsigned char c;
2595
2596 while ((c = *str++) != 0)
559a7a62
JK
2597 {
2598 if (index_version >= 5)
2599 c = tolower (c);
2600 r = r * 67 + c - 113;
2601 }
9291a0cd
TT
2602
2603 return r;
2604}
2605
2606/* Find a slot in the mapped index INDEX for the object named NAME.
2607 If NAME is found, set *VEC_OUT to point to the CU vector in the
2608 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2609
9291a0cd
TT
2610static int
2611find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2612 offset_type **vec_out)
2613{
0cf03b49
JK
2614 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2615 offset_type hash;
9291a0cd 2616 offset_type slot, step;
559a7a62 2617 int (*cmp) (const char *, const char *);
9291a0cd 2618
0cf03b49
JK
2619 if (current_language->la_language == language_cplus
2620 || current_language->la_language == language_java
2621 || current_language->la_language == language_fortran)
2622 {
2623 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2624 not contain any. */
2625 const char *paren = strchr (name, '(');
2626
2627 if (paren)
2628 {
2629 char *dup;
2630
2631 dup = xmalloc (paren - name + 1);
2632 memcpy (dup, name, paren - name);
2633 dup[paren - name] = 0;
2634
2635 make_cleanup (xfree, dup);
2636 name = dup;
2637 }
2638 }
2639
559a7a62 2640 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2641 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2642 simulate our NAME being searched is also lowercased. */
2643 hash = mapped_index_string_hash ((index->version == 4
2644 && case_sensitivity == case_sensitive_off
2645 ? 5 : index->version),
2646 name);
2647
3876f04e
DE
2648 slot = hash & (index->symbol_table_slots - 1);
2649 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2650 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2651
2652 for (;;)
2653 {
2654 /* Convert a slot number to an offset into the table. */
2655 offset_type i = 2 * slot;
2656 const char *str;
3876f04e 2657 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2658 {
2659 do_cleanups (back_to);
2660 return 0;
2661 }
9291a0cd 2662
3876f04e 2663 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2664 if (!cmp (name, str))
9291a0cd
TT
2665 {
2666 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2667 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2668 do_cleanups (back_to);
9291a0cd
TT
2669 return 1;
2670 }
2671
3876f04e 2672 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2673 }
2674}
2675
2ec9a5e0
TT
2676/* A helper function that reads the .gdb_index from SECTION and fills
2677 in MAP. FILENAME is the name of the file containing the section;
2678 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2679 ok to use deprecated sections.
2680
2681 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2682 out parameters that are filled in with information about the CU and
2683 TU lists in the section.
2684
2685 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2686
9291a0cd 2687static int
2ec9a5e0
TT
2688read_index_from_section (struct objfile *objfile,
2689 const char *filename,
2690 int deprecated_ok,
2691 struct dwarf2_section_info *section,
2692 struct mapped_index *map,
2693 const gdb_byte **cu_list,
2694 offset_type *cu_list_elements,
2695 const gdb_byte **types_list,
2696 offset_type *types_list_elements)
9291a0cd 2697{
948f8e3d 2698 const gdb_byte *addr;
2ec9a5e0 2699 offset_type version;
b3b272e1 2700 offset_type *metadata;
1fd400ff 2701 int i;
9291a0cd 2702
2ec9a5e0 2703 if (dwarf2_section_empty_p (section))
9291a0cd 2704 return 0;
82430852
JK
2705
2706 /* Older elfutils strip versions could keep the section in the main
2707 executable while splitting it for the separate debug info file. */
2ec9a5e0 2708 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2709 return 0;
2710
2ec9a5e0 2711 dwarf2_read_section (objfile, section);
9291a0cd 2712
2ec9a5e0 2713 addr = section->buffer;
9291a0cd 2714 /* Version check. */
1fd400ff 2715 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2716 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2717 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2718 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2719 indices. */
831adc1f 2720 if (version < 4)
481860b3
GB
2721 {
2722 static int warning_printed = 0;
2723 if (!warning_printed)
2724 {
2725 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2726 filename);
481860b3
GB
2727 warning_printed = 1;
2728 }
2729 return 0;
2730 }
2731 /* Index version 4 uses a different hash function than index version
2732 5 and later.
2733
2734 Versions earlier than 6 did not emit psymbols for inlined
2735 functions. Using these files will cause GDB not to be able to
2736 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2737 indices unless the user has done
2738 "set use-deprecated-index-sections on". */
2ec9a5e0 2739 if (version < 6 && !deprecated_ok)
481860b3
GB
2740 {
2741 static int warning_printed = 0;
2742 if (!warning_printed)
2743 {
e615022a
DE
2744 warning (_("\
2745Skipping deprecated .gdb_index section in %s.\n\
2746Do \"set use-deprecated-index-sections on\" before the file is read\n\
2747to use the section anyway."),
2ec9a5e0 2748 filename);
481860b3
GB
2749 warning_printed = 1;
2750 }
2751 return 0;
2752 }
796a7ff8
DE
2753 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2754 of the TU (for symbols coming from TUs). It's just a performance bug, and
2755 we can't distinguish gdb-generated indices from gold-generated ones, so
2756 nothing to do here. */
2757
481860b3 2758 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2759 longer backward compatible. */
796a7ff8 2760 if (version > 8)
594e8718 2761 return 0;
9291a0cd 2762
559a7a62 2763 map->version = version;
2ec9a5e0 2764 map->total_size = section->size;
9291a0cd
TT
2765
2766 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2767
2768 i = 0;
2ec9a5e0
TT
2769 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2770 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2771 / 8);
1fd400ff
TT
2772 ++i;
2773
2ec9a5e0
TT
2774 *types_list = addr + MAYBE_SWAP (metadata[i]);
2775 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2776 - MAYBE_SWAP (metadata[i]))
2777 / 8);
987d643c 2778 ++i;
1fd400ff
TT
2779
2780 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2781 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2782 - MAYBE_SWAP (metadata[i]));
2783 ++i;
2784
3876f04e
DE
2785 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2786 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2787 - MAYBE_SWAP (metadata[i]))
2788 / (2 * sizeof (offset_type)));
1fd400ff 2789 ++i;
9291a0cd 2790
f9d83a0b 2791 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2792
2ec9a5e0
TT
2793 return 1;
2794}
2795
2796
2797/* Read the index file. If everything went ok, initialize the "quick"
2798 elements of all the CUs and return 1. Otherwise, return 0. */
2799
2800static int
2801dwarf2_read_index (struct objfile *objfile)
2802{
2803 struct mapped_index local_map, *map;
2804 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2805 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2806 struct dwz_file *dwz;
2ec9a5e0
TT
2807
2808 if (!read_index_from_section (objfile, objfile->name,
2809 use_deprecated_index_sections,
2810 &dwarf2_per_objfile->gdb_index, &local_map,
2811 &cu_list, &cu_list_elements,
2812 &types_list, &types_list_elements))
2813 return 0;
2814
0fefef59 2815 /* Don't use the index if it's empty. */
2ec9a5e0 2816 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2817 return 0;
2818
2ec9a5e0
TT
2819 /* If there is a .dwz file, read it so we can get its CU list as
2820 well. */
4db1a1dc
TT
2821 dwz = dwarf2_get_dwz_file ();
2822 if (dwz != NULL)
2ec9a5e0 2823 {
2ec9a5e0
TT
2824 struct mapped_index dwz_map;
2825 const gdb_byte *dwz_types_ignore;
2826 offset_type dwz_types_elements_ignore;
2827
2828 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2829 1,
2830 &dwz->gdb_index, &dwz_map,
2831 &dwz_list, &dwz_list_elements,
2832 &dwz_types_ignore,
2833 &dwz_types_elements_ignore))
2834 {
2835 warning (_("could not read '.gdb_index' section from %s; skipping"),
2836 bfd_get_filename (dwz->dwz_bfd));
2837 return 0;
2838 }
2839 }
2840
74a0d9f6
JK
2841 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2842 dwz_list_elements);
1fd400ff 2843
8b70b953
TT
2844 if (types_list_elements)
2845 {
2846 struct dwarf2_section_info *section;
2847
2848 /* We can only handle a single .debug_types when we have an
2849 index. */
2850 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2851 return 0;
2852
2853 section = VEC_index (dwarf2_section_info_def,
2854 dwarf2_per_objfile->types, 0);
2855
74a0d9f6
JK
2856 create_signatured_type_table_from_index (objfile, section, types_list,
2857 types_list_elements);
8b70b953 2858 }
9291a0cd 2859
2ec9a5e0
TT
2860 create_addrmap_from_index (objfile, &local_map);
2861
2862 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2863 *map = local_map;
9291a0cd
TT
2864
2865 dwarf2_per_objfile->index_table = map;
2866 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2867 dwarf2_per_objfile->quick_file_names_table =
2868 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2869
2870 return 1;
2871}
2872
2873/* A helper for the "quick" functions which sets the global
2874 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2875
9291a0cd
TT
2876static void
2877dw2_setup (struct objfile *objfile)
2878{
2879 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2880 gdb_assert (dwarf2_per_objfile);
2881}
2882
dee91e82 2883/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2884
dee91e82
DE
2885static void
2886dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2887 const gdb_byte *info_ptr,
dee91e82
DE
2888 struct die_info *comp_unit_die,
2889 int has_children,
2890 void *data)
9291a0cd 2891{
dee91e82
DE
2892 struct dwarf2_cu *cu = reader->cu;
2893 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2894 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2895 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2896 struct line_header *lh;
9291a0cd 2897 struct attribute *attr;
dee91e82 2898 int i;
15d034d0 2899 const char *name, *comp_dir;
7b9f3c50
DE
2900 void **slot;
2901 struct quick_file_names *qfn;
2902 unsigned int line_offset;
9291a0cd 2903
0186c6a7
DE
2904 gdb_assert (! this_cu->is_debug_types);
2905
07261596
TT
2906 /* Our callers never want to match partial units -- instead they
2907 will match the enclosing full CU. */
2908 if (comp_unit_die->tag == DW_TAG_partial_unit)
2909 {
2910 this_cu->v.quick->no_file_data = 1;
2911 return;
2912 }
2913
0186c6a7 2914 lh_cu = this_cu;
7b9f3c50
DE
2915 lh = NULL;
2916 slot = NULL;
2917 line_offset = 0;
dee91e82
DE
2918
2919 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2920 if (attr)
2921 {
7b9f3c50
DE
2922 struct quick_file_names find_entry;
2923
2924 line_offset = DW_UNSND (attr);
2925
2926 /* We may have already read in this line header (TU line header sharing).
2927 If we have we're done. */
094b34ac
DE
2928 find_entry.hash.dwo_unit = cu->dwo_unit;
2929 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2930 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2931 &find_entry, INSERT);
2932 if (*slot != NULL)
2933 {
094b34ac 2934 lh_cu->v.quick->file_names = *slot;
dee91e82 2935 return;
7b9f3c50
DE
2936 }
2937
3019eac3 2938 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2939 }
2940 if (lh == NULL)
2941 {
094b34ac 2942 lh_cu->v.quick->no_file_data = 1;
dee91e82 2943 return;
9291a0cd
TT
2944 }
2945
7b9f3c50 2946 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2947 qfn->hash.dwo_unit = cu->dwo_unit;
2948 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2949 gdb_assert (slot != NULL);
2950 *slot = qfn;
9291a0cd 2951
dee91e82 2952 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2953
7b9f3c50
DE
2954 qfn->num_file_names = lh->num_file_names;
2955 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2956 lh->num_file_names * sizeof (char *));
9291a0cd 2957 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2958 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2959 qfn->real_names = NULL;
9291a0cd 2960
7b9f3c50 2961 free_line_header (lh);
7b9f3c50 2962
094b34ac 2963 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2964}
2965
2966/* A helper for the "quick" functions which attempts to read the line
2967 table for THIS_CU. */
2968
2969static struct quick_file_names *
e4a48d9d 2970dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2971{
0186c6a7
DE
2972 /* This should never be called for TUs. */
2973 gdb_assert (! this_cu->is_debug_types);
2974 /* Nor type unit groups. */
2975 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 2976
dee91e82
DE
2977 if (this_cu->v.quick->file_names != NULL)
2978 return this_cu->v.quick->file_names;
2979 /* If we know there is no line data, no point in looking again. */
2980 if (this_cu->v.quick->no_file_data)
2981 return NULL;
2982
0186c6a7 2983 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2984
2985 if (this_cu->v.quick->no_file_data)
2986 return NULL;
2987 return this_cu->v.quick->file_names;
9291a0cd
TT
2988}
2989
2990/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2991 real path for a given file name from the line table. */
2fdf6df6 2992
9291a0cd 2993static const char *
7b9f3c50
DE
2994dw2_get_real_path (struct objfile *objfile,
2995 struct quick_file_names *qfn, int index)
9291a0cd 2996{
7b9f3c50
DE
2997 if (qfn->real_names == NULL)
2998 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2999 qfn->num_file_names, sizeof (char *));
9291a0cd 3000
7b9f3c50
DE
3001 if (qfn->real_names[index] == NULL)
3002 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3003
7b9f3c50 3004 return qfn->real_names[index];
9291a0cd
TT
3005}
3006
3007static struct symtab *
3008dw2_find_last_source_symtab (struct objfile *objfile)
3009{
3010 int index;
ae2de4f8 3011
9291a0cd
TT
3012 dw2_setup (objfile);
3013 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3014 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3015}
3016
7b9f3c50
DE
3017/* Traversal function for dw2_forget_cached_source_info. */
3018
3019static int
3020dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3021{
7b9f3c50 3022 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3023
7b9f3c50 3024 if (file_data->real_names)
9291a0cd 3025 {
7b9f3c50 3026 int i;
9291a0cd 3027
7b9f3c50 3028 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3029 {
7b9f3c50
DE
3030 xfree ((void*) file_data->real_names[i]);
3031 file_data->real_names[i] = NULL;
9291a0cd
TT
3032 }
3033 }
7b9f3c50
DE
3034
3035 return 1;
3036}
3037
3038static void
3039dw2_forget_cached_source_info (struct objfile *objfile)
3040{
3041 dw2_setup (objfile);
3042
3043 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3044 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3045}
3046
f8eba3c6
TT
3047/* Helper function for dw2_map_symtabs_matching_filename that expands
3048 the symtabs and calls the iterator. */
3049
3050static int
3051dw2_map_expand_apply (struct objfile *objfile,
3052 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3053 const char *name, const char *real_path,
f8eba3c6
TT
3054 int (*callback) (struct symtab *, void *),
3055 void *data)
3056{
3057 struct symtab *last_made = objfile->symtabs;
3058
3059 /* Don't visit already-expanded CUs. */
3060 if (per_cu->v.quick->symtab)
3061 return 0;
3062
3063 /* This may expand more than one symtab, and we want to iterate over
3064 all of them. */
a0f42c21 3065 dw2_instantiate_symtab (per_cu);
f8eba3c6 3066
f5b95b50 3067 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3068 objfile->symtabs, last_made);
3069}
3070
3071/* Implementation of the map_symtabs_matching_filename method. */
3072
9291a0cd 3073static int
f8eba3c6 3074dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3075 const char *real_path,
f8eba3c6
TT
3076 int (*callback) (struct symtab *, void *),
3077 void *data)
9291a0cd
TT
3078{
3079 int i;
c011a4f4 3080 const char *name_basename = lbasename (name);
9291a0cd
TT
3081
3082 dw2_setup (objfile);
ae2de4f8 3083
848e3e78
DE
3084 /* The rule is CUs specify all the files, including those used by
3085 any TU, so there's no need to scan TUs here. */
f4dc4d17 3086
848e3e78 3087 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3088 {
3089 int j;
f4dc4d17 3090 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3091 struct quick_file_names *file_data;
9291a0cd 3092
3d7bb9d9 3093 /* We only need to look at symtabs not already expanded. */
e254ef6a 3094 if (per_cu->v.quick->symtab)
9291a0cd
TT
3095 continue;
3096
e4a48d9d 3097 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3098 if (file_data == NULL)
9291a0cd
TT
3099 continue;
3100
7b9f3c50 3101 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3102 {
7b9f3c50 3103 const char *this_name = file_data->file_names[j];
da235a7c 3104 const char *this_real_name;
9291a0cd 3105
af529f8f 3106 if (compare_filenames_for_search (this_name, name))
9291a0cd 3107 {
f5b95b50 3108 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3109 callback, data))
3110 return 1;
288e77a7 3111 continue;
4aac40c8 3112 }
9291a0cd 3113
c011a4f4
DE
3114 /* Before we invoke realpath, which can get expensive when many
3115 files are involved, do a quick comparison of the basenames. */
3116 if (! basenames_may_differ
3117 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3118 continue;
3119
da235a7c
JK
3120 this_real_name = dw2_get_real_path (objfile, file_data, j);
3121 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3122 {
da235a7c
JK
3123 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3124 callback, data))
3125 return 1;
288e77a7 3126 continue;
da235a7c 3127 }
9291a0cd 3128
da235a7c
JK
3129 if (real_path != NULL)
3130 {
af529f8f
JK
3131 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3132 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3133 if (this_real_name != NULL
af529f8f 3134 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3135 {
f5b95b50 3136 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3137 callback, data))
3138 return 1;
288e77a7 3139 continue;
9291a0cd
TT
3140 }
3141 }
3142 }
3143 }
3144
9291a0cd
TT
3145 return 0;
3146}
3147
da51c347
DE
3148/* Struct used to manage iterating over all CUs looking for a symbol. */
3149
3150struct dw2_symtab_iterator
9291a0cd 3151{
da51c347
DE
3152 /* The internalized form of .gdb_index. */
3153 struct mapped_index *index;
3154 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3155 int want_specific_block;
3156 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3157 Unused if !WANT_SPECIFIC_BLOCK. */
3158 int block_index;
3159 /* The kind of symbol we're looking for. */
3160 domain_enum domain;
3161 /* The list of CUs from the index entry of the symbol,
3162 or NULL if not found. */
3163 offset_type *vec;
3164 /* The next element in VEC to look at. */
3165 int next;
3166 /* The number of elements in VEC, or zero if there is no match. */
3167 int length;
3168};
9291a0cd 3169
da51c347
DE
3170/* Initialize the index symtab iterator ITER.
3171 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3172 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3173
9291a0cd 3174static void
da51c347
DE
3175dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3176 struct mapped_index *index,
3177 int want_specific_block,
3178 int block_index,
3179 domain_enum domain,
3180 const char *name)
3181{
3182 iter->index = index;
3183 iter->want_specific_block = want_specific_block;
3184 iter->block_index = block_index;
3185 iter->domain = domain;
3186 iter->next = 0;
3187
3188 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3189 iter->length = MAYBE_SWAP (*iter->vec);
3190 else
3191 {
3192 iter->vec = NULL;
3193 iter->length = 0;
3194 }
3195}
3196
3197/* Return the next matching CU or NULL if there are no more. */
3198
3199static struct dwarf2_per_cu_data *
3200dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3201{
3202 for ( ; iter->next < iter->length; ++iter->next)
3203 {
3204 offset_type cu_index_and_attrs =
3205 MAYBE_SWAP (iter->vec[iter->next + 1]);
3206 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3207 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3208 int want_static = iter->block_index != GLOBAL_BLOCK;
3209 /* This value is only valid for index versions >= 7. */
3210 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3211 gdb_index_symbol_kind symbol_kind =
3212 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3213 /* Only check the symbol attributes if they're present.
3214 Indices prior to version 7 don't record them,
3215 and indices >= 7 may elide them for certain symbols
3216 (gold does this). */
3217 int attrs_valid =
3218 (iter->index->version >= 7
3219 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3220
3190f0c6
DE
3221 /* Don't crash on bad data. */
3222 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3223 + dwarf2_per_objfile->n_type_units))
3224 {
3225 complaint (&symfile_complaints,
3226 _(".gdb_index entry has bad CU index"
3227 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3228 continue;
3229 }
3230
3231 per_cu = dw2_get_cu (cu_index);
3232
da51c347
DE
3233 /* Skip if already read in. */
3234 if (per_cu->v.quick->symtab)
3235 continue;
3236
3237 if (attrs_valid
3238 && iter->want_specific_block
3239 && want_static != is_static)
3240 continue;
3241
3242 /* Only check the symbol's kind if it has one. */
3243 if (attrs_valid)
3244 {
3245 switch (iter->domain)
3246 {
3247 case VAR_DOMAIN:
3248 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3249 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3250 /* Some types are also in VAR_DOMAIN. */
3251 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3252 continue;
3253 break;
3254 case STRUCT_DOMAIN:
3255 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3256 continue;
3257 break;
3258 case LABEL_DOMAIN:
3259 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3260 continue;
3261 break;
3262 default:
3263 break;
3264 }
3265 }
3266
3267 ++iter->next;
3268 return per_cu;
3269 }
3270
3271 return NULL;
3272}
3273
3274static struct symtab *
3275dw2_lookup_symbol (struct objfile *objfile, int block_index,
3276 const char *name, domain_enum domain)
9291a0cd 3277{
da51c347 3278 struct symtab *stab_best = NULL;
156942c7
DE
3279 struct mapped_index *index;
3280
9291a0cd
TT
3281 dw2_setup (objfile);
3282
156942c7
DE
3283 index = dwarf2_per_objfile->index_table;
3284
da51c347 3285 /* index is NULL if OBJF_READNOW. */
156942c7 3286 if (index)
9291a0cd 3287 {
da51c347
DE
3288 struct dw2_symtab_iterator iter;
3289 struct dwarf2_per_cu_data *per_cu;
3290
3291 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3292
da51c347 3293 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3294 {
da51c347
DE
3295 struct symbol *sym = NULL;
3296 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3297
3298 /* Some caution must be observed with overloaded functions
3299 and methods, since the index will not contain any overload
3300 information (but NAME might contain it). */
3301 if (stab->primary)
9291a0cd 3302 {
da51c347
DE
3303 struct blockvector *bv = BLOCKVECTOR (stab);
3304 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3305
da51c347
DE
3306 sym = lookup_block_symbol (block, name, domain);
3307 }
1fd400ff 3308
da51c347
DE
3309 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3310 {
3311 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3312 return stab;
3313
3314 stab_best = stab;
9291a0cd 3315 }
da51c347
DE
3316
3317 /* Keep looking through other CUs. */
9291a0cd
TT
3318 }
3319 }
9291a0cd 3320
da51c347 3321 return stab_best;
9291a0cd
TT
3322}
3323
3324static void
3325dw2_print_stats (struct objfile *objfile)
3326{
e4a48d9d 3327 int i, total, count;
9291a0cd
TT
3328
3329 dw2_setup (objfile);
e4a48d9d 3330 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3331 count = 0;
e4a48d9d 3332 for (i = 0; i < total; ++i)
9291a0cd 3333 {
e254ef6a 3334 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3335
e254ef6a 3336 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3337 ++count;
3338 }
e4a48d9d 3339 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3340 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3341}
3342
3343static void
3344dw2_dump (struct objfile *objfile)
3345{
3346 /* Nothing worth printing. */
3347}
3348
3349static void
3189cb12
DE
3350dw2_relocate (struct objfile *objfile,
3351 const struct section_offsets *new_offsets,
3352 const struct section_offsets *delta)
9291a0cd
TT
3353{
3354 /* There's nothing to relocate here. */
3355}
3356
3357static void
3358dw2_expand_symtabs_for_function (struct objfile *objfile,
3359 const char *func_name)
3360{
da51c347
DE
3361 struct mapped_index *index;
3362
3363 dw2_setup (objfile);
3364
3365 index = dwarf2_per_objfile->index_table;
3366
3367 /* index is NULL if OBJF_READNOW. */
3368 if (index)
3369 {
3370 struct dw2_symtab_iterator iter;
3371 struct dwarf2_per_cu_data *per_cu;
3372
3373 /* Note: It doesn't matter what we pass for block_index here. */
3374 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3375 func_name);
3376
3377 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3378 dw2_instantiate_symtab (per_cu);
3379 }
9291a0cd
TT
3380}
3381
3382static void
3383dw2_expand_all_symtabs (struct objfile *objfile)
3384{
3385 int i;
3386
3387 dw2_setup (objfile);
1fd400ff
TT
3388
3389 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3390 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3391 {
e254ef6a 3392 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3393
a0f42c21 3394 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3395 }
3396}
3397
3398static void
652a8996
JK
3399dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3400 const char *fullname)
9291a0cd
TT
3401{
3402 int i;
3403
3404 dw2_setup (objfile);
d4637a04
DE
3405
3406 /* We don't need to consider type units here.
3407 This is only called for examining code, e.g. expand_line_sal.
3408 There can be an order of magnitude (or more) more type units
3409 than comp units, and we avoid them if we can. */
3410
3411 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3412 {
3413 int j;
e254ef6a 3414 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3415 struct quick_file_names *file_data;
9291a0cd 3416
3d7bb9d9 3417 /* We only need to look at symtabs not already expanded. */
e254ef6a 3418 if (per_cu->v.quick->symtab)
9291a0cd
TT
3419 continue;
3420
e4a48d9d 3421 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3422 if (file_data == NULL)
9291a0cd
TT
3423 continue;
3424
7b9f3c50 3425 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3426 {
652a8996
JK
3427 const char *this_fullname = file_data->file_names[j];
3428
3429 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3430 {
a0f42c21 3431 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3432 break;
3433 }
3434 }
3435 }
3436}
3437
356d9f9d
TT
3438/* A helper function for dw2_find_symbol_file that finds the primary
3439 file name for a given CU. This is a die_reader_func. */
3440
3441static void
3442dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
d521ce57 3443 const gdb_byte *info_ptr,
356d9f9d
TT
3444 struct die_info *comp_unit_die,
3445 int has_children,
3446 void *data)
3447{
3448 const char **result_ptr = data;
3449 struct dwarf2_cu *cu = reader->cu;
3450 struct attribute *attr;
3451
3452 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3453 if (attr == NULL)
3454 *result_ptr = NULL;
3455 else
3456 *result_ptr = DW_STRING (attr);
3457}
3458
dd786858 3459static const char *
9291a0cd
TT
3460dw2_find_symbol_file (struct objfile *objfile, const char *name)
3461{
e254ef6a 3462 struct dwarf2_per_cu_data *per_cu;
9291a0cd 3463 offset_type *vec;
356d9f9d 3464 const char *filename;
9291a0cd
TT
3465
3466 dw2_setup (objfile);
3467
ae2de4f8 3468 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 3469 if (!dwarf2_per_objfile->index_table)
96408a79
SA
3470 {
3471 struct symtab *s;
3472
d790cf0a
DE
3473 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3474 {
3475 struct blockvector *bv = BLOCKVECTOR (s);
3476 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3477 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3478
3479 if (sym)
652a8996
JK
3480 {
3481 /* Only file extension of returned filename is recognized. */
3482 return SYMBOL_SYMTAB (sym)->filename;
3483 }
d790cf0a 3484 }
96408a79
SA
3485 return NULL;
3486 }
9291a0cd
TT
3487
3488 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3489 name, &vec))
3490 return NULL;
3491
3492 /* Note that this just looks at the very first one named NAME -- but
3493 actually we are looking for a function. find_main_filename
3494 should be rewritten so that it doesn't require a custom hook. It
3495 could just use the ordinary symbol tables. */
3496 /* vec[0] is the length, which must always be >0. */
156942c7 3497 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 3498
356d9f9d 3499 if (per_cu->v.quick->symtab != NULL)
652a8996
JK
3500 {
3501 /* Only file extension of returned filename is recognized. */
3502 return per_cu->v.quick->symtab->filename;
3503 }
356d9f9d 3504
a98c29a0
DE
3505 /* Initialize filename in case there's a problem reading the DWARF,
3506 dw2_get_primary_filename_reader may not get called. */
3507 filename = NULL;
f4dc4d17
DE
3508 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3509 dw2_get_primary_filename_reader, &filename);
9291a0cd 3510
652a8996 3511 /* Only file extension of returned filename is recognized. */
356d9f9d 3512 return filename;
9291a0cd
TT
3513}
3514
3515static void
40658b94
PH
3516dw2_map_matching_symbols (const char * name, domain_enum namespace,
3517 struct objfile *objfile, int global,
3518 int (*callback) (struct block *,
3519 struct symbol *, void *),
2edb89d3
JK
3520 void *data, symbol_compare_ftype *match,
3521 symbol_compare_ftype *ordered_compare)
9291a0cd 3522{
40658b94 3523 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3524 current language is Ada for a non-Ada objfile using GNU index. As Ada
3525 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3526}
3527
3528static void
f8eba3c6
TT
3529dw2_expand_symtabs_matching
3530 (struct objfile *objfile,
fbd9ab74 3531 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3532 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3533 enum search_domain kind,
3534 void *data)
9291a0cd
TT
3535{
3536 int i;
3537 offset_type iter;
4b5246aa 3538 struct mapped_index *index;
9291a0cd
TT
3539
3540 dw2_setup (objfile);
ae2de4f8
DE
3541
3542 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3543 if (!dwarf2_per_objfile->index_table)
3544 return;
4b5246aa 3545 index = dwarf2_per_objfile->index_table;
9291a0cd 3546
7b08b9eb 3547 if (file_matcher != NULL)
24c79950
TT
3548 {
3549 struct cleanup *cleanup;
3550 htab_t visited_found, visited_not_found;
3551
3552 visited_found = htab_create_alloc (10,
3553 htab_hash_pointer, htab_eq_pointer,
3554 NULL, xcalloc, xfree);
3555 cleanup = make_cleanup_htab_delete (visited_found);
3556 visited_not_found = htab_create_alloc (10,
3557 htab_hash_pointer, htab_eq_pointer,
3558 NULL, xcalloc, xfree);
3559 make_cleanup_htab_delete (visited_not_found);
3560
848e3e78
DE
3561 /* The rule is CUs specify all the files, including those used by
3562 any TU, so there's no need to scan TUs here. */
3563
3564 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3565 {
3566 int j;
f4dc4d17 3567 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3568 struct quick_file_names *file_data;
3569 void **slot;
7b08b9eb 3570
24c79950 3571 per_cu->v.quick->mark = 0;
3d7bb9d9 3572
24c79950
TT
3573 /* We only need to look at symtabs not already expanded. */
3574 if (per_cu->v.quick->symtab)
3575 continue;
7b08b9eb 3576
e4a48d9d 3577 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3578 if (file_data == NULL)
3579 continue;
7b08b9eb 3580
24c79950
TT
3581 if (htab_find (visited_not_found, file_data) != NULL)
3582 continue;
3583 else if (htab_find (visited_found, file_data) != NULL)
3584 {
3585 per_cu->v.quick->mark = 1;
3586 continue;
3587 }
3588
3589 for (j = 0; j < file_data->num_file_names; ++j)
3590 {
da235a7c
JK
3591 const char *this_real_name;
3592
fbd9ab74 3593 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3594 {
3595 per_cu->v.quick->mark = 1;
3596 break;
3597 }
da235a7c
JK
3598
3599 /* Before we invoke realpath, which can get expensive when many
3600 files are involved, do a quick comparison of the basenames. */
3601 if (!basenames_may_differ
3602 && !file_matcher (lbasename (file_data->file_names[j]),
3603 data, 1))
3604 continue;
3605
3606 this_real_name = dw2_get_real_path (objfile, file_data, j);
3607 if (file_matcher (this_real_name, data, 0))
3608 {
3609 per_cu->v.quick->mark = 1;
3610 break;
3611 }
24c79950
TT
3612 }
3613
3614 slot = htab_find_slot (per_cu->v.quick->mark
3615 ? visited_found
3616 : visited_not_found,
3617 file_data, INSERT);
3618 *slot = file_data;
3619 }
3620
3621 do_cleanups (cleanup);
3622 }
9291a0cd 3623
3876f04e 3624 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3625 {
3626 offset_type idx = 2 * iter;
3627 const char *name;
3628 offset_type *vec, vec_len, vec_idx;
3629
3876f04e 3630 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3631 continue;
3632
3876f04e 3633 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3634
e078317b 3635 if (! (*name_matcher) (name, data))
9291a0cd
TT
3636 continue;
3637
3638 /* The name was matched, now expand corresponding CUs that were
3639 marked. */
4b5246aa 3640 vec = (offset_type *) (index->constant_pool
3876f04e 3641 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3642 vec_len = MAYBE_SWAP (vec[0]);
3643 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3644 {
e254ef6a 3645 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3646 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3647 gdb_index_symbol_kind symbol_kind =
3648 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3649 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3650 /* Only check the symbol attributes if they're present.
3651 Indices prior to version 7 don't record them,
3652 and indices >= 7 may elide them for certain symbols
3653 (gold does this). */
3654 int attrs_valid =
3655 (index->version >= 7
3656 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3657
3658 /* Only check the symbol's kind if it has one. */
3659 if (attrs_valid)
156942c7
DE
3660 {
3661 switch (kind)
3662 {
3663 case VARIABLES_DOMAIN:
3664 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3665 continue;
3666 break;
3667 case FUNCTIONS_DOMAIN:
3668 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3669 continue;
3670 break;
3671 case TYPES_DOMAIN:
3672 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3673 continue;
3674 break;
3675 default:
3676 break;
3677 }
3678 }
3679
3190f0c6
DE
3680 /* Don't crash on bad data. */
3681 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3682 + dwarf2_per_objfile->n_type_units))
3683 {
3684 complaint (&symfile_complaints,
3685 _(".gdb_index entry has bad CU index"
3686 " [in module %s]"), objfile->name);
3687 continue;
3688 }
3689
156942c7 3690 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3691 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3692 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3693 }
3694 }
3695}
3696
9703b513
TT
3697/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3698 symtab. */
3699
3700static struct symtab *
3701recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3702{
3703 int i;
3704
3705 if (BLOCKVECTOR (symtab) != NULL
3706 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3707 return symtab;
3708
a3ec0bb1
DE
3709 if (symtab->includes == NULL)
3710 return NULL;
3711
9703b513
TT
3712 for (i = 0; symtab->includes[i]; ++i)
3713 {
a3ec0bb1 3714 struct symtab *s = symtab->includes[i];
9703b513
TT
3715
3716 s = recursively_find_pc_sect_symtab (s, pc);
3717 if (s != NULL)
3718 return s;
3719 }
3720
3721 return NULL;
3722}
3723
9291a0cd
TT
3724static struct symtab *
3725dw2_find_pc_sect_symtab (struct objfile *objfile,
3726 struct minimal_symbol *msymbol,
3727 CORE_ADDR pc,
3728 struct obj_section *section,
3729 int warn_if_readin)
3730{
3731 struct dwarf2_per_cu_data *data;
9703b513 3732 struct symtab *result;
9291a0cd
TT
3733
3734 dw2_setup (objfile);
3735
3736 if (!objfile->psymtabs_addrmap)
3737 return NULL;
3738
3739 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3740 if (!data)
3741 return NULL;
3742
3743 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3744 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3745 paddress (get_objfile_arch (objfile), pc));
3746
9703b513
TT
3747 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3748 gdb_assert (result != NULL);
3749 return result;
9291a0cd
TT
3750}
3751
9291a0cd 3752static void
44b13c5a 3753dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3754 void *data, int need_fullname)
9291a0cd
TT
3755{
3756 int i;
24c79950
TT
3757 struct cleanup *cleanup;
3758 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3759 NULL, xcalloc, xfree);
9291a0cd 3760
24c79950 3761 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3762 dw2_setup (objfile);
ae2de4f8 3763
848e3e78
DE
3764 /* The rule is CUs specify all the files, including those used by
3765 any TU, so there's no need to scan TUs here.
3766 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3767
848e3e78 3768 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3769 {
3770 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3771
3772 if (per_cu->v.quick->symtab)
3773 {
3774 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3775 INSERT);
3776
3777 *slot = per_cu->v.quick->file_names;
3778 }
3779 }
3780
848e3e78 3781 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3782 {
3783 int j;
f4dc4d17 3784 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3785 struct quick_file_names *file_data;
24c79950 3786 void **slot;
9291a0cd 3787
3d7bb9d9 3788 /* We only need to look at symtabs not already expanded. */
e254ef6a 3789 if (per_cu->v.quick->symtab)
9291a0cd
TT
3790 continue;
3791
e4a48d9d 3792 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3793 if (file_data == NULL)
9291a0cd
TT
3794 continue;
3795
24c79950
TT
3796 slot = htab_find_slot (visited, file_data, INSERT);
3797 if (*slot)
3798 {
3799 /* Already visited. */
3800 continue;
3801 }
3802 *slot = file_data;
3803
7b9f3c50 3804 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3805 {
74e2f255
DE
3806 const char *this_real_name;
3807
3808 if (need_fullname)
3809 this_real_name = dw2_get_real_path (objfile, file_data, j);
3810 else
3811 this_real_name = NULL;
7b9f3c50 3812 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3813 }
3814 }
24c79950
TT
3815
3816 do_cleanups (cleanup);
9291a0cd
TT
3817}
3818
3819static int
3820dw2_has_symbols (struct objfile *objfile)
3821{
3822 return 1;
3823}
3824
3825const struct quick_symbol_functions dwarf2_gdb_index_functions =
3826{
3827 dw2_has_symbols,
3828 dw2_find_last_source_symtab,
3829 dw2_forget_cached_source_info,
f8eba3c6 3830 dw2_map_symtabs_matching_filename,
9291a0cd 3831 dw2_lookup_symbol,
9291a0cd
TT
3832 dw2_print_stats,
3833 dw2_dump,
3834 dw2_relocate,
3835 dw2_expand_symtabs_for_function,
3836 dw2_expand_all_symtabs,
652a8996 3837 dw2_expand_symtabs_with_fullname,
9291a0cd 3838 dw2_find_symbol_file,
40658b94 3839 dw2_map_matching_symbols,
9291a0cd
TT
3840 dw2_expand_symtabs_matching,
3841 dw2_find_pc_sect_symtab,
9291a0cd
TT
3842 dw2_map_symbol_filenames
3843};
3844
3845/* Initialize for reading DWARF for this objfile. Return 0 if this
3846 file will use psymtabs, or 1 if using the GNU index. */
3847
3848int
3849dwarf2_initialize_objfile (struct objfile *objfile)
3850{
3851 /* If we're about to read full symbols, don't bother with the
3852 indices. In this case we also don't care if some other debug
3853 format is making psymtabs, because they are all about to be
3854 expanded anyway. */
3855 if ((objfile->flags & OBJF_READNOW))
3856 {
3857 int i;
3858
3859 dwarf2_per_objfile->using_index = 1;
3860 create_all_comp_units (objfile);
0e50663e 3861 create_all_type_units (objfile);
7b9f3c50
DE
3862 dwarf2_per_objfile->quick_file_names_table =
3863 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3864
1fd400ff 3865 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3866 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3867 {
e254ef6a 3868 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3869
e254ef6a
DE
3870 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3871 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3872 }
3873
3874 /* Return 1 so that gdb sees the "quick" functions. However,
3875 these functions will be no-ops because we will have expanded
3876 all symtabs. */
3877 return 1;
3878 }
3879
3880 if (dwarf2_read_index (objfile))
3881 return 1;
3882
9291a0cd
TT
3883 return 0;
3884}
3885
3886\f
3887
dce234bc
PP
3888/* Build a partial symbol table. */
3889
3890void
f29dff0a 3891dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3892{
c9bf0622
TT
3893 volatile struct gdb_exception except;
3894
f29dff0a 3895 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3896 {
3897 init_psymbol_list (objfile, 1024);
3898 }
3899
c9bf0622
TT
3900 TRY_CATCH (except, RETURN_MASK_ERROR)
3901 {
3902 /* This isn't really ideal: all the data we allocate on the
3903 objfile's obstack is still uselessly kept around. However,
3904 freeing it seems unsafe. */
3905 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3906
3907 dwarf2_build_psymtabs_hard (objfile);
3908 discard_cleanups (cleanups);
3909 }
3910 if (except.reason < 0)
3911 exception_print (gdb_stderr, except);
c906108c 3912}
c906108c 3913
1ce1cefd
DE
3914/* Return the total length of the CU described by HEADER. */
3915
3916static unsigned int
3917get_cu_length (const struct comp_unit_head *header)
3918{
3919 return header->initial_length_size + header->length;
3920}
3921
45452591
DE
3922/* Return TRUE if OFFSET is within CU_HEADER. */
3923
3924static inline int
b64f50a1 3925offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3926{
b64f50a1 3927 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3928 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3929
b64f50a1 3930 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3931}
3932
3b80fe9b
DE
3933/* Find the base address of the compilation unit for range lists and
3934 location lists. It will normally be specified by DW_AT_low_pc.
3935 In DWARF-3 draft 4, the base address could be overridden by
3936 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3937 compilation units with discontinuous ranges. */
3938
3939static void
3940dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3941{
3942 struct attribute *attr;
3943
3944 cu->base_known = 0;
3945 cu->base_address = 0;
3946
3947 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3948 if (attr)
3949 {
3950 cu->base_address = DW_ADDR (attr);
3951 cu->base_known = 1;
3952 }
3953 else
3954 {
3955 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3956 if (attr)
3957 {
3958 cu->base_address = DW_ADDR (attr);
3959 cu->base_known = 1;
3960 }
3961 }
3962}
3963
93311388
DE
3964/* Read in the comp unit header information from the debug_info at info_ptr.
3965 NOTE: This leaves members offset, first_die_offset to be filled in
3966 by the caller. */
107d2387 3967
d521ce57 3968static const gdb_byte *
107d2387 3969read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3970 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3971{
3972 int signed_addr;
891d2f0b 3973 unsigned int bytes_read;
c764a876
DE
3974
3975 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3976 cu_header->initial_length_size = bytes_read;
3977 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3978 info_ptr += bytes_read;
107d2387
AC
3979 cu_header->version = read_2_bytes (abfd, info_ptr);
3980 info_ptr += 2;
b64f50a1
JK
3981 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3982 &bytes_read);
613e1657 3983 info_ptr += bytes_read;
107d2387
AC
3984 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3985 info_ptr += 1;
3986 signed_addr = bfd_get_sign_extend_vma (abfd);
3987 if (signed_addr < 0)
8e65ff28 3988 internal_error (__FILE__, __LINE__,
e2e0b3e5 3989 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3990 cu_header->signed_addr_p = signed_addr;
c764a876 3991
107d2387
AC
3992 return info_ptr;
3993}
3994
36586728
TT
3995/* Helper function that returns the proper abbrev section for
3996 THIS_CU. */
3997
3998static struct dwarf2_section_info *
3999get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4000{
4001 struct dwarf2_section_info *abbrev;
4002
4003 if (this_cu->is_dwz)
4004 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4005 else
4006 abbrev = &dwarf2_per_objfile->abbrev;
4007
4008 return abbrev;
4009}
4010
9ff913ba
DE
4011/* Subroutine of read_and_check_comp_unit_head and
4012 read_and_check_type_unit_head to simplify them.
4013 Perform various error checking on the header. */
4014
4015static void
4016error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
4017 struct dwarf2_section_info *section,
4018 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
4019{
4020 bfd *abfd = section->asection->owner;
4021 const char *filename = bfd_get_filename (abfd);
4022
4023 if (header->version != 2 && header->version != 3 && header->version != 4)
4024 error (_("Dwarf Error: wrong version in compilation unit header "
4025 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
4026 filename);
4027
b64f50a1 4028 if (header->abbrev_offset.sect_off
36586728 4029 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
4030 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4031 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 4032 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
4033 filename);
4034
4035 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4036 avoid potential 32-bit overflow. */
1ce1cefd 4037 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4038 > section->size)
4039 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4040 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4041 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4042 filename);
4043}
4044
4045/* Read in a CU/TU header and perform some basic error checking.
4046 The contents of the header are stored in HEADER.
4047 The result is a pointer to the start of the first DIE. */
adabb602 4048
d521ce57 4049static const gdb_byte *
9ff913ba
DE
4050read_and_check_comp_unit_head (struct comp_unit_head *header,
4051 struct dwarf2_section_info *section,
4bdcc0c1 4052 struct dwarf2_section_info *abbrev_section,
d521ce57 4053 const gdb_byte *info_ptr,
9ff913ba 4054 int is_debug_types_section)
72bf9492 4055{
d521ce57 4056 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4057 bfd *abfd = section->asection->owner;
72bf9492 4058
b64f50a1 4059 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4060
72bf9492
DJ
4061 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4062
460c1c54
CC
4063 /* If we're reading a type unit, skip over the signature and
4064 type_offset fields. */
b0df02fd 4065 if (is_debug_types_section)
460c1c54
CC
4066 info_ptr += 8 /*signature*/ + header->offset_size;
4067
b64f50a1 4068 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4069
4bdcc0c1 4070 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4071
4072 return info_ptr;
4073}
4074
348e048f
DE
4075/* Read in the types comp unit header information from .debug_types entry at
4076 types_ptr. The result is a pointer to one past the end of the header. */
4077
d521ce57 4078static const gdb_byte *
9ff913ba
DE
4079read_and_check_type_unit_head (struct comp_unit_head *header,
4080 struct dwarf2_section_info *section,
4bdcc0c1 4081 struct dwarf2_section_info *abbrev_section,
d521ce57 4082 const gdb_byte *info_ptr,
dee91e82
DE
4083 ULONGEST *signature,
4084 cu_offset *type_offset_in_tu)
348e048f 4085{
d521ce57 4086 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4087 bfd *abfd = section->asection->owner;
348e048f 4088
b64f50a1 4089 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4090
9ff913ba 4091 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4092
9ff913ba
DE
4093 /* If we're reading a type unit, skip over the signature and
4094 type_offset fields. */
4095 if (signature != NULL)
4096 *signature = read_8_bytes (abfd, info_ptr);
4097 info_ptr += 8;
dee91e82
DE
4098 if (type_offset_in_tu != NULL)
4099 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4100 header->offset_size);
9ff913ba
DE
4101 info_ptr += header->offset_size;
4102
b64f50a1 4103 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4104
4bdcc0c1 4105 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4106
4107 return info_ptr;
348e048f
DE
4108}
4109
f4dc4d17
DE
4110/* Fetch the abbreviation table offset from a comp or type unit header. */
4111
4112static sect_offset
4113read_abbrev_offset (struct dwarf2_section_info *section,
4114 sect_offset offset)
4115{
4116 bfd *abfd = section->asection->owner;
d521ce57 4117 const gdb_byte *info_ptr;
f4dc4d17
DE
4118 unsigned int length, initial_length_size, offset_size;
4119 sect_offset abbrev_offset;
4120
4121 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4122 info_ptr = section->buffer + offset.sect_off;
4123 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4124 offset_size = initial_length_size == 4 ? 4 : 8;
4125 info_ptr += initial_length_size + 2 /*version*/;
4126 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4127 return abbrev_offset;
4128}
4129
aaa75496
JB
4130/* Allocate a new partial symtab for file named NAME and mark this new
4131 partial symtab as being an include of PST. */
4132
4133static void
d521ce57 4134dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4135 struct objfile *objfile)
4136{
4137 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4138
fbd9ab74
JK
4139 if (!IS_ABSOLUTE_PATH (subpst->filename))
4140 {
4141 /* It shares objfile->objfile_obstack. */
4142 subpst->dirname = pst->dirname;
4143 }
4144
aaa75496
JB
4145 subpst->section_offsets = pst->section_offsets;
4146 subpst->textlow = 0;
4147 subpst->texthigh = 0;
4148
4149 subpst->dependencies = (struct partial_symtab **)
4150 obstack_alloc (&objfile->objfile_obstack,
4151 sizeof (struct partial_symtab *));
4152 subpst->dependencies[0] = pst;
4153 subpst->number_of_dependencies = 1;
4154
4155 subpst->globals_offset = 0;
4156 subpst->n_global_syms = 0;
4157 subpst->statics_offset = 0;
4158 subpst->n_static_syms = 0;
4159 subpst->symtab = NULL;
4160 subpst->read_symtab = pst->read_symtab;
4161 subpst->readin = 0;
4162
4163 /* No private part is necessary for include psymtabs. This property
4164 can be used to differentiate between such include psymtabs and
10b3939b 4165 the regular ones. */
58a9656e 4166 subpst->read_symtab_private = NULL;
aaa75496
JB
4167}
4168
4169/* Read the Line Number Program data and extract the list of files
4170 included by the source file represented by PST. Build an include
d85a05f0 4171 partial symtab for each of these included files. */
aaa75496
JB
4172
4173static void
4174dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4175 struct die_info *die,
4176 struct partial_symtab *pst)
aaa75496 4177{
d85a05f0
DJ
4178 struct line_header *lh = NULL;
4179 struct attribute *attr;
aaa75496 4180
d85a05f0
DJ
4181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4182 if (attr)
3019eac3 4183 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4184 if (lh == NULL)
4185 return; /* No linetable, so no includes. */
4186
c6da4cef 4187 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4188 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4189
4190 free_line_header (lh);
4191}
4192
348e048f 4193static hashval_t
52dc124a 4194hash_signatured_type (const void *item)
348e048f 4195{
52dc124a 4196 const struct signatured_type *sig_type = item;
9a619af0 4197
348e048f 4198 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4199 return sig_type->signature;
348e048f
DE
4200}
4201
4202static int
52dc124a 4203eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4204{
4205 const struct signatured_type *lhs = item_lhs;
4206 const struct signatured_type *rhs = item_rhs;
9a619af0 4207
348e048f
DE
4208 return lhs->signature == rhs->signature;
4209}
4210
1fd400ff
TT
4211/* Allocate a hash table for signatured types. */
4212
4213static htab_t
673bfd45 4214allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4215{
4216 return htab_create_alloc_ex (41,
52dc124a
DE
4217 hash_signatured_type,
4218 eq_signatured_type,
1fd400ff
TT
4219 NULL,
4220 &objfile->objfile_obstack,
4221 hashtab_obstack_allocate,
4222 dummy_obstack_deallocate);
4223}
4224
d467dd73 4225/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4226
4227static int
d467dd73 4228add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4229{
4230 struct signatured_type *sigt = *slot;
b4dd5633 4231 struct signatured_type ***datap = datum;
1fd400ff 4232
b4dd5633 4233 **datap = sigt;
1fd400ff
TT
4234 ++*datap;
4235
4236 return 1;
4237}
4238
c88ee1f0
DE
4239/* Create the hash table of all entries in the .debug_types
4240 (or .debug_types.dwo) section(s).
4241 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4242 otherwise it is NULL.
4243
4244 The result is a pointer to the hash table or NULL if there are no types.
4245
4246 Note: This function processes DWO files only, not DWP files. */
348e048f 4247
3019eac3
DE
4248static htab_t
4249create_debug_types_hash_table (struct dwo_file *dwo_file,
4250 VEC (dwarf2_section_info_def) *types)
348e048f 4251{
3019eac3 4252 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4253 htab_t types_htab = NULL;
8b70b953
TT
4254 int ix;
4255 struct dwarf2_section_info *section;
4bdcc0c1 4256 struct dwarf2_section_info *abbrev_section;
348e048f 4257
3019eac3
DE
4258 if (VEC_empty (dwarf2_section_info_def, types))
4259 return NULL;
348e048f 4260
4bdcc0c1
DE
4261 abbrev_section = (dwo_file != NULL
4262 ? &dwo_file->sections.abbrev
4263 : &dwarf2_per_objfile->abbrev);
4264
09406207
DE
4265 if (dwarf2_read_debug)
4266 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4267 dwo_file ? ".dwo" : "",
4268 bfd_get_filename (abbrev_section->asection->owner));
4269
8b70b953 4270 for (ix = 0;
3019eac3 4271 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4272 ++ix)
4273 {
3019eac3 4274 bfd *abfd;
d521ce57 4275 const gdb_byte *info_ptr, *end_ptr;
36586728 4276 struct dwarf2_section_info *abbrev_section;
348e048f 4277
8b70b953
TT
4278 dwarf2_read_section (objfile, section);
4279 info_ptr = section->buffer;
348e048f 4280
8b70b953
TT
4281 if (info_ptr == NULL)
4282 continue;
348e048f 4283
3019eac3
DE
4284 /* We can't set abfd until now because the section may be empty or
4285 not present, in which case section->asection will be NULL. */
4286 abfd = section->asection->owner;
4287
36586728
TT
4288 if (dwo_file)
4289 abbrev_section = &dwo_file->sections.abbrev;
4290 else
4291 abbrev_section = &dwarf2_per_objfile->abbrev;
4292
dee91e82
DE
4293 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4294 because we don't need to read any dies: the signature is in the
4295 header. */
8b70b953
TT
4296
4297 end_ptr = info_ptr + section->size;
4298 while (info_ptr < end_ptr)
4299 {
b64f50a1 4300 sect_offset offset;
3019eac3 4301 cu_offset type_offset_in_tu;
8b70b953 4302 ULONGEST signature;
52dc124a 4303 struct signatured_type *sig_type;
3019eac3 4304 struct dwo_unit *dwo_tu;
8b70b953 4305 void **slot;
d521ce57 4306 const gdb_byte *ptr = info_ptr;
9ff913ba 4307 struct comp_unit_head header;
dee91e82 4308 unsigned int length;
348e048f 4309
b64f50a1 4310 offset.sect_off = ptr - section->buffer;
348e048f 4311
8b70b953 4312 /* We need to read the type's signature in order to build the hash
9ff913ba 4313 table, but we don't need anything else just yet. */
348e048f 4314
4bdcc0c1
DE
4315 ptr = read_and_check_type_unit_head (&header, section,
4316 abbrev_section, ptr,
3019eac3 4317 &signature, &type_offset_in_tu);
6caca83c 4318
1ce1cefd 4319 length = get_cu_length (&header);
dee91e82 4320
6caca83c 4321 /* Skip dummy type units. */
dee91e82
DE
4322 if (ptr >= info_ptr + length
4323 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4324 {
1ce1cefd 4325 info_ptr += length;
6caca83c
CC
4326 continue;
4327 }
8b70b953 4328
0349ea22
DE
4329 if (types_htab == NULL)
4330 {
4331 if (dwo_file)
4332 types_htab = allocate_dwo_unit_table (objfile);
4333 else
4334 types_htab = allocate_signatured_type_table (objfile);
4335 }
4336
3019eac3
DE
4337 if (dwo_file)
4338 {
4339 sig_type = NULL;
4340 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4341 struct dwo_unit);
4342 dwo_tu->dwo_file = dwo_file;
4343 dwo_tu->signature = signature;
4344 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4345 dwo_tu->section = section;
3019eac3
DE
4346 dwo_tu->offset = offset;
4347 dwo_tu->length = length;
4348 }
4349 else
4350 {
4351 /* N.B.: type_offset is not usable if this type uses a DWO file.
4352 The real type_offset is in the DWO file. */
4353 dwo_tu = NULL;
4354 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4355 struct signatured_type);
4356 sig_type->signature = signature;
4357 sig_type->type_offset_in_tu = type_offset_in_tu;
4358 sig_type->per_cu.objfile = objfile;
4359 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4360 sig_type->per_cu.section = section;
3019eac3
DE
4361 sig_type->per_cu.offset = offset;
4362 sig_type->per_cu.length = length;
4363 }
8b70b953 4364
3019eac3
DE
4365 slot = htab_find_slot (types_htab,
4366 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4367 INSERT);
8b70b953
TT
4368 gdb_assert (slot != NULL);
4369 if (*slot != NULL)
4370 {
3019eac3
DE
4371 sect_offset dup_offset;
4372
4373 if (dwo_file)
4374 {
4375 const struct dwo_unit *dup_tu = *slot;
4376
4377 dup_offset = dup_tu->offset;
4378 }
4379 else
4380 {
4381 const struct signatured_type *dup_tu = *slot;
4382
4383 dup_offset = dup_tu->per_cu.offset;
4384 }
b3c8eb43 4385
8b70b953 4386 complaint (&symfile_complaints,
c88ee1f0 4387 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4388 " the entry at offset 0x%x, signature %s"),
3019eac3 4389 offset.sect_off, dup_offset.sect_off,
4031ecc5 4390 hex_string (signature));
8b70b953 4391 }
3019eac3 4392 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4393
09406207 4394 if (dwarf2_read_debug)
4031ecc5 4395 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4396 offset.sect_off,
4031ecc5 4397 hex_string (signature));
348e048f 4398
dee91e82 4399 info_ptr += length;
8b70b953 4400 }
348e048f
DE
4401 }
4402
3019eac3
DE
4403 return types_htab;
4404}
4405
4406/* Create the hash table of all entries in the .debug_types section,
4407 and initialize all_type_units.
4408 The result is zero if there is an error (e.g. missing .debug_types section),
4409 otherwise non-zero. */
4410
4411static int
4412create_all_type_units (struct objfile *objfile)
4413{
4414 htab_t types_htab;
b4dd5633 4415 struct signatured_type **iter;
3019eac3
DE
4416
4417 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4418 if (types_htab == NULL)
4419 {
4420 dwarf2_per_objfile->signatured_types = NULL;
4421 return 0;
4422 }
4423
348e048f
DE
4424 dwarf2_per_objfile->signatured_types = types_htab;
4425
d467dd73
DE
4426 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4427 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4428 = xmalloc (dwarf2_per_objfile->n_type_units
4429 * sizeof (struct signatured_type *));
d467dd73
DE
4430 iter = &dwarf2_per_objfile->all_type_units[0];
4431 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4432 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4433 == dwarf2_per_objfile->n_type_units);
1fd400ff 4434
348e048f
DE
4435 return 1;
4436}
4437
a2ce51a0
DE
4438/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4439 Fill in SIG_ENTRY with DWO_ENTRY. */
4440
4441static void
4442fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4443 struct signatured_type *sig_entry,
4444 struct dwo_unit *dwo_entry)
4445{
4446 sig_entry->per_cu.section = dwo_entry->section;
4447 sig_entry->per_cu.offset = dwo_entry->offset;
4448 sig_entry->per_cu.length = dwo_entry->length;
4449 sig_entry->per_cu.reading_dwo_directly = 1;
4450 sig_entry->per_cu.objfile = objfile;
4451 gdb_assert (! sig_entry->per_cu.queued);
4452 gdb_assert (sig_entry->per_cu.cu == NULL);
4453 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4454 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4455 gdb_assert (sig_entry->signature == dwo_entry->signature);
4456 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4457 gdb_assert (sig_entry->type_unit_group == NULL);
4458 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4459 sig_entry->dwo_unit = dwo_entry;
4460}
4461
4462/* Subroutine of lookup_signatured_type.
4463 Create the signatured_type data structure for a TU to be read in
4464 directly from a DWO file, bypassing the stub.
4465 We do this for the case where there is no DWP file and we're using
4466 .gdb_index: When reading a CU we want to stay in the DWO file containing
4467 that CU. Otherwise we could end up reading several other DWO files (due
4468 to comdat folding) to process the transitive closure of all the mentioned
4469 TUs, and that can be slow. The current DWO file will have every type
4470 signature that it needs.
4471 We only do this for .gdb_index because in the psymtab case we already have
4472 to read all the DWOs to build the type unit groups. */
4473
4474static struct signatured_type *
4475lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4476{
4477 struct objfile *objfile = dwarf2_per_objfile->objfile;
4478 struct dwo_file *dwo_file;
4479 struct dwo_unit find_dwo_entry, *dwo_entry;
4480 struct signatured_type find_sig_entry, *sig_entry;
4481
4482 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4483
4484 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4485 dwo_unit of the TU itself. */
4486 dwo_file = cu->dwo_unit->dwo_file;
4487
4488 /* We only ever need to read in one copy of a signatured type.
4489 Just use the global signatured_types array. If this is the first time
4490 we're reading this type, replace the recorded data from .gdb_index with
4491 this TU. */
4492
4493 if (dwarf2_per_objfile->signatured_types == NULL)
4494 return NULL;
4495 find_sig_entry.signature = sig;
4496 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4497 if (sig_entry == NULL)
4498 return NULL;
4499 /* Have we already tried to read this TU? */
4500 if (sig_entry->dwo_unit != NULL)
4501 return sig_entry;
4502
4503 /* Ok, this is the first time we're reading this TU. */
4504 if (dwo_file->tus == NULL)
4505 return NULL;
4506 find_dwo_entry.signature = sig;
4507 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4508 if (dwo_entry == NULL)
4509 return NULL;
4510
4511 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4512 return sig_entry;
4513}
4514
4515/* Subroutine of lookup_dwp_signatured_type.
4516 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4517
4518static struct signatured_type *
4519add_type_unit (ULONGEST sig)
4520{
4521 struct objfile *objfile = dwarf2_per_objfile->objfile;
4522 int n_type_units = dwarf2_per_objfile->n_type_units;
4523 struct signatured_type *sig_type;
4524 void **slot;
4525
4526 ++n_type_units;
4527 dwarf2_per_objfile->all_type_units =
4528 xrealloc (dwarf2_per_objfile->all_type_units,
4529 n_type_units * sizeof (struct signatured_type *));
4530 dwarf2_per_objfile->n_type_units = n_type_units;
4531 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4532 struct signatured_type);
4533 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4534 sig_type->signature = sig;
4535 sig_type->per_cu.is_debug_types = 1;
4536 sig_type->per_cu.v.quick =
4537 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4538 struct dwarf2_per_cu_quick_data);
4539 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4540 sig_type, INSERT);
4541 gdb_assert (*slot == NULL);
4542 *slot = sig_type;
4543 /* The rest of sig_type must be filled in by the caller. */
4544 return sig_type;
4545}
4546
4547/* Subroutine of lookup_signatured_type.
4548 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4549 then try the DWP file.
4550 Normally this "can't happen", but if there's a bug in signature
4551 generation and/or the DWP file is built incorrectly, it can happen.
4552 Using the type directly from the DWP file means we don't have the stub
4553 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4554 not critical. [Eventually the stub may go away for type units anyway.] */
4555
4556static struct signatured_type *
4557lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4558{
4559 struct objfile *objfile = dwarf2_per_objfile->objfile;
4560 struct dwp_file *dwp_file = get_dwp_file ();
4561 struct dwo_unit *dwo_entry;
4562 struct signatured_type find_sig_entry, *sig_entry;
4563
4564 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4565 gdb_assert (dwp_file != NULL);
4566
4567 if (dwarf2_per_objfile->signatured_types != NULL)
4568 {
4569 find_sig_entry.signature = sig;
4570 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4571 &find_sig_entry);
4572 if (sig_entry != NULL)
4573 return sig_entry;
4574 }
4575
4576 /* This is the "shouldn't happen" case.
4577 Try the DWP file and hope for the best. */
4578 if (dwp_file->tus == NULL)
4579 return NULL;
4580 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4581 sig, 1 /* is_debug_types */);
4582 if (dwo_entry == NULL)
4583 return NULL;
4584
4585 sig_entry = add_type_unit (sig);
4586 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4587
4588 /* The caller will signal a complaint if we return NULL.
4589 Here we don't return NULL but we still want to complain. */
4590 complaint (&symfile_complaints,
4591 _("Bad type signature %s referenced by %s at 0x%x,"
4592 " coping by using copy in DWP [in module %s]"),
4593 hex_string (sig),
4594 cu->per_cu->is_debug_types ? "TU" : "CU",
4595 cu->per_cu->offset.sect_off,
4596 objfile->name);
4597
4598 return sig_entry;
4599}
4600
380bca97 4601/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4602 Returns NULL if signature SIG is not present in the table.
4603 It is up to the caller to complain about this. */
348e048f
DE
4604
4605static struct signatured_type *
a2ce51a0 4606lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4607{
a2ce51a0
DE
4608 if (cu->dwo_unit
4609 && dwarf2_per_objfile->using_index)
4610 {
4611 /* We're in a DWO/DWP file, and we're using .gdb_index.
4612 These cases require special processing. */
4613 if (get_dwp_file () == NULL)
4614 return lookup_dwo_signatured_type (cu, sig);
4615 else
4616 return lookup_dwp_signatured_type (cu, sig);
4617 }
4618 else
4619 {
4620 struct signatured_type find_entry, *entry;
348e048f 4621
a2ce51a0
DE
4622 if (dwarf2_per_objfile->signatured_types == NULL)
4623 return NULL;
4624 find_entry.signature = sig;
4625 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4626 return entry;
4627 }
348e048f 4628}
42e7ad6c
DE
4629\f
4630/* Low level DIE reading support. */
348e048f 4631
d85a05f0
DJ
4632/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4633
4634static void
4635init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4636 struct dwarf2_cu *cu,
3019eac3
DE
4637 struct dwarf2_section_info *section,
4638 struct dwo_file *dwo_file)
d85a05f0 4639{
fceca515 4640 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4641 reader->abfd = section->asection->owner;
d85a05f0 4642 reader->cu = cu;
3019eac3 4643 reader->dwo_file = dwo_file;
dee91e82
DE
4644 reader->die_section = section;
4645 reader->buffer = section->buffer;
f664829e 4646 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4647 reader->comp_dir = NULL;
d85a05f0
DJ
4648}
4649
b0c7bfa9
DE
4650/* Subroutine of init_cutu_and_read_dies to simplify it.
4651 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4652 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4653 already.
4654
4655 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4656 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4657 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4658 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4659 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4660 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4661 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4662 are filled in with the info of the DIE from the DWO file.
4663 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4664 provided an abbrev table to use.
4665 The result is non-zero if a valid (non-dummy) DIE was found. */
4666
4667static int
4668read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4669 struct dwo_unit *dwo_unit,
4670 int abbrev_table_provided,
4671 struct die_info *stub_comp_unit_die,
a2ce51a0 4672 const char *stub_comp_dir,
b0c7bfa9 4673 struct die_reader_specs *result_reader,
d521ce57 4674 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4675 struct die_info **result_comp_unit_die,
4676 int *result_has_children)
4677{
4678 struct objfile *objfile = dwarf2_per_objfile->objfile;
4679 struct dwarf2_cu *cu = this_cu->cu;
4680 struct dwarf2_section_info *section;
4681 bfd *abfd;
d521ce57 4682 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4683 const char *comp_dir_string;
4684 ULONGEST signature; /* Or dwo_id. */
4685 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4686 int i,num_extra_attrs;
4687 struct dwarf2_section_info *dwo_abbrev_section;
4688 struct attribute *attr;
a2ce51a0 4689 struct attribute comp_dir_attr;
b0c7bfa9
DE
4690 struct die_info *comp_unit_die;
4691
a2ce51a0
DE
4692 /* Both can't be provided. */
4693 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4694
b0c7bfa9
DE
4695 /* These attributes aren't processed until later:
4696 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4697 However, the attribute is found in the stub which we won't have later.
4698 In order to not impose this complication on the rest of the code,
4699 we read them here and copy them to the DWO CU/TU die. */
4700
4701 stmt_list = NULL;
4702 low_pc = NULL;
4703 high_pc = NULL;
4704 ranges = NULL;
4705 comp_dir = NULL;
4706
4707 if (stub_comp_unit_die != NULL)
4708 {
4709 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4710 DWO file. */
4711 if (! this_cu->is_debug_types)
4712 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4713 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4714 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4715 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4716 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4717
4718 /* There should be a DW_AT_addr_base attribute here (if needed).
4719 We need the value before we can process DW_FORM_GNU_addr_index. */
4720 cu->addr_base = 0;
4721 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4722 if (attr)
4723 cu->addr_base = DW_UNSND (attr);
4724
4725 /* There should be a DW_AT_ranges_base attribute here (if needed).
4726 We need the value before we can process DW_AT_ranges. */
4727 cu->ranges_base = 0;
4728 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4729 if (attr)
4730 cu->ranges_base = DW_UNSND (attr);
4731 }
a2ce51a0
DE
4732 else if (stub_comp_dir != NULL)
4733 {
4734 /* Reconstruct the comp_dir attribute to simplify the code below. */
4735 comp_dir = (struct attribute *)
4736 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4737 comp_dir->name = DW_AT_comp_dir;
4738 comp_dir->form = DW_FORM_string;
4739 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4740 DW_STRING (comp_dir) = stub_comp_dir;
4741 }
b0c7bfa9
DE
4742
4743 /* Set up for reading the DWO CU/TU. */
4744 cu->dwo_unit = dwo_unit;
4745 section = dwo_unit->section;
4746 dwarf2_read_section (objfile, section);
4747 abfd = section->asection->owner;
4748 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4749 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4750 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4751
4752 if (this_cu->is_debug_types)
4753 {
4754 ULONGEST header_signature;
4755 cu_offset type_offset_in_tu;
4756 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4757
4758 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4759 dwo_abbrev_section,
4760 info_ptr,
4761 &header_signature,
4762 &type_offset_in_tu);
a2ce51a0
DE
4763 /* This is not an assert because it can be caused by bad debug info. */
4764 if (sig_type->signature != header_signature)
4765 {
4766 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4767 " TU at offset 0x%x [in module %s]"),
4768 hex_string (sig_type->signature),
4769 hex_string (header_signature),
4770 dwo_unit->offset.sect_off,
4771 bfd_get_filename (abfd));
4772 }
b0c7bfa9
DE
4773 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4774 /* For DWOs coming from DWP files, we don't know the CU length
4775 nor the type's offset in the TU until now. */
4776 dwo_unit->length = get_cu_length (&cu->header);
4777 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4778
4779 /* Establish the type offset that can be used to lookup the type.
4780 For DWO files, we don't know it until now. */
4781 sig_type->type_offset_in_section.sect_off =
4782 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4783 }
4784 else
4785 {
4786 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4787 dwo_abbrev_section,
4788 info_ptr, 0);
4789 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4790 /* For DWOs coming from DWP files, we don't know the CU length
4791 until now. */
4792 dwo_unit->length = get_cu_length (&cu->header);
4793 }
4794
02142a6c
DE
4795 /* Replace the CU's original abbrev table with the DWO's.
4796 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4797 if (abbrev_table_provided)
4798 {
4799 /* Don't free the provided abbrev table, the caller of
4800 init_cutu_and_read_dies owns it. */
4801 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4802 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4803 make_cleanup (dwarf2_free_abbrev_table, cu);
4804 }
4805 else
4806 {
4807 dwarf2_free_abbrev_table (cu);
4808 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4809 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4810 }
4811
4812 /* Read in the die, but leave space to copy over the attributes
4813 from the stub. This has the benefit of simplifying the rest of
4814 the code - all the work to maintain the illusion of a single
4815 DW_TAG_{compile,type}_unit DIE is done here. */
4816 num_extra_attrs = ((stmt_list != NULL)
4817 + (low_pc != NULL)
4818 + (high_pc != NULL)
4819 + (ranges != NULL)
4820 + (comp_dir != NULL));
4821 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4822 result_has_children, num_extra_attrs);
4823
4824 /* Copy over the attributes from the stub to the DIE we just read in. */
4825 comp_unit_die = *result_comp_unit_die;
4826 i = comp_unit_die->num_attrs;
4827 if (stmt_list != NULL)
4828 comp_unit_die->attrs[i++] = *stmt_list;
4829 if (low_pc != NULL)
4830 comp_unit_die->attrs[i++] = *low_pc;
4831 if (high_pc != NULL)
4832 comp_unit_die->attrs[i++] = *high_pc;
4833 if (ranges != NULL)
4834 comp_unit_die->attrs[i++] = *ranges;
4835 if (comp_dir != NULL)
4836 comp_unit_die->attrs[i++] = *comp_dir;
4837 comp_unit_die->num_attrs += num_extra_attrs;
4838
bf6af496
DE
4839 if (dwarf2_die_debug)
4840 {
4841 fprintf_unfiltered (gdb_stdlog,
4842 "Read die from %s@0x%x of %s:\n",
4843 bfd_section_name (abfd, section->asection),
4844 (unsigned) (begin_info_ptr - section->buffer),
4845 bfd_get_filename (abfd));
4846 dump_die (comp_unit_die, dwarf2_die_debug);
4847 }
4848
a2ce51a0
DE
4849 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4850 TUs by skipping the stub and going directly to the entry in the DWO file.
4851 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4852 to get it via circuitous means. Blech. */
4853 if (comp_dir != NULL)
4854 result_reader->comp_dir = DW_STRING (comp_dir);
4855
b0c7bfa9
DE
4856 /* Skip dummy compilation units. */
4857 if (info_ptr >= begin_info_ptr + dwo_unit->length
4858 || peek_abbrev_code (abfd, info_ptr) == 0)
4859 return 0;
4860
4861 *result_info_ptr = info_ptr;
4862 return 1;
4863}
4864
4865/* Subroutine of init_cutu_and_read_dies to simplify it.
4866 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4867 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4868
4869static struct dwo_unit *
4870lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4871 struct die_info *comp_unit_die)
4872{
4873 struct dwarf2_cu *cu = this_cu->cu;
4874 struct attribute *attr;
4875 ULONGEST signature;
4876 struct dwo_unit *dwo_unit;
4877 const char *comp_dir, *dwo_name;
4878
a2ce51a0
DE
4879 gdb_assert (cu != NULL);
4880
b0c7bfa9
DE
4881 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4882 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4883 gdb_assert (attr != NULL);
4884 dwo_name = DW_STRING (attr);
4885 comp_dir = NULL;
4886 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4887 if (attr)
4888 comp_dir = DW_STRING (attr);
4889
4890 if (this_cu->is_debug_types)
4891 {
4892 struct signatured_type *sig_type;
4893
4894 /* Since this_cu is the first member of struct signatured_type,
4895 we can go from a pointer to one to a pointer to the other. */
4896 sig_type = (struct signatured_type *) this_cu;
4897 signature = sig_type->signature;
4898 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4899 }
4900 else
4901 {
4902 struct attribute *attr;
4903
4904 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4905 if (! attr)
4906 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4907 " [in module %s]"),
4908 dwo_name, this_cu->objfile->name);
4909 signature = DW_UNSND (attr);
4910 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4911 signature);
4912 }
4913
b0c7bfa9
DE
4914 return dwo_unit;
4915}
4916
a2ce51a0
DE
4917/* Subroutine of init_cutu_and_read_dies to simplify it.
4918 Read a TU directly from a DWO file, bypassing the stub. */
4919
4920static void
4921init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4922 die_reader_func_ftype *die_reader_func,
4923 void *data)
4924{
4925 struct dwarf2_cu *cu;
4926 struct signatured_type *sig_type;
4927 struct cleanup *cleanups, *free_cu_cleanup;
4928 struct die_reader_specs reader;
4929 const gdb_byte *info_ptr;
4930 struct die_info *comp_unit_die;
4931 int has_children;
4932
4933 /* Verify we can do the following downcast, and that we have the
4934 data we need. */
4935 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4936 sig_type = (struct signatured_type *) this_cu;
4937 gdb_assert (sig_type->dwo_unit != NULL);
4938
4939 cleanups = make_cleanup (null_cleanup, NULL);
4940
4941 gdb_assert (this_cu->cu == NULL);
4942 cu = xmalloc (sizeof (*cu));
4943 init_one_comp_unit (cu, this_cu);
4944 /* If an error occurs while loading, release our storage. */
4945 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4946
4947 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4948 0 /* abbrev_table_provided */,
4949 NULL /* stub_comp_unit_die */,
4950 sig_type->dwo_unit->dwo_file->comp_dir,
4951 &reader, &info_ptr,
4952 &comp_unit_die, &has_children) == 0)
4953 {
4954 /* Dummy die. */
4955 do_cleanups (cleanups);
4956 return;
4957 }
4958
4959 /* All the "real" work is done here. */
4960 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4961
4962 /* This duplicates some code in init_cutu_and_read_dies,
4963 but the alternative is making the latter more complex.
4964 This function is only for the special case of using DWO files directly:
4965 no point in overly complicating the general case just to handle this. */
4966 if (keep)
4967 {
4968 /* We've successfully allocated this compilation unit. Let our
4969 caller clean it up when finished with it. */
4970 discard_cleanups (free_cu_cleanup);
4971
4972 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4973 So we have to manually free the abbrev table. */
4974 dwarf2_free_abbrev_table (cu);
4975
4976 /* Link this CU into read_in_chain. */
4977 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4978 dwarf2_per_objfile->read_in_chain = this_cu;
4979 }
4980 else
4981 do_cleanups (free_cu_cleanup);
4982
4983 do_cleanups (cleanups);
4984}
4985
fd820528 4986/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4987 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4988
f4dc4d17
DE
4989 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4990 Otherwise the table specified in the comp unit header is read in and used.
4991 This is an optimization for when we already have the abbrev table.
4992
dee91e82
DE
4993 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4994 Otherwise, a new CU is allocated with xmalloc.
4995
4996 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4997 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4998
4999 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 5000 linker) then DIE_READER_FUNC will not get called. */
aaa75496 5001
70221824 5002static void
fd820528 5003init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 5004 struct abbrev_table *abbrev_table,
fd820528
DE
5005 int use_existing_cu, int keep,
5006 die_reader_func_ftype *die_reader_func,
5007 void *data)
c906108c 5008{
dee91e82 5009 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5010 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5011 bfd *abfd = section->asection->owner;
dee91e82 5012 struct dwarf2_cu *cu;
d521ce57 5013 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 5014 struct die_reader_specs reader;
d85a05f0 5015 struct die_info *comp_unit_die;
dee91e82 5016 int has_children;
d85a05f0 5017 struct attribute *attr;
365156ad 5018 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 5019 struct signatured_type *sig_type = NULL;
4bdcc0c1 5020 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
5021 /* Non-zero if CU currently points to a DWO file and we need to
5022 reread it. When this happens we need to reread the skeleton die
a2ce51a0 5023 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 5024 int rereading_dwo_cu = 0;
c906108c 5025
09406207
DE
5026 if (dwarf2_die_debug)
5027 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5028 this_cu->is_debug_types ? "type" : "comp",
5029 this_cu->offset.sect_off);
5030
dee91e82
DE
5031 if (use_existing_cu)
5032 gdb_assert (keep);
23745b47 5033
a2ce51a0
DE
5034 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5035 file (instead of going through the stub), short-circuit all of this. */
5036 if (this_cu->reading_dwo_directly)
5037 {
5038 /* Narrow down the scope of possibilities to have to understand. */
5039 gdb_assert (this_cu->is_debug_types);
5040 gdb_assert (abbrev_table == NULL);
5041 gdb_assert (!use_existing_cu);
5042 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5043 return;
5044 }
5045
dee91e82
DE
5046 cleanups = make_cleanup (null_cleanup, NULL);
5047
5048 /* This is cheap if the section is already read in. */
5049 dwarf2_read_section (objfile, section);
5050
5051 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5052
5053 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5054
5055 if (use_existing_cu && this_cu->cu != NULL)
5056 {
5057 cu = this_cu->cu;
42e7ad6c
DE
5058
5059 /* If this CU is from a DWO file we need to start over, we need to
5060 refetch the attributes from the skeleton CU.
5061 This could be optimized by retrieving those attributes from when we
5062 were here the first time: the previous comp_unit_die was stored in
5063 comp_unit_obstack. But there's no data yet that we need this
5064 optimization. */
5065 if (cu->dwo_unit != NULL)
5066 rereading_dwo_cu = 1;
dee91e82
DE
5067 }
5068 else
5069 {
5070 /* If !use_existing_cu, this_cu->cu must be NULL. */
5071 gdb_assert (this_cu->cu == NULL);
5072
5073 cu = xmalloc (sizeof (*cu));
5074 init_one_comp_unit (cu, this_cu);
5075
5076 /* If an error occurs while loading, release our storage. */
365156ad 5077 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5078 }
dee91e82 5079
b0c7bfa9 5080 /* Get the header. */
42e7ad6c
DE
5081 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5082 {
5083 /* We already have the header, there's no need to read it in again. */
5084 info_ptr += cu->header.first_die_offset.cu_off;
5085 }
5086 else
5087 {
3019eac3 5088 if (this_cu->is_debug_types)
dee91e82
DE
5089 {
5090 ULONGEST signature;
42e7ad6c 5091 cu_offset type_offset_in_tu;
dee91e82 5092
4bdcc0c1
DE
5093 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5094 abbrev_section, info_ptr,
42e7ad6c
DE
5095 &signature,
5096 &type_offset_in_tu);
dee91e82 5097
42e7ad6c
DE
5098 /* Since per_cu is the first member of struct signatured_type,
5099 we can go from a pointer to one to a pointer to the other. */
5100 sig_type = (struct signatured_type *) this_cu;
5101 gdb_assert (sig_type->signature == signature);
5102 gdb_assert (sig_type->type_offset_in_tu.cu_off
5103 == type_offset_in_tu.cu_off);
dee91e82
DE
5104 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5105
42e7ad6c
DE
5106 /* LENGTH has not been set yet for type units if we're
5107 using .gdb_index. */
1ce1cefd 5108 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5109
5110 /* Establish the type offset that can be used to lookup the type. */
5111 sig_type->type_offset_in_section.sect_off =
5112 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5113 }
5114 else
5115 {
4bdcc0c1
DE
5116 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5117 abbrev_section,
5118 info_ptr, 0);
dee91e82
DE
5119
5120 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5121 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5122 }
5123 }
10b3939b 5124
6caca83c 5125 /* Skip dummy compilation units. */
dee91e82 5126 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5127 || peek_abbrev_code (abfd, info_ptr) == 0)
5128 {
dee91e82 5129 do_cleanups (cleanups);
21b2bd31 5130 return;
6caca83c
CC
5131 }
5132
433df2d4
DE
5133 /* If we don't have them yet, read the abbrevs for this compilation unit.
5134 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5135 done. Note that it's important that if the CU had an abbrev table
5136 on entry we don't free it when we're done: Somewhere up the call stack
5137 it may be in use. */
f4dc4d17
DE
5138 if (abbrev_table != NULL)
5139 {
5140 gdb_assert (cu->abbrev_table == NULL);
5141 gdb_assert (cu->header.abbrev_offset.sect_off
5142 == abbrev_table->offset.sect_off);
5143 cu->abbrev_table = abbrev_table;
5144 }
5145 else if (cu->abbrev_table == NULL)
dee91e82 5146 {
4bdcc0c1 5147 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5148 make_cleanup (dwarf2_free_abbrev_table, cu);
5149 }
42e7ad6c
DE
5150 else if (rereading_dwo_cu)
5151 {
5152 dwarf2_free_abbrev_table (cu);
5153 dwarf2_read_abbrevs (cu, abbrev_section);
5154 }
af703f96 5155
dee91e82 5156 /* Read the top level CU/TU die. */
3019eac3 5157 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5158 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5159
b0c7bfa9
DE
5160 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5161 from the DWO file.
5162 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5163 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5164 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5165 if (attr)
5166 {
3019eac3 5167 struct dwo_unit *dwo_unit;
b0c7bfa9 5168 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5169
5170 if (has_children)
6a506a2d
DE
5171 {
5172 complaint (&symfile_complaints,
5173 _("compilation unit with DW_AT_GNU_dwo_name"
5174 " has children (offset 0x%x) [in module %s]"),
5175 this_cu->offset.sect_off, bfd_get_filename (abfd));
5176 }
b0c7bfa9 5177 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5178 if (dwo_unit != NULL)
3019eac3 5179 {
6a506a2d
DE
5180 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5181 abbrev_table != NULL,
a2ce51a0 5182 comp_unit_die, NULL,
6a506a2d
DE
5183 &reader, &info_ptr,
5184 &dwo_comp_unit_die, &has_children) == 0)
5185 {
5186 /* Dummy die. */
5187 do_cleanups (cleanups);
5188 return;
5189 }
5190 comp_unit_die = dwo_comp_unit_die;
5191 }
5192 else
5193 {
5194 /* Yikes, we couldn't find the rest of the DIE, we only have
5195 the stub. A complaint has already been logged. There's
5196 not much more we can do except pass on the stub DIE to
5197 die_reader_func. We don't want to throw an error on bad
5198 debug info. */
3019eac3
DE
5199 }
5200 }
5201
b0c7bfa9 5202 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5203 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5204
b0c7bfa9 5205 /* Done, clean up. */
365156ad 5206 if (free_cu_cleanup != NULL)
348e048f 5207 {
365156ad
TT
5208 if (keep)
5209 {
5210 /* We've successfully allocated this compilation unit. Let our
5211 caller clean it up when finished with it. */
5212 discard_cleanups (free_cu_cleanup);
dee91e82 5213
365156ad
TT
5214 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5215 So we have to manually free the abbrev table. */
5216 dwarf2_free_abbrev_table (cu);
dee91e82 5217
365156ad
TT
5218 /* Link this CU into read_in_chain. */
5219 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5220 dwarf2_per_objfile->read_in_chain = this_cu;
5221 }
5222 else
5223 do_cleanups (free_cu_cleanup);
348e048f 5224 }
365156ad
TT
5225
5226 do_cleanups (cleanups);
dee91e82
DE
5227}
5228
3019eac3
DE
5229/* Read CU/TU THIS_CU in section SECTION,
5230 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5231 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5232 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5233
5234 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5235 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5236
5237 We fill in THIS_CU->length.
5238
5239 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5240 linker) then DIE_READER_FUNC will not get called.
5241
5242 THIS_CU->cu is always freed when done.
3019eac3
DE
5243 This is done in order to not leave THIS_CU->cu in a state where we have
5244 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5245
5246static void
5247init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5248 struct dwarf2_section_info *abbrev_section,
3019eac3 5249 struct dwo_file *dwo_file,
dee91e82
DE
5250 die_reader_func_ftype *die_reader_func,
5251 void *data)
5252{
5253 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5254 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5255 bfd *abfd = section->asection->owner;
dee91e82 5256 struct dwarf2_cu cu;
d521ce57 5257 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5258 struct die_reader_specs reader;
5259 struct cleanup *cleanups;
5260 struct die_info *comp_unit_die;
5261 int has_children;
5262
09406207
DE
5263 if (dwarf2_die_debug)
5264 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5265 this_cu->is_debug_types ? "type" : "comp",
5266 this_cu->offset.sect_off);
5267
dee91e82
DE
5268 gdb_assert (this_cu->cu == NULL);
5269
dee91e82
DE
5270 /* This is cheap if the section is already read in. */
5271 dwarf2_read_section (objfile, section);
5272
5273 init_one_comp_unit (&cu, this_cu);
5274
5275 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5276
5277 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5278 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5279 abbrev_section, info_ptr,
3019eac3 5280 this_cu->is_debug_types);
dee91e82 5281
1ce1cefd 5282 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5283
5284 /* Skip dummy compilation units. */
5285 if (info_ptr >= begin_info_ptr + this_cu->length
5286 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5287 {
dee91e82 5288 do_cleanups (cleanups);
21b2bd31 5289 return;
93311388 5290 }
72bf9492 5291
dee91e82
DE
5292 dwarf2_read_abbrevs (&cu, abbrev_section);
5293 make_cleanup (dwarf2_free_abbrev_table, &cu);
5294
3019eac3 5295 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5296 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5297
5298 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5299
5300 do_cleanups (cleanups);
5301}
5302
3019eac3
DE
5303/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5304 does not lookup the specified DWO file.
5305 This cannot be used to read DWO files.
dee91e82
DE
5306
5307 THIS_CU->cu is always freed when done.
3019eac3
DE
5308 This is done in order to not leave THIS_CU->cu in a state where we have
5309 to care whether it refers to the "main" CU or the DWO CU.
5310 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5311
5312static void
5313init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5314 die_reader_func_ftype *die_reader_func,
5315 void *data)
5316{
5317 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5318 get_abbrev_section_for_cu (this_cu),
3019eac3 5319 NULL,
dee91e82
DE
5320 die_reader_func, data);
5321}
0018ea6f
DE
5322\f
5323/* Type Unit Groups.
dee91e82 5324
0018ea6f
DE
5325 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5326 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5327 so that all types coming from the same compilation (.o file) are grouped
5328 together. A future step could be to put the types in the same symtab as
5329 the CU the types ultimately came from. */
ff013f42 5330
f4dc4d17
DE
5331static hashval_t
5332hash_type_unit_group (const void *item)
5333{
094b34ac 5334 const struct type_unit_group *tu_group = item;
f4dc4d17 5335
094b34ac 5336 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5337}
348e048f
DE
5338
5339static int
f4dc4d17 5340eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5341{
f4dc4d17
DE
5342 const struct type_unit_group *lhs = item_lhs;
5343 const struct type_unit_group *rhs = item_rhs;
348e048f 5344
094b34ac 5345 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5346}
348e048f 5347
f4dc4d17
DE
5348/* Allocate a hash table for type unit groups. */
5349
5350static htab_t
5351allocate_type_unit_groups_table (void)
5352{
5353 return htab_create_alloc_ex (3,
5354 hash_type_unit_group,
5355 eq_type_unit_group,
5356 NULL,
5357 &dwarf2_per_objfile->objfile->objfile_obstack,
5358 hashtab_obstack_allocate,
5359 dummy_obstack_deallocate);
5360}
dee91e82 5361
f4dc4d17
DE
5362/* Type units that don't have DW_AT_stmt_list are grouped into their own
5363 partial symtabs. We combine several TUs per psymtab to not let the size
5364 of any one psymtab grow too big. */
5365#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5366#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5367
094b34ac 5368/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5369 Create the type_unit_group object used to hold one or more TUs. */
5370
5371static struct type_unit_group *
094b34ac 5372create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5373{
5374 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5375 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5376 struct type_unit_group *tu_group;
f4dc4d17
DE
5377
5378 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5379 struct type_unit_group);
094b34ac 5380 per_cu = &tu_group->per_cu;
f4dc4d17 5381 per_cu->objfile = objfile;
f4dc4d17 5382
094b34ac
DE
5383 if (dwarf2_per_objfile->using_index)
5384 {
5385 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5386 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5387 }
5388 else
5389 {
5390 unsigned int line_offset = line_offset_struct.sect_off;
5391 struct partial_symtab *pst;
5392 char *name;
5393
5394 /* Give the symtab a useful name for debug purposes. */
5395 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5396 name = xstrprintf ("<type_units_%d>",
5397 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5398 else
5399 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5400
5401 pst = create_partial_symtab (per_cu, name);
5402 pst->anonymous = 1;
f4dc4d17 5403
094b34ac
DE
5404 xfree (name);
5405 }
f4dc4d17 5406
094b34ac
DE
5407 tu_group->hash.dwo_unit = cu->dwo_unit;
5408 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5409
5410 return tu_group;
5411}
5412
094b34ac
DE
5413/* Look up the type_unit_group for type unit CU, and create it if necessary.
5414 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5415
5416static struct type_unit_group *
ff39bb5e 5417get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5418{
5419 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5420 struct type_unit_group *tu_group;
5421 void **slot;
5422 unsigned int line_offset;
5423 struct type_unit_group type_unit_group_for_lookup;
5424
5425 if (dwarf2_per_objfile->type_unit_groups == NULL)
5426 {
5427 dwarf2_per_objfile->type_unit_groups =
5428 allocate_type_unit_groups_table ();
5429 }
5430
5431 /* Do we need to create a new group, or can we use an existing one? */
5432
5433 if (stmt_list)
5434 {
5435 line_offset = DW_UNSND (stmt_list);
5436 ++tu_stats->nr_symtab_sharers;
5437 }
5438 else
5439 {
5440 /* Ugh, no stmt_list. Rare, but we have to handle it.
5441 We can do various things here like create one group per TU or
5442 spread them over multiple groups to split up the expansion work.
5443 To avoid worst case scenarios (too many groups or too large groups)
5444 we, umm, group them in bunches. */
5445 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5446 | (tu_stats->nr_stmt_less_type_units
5447 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5448 ++tu_stats->nr_stmt_less_type_units;
5449 }
5450
094b34ac
DE
5451 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5452 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5453 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5454 &type_unit_group_for_lookup, INSERT);
5455 if (*slot != NULL)
5456 {
5457 tu_group = *slot;
5458 gdb_assert (tu_group != NULL);
5459 }
5460 else
5461 {
5462 sect_offset line_offset_struct;
5463
5464 line_offset_struct.sect_off = line_offset;
094b34ac 5465 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5466 *slot = tu_group;
5467 ++tu_stats->nr_symtabs;
5468 }
5469
5470 return tu_group;
5471}
5472
5473/* Struct used to sort TUs by their abbreviation table offset. */
5474
5475struct tu_abbrev_offset
5476{
5477 struct signatured_type *sig_type;
5478 sect_offset abbrev_offset;
5479};
5480
5481/* Helper routine for build_type_unit_groups, passed to qsort. */
5482
5483static int
5484sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5485{
5486 const struct tu_abbrev_offset * const *a = ap;
5487 const struct tu_abbrev_offset * const *b = bp;
5488 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5489 unsigned int boff = (*b)->abbrev_offset.sect_off;
5490
5491 return (aoff > boff) - (aoff < boff);
5492}
5493
5494/* A helper function to add a type_unit_group to a table. */
5495
5496static int
5497add_type_unit_group_to_table (void **slot, void *datum)
5498{
5499 struct type_unit_group *tu_group = *slot;
5500 struct type_unit_group ***datap = datum;
5501
5502 **datap = tu_group;
5503 ++*datap;
5504
5505 return 1;
5506}
5507
5508/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5509 each one passing FUNC,DATA.
5510
5511 The efficiency is because we sort TUs by the abbrev table they use and
5512 only read each abbrev table once. In one program there are 200K TUs
5513 sharing 8K abbrev tables.
5514
5515 The main purpose of this function is to support building the
5516 dwarf2_per_objfile->type_unit_groups table.
5517 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5518 can collapse the search space by grouping them by stmt_list.
5519 The savings can be significant, in the same program from above the 200K TUs
5520 share 8K stmt_list tables.
5521
5522 FUNC is expected to call get_type_unit_group, which will create the
5523 struct type_unit_group if necessary and add it to
5524 dwarf2_per_objfile->type_unit_groups. */
5525
5526static void
5527build_type_unit_groups (die_reader_func_ftype *func, void *data)
5528{
5529 struct objfile *objfile = dwarf2_per_objfile->objfile;
5530 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5531 struct cleanup *cleanups;
5532 struct abbrev_table *abbrev_table;
5533 sect_offset abbrev_offset;
5534 struct tu_abbrev_offset *sorted_by_abbrev;
5535 struct type_unit_group **iter;
5536 int i;
5537
5538 /* It's up to the caller to not call us multiple times. */
5539 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5540
5541 if (dwarf2_per_objfile->n_type_units == 0)
5542 return;
5543
5544 /* TUs typically share abbrev tables, and there can be way more TUs than
5545 abbrev tables. Sort by abbrev table to reduce the number of times we
5546 read each abbrev table in.
5547 Alternatives are to punt or to maintain a cache of abbrev tables.
5548 This is simpler and efficient enough for now.
5549
5550 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5551 symtab to use). Typically TUs with the same abbrev offset have the same
5552 stmt_list value too so in practice this should work well.
5553
5554 The basic algorithm here is:
5555
5556 sort TUs by abbrev table
5557 for each TU with same abbrev table:
5558 read abbrev table if first user
5559 read TU top level DIE
5560 [IWBN if DWO skeletons had DW_AT_stmt_list]
5561 call FUNC */
5562
5563 if (dwarf2_read_debug)
5564 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5565
5566 /* Sort in a separate table to maintain the order of all_type_units
5567 for .gdb_index: TU indices directly index all_type_units. */
5568 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5569 dwarf2_per_objfile->n_type_units);
5570 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5571 {
5572 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5573
5574 sorted_by_abbrev[i].sig_type = sig_type;
5575 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5576 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5577 sig_type->per_cu.offset);
5578 }
5579 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5580 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5581 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5582
094b34ac
DE
5583 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5584 called any number of times, so we don't reset tu_stats here. */
5585
f4dc4d17
DE
5586 abbrev_offset.sect_off = ~(unsigned) 0;
5587 abbrev_table = NULL;
5588 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5589
5590 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5591 {
5592 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5593
5594 /* Switch to the next abbrev table if necessary. */
5595 if (abbrev_table == NULL
5596 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5597 {
5598 if (abbrev_table != NULL)
5599 {
5600 abbrev_table_free (abbrev_table);
5601 /* Reset to NULL in case abbrev_table_read_table throws
5602 an error: abbrev_table_free_cleanup will get called. */
5603 abbrev_table = NULL;
5604 }
5605 abbrev_offset = tu->abbrev_offset;
5606 abbrev_table =
5607 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5608 abbrev_offset);
5609 ++tu_stats->nr_uniq_abbrev_tables;
5610 }
5611
5612 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5613 func, data);
5614 }
5615
a2ce51a0
DE
5616 /* type_unit_groups can be NULL if there is an error in the debug info.
5617 Just create an empty table so the rest of gdb doesn't have to watch
5618 for this error case. */
5619 if (dwarf2_per_objfile->type_unit_groups == NULL)
5620 {
5621 dwarf2_per_objfile->type_unit_groups =
5622 allocate_type_unit_groups_table ();
5623 dwarf2_per_objfile->n_type_unit_groups = 0;
5624 }
5625
f4dc4d17
DE
5626 /* Create a vector of pointers to primary type units to make it easy to
5627 iterate over them and CUs. See dw2_get_primary_cu. */
5628 dwarf2_per_objfile->n_type_unit_groups =
5629 htab_elements (dwarf2_per_objfile->type_unit_groups);
5630 dwarf2_per_objfile->all_type_unit_groups =
5631 obstack_alloc (&objfile->objfile_obstack,
5632 dwarf2_per_objfile->n_type_unit_groups
5633 * sizeof (struct type_unit_group *));
5634 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5635 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5636 add_type_unit_group_to_table, &iter);
5637 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5638 == dwarf2_per_objfile->n_type_unit_groups);
5639
5640 do_cleanups (cleanups);
5641
5642 if (dwarf2_read_debug)
5643 {
5644 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5645 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5646 dwarf2_per_objfile->n_type_units);
5647 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5648 tu_stats->nr_uniq_abbrev_tables);
5649 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5650 tu_stats->nr_symtabs);
5651 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5652 tu_stats->nr_symtab_sharers);
5653 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5654 tu_stats->nr_stmt_less_type_units);
5655 }
5656}
0018ea6f
DE
5657\f
5658/* Partial symbol tables. */
5659
5660/* Create a psymtab named NAME and assign it to PER_CU.
5661
5662 The caller must fill in the following details:
5663 dirname, textlow, texthigh. */
5664
5665static struct partial_symtab *
5666create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5667{
5668 struct objfile *objfile = per_cu->objfile;
5669 struct partial_symtab *pst;
5670
5671 pst = start_psymtab_common (objfile, objfile->section_offsets,
5672 name, 0,
5673 objfile->global_psymbols.next,
5674 objfile->static_psymbols.next);
5675
5676 pst->psymtabs_addrmap_supported = 1;
5677
5678 /* This is the glue that links PST into GDB's symbol API. */
5679 pst->read_symtab_private = per_cu;
5680 pst->read_symtab = dwarf2_read_symtab;
5681 per_cu->v.psymtab = pst;
5682
5683 return pst;
5684}
5685
5686/* die_reader_func for process_psymtab_comp_unit. */
5687
5688static void
5689process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5690 const gdb_byte *info_ptr,
0018ea6f
DE
5691 struct die_info *comp_unit_die,
5692 int has_children,
5693 void *data)
5694{
5695 struct dwarf2_cu *cu = reader->cu;
5696 struct objfile *objfile = cu->objfile;
5697 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5698 struct attribute *attr;
5699 CORE_ADDR baseaddr;
5700 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5701 struct partial_symtab *pst;
5702 int has_pc_info;
5703 const char *filename;
5704 int *want_partial_unit_ptr = data;
5705
5706 if (comp_unit_die->tag == DW_TAG_partial_unit
5707 && (want_partial_unit_ptr == NULL
5708 || !*want_partial_unit_ptr))
5709 return;
5710
5711 gdb_assert (! per_cu->is_debug_types);
5712
5713 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5714
5715 cu->list_in_scope = &file_symbols;
5716
5717 /* Allocate a new partial symbol table structure. */
5718 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5719 if (attr == NULL || !DW_STRING (attr))
5720 filename = "";
5721 else
5722 filename = DW_STRING (attr);
5723
5724 pst = create_partial_symtab (per_cu, filename);
5725
5726 /* This must be done before calling dwarf2_build_include_psymtabs. */
5727 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5728 if (attr != NULL)
5729 pst->dirname = DW_STRING (attr);
5730
5731 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5732
5733 dwarf2_find_base_address (comp_unit_die, cu);
5734
5735 /* Possibly set the default values of LOWPC and HIGHPC from
5736 `DW_AT_ranges'. */
5737 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5738 &best_highpc, cu, pst);
5739 if (has_pc_info == 1 && best_lowpc < best_highpc)
5740 /* Store the contiguous range if it is not empty; it can be empty for
5741 CUs with no code. */
5742 addrmap_set_empty (objfile->psymtabs_addrmap,
5743 best_lowpc + baseaddr,
5744 best_highpc + baseaddr - 1, pst);
5745
5746 /* Check if comp unit has_children.
5747 If so, read the rest of the partial symbols from this comp unit.
5748 If not, there's no more debug_info for this comp unit. */
5749 if (has_children)
5750 {
5751 struct partial_die_info *first_die;
5752 CORE_ADDR lowpc, highpc;
5753
5754 lowpc = ((CORE_ADDR) -1);
5755 highpc = ((CORE_ADDR) 0);
5756
5757 first_die = load_partial_dies (reader, info_ptr, 1);
5758
5759 scan_partial_symbols (first_die, &lowpc, &highpc,
5760 ! has_pc_info, cu);
5761
5762 /* If we didn't find a lowpc, set it to highpc to avoid
5763 complaints from `maint check'. */
5764 if (lowpc == ((CORE_ADDR) -1))
5765 lowpc = highpc;
5766
5767 /* If the compilation unit didn't have an explicit address range,
5768 then use the information extracted from its child dies. */
5769 if (! has_pc_info)
5770 {
5771 best_lowpc = lowpc;
5772 best_highpc = highpc;
5773 }
5774 }
5775 pst->textlow = best_lowpc + baseaddr;
5776 pst->texthigh = best_highpc + baseaddr;
5777
5778 pst->n_global_syms = objfile->global_psymbols.next -
5779 (objfile->global_psymbols.list + pst->globals_offset);
5780 pst->n_static_syms = objfile->static_psymbols.next -
5781 (objfile->static_psymbols.list + pst->statics_offset);
5782 sort_pst_symbols (objfile, pst);
5783
5784 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5785 {
5786 int i;
5787 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5788 struct dwarf2_per_cu_data *iter;
5789
5790 /* Fill in 'dependencies' here; we fill in 'users' in a
5791 post-pass. */
5792 pst->number_of_dependencies = len;
5793 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5794 len * sizeof (struct symtab *));
5795 for (i = 0;
5796 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5797 i, iter);
5798 ++i)
5799 pst->dependencies[i] = iter->v.psymtab;
5800
5801 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5802 }
5803
5804 /* Get the list of files included in the current compilation unit,
5805 and build a psymtab for each of them. */
5806 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5807
5808 if (dwarf2_read_debug)
5809 {
5810 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5811
5812 fprintf_unfiltered (gdb_stdlog,
5813 "Psymtab for %s unit @0x%x: %s - %s"
5814 ", %d global, %d static syms\n",
5815 per_cu->is_debug_types ? "type" : "comp",
5816 per_cu->offset.sect_off,
5817 paddress (gdbarch, pst->textlow),
5818 paddress (gdbarch, pst->texthigh),
5819 pst->n_global_syms, pst->n_static_syms);
5820 }
5821}
5822
5823/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5824 Process compilation unit THIS_CU for a psymtab. */
5825
5826static void
5827process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5828 int want_partial_unit)
5829{
5830 /* If this compilation unit was already read in, free the
5831 cached copy in order to read it in again. This is
5832 necessary because we skipped some symbols when we first
5833 read in the compilation unit (see load_partial_dies).
5834 This problem could be avoided, but the benefit is unclear. */
5835 if (this_cu->cu != NULL)
5836 free_one_cached_comp_unit (this_cu);
5837
5838 gdb_assert (! this_cu->is_debug_types);
5839 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5840 process_psymtab_comp_unit_reader,
5841 &want_partial_unit);
5842
5843 /* Age out any secondary CUs. */
5844 age_cached_comp_units ();
5845}
f4dc4d17
DE
5846
5847/* Reader function for build_type_psymtabs. */
5848
5849static void
5850build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5851 const gdb_byte *info_ptr,
f4dc4d17
DE
5852 struct die_info *type_unit_die,
5853 int has_children,
5854 void *data)
5855{
5856 struct objfile *objfile = dwarf2_per_objfile->objfile;
5857 struct dwarf2_cu *cu = reader->cu;
5858 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5859 struct signatured_type *sig_type;
f4dc4d17
DE
5860 struct type_unit_group *tu_group;
5861 struct attribute *attr;
5862 struct partial_die_info *first_die;
5863 CORE_ADDR lowpc, highpc;
5864 struct partial_symtab *pst;
5865
5866 gdb_assert (data == NULL);
0186c6a7
DE
5867 gdb_assert (per_cu->is_debug_types);
5868 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5869
5870 if (! has_children)
5871 return;
5872
5873 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5874 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5875
0186c6a7 5876 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5877
5878 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5879 cu->list_in_scope = &file_symbols;
5880 pst = create_partial_symtab (per_cu, "");
5881 pst->anonymous = 1;
5882
5883 first_die = load_partial_dies (reader, info_ptr, 1);
5884
5885 lowpc = (CORE_ADDR) -1;
5886 highpc = (CORE_ADDR) 0;
5887 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5888
5889 pst->n_global_syms = objfile->global_psymbols.next -
5890 (objfile->global_psymbols.list + pst->globals_offset);
5891 pst->n_static_syms = objfile->static_psymbols.next -
5892 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5893 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5894}
5895
5896/* Traversal function for build_type_psymtabs. */
5897
5898static int
5899build_type_psymtab_dependencies (void **slot, void *info)
5900{
5901 struct objfile *objfile = dwarf2_per_objfile->objfile;
5902 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5903 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5904 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5905 int len = VEC_length (sig_type_ptr, tu_group->tus);
5906 struct signatured_type *iter;
f4dc4d17
DE
5907 int i;
5908
5909 gdb_assert (len > 0);
0186c6a7 5910 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5911
5912 pst->number_of_dependencies = len;
5913 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5914 len * sizeof (struct psymtab *));
5915 for (i = 0;
0186c6a7 5916 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5917 ++i)
5918 {
0186c6a7
DE
5919 gdb_assert (iter->per_cu.is_debug_types);
5920 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5921 iter->type_unit_group = tu_group;
f4dc4d17
DE
5922 }
5923
0186c6a7 5924 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5925
5926 return 1;
5927}
5928
5929/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5930 Build partial symbol tables for the .debug_types comp-units. */
5931
5932static void
5933build_type_psymtabs (struct objfile *objfile)
5934{
0e50663e 5935 if (! create_all_type_units (objfile))
348e048f
DE
5936 return;
5937
f4dc4d17
DE
5938 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5939
5940 /* Now that all TUs have been processed we can fill in the dependencies. */
5941 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5942 build_type_psymtab_dependencies, NULL);
348e048f
DE
5943}
5944
60606b2c
TT
5945/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5946
5947static void
5948psymtabs_addrmap_cleanup (void *o)
5949{
5950 struct objfile *objfile = o;
ec61707d 5951
60606b2c
TT
5952 objfile->psymtabs_addrmap = NULL;
5953}
5954
95554aad
TT
5955/* Compute the 'user' field for each psymtab in OBJFILE. */
5956
5957static void
5958set_partial_user (struct objfile *objfile)
5959{
5960 int i;
5961
5962 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5963 {
5964 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5965 struct partial_symtab *pst = per_cu->v.psymtab;
5966 int j;
5967
36586728
TT
5968 if (pst == NULL)
5969 continue;
5970
95554aad
TT
5971 for (j = 0; j < pst->number_of_dependencies; ++j)
5972 {
5973 /* Set the 'user' field only if it is not already set. */
5974 if (pst->dependencies[j]->user == NULL)
5975 pst->dependencies[j]->user = pst;
5976 }
5977 }
5978}
5979
93311388
DE
5980/* Build the partial symbol table by doing a quick pass through the
5981 .debug_info and .debug_abbrev sections. */
72bf9492 5982
93311388 5983static void
c67a9c90 5984dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5985{
60606b2c
TT
5986 struct cleanup *back_to, *addrmap_cleanup;
5987 struct obstack temp_obstack;
21b2bd31 5988 int i;
93311388 5989
45cfd468
DE
5990 if (dwarf2_read_debug)
5991 {
5992 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5993 objfile->name);
5994 }
5995
98bfdba5
PA
5996 dwarf2_per_objfile->reading_partial_symbols = 1;
5997
be391dca 5998 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5999
93311388
DE
6000 /* Any cached compilation units will be linked by the per-objfile
6001 read_in_chain. Make sure to free them when we're done. */
6002 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 6003
348e048f
DE
6004 build_type_psymtabs (objfile);
6005
93311388 6006 create_all_comp_units (objfile);
c906108c 6007
60606b2c
TT
6008 /* Create a temporary address map on a temporary obstack. We later
6009 copy this to the final obstack. */
6010 obstack_init (&temp_obstack);
6011 make_cleanup_obstack_free (&temp_obstack);
6012 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6013 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6014
21b2bd31 6015 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6016 {
21b2bd31 6017 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 6018
95554aad 6019 process_psymtab_comp_unit (per_cu, 0);
c906108c 6020 }
ff013f42 6021
95554aad
TT
6022 set_partial_user (objfile);
6023
ff013f42
JK
6024 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6025 &objfile->objfile_obstack);
60606b2c 6026 discard_cleanups (addrmap_cleanup);
ff013f42 6027
ae038cb0 6028 do_cleanups (back_to);
45cfd468
DE
6029
6030 if (dwarf2_read_debug)
6031 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6032 objfile->name);
ae038cb0
DJ
6033}
6034
3019eac3 6035/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6036
6037static void
dee91e82 6038load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6039 const gdb_byte *info_ptr,
dee91e82
DE
6040 struct die_info *comp_unit_die,
6041 int has_children,
6042 void *data)
ae038cb0 6043{
dee91e82 6044 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6045
95554aad 6046 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6047
ae038cb0
DJ
6048 /* Check if comp unit has_children.
6049 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6050 If not, there's no more debug_info for this comp unit. */
d85a05f0 6051 if (has_children)
dee91e82
DE
6052 load_partial_dies (reader, info_ptr, 0);
6053}
98bfdba5 6054
dee91e82
DE
6055/* Load the partial DIEs for a secondary CU into memory.
6056 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6057
dee91e82
DE
6058static void
6059load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6060{
f4dc4d17
DE
6061 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6062 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6063}
6064
ae038cb0 6065static void
36586728
TT
6066read_comp_units_from_section (struct objfile *objfile,
6067 struct dwarf2_section_info *section,
6068 unsigned int is_dwz,
6069 int *n_allocated,
6070 int *n_comp_units,
6071 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6072{
d521ce57 6073 const gdb_byte *info_ptr;
36586728 6074 bfd *abfd = section->asection->owner;
be391dca 6075
bf6af496
DE
6076 if (dwarf2_read_debug)
6077 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6078 section->asection->name, bfd_get_filename (abfd));
6079
36586728 6080 dwarf2_read_section (objfile, section);
ae038cb0 6081
36586728 6082 info_ptr = section->buffer;
6e70227d 6083
36586728 6084 while (info_ptr < section->buffer + section->size)
ae038cb0 6085 {
c764a876 6086 unsigned int length, initial_length_size;
ae038cb0 6087 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6088 sect_offset offset;
ae038cb0 6089
36586728 6090 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6091
6092 /* Read just enough information to find out where the next
6093 compilation unit is. */
36586728 6094 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6095
6096 /* Save the compilation unit for later lookup. */
6097 this_cu = obstack_alloc (&objfile->objfile_obstack,
6098 sizeof (struct dwarf2_per_cu_data));
6099 memset (this_cu, 0, sizeof (*this_cu));
6100 this_cu->offset = offset;
c764a876 6101 this_cu->length = length + initial_length_size;
36586728 6102 this_cu->is_dwz = is_dwz;
9291a0cd 6103 this_cu->objfile = objfile;
8a0459fd 6104 this_cu->section = section;
ae038cb0 6105
36586728 6106 if (*n_comp_units == *n_allocated)
ae038cb0 6107 {
36586728
TT
6108 *n_allocated *= 2;
6109 *all_comp_units = xrealloc (*all_comp_units,
6110 *n_allocated
6111 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6112 }
36586728
TT
6113 (*all_comp_units)[*n_comp_units] = this_cu;
6114 ++*n_comp_units;
ae038cb0
DJ
6115
6116 info_ptr = info_ptr + this_cu->length;
6117 }
36586728
TT
6118}
6119
6120/* Create a list of all compilation units in OBJFILE.
6121 This is only done for -readnow and building partial symtabs. */
6122
6123static void
6124create_all_comp_units (struct objfile *objfile)
6125{
6126 int n_allocated;
6127 int n_comp_units;
6128 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6129 struct dwz_file *dwz;
36586728
TT
6130
6131 n_comp_units = 0;
6132 n_allocated = 10;
6133 all_comp_units = xmalloc (n_allocated
6134 * sizeof (struct dwarf2_per_cu_data *));
6135
6136 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6137 &n_allocated, &n_comp_units, &all_comp_units);
6138
4db1a1dc
TT
6139 dwz = dwarf2_get_dwz_file ();
6140 if (dwz != NULL)
6141 read_comp_units_from_section (objfile, &dwz->info, 1,
6142 &n_allocated, &n_comp_units,
6143 &all_comp_units);
ae038cb0
DJ
6144
6145 dwarf2_per_objfile->all_comp_units
6146 = obstack_alloc (&objfile->objfile_obstack,
6147 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6148 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6149 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6150 xfree (all_comp_units);
6151 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6152}
6153
5734ee8b
DJ
6154/* Process all loaded DIEs for compilation unit CU, starting at
6155 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6156 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6157 DW_AT_ranges). If NEED_PC is set, then this function will set
6158 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6159 and record the covered ranges in the addrmap. */
c906108c 6160
72bf9492
DJ
6161static void
6162scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6163 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6164{
72bf9492 6165 struct partial_die_info *pdi;
c906108c 6166
91c24f0a
DC
6167 /* Now, march along the PDI's, descending into ones which have
6168 interesting children but skipping the children of the other ones,
6169 until we reach the end of the compilation unit. */
c906108c 6170
72bf9492 6171 pdi = first_die;
91c24f0a 6172
72bf9492
DJ
6173 while (pdi != NULL)
6174 {
6175 fixup_partial_die (pdi, cu);
c906108c 6176
f55ee35c 6177 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6178 children, so we need to look at them. Ditto for anonymous
6179 enums. */
933c6fe4 6180
72bf9492 6181 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6182 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6183 || pdi->tag == DW_TAG_imported_unit)
c906108c 6184 {
72bf9492 6185 switch (pdi->tag)
c906108c
SS
6186 {
6187 case DW_TAG_subprogram:
5734ee8b 6188 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6189 break;
72929c62 6190 case DW_TAG_constant:
c906108c
SS
6191 case DW_TAG_variable:
6192 case DW_TAG_typedef:
91c24f0a 6193 case DW_TAG_union_type:
72bf9492 6194 if (!pdi->is_declaration)
63d06c5c 6195 {
72bf9492 6196 add_partial_symbol (pdi, cu);
63d06c5c
DC
6197 }
6198 break;
c906108c 6199 case DW_TAG_class_type:
680b30c7 6200 case DW_TAG_interface_type:
c906108c 6201 case DW_TAG_structure_type:
72bf9492 6202 if (!pdi->is_declaration)
c906108c 6203 {
72bf9492 6204 add_partial_symbol (pdi, cu);
c906108c
SS
6205 }
6206 break;
91c24f0a 6207 case DW_TAG_enumeration_type:
72bf9492
DJ
6208 if (!pdi->is_declaration)
6209 add_partial_enumeration (pdi, cu);
c906108c
SS
6210 break;
6211 case DW_TAG_base_type:
a02abb62 6212 case DW_TAG_subrange_type:
c906108c 6213 /* File scope base type definitions are added to the partial
c5aa993b 6214 symbol table. */
72bf9492 6215 add_partial_symbol (pdi, cu);
c906108c 6216 break;
d9fa45fe 6217 case DW_TAG_namespace:
5734ee8b 6218 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6219 break;
5d7cb8df
JK
6220 case DW_TAG_module:
6221 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6222 break;
95554aad
TT
6223 case DW_TAG_imported_unit:
6224 {
6225 struct dwarf2_per_cu_data *per_cu;
6226
f4dc4d17
DE
6227 /* For now we don't handle imported units in type units. */
6228 if (cu->per_cu->is_debug_types)
6229 {
6230 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6231 " supported in type units [in module %s]"),
6232 cu->objfile->name);
6233 }
6234
95554aad 6235 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6236 pdi->is_dwz,
95554aad
TT
6237 cu->objfile);
6238
6239 /* Go read the partial unit, if needed. */
6240 if (per_cu->v.psymtab == NULL)
6241 process_psymtab_comp_unit (per_cu, 1);
6242
f4dc4d17 6243 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6244 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6245 }
6246 break;
c906108c
SS
6247 default:
6248 break;
6249 }
6250 }
6251
72bf9492
DJ
6252 /* If the die has a sibling, skip to the sibling. */
6253
6254 pdi = pdi->die_sibling;
6255 }
6256}
6257
6258/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6259
72bf9492 6260 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6261 name is concatenated with "::" and the partial DIE's name. For
6262 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6263 Enumerators are an exception; they use the scope of their parent
6264 enumeration type, i.e. the name of the enumeration type is not
6265 prepended to the enumerator.
91c24f0a 6266
72bf9492
DJ
6267 There are two complexities. One is DW_AT_specification; in this
6268 case "parent" means the parent of the target of the specification,
6269 instead of the direct parent of the DIE. The other is compilers
6270 which do not emit DW_TAG_namespace; in this case we try to guess
6271 the fully qualified name of structure types from their members'
6272 linkage names. This must be done using the DIE's children rather
6273 than the children of any DW_AT_specification target. We only need
6274 to do this for structures at the top level, i.e. if the target of
6275 any DW_AT_specification (if any; otherwise the DIE itself) does not
6276 have a parent. */
6277
6278/* Compute the scope prefix associated with PDI's parent, in
6279 compilation unit CU. The result will be allocated on CU's
6280 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6281 field. NULL is returned if no prefix is necessary. */
15d034d0 6282static const char *
72bf9492
DJ
6283partial_die_parent_scope (struct partial_die_info *pdi,
6284 struct dwarf2_cu *cu)
6285{
15d034d0 6286 const char *grandparent_scope;
72bf9492 6287 struct partial_die_info *parent, *real_pdi;
91c24f0a 6288
72bf9492
DJ
6289 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6290 then this means the parent of the specification DIE. */
6291
6292 real_pdi = pdi;
72bf9492 6293 while (real_pdi->has_specification)
36586728
TT
6294 real_pdi = find_partial_die (real_pdi->spec_offset,
6295 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6296
6297 parent = real_pdi->die_parent;
6298 if (parent == NULL)
6299 return NULL;
6300
6301 if (parent->scope_set)
6302 return parent->scope;
6303
6304 fixup_partial_die (parent, cu);
6305
10b3939b 6306 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6307
acebe513
UW
6308 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6309 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6310 Work around this problem here. */
6311 if (cu->language == language_cplus
6e70227d 6312 && parent->tag == DW_TAG_namespace
acebe513
UW
6313 && strcmp (parent->name, "::") == 0
6314 && grandparent_scope == NULL)
6315 {
6316 parent->scope = NULL;
6317 parent->scope_set = 1;
6318 return NULL;
6319 }
6320
9c6c53f7
SA
6321 if (pdi->tag == DW_TAG_enumerator)
6322 /* Enumerators should not get the name of the enumeration as a prefix. */
6323 parent->scope = grandparent_scope;
6324 else if (parent->tag == DW_TAG_namespace
f55ee35c 6325 || parent->tag == DW_TAG_module
72bf9492
DJ
6326 || parent->tag == DW_TAG_structure_type
6327 || parent->tag == DW_TAG_class_type
680b30c7 6328 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6329 || parent->tag == DW_TAG_union_type
6330 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6331 {
6332 if (grandparent_scope == NULL)
6333 parent->scope = parent->name;
6334 else
3e43a32a
MS
6335 parent->scope = typename_concat (&cu->comp_unit_obstack,
6336 grandparent_scope,
f55ee35c 6337 parent->name, 0, cu);
72bf9492 6338 }
72bf9492
DJ
6339 else
6340 {
6341 /* FIXME drow/2004-04-01: What should we be doing with
6342 function-local names? For partial symbols, we should probably be
6343 ignoring them. */
6344 complaint (&symfile_complaints,
e2e0b3e5 6345 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6346 parent->tag, pdi->offset.sect_off);
72bf9492 6347 parent->scope = grandparent_scope;
c906108c
SS
6348 }
6349
72bf9492
DJ
6350 parent->scope_set = 1;
6351 return parent->scope;
6352}
6353
6354/* Return the fully scoped name associated with PDI, from compilation unit
6355 CU. The result will be allocated with malloc. */
4568ecf9 6356
72bf9492
DJ
6357static char *
6358partial_die_full_name (struct partial_die_info *pdi,
6359 struct dwarf2_cu *cu)
6360{
15d034d0 6361 const char *parent_scope;
72bf9492 6362
98bfdba5
PA
6363 /* If this is a template instantiation, we can not work out the
6364 template arguments from partial DIEs. So, unfortunately, we have
6365 to go through the full DIEs. At least any work we do building
6366 types here will be reused if full symbols are loaded later. */
6367 if (pdi->has_template_arguments)
6368 {
6369 fixup_partial_die (pdi, cu);
6370
6371 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6372 {
6373 struct die_info *die;
6374 struct attribute attr;
6375 struct dwarf2_cu *ref_cu = cu;
6376
b64f50a1 6377 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6378 attr.name = 0;
6379 attr.form = DW_FORM_ref_addr;
4568ecf9 6380 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6381 die = follow_die_ref (NULL, &attr, &ref_cu);
6382
6383 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6384 }
6385 }
6386
72bf9492
DJ
6387 parent_scope = partial_die_parent_scope (pdi, cu);
6388 if (parent_scope == NULL)
6389 return NULL;
6390 else
f55ee35c 6391 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6392}
6393
6394static void
72bf9492 6395add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6396{
e7c27a73 6397 struct objfile *objfile = cu->objfile;
c906108c 6398 CORE_ADDR addr = 0;
15d034d0 6399 const char *actual_name = NULL;
e142c38c 6400 CORE_ADDR baseaddr;
15d034d0 6401 char *built_actual_name;
e142c38c
DJ
6402
6403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6404
15d034d0
TT
6405 built_actual_name = partial_die_full_name (pdi, cu);
6406 if (built_actual_name != NULL)
6407 actual_name = built_actual_name;
63d06c5c 6408
72bf9492
DJ
6409 if (actual_name == NULL)
6410 actual_name = pdi->name;
6411
c906108c
SS
6412 switch (pdi->tag)
6413 {
6414 case DW_TAG_subprogram:
2cfa0c8d 6415 if (pdi->is_external || cu->language == language_ada)
c906108c 6416 {
2cfa0c8d
JB
6417 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6418 of the global scope. But in Ada, we want to be able to access
6419 nested procedures globally. So all Ada subprograms are stored
6420 in the global scope. */
f47fb265 6421 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6422 mst_text, objfile); */
f47fb265 6423 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6424 built_actual_name != NULL,
f47fb265
MS
6425 VAR_DOMAIN, LOC_BLOCK,
6426 &objfile->global_psymbols,
6427 0, pdi->lowpc + baseaddr,
6428 cu->language, objfile);
c906108c
SS
6429 }
6430 else
6431 {
f47fb265 6432 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6433 mst_file_text, objfile); */
f47fb265 6434 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6435 built_actual_name != NULL,
f47fb265
MS
6436 VAR_DOMAIN, LOC_BLOCK,
6437 &objfile->static_psymbols,
6438 0, pdi->lowpc + baseaddr,
6439 cu->language, objfile);
c906108c
SS
6440 }
6441 break;
72929c62
JB
6442 case DW_TAG_constant:
6443 {
6444 struct psymbol_allocation_list *list;
6445
6446 if (pdi->is_external)
6447 list = &objfile->global_psymbols;
6448 else
6449 list = &objfile->static_psymbols;
f47fb265 6450 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6451 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6452 list, 0, 0, cu->language, objfile);
72929c62
JB
6453 }
6454 break;
c906108c 6455 case DW_TAG_variable:
95554aad
TT
6456 if (pdi->d.locdesc)
6457 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6458
95554aad 6459 if (pdi->d.locdesc
caac4577
JG
6460 && addr == 0
6461 && !dwarf2_per_objfile->has_section_at_zero)
6462 {
6463 /* A global or static variable may also have been stripped
6464 out by the linker if unused, in which case its address
6465 will be nullified; do not add such variables into partial
6466 symbol table then. */
6467 }
6468 else if (pdi->is_external)
c906108c
SS
6469 {
6470 /* Global Variable.
6471 Don't enter into the minimal symbol tables as there is
6472 a minimal symbol table entry from the ELF symbols already.
6473 Enter into partial symbol table if it has a location
6474 descriptor or a type.
6475 If the location descriptor is missing, new_symbol will create
6476 a LOC_UNRESOLVED symbol, the address of the variable will then
6477 be determined from the minimal symbol table whenever the variable
6478 is referenced.
6479 The address for the partial symbol table entry is not
6480 used by GDB, but it comes in handy for debugging partial symbol
6481 table building. */
6482
95554aad 6483 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6484 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6485 built_actual_name != NULL,
f47fb265
MS
6486 VAR_DOMAIN, LOC_STATIC,
6487 &objfile->global_psymbols,
6488 0, addr + baseaddr,
6489 cu->language, objfile);
c906108c
SS
6490 }
6491 else
6492 {
0963b4bd 6493 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6494 if (pdi->d.locdesc == NULL)
decbce07 6495 {
15d034d0 6496 xfree (built_actual_name);
decbce07
MS
6497 return;
6498 }
f47fb265 6499 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6500 mst_file_data, objfile); */
f47fb265 6501 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6502 built_actual_name != NULL,
f47fb265
MS
6503 VAR_DOMAIN, LOC_STATIC,
6504 &objfile->static_psymbols,
6505 0, addr + baseaddr,
6506 cu->language, objfile);
c906108c
SS
6507 }
6508 break;
6509 case DW_TAG_typedef:
6510 case DW_TAG_base_type:
a02abb62 6511 case DW_TAG_subrange_type:
38d518c9 6512 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6513 built_actual_name != NULL,
176620f1 6514 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6515 &objfile->static_psymbols,
e142c38c 6516 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6517 break;
72bf9492
DJ
6518 case DW_TAG_namespace:
6519 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6520 built_actual_name != NULL,
72bf9492
DJ
6521 VAR_DOMAIN, LOC_TYPEDEF,
6522 &objfile->global_psymbols,
6523 0, (CORE_ADDR) 0, cu->language, objfile);
6524 break;
c906108c 6525 case DW_TAG_class_type:
680b30c7 6526 case DW_TAG_interface_type:
c906108c
SS
6527 case DW_TAG_structure_type:
6528 case DW_TAG_union_type:
6529 case DW_TAG_enumeration_type:
fa4028e9
JB
6530 /* Skip external references. The DWARF standard says in the section
6531 about "Structure, Union, and Class Type Entries": "An incomplete
6532 structure, union or class type is represented by a structure,
6533 union or class entry that does not have a byte size attribute
6534 and that has a DW_AT_declaration attribute." */
6535 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6536 {
15d034d0 6537 xfree (built_actual_name);
decbce07
MS
6538 return;
6539 }
fa4028e9 6540
63d06c5c
DC
6541 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6542 static vs. global. */
38d518c9 6543 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6544 built_actual_name != NULL,
176620f1 6545 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6546 (cu->language == language_cplus
6547 || cu->language == language_java)
63d06c5c
DC
6548 ? &objfile->global_psymbols
6549 : &objfile->static_psymbols,
e142c38c 6550 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6551
c906108c
SS
6552 break;
6553 case DW_TAG_enumerator:
38d518c9 6554 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6555 built_actual_name != NULL,
176620f1 6556 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6557 (cu->language == language_cplus
6558 || cu->language == language_java)
f6fe98ef
DJ
6559 ? &objfile->global_psymbols
6560 : &objfile->static_psymbols,
e142c38c 6561 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6562 break;
6563 default:
6564 break;
6565 }
5c4e30ca 6566
15d034d0 6567 xfree (built_actual_name);
c906108c
SS
6568}
6569
5c4e30ca
DC
6570/* Read a partial die corresponding to a namespace; also, add a symbol
6571 corresponding to that namespace to the symbol table. NAMESPACE is
6572 the name of the enclosing namespace. */
91c24f0a 6573
72bf9492
DJ
6574static void
6575add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6576 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6577 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6578{
72bf9492 6579 /* Add a symbol for the namespace. */
e7c27a73 6580
72bf9492 6581 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6582
6583 /* Now scan partial symbols in that namespace. */
6584
91c24f0a 6585 if (pdi->has_children)
5734ee8b 6586 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6587}
6588
5d7cb8df
JK
6589/* Read a partial die corresponding to a Fortran module. */
6590
6591static void
6592add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6593 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6594{
f55ee35c 6595 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6596
6597 if (pdi->has_children)
6598 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6599}
6600
bc30ff58
JB
6601/* Read a partial die corresponding to a subprogram and create a partial
6602 symbol for that subprogram. When the CU language allows it, this
6603 routine also defines a partial symbol for each nested subprogram
6604 that this subprogram contains.
6e70227d 6605
bc30ff58
JB
6606 DIE my also be a lexical block, in which case we simply search
6607 recursively for suprograms defined inside that lexical block.
6608 Again, this is only performed when the CU language allows this
6609 type of definitions. */
6610
6611static void
6612add_partial_subprogram (struct partial_die_info *pdi,
6613 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6614 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6615{
6616 if (pdi->tag == DW_TAG_subprogram)
6617 {
6618 if (pdi->has_pc_info)
6619 {
6620 if (pdi->lowpc < *lowpc)
6621 *lowpc = pdi->lowpc;
6622 if (pdi->highpc > *highpc)
6623 *highpc = pdi->highpc;
5734ee8b
DJ
6624 if (need_pc)
6625 {
6626 CORE_ADDR baseaddr;
6627 struct objfile *objfile = cu->objfile;
6628
6629 baseaddr = ANOFFSET (objfile->section_offsets,
6630 SECT_OFF_TEXT (objfile));
6631 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6632 pdi->lowpc + baseaddr,
6633 pdi->highpc - 1 + baseaddr,
9291a0cd 6634 cu->per_cu->v.psymtab);
5734ee8b 6635 }
481860b3
GB
6636 }
6637
6638 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6639 {
bc30ff58 6640 if (!pdi->is_declaration)
e8d05480
JB
6641 /* Ignore subprogram DIEs that do not have a name, they are
6642 illegal. Do not emit a complaint at this point, we will
6643 do so when we convert this psymtab into a symtab. */
6644 if (pdi->name)
6645 add_partial_symbol (pdi, cu);
bc30ff58
JB
6646 }
6647 }
6e70227d 6648
bc30ff58
JB
6649 if (! pdi->has_children)
6650 return;
6651
6652 if (cu->language == language_ada)
6653 {
6654 pdi = pdi->die_child;
6655 while (pdi != NULL)
6656 {
6657 fixup_partial_die (pdi, cu);
6658 if (pdi->tag == DW_TAG_subprogram
6659 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6660 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6661 pdi = pdi->die_sibling;
6662 }
6663 }
6664}
6665
91c24f0a
DC
6666/* Read a partial die corresponding to an enumeration type. */
6667
72bf9492
DJ
6668static void
6669add_partial_enumeration (struct partial_die_info *enum_pdi,
6670 struct dwarf2_cu *cu)
91c24f0a 6671{
72bf9492 6672 struct partial_die_info *pdi;
91c24f0a
DC
6673
6674 if (enum_pdi->name != NULL)
72bf9492
DJ
6675 add_partial_symbol (enum_pdi, cu);
6676
6677 pdi = enum_pdi->die_child;
6678 while (pdi)
91c24f0a 6679 {
72bf9492 6680 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6681 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6682 else
72bf9492
DJ
6683 add_partial_symbol (pdi, cu);
6684 pdi = pdi->die_sibling;
91c24f0a 6685 }
91c24f0a
DC
6686}
6687
6caca83c
CC
6688/* Return the initial uleb128 in the die at INFO_PTR. */
6689
6690static unsigned int
d521ce57 6691peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6692{
6693 unsigned int bytes_read;
6694
6695 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6696}
6697
4bb7a0a7
DJ
6698/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6699 Return the corresponding abbrev, or NULL if the number is zero (indicating
6700 an empty DIE). In either case *BYTES_READ will be set to the length of
6701 the initial number. */
6702
6703static struct abbrev_info *
d521ce57 6704peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6705 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6706{
6707 bfd *abfd = cu->objfile->obfd;
6708 unsigned int abbrev_number;
6709 struct abbrev_info *abbrev;
6710
6711 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6712
6713 if (abbrev_number == 0)
6714 return NULL;
6715
433df2d4 6716 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6717 if (!abbrev)
6718 {
3e43a32a
MS
6719 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6720 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6721 }
6722
6723 return abbrev;
6724}
6725
93311388
DE
6726/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6727 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6728 DIE. Any children of the skipped DIEs will also be skipped. */
6729
d521ce57
TT
6730static const gdb_byte *
6731skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6732{
dee91e82 6733 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6734 struct abbrev_info *abbrev;
6735 unsigned int bytes_read;
6736
6737 while (1)
6738 {
6739 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6740 if (abbrev == NULL)
6741 return info_ptr + bytes_read;
6742 else
dee91e82 6743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6744 }
6745}
6746
93311388
DE
6747/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6748 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6749 abbrev corresponding to that skipped uleb128 should be passed in
6750 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6751 children. */
6752
d521ce57
TT
6753static const gdb_byte *
6754skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6755 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6756{
6757 unsigned int bytes_read;
6758 struct attribute attr;
dee91e82
DE
6759 bfd *abfd = reader->abfd;
6760 struct dwarf2_cu *cu = reader->cu;
d521ce57 6761 const gdb_byte *buffer = reader->buffer;
f664829e 6762 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6763 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6764 unsigned int form, i;
6765
6766 for (i = 0; i < abbrev->num_attrs; i++)
6767 {
6768 /* The only abbrev we care about is DW_AT_sibling. */
6769 if (abbrev->attrs[i].name == DW_AT_sibling)
6770 {
dee91e82 6771 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6772 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6773 complaint (&symfile_complaints,
6774 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6775 else
b64f50a1 6776 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6777 }
6778
6779 /* If it isn't DW_AT_sibling, skip this attribute. */
6780 form = abbrev->attrs[i].form;
6781 skip_attribute:
6782 switch (form)
6783 {
4bb7a0a7 6784 case DW_FORM_ref_addr:
ae411497
TT
6785 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6786 and later it is offset sized. */
6787 if (cu->header.version == 2)
6788 info_ptr += cu->header.addr_size;
6789 else
6790 info_ptr += cu->header.offset_size;
6791 break;
36586728
TT
6792 case DW_FORM_GNU_ref_alt:
6793 info_ptr += cu->header.offset_size;
6794 break;
ae411497 6795 case DW_FORM_addr:
4bb7a0a7
DJ
6796 info_ptr += cu->header.addr_size;
6797 break;
6798 case DW_FORM_data1:
6799 case DW_FORM_ref1:
6800 case DW_FORM_flag:
6801 info_ptr += 1;
6802 break;
2dc7f7b3
TT
6803 case DW_FORM_flag_present:
6804 break;
4bb7a0a7
DJ
6805 case DW_FORM_data2:
6806 case DW_FORM_ref2:
6807 info_ptr += 2;
6808 break;
6809 case DW_FORM_data4:
6810 case DW_FORM_ref4:
6811 info_ptr += 4;
6812 break;
6813 case DW_FORM_data8:
6814 case DW_FORM_ref8:
55f1336d 6815 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6816 info_ptr += 8;
6817 break;
6818 case DW_FORM_string:
9b1c24c8 6819 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6820 info_ptr += bytes_read;
6821 break;
2dc7f7b3 6822 case DW_FORM_sec_offset:
4bb7a0a7 6823 case DW_FORM_strp:
36586728 6824 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6825 info_ptr += cu->header.offset_size;
6826 break;
2dc7f7b3 6827 case DW_FORM_exprloc:
4bb7a0a7
DJ
6828 case DW_FORM_block:
6829 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6830 info_ptr += bytes_read;
6831 break;
6832 case DW_FORM_block1:
6833 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6834 break;
6835 case DW_FORM_block2:
6836 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6837 break;
6838 case DW_FORM_block4:
6839 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6840 break;
6841 case DW_FORM_sdata:
6842 case DW_FORM_udata:
6843 case DW_FORM_ref_udata:
3019eac3
DE
6844 case DW_FORM_GNU_addr_index:
6845 case DW_FORM_GNU_str_index:
d521ce57 6846 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6847 break;
6848 case DW_FORM_indirect:
6849 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6850 info_ptr += bytes_read;
6851 /* We need to continue parsing from here, so just go back to
6852 the top. */
6853 goto skip_attribute;
6854
6855 default:
3e43a32a
MS
6856 error (_("Dwarf Error: Cannot handle %s "
6857 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6858 dwarf_form_name (form),
6859 bfd_get_filename (abfd));
6860 }
6861 }
6862
6863 if (abbrev->has_children)
dee91e82 6864 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6865 else
6866 return info_ptr;
6867}
6868
93311388 6869/* Locate ORIG_PDI's sibling.
dee91e82 6870 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6871
d521ce57 6872static const gdb_byte *
dee91e82
DE
6873locate_pdi_sibling (const struct die_reader_specs *reader,
6874 struct partial_die_info *orig_pdi,
d521ce57 6875 const gdb_byte *info_ptr)
91c24f0a
DC
6876{
6877 /* Do we know the sibling already? */
72bf9492 6878
91c24f0a
DC
6879 if (orig_pdi->sibling)
6880 return orig_pdi->sibling;
6881
6882 /* Are there any children to deal with? */
6883
6884 if (!orig_pdi->has_children)
6885 return info_ptr;
6886
4bb7a0a7 6887 /* Skip the children the long way. */
91c24f0a 6888
dee91e82 6889 return skip_children (reader, info_ptr);
91c24f0a
DC
6890}
6891
257e7a09 6892/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6893 not NULL. */
c906108c
SS
6894
6895static void
257e7a09
YQ
6896dwarf2_read_symtab (struct partial_symtab *self,
6897 struct objfile *objfile)
c906108c 6898{
257e7a09 6899 if (self->readin)
c906108c 6900 {
442e4d9c 6901 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6902 self->filename);
442e4d9c
YQ
6903 }
6904 else
6905 {
6906 if (info_verbose)
c906108c 6907 {
442e4d9c 6908 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6909 self->filename);
442e4d9c 6910 gdb_flush (gdb_stdout);
c906108c 6911 }
c906108c 6912
442e4d9c
YQ
6913 /* Restore our global data. */
6914 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6915
442e4d9c
YQ
6916 /* If this psymtab is constructed from a debug-only objfile, the
6917 has_section_at_zero flag will not necessarily be correct. We
6918 can get the correct value for this flag by looking at the data
6919 associated with the (presumably stripped) associated objfile. */
6920 if (objfile->separate_debug_objfile_backlink)
6921 {
6922 struct dwarf2_per_objfile *dpo_backlink
6923 = objfile_data (objfile->separate_debug_objfile_backlink,
6924 dwarf2_objfile_data_key);
9a619af0 6925
442e4d9c
YQ
6926 dwarf2_per_objfile->has_section_at_zero
6927 = dpo_backlink->has_section_at_zero;
6928 }
b2ab525c 6929
442e4d9c 6930 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6931
257e7a09 6932 psymtab_to_symtab_1 (self);
c906108c 6933
442e4d9c
YQ
6934 /* Finish up the debug error message. */
6935 if (info_verbose)
6936 printf_filtered (_("done.\n"));
c906108c 6937 }
95554aad
TT
6938
6939 process_cu_includes ();
c906108c 6940}
9cdd5dbd
DE
6941\f
6942/* Reading in full CUs. */
c906108c 6943
10b3939b
DJ
6944/* Add PER_CU to the queue. */
6945
6946static void
95554aad
TT
6947queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6948 enum language pretend_language)
10b3939b
DJ
6949{
6950 struct dwarf2_queue_item *item;
6951
6952 per_cu->queued = 1;
6953 item = xmalloc (sizeof (*item));
6954 item->per_cu = per_cu;
95554aad 6955 item->pretend_language = pretend_language;
10b3939b
DJ
6956 item->next = NULL;
6957
6958 if (dwarf2_queue == NULL)
6959 dwarf2_queue = item;
6960 else
6961 dwarf2_queue_tail->next = item;
6962
6963 dwarf2_queue_tail = item;
6964}
6965
0907af0c
DE
6966/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6967 unit and add it to our queue.
6968 The result is non-zero if PER_CU was queued, otherwise the result is zero
6969 meaning either PER_CU is already queued or it is already loaded. */
6970
6971static int
6972maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6973 struct dwarf2_per_cu_data *per_cu,
6974 enum language pretend_language)
6975{
6976 /* We may arrive here during partial symbol reading, if we need full
6977 DIEs to process an unusual case (e.g. template arguments). Do
6978 not queue PER_CU, just tell our caller to load its DIEs. */
6979 if (dwarf2_per_objfile->reading_partial_symbols)
6980 {
6981 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6982 return 1;
6983 return 0;
6984 }
6985
6986 /* Mark the dependence relation so that we don't flush PER_CU
6987 too early. */
6988 dwarf2_add_dependence (this_cu, per_cu);
6989
6990 /* If it's already on the queue, we have nothing to do. */
6991 if (per_cu->queued)
6992 return 0;
6993
6994 /* If the compilation unit is already loaded, just mark it as
6995 used. */
6996 if (per_cu->cu != NULL)
6997 {
6998 per_cu->cu->last_used = 0;
6999 return 0;
7000 }
7001
7002 /* Add it to the queue. */
7003 queue_comp_unit (per_cu, pretend_language);
7004
7005 return 1;
7006}
7007
10b3939b
DJ
7008/* Process the queue. */
7009
7010static void
a0f42c21 7011process_queue (void)
10b3939b
DJ
7012{
7013 struct dwarf2_queue_item *item, *next_item;
7014
45cfd468
DE
7015 if (dwarf2_read_debug)
7016 {
7017 fprintf_unfiltered (gdb_stdlog,
7018 "Expanding one or more symtabs of objfile %s ...\n",
7019 dwarf2_per_objfile->objfile->name);
7020 }
7021
03dd20cc
DJ
7022 /* The queue starts out with one item, but following a DIE reference
7023 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7024 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7025 {
9291a0cd
TT
7026 if (dwarf2_per_objfile->using_index
7027 ? !item->per_cu->v.quick->symtab
7028 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7029 {
7030 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 7031 char buf[100];
f4dc4d17 7032
247f5c4f 7033 if (per_cu->is_debug_types)
f4dc4d17 7034 {
247f5c4f
DE
7035 struct signatured_type *sig_type =
7036 (struct signatured_type *) per_cu;
7037
7038 sprintf (buf, "TU %s at offset 0x%x",
7039 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7040 }
247f5c4f
DE
7041 else
7042 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7043
7044 if (dwarf2_read_debug)
7045 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7046
7047 if (per_cu->is_debug_types)
7048 process_full_type_unit (per_cu, item->pretend_language);
7049 else
7050 process_full_comp_unit (per_cu, item->pretend_language);
7051
7052 if (dwarf2_read_debug)
247f5c4f 7053 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7054 }
10b3939b
DJ
7055
7056 item->per_cu->queued = 0;
7057 next_item = item->next;
7058 xfree (item);
7059 }
7060
7061 dwarf2_queue_tail = NULL;
45cfd468
DE
7062
7063 if (dwarf2_read_debug)
7064 {
7065 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7066 dwarf2_per_objfile->objfile->name);
7067 }
10b3939b
DJ
7068}
7069
7070/* Free all allocated queue entries. This function only releases anything if
7071 an error was thrown; if the queue was processed then it would have been
7072 freed as we went along. */
7073
7074static void
7075dwarf2_release_queue (void *dummy)
7076{
7077 struct dwarf2_queue_item *item, *last;
7078
7079 item = dwarf2_queue;
7080 while (item)
7081 {
7082 /* Anything still marked queued is likely to be in an
7083 inconsistent state, so discard it. */
7084 if (item->per_cu->queued)
7085 {
7086 if (item->per_cu->cu != NULL)
dee91e82 7087 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7088 item->per_cu->queued = 0;
7089 }
7090
7091 last = item;
7092 item = item->next;
7093 xfree (last);
7094 }
7095
7096 dwarf2_queue = dwarf2_queue_tail = NULL;
7097}
7098
7099/* Read in full symbols for PST, and anything it depends on. */
7100
c906108c 7101static void
fba45db2 7102psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7103{
10b3939b 7104 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7105 int i;
7106
95554aad
TT
7107 if (pst->readin)
7108 return;
7109
aaa75496 7110 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7111 if (!pst->dependencies[i]->readin
7112 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7113 {
7114 /* Inform about additional files that need to be read in. */
7115 if (info_verbose)
7116 {
a3f17187 7117 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7118 fputs_filtered (" ", gdb_stdout);
7119 wrap_here ("");
7120 fputs_filtered ("and ", gdb_stdout);
7121 wrap_here ("");
7122 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7123 wrap_here (""); /* Flush output. */
aaa75496
JB
7124 gdb_flush (gdb_stdout);
7125 }
7126 psymtab_to_symtab_1 (pst->dependencies[i]);
7127 }
7128
e38df1d0 7129 per_cu = pst->read_symtab_private;
10b3939b
DJ
7130
7131 if (per_cu == NULL)
aaa75496
JB
7132 {
7133 /* It's an include file, no symbols to read for it.
7134 Everything is in the parent symtab. */
7135 pst->readin = 1;
7136 return;
7137 }
c906108c 7138
a0f42c21 7139 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7140}
7141
dee91e82
DE
7142/* Trivial hash function for die_info: the hash value of a DIE
7143 is its offset in .debug_info for this objfile. */
10b3939b 7144
dee91e82
DE
7145static hashval_t
7146die_hash (const void *item)
10b3939b 7147{
dee91e82 7148 const struct die_info *die = item;
6502dd73 7149
dee91e82
DE
7150 return die->offset.sect_off;
7151}
63d06c5c 7152
dee91e82
DE
7153/* Trivial comparison function for die_info structures: two DIEs
7154 are equal if they have the same offset. */
98bfdba5 7155
dee91e82
DE
7156static int
7157die_eq (const void *item_lhs, const void *item_rhs)
7158{
7159 const struct die_info *die_lhs = item_lhs;
7160 const struct die_info *die_rhs = item_rhs;
c906108c 7161
dee91e82
DE
7162 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7163}
c906108c 7164
dee91e82
DE
7165/* die_reader_func for load_full_comp_unit.
7166 This is identical to read_signatured_type_reader,
7167 but is kept separate for now. */
c906108c 7168
dee91e82
DE
7169static void
7170load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7171 const gdb_byte *info_ptr,
dee91e82
DE
7172 struct die_info *comp_unit_die,
7173 int has_children,
7174 void *data)
7175{
7176 struct dwarf2_cu *cu = reader->cu;
95554aad 7177 enum language *language_ptr = data;
6caca83c 7178
dee91e82
DE
7179 gdb_assert (cu->die_hash == NULL);
7180 cu->die_hash =
7181 htab_create_alloc_ex (cu->header.length / 12,
7182 die_hash,
7183 die_eq,
7184 NULL,
7185 &cu->comp_unit_obstack,
7186 hashtab_obstack_allocate,
7187 dummy_obstack_deallocate);
e142c38c 7188
dee91e82
DE
7189 if (has_children)
7190 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7191 &info_ptr, comp_unit_die);
7192 cu->dies = comp_unit_die;
7193 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7194
7195 /* We try not to read any attributes in this function, because not
9cdd5dbd 7196 all CUs needed for references have been loaded yet, and symbol
10b3939b 7197 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7198 or we won't be able to build types correctly.
7199 Similarly, if we do not read the producer, we can not apply
7200 producer-specific interpretation. */
95554aad 7201 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7202}
10b3939b 7203
dee91e82 7204/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7205
dee91e82 7206static void
95554aad
TT
7207load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7208 enum language pretend_language)
dee91e82 7209{
3019eac3 7210 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7211
f4dc4d17
DE
7212 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7213 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7214}
7215
3da10d80
KS
7216/* Add a DIE to the delayed physname list. */
7217
7218static void
7219add_to_method_list (struct type *type, int fnfield_index, int index,
7220 const char *name, struct die_info *die,
7221 struct dwarf2_cu *cu)
7222{
7223 struct delayed_method_info mi;
7224 mi.type = type;
7225 mi.fnfield_index = fnfield_index;
7226 mi.index = index;
7227 mi.name = name;
7228 mi.die = die;
7229 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7230}
7231
7232/* A cleanup for freeing the delayed method list. */
7233
7234static void
7235free_delayed_list (void *ptr)
7236{
7237 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7238 if (cu->method_list != NULL)
7239 {
7240 VEC_free (delayed_method_info, cu->method_list);
7241 cu->method_list = NULL;
7242 }
7243}
7244
7245/* Compute the physnames of any methods on the CU's method list.
7246
7247 The computation of method physnames is delayed in order to avoid the
7248 (bad) condition that one of the method's formal parameters is of an as yet
7249 incomplete type. */
7250
7251static void
7252compute_delayed_physnames (struct dwarf2_cu *cu)
7253{
7254 int i;
7255 struct delayed_method_info *mi;
7256 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7257 {
1d06ead6 7258 const char *physname;
3da10d80
KS
7259 struct fn_fieldlist *fn_flp
7260 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7261 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7262 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7263 }
7264}
7265
a766d390
DE
7266/* Go objects should be embedded in a DW_TAG_module DIE,
7267 and it's not clear if/how imported objects will appear.
7268 To keep Go support simple until that's worked out,
7269 go back through what we've read and create something usable.
7270 We could do this while processing each DIE, and feels kinda cleaner,
7271 but that way is more invasive.
7272 This is to, for example, allow the user to type "p var" or "b main"
7273 without having to specify the package name, and allow lookups
7274 of module.object to work in contexts that use the expression
7275 parser. */
7276
7277static void
7278fixup_go_packaging (struct dwarf2_cu *cu)
7279{
7280 char *package_name = NULL;
7281 struct pending *list;
7282 int i;
7283
7284 for (list = global_symbols; list != NULL; list = list->next)
7285 {
7286 for (i = 0; i < list->nsyms; ++i)
7287 {
7288 struct symbol *sym = list->symbol[i];
7289
7290 if (SYMBOL_LANGUAGE (sym) == language_go
7291 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7292 {
7293 char *this_package_name = go_symbol_package_name (sym);
7294
7295 if (this_package_name == NULL)
7296 continue;
7297 if (package_name == NULL)
7298 package_name = this_package_name;
7299 else
7300 {
7301 if (strcmp (package_name, this_package_name) != 0)
7302 complaint (&symfile_complaints,
7303 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7304 (SYMBOL_SYMTAB (sym)
05cba821 7305 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
7306 : cu->objfile->name),
7307 this_package_name, package_name);
7308 xfree (this_package_name);
7309 }
7310 }
7311 }
7312 }
7313
7314 if (package_name != NULL)
7315 {
7316 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7317 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7318 package_name,
7319 strlen (package_name));
a766d390 7320 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7321 saved_package_name, objfile);
a766d390
DE
7322 struct symbol *sym;
7323
7324 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7325
e623cf5d 7326 sym = allocate_symbol (objfile);
f85f34ed 7327 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7328 SYMBOL_SET_NAMES (sym, saved_package_name,
7329 strlen (saved_package_name), 0, objfile);
a766d390
DE
7330 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7331 e.g., "main" finds the "main" module and not C's main(). */
7332 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7333 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7334 SYMBOL_TYPE (sym) = type;
7335
7336 add_symbol_to_list (sym, &global_symbols);
7337
7338 xfree (package_name);
7339 }
7340}
7341
95554aad
TT
7342/* Return the symtab for PER_CU. This works properly regardless of
7343 whether we're using the index or psymtabs. */
7344
7345static struct symtab *
7346get_symtab (struct dwarf2_per_cu_data *per_cu)
7347{
7348 return (dwarf2_per_objfile->using_index
7349 ? per_cu->v.quick->symtab
7350 : per_cu->v.psymtab->symtab);
7351}
7352
7353/* A helper function for computing the list of all symbol tables
7354 included by PER_CU. */
7355
7356static void
ec94af83
DE
7357recursively_compute_inclusions (VEC (symtab_ptr) **result,
7358 htab_t all_children, htab_t all_type_symtabs,
95554aad
TT
7359 struct dwarf2_per_cu_data *per_cu)
7360{
7361 void **slot;
7362 int ix;
ec94af83 7363 struct symtab *symtab;
95554aad
TT
7364 struct dwarf2_per_cu_data *iter;
7365
7366 slot = htab_find_slot (all_children, per_cu, INSERT);
7367 if (*slot != NULL)
7368 {
7369 /* This inclusion and its children have been processed. */
7370 return;
7371 }
7372
7373 *slot = per_cu;
7374 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7375 symtab = get_symtab (per_cu);
7376 if (symtab != NULL)
7377 {
7378 /* If this is a type unit only add its symbol table if we haven't
7379 seen it yet (type unit per_cu's can share symtabs). */
7380 if (per_cu->is_debug_types)
7381 {
7382 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7383 if (*slot == NULL)
7384 {
7385 *slot = symtab;
7386 VEC_safe_push (symtab_ptr, *result, symtab);
7387 }
7388 }
7389 else
7390 VEC_safe_push (symtab_ptr, *result, symtab);
7391 }
95554aad
TT
7392
7393 for (ix = 0;
796a7ff8 7394 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7395 ++ix)
ec94af83
DE
7396 {
7397 recursively_compute_inclusions (result, all_children,
7398 all_type_symtabs, iter);
7399 }
95554aad
TT
7400}
7401
7402/* Compute the symtab 'includes' fields for the symtab related to
7403 PER_CU. */
7404
7405static void
7406compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7407{
f4dc4d17
DE
7408 gdb_assert (! per_cu->is_debug_types);
7409
796a7ff8 7410 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7411 {
7412 int ix, len;
ec94af83
DE
7413 struct dwarf2_per_cu_data *per_cu_iter;
7414 struct symtab *symtab_iter;
7415 VEC (symtab_ptr) *result_symtabs = NULL;
7416 htab_t all_children, all_type_symtabs;
95554aad
TT
7417 struct symtab *symtab = get_symtab (per_cu);
7418
7419 /* If we don't have a symtab, we can just skip this case. */
7420 if (symtab == NULL)
7421 return;
7422
7423 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7424 NULL, xcalloc, xfree);
ec94af83
DE
7425 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7426 NULL, xcalloc, xfree);
95554aad
TT
7427
7428 for (ix = 0;
796a7ff8 7429 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7430 ix, per_cu_iter);
95554aad 7431 ++ix)
ec94af83
DE
7432 {
7433 recursively_compute_inclusions (&result_symtabs, all_children,
7434 all_type_symtabs, per_cu_iter);
7435 }
95554aad 7436
ec94af83
DE
7437 /* Now we have a transitive closure of all the included symtabs. */
7438 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7439 symtab->includes
7440 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7441 (len + 1) * sizeof (struct symtab *));
7442 for (ix = 0;
ec94af83 7443 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7444 ++ix)
ec94af83 7445 symtab->includes[ix] = symtab_iter;
95554aad
TT
7446 symtab->includes[len] = NULL;
7447
ec94af83 7448 VEC_free (symtab_ptr, result_symtabs);
95554aad 7449 htab_delete (all_children);
ec94af83 7450 htab_delete (all_type_symtabs);
95554aad
TT
7451 }
7452}
7453
7454/* Compute the 'includes' field for the symtabs of all the CUs we just
7455 read. */
7456
7457static void
7458process_cu_includes (void)
7459{
7460 int ix;
7461 struct dwarf2_per_cu_data *iter;
7462
7463 for (ix = 0;
7464 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7465 ix, iter);
7466 ++ix)
f4dc4d17
DE
7467 {
7468 if (! iter->is_debug_types)
7469 compute_symtab_includes (iter);
7470 }
95554aad
TT
7471
7472 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7473}
7474
9cdd5dbd 7475/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7476 already been loaded into memory. */
7477
7478static void
95554aad
TT
7479process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7480 enum language pretend_language)
10b3939b 7481{
10b3939b 7482 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7483 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7484 CORE_ADDR lowpc, highpc;
7485 struct symtab *symtab;
3da10d80 7486 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7487 CORE_ADDR baseaddr;
4359dff1 7488 struct block *static_block;
10b3939b
DJ
7489
7490 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7491
10b3939b
DJ
7492 buildsym_init ();
7493 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7494 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7495
7496 cu->list_in_scope = &file_symbols;
c906108c 7497
95554aad
TT
7498 cu->language = pretend_language;
7499 cu->language_defn = language_def (cu->language);
7500
c906108c 7501 /* Do line number decoding in read_file_scope () */
10b3939b 7502 process_die (cu->dies, cu);
c906108c 7503
a766d390
DE
7504 /* For now fudge the Go package. */
7505 if (cu->language == language_go)
7506 fixup_go_packaging (cu);
7507
3da10d80
KS
7508 /* Now that we have processed all the DIEs in the CU, all the types
7509 should be complete, and it should now be safe to compute all of the
7510 physnames. */
7511 compute_delayed_physnames (cu);
7512 do_cleanups (delayed_list_cleanup);
7513
fae299cd
DC
7514 /* Some compilers don't define a DW_AT_high_pc attribute for the
7515 compilation unit. If the DW_AT_high_pc is missing, synthesize
7516 it, by scanning the DIE's below the compilation unit. */
10b3939b 7517 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7518
36586728 7519 static_block
ff546935 7520 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7521
7522 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7523 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7524 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7525 addrmap to help ensure it has an accurate map of pc values belonging to
7526 this comp unit. */
7527 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7528
7529 symtab = end_symtab_from_static_block (static_block, objfile,
7530 SECT_OFF_TEXT (objfile), 0);
c906108c 7531
8be455d7 7532 if (symtab != NULL)
c906108c 7533 {
df15bd07 7534 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7535
8be455d7
JK
7536 /* Set symtab language to language from DW_AT_language. If the
7537 compilation is from a C file generated by language preprocessors, do
7538 not set the language if it was already deduced by start_subfile. */
7539 if (!(cu->language == language_c && symtab->language != language_c))
7540 symtab->language = cu->language;
7541
7542 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7543 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7544 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7545 there were bugs in prologue debug info, fixed later in GCC-4.5
7546 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7547
7548 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7549 needed, it would be wrong due to missing DW_AT_producer there.
7550
7551 Still one can confuse GDB by using non-standard GCC compilation
7552 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7553 */
ab260dad 7554 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7555 symtab->locations_valid = 1;
e0d00bc7
JK
7556
7557 if (gcc_4_minor >= 5)
7558 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7559
7560 symtab->call_site_htab = cu->call_site_htab;
c906108c 7561 }
9291a0cd
TT
7562
7563 if (dwarf2_per_objfile->using_index)
7564 per_cu->v.quick->symtab = symtab;
7565 else
7566 {
7567 struct partial_symtab *pst = per_cu->v.psymtab;
7568 pst->symtab = symtab;
7569 pst->readin = 1;
7570 }
c906108c 7571
95554aad
TT
7572 /* Push it for inclusion processing later. */
7573 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7574
c906108c 7575 do_cleanups (back_to);
f4dc4d17 7576}
45cfd468 7577
f4dc4d17
DE
7578/* Generate full symbol information for type unit PER_CU, whose DIEs have
7579 already been loaded into memory. */
7580
7581static void
7582process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7583 enum language pretend_language)
7584{
7585 struct dwarf2_cu *cu = per_cu->cu;
7586 struct objfile *objfile = per_cu->objfile;
7587 struct symtab *symtab;
7588 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7589 struct signatured_type *sig_type;
7590
7591 gdb_assert (per_cu->is_debug_types);
7592 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7593
7594 buildsym_init ();
7595 back_to = make_cleanup (really_free_pendings, NULL);
7596 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7597
7598 cu->list_in_scope = &file_symbols;
7599
7600 cu->language = pretend_language;
7601 cu->language_defn = language_def (cu->language);
7602
7603 /* The symbol tables are set up in read_type_unit_scope. */
7604 process_die (cu->dies, cu);
7605
7606 /* For now fudge the Go package. */
7607 if (cu->language == language_go)
7608 fixup_go_packaging (cu);
7609
7610 /* Now that we have processed all the DIEs in the CU, all the types
7611 should be complete, and it should now be safe to compute all of the
7612 physnames. */
7613 compute_delayed_physnames (cu);
7614 do_cleanups (delayed_list_cleanup);
7615
7616 /* TUs share symbol tables.
7617 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7618 of it with end_expandable_symtab. Otherwise, complete the addition of
7619 this TU's symbols to the existing symtab. */
0186c6a7 7620 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7621 {
f4dc4d17 7622 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7623 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7624
7625 if (symtab != NULL)
7626 {
7627 /* Set symtab language to language from DW_AT_language. If the
7628 compilation is from a C file generated by language preprocessors,
7629 do not set the language if it was already deduced by
7630 start_subfile. */
7631 if (!(cu->language == language_c && symtab->language != language_c))
7632 symtab->language = cu->language;
7633 }
7634 }
7635 else
7636 {
7637 augment_type_symtab (objfile,
0186c6a7
DE
7638 sig_type->type_unit_group->primary_symtab);
7639 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7640 }
7641
7642 if (dwarf2_per_objfile->using_index)
7643 per_cu->v.quick->symtab = symtab;
7644 else
7645 {
7646 struct partial_symtab *pst = per_cu->v.psymtab;
7647 pst->symtab = symtab;
7648 pst->readin = 1;
45cfd468 7649 }
f4dc4d17
DE
7650
7651 do_cleanups (back_to);
c906108c
SS
7652}
7653
95554aad
TT
7654/* Process an imported unit DIE. */
7655
7656static void
7657process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7658{
7659 struct attribute *attr;
7660
f4dc4d17
DE
7661 /* For now we don't handle imported units in type units. */
7662 if (cu->per_cu->is_debug_types)
7663 {
7664 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7665 " supported in type units [in module %s]"),
7666 cu->objfile->name);
7667 }
7668
95554aad
TT
7669 attr = dwarf2_attr (die, DW_AT_import, cu);
7670 if (attr != NULL)
7671 {
7672 struct dwarf2_per_cu_data *per_cu;
7673 struct symtab *imported_symtab;
7674 sect_offset offset;
36586728 7675 int is_dwz;
95554aad
TT
7676
7677 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7678 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7679 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad
TT
7680
7681 /* Queue the unit, if needed. */
7682 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7683 load_full_comp_unit (per_cu, cu->language);
7684
796a7ff8 7685 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7686 per_cu);
7687 }
7688}
7689
c906108c
SS
7690/* Process a die and its children. */
7691
7692static void
e7c27a73 7693process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7694{
7695 switch (die->tag)
7696 {
7697 case DW_TAG_padding:
7698 break;
7699 case DW_TAG_compile_unit:
95554aad 7700 case DW_TAG_partial_unit:
e7c27a73 7701 read_file_scope (die, cu);
c906108c 7702 break;
348e048f
DE
7703 case DW_TAG_type_unit:
7704 read_type_unit_scope (die, cu);
7705 break;
c906108c 7706 case DW_TAG_subprogram:
c906108c 7707 case DW_TAG_inlined_subroutine:
edb3359d 7708 read_func_scope (die, cu);
c906108c
SS
7709 break;
7710 case DW_TAG_lexical_block:
14898363
L
7711 case DW_TAG_try_block:
7712 case DW_TAG_catch_block:
e7c27a73 7713 read_lexical_block_scope (die, cu);
c906108c 7714 break;
96408a79
SA
7715 case DW_TAG_GNU_call_site:
7716 read_call_site_scope (die, cu);
7717 break;
c906108c 7718 case DW_TAG_class_type:
680b30c7 7719 case DW_TAG_interface_type:
c906108c
SS
7720 case DW_TAG_structure_type:
7721 case DW_TAG_union_type:
134d01f1 7722 process_structure_scope (die, cu);
c906108c
SS
7723 break;
7724 case DW_TAG_enumeration_type:
134d01f1 7725 process_enumeration_scope (die, cu);
c906108c 7726 break;
134d01f1 7727
f792889a
DJ
7728 /* These dies have a type, but processing them does not create
7729 a symbol or recurse to process the children. Therefore we can
7730 read them on-demand through read_type_die. */
c906108c 7731 case DW_TAG_subroutine_type:
72019c9c 7732 case DW_TAG_set_type:
c906108c 7733 case DW_TAG_array_type:
c906108c 7734 case DW_TAG_pointer_type:
c906108c 7735 case DW_TAG_ptr_to_member_type:
c906108c 7736 case DW_TAG_reference_type:
c906108c 7737 case DW_TAG_string_type:
c906108c 7738 break;
134d01f1 7739
c906108c 7740 case DW_TAG_base_type:
a02abb62 7741 case DW_TAG_subrange_type:
cb249c71 7742 case DW_TAG_typedef:
134d01f1
DJ
7743 /* Add a typedef symbol for the type definition, if it has a
7744 DW_AT_name. */
f792889a 7745 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7746 break;
c906108c 7747 case DW_TAG_common_block:
e7c27a73 7748 read_common_block (die, cu);
c906108c
SS
7749 break;
7750 case DW_TAG_common_inclusion:
7751 break;
d9fa45fe 7752 case DW_TAG_namespace:
4d4ec4e5 7753 cu->processing_has_namespace_info = 1;
e7c27a73 7754 read_namespace (die, cu);
d9fa45fe 7755 break;
5d7cb8df 7756 case DW_TAG_module:
4d4ec4e5 7757 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7758 read_module (die, cu);
7759 break;
d9fa45fe
DC
7760 case DW_TAG_imported_declaration:
7761 case DW_TAG_imported_module:
4d4ec4e5 7762 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7763 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7764 || cu->language != language_fortran))
7765 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7766 dwarf_tag_name (die->tag));
7767 read_import_statement (die, cu);
d9fa45fe 7768 break;
95554aad
TT
7769
7770 case DW_TAG_imported_unit:
7771 process_imported_unit_die (die, cu);
7772 break;
7773
c906108c 7774 default:
e7c27a73 7775 new_symbol (die, NULL, cu);
c906108c
SS
7776 break;
7777 }
7778}
ca69b9e6
DE
7779\f
7780/* DWARF name computation. */
c906108c 7781
94af9270
KS
7782/* A helper function for dwarf2_compute_name which determines whether DIE
7783 needs to have the name of the scope prepended to the name listed in the
7784 die. */
7785
7786static int
7787die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7788{
1c809c68
TT
7789 struct attribute *attr;
7790
94af9270
KS
7791 switch (die->tag)
7792 {
7793 case DW_TAG_namespace:
7794 case DW_TAG_typedef:
7795 case DW_TAG_class_type:
7796 case DW_TAG_interface_type:
7797 case DW_TAG_structure_type:
7798 case DW_TAG_union_type:
7799 case DW_TAG_enumeration_type:
7800 case DW_TAG_enumerator:
7801 case DW_TAG_subprogram:
7802 case DW_TAG_member:
7803 return 1;
7804
7805 case DW_TAG_variable:
c2b0a229 7806 case DW_TAG_constant:
94af9270
KS
7807 /* We only need to prefix "globally" visible variables. These include
7808 any variable marked with DW_AT_external or any variable that
7809 lives in a namespace. [Variables in anonymous namespaces
7810 require prefixing, but they are not DW_AT_external.] */
7811
7812 if (dwarf2_attr (die, DW_AT_specification, cu))
7813 {
7814 struct dwarf2_cu *spec_cu = cu;
9a619af0 7815
94af9270
KS
7816 return die_needs_namespace (die_specification (die, &spec_cu),
7817 spec_cu);
7818 }
7819
1c809c68 7820 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7821 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7822 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7823 return 0;
7824 /* A variable in a lexical block of some kind does not need a
7825 namespace, even though in C++ such variables may be external
7826 and have a mangled name. */
7827 if (die->parent->tag == DW_TAG_lexical_block
7828 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7829 || die->parent->tag == DW_TAG_catch_block
7830 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7831 return 0;
7832 return 1;
94af9270
KS
7833
7834 default:
7835 return 0;
7836 }
7837}
7838
98bfdba5
PA
7839/* Retrieve the last character from a mem_file. */
7840
7841static void
7842do_ui_file_peek_last (void *object, const char *buffer, long length)
7843{
7844 char *last_char_p = (char *) object;
7845
7846 if (length > 0)
7847 *last_char_p = buffer[length - 1];
7848}
7849
94af9270 7850/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7851 compute the physname for the object, which include a method's:
7852 - formal parameters (C++/Java),
7853 - receiver type (Go),
7854 - return type (Java).
7855
7856 The term "physname" is a bit confusing.
7857 For C++, for example, it is the demangled name.
7858 For Go, for example, it's the mangled name.
94af9270 7859
af6b7be1
JB
7860 For Ada, return the DIE's linkage name rather than the fully qualified
7861 name. PHYSNAME is ignored..
7862
94af9270
KS
7863 The result is allocated on the objfile_obstack and canonicalized. */
7864
7865static const char *
15d034d0
TT
7866dwarf2_compute_name (const char *name,
7867 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7868 int physname)
7869{
bb5ed363
DE
7870 struct objfile *objfile = cu->objfile;
7871
94af9270
KS
7872 if (name == NULL)
7873 name = dwarf2_name (die, cu);
7874
f55ee35c
JK
7875 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7876 compute it by typename_concat inside GDB. */
7877 if (cu->language == language_ada
7878 || (cu->language == language_fortran && physname))
7879 {
7880 /* For Ada unit, we prefer the linkage name over the name, as
7881 the former contains the exported name, which the user expects
7882 to be able to reference. Ideally, we want the user to be able
7883 to reference this entity using either natural or linkage name,
7884 but we haven't started looking at this enhancement yet. */
7885 struct attribute *attr;
7886
7887 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7888 if (attr == NULL)
7889 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7890 if (attr && DW_STRING (attr))
7891 return DW_STRING (attr);
7892 }
7893
94af9270
KS
7894 /* These are the only languages we know how to qualify names in. */
7895 if (name != NULL
f55ee35c
JK
7896 && (cu->language == language_cplus || cu->language == language_java
7897 || cu->language == language_fortran))
94af9270
KS
7898 {
7899 if (die_needs_namespace (die, cu))
7900 {
7901 long length;
0d5cff50 7902 const char *prefix;
94af9270
KS
7903 struct ui_file *buf;
7904
7905 prefix = determine_prefix (die, cu);
7906 buf = mem_fileopen ();
7907 if (*prefix != '\0')
7908 {
f55ee35c
JK
7909 char *prefixed_name = typename_concat (NULL, prefix, name,
7910 physname, cu);
9a619af0 7911
94af9270
KS
7912 fputs_unfiltered (prefixed_name, buf);
7913 xfree (prefixed_name);
7914 }
7915 else
62d5b8da 7916 fputs_unfiltered (name, buf);
94af9270 7917
98bfdba5
PA
7918 /* Template parameters may be specified in the DIE's DW_AT_name, or
7919 as children with DW_TAG_template_type_param or
7920 DW_TAG_value_type_param. If the latter, add them to the name
7921 here. If the name already has template parameters, then
7922 skip this step; some versions of GCC emit both, and
7923 it is more efficient to use the pre-computed name.
7924
7925 Something to keep in mind about this process: it is very
7926 unlikely, or in some cases downright impossible, to produce
7927 something that will match the mangled name of a function.
7928 If the definition of the function has the same debug info,
7929 we should be able to match up with it anyway. But fallbacks
7930 using the minimal symbol, for instance to find a method
7931 implemented in a stripped copy of libstdc++, will not work.
7932 If we do not have debug info for the definition, we will have to
7933 match them up some other way.
7934
7935 When we do name matching there is a related problem with function
7936 templates; two instantiated function templates are allowed to
7937 differ only by their return types, which we do not add here. */
7938
7939 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7940 {
7941 struct attribute *attr;
7942 struct die_info *child;
7943 int first = 1;
7944
7945 die->building_fullname = 1;
7946
7947 for (child = die->child; child != NULL; child = child->sibling)
7948 {
7949 struct type *type;
12df843f 7950 LONGEST value;
d521ce57 7951 const gdb_byte *bytes;
98bfdba5
PA
7952 struct dwarf2_locexpr_baton *baton;
7953 struct value *v;
7954
7955 if (child->tag != DW_TAG_template_type_param
7956 && child->tag != DW_TAG_template_value_param)
7957 continue;
7958
7959 if (first)
7960 {
7961 fputs_unfiltered ("<", buf);
7962 first = 0;
7963 }
7964 else
7965 fputs_unfiltered (", ", buf);
7966
7967 attr = dwarf2_attr (child, DW_AT_type, cu);
7968 if (attr == NULL)
7969 {
7970 complaint (&symfile_complaints,
7971 _("template parameter missing DW_AT_type"));
7972 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7973 continue;
7974 }
7975 type = die_type (child, cu);
7976
7977 if (child->tag == DW_TAG_template_type_param)
7978 {
79d43c61 7979 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7980 continue;
7981 }
7982
7983 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7984 if (attr == NULL)
7985 {
7986 complaint (&symfile_complaints,
3e43a32a
MS
7987 _("template parameter missing "
7988 "DW_AT_const_value"));
98bfdba5
PA
7989 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7990 continue;
7991 }
7992
7993 dwarf2_const_value_attr (attr, type, name,
7994 &cu->comp_unit_obstack, cu,
7995 &value, &bytes, &baton);
7996
7997 if (TYPE_NOSIGN (type))
7998 /* GDB prints characters as NUMBER 'CHAR'. If that's
7999 changed, this can use value_print instead. */
8000 c_printchar (value, type, buf);
8001 else
8002 {
8003 struct value_print_options opts;
8004
8005 if (baton != NULL)
8006 v = dwarf2_evaluate_loc_desc (type, NULL,
8007 baton->data,
8008 baton->size,
8009 baton->per_cu);
8010 else if (bytes != NULL)
8011 {
8012 v = allocate_value (type);
8013 memcpy (value_contents_writeable (v), bytes,
8014 TYPE_LENGTH (type));
8015 }
8016 else
8017 v = value_from_longest (type, value);
8018
3e43a32a
MS
8019 /* Specify decimal so that we do not depend on
8020 the radix. */
98bfdba5
PA
8021 get_formatted_print_options (&opts, 'd');
8022 opts.raw = 1;
8023 value_print (v, buf, &opts);
8024 release_value (v);
8025 value_free (v);
8026 }
8027 }
8028
8029 die->building_fullname = 0;
8030
8031 if (!first)
8032 {
8033 /* Close the argument list, with a space if necessary
8034 (nested templates). */
8035 char last_char = '\0';
8036 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8037 if (last_char == '>')
8038 fputs_unfiltered (" >", buf);
8039 else
8040 fputs_unfiltered (">", buf);
8041 }
8042 }
8043
94af9270
KS
8044 /* For Java and C++ methods, append formal parameter type
8045 information, if PHYSNAME. */
6e70227d 8046
94af9270
KS
8047 if (physname && die->tag == DW_TAG_subprogram
8048 && (cu->language == language_cplus
8049 || cu->language == language_java))
8050 {
8051 struct type *type = read_type_die (die, cu);
8052
79d43c61
TT
8053 c_type_print_args (type, buf, 1, cu->language,
8054 &type_print_raw_options);
94af9270
KS
8055
8056 if (cu->language == language_java)
8057 {
8058 /* For java, we must append the return type to method
0963b4bd 8059 names. */
94af9270
KS
8060 if (die->tag == DW_TAG_subprogram)
8061 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8062 0, 0, &type_print_raw_options);
94af9270
KS
8063 }
8064 else if (cu->language == language_cplus)
8065 {
60430eff
DJ
8066 /* Assume that an artificial first parameter is
8067 "this", but do not crash if it is not. RealView
8068 marks unnamed (and thus unused) parameters as
8069 artificial; there is no way to differentiate
8070 the two cases. */
94af9270
KS
8071 if (TYPE_NFIELDS (type) > 0
8072 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8073 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8074 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8075 0))))
94af9270
KS
8076 fputs_unfiltered (" const", buf);
8077 }
8078 }
8079
bb5ed363 8080 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8081 &length);
8082 ui_file_delete (buf);
8083
8084 if (cu->language == language_cplus)
8085 {
15d034d0 8086 const char *cname
94af9270 8087 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8088 &objfile->objfile_obstack);
9a619af0 8089
94af9270
KS
8090 if (cname != NULL)
8091 name = cname;
8092 }
8093 }
8094 }
8095
8096 return name;
8097}
8098
0114d602
DJ
8099/* Return the fully qualified name of DIE, based on its DW_AT_name.
8100 If scope qualifiers are appropriate they will be added. The result
8101 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8102 not have a name. NAME may either be from a previous call to
8103 dwarf2_name or NULL.
8104
0963b4bd 8105 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8106
8107static const char *
15d034d0 8108dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8109{
94af9270
KS
8110 return dwarf2_compute_name (name, die, cu, 0);
8111}
0114d602 8112
94af9270
KS
8113/* Construct a physname for the given DIE in CU. NAME may either be
8114 from a previous call to dwarf2_name or NULL. The result will be
8115 allocated on the objfile_objstack or NULL if the DIE does not have a
8116 name.
0114d602 8117
94af9270 8118 The output string will be canonicalized (if C++/Java). */
0114d602 8119
94af9270 8120static const char *
15d034d0 8121dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8122{
bb5ed363 8123 struct objfile *objfile = cu->objfile;
900e11f9
JK
8124 struct attribute *attr;
8125 const char *retval, *mangled = NULL, *canon = NULL;
8126 struct cleanup *back_to;
8127 int need_copy = 1;
8128
8129 /* In this case dwarf2_compute_name is just a shortcut not building anything
8130 on its own. */
8131 if (!die_needs_namespace (die, cu))
8132 return dwarf2_compute_name (name, die, cu, 1);
8133
8134 back_to = make_cleanup (null_cleanup, NULL);
8135
8136 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8137 if (!attr)
8138 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8139
8140 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8141 has computed. */
8142 if (attr && DW_STRING (attr))
8143 {
8144 char *demangled;
8145
8146 mangled = DW_STRING (attr);
8147
8148 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8149 type. It is easier for GDB users to search for such functions as
8150 `name(params)' than `long name(params)'. In such case the minimal
8151 symbol names do not match the full symbol names but for template
8152 functions there is never a need to look up their definition from their
8153 declaration so the only disadvantage remains the minimal symbol
8154 variant `long name(params)' does not have the proper inferior type.
8155 */
8156
a766d390
DE
8157 if (cu->language == language_go)
8158 {
8159 /* This is a lie, but we already lie to the caller new_symbol_full.
8160 new_symbol_full assumes we return the mangled name.
8161 This just undoes that lie until things are cleaned up. */
8162 demangled = NULL;
8163 }
8164 else
8165 {
8de20a37
TT
8166 demangled = gdb_demangle (mangled,
8167 (DMGL_PARAMS | DMGL_ANSI
8168 | (cu->language == language_java
8169 ? DMGL_JAVA | DMGL_RET_POSTFIX
8170 : DMGL_RET_DROP)));
a766d390 8171 }
900e11f9
JK
8172 if (demangled)
8173 {
8174 make_cleanup (xfree, demangled);
8175 canon = demangled;
8176 }
8177 else
8178 {
8179 canon = mangled;
8180 need_copy = 0;
8181 }
8182 }
8183
8184 if (canon == NULL || check_physname)
8185 {
8186 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8187
8188 if (canon != NULL && strcmp (physname, canon) != 0)
8189 {
8190 /* It may not mean a bug in GDB. The compiler could also
8191 compute DW_AT_linkage_name incorrectly. But in such case
8192 GDB would need to be bug-to-bug compatible. */
8193
8194 complaint (&symfile_complaints,
8195 _("Computed physname <%s> does not match demangled <%s> "
8196 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 8197 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
8198
8199 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8200 is available here - over computed PHYSNAME. It is safer
8201 against both buggy GDB and buggy compilers. */
8202
8203 retval = canon;
8204 }
8205 else
8206 {
8207 retval = physname;
8208 need_copy = 0;
8209 }
8210 }
8211 else
8212 retval = canon;
8213
8214 if (need_copy)
10f0c4bb 8215 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8216
8217 do_cleanups (back_to);
8218 return retval;
0114d602
DJ
8219}
8220
27aa8d6a
SW
8221/* Read the import statement specified by the given die and record it. */
8222
8223static void
8224read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8225{
bb5ed363 8226 struct objfile *objfile = cu->objfile;
27aa8d6a 8227 struct attribute *import_attr;
32019081 8228 struct die_info *imported_die, *child_die;
de4affc9 8229 struct dwarf2_cu *imported_cu;
27aa8d6a 8230 const char *imported_name;
794684b6 8231 const char *imported_name_prefix;
13387711
SW
8232 const char *canonical_name;
8233 const char *import_alias;
8234 const char *imported_declaration = NULL;
794684b6 8235 const char *import_prefix;
32019081
JK
8236 VEC (const_char_ptr) *excludes = NULL;
8237 struct cleanup *cleanups;
13387711 8238
27aa8d6a
SW
8239 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8240 if (import_attr == NULL)
8241 {
8242 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8243 dwarf_tag_name (die->tag));
8244 return;
8245 }
8246
de4affc9
CC
8247 imported_cu = cu;
8248 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8249 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8250 if (imported_name == NULL)
8251 {
8252 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8253
8254 The import in the following code:
8255 namespace A
8256 {
8257 typedef int B;
8258 }
8259
8260 int main ()
8261 {
8262 using A::B;
8263 B b;
8264 return b;
8265 }
8266
8267 ...
8268 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8269 <52> DW_AT_decl_file : 1
8270 <53> DW_AT_decl_line : 6
8271 <54> DW_AT_import : <0x75>
8272 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8273 <59> DW_AT_name : B
8274 <5b> DW_AT_decl_file : 1
8275 <5c> DW_AT_decl_line : 2
8276 <5d> DW_AT_type : <0x6e>
8277 ...
8278 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8279 <76> DW_AT_byte_size : 4
8280 <77> DW_AT_encoding : 5 (signed)
8281
8282 imports the wrong die ( 0x75 instead of 0x58 ).
8283 This case will be ignored until the gcc bug is fixed. */
8284 return;
8285 }
8286
82856980
SW
8287 /* Figure out the local name after import. */
8288 import_alias = dwarf2_name (die, cu);
27aa8d6a 8289
794684b6
SW
8290 /* Figure out where the statement is being imported to. */
8291 import_prefix = determine_prefix (die, cu);
8292
8293 /* Figure out what the scope of the imported die is and prepend it
8294 to the name of the imported die. */
de4affc9 8295 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8296
f55ee35c
JK
8297 if (imported_die->tag != DW_TAG_namespace
8298 && imported_die->tag != DW_TAG_module)
794684b6 8299 {
13387711
SW
8300 imported_declaration = imported_name;
8301 canonical_name = imported_name_prefix;
794684b6 8302 }
13387711 8303 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8304 canonical_name = obconcat (&objfile->objfile_obstack,
8305 imported_name_prefix, "::", imported_name,
8306 (char *) NULL);
13387711
SW
8307 else
8308 canonical_name = imported_name;
794684b6 8309
32019081
JK
8310 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8311
8312 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8313 for (child_die = die->child; child_die && child_die->tag;
8314 child_die = sibling_die (child_die))
8315 {
8316 /* DWARF-4: A Fortran use statement with a “rename list” may be
8317 represented by an imported module entry with an import attribute
8318 referring to the module and owned entries corresponding to those
8319 entities that are renamed as part of being imported. */
8320
8321 if (child_die->tag != DW_TAG_imported_declaration)
8322 {
8323 complaint (&symfile_complaints,
8324 _("child DW_TAG_imported_declaration expected "
8325 "- DIE at 0x%x [in module %s]"),
b64f50a1 8326 child_die->offset.sect_off, objfile->name);
32019081
JK
8327 continue;
8328 }
8329
8330 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8331 if (import_attr == NULL)
8332 {
8333 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8334 dwarf_tag_name (child_die->tag));
8335 continue;
8336 }
8337
8338 imported_cu = cu;
8339 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8340 &imported_cu);
8341 imported_name = dwarf2_name (imported_die, imported_cu);
8342 if (imported_name == NULL)
8343 {
8344 complaint (&symfile_complaints,
8345 _("child DW_TAG_imported_declaration has unknown "
8346 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 8347 child_die->offset.sect_off, objfile->name);
32019081
JK
8348 continue;
8349 }
8350
8351 VEC_safe_push (const_char_ptr, excludes, imported_name);
8352
8353 process_die (child_die, cu);
8354 }
8355
c0cc3a76
SW
8356 cp_add_using_directive (import_prefix,
8357 canonical_name,
8358 import_alias,
13387711 8359 imported_declaration,
32019081 8360 excludes,
12aaed36 8361 0,
bb5ed363 8362 &objfile->objfile_obstack);
32019081
JK
8363
8364 do_cleanups (cleanups);
27aa8d6a
SW
8365}
8366
f4dc4d17 8367/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8368
cb1df416
DJ
8369static void
8370free_cu_line_header (void *arg)
8371{
8372 struct dwarf2_cu *cu = arg;
8373
8374 free_line_header (cu->line_header);
8375 cu->line_header = NULL;
8376}
8377
1b80a9fa
JK
8378/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8379 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8380 this, it was first present in GCC release 4.3.0. */
8381
8382static int
8383producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8384{
8385 if (!cu->checked_producer)
8386 check_producer (cu);
8387
8388 return cu->producer_is_gcc_lt_4_3;
8389}
8390
9291a0cd
TT
8391static void
8392find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8393 const char **name, const char **comp_dir)
9291a0cd
TT
8394{
8395 struct attribute *attr;
8396
8397 *name = NULL;
8398 *comp_dir = NULL;
8399
8400 /* Find the filename. Do not use dwarf2_name here, since the filename
8401 is not a source language identifier. */
8402 attr = dwarf2_attr (die, DW_AT_name, cu);
8403 if (attr)
8404 {
8405 *name = DW_STRING (attr);
8406 }
8407
8408 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8409 if (attr)
8410 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8411 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8412 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8413 {
15d034d0
TT
8414 char *d = ldirname (*name);
8415
8416 *comp_dir = d;
8417 if (d != NULL)
8418 make_cleanup (xfree, d);
9291a0cd
TT
8419 }
8420 if (*comp_dir != NULL)
8421 {
8422 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8423 directory, get rid of it. */
8424 char *cp = strchr (*comp_dir, ':');
8425
8426 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8427 *comp_dir = cp + 1;
8428 }
8429
8430 if (*name == NULL)
8431 *name = "<unknown>";
8432}
8433
f4dc4d17
DE
8434/* Handle DW_AT_stmt_list for a compilation unit.
8435 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8436 COMP_DIR is the compilation directory.
8437 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8438
8439static void
8440handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8441 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8442{
8443 struct attribute *attr;
2ab95328 8444
f4dc4d17
DE
8445 gdb_assert (! cu->per_cu->is_debug_types);
8446
2ab95328
TT
8447 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8448 if (attr)
8449 {
8450 unsigned int line_offset = DW_UNSND (attr);
8451 struct line_header *line_header
3019eac3 8452 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8453
8454 if (line_header)
dee91e82
DE
8455 {
8456 cu->line_header = line_header;
8457 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8458 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8459 }
2ab95328
TT
8460 }
8461}
8462
95554aad 8463/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8464
c906108c 8465static void
e7c27a73 8466read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8467{
dee91e82 8468 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8469 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8470 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8471 CORE_ADDR highpc = ((CORE_ADDR) 0);
8472 struct attribute *attr;
15d034d0
TT
8473 const char *name = NULL;
8474 const char *comp_dir = NULL;
c906108c
SS
8475 struct die_info *child_die;
8476 bfd *abfd = objfile->obfd;
e142c38c 8477 CORE_ADDR baseaddr;
6e70227d 8478
e142c38c 8479 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8480
fae299cd 8481 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8482
8483 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8484 from finish_block. */
2acceee2 8485 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8486 lowpc = highpc;
8487 lowpc += baseaddr;
8488 highpc += baseaddr;
8489
9291a0cd 8490 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8491
95554aad 8492 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8493
f4b8a18d
KW
8494 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8495 standardised yet. As a workaround for the language detection we fall
8496 back to the DW_AT_producer string. */
8497 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8498 cu->language = language_opencl;
8499
3019eac3
DE
8500 /* Similar hack for Go. */
8501 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8502 set_cu_language (DW_LANG_Go, cu);
8503
f4dc4d17 8504 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8505
8506 /* Decode line number information if present. We do this before
8507 processing child DIEs, so that the line header table is available
8508 for DW_AT_decl_file. */
f4dc4d17 8509 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8510
8511 /* Process all dies in compilation unit. */
8512 if (die->child != NULL)
8513 {
8514 child_die = die->child;
8515 while (child_die && child_die->tag)
8516 {
8517 process_die (child_die, cu);
8518 child_die = sibling_die (child_die);
8519 }
8520 }
8521
8522 /* Decode macro information, if present. Dwarf 2 macro information
8523 refers to information in the line number info statement program
8524 header, so we can only read it if we've read the header
8525 successfully. */
8526 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8527 if (attr && cu->line_header)
8528 {
8529 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8530 complaint (&symfile_complaints,
8531 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8532
09262596 8533 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8534 }
8535 else
8536 {
8537 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8538 if (attr && cu->line_header)
8539 {
8540 unsigned int macro_offset = DW_UNSND (attr);
8541
09262596 8542 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8543 }
8544 }
8545
8546 do_cleanups (back_to);
8547}
8548
f4dc4d17
DE
8549/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8550 Create the set of symtabs used by this TU, or if this TU is sharing
8551 symtabs with another TU and the symtabs have already been created
8552 then restore those symtabs in the line header.
8553 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8554
8555static void
f4dc4d17 8556setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8557{
f4dc4d17
DE
8558 struct objfile *objfile = dwarf2_per_objfile->objfile;
8559 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8560 struct type_unit_group *tu_group;
8561 int first_time;
8562 struct line_header *lh;
3019eac3 8563 struct attribute *attr;
f4dc4d17 8564 unsigned int i, line_offset;
0186c6a7 8565 struct signatured_type *sig_type;
3019eac3 8566
f4dc4d17 8567 gdb_assert (per_cu->is_debug_types);
0186c6a7 8568 sig_type = (struct signatured_type *) per_cu;
3019eac3 8569
f4dc4d17 8570 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8571
f4dc4d17 8572 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8573 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8574 if (sig_type->type_unit_group == NULL)
8575 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8576 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8577
8578 /* If we've already processed this stmt_list there's no real need to
8579 do it again, we could fake it and just recreate the part we need
8580 (file name,index -> symtab mapping). If data shows this optimization
8581 is useful we can do it then. */
8582 first_time = tu_group->primary_symtab == NULL;
8583
8584 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8585 debug info. */
8586 lh = NULL;
8587 if (attr != NULL)
3019eac3 8588 {
f4dc4d17
DE
8589 line_offset = DW_UNSND (attr);
8590 lh = dwarf_decode_line_header (line_offset, cu);
8591 }
8592 if (lh == NULL)
8593 {
8594 if (first_time)
8595 dwarf2_start_symtab (cu, "", NULL, 0);
8596 else
8597 {
8598 gdb_assert (tu_group->symtabs == NULL);
8599 restart_symtab (0);
8600 }
8601 /* Note: The primary symtab will get allocated at the end. */
8602 return;
3019eac3
DE
8603 }
8604
f4dc4d17
DE
8605 cu->line_header = lh;
8606 make_cleanup (free_cu_line_header, cu);
3019eac3 8607
f4dc4d17
DE
8608 if (first_time)
8609 {
8610 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8611
f4dc4d17
DE
8612 tu_group->num_symtabs = lh->num_file_names;
8613 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8614
f4dc4d17
DE
8615 for (i = 0; i < lh->num_file_names; ++i)
8616 {
d521ce57 8617 const char *dir = NULL;
f4dc4d17 8618 struct file_entry *fe = &lh->file_names[i];
3019eac3 8619
f4dc4d17
DE
8620 if (fe->dir_index)
8621 dir = lh->include_dirs[fe->dir_index - 1];
8622 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8623
f4dc4d17
DE
8624 /* Note: We don't have to watch for the main subfile here, type units
8625 don't have DW_AT_name. */
3019eac3 8626
f4dc4d17
DE
8627 if (current_subfile->symtab == NULL)
8628 {
8629 /* NOTE: start_subfile will recognize when it's been passed
8630 a file it has already seen. So we can't assume there's a
8631 simple mapping from lh->file_names to subfiles,
8632 lh->file_names may contain dups. */
8633 current_subfile->symtab = allocate_symtab (current_subfile->name,
8634 objfile);
8635 }
8636
8637 fe->symtab = current_subfile->symtab;
8638 tu_group->symtabs[i] = fe->symtab;
8639 }
8640 }
8641 else
3019eac3 8642 {
f4dc4d17
DE
8643 restart_symtab (0);
8644
8645 for (i = 0; i < lh->num_file_names; ++i)
8646 {
8647 struct file_entry *fe = &lh->file_names[i];
8648
8649 fe->symtab = tu_group->symtabs[i];
8650 }
3019eac3
DE
8651 }
8652
f4dc4d17
DE
8653 /* The main symtab is allocated last. Type units don't have DW_AT_name
8654 so they don't have a "real" (so to speak) symtab anyway.
8655 There is later code that will assign the main symtab to all symbols
8656 that don't have one. We need to handle the case of a symbol with a
8657 missing symtab (DW_AT_decl_file) anyway. */
8658}
3019eac3 8659
f4dc4d17
DE
8660/* Process DW_TAG_type_unit.
8661 For TUs we want to skip the first top level sibling if it's not the
8662 actual type being defined by this TU. In this case the first top
8663 level sibling is there to provide context only. */
3019eac3 8664
f4dc4d17
DE
8665static void
8666read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8667{
8668 struct die_info *child_die;
3019eac3 8669
f4dc4d17
DE
8670 prepare_one_comp_unit (cu, die, language_minimal);
8671
8672 /* Initialize (or reinitialize) the machinery for building symtabs.
8673 We do this before processing child DIEs, so that the line header table
8674 is available for DW_AT_decl_file. */
8675 setup_type_unit_groups (die, cu);
8676
8677 if (die->child != NULL)
8678 {
8679 child_die = die->child;
8680 while (child_die && child_die->tag)
8681 {
8682 process_die (child_die, cu);
8683 child_die = sibling_die (child_die);
8684 }
8685 }
3019eac3
DE
8686}
8687\f
80626a55
DE
8688/* DWO/DWP files.
8689
8690 http://gcc.gnu.org/wiki/DebugFission
8691 http://gcc.gnu.org/wiki/DebugFissionDWP
8692
8693 To simplify handling of both DWO files ("object" files with the DWARF info)
8694 and DWP files (a file with the DWOs packaged up into one file), we treat
8695 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8696
8697static hashval_t
8698hash_dwo_file (const void *item)
8699{
8700 const struct dwo_file *dwo_file = item;
a2ce51a0 8701 hashval_t hash;
3019eac3 8702
a2ce51a0
DE
8703 hash = htab_hash_string (dwo_file->dwo_name);
8704 if (dwo_file->comp_dir != NULL)
8705 hash += htab_hash_string (dwo_file->comp_dir);
8706 return hash;
3019eac3
DE
8707}
8708
8709static int
8710eq_dwo_file (const void *item_lhs, const void *item_rhs)
8711{
8712 const struct dwo_file *lhs = item_lhs;
8713 const struct dwo_file *rhs = item_rhs;
8714
a2ce51a0
DE
8715 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8716 return 0;
8717 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8718 return lhs->comp_dir == rhs->comp_dir;
8719 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8720}
8721
8722/* Allocate a hash table for DWO files. */
8723
8724static htab_t
8725allocate_dwo_file_hash_table (void)
8726{
8727 struct objfile *objfile = dwarf2_per_objfile->objfile;
8728
8729 return htab_create_alloc_ex (41,
8730 hash_dwo_file,
8731 eq_dwo_file,
8732 NULL,
8733 &objfile->objfile_obstack,
8734 hashtab_obstack_allocate,
8735 dummy_obstack_deallocate);
8736}
8737
80626a55
DE
8738/* Lookup DWO file DWO_NAME. */
8739
8740static void **
0ac5b59e 8741lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8742{
8743 struct dwo_file find_entry;
8744 void **slot;
8745
8746 if (dwarf2_per_objfile->dwo_files == NULL)
8747 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8748
8749 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8750 find_entry.dwo_name = dwo_name;
8751 find_entry.comp_dir = comp_dir;
80626a55
DE
8752 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8753
8754 return slot;
8755}
8756
3019eac3
DE
8757static hashval_t
8758hash_dwo_unit (const void *item)
8759{
8760 const struct dwo_unit *dwo_unit = item;
8761
8762 /* This drops the top 32 bits of the id, but is ok for a hash. */
8763 return dwo_unit->signature;
8764}
8765
8766static int
8767eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8768{
8769 const struct dwo_unit *lhs = item_lhs;
8770 const struct dwo_unit *rhs = item_rhs;
8771
8772 /* The signature is assumed to be unique within the DWO file.
8773 So while object file CU dwo_id's always have the value zero,
8774 that's OK, assuming each object file DWO file has only one CU,
8775 and that's the rule for now. */
8776 return lhs->signature == rhs->signature;
8777}
8778
8779/* Allocate a hash table for DWO CUs,TUs.
8780 There is one of these tables for each of CUs,TUs for each DWO file. */
8781
8782static htab_t
8783allocate_dwo_unit_table (struct objfile *objfile)
8784{
8785 /* Start out with a pretty small number.
8786 Generally DWO files contain only one CU and maybe some TUs. */
8787 return htab_create_alloc_ex (3,
8788 hash_dwo_unit,
8789 eq_dwo_unit,
8790 NULL,
8791 &objfile->objfile_obstack,
8792 hashtab_obstack_allocate,
8793 dummy_obstack_deallocate);
8794}
8795
80626a55 8796/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8797
19c3d4c9 8798struct create_dwo_cu_data
3019eac3
DE
8799{
8800 struct dwo_file *dwo_file;
19c3d4c9 8801 struct dwo_unit dwo_unit;
3019eac3
DE
8802};
8803
19c3d4c9 8804/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8805
8806static void
19c3d4c9
DE
8807create_dwo_cu_reader (const struct die_reader_specs *reader,
8808 const gdb_byte *info_ptr,
8809 struct die_info *comp_unit_die,
8810 int has_children,
8811 void *datap)
3019eac3
DE
8812{
8813 struct dwarf2_cu *cu = reader->cu;
8814 struct objfile *objfile = dwarf2_per_objfile->objfile;
8815 sect_offset offset = cu->per_cu->offset;
8a0459fd 8816 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8817 struct create_dwo_cu_data *data = datap;
3019eac3 8818 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8819 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8820 struct attribute *attr;
3019eac3
DE
8821
8822 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8823 if (attr == NULL)
8824 {
19c3d4c9
DE
8825 complaint (&symfile_complaints,
8826 _("Dwarf Error: debug entry at offset 0x%x is missing"
8827 " its dwo_id [in module %s]"),
8828 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8829 return;
8830 }
8831
3019eac3
DE
8832 dwo_unit->dwo_file = dwo_file;
8833 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8834 dwo_unit->section = section;
3019eac3
DE
8835 dwo_unit->offset = offset;
8836 dwo_unit->length = cu->per_cu->length;
8837
09406207 8838 if (dwarf2_read_debug)
4031ecc5
DE
8839 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8840 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8841}
8842
19c3d4c9
DE
8843/* Create the dwo_unit for the lone CU in DWO_FILE.
8844 Note: This function processes DWO files only, not DWP files. */
3019eac3 8845
19c3d4c9
DE
8846static struct dwo_unit *
8847create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8848{
8849 struct objfile *objfile = dwarf2_per_objfile->objfile;
8850 struct dwarf2_section_info *section = &dwo_file->sections.info;
8851 bfd *abfd;
8852 htab_t cu_htab;
d521ce57 8853 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8854 struct create_dwo_cu_data create_dwo_cu_data;
8855 struct dwo_unit *dwo_unit;
3019eac3
DE
8856
8857 dwarf2_read_section (objfile, section);
8858 info_ptr = section->buffer;
8859
8860 if (info_ptr == NULL)
8861 return NULL;
8862
8863 /* We can't set abfd until now because the section may be empty or
8864 not present, in which case section->asection will be NULL. */
8865 abfd = section->asection->owner;
8866
09406207 8867 if (dwarf2_read_debug)
19c3d4c9
DE
8868 {
8869 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8870 bfd_section_name (abfd, section->asection),
8871 bfd_get_filename (abfd));
8872 }
3019eac3 8873
19c3d4c9
DE
8874 create_dwo_cu_data.dwo_file = dwo_file;
8875 dwo_unit = NULL;
3019eac3
DE
8876
8877 end_ptr = info_ptr + section->size;
8878 while (info_ptr < end_ptr)
8879 {
8880 struct dwarf2_per_cu_data per_cu;
8881
19c3d4c9
DE
8882 memset (&create_dwo_cu_data.dwo_unit, 0,
8883 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8884 memset (&per_cu, 0, sizeof (per_cu));
8885 per_cu.objfile = objfile;
8886 per_cu.is_debug_types = 0;
8887 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8888 per_cu.section = section;
3019eac3
DE
8889
8890 init_cutu_and_read_dies_no_follow (&per_cu,
8891 &dwo_file->sections.abbrev,
8892 dwo_file,
19c3d4c9
DE
8893 create_dwo_cu_reader,
8894 &create_dwo_cu_data);
8895
8896 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8897 {
8898 /* If we've already found one, complain. We only support one
8899 because having more than one requires hacking the dwo_name of
8900 each to match, which is highly unlikely to happen. */
8901 if (dwo_unit != NULL)
8902 {
8903 complaint (&symfile_complaints,
8904 _("Multiple CUs in DWO file %s [in module %s]"),
8905 dwo_file->dwo_name, objfile->name);
8906 break;
8907 }
8908
8909 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8910 *dwo_unit = create_dwo_cu_data.dwo_unit;
8911 }
3019eac3
DE
8912
8913 info_ptr += per_cu.length;
8914 }
8915
19c3d4c9 8916 return dwo_unit;
3019eac3
DE
8917}
8918
80626a55
DE
8919/* DWP file .debug_{cu,tu}_index section format:
8920 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8921
d2415c6c
DE
8922 DWP Version 1:
8923
80626a55
DE
8924 Both index sections have the same format, and serve to map a 64-bit
8925 signature to a set of section numbers. Each section begins with a header,
8926 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8927 indexes, and a pool of 32-bit section numbers. The index sections will be
8928 aligned at 8-byte boundaries in the file.
8929
d2415c6c
DE
8930 The index section header consists of:
8931
8932 V, 32 bit version number
8933 -, 32 bits unused
8934 N, 32 bit number of compilation units or type units in the index
8935 M, 32 bit number of slots in the hash table
80626a55 8936
d2415c6c 8937 Numbers are recorded using the byte order of the application binary.
80626a55 8938
d2415c6c 8939 We assume that N and M will not exceed 2^32 - 1.
80626a55 8940
d2415c6c 8941 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8942
d2415c6c
DE
8943 The hash table begins at offset 16 in the section, and consists of an array
8944 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8945 order of the application binary). Unused slots in the hash table are 0.
8946 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8947
d2415c6c
DE
8948 The parallel table begins immediately after the hash table
8949 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8950 array of 32-bit indexes (using the byte order of the application binary),
8951 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8952 table contains a 32-bit index into the pool of section numbers. For unused
8953 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8954
d2415c6c
DE
8955 Given a 64-bit compilation unit signature or a type signature S, an entry
8956 in the hash table is located as follows:
80626a55 8957
d2415c6c
DE
8958 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8959 the low-order k bits all set to 1.
80626a55 8960
d2415c6c 8961 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8962
d2415c6c
DE
8963 3) If the hash table entry at index H matches the signature, use that
8964 entry. If the hash table entry at index H is unused (all zeroes),
8965 terminate the search: the signature is not present in the table.
80626a55 8966
d2415c6c 8967 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8968
d2415c6c
DE
8969 Because M > N and H' and M are relatively prime, the search is guaranteed
8970 to stop at an unused slot or find the match.
80626a55 8971
d2415c6c
DE
8972 The pool of section numbers begins immediately following the hash table
8973 (at offset 16 + 12 * M from the beginning of the section). The pool of
8974 section numbers consists of an array of 32-bit words (using the byte order
8975 of the application binary). Each item in the array is indexed starting
8976 from 0. The hash table entry provides the index of the first section
8977 number in the set. Additional section numbers in the set follow, and the
8978 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 8979
d2415c6c
DE
8980 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8981 section must be the first entry in the set, and the .debug_abbrev.dwo must
8982 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
8983
8984/* Create a hash table to map DWO IDs to their CU/TU entry in
8985 .debug_{info,types}.dwo in DWP_FILE.
8986 Returns NULL if there isn't one.
8987 Note: This function processes DWP files only, not DWO files. */
8988
8989static struct dwp_hash_table *
8990create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8991{
8992 struct objfile *objfile = dwarf2_per_objfile->objfile;
8993 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8994 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8995 struct dwarf2_section_info *index;
8996 uint32_t version, nr_units, nr_slots;
8997 struct dwp_hash_table *htab;
8998
8999 if (is_debug_types)
9000 index = &dwp_file->sections.tu_index;
9001 else
9002 index = &dwp_file->sections.cu_index;
9003
9004 if (dwarf2_section_empty_p (index))
9005 return NULL;
9006 dwarf2_read_section (objfile, index);
9007
9008 index_ptr = index->buffer;
9009 index_end = index_ptr + index->size;
9010
9011 version = read_4_bytes (dbfd, index_ptr);
9012 index_ptr += 8; /* Skip the unused word. */
9013 nr_units = read_4_bytes (dbfd, index_ptr);
9014 index_ptr += 4;
9015 nr_slots = read_4_bytes (dbfd, index_ptr);
9016 index_ptr += 4;
9017
9018 if (version != 1)
9019 {
21aa081e 9020 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9021 " [in module %s]"),
21aa081e 9022 pulongest (version), dwp_file->name);
80626a55
DE
9023 }
9024 if (nr_slots != (nr_slots & -nr_slots))
9025 {
21aa081e 9026 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9027 " is not power of 2 [in module %s]"),
21aa081e 9028 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9029 }
9030
9031 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9032 htab->nr_units = nr_units;
9033 htab->nr_slots = nr_slots;
9034 htab->hash_table = index_ptr;
9035 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9036 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9037
9038 return htab;
9039}
9040
9041/* Update SECTIONS with the data from SECTP.
9042
9043 This function is like the other "locate" section routines that are
9044 passed to bfd_map_over_sections, but in this context the sections to
9045 read comes from the DWP hash table, not the full ELF section table.
9046
9047 The result is non-zero for success, or zero if an error was found. */
9048
9049static int
9050locate_virtual_dwo_sections (asection *sectp,
9051 struct virtual_dwo_sections *sections)
9052{
9053 const struct dwop_section_names *names = &dwop_section_names;
9054
9055 if (section_is_p (sectp->name, &names->abbrev_dwo))
9056 {
9057 /* There can be only one. */
9058 if (sections->abbrev.asection != NULL)
9059 return 0;
9060 sections->abbrev.asection = sectp;
9061 sections->abbrev.size = bfd_get_section_size (sectp);
9062 }
9063 else if (section_is_p (sectp->name, &names->info_dwo)
9064 || section_is_p (sectp->name, &names->types_dwo))
9065 {
9066 /* There can be only one. */
9067 if (sections->info_or_types.asection != NULL)
9068 return 0;
9069 sections->info_or_types.asection = sectp;
9070 sections->info_or_types.size = bfd_get_section_size (sectp);
9071 }
9072 else if (section_is_p (sectp->name, &names->line_dwo))
9073 {
9074 /* There can be only one. */
9075 if (sections->line.asection != NULL)
9076 return 0;
9077 sections->line.asection = sectp;
9078 sections->line.size = bfd_get_section_size (sectp);
9079 }
9080 else if (section_is_p (sectp->name, &names->loc_dwo))
9081 {
9082 /* There can be only one. */
9083 if (sections->loc.asection != NULL)
9084 return 0;
9085 sections->loc.asection = sectp;
9086 sections->loc.size = bfd_get_section_size (sectp);
9087 }
9088 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9089 {
9090 /* There can be only one. */
9091 if (sections->macinfo.asection != NULL)
9092 return 0;
9093 sections->macinfo.asection = sectp;
9094 sections->macinfo.size = bfd_get_section_size (sectp);
9095 }
9096 else if (section_is_p (sectp->name, &names->macro_dwo))
9097 {
9098 /* There can be only one. */
9099 if (sections->macro.asection != NULL)
9100 return 0;
9101 sections->macro.asection = sectp;
9102 sections->macro.size = bfd_get_section_size (sectp);
9103 }
9104 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9105 {
9106 /* There can be only one. */
9107 if (sections->str_offsets.asection != NULL)
9108 return 0;
9109 sections->str_offsets.asection = sectp;
9110 sections->str_offsets.size = bfd_get_section_size (sectp);
9111 }
9112 else
9113 {
9114 /* No other kind of section is valid. */
9115 return 0;
9116 }
9117
9118 return 1;
9119}
9120
9121/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9122 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9123 SECTION_INDEX is the index of the DWO in HTAB.
9124 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9125
9126static struct dwo_unit *
9127create_dwo_in_dwp (struct dwp_file *dwp_file,
9128 const struct dwp_hash_table *htab,
9129 uint32_t section_index,
0ac5b59e 9130 const char *comp_dir,
80626a55
DE
9131 ULONGEST signature, int is_debug_types)
9132{
9133 struct objfile *objfile = dwarf2_per_objfile->objfile;
9134 bfd *dbfd = dwp_file->dbfd;
9135 const char *kind = is_debug_types ? "TU" : "CU";
9136 struct dwo_file *dwo_file;
9137 struct dwo_unit *dwo_unit;
9138 struct virtual_dwo_sections sections;
9139 void **dwo_file_slot;
9140 char *virtual_dwo_name;
9141 struct dwarf2_section_info *cutu;
9142 struct cleanup *cleanups;
9143 int i;
9144
9145 if (dwarf2_read_debug)
9146 {
21aa081e 9147 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9148 kind,
21aa081e 9149 pulongest (section_index), hex_string (signature),
80626a55
DE
9150 dwp_file->name);
9151 }
9152
9153 /* Fetch the sections of this DWO.
9154 Put a limit on the number of sections we look for so that bad data
9155 doesn't cause us to loop forever. */
9156
9157#define MAX_NR_DWO_SECTIONS \
9158 (1 /* .debug_info or .debug_types */ \
9159 + 1 /* .debug_abbrev */ \
9160 + 1 /* .debug_line */ \
9161 + 1 /* .debug_loc */ \
9162 + 1 /* .debug_str_offsets */ \
9163 + 1 /* .debug_macro */ \
9164 + 1 /* .debug_macinfo */ \
9165 + 1 /* trailing zero */)
9166
9167 memset (&sections, 0, sizeof (sections));
9168 cleanups = make_cleanup (null_cleanup, 0);
9169
9170 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9171 {
9172 asection *sectp;
9173 uint32_t section_nr =
9174 read_4_bytes (dbfd,
9175 htab->section_pool
9176 + (section_index + i) * sizeof (uint32_t));
9177
9178 if (section_nr == 0)
9179 break;
9180 if (section_nr >= dwp_file->num_sections)
9181 {
9182 error (_("Dwarf Error: bad DWP hash table, section number too large"
9183 " [in module %s]"),
9184 dwp_file->name);
9185 }
9186
9187 sectp = dwp_file->elf_sections[section_nr];
9188 if (! locate_virtual_dwo_sections (sectp, &sections))
9189 {
9190 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9191 " [in module %s]"),
9192 dwp_file->name);
9193 }
9194 }
9195
9196 if (i < 2
9197 || sections.info_or_types.asection == NULL
9198 || sections.abbrev.asection == NULL)
9199 {
9200 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9201 " [in module %s]"),
9202 dwp_file->name);
9203 }
9204 if (i == MAX_NR_DWO_SECTIONS)
9205 {
9206 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9207 " [in module %s]"),
9208 dwp_file->name);
9209 }
9210
9211 /* It's easier for the rest of the code if we fake a struct dwo_file and
9212 have dwo_unit "live" in that. At least for now.
9213
9214 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9215 However, for each CU + set of TUs that came from the same original DWO
9216 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9217 (fewer struct dwo_file objects to allocated). Remember that for really
9218 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9219
2792b94d
PM
9220 virtual_dwo_name =
9221 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9222 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9223 sections.line.asection ? sections.line.asection->id : 0,
9224 sections.loc.asection ? sections.loc.asection->id : 0,
9225 (sections.str_offsets.asection
9226 ? sections.str_offsets.asection->id
9227 : 0));
80626a55
DE
9228 make_cleanup (xfree, virtual_dwo_name);
9229 /* Can we use an existing virtual DWO file? */
0ac5b59e 9230 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9231 /* Create one if necessary. */
9232 if (*dwo_file_slot == NULL)
9233 {
9234 if (dwarf2_read_debug)
9235 {
9236 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9237 virtual_dwo_name);
9238 }
9239 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9240 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9241 virtual_dwo_name,
9242 strlen (virtual_dwo_name));
9243 dwo_file->comp_dir = comp_dir;
80626a55
DE
9244 dwo_file->sections.abbrev = sections.abbrev;
9245 dwo_file->sections.line = sections.line;
9246 dwo_file->sections.loc = sections.loc;
9247 dwo_file->sections.macinfo = sections.macinfo;
9248 dwo_file->sections.macro = sections.macro;
9249 dwo_file->sections.str_offsets = sections.str_offsets;
9250 /* The "str" section is global to the entire DWP file. */
9251 dwo_file->sections.str = dwp_file->sections.str;
9252 /* The info or types section is assigned later to dwo_unit,
9253 there's no need to record it in dwo_file.
9254 Also, we can't simply record type sections in dwo_file because
9255 we record a pointer into the vector in dwo_unit. As we collect more
9256 types we'll grow the vector and eventually have to reallocate space
9257 for it, invalidating all the pointers into the current copy. */
9258 *dwo_file_slot = dwo_file;
9259 }
9260 else
9261 {
9262 if (dwarf2_read_debug)
9263 {
9264 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9265 virtual_dwo_name);
9266 }
9267 dwo_file = *dwo_file_slot;
9268 }
9269 do_cleanups (cleanups);
9270
9271 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9272 dwo_unit->dwo_file = dwo_file;
9273 dwo_unit->signature = signature;
8a0459fd
DE
9274 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9275 sizeof (struct dwarf2_section_info));
9276 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9277 /* offset, length, type_offset_in_tu are set later. */
9278
9279 return dwo_unit;
9280}
9281
9282/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9283
9284static struct dwo_unit *
9285lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9286 const struct dwp_hash_table *htab,
0ac5b59e 9287 const char *comp_dir,
80626a55
DE
9288 ULONGEST signature, int is_debug_types)
9289{
9290 bfd *dbfd = dwp_file->dbfd;
9291 uint32_t mask = htab->nr_slots - 1;
9292 uint32_t hash = signature & mask;
9293 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9294 unsigned int i;
9295 void **slot;
9296 struct dwo_unit find_dwo_cu, *dwo_cu;
9297
9298 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9299 find_dwo_cu.signature = signature;
9300 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9301
9302 if (*slot != NULL)
9303 return *slot;
9304
9305 /* Use a for loop so that we don't loop forever on bad debug info. */
9306 for (i = 0; i < htab->nr_slots; ++i)
9307 {
9308 ULONGEST signature_in_table;
9309
9310 signature_in_table =
9311 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9312 if (signature_in_table == signature)
9313 {
9314 uint32_t section_index =
9315 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9316
9317 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9318 comp_dir, signature, is_debug_types);
80626a55
DE
9319 return *slot;
9320 }
9321 if (signature_in_table == 0)
9322 return NULL;
9323 hash = (hash + hash2) & mask;
9324 }
9325
9326 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9327 " [in module %s]"),
9328 dwp_file->name);
9329}
9330
ab5088bf 9331/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9332 Open the file specified by FILE_NAME and hand it off to BFD for
9333 preliminary analysis. Return a newly initialized bfd *, which
9334 includes a canonicalized copy of FILE_NAME.
80626a55 9335 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9336 SEARCH_CWD is true if the current directory is to be searched.
9337 It will be searched before debug-file-directory.
9338 If unable to find/open the file, return NULL.
3019eac3
DE
9339 NOTE: This function is derived from symfile_bfd_open. */
9340
9341static bfd *
6ac97d4c 9342try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9343{
9344 bfd *sym_bfd;
80626a55 9345 int desc, flags;
3019eac3 9346 char *absolute_name;
9c02c129
DE
9347 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9348 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9349 to debug_file_directory. */
9350 char *search_path;
9351 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9352
6ac97d4c
DE
9353 if (search_cwd)
9354 {
9355 if (*debug_file_directory != '\0')
9356 search_path = concat (".", dirname_separator_string,
9357 debug_file_directory, NULL);
9358 else
9359 search_path = xstrdup (".");
9360 }
9c02c129 9361 else
6ac97d4c 9362 search_path = xstrdup (debug_file_directory);
3019eac3 9363
9c02c129 9364 flags = 0;
80626a55
DE
9365 if (is_dwp)
9366 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9367 desc = openp (search_path, flags, file_name,
3019eac3 9368 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9369 xfree (search_path);
3019eac3
DE
9370 if (desc < 0)
9371 return NULL;
9372
bb397797 9373 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9374 xfree (absolute_name);
9c02c129
DE
9375 if (sym_bfd == NULL)
9376 return NULL;
3019eac3
DE
9377 bfd_set_cacheable (sym_bfd, 1);
9378
9379 if (!bfd_check_format (sym_bfd, bfd_object))
9380 {
cbb099e8 9381 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9382 return NULL;
9383 }
9384
3019eac3
DE
9385 return sym_bfd;
9386}
9387
ab5088bf 9388/* Try to open DWO file FILE_NAME.
3019eac3
DE
9389 COMP_DIR is the DW_AT_comp_dir attribute.
9390 The result is the bfd handle of the file.
9391 If there is a problem finding or opening the file, return NULL.
9392 Upon success, the canonicalized path of the file is stored in the bfd,
9393 same as symfile_bfd_open. */
9394
9395static bfd *
ab5088bf 9396open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9397{
9398 bfd *abfd;
3019eac3 9399
80626a55 9400 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9401 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9402
9403 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9404
9405 if (comp_dir != NULL)
9406 {
80626a55 9407 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9408
9409 /* NOTE: If comp_dir is a relative path, this will also try the
9410 search path, which seems useful. */
6ac97d4c 9411 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9412 xfree (path_to_try);
9413 if (abfd != NULL)
9414 return abfd;
9415 }
9416
9417 /* That didn't work, try debug-file-directory, which, despite its name,
9418 is a list of paths. */
9419
9420 if (*debug_file_directory == '\0')
9421 return NULL;
9422
6ac97d4c 9423 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9424}
9425
80626a55
DE
9426/* This function is mapped across the sections and remembers the offset and
9427 size of each of the DWO debugging sections we are interested in. */
9428
9429static void
9430dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9431{
9432 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9433 const struct dwop_section_names *names = &dwop_section_names;
9434
9435 if (section_is_p (sectp->name, &names->abbrev_dwo))
9436 {
9437 dwo_sections->abbrev.asection = sectp;
9438 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9439 }
9440 else if (section_is_p (sectp->name, &names->info_dwo))
9441 {
9442 dwo_sections->info.asection = sectp;
9443 dwo_sections->info.size = bfd_get_section_size (sectp);
9444 }
9445 else if (section_is_p (sectp->name, &names->line_dwo))
9446 {
9447 dwo_sections->line.asection = sectp;
9448 dwo_sections->line.size = bfd_get_section_size (sectp);
9449 }
9450 else if (section_is_p (sectp->name, &names->loc_dwo))
9451 {
9452 dwo_sections->loc.asection = sectp;
9453 dwo_sections->loc.size = bfd_get_section_size (sectp);
9454 }
9455 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9456 {
9457 dwo_sections->macinfo.asection = sectp;
9458 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9459 }
9460 else if (section_is_p (sectp->name, &names->macro_dwo))
9461 {
9462 dwo_sections->macro.asection = sectp;
9463 dwo_sections->macro.size = bfd_get_section_size (sectp);
9464 }
9465 else if (section_is_p (sectp->name, &names->str_dwo))
9466 {
9467 dwo_sections->str.asection = sectp;
9468 dwo_sections->str.size = bfd_get_section_size (sectp);
9469 }
9470 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9471 {
9472 dwo_sections->str_offsets.asection = sectp;
9473 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9474 }
9475 else if (section_is_p (sectp->name, &names->types_dwo))
9476 {
9477 struct dwarf2_section_info type_section;
9478
9479 memset (&type_section, 0, sizeof (type_section));
9480 type_section.asection = sectp;
9481 type_section.size = bfd_get_section_size (sectp);
9482 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9483 &type_section);
9484 }
9485}
9486
ab5088bf 9487/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9488 by PER_CU. This is for the non-DWP case.
80626a55 9489 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9490
9491static struct dwo_file *
0ac5b59e
DE
9492open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9493 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9494{
9495 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9496 struct dwo_file *dwo_file;
9497 bfd *dbfd;
3019eac3
DE
9498 struct cleanup *cleanups;
9499
ab5088bf 9500 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9501 if (dbfd == NULL)
9502 {
9503 if (dwarf2_read_debug)
9504 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9505 return NULL;
9506 }
9507 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9508 dwo_file->dwo_name = dwo_name;
9509 dwo_file->comp_dir = comp_dir;
80626a55 9510 dwo_file->dbfd = dbfd;
3019eac3
DE
9511
9512 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9513
80626a55 9514 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9515
19c3d4c9 9516 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9517
9518 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9519 dwo_file->sections.types);
9520
9521 discard_cleanups (cleanups);
9522
80626a55
DE
9523 if (dwarf2_read_debug)
9524 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9525
3019eac3
DE
9526 return dwo_file;
9527}
9528
80626a55
DE
9529/* This function is mapped across the sections and remembers the offset and
9530 size of each of the DWP debugging sections we are interested in. */
3019eac3 9531
80626a55
DE
9532static void
9533dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9534{
80626a55
DE
9535 struct dwp_file *dwp_file = dwp_file_ptr;
9536 const struct dwop_section_names *names = &dwop_section_names;
9537 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9538
80626a55
DE
9539 /* Record the ELF section number for later lookup: this is what the
9540 .debug_cu_index,.debug_tu_index tables use. */
9541 gdb_assert (elf_section_nr < dwp_file->num_sections);
9542 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9543
80626a55
DE
9544 /* Look for specific sections that we need. */
9545 if (section_is_p (sectp->name, &names->str_dwo))
9546 {
9547 dwp_file->sections.str.asection = sectp;
9548 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9549 }
9550 else if (section_is_p (sectp->name, &names->cu_index))
9551 {
9552 dwp_file->sections.cu_index.asection = sectp;
9553 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9554 }
9555 else if (section_is_p (sectp->name, &names->tu_index))
9556 {
9557 dwp_file->sections.tu_index.asection = sectp;
9558 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9559 }
9560}
3019eac3 9561
80626a55 9562/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9563
80626a55
DE
9564static hashval_t
9565hash_dwp_loaded_cutus (const void *item)
9566{
9567 const struct dwo_unit *dwo_unit = item;
3019eac3 9568
80626a55
DE
9569 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9570 return dwo_unit->signature;
3019eac3
DE
9571}
9572
80626a55 9573/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9574
80626a55
DE
9575static int
9576eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9577{
80626a55
DE
9578 const struct dwo_unit *dua = a;
9579 const struct dwo_unit *dub = b;
3019eac3 9580
80626a55
DE
9581 return dua->signature == dub->signature;
9582}
3019eac3 9583
80626a55 9584/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9585
80626a55
DE
9586static htab_t
9587allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9588{
9589 return htab_create_alloc_ex (3,
9590 hash_dwp_loaded_cutus,
9591 eq_dwp_loaded_cutus,
9592 NULL,
9593 &objfile->objfile_obstack,
9594 hashtab_obstack_allocate,
9595 dummy_obstack_deallocate);
9596}
3019eac3 9597
ab5088bf
DE
9598/* Try to open DWP file FILE_NAME.
9599 The result is the bfd handle of the file.
9600 If there is a problem finding or opening the file, return NULL.
9601 Upon success, the canonicalized path of the file is stored in the bfd,
9602 same as symfile_bfd_open. */
9603
9604static bfd *
9605open_dwp_file (const char *file_name)
9606{
6ac97d4c
DE
9607 bfd *abfd;
9608
9609 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9610 if (abfd != NULL)
9611 return abfd;
9612
9613 /* Work around upstream bug 15652.
9614 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9615 [Whether that's a "bug" is debatable, but it is getting in our way.]
9616 We have no real idea where the dwp file is, because gdb's realpath-ing
9617 of the executable's path may have discarded the needed info.
9618 [IWBN if the dwp file name was recorded in the executable, akin to
9619 .gnu_debuglink, but that doesn't exist yet.]
9620 Strip the directory from FILE_NAME and search again. */
9621 if (*debug_file_directory != '\0')
9622 {
9623 /* Don't implicitly search the current directory here.
9624 If the user wants to search "." to handle this case,
9625 it must be added to debug-file-directory. */
9626 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9627 0 /*search_cwd*/);
9628 }
9629
9630 return NULL;
ab5088bf
DE
9631}
9632
80626a55
DE
9633/* Initialize the use of the DWP file for the current objfile.
9634 By convention the name of the DWP file is ${objfile}.dwp.
9635 The result is NULL if it can't be found. */
a766d390 9636
80626a55 9637static struct dwp_file *
ab5088bf 9638open_and_init_dwp_file (void)
80626a55
DE
9639{
9640 struct objfile *objfile = dwarf2_per_objfile->objfile;
9641 struct dwp_file *dwp_file;
9642 char *dwp_name;
9643 bfd *dbfd;
9644 struct cleanup *cleanups;
9645
2792b94d 9646 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9647 cleanups = make_cleanup (xfree, dwp_name);
9648
ab5088bf 9649 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9650 if (dbfd == NULL)
9651 {
9652 if (dwarf2_read_debug)
9653 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9654 do_cleanups (cleanups);
9655 return NULL;
3019eac3 9656 }
80626a55 9657 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9658 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9659 dwp_file->dbfd = dbfd;
9660 do_cleanups (cleanups);
c906108c 9661
80626a55
DE
9662 /* +1: section 0 is unused */
9663 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9664 dwp_file->elf_sections =
9665 OBSTACK_CALLOC (&objfile->objfile_obstack,
9666 dwp_file->num_sections, asection *);
9667
9668 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9669
9670 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9671
9672 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9673
9674 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9675
80626a55
DE
9676 if (dwarf2_read_debug)
9677 {
9678 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9679 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9680 " %s CUs, %s TUs\n",
9681 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9682 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9683 }
9684
9685 return dwp_file;
3019eac3 9686}
c906108c 9687
ab5088bf
DE
9688/* Wrapper around open_and_init_dwp_file, only open it once. */
9689
9690static struct dwp_file *
9691get_dwp_file (void)
9692{
9693 if (! dwarf2_per_objfile->dwp_checked)
9694 {
9695 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9696 dwarf2_per_objfile->dwp_checked = 1;
9697 }
9698 return dwarf2_per_objfile->dwp_file;
9699}
9700
80626a55
DE
9701/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9702 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9703 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9704 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9705 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9706
9707 This is called, for example, when wanting to read a variable with a
9708 complex location. Therefore we don't want to do file i/o for every call.
9709 Therefore we don't want to look for a DWO file on every call.
9710 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9711 then we check if we've already seen DWO_NAME, and only THEN do we check
9712 for a DWO file.
9713
1c658ad5 9714 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9715 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9716
3019eac3 9717static struct dwo_unit *
80626a55
DE
9718lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9719 const char *dwo_name, const char *comp_dir,
9720 ULONGEST signature, int is_debug_types)
3019eac3
DE
9721{
9722 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9723 const char *kind = is_debug_types ? "TU" : "CU";
9724 void **dwo_file_slot;
3019eac3 9725 struct dwo_file *dwo_file;
80626a55 9726 struct dwp_file *dwp_file;
cb1df416 9727
6a506a2d
DE
9728 /* First see if there's a DWP file.
9729 If we have a DWP file but didn't find the DWO inside it, don't
9730 look for the original DWO file. It makes gdb behave differently
9731 depending on whether one is debugging in the build tree. */
cf2c3c16 9732
ab5088bf 9733 dwp_file = get_dwp_file ();
80626a55 9734 if (dwp_file != NULL)
cf2c3c16 9735 {
80626a55
DE
9736 const struct dwp_hash_table *dwp_htab =
9737 is_debug_types ? dwp_file->tus : dwp_file->cus;
9738
9739 if (dwp_htab != NULL)
9740 {
9741 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9742 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9743 signature, is_debug_types);
80626a55
DE
9744
9745 if (dwo_cutu != NULL)
9746 {
9747 if (dwarf2_read_debug)
9748 {
9749 fprintf_unfiltered (gdb_stdlog,
9750 "Virtual DWO %s %s found: @%s\n",
9751 kind, hex_string (signature),
9752 host_address_to_string (dwo_cutu));
9753 }
9754 return dwo_cutu;
9755 }
9756 }
9757 }
6a506a2d 9758 else
80626a55 9759 {
6a506a2d 9760 /* No DWP file, look for the DWO file. */
80626a55 9761
6a506a2d
DE
9762 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9763 if (*dwo_file_slot == NULL)
80626a55 9764 {
6a506a2d
DE
9765 /* Read in the file and build a table of the CUs/TUs it contains. */
9766 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9767 }
6a506a2d
DE
9768 /* NOTE: This will be NULL if unable to open the file. */
9769 dwo_file = *dwo_file_slot;
3019eac3 9770
6a506a2d 9771 if (dwo_file != NULL)
19c3d4c9 9772 {
6a506a2d
DE
9773 struct dwo_unit *dwo_cutu = NULL;
9774
9775 if (is_debug_types && dwo_file->tus)
9776 {
9777 struct dwo_unit find_dwo_cutu;
9778
9779 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9780 find_dwo_cutu.signature = signature;
9781 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9782 }
9783 else if (!is_debug_types && dwo_file->cu)
80626a55 9784 {
6a506a2d
DE
9785 if (signature == dwo_file->cu->signature)
9786 dwo_cutu = dwo_file->cu;
9787 }
9788
9789 if (dwo_cutu != NULL)
9790 {
9791 if (dwarf2_read_debug)
9792 {
9793 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9794 kind, dwo_name, hex_string (signature),
9795 host_address_to_string (dwo_cutu));
9796 }
9797 return dwo_cutu;
80626a55
DE
9798 }
9799 }
2e276125 9800 }
9cdd5dbd 9801
80626a55
DE
9802 /* We didn't find it. This could mean a dwo_id mismatch, or
9803 someone deleted the DWO/DWP file, or the search path isn't set up
9804 correctly to find the file. */
9805
9806 if (dwarf2_read_debug)
9807 {
9808 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9809 kind, dwo_name, hex_string (signature));
9810 }
3019eac3 9811
6656a72d
DE
9812 /* This is a warning and not a complaint because it can be caused by
9813 pilot error (e.g., user accidentally deleting the DWO). */
9814 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9815 " [in module %s]"),
9816 kind, dwo_name, hex_string (signature),
9817 this_unit->is_debug_types ? "TU" : "CU",
9818 this_unit->offset.sect_off, objfile->name);
3019eac3 9819 return NULL;
5fb290d7
DJ
9820}
9821
80626a55
DE
9822/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9823 See lookup_dwo_cutu_unit for details. */
9824
9825static struct dwo_unit *
9826lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9827 const char *dwo_name, const char *comp_dir,
9828 ULONGEST signature)
9829{
9830 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9831}
9832
9833/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9834 See lookup_dwo_cutu_unit for details. */
9835
9836static struct dwo_unit *
9837lookup_dwo_type_unit (struct signatured_type *this_tu,
9838 const char *dwo_name, const char *comp_dir)
9839{
9840 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9841}
9842
3019eac3
DE
9843/* Free all resources associated with DWO_FILE.
9844 Close the DWO file and munmap the sections.
9845 All memory should be on the objfile obstack. */
348e048f
DE
9846
9847static void
3019eac3 9848free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9849{
3019eac3
DE
9850 int ix;
9851 struct dwarf2_section_info *section;
348e048f 9852
5c6fa7ab 9853 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9854 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9855
3019eac3
DE
9856 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9857}
348e048f 9858
3019eac3 9859/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9860
3019eac3
DE
9861static void
9862free_dwo_file_cleanup (void *arg)
9863{
9864 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9865 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9866
3019eac3
DE
9867 free_dwo_file (dwo_file, objfile);
9868}
348e048f 9869
3019eac3 9870/* Traversal function for free_dwo_files. */
2ab95328 9871
3019eac3
DE
9872static int
9873free_dwo_file_from_slot (void **slot, void *info)
9874{
9875 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9876 struct objfile *objfile = (struct objfile *) info;
348e048f 9877
3019eac3 9878 free_dwo_file (dwo_file, objfile);
348e048f 9879
3019eac3
DE
9880 return 1;
9881}
348e048f 9882
3019eac3 9883/* Free all resources associated with DWO_FILES. */
348e048f 9884
3019eac3
DE
9885static void
9886free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9887{
9888 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9889}
3019eac3
DE
9890\f
9891/* Read in various DIEs. */
348e048f 9892
d389af10
JK
9893/* qsort helper for inherit_abstract_dies. */
9894
9895static int
9896unsigned_int_compar (const void *ap, const void *bp)
9897{
9898 unsigned int a = *(unsigned int *) ap;
9899 unsigned int b = *(unsigned int *) bp;
9900
9901 return (a > b) - (b > a);
9902}
9903
9904/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9905 Inherit only the children of the DW_AT_abstract_origin DIE not being
9906 already referenced by DW_AT_abstract_origin from the children of the
9907 current DIE. */
d389af10
JK
9908
9909static void
9910inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9911{
9912 struct die_info *child_die;
9913 unsigned die_children_count;
9914 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9915 sect_offset *offsets;
9916 sect_offset *offsets_end, *offsetp;
d389af10
JK
9917 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9918 struct die_info *origin_die;
9919 /* Iterator of the ORIGIN_DIE children. */
9920 struct die_info *origin_child_die;
9921 struct cleanup *cleanups;
9922 struct attribute *attr;
cd02d79d
PA
9923 struct dwarf2_cu *origin_cu;
9924 struct pending **origin_previous_list_in_scope;
d389af10
JK
9925
9926 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9927 if (!attr)
9928 return;
9929
cd02d79d
PA
9930 /* Note that following die references may follow to a die in a
9931 different cu. */
9932
9933 origin_cu = cu;
9934 origin_die = follow_die_ref (die, attr, &origin_cu);
9935
9936 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9937 symbols in. */
9938 origin_previous_list_in_scope = origin_cu->list_in_scope;
9939 origin_cu->list_in_scope = cu->list_in_scope;
9940
edb3359d
DJ
9941 if (die->tag != origin_die->tag
9942 && !(die->tag == DW_TAG_inlined_subroutine
9943 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9944 complaint (&symfile_complaints,
9945 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9946 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9947
9948 child_die = die->child;
9949 die_children_count = 0;
9950 while (child_die && child_die->tag)
9951 {
9952 child_die = sibling_die (child_die);
9953 die_children_count++;
9954 }
9955 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9956 cleanups = make_cleanup (xfree, offsets);
9957
9958 offsets_end = offsets;
9959 child_die = die->child;
9960 while (child_die && child_die->tag)
9961 {
c38f313d
DJ
9962 /* For each CHILD_DIE, find the corresponding child of
9963 ORIGIN_DIE. If there is more than one layer of
9964 DW_AT_abstract_origin, follow them all; there shouldn't be,
9965 but GCC versions at least through 4.4 generate this (GCC PR
9966 40573). */
9967 struct die_info *child_origin_die = child_die;
cd02d79d 9968 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 9969
c38f313d
DJ
9970 while (1)
9971 {
cd02d79d
PA
9972 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9973 child_origin_cu);
c38f313d
DJ
9974 if (attr == NULL)
9975 break;
cd02d79d
PA
9976 child_origin_die = follow_die_ref (child_origin_die, attr,
9977 &child_origin_cu);
c38f313d
DJ
9978 }
9979
d389af10
JK
9980 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9981 counterpart may exist. */
c38f313d 9982 if (child_origin_die != child_die)
d389af10 9983 {
edb3359d
DJ
9984 if (child_die->tag != child_origin_die->tag
9985 && !(child_die->tag == DW_TAG_inlined_subroutine
9986 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9987 complaint (&symfile_complaints,
9988 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9989 "different tags"), child_die->offset.sect_off,
9990 child_origin_die->offset.sect_off);
c38f313d
DJ
9991 if (child_origin_die->parent != origin_die)
9992 complaint (&symfile_complaints,
9993 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
9994 "different parents"), child_die->offset.sect_off,
9995 child_origin_die->offset.sect_off);
c38f313d
DJ
9996 else
9997 *offsets_end++ = child_origin_die->offset;
d389af10
JK
9998 }
9999 child_die = sibling_die (child_die);
10000 }
10001 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10002 unsigned_int_compar);
10003 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 10004 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
10005 complaint (&symfile_complaints,
10006 _("Multiple children of DIE 0x%x refer "
10007 "to DIE 0x%x as their abstract origin"),
b64f50a1 10008 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
10009
10010 offsetp = offsets;
10011 origin_child_die = origin_die->child;
10012 while (origin_child_die && origin_child_die->tag)
10013 {
10014 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
10015 while (offsetp < offsets_end
10016 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 10017 offsetp++;
b64f50a1
JK
10018 if (offsetp >= offsets_end
10019 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
10020 {
10021 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 10022 process_die (origin_child_die, origin_cu);
d389af10
JK
10023 }
10024 origin_child_die = sibling_die (origin_child_die);
10025 }
cd02d79d 10026 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10027
10028 do_cleanups (cleanups);
10029}
10030
c906108c 10031static void
e7c27a73 10032read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10033{
e7c27a73 10034 struct objfile *objfile = cu->objfile;
52f0bd74 10035 struct context_stack *new;
c906108c
SS
10036 CORE_ADDR lowpc;
10037 CORE_ADDR highpc;
10038 struct die_info *child_die;
edb3359d 10039 struct attribute *attr, *call_line, *call_file;
15d034d0 10040 const char *name;
e142c38c 10041 CORE_ADDR baseaddr;
801e3a5b 10042 struct block *block;
edb3359d 10043 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10044 VEC (symbolp) *template_args = NULL;
10045 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10046
10047 if (inlined_func)
10048 {
10049 /* If we do not have call site information, we can't show the
10050 caller of this inlined function. That's too confusing, so
10051 only use the scope for local variables. */
10052 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10053 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10054 if (call_line == NULL || call_file == NULL)
10055 {
10056 read_lexical_block_scope (die, cu);
10057 return;
10058 }
10059 }
c906108c 10060
e142c38c
DJ
10061 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10062
94af9270 10063 name = dwarf2_name (die, cu);
c906108c 10064
e8d05480
JB
10065 /* Ignore functions with missing or empty names. These are actually
10066 illegal according to the DWARF standard. */
10067 if (name == NULL)
10068 {
10069 complaint (&symfile_complaints,
b64f50a1
JK
10070 _("missing name for subprogram DIE at %d"),
10071 die->offset.sect_off);
e8d05480
JB
10072 return;
10073 }
10074
10075 /* Ignore functions with missing or invalid low and high pc attributes. */
10076 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10077 {
ae4d0c03
PM
10078 attr = dwarf2_attr (die, DW_AT_external, cu);
10079 if (!attr || !DW_UNSND (attr))
10080 complaint (&symfile_complaints,
3e43a32a
MS
10081 _("cannot get low and high bounds "
10082 "for subprogram DIE at %d"),
b64f50a1 10083 die->offset.sect_off);
e8d05480
JB
10084 return;
10085 }
c906108c
SS
10086
10087 lowpc += baseaddr;
10088 highpc += baseaddr;
10089
34eaf542
TT
10090 /* If we have any template arguments, then we must allocate a
10091 different sort of symbol. */
10092 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10093 {
10094 if (child_die->tag == DW_TAG_template_type_param
10095 || child_die->tag == DW_TAG_template_value_param)
10096 {
e623cf5d 10097 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10098 templ_func->base.is_cplus_template_function = 1;
10099 break;
10100 }
10101 }
10102
c906108c 10103 new = push_context (0, lowpc);
34eaf542
TT
10104 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10105 (struct symbol *) templ_func);
4c2df51b 10106
4cecd739
DJ
10107 /* If there is a location expression for DW_AT_frame_base, record
10108 it. */
e142c38c 10109 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10110 if (attr)
f1e6e072 10111 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10112
e142c38c 10113 cu->list_in_scope = &local_symbols;
c906108c 10114
639d11d3 10115 if (die->child != NULL)
c906108c 10116 {
639d11d3 10117 child_die = die->child;
c906108c
SS
10118 while (child_die && child_die->tag)
10119 {
34eaf542
TT
10120 if (child_die->tag == DW_TAG_template_type_param
10121 || child_die->tag == DW_TAG_template_value_param)
10122 {
10123 struct symbol *arg = new_symbol (child_die, NULL, cu);
10124
f1078f66
DJ
10125 if (arg != NULL)
10126 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10127 }
10128 else
10129 process_die (child_die, cu);
c906108c
SS
10130 child_die = sibling_die (child_die);
10131 }
10132 }
10133
d389af10
JK
10134 inherit_abstract_dies (die, cu);
10135
4a811a97
UW
10136 /* If we have a DW_AT_specification, we might need to import using
10137 directives from the context of the specification DIE. See the
10138 comment in determine_prefix. */
10139 if (cu->language == language_cplus
10140 && dwarf2_attr (die, DW_AT_specification, cu))
10141 {
10142 struct dwarf2_cu *spec_cu = cu;
10143 struct die_info *spec_die = die_specification (die, &spec_cu);
10144
10145 while (spec_die)
10146 {
10147 child_die = spec_die->child;
10148 while (child_die && child_die->tag)
10149 {
10150 if (child_die->tag == DW_TAG_imported_module)
10151 process_die (child_die, spec_cu);
10152 child_die = sibling_die (child_die);
10153 }
10154
10155 /* In some cases, GCC generates specification DIEs that
10156 themselves contain DW_AT_specification attributes. */
10157 spec_die = die_specification (spec_die, &spec_cu);
10158 }
10159 }
10160
c906108c
SS
10161 new = pop_context ();
10162 /* Make a block for the local symbols within. */
801e3a5b
JB
10163 block = finish_block (new->name, &local_symbols, new->old_blocks,
10164 lowpc, highpc, objfile);
10165
df8a16a1 10166 /* For C++, set the block's scope. */
195a3f6c 10167 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10168 && cu->processing_has_namespace_info)
195a3f6c
TT
10169 block_set_scope (block, determine_prefix (die, cu),
10170 &objfile->objfile_obstack);
df8a16a1 10171
801e3a5b
JB
10172 /* If we have address ranges, record them. */
10173 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10174
34eaf542
TT
10175 /* Attach template arguments to function. */
10176 if (! VEC_empty (symbolp, template_args))
10177 {
10178 gdb_assert (templ_func != NULL);
10179
10180 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10181 templ_func->template_arguments
10182 = obstack_alloc (&objfile->objfile_obstack,
10183 (templ_func->n_template_arguments
10184 * sizeof (struct symbol *)));
10185 memcpy (templ_func->template_arguments,
10186 VEC_address (symbolp, template_args),
10187 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10188 VEC_free (symbolp, template_args);
10189 }
10190
208d8187
JB
10191 /* In C++, we can have functions nested inside functions (e.g., when
10192 a function declares a class that has methods). This means that
10193 when we finish processing a function scope, we may need to go
10194 back to building a containing block's symbol lists. */
10195 local_symbols = new->locals;
27aa8d6a 10196 using_directives = new->using_directives;
208d8187 10197
921e78cf
JB
10198 /* If we've finished processing a top-level function, subsequent
10199 symbols go in the file symbol list. */
10200 if (outermost_context_p ())
e142c38c 10201 cu->list_in_scope = &file_symbols;
c906108c
SS
10202}
10203
10204/* Process all the DIES contained within a lexical block scope. Start
10205 a new scope, process the dies, and then close the scope. */
10206
10207static void
e7c27a73 10208read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10209{
e7c27a73 10210 struct objfile *objfile = cu->objfile;
52f0bd74 10211 struct context_stack *new;
c906108c
SS
10212 CORE_ADDR lowpc, highpc;
10213 struct die_info *child_die;
e142c38c
DJ
10214 CORE_ADDR baseaddr;
10215
10216 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10217
10218 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10219 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10220 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10221 be nasty. Might be easier to properly extend generic blocks to
af34e669 10222 describe ranges. */
d85a05f0 10223 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10224 return;
10225 lowpc += baseaddr;
10226 highpc += baseaddr;
10227
10228 push_context (0, lowpc);
639d11d3 10229 if (die->child != NULL)
c906108c 10230 {
639d11d3 10231 child_die = die->child;
c906108c
SS
10232 while (child_die && child_die->tag)
10233 {
e7c27a73 10234 process_die (child_die, cu);
c906108c
SS
10235 child_die = sibling_die (child_die);
10236 }
10237 }
10238 new = pop_context ();
10239
8540c487 10240 if (local_symbols != NULL || using_directives != NULL)
c906108c 10241 {
801e3a5b
JB
10242 struct block *block
10243 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10244 highpc, objfile);
10245
10246 /* Note that recording ranges after traversing children, as we
10247 do here, means that recording a parent's ranges entails
10248 walking across all its children's ranges as they appear in
10249 the address map, which is quadratic behavior.
10250
10251 It would be nicer to record the parent's ranges before
10252 traversing its children, simply overriding whatever you find
10253 there. But since we don't even decide whether to create a
10254 block until after we've traversed its children, that's hard
10255 to do. */
10256 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10257 }
10258 local_symbols = new->locals;
27aa8d6a 10259 using_directives = new->using_directives;
c906108c
SS
10260}
10261
96408a79
SA
10262/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10263
10264static void
10265read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10266{
10267 struct objfile *objfile = cu->objfile;
10268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10269 CORE_ADDR pc, baseaddr;
10270 struct attribute *attr;
10271 struct call_site *call_site, call_site_local;
10272 void **slot;
10273 int nparams;
10274 struct die_info *child_die;
10275
10276 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10277
10278 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10279 if (!attr)
10280 {
10281 complaint (&symfile_complaints,
10282 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10283 "DIE 0x%x [in module %s]"),
b64f50a1 10284 die->offset.sect_off, objfile->name);
96408a79
SA
10285 return;
10286 }
10287 pc = DW_ADDR (attr) + baseaddr;
10288
10289 if (cu->call_site_htab == NULL)
10290 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10291 NULL, &objfile->objfile_obstack,
10292 hashtab_obstack_allocate, NULL);
10293 call_site_local.pc = pc;
10294 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10295 if (*slot != NULL)
10296 {
10297 complaint (&symfile_complaints,
10298 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10299 "DIE 0x%x [in module %s]"),
b64f50a1 10300 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
10301 return;
10302 }
10303
10304 /* Count parameters at the caller. */
10305
10306 nparams = 0;
10307 for (child_die = die->child; child_die && child_die->tag;
10308 child_die = sibling_die (child_die))
10309 {
10310 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10311 {
10312 complaint (&symfile_complaints,
10313 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10314 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10315 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
10316 continue;
10317 }
10318
10319 nparams++;
10320 }
10321
10322 call_site = obstack_alloc (&objfile->objfile_obstack,
10323 (sizeof (*call_site)
10324 + (sizeof (*call_site->parameter)
10325 * (nparams - 1))));
10326 *slot = call_site;
10327 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10328 call_site->pc = pc;
10329
10330 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10331 {
10332 struct die_info *func_die;
10333
10334 /* Skip also over DW_TAG_inlined_subroutine. */
10335 for (func_die = die->parent;
10336 func_die && func_die->tag != DW_TAG_subprogram
10337 && func_die->tag != DW_TAG_subroutine_type;
10338 func_die = func_die->parent);
10339
10340 /* DW_AT_GNU_all_call_sites is a superset
10341 of DW_AT_GNU_all_tail_call_sites. */
10342 if (func_die
10343 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10344 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10345 {
10346 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10347 not complete. But keep CALL_SITE for look ups via call_site_htab,
10348 both the initial caller containing the real return address PC and
10349 the final callee containing the current PC of a chain of tail
10350 calls do not need to have the tail call list complete. But any
10351 function candidate for a virtual tail call frame searched via
10352 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10353 determined unambiguously. */
10354 }
10355 else
10356 {
10357 struct type *func_type = NULL;
10358
10359 if (func_die)
10360 func_type = get_die_type (func_die, cu);
10361 if (func_type != NULL)
10362 {
10363 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10364
10365 /* Enlist this call site to the function. */
10366 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10367 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10368 }
10369 else
10370 complaint (&symfile_complaints,
10371 _("Cannot find function owning DW_TAG_GNU_call_site "
10372 "DIE 0x%x [in module %s]"),
b64f50a1 10373 die->offset.sect_off, objfile->name);
96408a79
SA
10374 }
10375 }
10376
10377 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10378 if (attr == NULL)
10379 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10380 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10381 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10382 /* Keep NULL DWARF_BLOCK. */;
10383 else if (attr_form_is_block (attr))
10384 {
10385 struct dwarf2_locexpr_baton *dlbaton;
10386
10387 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10388 dlbaton->data = DW_BLOCK (attr)->data;
10389 dlbaton->size = DW_BLOCK (attr)->size;
10390 dlbaton->per_cu = cu->per_cu;
10391
10392 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10393 }
7771576e 10394 else if (attr_form_is_ref (attr))
96408a79 10395 {
96408a79
SA
10396 struct dwarf2_cu *target_cu = cu;
10397 struct die_info *target_die;
10398
ac9ec31b 10399 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10400 gdb_assert (target_cu->objfile == objfile);
10401 if (die_is_declaration (target_die, target_cu))
10402 {
9112db09
JK
10403 const char *target_physname = NULL;
10404 struct attribute *target_attr;
10405
10406 /* Prefer the mangled name; otherwise compute the demangled one. */
10407 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10408 if (target_attr == NULL)
10409 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10410 target_cu);
10411 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10412 target_physname = DW_STRING (target_attr);
10413 else
10414 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10415 if (target_physname == NULL)
10416 complaint (&symfile_complaints,
10417 _("DW_AT_GNU_call_site_target target DIE has invalid "
10418 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10419 die->offset.sect_off, objfile->name);
96408a79 10420 else
7d455152 10421 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10422 }
10423 else
10424 {
10425 CORE_ADDR lowpc;
10426
10427 /* DW_AT_entry_pc should be preferred. */
10428 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10429 complaint (&symfile_complaints,
10430 _("DW_AT_GNU_call_site_target target DIE has invalid "
10431 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10432 die->offset.sect_off, objfile->name);
96408a79
SA
10433 else
10434 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10435 }
10436 }
10437 else
10438 complaint (&symfile_complaints,
10439 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10440 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10441 die->offset.sect_off, objfile->name);
96408a79
SA
10442
10443 call_site->per_cu = cu->per_cu;
10444
10445 for (child_die = die->child;
10446 child_die && child_die->tag;
10447 child_die = sibling_die (child_die))
10448 {
96408a79 10449 struct call_site_parameter *parameter;
1788b2d3 10450 struct attribute *loc, *origin;
96408a79
SA
10451
10452 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10453 {
10454 /* Already printed the complaint above. */
10455 continue;
10456 }
10457
10458 gdb_assert (call_site->parameter_count < nparams);
10459 parameter = &call_site->parameter[call_site->parameter_count];
10460
1788b2d3
JK
10461 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10462 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10463 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10464
24c5c679 10465 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10466 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10467 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10468 {
10469 sect_offset offset;
10470
10471 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10472 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10473 if (!offset_in_cu_p (&cu->header, offset))
10474 {
10475 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10476 binding can be done only inside one CU. Such referenced DIE
10477 therefore cannot be even moved to DW_TAG_partial_unit. */
10478 complaint (&symfile_complaints,
10479 _("DW_AT_abstract_origin offset is not in CU for "
10480 "DW_TAG_GNU_call_site child DIE 0x%x "
10481 "[in module %s]"),
10482 child_die->offset.sect_off, objfile->name);
10483 continue;
10484 }
1788b2d3
JK
10485 parameter->u.param_offset.cu_off = (offset.sect_off
10486 - cu->header.offset.sect_off);
10487 }
10488 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10489 {
10490 complaint (&symfile_complaints,
10491 _("No DW_FORM_block* DW_AT_location for "
10492 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10493 child_die->offset.sect_off, objfile->name);
96408a79
SA
10494 continue;
10495 }
24c5c679 10496 else
96408a79 10497 {
24c5c679
JK
10498 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10499 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10500 if (parameter->u.dwarf_reg != -1)
10501 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10502 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10503 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10504 &parameter->u.fb_offset))
10505 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10506 else
10507 {
10508 complaint (&symfile_complaints,
10509 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10510 "for DW_FORM_block* DW_AT_location is supported for "
10511 "DW_TAG_GNU_call_site child DIE 0x%x "
10512 "[in module %s]"),
10513 child_die->offset.sect_off, objfile->name);
10514 continue;
10515 }
96408a79
SA
10516 }
10517
10518 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10519 if (!attr_form_is_block (attr))
10520 {
10521 complaint (&symfile_complaints,
10522 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10523 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10524 child_die->offset.sect_off, objfile->name);
96408a79
SA
10525 continue;
10526 }
10527 parameter->value = DW_BLOCK (attr)->data;
10528 parameter->value_size = DW_BLOCK (attr)->size;
10529
10530 /* Parameters are not pre-cleared by memset above. */
10531 parameter->data_value = NULL;
10532 parameter->data_value_size = 0;
10533 call_site->parameter_count++;
10534
10535 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10536 if (attr)
10537 {
10538 if (!attr_form_is_block (attr))
10539 complaint (&symfile_complaints,
10540 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10541 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10542 child_die->offset.sect_off, objfile->name);
96408a79
SA
10543 else
10544 {
10545 parameter->data_value = DW_BLOCK (attr)->data;
10546 parameter->data_value_size = DW_BLOCK (attr)->size;
10547 }
10548 }
10549 }
10550}
10551
43039443 10552/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10553 Return 1 if the attributes are present and valid, otherwise, return 0.
10554 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10555
10556static int
10557dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10558 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10559 struct partial_symtab *ranges_pst)
43039443
JK
10560{
10561 struct objfile *objfile = cu->objfile;
10562 struct comp_unit_head *cu_header = &cu->header;
10563 bfd *obfd = objfile->obfd;
10564 unsigned int addr_size = cu_header->addr_size;
10565 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10566 /* Base address selection entry. */
10567 CORE_ADDR base;
10568 int found_base;
10569 unsigned int dummy;
d521ce57 10570 const gdb_byte *buffer;
43039443
JK
10571 CORE_ADDR marker;
10572 int low_set;
10573 CORE_ADDR low = 0;
10574 CORE_ADDR high = 0;
ff013f42 10575 CORE_ADDR baseaddr;
43039443 10576
d00adf39
DE
10577 found_base = cu->base_known;
10578 base = cu->base_address;
43039443 10579
be391dca 10580 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10581 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10582 {
10583 complaint (&symfile_complaints,
10584 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10585 offset);
10586 return 0;
10587 }
dce234bc 10588 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10589
10590 /* Read in the largest possible address. */
10591 marker = read_address (obfd, buffer, cu, &dummy);
10592 if ((marker & mask) == mask)
10593 {
10594 /* If we found the largest possible address, then
10595 read the base address. */
10596 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10597 buffer += 2 * addr_size;
10598 offset += 2 * addr_size;
10599 found_base = 1;
10600 }
10601
10602 low_set = 0;
10603
e7030f15 10604 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10605
43039443
JK
10606 while (1)
10607 {
10608 CORE_ADDR range_beginning, range_end;
10609
10610 range_beginning = read_address (obfd, buffer, cu, &dummy);
10611 buffer += addr_size;
10612 range_end = read_address (obfd, buffer, cu, &dummy);
10613 buffer += addr_size;
10614 offset += 2 * addr_size;
10615
10616 /* An end of list marker is a pair of zero addresses. */
10617 if (range_beginning == 0 && range_end == 0)
10618 /* Found the end of list entry. */
10619 break;
10620
10621 /* Each base address selection entry is a pair of 2 values.
10622 The first is the largest possible address, the second is
10623 the base address. Check for a base address here. */
10624 if ((range_beginning & mask) == mask)
10625 {
10626 /* If we found the largest possible address, then
10627 read the base address. */
10628 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10629 found_base = 1;
10630 continue;
10631 }
10632
10633 if (!found_base)
10634 {
10635 /* We have no valid base address for the ranges
10636 data. */
10637 complaint (&symfile_complaints,
10638 _("Invalid .debug_ranges data (no base address)"));
10639 return 0;
10640 }
10641
9277c30c
UW
10642 if (range_beginning > range_end)
10643 {
10644 /* Inverted range entries are invalid. */
10645 complaint (&symfile_complaints,
10646 _("Invalid .debug_ranges data (inverted range)"));
10647 return 0;
10648 }
10649
10650 /* Empty range entries have no effect. */
10651 if (range_beginning == range_end)
10652 continue;
10653
43039443
JK
10654 range_beginning += base;
10655 range_end += base;
10656
01093045
DE
10657 /* A not-uncommon case of bad debug info.
10658 Don't pollute the addrmap with bad data. */
10659 if (range_beginning + baseaddr == 0
10660 && !dwarf2_per_objfile->has_section_at_zero)
10661 {
10662 complaint (&symfile_complaints,
10663 _(".debug_ranges entry has start address of zero"
10664 " [in module %s]"), objfile->name);
10665 continue;
10666 }
10667
9277c30c 10668 if (ranges_pst != NULL)
ff013f42 10669 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10670 range_beginning + baseaddr,
10671 range_end - 1 + baseaddr,
ff013f42
JK
10672 ranges_pst);
10673
43039443
JK
10674 /* FIXME: This is recording everything as a low-high
10675 segment of consecutive addresses. We should have a
10676 data structure for discontiguous block ranges
10677 instead. */
10678 if (! low_set)
10679 {
10680 low = range_beginning;
10681 high = range_end;
10682 low_set = 1;
10683 }
10684 else
10685 {
10686 if (range_beginning < low)
10687 low = range_beginning;
10688 if (range_end > high)
10689 high = range_end;
10690 }
10691 }
10692
10693 if (! low_set)
10694 /* If the first entry is an end-of-list marker, the range
10695 describes an empty scope, i.e. no instructions. */
10696 return 0;
10697
10698 if (low_return)
10699 *low_return = low;
10700 if (high_return)
10701 *high_return = high;
10702 return 1;
10703}
10704
af34e669
DJ
10705/* Get low and high pc attributes from a die. Return 1 if the attributes
10706 are present and valid, otherwise, return 0. Return -1 if the range is
10707 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10708
c906108c 10709static int
af34e669 10710dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10711 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10712 struct partial_symtab *pst)
c906108c
SS
10713{
10714 struct attribute *attr;
91da1414 10715 struct attribute *attr_high;
af34e669
DJ
10716 CORE_ADDR low = 0;
10717 CORE_ADDR high = 0;
10718 int ret = 0;
c906108c 10719
91da1414
MW
10720 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10721 if (attr_high)
af34e669 10722 {
e142c38c 10723 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10724 if (attr)
91da1414
MW
10725 {
10726 low = DW_ADDR (attr);
3019eac3
DE
10727 if (attr_high->form == DW_FORM_addr
10728 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10729 high = DW_ADDR (attr_high);
10730 else
10731 high = low + DW_UNSND (attr_high);
10732 }
af34e669
DJ
10733 else
10734 /* Found high w/o low attribute. */
10735 return 0;
10736
10737 /* Found consecutive range of addresses. */
10738 ret = 1;
10739 }
c906108c 10740 else
af34e669 10741 {
e142c38c 10742 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10743 if (attr != NULL)
10744 {
ab435259
DE
10745 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10746 We take advantage of the fact that DW_AT_ranges does not appear
10747 in DW_TAG_compile_unit of DWO files. */
10748 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10749 unsigned int ranges_offset = (DW_UNSND (attr)
10750 + (need_ranges_base
10751 ? cu->ranges_base
10752 : 0));
2e3cf129 10753
af34e669 10754 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10755 .debug_ranges section. */
2e3cf129 10756 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10757 return 0;
43039443 10758 /* Found discontinuous range of addresses. */
af34e669
DJ
10759 ret = -1;
10760 }
10761 }
c906108c 10762
9373cf26
JK
10763 /* read_partial_die has also the strict LOW < HIGH requirement. */
10764 if (high <= low)
c906108c
SS
10765 return 0;
10766
10767 /* When using the GNU linker, .gnu.linkonce. sections are used to
10768 eliminate duplicate copies of functions and vtables and such.
10769 The linker will arbitrarily choose one and discard the others.
10770 The AT_*_pc values for such functions refer to local labels in
10771 these sections. If the section from that file was discarded, the
10772 labels are not in the output, so the relocs get a value of 0.
10773 If this is a discarded function, mark the pc bounds as invalid,
10774 so that GDB will ignore it. */
72dca2f5 10775 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10776 return 0;
10777
10778 *lowpc = low;
96408a79
SA
10779 if (highpc)
10780 *highpc = high;
af34e669 10781 return ret;
c906108c
SS
10782}
10783
b084d499
JB
10784/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10785 its low and high PC addresses. Do nothing if these addresses could not
10786 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10787 and HIGHPC to the high address if greater than HIGHPC. */
10788
10789static void
10790dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10791 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10792 struct dwarf2_cu *cu)
10793{
10794 CORE_ADDR low, high;
10795 struct die_info *child = die->child;
10796
d85a05f0 10797 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10798 {
10799 *lowpc = min (*lowpc, low);
10800 *highpc = max (*highpc, high);
10801 }
10802
10803 /* If the language does not allow nested subprograms (either inside
10804 subprograms or lexical blocks), we're done. */
10805 if (cu->language != language_ada)
10806 return;
6e70227d 10807
b084d499
JB
10808 /* Check all the children of the given DIE. If it contains nested
10809 subprograms, then check their pc bounds. Likewise, we need to
10810 check lexical blocks as well, as they may also contain subprogram
10811 definitions. */
10812 while (child && child->tag)
10813 {
10814 if (child->tag == DW_TAG_subprogram
10815 || child->tag == DW_TAG_lexical_block)
10816 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10817 child = sibling_die (child);
10818 }
10819}
10820
fae299cd
DC
10821/* Get the low and high pc's represented by the scope DIE, and store
10822 them in *LOWPC and *HIGHPC. If the correct values can't be
10823 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10824
10825static void
10826get_scope_pc_bounds (struct die_info *die,
10827 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10828 struct dwarf2_cu *cu)
10829{
10830 CORE_ADDR best_low = (CORE_ADDR) -1;
10831 CORE_ADDR best_high = (CORE_ADDR) 0;
10832 CORE_ADDR current_low, current_high;
10833
d85a05f0 10834 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10835 {
10836 best_low = current_low;
10837 best_high = current_high;
10838 }
10839 else
10840 {
10841 struct die_info *child = die->child;
10842
10843 while (child && child->tag)
10844 {
10845 switch (child->tag) {
10846 case DW_TAG_subprogram:
b084d499 10847 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10848 break;
10849 case DW_TAG_namespace:
f55ee35c 10850 case DW_TAG_module:
fae299cd
DC
10851 /* FIXME: carlton/2004-01-16: Should we do this for
10852 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10853 that current GCC's always emit the DIEs corresponding
10854 to definitions of methods of classes as children of a
10855 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10856 the DIEs giving the declarations, which could be
10857 anywhere). But I don't see any reason why the
10858 standards says that they have to be there. */
10859 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10860
10861 if (current_low != ((CORE_ADDR) -1))
10862 {
10863 best_low = min (best_low, current_low);
10864 best_high = max (best_high, current_high);
10865 }
10866 break;
10867 default:
0963b4bd 10868 /* Ignore. */
fae299cd
DC
10869 break;
10870 }
10871
10872 child = sibling_die (child);
10873 }
10874 }
10875
10876 *lowpc = best_low;
10877 *highpc = best_high;
10878}
10879
801e3a5b
JB
10880/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10881 in DIE. */
380bca97 10882
801e3a5b
JB
10883static void
10884dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10885 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10886{
bb5ed363 10887 struct objfile *objfile = cu->objfile;
801e3a5b 10888 struct attribute *attr;
91da1414 10889 struct attribute *attr_high;
801e3a5b 10890
91da1414
MW
10891 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10892 if (attr_high)
801e3a5b 10893 {
801e3a5b
JB
10894 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10895 if (attr)
10896 {
10897 CORE_ADDR low = DW_ADDR (attr);
91da1414 10898 CORE_ADDR high;
3019eac3
DE
10899 if (attr_high->form == DW_FORM_addr
10900 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10901 high = DW_ADDR (attr_high);
10902 else
10903 high = low + DW_UNSND (attr_high);
9a619af0 10904
801e3a5b
JB
10905 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10906 }
10907 }
10908
10909 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10910 if (attr)
10911 {
bb5ed363 10912 bfd *obfd = objfile->obfd;
ab435259
DE
10913 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10914 We take advantage of the fact that DW_AT_ranges does not appear
10915 in DW_TAG_compile_unit of DWO files. */
10916 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10917
10918 /* The value of the DW_AT_ranges attribute is the offset of the
10919 address range list in the .debug_ranges section. */
ab435259
DE
10920 unsigned long offset = (DW_UNSND (attr)
10921 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10922 const gdb_byte *buffer;
801e3a5b
JB
10923
10924 /* For some target architectures, but not others, the
10925 read_address function sign-extends the addresses it returns.
10926 To recognize base address selection entries, we need a
10927 mask. */
10928 unsigned int addr_size = cu->header.addr_size;
10929 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10930
10931 /* The base address, to which the next pair is relative. Note
10932 that this 'base' is a DWARF concept: most entries in a range
10933 list are relative, to reduce the number of relocs against the
10934 debugging information. This is separate from this function's
10935 'baseaddr' argument, which GDB uses to relocate debugging
10936 information from a shared library based on the address at
10937 which the library was loaded. */
d00adf39
DE
10938 CORE_ADDR base = cu->base_address;
10939 int base_known = cu->base_known;
801e3a5b 10940
d62bfeaf 10941 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10942 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10943 {
10944 complaint (&symfile_complaints,
10945 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10946 offset);
10947 return;
10948 }
d62bfeaf 10949 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
10950
10951 for (;;)
10952 {
10953 unsigned int bytes_read;
10954 CORE_ADDR start, end;
10955
10956 start = read_address (obfd, buffer, cu, &bytes_read);
10957 buffer += bytes_read;
10958 end = read_address (obfd, buffer, cu, &bytes_read);
10959 buffer += bytes_read;
10960
10961 /* Did we find the end of the range list? */
10962 if (start == 0 && end == 0)
10963 break;
10964
10965 /* Did we find a base address selection entry? */
10966 else if ((start & base_select_mask) == base_select_mask)
10967 {
10968 base = end;
10969 base_known = 1;
10970 }
10971
10972 /* We found an ordinary address range. */
10973 else
10974 {
10975 if (!base_known)
10976 {
10977 complaint (&symfile_complaints,
3e43a32a
MS
10978 _("Invalid .debug_ranges data "
10979 "(no base address)"));
801e3a5b
JB
10980 return;
10981 }
10982
9277c30c
UW
10983 if (start > end)
10984 {
10985 /* Inverted range entries are invalid. */
10986 complaint (&symfile_complaints,
10987 _("Invalid .debug_ranges data "
10988 "(inverted range)"));
10989 return;
10990 }
10991
10992 /* Empty range entries have no effect. */
10993 if (start == end)
10994 continue;
10995
01093045
DE
10996 start += base + baseaddr;
10997 end += base + baseaddr;
10998
10999 /* A not-uncommon case of bad debug info.
11000 Don't pollute the addrmap with bad data. */
11001 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11002 {
11003 complaint (&symfile_complaints,
11004 _(".debug_ranges entry has start address of zero"
11005 " [in module %s]"), objfile->name);
11006 continue;
11007 }
11008
11009 record_block_range (block, start, end - 1);
801e3a5b
JB
11010 }
11011 }
11012 }
11013}
11014
685b1105
JK
11015/* Check whether the producer field indicates either of GCC < 4.6, or the
11016 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 11017
685b1105
JK
11018static void
11019check_producer (struct dwarf2_cu *cu)
60d5a603
JK
11020{
11021 const char *cs;
11022 int major, minor, release;
11023
11024 if (cu->producer == NULL)
11025 {
11026 /* For unknown compilers expect their behavior is DWARF version
11027 compliant.
11028
11029 GCC started to support .debug_types sections by -gdwarf-4 since
11030 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11031 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11032 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11033 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11034 }
685b1105 11035 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11036 {
685b1105
JK
11037 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11038
ba919b58
TT
11039 cs = &cu->producer[strlen ("GNU ")];
11040 while (*cs && !isdigit (*cs))
11041 cs++;
11042 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11043 {
11044 /* Not recognized as GCC. */
11045 }
11046 else
1b80a9fa
JK
11047 {
11048 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11049 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11050 }
685b1105
JK
11051 }
11052 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11053 cu->producer_is_icc = 1;
11054 else
11055 {
11056 /* For other non-GCC compilers, expect their behavior is DWARF version
11057 compliant. */
60d5a603
JK
11058 }
11059
ba919b58 11060 cu->checked_producer = 1;
685b1105 11061}
ba919b58 11062
685b1105
JK
11063/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11064 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11065 during 4.6.0 experimental. */
11066
11067static int
11068producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11069{
11070 if (!cu->checked_producer)
11071 check_producer (cu);
11072
11073 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11074}
11075
11076/* Return the default accessibility type if it is not overriden by
11077 DW_AT_accessibility. */
11078
11079static enum dwarf_access_attribute
11080dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11081{
11082 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11083 {
11084 /* The default DWARF 2 accessibility for members is public, the default
11085 accessibility for inheritance is private. */
11086
11087 if (die->tag != DW_TAG_inheritance)
11088 return DW_ACCESS_public;
11089 else
11090 return DW_ACCESS_private;
11091 }
11092 else
11093 {
11094 /* DWARF 3+ defines the default accessibility a different way. The same
11095 rules apply now for DW_TAG_inheritance as for the members and it only
11096 depends on the container kind. */
11097
11098 if (die->parent->tag == DW_TAG_class_type)
11099 return DW_ACCESS_private;
11100 else
11101 return DW_ACCESS_public;
11102 }
11103}
11104
74ac6d43
TT
11105/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11106 offset. If the attribute was not found return 0, otherwise return
11107 1. If it was found but could not properly be handled, set *OFFSET
11108 to 0. */
11109
11110static int
11111handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11112 LONGEST *offset)
11113{
11114 struct attribute *attr;
11115
11116 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11117 if (attr != NULL)
11118 {
11119 *offset = 0;
11120
11121 /* Note that we do not check for a section offset first here.
11122 This is because DW_AT_data_member_location is new in DWARF 4,
11123 so if we see it, we can assume that a constant form is really
11124 a constant and not a section offset. */
11125 if (attr_form_is_constant (attr))
11126 *offset = dwarf2_get_attr_constant_value (attr, 0);
11127 else if (attr_form_is_section_offset (attr))
11128 dwarf2_complex_location_expr_complaint ();
11129 else if (attr_form_is_block (attr))
11130 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11131 else
11132 dwarf2_complex_location_expr_complaint ();
11133
11134 return 1;
11135 }
11136
11137 return 0;
11138}
11139
c906108c
SS
11140/* Add an aggregate field to the field list. */
11141
11142static void
107d2387 11143dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11144 struct dwarf2_cu *cu)
6e70227d 11145{
e7c27a73 11146 struct objfile *objfile = cu->objfile;
5e2b427d 11147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11148 struct nextfield *new_field;
11149 struct attribute *attr;
11150 struct field *fp;
15d034d0 11151 const char *fieldname = "";
c906108c
SS
11152
11153 /* Allocate a new field list entry and link it in. */
11154 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11155 make_cleanup (xfree, new_field);
c906108c 11156 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11157
11158 if (die->tag == DW_TAG_inheritance)
11159 {
11160 new_field->next = fip->baseclasses;
11161 fip->baseclasses = new_field;
11162 }
11163 else
11164 {
11165 new_field->next = fip->fields;
11166 fip->fields = new_field;
11167 }
c906108c
SS
11168 fip->nfields++;
11169
e142c38c 11170 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11171 if (attr)
11172 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11173 else
11174 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11175 if (new_field->accessibility != DW_ACCESS_public)
11176 fip->non_public_fields = 1;
60d5a603 11177
e142c38c 11178 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11179 if (attr)
11180 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11181 else
11182 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11183
11184 fp = &new_field->field;
a9a9bd0f 11185
e142c38c 11186 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11187 {
74ac6d43
TT
11188 LONGEST offset;
11189
a9a9bd0f 11190 /* Data member other than a C++ static data member. */
6e70227d 11191
c906108c 11192 /* Get type of field. */
e7c27a73 11193 fp->type = die_type (die, cu);
c906108c 11194
d6a843b5 11195 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11196
c906108c 11197 /* Get bit size of field (zero if none). */
e142c38c 11198 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11199 if (attr)
11200 {
11201 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11202 }
11203 else
11204 {
11205 FIELD_BITSIZE (*fp) = 0;
11206 }
11207
11208 /* Get bit offset of field. */
74ac6d43
TT
11209 if (handle_data_member_location (die, cu, &offset))
11210 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11211 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11212 if (attr)
11213 {
5e2b427d 11214 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11215 {
11216 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11217 additional bit offset from the MSB of the containing
11218 anonymous object to the MSB of the field. We don't
11219 have to do anything special since we don't need to
11220 know the size of the anonymous object. */
f41f5e61 11221 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11222 }
11223 else
11224 {
11225 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11226 MSB of the anonymous object, subtract off the number of
11227 bits from the MSB of the field to the MSB of the
11228 object, and then subtract off the number of bits of
11229 the field itself. The result is the bit offset of
11230 the LSB of the field. */
c906108c
SS
11231 int anonymous_size;
11232 int bit_offset = DW_UNSND (attr);
11233
e142c38c 11234 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11235 if (attr)
11236 {
11237 /* The size of the anonymous object containing
11238 the bit field is explicit, so use the
11239 indicated size (in bytes). */
11240 anonymous_size = DW_UNSND (attr);
11241 }
11242 else
11243 {
11244 /* The size of the anonymous object containing
11245 the bit field must be inferred from the type
11246 attribute of the data member containing the
11247 bit field. */
11248 anonymous_size = TYPE_LENGTH (fp->type);
11249 }
f41f5e61
PA
11250 SET_FIELD_BITPOS (*fp,
11251 (FIELD_BITPOS (*fp)
11252 + anonymous_size * bits_per_byte
11253 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11254 }
11255 }
11256
11257 /* Get name of field. */
39cbfefa
DJ
11258 fieldname = dwarf2_name (die, cu);
11259 if (fieldname == NULL)
11260 fieldname = "";
d8151005
DJ
11261
11262 /* The name is already allocated along with this objfile, so we don't
11263 need to duplicate it for the type. */
11264 fp->name = fieldname;
c906108c
SS
11265
11266 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11267 pointer or virtual base class pointer) to private. */
e142c38c 11268 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11269 {
d48cc9dd 11270 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11271 new_field->accessibility = DW_ACCESS_private;
11272 fip->non_public_fields = 1;
11273 }
11274 }
a9a9bd0f 11275 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11276 {
a9a9bd0f
DC
11277 /* C++ static member. */
11278
11279 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11280 is a declaration, but all versions of G++ as of this writing
11281 (so through at least 3.2.1) incorrectly generate
11282 DW_TAG_variable tags. */
6e70227d 11283
ff355380 11284 const char *physname;
c906108c 11285
a9a9bd0f 11286 /* Get name of field. */
39cbfefa
DJ
11287 fieldname = dwarf2_name (die, cu);
11288 if (fieldname == NULL)
c906108c
SS
11289 return;
11290
254e6b9e 11291 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11292 if (attr
11293 /* Only create a symbol if this is an external value.
11294 new_symbol checks this and puts the value in the global symbol
11295 table, which we want. If it is not external, new_symbol
11296 will try to put the value in cu->list_in_scope which is wrong. */
11297 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11298 {
11299 /* A static const member, not much different than an enum as far as
11300 we're concerned, except that we can support more types. */
11301 new_symbol (die, NULL, cu);
11302 }
11303
2df3850c 11304 /* Get physical name. */
ff355380 11305 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11306
d8151005
DJ
11307 /* The name is already allocated along with this objfile, so we don't
11308 need to duplicate it for the type. */
11309 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11310 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11311 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11312 }
11313 else if (die->tag == DW_TAG_inheritance)
11314 {
74ac6d43 11315 LONGEST offset;
d4b96c9a 11316
74ac6d43
TT
11317 /* C++ base class field. */
11318 if (handle_data_member_location (die, cu, &offset))
11319 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11320 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11321 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11322 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11323 fip->nbaseclasses++;
11324 }
11325}
11326
98751a41
JK
11327/* Add a typedef defined in the scope of the FIP's class. */
11328
11329static void
11330dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11331 struct dwarf2_cu *cu)
6e70227d 11332{
98751a41 11333 struct objfile *objfile = cu->objfile;
98751a41
JK
11334 struct typedef_field_list *new_field;
11335 struct attribute *attr;
11336 struct typedef_field *fp;
11337 char *fieldname = "";
11338
11339 /* Allocate a new field list entry and link it in. */
11340 new_field = xzalloc (sizeof (*new_field));
11341 make_cleanup (xfree, new_field);
11342
11343 gdb_assert (die->tag == DW_TAG_typedef);
11344
11345 fp = &new_field->field;
11346
11347 /* Get name of field. */
11348 fp->name = dwarf2_name (die, cu);
11349 if (fp->name == NULL)
11350 return;
11351
11352 fp->type = read_type_die (die, cu);
11353
11354 new_field->next = fip->typedef_field_list;
11355 fip->typedef_field_list = new_field;
11356 fip->typedef_field_list_count++;
11357}
11358
c906108c
SS
11359/* Create the vector of fields, and attach it to the type. */
11360
11361static void
fba45db2 11362dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11363 struct dwarf2_cu *cu)
c906108c
SS
11364{
11365 int nfields = fip->nfields;
11366
11367 /* Record the field count, allocate space for the array of fields,
11368 and create blank accessibility bitfields if necessary. */
11369 TYPE_NFIELDS (type) = nfields;
11370 TYPE_FIELDS (type) = (struct field *)
11371 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11372 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11373
b4ba55a1 11374 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11375 {
11376 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11377
11378 TYPE_FIELD_PRIVATE_BITS (type) =
11379 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11380 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11381
11382 TYPE_FIELD_PROTECTED_BITS (type) =
11383 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11384 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11385
774b6a14
TT
11386 TYPE_FIELD_IGNORE_BITS (type) =
11387 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11388 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11389 }
11390
11391 /* If the type has baseclasses, allocate and clear a bit vector for
11392 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11393 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11394 {
11395 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11396 unsigned char *pointer;
c906108c
SS
11397
11398 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11399 pointer = TYPE_ALLOC (type, num_bytes);
11400 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11401 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11402 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11403 }
11404
3e43a32a
MS
11405 /* Copy the saved-up fields into the field vector. Start from the head of
11406 the list, adding to the tail of the field array, so that they end up in
11407 the same order in the array in which they were added to the list. */
c906108c
SS
11408 while (nfields-- > 0)
11409 {
7d0ccb61
DJ
11410 struct nextfield *fieldp;
11411
11412 if (fip->fields)
11413 {
11414 fieldp = fip->fields;
11415 fip->fields = fieldp->next;
11416 }
11417 else
11418 {
11419 fieldp = fip->baseclasses;
11420 fip->baseclasses = fieldp->next;
11421 }
11422
11423 TYPE_FIELD (type, nfields) = fieldp->field;
11424 switch (fieldp->accessibility)
c906108c 11425 {
c5aa993b 11426 case DW_ACCESS_private:
b4ba55a1
JB
11427 if (cu->language != language_ada)
11428 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11429 break;
c906108c 11430
c5aa993b 11431 case DW_ACCESS_protected:
b4ba55a1
JB
11432 if (cu->language != language_ada)
11433 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11434 break;
c906108c 11435
c5aa993b
JM
11436 case DW_ACCESS_public:
11437 break;
c906108c 11438
c5aa993b
JM
11439 default:
11440 /* Unknown accessibility. Complain and treat it as public. */
11441 {
e2e0b3e5 11442 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11443 fieldp->accessibility);
c5aa993b
JM
11444 }
11445 break;
c906108c
SS
11446 }
11447 if (nfields < fip->nbaseclasses)
11448 {
7d0ccb61 11449 switch (fieldp->virtuality)
c906108c 11450 {
c5aa993b
JM
11451 case DW_VIRTUALITY_virtual:
11452 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11453 if (cu->language == language_ada)
a73c6dcd 11454 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11455 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11456 break;
c906108c
SS
11457 }
11458 }
c906108c
SS
11459 }
11460}
11461
7d27a96d
TT
11462/* Return true if this member function is a constructor, false
11463 otherwise. */
11464
11465static int
11466dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11467{
11468 const char *fieldname;
11469 const char *typename;
11470 int len;
11471
11472 if (die->parent == NULL)
11473 return 0;
11474
11475 if (die->parent->tag != DW_TAG_structure_type
11476 && die->parent->tag != DW_TAG_union_type
11477 && die->parent->tag != DW_TAG_class_type)
11478 return 0;
11479
11480 fieldname = dwarf2_name (die, cu);
11481 typename = dwarf2_name (die->parent, cu);
11482 if (fieldname == NULL || typename == NULL)
11483 return 0;
11484
11485 len = strlen (fieldname);
11486 return (strncmp (fieldname, typename, len) == 0
11487 && (typename[len] == '\0' || typename[len] == '<'));
11488}
11489
c906108c
SS
11490/* Add a member function to the proper fieldlist. */
11491
11492static void
107d2387 11493dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11494 struct type *type, struct dwarf2_cu *cu)
c906108c 11495{
e7c27a73 11496 struct objfile *objfile = cu->objfile;
c906108c
SS
11497 struct attribute *attr;
11498 struct fnfieldlist *flp;
11499 int i;
11500 struct fn_field *fnp;
15d034d0 11501 const char *fieldname;
c906108c 11502 struct nextfnfield *new_fnfield;
f792889a 11503 struct type *this_type;
60d5a603 11504 enum dwarf_access_attribute accessibility;
c906108c 11505
b4ba55a1 11506 if (cu->language == language_ada)
a73c6dcd 11507 error (_("unexpected member function in Ada type"));
b4ba55a1 11508
2df3850c 11509 /* Get name of member function. */
39cbfefa
DJ
11510 fieldname = dwarf2_name (die, cu);
11511 if (fieldname == NULL)
2df3850c 11512 return;
c906108c 11513
c906108c
SS
11514 /* Look up member function name in fieldlist. */
11515 for (i = 0; i < fip->nfnfields; i++)
11516 {
27bfe10e 11517 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11518 break;
11519 }
11520
11521 /* Create new list element if necessary. */
11522 if (i < fip->nfnfields)
11523 flp = &fip->fnfieldlists[i];
11524 else
11525 {
11526 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11527 {
11528 fip->fnfieldlists = (struct fnfieldlist *)
11529 xrealloc (fip->fnfieldlists,
11530 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11531 * sizeof (struct fnfieldlist));
c906108c 11532 if (fip->nfnfields == 0)
c13c43fd 11533 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11534 }
11535 flp = &fip->fnfieldlists[fip->nfnfields];
11536 flp->name = fieldname;
11537 flp->length = 0;
11538 flp->head = NULL;
3da10d80 11539 i = fip->nfnfields++;
c906108c
SS
11540 }
11541
11542 /* Create a new member function field and chain it to the field list
0963b4bd 11543 entry. */
c906108c 11544 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11545 make_cleanup (xfree, new_fnfield);
c906108c
SS
11546 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11547 new_fnfield->next = flp->head;
11548 flp->head = new_fnfield;
11549 flp->length++;
11550
11551 /* Fill in the member function field info. */
11552 fnp = &new_fnfield->fnfield;
3da10d80
KS
11553
11554 /* Delay processing of the physname until later. */
11555 if (cu->language == language_cplus || cu->language == language_java)
11556 {
11557 add_to_method_list (type, i, flp->length - 1, fieldname,
11558 die, cu);
11559 }
11560 else
11561 {
1d06ead6 11562 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11563 fnp->physname = physname ? physname : "";
11564 }
11565
c906108c 11566 fnp->type = alloc_type (objfile);
f792889a
DJ
11567 this_type = read_type_die (die, cu);
11568 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11569 {
f792889a 11570 int nparams = TYPE_NFIELDS (this_type);
c906108c 11571
f792889a 11572 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11573 of the method itself (TYPE_CODE_METHOD). */
11574 smash_to_method_type (fnp->type, type,
f792889a
DJ
11575 TYPE_TARGET_TYPE (this_type),
11576 TYPE_FIELDS (this_type),
11577 TYPE_NFIELDS (this_type),
11578 TYPE_VARARGS (this_type));
c906108c
SS
11579
11580 /* Handle static member functions.
c5aa993b 11581 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11582 member functions. G++ helps GDB by marking the first
11583 parameter for non-static member functions (which is the this
11584 pointer) as artificial. We obtain this information from
11585 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11586 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11587 fnp->voffset = VOFFSET_STATIC;
11588 }
11589 else
e2e0b3e5 11590 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11591 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11592
11593 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11594 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11595 fnp->fcontext = die_containing_type (die, cu);
c906108c 11596
3e43a32a
MS
11597 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11598 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11599
11600 /* Get accessibility. */
e142c38c 11601 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11602 if (attr)
60d5a603
JK
11603 accessibility = DW_UNSND (attr);
11604 else
11605 accessibility = dwarf2_default_access_attribute (die, cu);
11606 switch (accessibility)
c906108c 11607 {
60d5a603
JK
11608 case DW_ACCESS_private:
11609 fnp->is_private = 1;
11610 break;
11611 case DW_ACCESS_protected:
11612 fnp->is_protected = 1;
11613 break;
c906108c
SS
11614 }
11615
b02dede2 11616 /* Check for artificial methods. */
e142c38c 11617 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11618 if (attr && DW_UNSND (attr) != 0)
11619 fnp->is_artificial = 1;
11620
7d27a96d
TT
11621 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11622
0d564a31 11623 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11624 function. For older versions of GCC, this is an offset in the
11625 appropriate virtual table, as specified by DW_AT_containing_type.
11626 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11627 to the object address. */
11628
e142c38c 11629 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11630 if (attr)
8e19ed76 11631 {
aec5aa8b 11632 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11633 {
aec5aa8b
TT
11634 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11635 {
11636 /* Old-style GCC. */
11637 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11638 }
11639 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11640 || (DW_BLOCK (attr)->size > 1
11641 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11642 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11643 {
11644 struct dwarf_block blk;
11645 int offset;
11646
11647 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11648 ? 1 : 2);
11649 blk.size = DW_BLOCK (attr)->size - offset;
11650 blk.data = DW_BLOCK (attr)->data + offset;
11651 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11652 if ((fnp->voffset % cu->header.addr_size) != 0)
11653 dwarf2_complex_location_expr_complaint ();
11654 else
11655 fnp->voffset /= cu->header.addr_size;
11656 fnp->voffset += 2;
11657 }
11658 else
11659 dwarf2_complex_location_expr_complaint ();
11660
11661 if (!fnp->fcontext)
11662 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11663 }
3690dd37 11664 else if (attr_form_is_section_offset (attr))
8e19ed76 11665 {
4d3c2250 11666 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11667 }
11668 else
11669 {
4d3c2250
KB
11670 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11671 fieldname);
8e19ed76 11672 }
0d564a31 11673 }
d48cc9dd
DJ
11674 else
11675 {
11676 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11677 if (attr && DW_UNSND (attr))
11678 {
11679 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11680 complaint (&symfile_complaints,
3e43a32a
MS
11681 _("Member function \"%s\" (offset %d) is virtual "
11682 "but the vtable offset is not specified"),
b64f50a1 11683 fieldname, die->offset.sect_off);
9655fd1a 11684 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11685 TYPE_CPLUS_DYNAMIC (type) = 1;
11686 }
11687 }
c906108c
SS
11688}
11689
11690/* Create the vector of member function fields, and attach it to the type. */
11691
11692static void
fba45db2 11693dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11694 struct dwarf2_cu *cu)
c906108c
SS
11695{
11696 struct fnfieldlist *flp;
c906108c
SS
11697 int i;
11698
b4ba55a1 11699 if (cu->language == language_ada)
a73c6dcd 11700 error (_("unexpected member functions in Ada type"));
b4ba55a1 11701
c906108c
SS
11702 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11703 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11704 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11705
11706 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11707 {
11708 struct nextfnfield *nfp = flp->head;
11709 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11710 int k;
11711
11712 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11713 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11714 fn_flp->fn_fields = (struct fn_field *)
11715 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11716 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11717 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11718 }
11719
11720 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11721}
11722
1168df01
JB
11723/* Returns non-zero if NAME is the name of a vtable member in CU's
11724 language, zero otherwise. */
11725static int
11726is_vtable_name (const char *name, struct dwarf2_cu *cu)
11727{
11728 static const char vptr[] = "_vptr";
987504bb 11729 static const char vtable[] = "vtable";
1168df01 11730
987504bb
JJ
11731 /* Look for the C++ and Java forms of the vtable. */
11732 if ((cu->language == language_java
11733 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11734 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11735 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11736 return 1;
11737
11738 return 0;
11739}
11740
c0dd20ea 11741/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11742 functions, with the ABI-specified layout. If TYPE describes
11743 such a structure, smash it into a member function type.
61049d3b
DJ
11744
11745 GCC shouldn't do this; it should just output pointer to member DIEs.
11746 This is GCC PR debug/28767. */
c0dd20ea 11747
0b92b5bb
TT
11748static void
11749quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11750{
0b92b5bb 11751 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11752
11753 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11754 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11755 return;
c0dd20ea
DJ
11756
11757 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11758 if (TYPE_FIELD_NAME (type, 0) == NULL
11759 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11760 || TYPE_FIELD_NAME (type, 1) == NULL
11761 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11762 return;
c0dd20ea
DJ
11763
11764 /* Find the type of the method. */
0b92b5bb 11765 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11766 if (pfn_type == NULL
11767 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11768 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11769 return;
c0dd20ea
DJ
11770
11771 /* Look for the "this" argument. */
11772 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11773 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11774 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11775 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11776 return;
c0dd20ea
DJ
11777
11778 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11779 new_type = alloc_type (objfile);
11780 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11781 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11782 TYPE_VARARGS (pfn_type));
0b92b5bb 11783 smash_to_methodptr_type (type, new_type);
c0dd20ea 11784}
1168df01 11785
685b1105
JK
11786/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11787 (icc). */
11788
11789static int
11790producer_is_icc (struct dwarf2_cu *cu)
11791{
11792 if (!cu->checked_producer)
11793 check_producer (cu);
11794
11795 return cu->producer_is_icc;
11796}
11797
c906108c 11798/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11799 (definition) to create a type for the structure or union. Fill in
11800 the type's name and general properties; the members will not be
3d1d5ea3 11801 processed until process_structure_scope.
c906108c 11802
c767944b
DJ
11803 NOTE: we need to call these functions regardless of whether or not the
11804 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11805 structure or union. This gets the type entered into our set of
11806 user defined types.
11807
11808 However, if the structure is incomplete (an opaque struct/union)
11809 then suppress creating a symbol table entry for it since gdb only
11810 wants to find the one with the complete definition. Note that if
11811 it is complete, we just call new_symbol, which does it's own
11812 checking about whether the struct/union is anonymous or not (and
11813 suppresses creating a symbol table entry itself). */
11814
f792889a 11815static struct type *
134d01f1 11816read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11817{
e7c27a73 11818 struct objfile *objfile = cu->objfile;
c906108c
SS
11819 struct type *type;
11820 struct attribute *attr;
15d034d0 11821 const char *name;
c906108c 11822
348e048f
DE
11823 /* If the definition of this type lives in .debug_types, read that type.
11824 Don't follow DW_AT_specification though, that will take us back up
11825 the chain and we want to go down. */
45e58e77 11826 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11827 if (attr)
11828 {
ac9ec31b 11829 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11830
ac9ec31b 11831 /* The type's CU may not be the same as CU.
02142a6c 11832 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11833 return set_die_type (die, type, cu);
11834 }
11835
c0dd20ea 11836 type = alloc_type (objfile);
c906108c 11837 INIT_CPLUS_SPECIFIC (type);
93311388 11838
39cbfefa
DJ
11839 name = dwarf2_name (die, cu);
11840 if (name != NULL)
c906108c 11841 {
987504bb
JJ
11842 if (cu->language == language_cplus
11843 || cu->language == language_java)
63d06c5c 11844 {
15d034d0 11845 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11846
11847 /* dwarf2_full_name might have already finished building the DIE's
11848 type. If so, there is no need to continue. */
11849 if (get_die_type (die, cu) != NULL)
11850 return get_die_type (die, cu);
11851
11852 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11853 if (die->tag == DW_TAG_structure_type
11854 || die->tag == DW_TAG_class_type)
11855 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11856 }
11857 else
11858 {
d8151005
DJ
11859 /* The name is already allocated along with this objfile, so
11860 we don't need to duplicate it for the type. */
7d455152 11861 TYPE_TAG_NAME (type) = name;
94af9270
KS
11862 if (die->tag == DW_TAG_class_type)
11863 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11864 }
c906108c
SS
11865 }
11866
11867 if (die->tag == DW_TAG_structure_type)
11868 {
11869 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11870 }
11871 else if (die->tag == DW_TAG_union_type)
11872 {
11873 TYPE_CODE (type) = TYPE_CODE_UNION;
11874 }
11875 else
11876 {
c906108c
SS
11877 TYPE_CODE (type) = TYPE_CODE_CLASS;
11878 }
11879
0cc2414c
TT
11880 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11881 TYPE_DECLARED_CLASS (type) = 1;
11882
e142c38c 11883 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11884 if (attr)
11885 {
11886 TYPE_LENGTH (type) = DW_UNSND (attr);
11887 }
11888 else
11889 {
11890 TYPE_LENGTH (type) = 0;
11891 }
11892
685b1105
JK
11893 if (producer_is_icc (cu))
11894 {
11895 /* ICC does not output the required DW_AT_declaration
11896 on incomplete types, but gives them a size of zero. */
11897 }
11898 else
11899 TYPE_STUB_SUPPORTED (type) = 1;
11900
dc718098 11901 if (die_is_declaration (die, cu))
876cecd0 11902 TYPE_STUB (type) = 1;
a6c727b2
DJ
11903 else if (attr == NULL && die->child == NULL
11904 && producer_is_realview (cu->producer))
11905 /* RealView does not output the required DW_AT_declaration
11906 on incomplete types. */
11907 TYPE_STUB (type) = 1;
dc718098 11908
c906108c
SS
11909 /* We need to add the type field to the die immediately so we don't
11910 infinitely recurse when dealing with pointers to the structure
0963b4bd 11911 type within the structure itself. */
1c379e20 11912 set_die_type (die, type, cu);
c906108c 11913
7e314c57
JK
11914 /* set_die_type should be already done. */
11915 set_descriptive_type (type, die, cu);
11916
c767944b
DJ
11917 return type;
11918}
11919
11920/* Finish creating a structure or union type, including filling in
11921 its members and creating a symbol for it. */
11922
11923static void
11924process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11925{
11926 struct objfile *objfile = cu->objfile;
11927 struct die_info *child_die = die->child;
11928 struct type *type;
11929
11930 type = get_die_type (die, cu);
11931 if (type == NULL)
11932 type = read_structure_type (die, cu);
11933
e142c38c 11934 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11935 {
11936 struct field_info fi;
11937 struct die_info *child_die;
34eaf542 11938 VEC (symbolp) *template_args = NULL;
c767944b 11939 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11940
11941 memset (&fi, 0, sizeof (struct field_info));
11942
639d11d3 11943 child_die = die->child;
c906108c
SS
11944
11945 while (child_die && child_die->tag)
11946 {
a9a9bd0f
DC
11947 if (child_die->tag == DW_TAG_member
11948 || child_die->tag == DW_TAG_variable)
c906108c 11949 {
a9a9bd0f
DC
11950 /* NOTE: carlton/2002-11-05: A C++ static data member
11951 should be a DW_TAG_member that is a declaration, but
11952 all versions of G++ as of this writing (so through at
11953 least 3.2.1) incorrectly generate DW_TAG_variable
11954 tags for them instead. */
e7c27a73 11955 dwarf2_add_field (&fi, child_die, cu);
c906108c 11956 }
8713b1b1 11957 else if (child_die->tag == DW_TAG_subprogram)
c906108c 11958 {
0963b4bd 11959 /* C++ member function. */
e7c27a73 11960 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
11961 }
11962 else if (child_die->tag == DW_TAG_inheritance)
11963 {
11964 /* C++ base class field. */
e7c27a73 11965 dwarf2_add_field (&fi, child_die, cu);
c906108c 11966 }
98751a41
JK
11967 else if (child_die->tag == DW_TAG_typedef)
11968 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
11969 else if (child_die->tag == DW_TAG_template_type_param
11970 || child_die->tag == DW_TAG_template_value_param)
11971 {
11972 struct symbol *arg = new_symbol (child_die, NULL, cu);
11973
f1078f66
DJ
11974 if (arg != NULL)
11975 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
11976 }
11977
c906108c
SS
11978 child_die = sibling_die (child_die);
11979 }
11980
34eaf542
TT
11981 /* Attach template arguments to type. */
11982 if (! VEC_empty (symbolp, template_args))
11983 {
11984 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11985 TYPE_N_TEMPLATE_ARGUMENTS (type)
11986 = VEC_length (symbolp, template_args);
11987 TYPE_TEMPLATE_ARGUMENTS (type)
11988 = obstack_alloc (&objfile->objfile_obstack,
11989 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11990 * sizeof (struct symbol *)));
11991 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11992 VEC_address (symbolp, template_args),
11993 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11994 * sizeof (struct symbol *)));
11995 VEC_free (symbolp, template_args);
11996 }
11997
c906108c
SS
11998 /* Attach fields and member functions to the type. */
11999 if (fi.nfields)
e7c27a73 12000 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
12001 if (fi.nfnfields)
12002 {
e7c27a73 12003 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 12004
c5aa993b 12005 /* Get the type which refers to the base class (possibly this
c906108c 12006 class itself) which contains the vtable pointer for the current
0d564a31
DJ
12007 class from the DW_AT_containing_type attribute. This use of
12008 DW_AT_containing_type is a GNU extension. */
c906108c 12009
e142c38c 12010 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 12011 {
e7c27a73 12012 struct type *t = die_containing_type (die, cu);
c906108c
SS
12013
12014 TYPE_VPTR_BASETYPE (type) = t;
12015 if (type == t)
12016 {
c906108c
SS
12017 int i;
12018
12019 /* Our own class provides vtbl ptr. */
12020 for (i = TYPE_NFIELDS (t) - 1;
12021 i >= TYPE_N_BASECLASSES (t);
12022 --i)
12023 {
0d5cff50 12024 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 12025
1168df01 12026 if (is_vtable_name (fieldname, cu))
c906108c
SS
12027 {
12028 TYPE_VPTR_FIELDNO (type) = i;
12029 break;
12030 }
12031 }
12032
12033 /* Complain if virtual function table field not found. */
12034 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12035 complaint (&symfile_complaints,
3e43a32a
MS
12036 _("virtual function table pointer "
12037 "not found when defining class '%s'"),
4d3c2250
KB
12038 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12039 "");
c906108c
SS
12040 }
12041 else
12042 {
12043 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12044 }
12045 }
f6235d4c
EZ
12046 else if (cu->producer
12047 && strncmp (cu->producer,
12048 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12049 {
12050 /* The IBM XLC compiler does not provide direct indication
12051 of the containing type, but the vtable pointer is
12052 always named __vfp. */
12053
12054 int i;
12055
12056 for (i = TYPE_NFIELDS (type) - 1;
12057 i >= TYPE_N_BASECLASSES (type);
12058 --i)
12059 {
12060 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12061 {
12062 TYPE_VPTR_FIELDNO (type) = i;
12063 TYPE_VPTR_BASETYPE (type) = type;
12064 break;
12065 }
12066 }
12067 }
c906108c 12068 }
98751a41
JK
12069
12070 /* Copy fi.typedef_field_list linked list elements content into the
12071 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12072 if (fi.typedef_field_list)
12073 {
12074 int i = fi.typedef_field_list_count;
12075
a0d7a4ff 12076 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12077 TYPE_TYPEDEF_FIELD_ARRAY (type)
12078 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12079 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12080
12081 /* Reverse the list order to keep the debug info elements order. */
12082 while (--i >= 0)
12083 {
12084 struct typedef_field *dest, *src;
6e70227d 12085
98751a41
JK
12086 dest = &TYPE_TYPEDEF_FIELD (type, i);
12087 src = &fi.typedef_field_list->field;
12088 fi.typedef_field_list = fi.typedef_field_list->next;
12089 *dest = *src;
12090 }
12091 }
c767944b
DJ
12092
12093 do_cleanups (back_to);
eb2a6f42
TT
12094
12095 if (HAVE_CPLUS_STRUCT (type))
12096 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12097 }
63d06c5c 12098
bb5ed363 12099 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12100
90aeadfc
DC
12101 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12102 snapshots) has been known to create a die giving a declaration
12103 for a class that has, as a child, a die giving a definition for a
12104 nested class. So we have to process our children even if the
12105 current die is a declaration. Normally, of course, a declaration
12106 won't have any children at all. */
134d01f1 12107
90aeadfc
DC
12108 while (child_die != NULL && child_die->tag)
12109 {
12110 if (child_die->tag == DW_TAG_member
12111 || child_die->tag == DW_TAG_variable
34eaf542
TT
12112 || child_die->tag == DW_TAG_inheritance
12113 || child_die->tag == DW_TAG_template_value_param
12114 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12115 {
90aeadfc 12116 /* Do nothing. */
134d01f1 12117 }
90aeadfc
DC
12118 else
12119 process_die (child_die, cu);
134d01f1 12120
90aeadfc 12121 child_die = sibling_die (child_die);
134d01f1
DJ
12122 }
12123
fa4028e9
JB
12124 /* Do not consider external references. According to the DWARF standard,
12125 these DIEs are identified by the fact that they have no byte_size
12126 attribute, and a declaration attribute. */
12127 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12128 || !die_is_declaration (die, cu))
c767944b 12129 new_symbol (die, type, cu);
134d01f1
DJ
12130}
12131
12132/* Given a DW_AT_enumeration_type die, set its type. We do not
12133 complete the type's fields yet, or create any symbols. */
c906108c 12134
f792889a 12135static struct type *
134d01f1 12136read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12137{
e7c27a73 12138 struct objfile *objfile = cu->objfile;
c906108c 12139 struct type *type;
c906108c 12140 struct attribute *attr;
0114d602 12141 const char *name;
134d01f1 12142
348e048f
DE
12143 /* If the definition of this type lives in .debug_types, read that type.
12144 Don't follow DW_AT_specification though, that will take us back up
12145 the chain and we want to go down. */
45e58e77 12146 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12147 if (attr)
12148 {
ac9ec31b 12149 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12150
ac9ec31b 12151 /* The type's CU may not be the same as CU.
02142a6c 12152 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12153 return set_die_type (die, type, cu);
12154 }
12155
c906108c
SS
12156 type = alloc_type (objfile);
12157
12158 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12159 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12160 if (name != NULL)
7d455152 12161 TYPE_TAG_NAME (type) = name;
c906108c 12162
e142c38c 12163 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12164 if (attr)
12165 {
12166 TYPE_LENGTH (type) = DW_UNSND (attr);
12167 }
12168 else
12169 {
12170 TYPE_LENGTH (type) = 0;
12171 }
12172
137033e9
JB
12173 /* The enumeration DIE can be incomplete. In Ada, any type can be
12174 declared as private in the package spec, and then defined only
12175 inside the package body. Such types are known as Taft Amendment
12176 Types. When another package uses such a type, an incomplete DIE
12177 may be generated by the compiler. */
02eb380e 12178 if (die_is_declaration (die, cu))
876cecd0 12179 TYPE_STUB (type) = 1;
02eb380e 12180
f792889a 12181 return set_die_type (die, type, cu);
134d01f1
DJ
12182}
12183
12184/* Given a pointer to a die which begins an enumeration, process all
12185 the dies that define the members of the enumeration, and create the
12186 symbol for the enumeration type.
12187
12188 NOTE: We reverse the order of the element list. */
12189
12190static void
12191process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12192{
f792889a 12193 struct type *this_type;
134d01f1 12194
f792889a
DJ
12195 this_type = get_die_type (die, cu);
12196 if (this_type == NULL)
12197 this_type = read_enumeration_type (die, cu);
9dc481d3 12198
639d11d3 12199 if (die->child != NULL)
c906108c 12200 {
9dc481d3
DE
12201 struct die_info *child_die;
12202 struct symbol *sym;
12203 struct field *fields = NULL;
12204 int num_fields = 0;
12205 int unsigned_enum = 1;
15d034d0 12206 const char *name;
cafec441
TT
12207 int flag_enum = 1;
12208 ULONGEST mask = 0;
9dc481d3 12209
639d11d3 12210 child_die = die->child;
c906108c
SS
12211 while (child_die && child_die->tag)
12212 {
12213 if (child_die->tag != DW_TAG_enumerator)
12214 {
e7c27a73 12215 process_die (child_die, cu);
c906108c
SS
12216 }
12217 else
12218 {
39cbfefa
DJ
12219 name = dwarf2_name (child_die, cu);
12220 if (name)
c906108c 12221 {
f792889a 12222 sym = new_symbol (child_die, this_type, cu);
c906108c 12223 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12224 {
12225 unsigned_enum = 0;
12226 flag_enum = 0;
12227 }
12228 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12229 flag_enum = 0;
12230 else
12231 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12232
12233 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12234 {
12235 fields = (struct field *)
12236 xrealloc (fields,
12237 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12238 * sizeof (struct field));
c906108c
SS
12239 }
12240
3567439c 12241 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12242 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12243 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12244 FIELD_BITSIZE (fields[num_fields]) = 0;
12245
12246 num_fields++;
12247 }
12248 }
12249
12250 child_die = sibling_die (child_die);
12251 }
12252
12253 if (num_fields)
12254 {
f792889a
DJ
12255 TYPE_NFIELDS (this_type) = num_fields;
12256 TYPE_FIELDS (this_type) = (struct field *)
12257 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12258 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12259 sizeof (struct field) * num_fields);
b8c9b27d 12260 xfree (fields);
c906108c
SS
12261 }
12262 if (unsigned_enum)
876cecd0 12263 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12264 if (flag_enum)
12265 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12266 }
134d01f1 12267
6c83ed52
TT
12268 /* If we are reading an enum from a .debug_types unit, and the enum
12269 is a declaration, and the enum is not the signatured type in the
12270 unit, then we do not want to add a symbol for it. Adding a
12271 symbol would in some cases obscure the true definition of the
12272 enum, giving users an incomplete type when the definition is
12273 actually available. Note that we do not want to do this for all
12274 enums which are just declarations, because C++0x allows forward
12275 enum declarations. */
3019eac3 12276 if (cu->per_cu->is_debug_types
6c83ed52
TT
12277 && die_is_declaration (die, cu))
12278 {
52dc124a 12279 struct signatured_type *sig_type;
6c83ed52 12280
c0f78cd4 12281 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12282 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12283 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12284 return;
12285 }
12286
f792889a 12287 new_symbol (die, this_type, cu);
c906108c
SS
12288}
12289
12290/* Extract all information from a DW_TAG_array_type DIE and put it in
12291 the DIE's type field. For now, this only handles one dimensional
12292 arrays. */
12293
f792889a 12294static struct type *
e7c27a73 12295read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12296{
e7c27a73 12297 struct objfile *objfile = cu->objfile;
c906108c 12298 struct die_info *child_die;
7e314c57 12299 struct type *type;
c906108c
SS
12300 struct type *element_type, *range_type, *index_type;
12301 struct type **range_types = NULL;
12302 struct attribute *attr;
12303 int ndim = 0;
12304 struct cleanup *back_to;
15d034d0 12305 const char *name;
c906108c 12306
e7c27a73 12307 element_type = die_type (die, cu);
c906108c 12308
7e314c57
JK
12309 /* The die_type call above may have already set the type for this DIE. */
12310 type = get_die_type (die, cu);
12311 if (type)
12312 return type;
12313
c906108c
SS
12314 /* Irix 6.2 native cc creates array types without children for
12315 arrays with unspecified length. */
639d11d3 12316 if (die->child == NULL)
c906108c 12317 {
46bf5051 12318 index_type = objfile_type (objfile)->builtin_int;
c906108c 12319 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12320 type = create_array_type (NULL, element_type, range_type);
12321 return set_die_type (die, type, cu);
c906108c
SS
12322 }
12323
12324 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12325 child_die = die->child;
c906108c
SS
12326 while (child_die && child_die->tag)
12327 {
12328 if (child_die->tag == DW_TAG_subrange_type)
12329 {
f792889a 12330 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12331
f792889a 12332 if (child_type != NULL)
a02abb62 12333 {
0963b4bd
MS
12334 /* The range type was succesfully read. Save it for the
12335 array type creation. */
a02abb62
JB
12336 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12337 {
12338 range_types = (struct type **)
12339 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12340 * sizeof (struct type *));
12341 if (ndim == 0)
12342 make_cleanup (free_current_contents, &range_types);
12343 }
f792889a 12344 range_types[ndim++] = child_type;
a02abb62 12345 }
c906108c
SS
12346 }
12347 child_die = sibling_die (child_die);
12348 }
12349
12350 /* Dwarf2 dimensions are output from left to right, create the
12351 necessary array types in backwards order. */
7ca2d3a3 12352
c906108c 12353 type = element_type;
7ca2d3a3
DL
12354
12355 if (read_array_order (die, cu) == DW_ORD_col_major)
12356 {
12357 int i = 0;
9a619af0 12358
7ca2d3a3
DL
12359 while (i < ndim)
12360 type = create_array_type (NULL, type, range_types[i++]);
12361 }
12362 else
12363 {
12364 while (ndim-- > 0)
12365 type = create_array_type (NULL, type, range_types[ndim]);
12366 }
c906108c 12367
f5f8a009
EZ
12368 /* Understand Dwarf2 support for vector types (like they occur on
12369 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12370 array type. This is not part of the Dwarf2/3 standard yet, but a
12371 custom vendor extension. The main difference between a regular
12372 array and the vector variant is that vectors are passed by value
12373 to functions. */
e142c38c 12374 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12375 if (attr)
ea37ba09 12376 make_vector_type (type);
f5f8a009 12377
dbc98a8b
KW
12378 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12379 implementation may choose to implement triple vectors using this
12380 attribute. */
12381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12382 if (attr)
12383 {
12384 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12385 TYPE_LENGTH (type) = DW_UNSND (attr);
12386 else
3e43a32a
MS
12387 complaint (&symfile_complaints,
12388 _("DW_AT_byte_size for array type smaller "
12389 "than the total size of elements"));
dbc98a8b
KW
12390 }
12391
39cbfefa
DJ
12392 name = dwarf2_name (die, cu);
12393 if (name)
12394 TYPE_NAME (type) = name;
6e70227d 12395
0963b4bd 12396 /* Install the type in the die. */
7e314c57
JK
12397 set_die_type (die, type, cu);
12398
12399 /* set_die_type should be already done. */
b4ba55a1
JB
12400 set_descriptive_type (type, die, cu);
12401
c906108c
SS
12402 do_cleanups (back_to);
12403
7e314c57 12404 return type;
c906108c
SS
12405}
12406
7ca2d3a3 12407static enum dwarf_array_dim_ordering
6e70227d 12408read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12409{
12410 struct attribute *attr;
12411
12412 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12413
12414 if (attr) return DW_SND (attr);
12415
0963b4bd
MS
12416 /* GNU F77 is a special case, as at 08/2004 array type info is the
12417 opposite order to the dwarf2 specification, but data is still
12418 laid out as per normal fortran.
7ca2d3a3 12419
0963b4bd
MS
12420 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12421 version checking. */
7ca2d3a3 12422
905e0470
PM
12423 if (cu->language == language_fortran
12424 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12425 {
12426 return DW_ORD_row_major;
12427 }
12428
6e70227d 12429 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12430 {
12431 case array_column_major:
12432 return DW_ORD_col_major;
12433 case array_row_major:
12434 default:
12435 return DW_ORD_row_major;
12436 };
12437}
12438
72019c9c 12439/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12440 the DIE's type field. */
72019c9c 12441
f792889a 12442static struct type *
72019c9c
GM
12443read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12444{
7e314c57
JK
12445 struct type *domain_type, *set_type;
12446 struct attribute *attr;
f792889a 12447
7e314c57
JK
12448 domain_type = die_type (die, cu);
12449
12450 /* The die_type call above may have already set the type for this DIE. */
12451 set_type = get_die_type (die, cu);
12452 if (set_type)
12453 return set_type;
12454
12455 set_type = create_set_type (NULL, domain_type);
12456
12457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12458 if (attr)
12459 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12460
f792889a 12461 return set_die_type (die, set_type, cu);
72019c9c 12462}
7ca2d3a3 12463
0971de02
TT
12464/* A helper for read_common_block that creates a locexpr baton.
12465 SYM is the symbol which we are marking as computed.
12466 COMMON_DIE is the DIE for the common block.
12467 COMMON_LOC is the location expression attribute for the common
12468 block itself.
12469 MEMBER_LOC is the location expression attribute for the particular
12470 member of the common block that we are processing.
12471 CU is the CU from which the above come. */
12472
12473static void
12474mark_common_block_symbol_computed (struct symbol *sym,
12475 struct die_info *common_die,
12476 struct attribute *common_loc,
12477 struct attribute *member_loc,
12478 struct dwarf2_cu *cu)
12479{
12480 struct objfile *objfile = dwarf2_per_objfile->objfile;
12481 struct dwarf2_locexpr_baton *baton;
12482 gdb_byte *ptr;
12483 unsigned int cu_off;
12484 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12485 LONGEST offset = 0;
12486
12487 gdb_assert (common_loc && member_loc);
12488 gdb_assert (attr_form_is_block (common_loc));
12489 gdb_assert (attr_form_is_block (member_loc)
12490 || attr_form_is_constant (member_loc));
12491
12492 baton = obstack_alloc (&objfile->objfile_obstack,
12493 sizeof (struct dwarf2_locexpr_baton));
12494 baton->per_cu = cu->per_cu;
12495 gdb_assert (baton->per_cu);
12496
12497 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12498
12499 if (attr_form_is_constant (member_loc))
12500 {
12501 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12502 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12503 }
12504 else
12505 baton->size += DW_BLOCK (member_loc)->size;
12506
12507 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12508 baton->data = ptr;
12509
12510 *ptr++ = DW_OP_call4;
12511 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12512 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12513 ptr += 4;
12514
12515 if (attr_form_is_constant (member_loc))
12516 {
12517 *ptr++ = DW_OP_addr;
12518 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12519 ptr += cu->header.addr_size;
12520 }
12521 else
12522 {
12523 /* We have to copy the data here, because DW_OP_call4 will only
12524 use a DW_AT_location attribute. */
12525 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12526 ptr += DW_BLOCK (member_loc)->size;
12527 }
12528
12529 *ptr++ = DW_OP_plus;
12530 gdb_assert (ptr - baton->data == baton->size);
12531
0971de02 12532 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12533 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12534}
12535
4357ac6c
TT
12536/* Create appropriate locally-scoped variables for all the
12537 DW_TAG_common_block entries. Also create a struct common_block
12538 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12539 is used to sepate the common blocks name namespace from regular
12540 variable names. */
c906108c
SS
12541
12542static void
e7c27a73 12543read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12544{
0971de02
TT
12545 struct attribute *attr;
12546
12547 attr = dwarf2_attr (die, DW_AT_location, cu);
12548 if (attr)
12549 {
12550 /* Support the .debug_loc offsets. */
12551 if (attr_form_is_block (attr))
12552 {
12553 /* Ok. */
12554 }
12555 else if (attr_form_is_section_offset (attr))
12556 {
12557 dwarf2_complex_location_expr_complaint ();
12558 attr = NULL;
12559 }
12560 else
12561 {
12562 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12563 "common block member");
12564 attr = NULL;
12565 }
12566 }
12567
639d11d3 12568 if (die->child != NULL)
c906108c 12569 {
4357ac6c
TT
12570 struct objfile *objfile = cu->objfile;
12571 struct die_info *child_die;
12572 size_t n_entries = 0, size;
12573 struct common_block *common_block;
12574 struct symbol *sym;
74ac6d43 12575
4357ac6c
TT
12576 for (child_die = die->child;
12577 child_die && child_die->tag;
12578 child_die = sibling_die (child_die))
12579 ++n_entries;
12580
12581 size = (sizeof (struct common_block)
12582 + (n_entries - 1) * sizeof (struct symbol *));
12583 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12584 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12585 common_block->n_entries = 0;
12586
12587 for (child_die = die->child;
12588 child_die && child_die->tag;
12589 child_die = sibling_die (child_die))
12590 {
12591 /* Create the symbol in the DW_TAG_common_block block in the current
12592 symbol scope. */
e7c27a73 12593 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12594 if (sym != NULL)
12595 {
12596 struct attribute *member_loc;
12597
12598 common_block->contents[common_block->n_entries++] = sym;
12599
12600 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12601 cu);
12602 if (member_loc)
12603 {
12604 /* GDB has handled this for a long time, but it is
12605 not specified by DWARF. It seems to have been
12606 emitted by gfortran at least as recently as:
12607 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12608 complaint (&symfile_complaints,
12609 _("Variable in common block has "
12610 "DW_AT_data_member_location "
12611 "- DIE at 0x%x [in module %s]"),
12612 child_die->offset.sect_off, cu->objfile->name);
12613
12614 if (attr_form_is_section_offset (member_loc))
12615 dwarf2_complex_location_expr_complaint ();
12616 else if (attr_form_is_constant (member_loc)
12617 || attr_form_is_block (member_loc))
12618 {
12619 if (attr)
12620 mark_common_block_symbol_computed (sym, die, attr,
12621 member_loc, cu);
12622 }
12623 else
12624 dwarf2_complex_location_expr_complaint ();
12625 }
12626 }
c906108c 12627 }
4357ac6c
TT
12628
12629 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12630 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12631 }
12632}
12633
0114d602 12634/* Create a type for a C++ namespace. */
d9fa45fe 12635
0114d602
DJ
12636static struct type *
12637read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12638{
e7c27a73 12639 struct objfile *objfile = cu->objfile;
0114d602 12640 const char *previous_prefix, *name;
9219021c 12641 int is_anonymous;
0114d602
DJ
12642 struct type *type;
12643
12644 /* For extensions, reuse the type of the original namespace. */
12645 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12646 {
12647 struct die_info *ext_die;
12648 struct dwarf2_cu *ext_cu = cu;
9a619af0 12649
0114d602
DJ
12650 ext_die = dwarf2_extension (die, &ext_cu);
12651 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12652
12653 /* EXT_CU may not be the same as CU.
02142a6c 12654 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12655 return set_die_type (die, type, cu);
12656 }
9219021c 12657
e142c38c 12658 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12659
12660 /* Now build the name of the current namespace. */
12661
0114d602
DJ
12662 previous_prefix = determine_prefix (die, cu);
12663 if (previous_prefix[0] != '\0')
12664 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12665 previous_prefix, name, 0, cu);
0114d602
DJ
12666
12667 /* Create the type. */
12668 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12669 objfile);
abee88f2 12670 TYPE_NAME (type) = name;
0114d602
DJ
12671 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12672
60531b24 12673 return set_die_type (die, type, cu);
0114d602
DJ
12674}
12675
12676/* Read a C++ namespace. */
12677
12678static void
12679read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12680{
12681 struct objfile *objfile = cu->objfile;
0114d602 12682 int is_anonymous;
9219021c 12683
5c4e30ca
DC
12684 /* Add a symbol associated to this if we haven't seen the namespace
12685 before. Also, add a using directive if it's an anonymous
12686 namespace. */
9219021c 12687
f2f0e013 12688 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12689 {
12690 struct type *type;
12691
0114d602 12692 type = read_type_die (die, cu);
e7c27a73 12693 new_symbol (die, type, cu);
5c4e30ca 12694
e8e80198 12695 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12696 if (is_anonymous)
0114d602
DJ
12697 {
12698 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12699
c0cc3a76 12700 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12701 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12702 }
5c4e30ca 12703 }
9219021c 12704
639d11d3 12705 if (die->child != NULL)
d9fa45fe 12706 {
639d11d3 12707 struct die_info *child_die = die->child;
6e70227d 12708
d9fa45fe
DC
12709 while (child_die && child_die->tag)
12710 {
e7c27a73 12711 process_die (child_die, cu);
d9fa45fe
DC
12712 child_die = sibling_die (child_die);
12713 }
12714 }
38d518c9
EZ
12715}
12716
f55ee35c
JK
12717/* Read a Fortran module as type. This DIE can be only a declaration used for
12718 imported module. Still we need that type as local Fortran "use ... only"
12719 declaration imports depend on the created type in determine_prefix. */
12720
12721static struct type *
12722read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12723{
12724 struct objfile *objfile = cu->objfile;
15d034d0 12725 const char *module_name;
f55ee35c
JK
12726 struct type *type;
12727
12728 module_name = dwarf2_name (die, cu);
12729 if (!module_name)
3e43a32a
MS
12730 complaint (&symfile_complaints,
12731 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12732 die->offset.sect_off);
f55ee35c
JK
12733 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12734
12735 /* determine_prefix uses TYPE_TAG_NAME. */
12736 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12737
12738 return set_die_type (die, type, cu);
12739}
12740
5d7cb8df
JK
12741/* Read a Fortran module. */
12742
12743static void
12744read_module (struct die_info *die, struct dwarf2_cu *cu)
12745{
12746 struct die_info *child_die = die->child;
12747
5d7cb8df
JK
12748 while (child_die && child_die->tag)
12749 {
12750 process_die (child_die, cu);
12751 child_die = sibling_die (child_die);
12752 }
12753}
12754
38d518c9
EZ
12755/* Return the name of the namespace represented by DIE. Set
12756 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12757 namespace. */
12758
12759static const char *
e142c38c 12760namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12761{
12762 struct die_info *current_die;
12763 const char *name = NULL;
12764
12765 /* Loop through the extensions until we find a name. */
12766
12767 for (current_die = die;
12768 current_die != NULL;
f2f0e013 12769 current_die = dwarf2_extension (die, &cu))
38d518c9 12770 {
e142c38c 12771 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12772 if (name != NULL)
12773 break;
12774 }
12775
12776 /* Is it an anonymous namespace? */
12777
12778 *is_anonymous = (name == NULL);
12779 if (*is_anonymous)
2b1dbab0 12780 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12781
12782 return name;
d9fa45fe
DC
12783}
12784
c906108c
SS
12785/* Extract all information from a DW_TAG_pointer_type DIE and add to
12786 the user defined type vector. */
12787
f792889a 12788static struct type *
e7c27a73 12789read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12790{
5e2b427d 12791 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12792 struct comp_unit_head *cu_header = &cu->header;
c906108c 12793 struct type *type;
8b2dbe47
KB
12794 struct attribute *attr_byte_size;
12795 struct attribute *attr_address_class;
12796 int byte_size, addr_class;
7e314c57
JK
12797 struct type *target_type;
12798
12799 target_type = die_type (die, cu);
c906108c 12800
7e314c57
JK
12801 /* The die_type call above may have already set the type for this DIE. */
12802 type = get_die_type (die, cu);
12803 if (type)
12804 return type;
12805
12806 type = lookup_pointer_type (target_type);
8b2dbe47 12807
e142c38c 12808 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12809 if (attr_byte_size)
12810 byte_size = DW_UNSND (attr_byte_size);
c906108c 12811 else
8b2dbe47
KB
12812 byte_size = cu_header->addr_size;
12813
e142c38c 12814 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12815 if (attr_address_class)
12816 addr_class = DW_UNSND (attr_address_class);
12817 else
12818 addr_class = DW_ADDR_none;
12819
12820 /* If the pointer size or address class is different than the
12821 default, create a type variant marked as such and set the
12822 length accordingly. */
12823 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12824 {
5e2b427d 12825 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12826 {
12827 int type_flags;
12828
849957d9 12829 type_flags = gdbarch_address_class_type_flags
5e2b427d 12830 (gdbarch, byte_size, addr_class);
876cecd0
TT
12831 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12832 == 0);
8b2dbe47
KB
12833 type = make_type_with_address_space (type, type_flags);
12834 }
12835 else if (TYPE_LENGTH (type) != byte_size)
12836 {
3e43a32a
MS
12837 complaint (&symfile_complaints,
12838 _("invalid pointer size %d"), byte_size);
8b2dbe47 12839 }
6e70227d 12840 else
9a619af0
MS
12841 {
12842 /* Should we also complain about unhandled address classes? */
12843 }
c906108c 12844 }
8b2dbe47
KB
12845
12846 TYPE_LENGTH (type) = byte_size;
f792889a 12847 return set_die_type (die, type, cu);
c906108c
SS
12848}
12849
12850/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12851 the user defined type vector. */
12852
f792889a 12853static struct type *
e7c27a73 12854read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12855{
12856 struct type *type;
12857 struct type *to_type;
12858 struct type *domain;
12859
e7c27a73
DJ
12860 to_type = die_type (die, cu);
12861 domain = die_containing_type (die, cu);
0d5de010 12862
7e314c57
JK
12863 /* The calls above may have already set the type for this DIE. */
12864 type = get_die_type (die, cu);
12865 if (type)
12866 return type;
12867
0d5de010
DJ
12868 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12869 type = lookup_methodptr_type (to_type);
7078baeb
TT
12870 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12871 {
12872 struct type *new_type = alloc_type (cu->objfile);
12873
12874 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12875 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12876 TYPE_VARARGS (to_type));
12877 type = lookup_methodptr_type (new_type);
12878 }
0d5de010
DJ
12879 else
12880 type = lookup_memberptr_type (to_type, domain);
c906108c 12881
f792889a 12882 return set_die_type (die, type, cu);
c906108c
SS
12883}
12884
12885/* Extract all information from a DW_TAG_reference_type DIE and add to
12886 the user defined type vector. */
12887
f792889a 12888static struct type *
e7c27a73 12889read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12890{
e7c27a73 12891 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12892 struct type *type, *target_type;
c906108c
SS
12893 struct attribute *attr;
12894
7e314c57
JK
12895 target_type = die_type (die, cu);
12896
12897 /* The die_type call above may have already set the type for this DIE. */
12898 type = get_die_type (die, cu);
12899 if (type)
12900 return type;
12901
12902 type = lookup_reference_type (target_type);
e142c38c 12903 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12904 if (attr)
12905 {
12906 TYPE_LENGTH (type) = DW_UNSND (attr);
12907 }
12908 else
12909 {
107d2387 12910 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12911 }
f792889a 12912 return set_die_type (die, type, cu);
c906108c
SS
12913}
12914
f792889a 12915static struct type *
e7c27a73 12916read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12917{
f792889a 12918 struct type *base_type, *cv_type;
c906108c 12919
e7c27a73 12920 base_type = die_type (die, cu);
7e314c57
JK
12921
12922 /* The die_type call above may have already set the type for this DIE. */
12923 cv_type = get_die_type (die, cu);
12924 if (cv_type)
12925 return cv_type;
12926
2f608a3a
KW
12927 /* In case the const qualifier is applied to an array type, the element type
12928 is so qualified, not the array type (section 6.7.3 of C99). */
12929 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12930 {
12931 struct type *el_type, *inner_array;
12932
12933 base_type = copy_type (base_type);
12934 inner_array = base_type;
12935
12936 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12937 {
12938 TYPE_TARGET_TYPE (inner_array) =
12939 copy_type (TYPE_TARGET_TYPE (inner_array));
12940 inner_array = TYPE_TARGET_TYPE (inner_array);
12941 }
12942
12943 el_type = TYPE_TARGET_TYPE (inner_array);
12944 TYPE_TARGET_TYPE (inner_array) =
12945 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12946
12947 return set_die_type (die, base_type, cu);
12948 }
12949
f792889a
DJ
12950 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12951 return set_die_type (die, cv_type, cu);
c906108c
SS
12952}
12953
f792889a 12954static struct type *
e7c27a73 12955read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12956{
f792889a 12957 struct type *base_type, *cv_type;
c906108c 12958
e7c27a73 12959 base_type = die_type (die, cu);
7e314c57
JK
12960
12961 /* The die_type call above may have already set the type for this DIE. */
12962 cv_type = get_die_type (die, cu);
12963 if (cv_type)
12964 return cv_type;
12965
f792889a
DJ
12966 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12967 return set_die_type (die, cv_type, cu);
c906108c
SS
12968}
12969
06d66ee9
TT
12970/* Handle DW_TAG_restrict_type. */
12971
12972static struct type *
12973read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12974{
12975 struct type *base_type, *cv_type;
12976
12977 base_type = die_type (die, cu);
12978
12979 /* The die_type call above may have already set the type for this DIE. */
12980 cv_type = get_die_type (die, cu);
12981 if (cv_type)
12982 return cv_type;
12983
12984 cv_type = make_restrict_type (base_type);
12985 return set_die_type (die, cv_type, cu);
12986}
12987
c906108c
SS
12988/* Extract all information from a DW_TAG_string_type DIE and add to
12989 the user defined type vector. It isn't really a user defined type,
12990 but it behaves like one, with other DIE's using an AT_user_def_type
12991 attribute to reference it. */
12992
f792889a 12993static struct type *
e7c27a73 12994read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12995{
e7c27a73 12996 struct objfile *objfile = cu->objfile;
3b7538c0 12997 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12998 struct type *type, *range_type, *index_type, *char_type;
12999 struct attribute *attr;
13000 unsigned int length;
13001
e142c38c 13002 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
13003 if (attr)
13004 {
13005 length = DW_UNSND (attr);
13006 }
13007 else
13008 {
0963b4bd 13009 /* Check for the DW_AT_byte_size attribute. */
e142c38c 13010 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
13011 if (attr)
13012 {
13013 length = DW_UNSND (attr);
13014 }
13015 else
13016 {
13017 length = 1;
13018 }
c906108c 13019 }
6ccb9162 13020
46bf5051 13021 index_type = objfile_type (objfile)->builtin_int;
c906108c 13022 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
13023 char_type = language_string_char_type (cu->language_defn, gdbarch);
13024 type = create_string_type (NULL, char_type, range_type);
6ccb9162 13025
f792889a 13026 return set_die_type (die, type, cu);
c906108c
SS
13027}
13028
4d804846
JB
13029/* Assuming that DIE corresponds to a function, returns nonzero
13030 if the function is prototyped. */
13031
13032static int
13033prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13034{
13035 struct attribute *attr;
13036
13037 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13038 if (attr && (DW_UNSND (attr) != 0))
13039 return 1;
13040
13041 /* The DWARF standard implies that the DW_AT_prototyped attribute
13042 is only meaninful for C, but the concept also extends to other
13043 languages that allow unprototyped functions (Eg: Objective C).
13044 For all other languages, assume that functions are always
13045 prototyped. */
13046 if (cu->language != language_c
13047 && cu->language != language_objc
13048 && cu->language != language_opencl)
13049 return 1;
13050
13051 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13052 prototyped and unprototyped functions; default to prototyped,
13053 since that is more common in modern code (and RealView warns
13054 about unprototyped functions). */
13055 if (producer_is_realview (cu->producer))
13056 return 1;
13057
13058 return 0;
13059}
13060
c906108c
SS
13061/* Handle DIES due to C code like:
13062
13063 struct foo
c5aa993b
JM
13064 {
13065 int (*funcp)(int a, long l);
13066 int b;
13067 };
c906108c 13068
0963b4bd 13069 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13070
f792889a 13071static struct type *
e7c27a73 13072read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13073{
bb5ed363 13074 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13075 struct type *type; /* Type that this function returns. */
13076 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13077 struct attribute *attr;
13078
e7c27a73 13079 type = die_type (die, cu);
7e314c57
JK
13080
13081 /* The die_type call above may have already set the type for this DIE. */
13082 ftype = get_die_type (die, cu);
13083 if (ftype)
13084 return ftype;
13085
0c8b41f1 13086 ftype = lookup_function_type (type);
c906108c 13087
4d804846 13088 if (prototyped_function_p (die, cu))
a6c727b2 13089 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13090
c055b101
CV
13091 /* Store the calling convention in the type if it's available in
13092 the subroutine die. Otherwise set the calling convention to
13093 the default value DW_CC_normal. */
13094 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13095 if (attr)
13096 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13097 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13098 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13099 else
13100 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13101
13102 /* We need to add the subroutine type to the die immediately so
13103 we don't infinitely recurse when dealing with parameters
0963b4bd 13104 declared as the same subroutine type. */
76c10ea2 13105 set_die_type (die, ftype, cu);
6e70227d 13106
639d11d3 13107 if (die->child != NULL)
c906108c 13108 {
bb5ed363 13109 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13110 struct die_info *child_die;
8072405b 13111 int nparams, iparams;
c906108c
SS
13112
13113 /* Count the number of parameters.
13114 FIXME: GDB currently ignores vararg functions, but knows about
13115 vararg member functions. */
8072405b 13116 nparams = 0;
639d11d3 13117 child_die = die->child;
c906108c
SS
13118 while (child_die && child_die->tag)
13119 {
13120 if (child_die->tag == DW_TAG_formal_parameter)
13121 nparams++;
13122 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13123 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13124 child_die = sibling_die (child_die);
13125 }
13126
13127 /* Allocate storage for parameters and fill them in. */
13128 TYPE_NFIELDS (ftype) = nparams;
13129 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13130 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13131
8072405b
JK
13132 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13133 even if we error out during the parameters reading below. */
13134 for (iparams = 0; iparams < nparams; iparams++)
13135 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13136
13137 iparams = 0;
639d11d3 13138 child_die = die->child;
c906108c
SS
13139 while (child_die && child_die->tag)
13140 {
13141 if (child_die->tag == DW_TAG_formal_parameter)
13142 {
3ce3b1ba
PA
13143 struct type *arg_type;
13144
13145 /* DWARF version 2 has no clean way to discern C++
13146 static and non-static member functions. G++ helps
13147 GDB by marking the first parameter for non-static
13148 member functions (which is the this pointer) as
13149 artificial. We pass this information to
13150 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13151
13152 DWARF version 3 added DW_AT_object_pointer, which GCC
13153 4.5 does not yet generate. */
e142c38c 13154 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13155 if (attr)
13156 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13157 else
418835cc
KS
13158 {
13159 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13160
13161 /* GCC/43521: In java, the formal parameter
13162 "this" is sometimes not marked with DW_AT_artificial. */
13163 if (cu->language == language_java)
13164 {
13165 const char *name = dwarf2_name (child_die, cu);
9a619af0 13166
418835cc
KS
13167 if (name && !strcmp (name, "this"))
13168 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13169 }
13170 }
3ce3b1ba
PA
13171 arg_type = die_type (child_die, cu);
13172
13173 /* RealView does not mark THIS as const, which the testsuite
13174 expects. GCC marks THIS as const in method definitions,
13175 but not in the class specifications (GCC PR 43053). */
13176 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13177 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13178 {
13179 int is_this = 0;
13180 struct dwarf2_cu *arg_cu = cu;
13181 const char *name = dwarf2_name (child_die, cu);
13182
13183 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13184 if (attr)
13185 {
13186 /* If the compiler emits this, use it. */
13187 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13188 is_this = 1;
13189 }
13190 else if (name && strcmp (name, "this") == 0)
13191 /* Function definitions will have the argument names. */
13192 is_this = 1;
13193 else if (name == NULL && iparams == 0)
13194 /* Declarations may not have the names, so like
13195 elsewhere in GDB, assume an artificial first
13196 argument is "this". */
13197 is_this = 1;
13198
13199 if (is_this)
13200 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13201 arg_type, 0);
13202 }
13203
13204 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13205 iparams++;
13206 }
13207 child_die = sibling_die (child_die);
13208 }
13209 }
13210
76c10ea2 13211 return ftype;
c906108c
SS
13212}
13213
f792889a 13214static struct type *
e7c27a73 13215read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13216{
e7c27a73 13217 struct objfile *objfile = cu->objfile;
0114d602 13218 const char *name = NULL;
3c8e0968 13219 struct type *this_type, *target_type;
c906108c 13220
94af9270 13221 name = dwarf2_full_name (NULL, die, cu);
f792889a 13222 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13223 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13224 TYPE_NAME (this_type) = name;
f792889a 13225 set_die_type (die, this_type, cu);
3c8e0968
DE
13226 target_type = die_type (die, cu);
13227 if (target_type != this_type)
13228 TYPE_TARGET_TYPE (this_type) = target_type;
13229 else
13230 {
13231 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13232 spec and cause infinite loops in GDB. */
13233 complaint (&symfile_complaints,
13234 _("Self-referential DW_TAG_typedef "
13235 "- DIE at 0x%x [in module %s]"),
b64f50a1 13236 die->offset.sect_off, objfile->name);
3c8e0968
DE
13237 TYPE_TARGET_TYPE (this_type) = NULL;
13238 }
f792889a 13239 return this_type;
c906108c
SS
13240}
13241
13242/* Find a representation of a given base type and install
13243 it in the TYPE field of the die. */
13244
f792889a 13245static struct type *
e7c27a73 13246read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13247{
e7c27a73 13248 struct objfile *objfile = cu->objfile;
c906108c
SS
13249 struct type *type;
13250 struct attribute *attr;
13251 int encoding = 0, size = 0;
15d034d0 13252 const char *name;
6ccb9162
UW
13253 enum type_code code = TYPE_CODE_INT;
13254 int type_flags = 0;
13255 struct type *target_type = NULL;
c906108c 13256
e142c38c 13257 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13258 if (attr)
13259 {
13260 encoding = DW_UNSND (attr);
13261 }
e142c38c 13262 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13263 if (attr)
13264 {
13265 size = DW_UNSND (attr);
13266 }
39cbfefa 13267 name = dwarf2_name (die, cu);
6ccb9162 13268 if (!name)
c906108c 13269 {
6ccb9162
UW
13270 complaint (&symfile_complaints,
13271 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13272 }
6ccb9162
UW
13273
13274 switch (encoding)
c906108c 13275 {
6ccb9162
UW
13276 case DW_ATE_address:
13277 /* Turn DW_ATE_address into a void * pointer. */
13278 code = TYPE_CODE_PTR;
13279 type_flags |= TYPE_FLAG_UNSIGNED;
13280 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13281 break;
13282 case DW_ATE_boolean:
13283 code = TYPE_CODE_BOOL;
13284 type_flags |= TYPE_FLAG_UNSIGNED;
13285 break;
13286 case DW_ATE_complex_float:
13287 code = TYPE_CODE_COMPLEX;
13288 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13289 break;
13290 case DW_ATE_decimal_float:
13291 code = TYPE_CODE_DECFLOAT;
13292 break;
13293 case DW_ATE_float:
13294 code = TYPE_CODE_FLT;
13295 break;
13296 case DW_ATE_signed:
13297 break;
13298 case DW_ATE_unsigned:
13299 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13300 if (cu->language == language_fortran
13301 && name
13302 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13303 code = TYPE_CODE_CHAR;
6ccb9162
UW
13304 break;
13305 case DW_ATE_signed_char:
6e70227d 13306 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13307 || cu->language == language_pascal
13308 || cu->language == language_fortran)
6ccb9162
UW
13309 code = TYPE_CODE_CHAR;
13310 break;
13311 case DW_ATE_unsigned_char:
868a0084 13312 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13313 || cu->language == language_pascal
13314 || cu->language == language_fortran)
6ccb9162
UW
13315 code = TYPE_CODE_CHAR;
13316 type_flags |= TYPE_FLAG_UNSIGNED;
13317 break;
75079b2b
TT
13318 case DW_ATE_UTF:
13319 /* We just treat this as an integer and then recognize the
13320 type by name elsewhere. */
13321 break;
13322
6ccb9162
UW
13323 default:
13324 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13325 dwarf_type_encoding_name (encoding));
13326 break;
c906108c 13327 }
6ccb9162 13328
0114d602
DJ
13329 type = init_type (code, size, type_flags, NULL, objfile);
13330 TYPE_NAME (type) = name;
6ccb9162
UW
13331 TYPE_TARGET_TYPE (type) = target_type;
13332
0114d602 13333 if (name && strcmp (name, "char") == 0)
876cecd0 13334 TYPE_NOSIGN (type) = 1;
0114d602 13335
f792889a 13336 return set_die_type (die, type, cu);
c906108c
SS
13337}
13338
a02abb62
JB
13339/* Read the given DW_AT_subrange DIE. */
13340
f792889a 13341static struct type *
a02abb62
JB
13342read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13343{
4c9ad8c2 13344 struct type *base_type, *orig_base_type;
a02abb62
JB
13345 struct type *range_type;
13346 struct attribute *attr;
4fae6e18
JK
13347 LONGEST low, high;
13348 int low_default_is_valid;
15d034d0 13349 const char *name;
43bbcdc2 13350 LONGEST negative_mask;
e77813c8 13351
4c9ad8c2
TT
13352 orig_base_type = die_type (die, cu);
13353 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13354 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13355 creating the range type, but we use the result of check_typedef
13356 when examining properties of the type. */
13357 base_type = check_typedef (orig_base_type);
a02abb62 13358
7e314c57
JK
13359 /* The die_type call above may have already set the type for this DIE. */
13360 range_type = get_die_type (die, cu);
13361 if (range_type)
13362 return range_type;
13363
4fae6e18
JK
13364 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13365 omitting DW_AT_lower_bound. */
13366 switch (cu->language)
6e70227d 13367 {
4fae6e18
JK
13368 case language_c:
13369 case language_cplus:
13370 low = 0;
13371 low_default_is_valid = 1;
13372 break;
13373 case language_fortran:
13374 low = 1;
13375 low_default_is_valid = 1;
13376 break;
13377 case language_d:
13378 case language_java:
13379 case language_objc:
13380 low = 0;
13381 low_default_is_valid = (cu->header.version >= 4);
13382 break;
13383 case language_ada:
13384 case language_m2:
13385 case language_pascal:
a02abb62 13386 low = 1;
4fae6e18
JK
13387 low_default_is_valid = (cu->header.version >= 4);
13388 break;
13389 default:
13390 low = 0;
13391 low_default_is_valid = 0;
13392 break;
a02abb62
JB
13393 }
13394
dd5e6932
DJ
13395 /* FIXME: For variable sized arrays either of these could be
13396 a variable rather than a constant value. We'll allow it,
13397 but we don't know how to handle it. */
e142c38c 13398 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13399 if (attr)
4fae6e18
JK
13400 low = dwarf2_get_attr_constant_value (attr, low);
13401 else if (!low_default_is_valid)
13402 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13403 "- DIE at 0x%x [in module %s]"),
13404 die->offset.sect_off, cu->objfile->name);
a02abb62 13405
e142c38c 13406 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13407 if (attr)
6e70227d 13408 {
7771576e 13409 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13410 {
13411 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13412 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13413 FIXME: GDB does not yet know how to handle dynamic
13414 arrays properly, treat them as arrays with unspecified
13415 length for now.
13416
13417 FIXME: jimb/2003-09-22: GDB does not really know
13418 how to handle arrays of unspecified length
13419 either; we just represent them as zero-length
13420 arrays. Choose an appropriate upper bound given
13421 the lower bound we've computed above. */
13422 high = low - 1;
13423 }
13424 else
13425 high = dwarf2_get_attr_constant_value (attr, 1);
13426 }
e77813c8
PM
13427 else
13428 {
13429 attr = dwarf2_attr (die, DW_AT_count, cu);
13430 if (attr)
13431 {
13432 int count = dwarf2_get_attr_constant_value (attr, 1);
13433 high = low + count - 1;
13434 }
c2ff108b
JK
13435 else
13436 {
13437 /* Unspecified array length. */
13438 high = low - 1;
13439 }
e77813c8
PM
13440 }
13441
13442 /* Dwarf-2 specifications explicitly allows to create subrange types
13443 without specifying a base type.
13444 In that case, the base type must be set to the type of
13445 the lower bound, upper bound or count, in that order, if any of these
13446 three attributes references an object that has a type.
13447 If no base type is found, the Dwarf-2 specifications say that
13448 a signed integer type of size equal to the size of an address should
13449 be used.
13450 For the following C code: `extern char gdb_int [];'
13451 GCC produces an empty range DIE.
13452 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13453 high bound or count are not yet handled by this code. */
e77813c8
PM
13454 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13455 {
13456 struct objfile *objfile = cu->objfile;
13457 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13458 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13459 struct type *int_type = objfile_type (objfile)->builtin_int;
13460
13461 /* Test "int", "long int", and "long long int" objfile types,
13462 and select the first one having a size above or equal to the
13463 architecture address size. */
13464 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13465 base_type = int_type;
13466 else
13467 {
13468 int_type = objfile_type (objfile)->builtin_long;
13469 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13470 base_type = int_type;
13471 else
13472 {
13473 int_type = objfile_type (objfile)->builtin_long_long;
13474 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13475 base_type = int_type;
13476 }
13477 }
13478 }
a02abb62 13479
6e70227d 13480 negative_mask =
43bbcdc2
PH
13481 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13482 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13483 low |= negative_mask;
13484 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13485 high |= negative_mask;
13486
4c9ad8c2 13487 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13488
bbb0eef6
JK
13489 /* Mark arrays with dynamic length at least as an array of unspecified
13490 length. GDB could check the boundary but before it gets implemented at
13491 least allow accessing the array elements. */
d48323d8 13492 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13493 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13494
c2ff108b
JK
13495 /* Ada expects an empty array on no boundary attributes. */
13496 if (attr == NULL && cu->language != language_ada)
13497 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13498
39cbfefa
DJ
13499 name = dwarf2_name (die, cu);
13500 if (name)
13501 TYPE_NAME (range_type) = name;
6e70227d 13502
e142c38c 13503 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13504 if (attr)
13505 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13506
7e314c57
JK
13507 set_die_type (die, range_type, cu);
13508
13509 /* set_die_type should be already done. */
b4ba55a1
JB
13510 set_descriptive_type (range_type, die, cu);
13511
7e314c57 13512 return range_type;
a02abb62 13513}
6e70227d 13514
f792889a 13515static struct type *
81a17f79
JB
13516read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13517{
13518 struct type *type;
81a17f79 13519
81a17f79
JB
13520 /* For now, we only support the C meaning of an unspecified type: void. */
13521
0114d602
DJ
13522 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13523 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13524
f792889a 13525 return set_die_type (die, type, cu);
81a17f79 13526}
a02abb62 13527
639d11d3
DC
13528/* Read a single die and all its descendents. Set the die's sibling
13529 field to NULL; set other fields in the die correctly, and set all
13530 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13531 location of the info_ptr after reading all of those dies. PARENT
13532 is the parent of the die in question. */
13533
13534static struct die_info *
dee91e82 13535read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13536 const gdb_byte *info_ptr,
13537 const gdb_byte **new_info_ptr,
dee91e82 13538 struct die_info *parent)
639d11d3
DC
13539{
13540 struct die_info *die;
d521ce57 13541 const gdb_byte *cur_ptr;
639d11d3
DC
13542 int has_children;
13543
bf6af496 13544 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13545 if (die == NULL)
13546 {
13547 *new_info_ptr = cur_ptr;
13548 return NULL;
13549 }
93311388 13550 store_in_ref_table (die, reader->cu);
639d11d3
DC
13551
13552 if (has_children)
bf6af496 13553 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13554 else
13555 {
13556 die->child = NULL;
13557 *new_info_ptr = cur_ptr;
13558 }
13559
13560 die->sibling = NULL;
13561 die->parent = parent;
13562 return die;
13563}
13564
13565/* Read a die, all of its descendents, and all of its siblings; set
13566 all of the fields of all of the dies correctly. Arguments are as
13567 in read_die_and_children. */
13568
13569static struct die_info *
bf6af496 13570read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13571 const gdb_byte *info_ptr,
13572 const gdb_byte **new_info_ptr,
bf6af496 13573 struct die_info *parent)
639d11d3
DC
13574{
13575 struct die_info *first_die, *last_sibling;
d521ce57 13576 const gdb_byte *cur_ptr;
639d11d3 13577
c906108c 13578 cur_ptr = info_ptr;
639d11d3
DC
13579 first_die = last_sibling = NULL;
13580
13581 while (1)
c906108c 13582 {
639d11d3 13583 struct die_info *die
dee91e82 13584 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13585
1d325ec1 13586 if (die == NULL)
c906108c 13587 {
639d11d3
DC
13588 *new_info_ptr = cur_ptr;
13589 return first_die;
c906108c 13590 }
1d325ec1
DJ
13591
13592 if (!first_die)
13593 first_die = die;
c906108c 13594 else
1d325ec1
DJ
13595 last_sibling->sibling = die;
13596
13597 last_sibling = die;
c906108c 13598 }
c906108c
SS
13599}
13600
bf6af496
DE
13601/* Read a die, all of its descendents, and all of its siblings; set
13602 all of the fields of all of the dies correctly. Arguments are as
13603 in read_die_and_children.
13604 This the main entry point for reading a DIE and all its children. */
13605
13606static struct die_info *
13607read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13608 const gdb_byte *info_ptr,
13609 const gdb_byte **new_info_ptr,
bf6af496
DE
13610 struct die_info *parent)
13611{
13612 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13613 new_info_ptr, parent);
13614
13615 if (dwarf2_die_debug)
13616 {
13617 fprintf_unfiltered (gdb_stdlog,
13618 "Read die from %s@0x%x of %s:\n",
13619 bfd_section_name (reader->abfd,
13620 reader->die_section->asection),
13621 (unsigned) (info_ptr - reader->die_section->buffer),
13622 bfd_get_filename (reader->abfd));
13623 dump_die (die, dwarf2_die_debug);
13624 }
13625
13626 return die;
13627}
13628
3019eac3
DE
13629/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13630 attributes.
13631 The caller is responsible for filling in the extra attributes
13632 and updating (*DIEP)->num_attrs.
13633 Set DIEP to point to a newly allocated die with its information,
13634 except for its child, sibling, and parent fields.
13635 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 13636
d521ce57 13637static const gdb_byte *
3019eac3 13638read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13639 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13640 int *has_children, int num_extra_attrs)
93311388 13641{
b64f50a1
JK
13642 unsigned int abbrev_number, bytes_read, i;
13643 sect_offset offset;
93311388
DE
13644 struct abbrev_info *abbrev;
13645 struct die_info *die;
13646 struct dwarf2_cu *cu = reader->cu;
13647 bfd *abfd = reader->abfd;
13648
b64f50a1 13649 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13650 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13651 info_ptr += bytes_read;
13652 if (!abbrev_number)
13653 {
13654 *diep = NULL;
13655 *has_children = 0;
13656 return info_ptr;
13657 }
13658
433df2d4 13659 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13660 if (!abbrev)
348e048f
DE
13661 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13662 abbrev_number,
13663 bfd_get_filename (abfd));
13664
3019eac3 13665 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13666 die->offset = offset;
13667 die->tag = abbrev->tag;
13668 die->abbrev = abbrev_number;
13669
3019eac3
DE
13670 /* Make the result usable.
13671 The caller needs to update num_attrs after adding the extra
13672 attributes. */
93311388
DE
13673 die->num_attrs = abbrev->num_attrs;
13674
13675 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13676 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13677 info_ptr);
93311388
DE
13678
13679 *diep = die;
13680 *has_children = abbrev->has_children;
13681 return info_ptr;
13682}
13683
3019eac3
DE
13684/* Read a die and all its attributes.
13685 Set DIEP to point to a newly allocated die with its information,
13686 except for its child, sibling, and parent fields.
13687 Set HAS_CHILDREN to tell whether the die has children or not. */
13688
d521ce57 13689static const gdb_byte *
3019eac3 13690read_full_die (const struct die_reader_specs *reader,
d521ce57 13691 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13692 int *has_children)
13693{
d521ce57 13694 const gdb_byte *result;
bf6af496
DE
13695
13696 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13697
13698 if (dwarf2_die_debug)
13699 {
13700 fprintf_unfiltered (gdb_stdlog,
13701 "Read die from %s@0x%x of %s:\n",
13702 bfd_section_name (reader->abfd,
13703 reader->die_section->asection),
13704 (unsigned) (info_ptr - reader->die_section->buffer),
13705 bfd_get_filename (reader->abfd));
13706 dump_die (*diep, dwarf2_die_debug);
13707 }
13708
13709 return result;
3019eac3 13710}
433df2d4
DE
13711\f
13712/* Abbreviation tables.
3019eac3 13713
433df2d4 13714 In DWARF version 2, the description of the debugging information is
c906108c
SS
13715 stored in a separate .debug_abbrev section. Before we read any
13716 dies from a section we read in all abbreviations and install them
433df2d4
DE
13717 in a hash table. */
13718
13719/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13720
13721static struct abbrev_info *
13722abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13723{
13724 struct abbrev_info *abbrev;
13725
13726 abbrev = (struct abbrev_info *)
13727 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13728 memset (abbrev, 0, sizeof (struct abbrev_info));
13729 return abbrev;
13730}
13731
13732/* Add an abbreviation to the table. */
c906108c
SS
13733
13734static void
433df2d4
DE
13735abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13736 unsigned int abbrev_number,
13737 struct abbrev_info *abbrev)
13738{
13739 unsigned int hash_number;
13740
13741 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13742 abbrev->next = abbrev_table->abbrevs[hash_number];
13743 abbrev_table->abbrevs[hash_number] = abbrev;
13744}
dee91e82 13745
433df2d4
DE
13746/* Look up an abbrev in the table.
13747 Returns NULL if the abbrev is not found. */
13748
13749static struct abbrev_info *
13750abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13751 unsigned int abbrev_number)
c906108c 13752{
433df2d4
DE
13753 unsigned int hash_number;
13754 struct abbrev_info *abbrev;
13755
13756 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13757 abbrev = abbrev_table->abbrevs[hash_number];
13758
13759 while (abbrev)
13760 {
13761 if (abbrev->number == abbrev_number)
13762 return abbrev;
13763 abbrev = abbrev->next;
13764 }
13765 return NULL;
13766}
13767
13768/* Read in an abbrev table. */
13769
13770static struct abbrev_table *
13771abbrev_table_read_table (struct dwarf2_section_info *section,
13772 sect_offset offset)
13773{
13774 struct objfile *objfile = dwarf2_per_objfile->objfile;
13775 bfd *abfd = section->asection->owner;
13776 struct abbrev_table *abbrev_table;
d521ce57 13777 const gdb_byte *abbrev_ptr;
c906108c
SS
13778 struct abbrev_info *cur_abbrev;
13779 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13780 unsigned int abbrev_form;
f3dd6933
DJ
13781 struct attr_abbrev *cur_attrs;
13782 unsigned int allocated_attrs;
c906108c 13783
433df2d4 13784 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13785 abbrev_table->offset = offset;
433df2d4
DE
13786 obstack_init (&abbrev_table->abbrev_obstack);
13787 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13788 (ABBREV_HASH_SIZE
13789 * sizeof (struct abbrev_info *)));
13790 memset (abbrev_table->abbrevs, 0,
13791 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13792
433df2d4
DE
13793 dwarf2_read_section (objfile, section);
13794 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13795 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13796 abbrev_ptr += bytes_read;
13797
f3dd6933
DJ
13798 allocated_attrs = ATTR_ALLOC_CHUNK;
13799 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13800
0963b4bd 13801 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13802 while (abbrev_number)
13803 {
433df2d4 13804 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13805
13806 /* read in abbrev header */
13807 cur_abbrev->number = abbrev_number;
13808 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13809 abbrev_ptr += bytes_read;
13810 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13811 abbrev_ptr += 1;
13812
13813 /* now read in declarations */
13814 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13815 abbrev_ptr += bytes_read;
13816 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13817 abbrev_ptr += bytes_read;
13818 while (abbrev_name)
13819 {
f3dd6933 13820 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13821 {
f3dd6933
DJ
13822 allocated_attrs += ATTR_ALLOC_CHUNK;
13823 cur_attrs
13824 = xrealloc (cur_attrs, (allocated_attrs
13825 * sizeof (struct attr_abbrev)));
c906108c 13826 }
ae038cb0 13827
f3dd6933
DJ
13828 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13829 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13830 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13831 abbrev_ptr += bytes_read;
13832 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13833 abbrev_ptr += bytes_read;
13834 }
13835
433df2d4 13836 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13837 (cur_abbrev->num_attrs
13838 * sizeof (struct attr_abbrev)));
13839 memcpy (cur_abbrev->attrs, cur_attrs,
13840 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13841
433df2d4 13842 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13843
13844 /* Get next abbreviation.
13845 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13846 always properly terminated with an abbrev number of 0.
13847 Exit loop if we encounter an abbreviation which we have
13848 already read (which means we are about to read the abbreviations
13849 for the next compile unit) or if the end of the abbreviation
13850 table is reached. */
433df2d4 13851 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13852 break;
13853 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13854 abbrev_ptr += bytes_read;
433df2d4 13855 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13856 break;
13857 }
f3dd6933
DJ
13858
13859 xfree (cur_attrs);
433df2d4 13860 return abbrev_table;
c906108c
SS
13861}
13862
433df2d4 13863/* Free the resources held by ABBREV_TABLE. */
c906108c 13864
c906108c 13865static void
433df2d4 13866abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13867{
433df2d4
DE
13868 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13869 xfree (abbrev_table);
c906108c
SS
13870}
13871
f4dc4d17
DE
13872/* Same as abbrev_table_free but as a cleanup.
13873 We pass in a pointer to the pointer to the table so that we can
13874 set the pointer to NULL when we're done. It also simplifies
13875 build_type_unit_groups. */
13876
13877static void
13878abbrev_table_free_cleanup (void *table_ptr)
13879{
13880 struct abbrev_table **abbrev_table_ptr = table_ptr;
13881
13882 if (*abbrev_table_ptr != NULL)
13883 abbrev_table_free (*abbrev_table_ptr);
13884 *abbrev_table_ptr = NULL;
13885}
13886
433df2d4
DE
13887/* Read the abbrev table for CU from ABBREV_SECTION. */
13888
13889static void
13890dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13891 struct dwarf2_section_info *abbrev_section)
c906108c 13892{
433df2d4
DE
13893 cu->abbrev_table =
13894 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13895}
c906108c 13896
433df2d4 13897/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13898
433df2d4
DE
13899static void
13900dwarf2_free_abbrev_table (void *ptr_to_cu)
13901{
13902 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13903
a2ce51a0
DE
13904 if (cu->abbrev_table != NULL)
13905 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13906 /* Set this to NULL so that we SEGV if we try to read it later,
13907 and also because free_comp_unit verifies this is NULL. */
13908 cu->abbrev_table = NULL;
13909}
13910\f
72bf9492
DJ
13911/* Returns nonzero if TAG represents a type that we might generate a partial
13912 symbol for. */
13913
13914static int
13915is_type_tag_for_partial (int tag)
13916{
13917 switch (tag)
13918 {
13919#if 0
13920 /* Some types that would be reasonable to generate partial symbols for,
13921 that we don't at present. */
13922 case DW_TAG_array_type:
13923 case DW_TAG_file_type:
13924 case DW_TAG_ptr_to_member_type:
13925 case DW_TAG_set_type:
13926 case DW_TAG_string_type:
13927 case DW_TAG_subroutine_type:
13928#endif
13929 case DW_TAG_base_type:
13930 case DW_TAG_class_type:
680b30c7 13931 case DW_TAG_interface_type:
72bf9492
DJ
13932 case DW_TAG_enumeration_type:
13933 case DW_TAG_structure_type:
13934 case DW_TAG_subrange_type:
13935 case DW_TAG_typedef:
13936 case DW_TAG_union_type:
13937 return 1;
13938 default:
13939 return 0;
13940 }
13941}
13942
13943/* Load all DIEs that are interesting for partial symbols into memory. */
13944
13945static struct partial_die_info *
dee91e82 13946load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13947 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 13948{
dee91e82 13949 struct dwarf2_cu *cu = reader->cu;
bb5ed363 13950 struct objfile *objfile = cu->objfile;
72bf9492
DJ
13951 struct partial_die_info *part_die;
13952 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13953 struct abbrev_info *abbrev;
13954 unsigned int bytes_read;
5afb4e99 13955 unsigned int load_all = 0;
72bf9492
DJ
13956 int nesting_level = 1;
13957
13958 parent_die = NULL;
13959 last_die = NULL;
13960
7adf1e79
DE
13961 gdb_assert (cu->per_cu != NULL);
13962 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
13963 load_all = 1;
13964
72bf9492
DJ
13965 cu->partial_dies
13966 = htab_create_alloc_ex (cu->header.length / 12,
13967 partial_die_hash,
13968 partial_die_eq,
13969 NULL,
13970 &cu->comp_unit_obstack,
13971 hashtab_obstack_allocate,
13972 dummy_obstack_deallocate);
13973
13974 part_die = obstack_alloc (&cu->comp_unit_obstack,
13975 sizeof (struct partial_die_info));
13976
13977 while (1)
13978 {
13979 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13980
13981 /* A NULL abbrev means the end of a series of children. */
13982 if (abbrev == NULL)
13983 {
13984 if (--nesting_level == 0)
13985 {
13986 /* PART_DIE was probably the last thing allocated on the
13987 comp_unit_obstack, so we could call obstack_free
13988 here. We don't do that because the waste is small,
13989 and will be cleaned up when we're done with this
13990 compilation unit. This way, we're also more robust
13991 against other users of the comp_unit_obstack. */
13992 return first_die;
13993 }
13994 info_ptr += bytes_read;
13995 last_die = parent_die;
13996 parent_die = parent_die->die_parent;
13997 continue;
13998 }
13999
98bfdba5
PA
14000 /* Check for template arguments. We never save these; if
14001 they're seen, we just mark the parent, and go on our way. */
14002 if (parent_die != NULL
14003 && cu->language == language_cplus
14004 && (abbrev->tag == DW_TAG_template_type_param
14005 || abbrev->tag == DW_TAG_template_value_param))
14006 {
14007 parent_die->has_template_arguments = 1;
14008
14009 if (!load_all)
14010 {
14011 /* We don't need a partial DIE for the template argument. */
dee91e82 14012 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14013 continue;
14014 }
14015 }
14016
0d99eb77 14017 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
14018 Skip their other children. */
14019 if (!load_all
14020 && cu->language == language_cplus
14021 && parent_die != NULL
14022 && parent_die->tag == DW_TAG_subprogram)
14023 {
dee91e82 14024 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14025 continue;
14026 }
14027
5afb4e99
DJ
14028 /* Check whether this DIE is interesting enough to save. Normally
14029 we would not be interested in members here, but there may be
14030 later variables referencing them via DW_AT_specification (for
14031 static members). */
14032 if (!load_all
14033 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14034 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14035 && abbrev->tag != DW_TAG_enumerator
14036 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14037 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14038 && abbrev->tag != DW_TAG_variable
5afb4e99 14039 && abbrev->tag != DW_TAG_namespace
f55ee35c 14040 && abbrev->tag != DW_TAG_module
95554aad
TT
14041 && abbrev->tag != DW_TAG_member
14042 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14043 {
14044 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14045 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14046 continue;
14047 }
14048
dee91e82
DE
14049 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14050 info_ptr);
72bf9492
DJ
14051
14052 /* This two-pass algorithm for processing partial symbols has a
14053 high cost in cache pressure. Thus, handle some simple cases
14054 here which cover the majority of C partial symbols. DIEs
14055 which neither have specification tags in them, nor could have
14056 specification tags elsewhere pointing at them, can simply be
14057 processed and discarded.
14058
14059 This segment is also optional; scan_partial_symbols and
14060 add_partial_symbol will handle these DIEs if we chain
14061 them in normally. When compilers which do not emit large
14062 quantities of duplicate debug information are more common,
14063 this code can probably be removed. */
14064
14065 /* Any complete simple types at the top level (pretty much all
14066 of them, for a language without namespaces), can be processed
14067 directly. */
14068 if (parent_die == NULL
14069 && part_die->has_specification == 0
14070 && part_die->is_declaration == 0
d8228535 14071 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14072 || part_die->tag == DW_TAG_base_type
14073 || part_die->tag == DW_TAG_subrange_type))
14074 {
14075 if (building_psymtab && part_die->name != NULL)
04a679b8 14076 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14077 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14078 &objfile->static_psymbols,
14079 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14080 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14081 continue;
14082 }
14083
d8228535
JK
14084 /* The exception for DW_TAG_typedef with has_children above is
14085 a workaround of GCC PR debug/47510. In the case of this complaint
14086 type_name_no_tag_or_error will error on such types later.
14087
14088 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14089 it could not find the child DIEs referenced later, this is checked
14090 above. In correct DWARF DW_TAG_typedef should have no children. */
14091
14092 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14093 complaint (&symfile_complaints,
14094 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14095 "- DIE at 0x%x [in module %s]"),
b64f50a1 14096 part_die->offset.sect_off, objfile->name);
d8228535 14097
72bf9492
DJ
14098 /* If we're at the second level, and we're an enumerator, and
14099 our parent has no specification (meaning possibly lives in a
14100 namespace elsewhere), then we can add the partial symbol now
14101 instead of queueing it. */
14102 if (part_die->tag == DW_TAG_enumerator
14103 && parent_die != NULL
14104 && parent_die->die_parent == NULL
14105 && parent_die->tag == DW_TAG_enumeration_type
14106 && parent_die->has_specification == 0)
14107 {
14108 if (part_die->name == NULL)
3e43a32a
MS
14109 complaint (&symfile_complaints,
14110 _("malformed enumerator DIE ignored"));
72bf9492 14111 else if (building_psymtab)
04a679b8 14112 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14113 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14114 (cu->language == language_cplus
14115 || cu->language == language_java)
bb5ed363
DE
14116 ? &objfile->global_psymbols
14117 : &objfile->static_psymbols,
14118 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14119
dee91e82 14120 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14121 continue;
14122 }
14123
14124 /* We'll save this DIE so link it in. */
14125 part_die->die_parent = parent_die;
14126 part_die->die_sibling = NULL;
14127 part_die->die_child = NULL;
14128
14129 if (last_die && last_die == parent_die)
14130 last_die->die_child = part_die;
14131 else if (last_die)
14132 last_die->die_sibling = part_die;
14133
14134 last_die = part_die;
14135
14136 if (first_die == NULL)
14137 first_die = part_die;
14138
14139 /* Maybe add the DIE to the hash table. Not all DIEs that we
14140 find interesting need to be in the hash table, because we
14141 also have the parent/sibling/child chains; only those that we
14142 might refer to by offset later during partial symbol reading.
14143
14144 For now this means things that might have be the target of a
14145 DW_AT_specification, DW_AT_abstract_origin, or
14146 DW_AT_extension. DW_AT_extension will refer only to
14147 namespaces; DW_AT_abstract_origin refers to functions (and
14148 many things under the function DIE, but we do not recurse
14149 into function DIEs during partial symbol reading) and
14150 possibly variables as well; DW_AT_specification refers to
14151 declarations. Declarations ought to have the DW_AT_declaration
14152 flag. It happens that GCC forgets to put it in sometimes, but
14153 only for functions, not for types.
14154
14155 Adding more things than necessary to the hash table is harmless
14156 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14157 wasted time in find_partial_die, when we reread the compilation
14158 unit with load_all_dies set. */
72bf9492 14159
5afb4e99 14160 if (load_all
72929c62 14161 || abbrev->tag == DW_TAG_constant
5afb4e99 14162 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14163 || abbrev->tag == DW_TAG_variable
14164 || abbrev->tag == DW_TAG_namespace
14165 || part_die->is_declaration)
14166 {
14167 void **slot;
14168
14169 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14170 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14171 *slot = part_die;
14172 }
14173
14174 part_die = obstack_alloc (&cu->comp_unit_obstack,
14175 sizeof (struct partial_die_info));
14176
14177 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14178 we have no reason to follow the children of structures; for other
98bfdba5
PA
14179 languages we have to, so that we can get at method physnames
14180 to infer fully qualified class names, for DW_AT_specification,
14181 and for C++ template arguments. For C++, we also look one level
14182 inside functions to find template arguments (if the name of the
14183 function does not already contain the template arguments).
bc30ff58
JB
14184
14185 For Ada, we need to scan the children of subprograms and lexical
14186 blocks as well because Ada allows the definition of nested
14187 entities that could be interesting for the debugger, such as
14188 nested subprograms for instance. */
72bf9492 14189 if (last_die->has_children
5afb4e99
DJ
14190 && (load_all
14191 || last_die->tag == DW_TAG_namespace
f55ee35c 14192 || last_die->tag == DW_TAG_module
72bf9492 14193 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14194 || (cu->language == language_cplus
14195 && last_die->tag == DW_TAG_subprogram
14196 && (last_die->name == NULL
14197 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14198 || (cu->language != language_c
14199 && (last_die->tag == DW_TAG_class_type
680b30c7 14200 || last_die->tag == DW_TAG_interface_type
72bf9492 14201 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14202 || last_die->tag == DW_TAG_union_type))
14203 || (cu->language == language_ada
14204 && (last_die->tag == DW_TAG_subprogram
14205 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14206 {
14207 nesting_level++;
14208 parent_die = last_die;
14209 continue;
14210 }
14211
14212 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14213 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14214
14215 /* Back to the top, do it again. */
14216 }
14217}
14218
c906108c
SS
14219/* Read a minimal amount of information into the minimal die structure. */
14220
d521ce57 14221static const gdb_byte *
dee91e82
DE
14222read_partial_die (const struct die_reader_specs *reader,
14223 struct partial_die_info *part_die,
14224 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14225 const gdb_byte *info_ptr)
c906108c 14226{
dee91e82 14227 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14228 struct objfile *objfile = cu->objfile;
d521ce57 14229 const gdb_byte *buffer = reader->buffer;
fa238c03 14230 unsigned int i;
c906108c 14231 struct attribute attr;
c5aa993b 14232 int has_low_pc_attr = 0;
c906108c 14233 int has_high_pc_attr = 0;
91da1414 14234 int high_pc_relative = 0;
c906108c 14235
72bf9492 14236 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14237
b64f50a1 14238 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14239
14240 info_ptr += abbrev_len;
14241
14242 if (abbrev == NULL)
14243 return info_ptr;
14244
c906108c
SS
14245 part_die->tag = abbrev->tag;
14246 part_die->has_children = abbrev->has_children;
c906108c
SS
14247
14248 for (i = 0; i < abbrev->num_attrs; ++i)
14249 {
dee91e82 14250 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14251
14252 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14253 partial symbol table. */
c906108c
SS
14254 switch (attr.name)
14255 {
14256 case DW_AT_name:
71c25dea
TT
14257 switch (part_die->tag)
14258 {
14259 case DW_TAG_compile_unit:
95554aad 14260 case DW_TAG_partial_unit:
348e048f 14261 case DW_TAG_type_unit:
71c25dea
TT
14262 /* Compilation units have a DW_AT_name that is a filename, not
14263 a source language identifier. */
14264 case DW_TAG_enumeration_type:
14265 case DW_TAG_enumerator:
14266 /* These tags always have simple identifiers already; no need
14267 to canonicalize them. */
14268 part_die->name = DW_STRING (&attr);
14269 break;
14270 default:
14271 part_die->name
14272 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14273 &objfile->objfile_obstack);
71c25dea
TT
14274 break;
14275 }
c906108c 14276 break;
31ef98ae 14277 case DW_AT_linkage_name:
c906108c 14278 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14279 /* Note that both forms of linkage name might appear. We
14280 assume they will be the same, and we only store the last
14281 one we see. */
94af9270
KS
14282 if (cu->language == language_ada)
14283 part_die->name = DW_STRING (&attr);
abc72ce4 14284 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14285 break;
14286 case DW_AT_low_pc:
14287 has_low_pc_attr = 1;
14288 part_die->lowpc = DW_ADDR (&attr);
14289 break;
14290 case DW_AT_high_pc:
14291 has_high_pc_attr = 1;
3019eac3
DE
14292 if (attr.form == DW_FORM_addr
14293 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14294 part_die->highpc = DW_ADDR (&attr);
14295 else
14296 {
14297 high_pc_relative = 1;
14298 part_die->highpc = DW_UNSND (&attr);
14299 }
c906108c
SS
14300 break;
14301 case DW_AT_location:
0963b4bd 14302 /* Support the .debug_loc offsets. */
8e19ed76
PS
14303 if (attr_form_is_block (&attr))
14304 {
95554aad 14305 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14306 }
3690dd37 14307 else if (attr_form_is_section_offset (&attr))
8e19ed76 14308 {
4d3c2250 14309 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14310 }
14311 else
14312 {
4d3c2250
KB
14313 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14314 "partial symbol information");
8e19ed76 14315 }
c906108c 14316 break;
c906108c
SS
14317 case DW_AT_external:
14318 part_die->is_external = DW_UNSND (&attr);
14319 break;
14320 case DW_AT_declaration:
14321 part_die->is_declaration = DW_UNSND (&attr);
14322 break;
14323 case DW_AT_type:
14324 part_die->has_type = 1;
14325 break;
14326 case DW_AT_abstract_origin:
14327 case DW_AT_specification:
72bf9492
DJ
14328 case DW_AT_extension:
14329 part_die->has_specification = 1;
c764a876 14330 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14331 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14332 || cu->per_cu->is_dwz);
c906108c
SS
14333 break;
14334 case DW_AT_sibling:
14335 /* Ignore absolute siblings, they might point outside of
14336 the current compile unit. */
14337 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14338 complaint (&symfile_complaints,
14339 _("ignoring absolute DW_AT_sibling"));
c906108c 14340 else
b64f50a1 14341 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14342 break;
fa4028e9
JB
14343 case DW_AT_byte_size:
14344 part_die->has_byte_size = 1;
14345 break;
68511cec
CES
14346 case DW_AT_calling_convention:
14347 /* DWARF doesn't provide a way to identify a program's source-level
14348 entry point. DW_AT_calling_convention attributes are only meant
14349 to describe functions' calling conventions.
14350
14351 However, because it's a necessary piece of information in
14352 Fortran, and because DW_CC_program is the only piece of debugging
14353 information whose definition refers to a 'main program' at all,
14354 several compilers have begun marking Fortran main programs with
14355 DW_CC_program --- even when those functions use the standard
14356 calling conventions.
14357
14358 So until DWARF specifies a way to provide this information and
14359 compilers pick up the new representation, we'll support this
14360 practice. */
14361 if (DW_UNSND (&attr) == DW_CC_program
14362 && cu->language == language_fortran)
01f8c46d
JK
14363 {
14364 set_main_name (part_die->name);
14365
14366 /* As this DIE has a static linkage the name would be difficult
14367 to look up later. */
14368 language_of_main = language_fortran;
14369 }
68511cec 14370 break;
481860b3
GB
14371 case DW_AT_inline:
14372 if (DW_UNSND (&attr) == DW_INL_inlined
14373 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14374 part_die->may_be_inlined = 1;
14375 break;
95554aad
TT
14376
14377 case DW_AT_import:
14378 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14379 {
14380 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14381 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14382 || cu->per_cu->is_dwz);
14383 }
95554aad
TT
14384 break;
14385
c906108c
SS
14386 default:
14387 break;
14388 }
14389 }
14390
91da1414
MW
14391 if (high_pc_relative)
14392 part_die->highpc += part_die->lowpc;
14393
9373cf26
JK
14394 if (has_low_pc_attr && has_high_pc_attr)
14395 {
14396 /* When using the GNU linker, .gnu.linkonce. sections are used to
14397 eliminate duplicate copies of functions and vtables and such.
14398 The linker will arbitrarily choose one and discard the others.
14399 The AT_*_pc values for such functions refer to local labels in
14400 these sections. If the section from that file was discarded, the
14401 labels are not in the output, so the relocs get a value of 0.
14402 If this is a discarded function, mark the pc bounds as invalid,
14403 so that GDB will ignore it. */
14404 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14405 {
bb5ed363 14406 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14407
14408 complaint (&symfile_complaints,
14409 _("DW_AT_low_pc %s is zero "
14410 "for DIE at 0x%x [in module %s]"),
14411 paddress (gdbarch, part_die->lowpc),
b64f50a1 14412 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14413 }
14414 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14415 else if (part_die->lowpc >= part_die->highpc)
14416 {
bb5ed363 14417 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14418
14419 complaint (&symfile_complaints,
14420 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14421 "for DIE at 0x%x [in module %s]"),
14422 paddress (gdbarch, part_die->lowpc),
14423 paddress (gdbarch, part_die->highpc),
b64f50a1 14424 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14425 }
14426 else
14427 part_die->has_pc_info = 1;
14428 }
85cbf3d3 14429
c906108c
SS
14430 return info_ptr;
14431}
14432
72bf9492
DJ
14433/* Find a cached partial DIE at OFFSET in CU. */
14434
14435static struct partial_die_info *
b64f50a1 14436find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14437{
14438 struct partial_die_info *lookup_die = NULL;
14439 struct partial_die_info part_die;
14440
14441 part_die.offset = offset;
b64f50a1
JK
14442 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14443 offset.sect_off);
72bf9492 14444
72bf9492
DJ
14445 return lookup_die;
14446}
14447
348e048f
DE
14448/* Find a partial DIE at OFFSET, which may or may not be in CU,
14449 except in the case of .debug_types DIEs which do not reference
14450 outside their CU (they do however referencing other types via
55f1336d 14451 DW_FORM_ref_sig8). */
72bf9492
DJ
14452
14453static struct partial_die_info *
36586728 14454find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14455{
bb5ed363 14456 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14457 struct dwarf2_per_cu_data *per_cu = NULL;
14458 struct partial_die_info *pd = NULL;
72bf9492 14459
36586728
TT
14460 if (offset_in_dwz == cu->per_cu->is_dwz
14461 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14462 {
14463 pd = find_partial_die_in_comp_unit (offset, cu);
14464 if (pd != NULL)
14465 return pd;
0d99eb77
DE
14466 /* We missed recording what we needed.
14467 Load all dies and try again. */
14468 per_cu = cu->per_cu;
5afb4e99 14469 }
0d99eb77
DE
14470 else
14471 {
14472 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14473 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14474 {
14475 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14476 " external reference to offset 0x%lx [in module %s].\n"),
14477 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14478 bfd_get_filename (objfile->obfd));
14479 }
36586728
TT
14480 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14481 objfile);
72bf9492 14482
0d99eb77
DE
14483 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14484 load_partial_comp_unit (per_cu);
ae038cb0 14485
0d99eb77
DE
14486 per_cu->cu->last_used = 0;
14487 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14488 }
5afb4e99 14489
dee91e82
DE
14490 /* If we didn't find it, and not all dies have been loaded,
14491 load them all and try again. */
14492
5afb4e99
DJ
14493 if (pd == NULL && per_cu->load_all_dies == 0)
14494 {
5afb4e99 14495 per_cu->load_all_dies = 1;
fd820528
DE
14496
14497 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14498 THIS_CU->cu may already be in use. So we can't just free it and
14499 replace its DIEs with the ones we read in. Instead, we leave those
14500 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14501 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14502 set. */
dee91e82 14503 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14504
14505 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14506 }
14507
14508 if (pd == NULL)
14509 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14510 _("could not find partial DIE 0x%x "
14511 "in cache [from module %s]\n"),
b64f50a1 14512 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14513 return pd;
72bf9492
DJ
14514}
14515
abc72ce4
DE
14516/* See if we can figure out if the class lives in a namespace. We do
14517 this by looking for a member function; its demangled name will
14518 contain namespace info, if there is any. */
14519
14520static void
14521guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14522 struct dwarf2_cu *cu)
14523{
14524 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14525 what template types look like, because the demangler
14526 frequently doesn't give the same name as the debug info. We
14527 could fix this by only using the demangled name to get the
14528 prefix (but see comment in read_structure_type). */
14529
14530 struct partial_die_info *real_pdi;
14531 struct partial_die_info *child_pdi;
14532
14533 /* If this DIE (this DIE's specification, if any) has a parent, then
14534 we should not do this. We'll prepend the parent's fully qualified
14535 name when we create the partial symbol. */
14536
14537 real_pdi = struct_pdi;
14538 while (real_pdi->has_specification)
36586728
TT
14539 real_pdi = find_partial_die (real_pdi->spec_offset,
14540 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14541
14542 if (real_pdi->die_parent != NULL)
14543 return;
14544
14545 for (child_pdi = struct_pdi->die_child;
14546 child_pdi != NULL;
14547 child_pdi = child_pdi->die_sibling)
14548 {
14549 if (child_pdi->tag == DW_TAG_subprogram
14550 && child_pdi->linkage_name != NULL)
14551 {
14552 char *actual_class_name
14553 = language_class_name_from_physname (cu->language_defn,
14554 child_pdi->linkage_name);
14555 if (actual_class_name != NULL)
14556 {
14557 struct_pdi->name
10f0c4bb
TT
14558 = obstack_copy0 (&cu->objfile->objfile_obstack,
14559 actual_class_name,
14560 strlen (actual_class_name));
abc72ce4
DE
14561 xfree (actual_class_name);
14562 }
14563 break;
14564 }
14565 }
14566}
14567
72bf9492
DJ
14568/* Adjust PART_DIE before generating a symbol for it. This function
14569 may set the is_external flag or change the DIE's name. */
14570
14571static void
14572fixup_partial_die (struct partial_die_info *part_die,
14573 struct dwarf2_cu *cu)
14574{
abc72ce4
DE
14575 /* Once we've fixed up a die, there's no point in doing so again.
14576 This also avoids a memory leak if we were to call
14577 guess_partial_die_structure_name multiple times. */
14578 if (part_die->fixup_called)
14579 return;
14580
72bf9492
DJ
14581 /* If we found a reference attribute and the DIE has no name, try
14582 to find a name in the referred to DIE. */
14583
14584 if (part_die->name == NULL && part_die->has_specification)
14585 {
14586 struct partial_die_info *spec_die;
72bf9492 14587
36586728
TT
14588 spec_die = find_partial_die (part_die->spec_offset,
14589 part_die->spec_is_dwz, cu);
72bf9492 14590
10b3939b 14591 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14592
14593 if (spec_die->name)
14594 {
14595 part_die->name = spec_die->name;
14596
14597 /* Copy DW_AT_external attribute if it is set. */
14598 if (spec_die->is_external)
14599 part_die->is_external = spec_die->is_external;
14600 }
14601 }
14602
14603 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14604
14605 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14606 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14607
abc72ce4
DE
14608 /* If there is no parent die to provide a namespace, and there are
14609 children, see if we can determine the namespace from their linkage
122d1940 14610 name. */
abc72ce4 14611 if (cu->language == language_cplus
8b70b953 14612 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14613 && part_die->die_parent == NULL
14614 && part_die->has_children
14615 && (part_die->tag == DW_TAG_class_type
14616 || part_die->tag == DW_TAG_structure_type
14617 || part_die->tag == DW_TAG_union_type))
14618 guess_partial_die_structure_name (part_die, cu);
14619
53832f31
TT
14620 /* GCC might emit a nameless struct or union that has a linkage
14621 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14622 if (part_die->name == NULL
96408a79
SA
14623 && (part_die->tag == DW_TAG_class_type
14624 || part_die->tag == DW_TAG_interface_type
14625 || part_die->tag == DW_TAG_structure_type
14626 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14627 && part_die->linkage_name != NULL)
14628 {
14629 char *demangled;
14630
8de20a37 14631 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14632 if (demangled)
14633 {
96408a79
SA
14634 const char *base;
14635
14636 /* Strip any leading namespaces/classes, keep only the base name.
14637 DW_AT_name for named DIEs does not contain the prefixes. */
14638 base = strrchr (demangled, ':');
14639 if (base && base > demangled && base[-1] == ':')
14640 base++;
14641 else
14642 base = demangled;
14643
10f0c4bb
TT
14644 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14645 base, strlen (base));
53832f31
TT
14646 xfree (demangled);
14647 }
14648 }
14649
abc72ce4 14650 part_die->fixup_called = 1;
72bf9492
DJ
14651}
14652
a8329558 14653/* Read an attribute value described by an attribute form. */
c906108c 14654
d521ce57 14655static const gdb_byte *
dee91e82
DE
14656read_attribute_value (const struct die_reader_specs *reader,
14657 struct attribute *attr, unsigned form,
d521ce57 14658 const gdb_byte *info_ptr)
c906108c 14659{
dee91e82
DE
14660 struct dwarf2_cu *cu = reader->cu;
14661 bfd *abfd = reader->abfd;
e7c27a73 14662 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14663 unsigned int bytes_read;
14664 struct dwarf_block *blk;
14665
a8329558
KW
14666 attr->form = form;
14667 switch (form)
c906108c 14668 {
c906108c 14669 case DW_FORM_ref_addr:
ae411497 14670 if (cu->header.version == 2)
4568ecf9 14671 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14672 else
4568ecf9
DE
14673 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14674 &cu->header, &bytes_read);
ae411497
TT
14675 info_ptr += bytes_read;
14676 break;
36586728
TT
14677 case DW_FORM_GNU_ref_alt:
14678 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14679 info_ptr += bytes_read;
14680 break;
ae411497 14681 case DW_FORM_addr:
e7c27a73 14682 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14683 info_ptr += bytes_read;
c906108c
SS
14684 break;
14685 case DW_FORM_block2:
7b5a2f43 14686 blk = dwarf_alloc_block (cu);
c906108c
SS
14687 blk->size = read_2_bytes (abfd, info_ptr);
14688 info_ptr += 2;
14689 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14690 info_ptr += blk->size;
14691 DW_BLOCK (attr) = blk;
14692 break;
14693 case DW_FORM_block4:
7b5a2f43 14694 blk = dwarf_alloc_block (cu);
c906108c
SS
14695 blk->size = read_4_bytes (abfd, info_ptr);
14696 info_ptr += 4;
14697 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14698 info_ptr += blk->size;
14699 DW_BLOCK (attr) = blk;
14700 break;
14701 case DW_FORM_data2:
14702 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14703 info_ptr += 2;
14704 break;
14705 case DW_FORM_data4:
14706 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14707 info_ptr += 4;
14708 break;
14709 case DW_FORM_data8:
14710 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14711 info_ptr += 8;
14712 break;
2dc7f7b3
TT
14713 case DW_FORM_sec_offset:
14714 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14715 info_ptr += bytes_read;
14716 break;
c906108c 14717 case DW_FORM_string:
9b1c24c8 14718 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14719 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14720 info_ptr += bytes_read;
14721 break;
4bdf3d34 14722 case DW_FORM_strp:
36586728
TT
14723 if (!cu->per_cu->is_dwz)
14724 {
14725 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14726 &bytes_read);
14727 DW_STRING_IS_CANONICAL (attr) = 0;
14728 info_ptr += bytes_read;
14729 break;
14730 }
14731 /* FALLTHROUGH */
14732 case DW_FORM_GNU_strp_alt:
14733 {
14734 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14735 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14736 &bytes_read);
14737
14738 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14739 DW_STRING_IS_CANONICAL (attr) = 0;
14740 info_ptr += bytes_read;
14741 }
4bdf3d34 14742 break;
2dc7f7b3 14743 case DW_FORM_exprloc:
c906108c 14744 case DW_FORM_block:
7b5a2f43 14745 blk = dwarf_alloc_block (cu);
c906108c
SS
14746 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14747 info_ptr += bytes_read;
14748 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14749 info_ptr += blk->size;
14750 DW_BLOCK (attr) = blk;
14751 break;
14752 case DW_FORM_block1:
7b5a2f43 14753 blk = dwarf_alloc_block (cu);
c906108c
SS
14754 blk->size = read_1_byte (abfd, info_ptr);
14755 info_ptr += 1;
14756 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14757 info_ptr += blk->size;
14758 DW_BLOCK (attr) = blk;
14759 break;
14760 case DW_FORM_data1:
14761 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14762 info_ptr += 1;
14763 break;
14764 case DW_FORM_flag:
14765 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14766 info_ptr += 1;
14767 break;
2dc7f7b3
TT
14768 case DW_FORM_flag_present:
14769 DW_UNSND (attr) = 1;
14770 break;
c906108c
SS
14771 case DW_FORM_sdata:
14772 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14773 info_ptr += bytes_read;
14774 break;
14775 case DW_FORM_udata:
14776 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14777 info_ptr += bytes_read;
14778 break;
14779 case DW_FORM_ref1:
4568ecf9
DE
14780 DW_UNSND (attr) = (cu->header.offset.sect_off
14781 + read_1_byte (abfd, info_ptr));
c906108c
SS
14782 info_ptr += 1;
14783 break;
14784 case DW_FORM_ref2:
4568ecf9
DE
14785 DW_UNSND (attr) = (cu->header.offset.sect_off
14786 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14787 info_ptr += 2;
14788 break;
14789 case DW_FORM_ref4:
4568ecf9
DE
14790 DW_UNSND (attr) = (cu->header.offset.sect_off
14791 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14792 info_ptr += 4;
14793 break;
613e1657 14794 case DW_FORM_ref8:
4568ecf9
DE
14795 DW_UNSND (attr) = (cu->header.offset.sect_off
14796 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14797 info_ptr += 8;
14798 break;
55f1336d 14799 case DW_FORM_ref_sig8:
ac9ec31b 14800 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14801 info_ptr += 8;
14802 break;
c906108c 14803 case DW_FORM_ref_udata:
4568ecf9
DE
14804 DW_UNSND (attr) = (cu->header.offset.sect_off
14805 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14806 info_ptr += bytes_read;
14807 break;
c906108c 14808 case DW_FORM_indirect:
a8329558
KW
14809 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14810 info_ptr += bytes_read;
dee91e82 14811 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14812 break;
3019eac3
DE
14813 case DW_FORM_GNU_addr_index:
14814 if (reader->dwo_file == NULL)
14815 {
14816 /* For now flag a hard error.
14817 Later we can turn this into a complaint. */
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 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14823 info_ptr += bytes_read;
14824 break;
14825 case DW_FORM_GNU_str_index:
14826 if (reader->dwo_file == NULL)
14827 {
14828 /* For now flag a hard error.
14829 Later we can turn this into a complaint if warranted. */
14830 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14831 dwarf_form_name (form),
14832 bfd_get_filename (abfd));
14833 }
14834 {
14835 ULONGEST str_index =
14836 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14837
14838 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14839 DW_STRING_IS_CANONICAL (attr) = 0;
14840 info_ptr += bytes_read;
14841 }
14842 break;
c906108c 14843 default:
8a3fe4f8 14844 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14845 dwarf_form_name (form),
14846 bfd_get_filename (abfd));
c906108c 14847 }
28e94949 14848
36586728 14849 /* Super hack. */
7771576e 14850 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14851 attr->form = DW_FORM_GNU_ref_alt;
14852
28e94949
JB
14853 /* We have seen instances where the compiler tried to emit a byte
14854 size attribute of -1 which ended up being encoded as an unsigned
14855 0xffffffff. Although 0xffffffff is technically a valid size value,
14856 an object of this size seems pretty unlikely so we can relatively
14857 safely treat these cases as if the size attribute was invalid and
14858 treat them as zero by default. */
14859 if (attr->name == DW_AT_byte_size
14860 && form == DW_FORM_data4
14861 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14862 {
14863 complaint
14864 (&symfile_complaints,
43bbcdc2
PH
14865 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14866 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14867 DW_UNSND (attr) = 0;
14868 }
28e94949 14869
c906108c
SS
14870 return info_ptr;
14871}
14872
a8329558
KW
14873/* Read an attribute described by an abbreviated attribute. */
14874
d521ce57 14875static const gdb_byte *
dee91e82
DE
14876read_attribute (const struct die_reader_specs *reader,
14877 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14878 const gdb_byte *info_ptr)
a8329558
KW
14879{
14880 attr->name = abbrev->name;
dee91e82 14881 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14882}
14883
0963b4bd 14884/* Read dwarf information from a buffer. */
c906108c
SS
14885
14886static unsigned int
a1855c1d 14887read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14888{
fe1b8b76 14889 return bfd_get_8 (abfd, buf);
c906108c
SS
14890}
14891
14892static int
a1855c1d 14893read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14894{
fe1b8b76 14895 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14896}
14897
14898static unsigned int
a1855c1d 14899read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14900{
fe1b8b76 14901 return bfd_get_16 (abfd, buf);
c906108c
SS
14902}
14903
21ae7a4d 14904static int
a1855c1d 14905read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14906{
14907 return bfd_get_signed_16 (abfd, buf);
14908}
14909
c906108c 14910static unsigned int
a1855c1d 14911read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14912{
fe1b8b76 14913 return bfd_get_32 (abfd, buf);
c906108c
SS
14914}
14915
21ae7a4d 14916static int
a1855c1d 14917read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14918{
14919 return bfd_get_signed_32 (abfd, buf);
14920}
14921
93311388 14922static ULONGEST
a1855c1d 14923read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14924{
fe1b8b76 14925 return bfd_get_64 (abfd, buf);
c906108c
SS
14926}
14927
14928static CORE_ADDR
d521ce57 14929read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14930 unsigned int *bytes_read)
c906108c 14931{
e7c27a73 14932 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14933 CORE_ADDR retval = 0;
14934
107d2387 14935 if (cu_header->signed_addr_p)
c906108c 14936 {
107d2387
AC
14937 switch (cu_header->addr_size)
14938 {
14939 case 2:
fe1b8b76 14940 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14941 break;
14942 case 4:
fe1b8b76 14943 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14944 break;
14945 case 8:
fe1b8b76 14946 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14947 break;
14948 default:
8e65ff28 14949 internal_error (__FILE__, __LINE__,
e2e0b3e5 14950 _("read_address: bad switch, signed [in module %s]"),
659b0389 14951 bfd_get_filename (abfd));
107d2387
AC
14952 }
14953 }
14954 else
14955 {
14956 switch (cu_header->addr_size)
14957 {
14958 case 2:
fe1b8b76 14959 retval = bfd_get_16 (abfd, buf);
107d2387
AC
14960 break;
14961 case 4:
fe1b8b76 14962 retval = bfd_get_32 (abfd, buf);
107d2387
AC
14963 break;
14964 case 8:
fe1b8b76 14965 retval = bfd_get_64 (abfd, buf);
107d2387
AC
14966 break;
14967 default:
8e65ff28 14968 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
14969 _("read_address: bad switch, "
14970 "unsigned [in module %s]"),
659b0389 14971 bfd_get_filename (abfd));
107d2387 14972 }
c906108c 14973 }
64367e0a 14974
107d2387
AC
14975 *bytes_read = cu_header->addr_size;
14976 return retval;
c906108c
SS
14977}
14978
f7ef9339 14979/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
14980 specification allows the initial length to take up either 4 bytes
14981 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14982 bytes describe the length and all offsets will be 8 bytes in length
14983 instead of 4.
14984
f7ef9339
KB
14985 An older, non-standard 64-bit format is also handled by this
14986 function. The older format in question stores the initial length
14987 as an 8-byte quantity without an escape value. Lengths greater
14988 than 2^32 aren't very common which means that the initial 4 bytes
14989 is almost always zero. Since a length value of zero doesn't make
14990 sense for the 32-bit format, this initial zero can be considered to
14991 be an escape value which indicates the presence of the older 64-bit
14992 format. As written, the code can't detect (old format) lengths
917c78fc
MK
14993 greater than 4GB. If it becomes necessary to handle lengths
14994 somewhat larger than 4GB, we could allow other small values (such
14995 as the non-sensical values of 1, 2, and 3) to also be used as
14996 escape values indicating the presence of the old format.
f7ef9339 14997
917c78fc
MK
14998 The value returned via bytes_read should be used to increment the
14999 relevant pointer after calling read_initial_length().
c764a876 15000
613e1657
KB
15001 [ Note: read_initial_length() and read_offset() are based on the
15002 document entitled "DWARF Debugging Information Format", revision
f7ef9339 15003 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
15004 from:
15005
f7ef9339 15006 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 15007
613e1657
KB
15008 This document is only a draft and is subject to change. (So beware.)
15009
f7ef9339 15010 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
15011 determined empirically by examining 64-bit ELF files produced by
15012 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
15013
15014 - Kevin, July 16, 2002
613e1657
KB
15015 ] */
15016
15017static LONGEST
d521ce57 15018read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 15019{
fe1b8b76 15020 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 15021
dd373385 15022 if (length == 0xffffffff)
613e1657 15023 {
fe1b8b76 15024 length = bfd_get_64 (abfd, buf + 4);
613e1657 15025 *bytes_read = 12;
613e1657 15026 }
dd373385 15027 else if (length == 0)
f7ef9339 15028 {
dd373385 15029 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15030 length = bfd_get_64 (abfd, buf);
f7ef9339 15031 *bytes_read = 8;
f7ef9339 15032 }
613e1657
KB
15033 else
15034 {
15035 *bytes_read = 4;
613e1657
KB
15036 }
15037
c764a876
DE
15038 return length;
15039}
dd373385 15040
c764a876
DE
15041/* Cover function for read_initial_length.
15042 Returns the length of the object at BUF, and stores the size of the
15043 initial length in *BYTES_READ and stores the size that offsets will be in
15044 *OFFSET_SIZE.
15045 If the initial length size is not equivalent to that specified in
15046 CU_HEADER then issue a complaint.
15047 This is useful when reading non-comp-unit headers. */
dd373385 15048
c764a876 15049static LONGEST
d521ce57 15050read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15051 const struct comp_unit_head *cu_header,
15052 unsigned int *bytes_read,
15053 unsigned int *offset_size)
15054{
15055 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15056
15057 gdb_assert (cu_header->initial_length_size == 4
15058 || cu_header->initial_length_size == 8
15059 || cu_header->initial_length_size == 12);
15060
15061 if (cu_header->initial_length_size != *bytes_read)
15062 complaint (&symfile_complaints,
15063 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15064
c764a876 15065 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15066 return length;
613e1657
KB
15067}
15068
15069/* Read an offset from the data stream. The size of the offset is
917c78fc 15070 given by cu_header->offset_size. */
613e1657
KB
15071
15072static LONGEST
d521ce57
TT
15073read_offset (bfd *abfd, const gdb_byte *buf,
15074 const struct comp_unit_head *cu_header,
891d2f0b 15075 unsigned int *bytes_read)
c764a876
DE
15076{
15077 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15078
c764a876
DE
15079 *bytes_read = cu_header->offset_size;
15080 return offset;
15081}
15082
15083/* Read an offset from the data stream. */
15084
15085static LONGEST
d521ce57 15086read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15087{
15088 LONGEST retval = 0;
15089
c764a876 15090 switch (offset_size)
613e1657
KB
15091 {
15092 case 4:
fe1b8b76 15093 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15094 break;
15095 case 8:
fe1b8b76 15096 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15097 break;
15098 default:
8e65ff28 15099 internal_error (__FILE__, __LINE__,
c764a876 15100 _("read_offset_1: bad switch [in module %s]"),
659b0389 15101 bfd_get_filename (abfd));
613e1657
KB
15102 }
15103
917c78fc 15104 return retval;
613e1657
KB
15105}
15106
d521ce57
TT
15107static const gdb_byte *
15108read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15109{
15110 /* If the size of a host char is 8 bits, we can return a pointer
15111 to the buffer, otherwise we have to copy the data to a buffer
15112 allocated on the temporary obstack. */
4bdf3d34 15113 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15114 return buf;
c906108c
SS
15115}
15116
d521ce57
TT
15117static const char *
15118read_direct_string (bfd *abfd, const gdb_byte *buf,
15119 unsigned int *bytes_read_ptr)
c906108c
SS
15120{
15121 /* If the size of a host char is 8 bits, we can return a pointer
15122 to the string, otherwise we have to copy the string to a buffer
15123 allocated on the temporary obstack. */
4bdf3d34 15124 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15125 if (*buf == '\0')
15126 {
15127 *bytes_read_ptr = 1;
15128 return NULL;
15129 }
d521ce57
TT
15130 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15131 return (const char *) buf;
4bdf3d34
JJ
15132}
15133
d521ce57 15134static const char *
cf2c3c16 15135read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15136{
be391dca 15137 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15138 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15139 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15140 bfd_get_filename (abfd));
dce234bc 15141 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15142 error (_("DW_FORM_strp pointing outside of "
15143 ".debug_str section [in module %s]"),
15144 bfd_get_filename (abfd));
4bdf3d34 15145 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15146 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15147 return NULL;
d521ce57 15148 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15149}
15150
36586728
TT
15151/* Read a string at offset STR_OFFSET in the .debug_str section from
15152 the .dwz file DWZ. Throw an error if the offset is too large. If
15153 the string consists of a single NUL byte, return NULL; otherwise
15154 return a pointer to the string. */
15155
d521ce57 15156static const char *
36586728
TT
15157read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15158{
15159 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15160
15161 if (dwz->str.buffer == NULL)
15162 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15163 "section [in module %s]"),
15164 bfd_get_filename (dwz->dwz_bfd));
15165 if (str_offset >= dwz->str.size)
15166 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15167 ".debug_str section [in module %s]"),
15168 bfd_get_filename (dwz->dwz_bfd));
15169 gdb_assert (HOST_CHAR_BIT == 8);
15170 if (dwz->str.buffer[str_offset] == '\0')
15171 return NULL;
d521ce57 15172 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15173}
15174
d521ce57
TT
15175static const char *
15176read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15177 const struct comp_unit_head *cu_header,
15178 unsigned int *bytes_read_ptr)
15179{
15180 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15181
15182 return read_indirect_string_at_offset (abfd, str_offset);
15183}
15184
12df843f 15185static ULONGEST
d521ce57
TT
15186read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15187 unsigned int *bytes_read_ptr)
c906108c 15188{
12df843f 15189 ULONGEST result;
ce5d95e1 15190 unsigned int num_read;
c906108c
SS
15191 int i, shift;
15192 unsigned char byte;
15193
15194 result = 0;
15195 shift = 0;
15196 num_read = 0;
15197 i = 0;
15198 while (1)
15199 {
fe1b8b76 15200 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15201 buf++;
15202 num_read++;
12df843f 15203 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15204 if ((byte & 128) == 0)
15205 {
15206 break;
15207 }
15208 shift += 7;
15209 }
15210 *bytes_read_ptr = num_read;
15211 return result;
15212}
15213
12df843f 15214static LONGEST
d521ce57
TT
15215read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15216 unsigned int *bytes_read_ptr)
c906108c 15217{
12df843f 15218 LONGEST result;
77e0b926 15219 int i, shift, num_read;
c906108c
SS
15220 unsigned char byte;
15221
15222 result = 0;
15223 shift = 0;
c906108c
SS
15224 num_read = 0;
15225 i = 0;
15226 while (1)
15227 {
fe1b8b76 15228 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15229 buf++;
15230 num_read++;
12df843f 15231 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15232 shift += 7;
15233 if ((byte & 128) == 0)
15234 {
15235 break;
15236 }
15237 }
77e0b926 15238 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15239 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15240 *bytes_read_ptr = num_read;
15241 return result;
15242}
15243
3019eac3
DE
15244/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15245 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15246 ADDR_SIZE is the size of addresses from the CU header. */
15247
15248static CORE_ADDR
15249read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15250{
15251 struct objfile *objfile = dwarf2_per_objfile->objfile;
15252 bfd *abfd = objfile->obfd;
15253 const gdb_byte *info_ptr;
15254
15255 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15256 if (dwarf2_per_objfile->addr.buffer == NULL)
15257 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15258 objfile->name);
15259 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15260 error (_("DW_FORM_addr_index pointing outside of "
15261 ".debug_addr section [in module %s]"),
15262 objfile->name);
15263 info_ptr = (dwarf2_per_objfile->addr.buffer
15264 + addr_base + addr_index * addr_size);
15265 if (addr_size == 4)
15266 return bfd_get_32 (abfd, info_ptr);
15267 else
15268 return bfd_get_64 (abfd, info_ptr);
15269}
15270
15271/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15272
15273static CORE_ADDR
15274read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15275{
15276 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15277}
15278
15279/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15280
15281static CORE_ADDR
d521ce57 15282read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15283 unsigned int *bytes_read)
15284{
15285 bfd *abfd = cu->objfile->obfd;
15286 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15287
15288 return read_addr_index (cu, addr_index);
15289}
15290
15291/* Data structure to pass results from dwarf2_read_addr_index_reader
15292 back to dwarf2_read_addr_index. */
15293
15294struct dwarf2_read_addr_index_data
15295{
15296 ULONGEST addr_base;
15297 int addr_size;
15298};
15299
15300/* die_reader_func for dwarf2_read_addr_index. */
15301
15302static void
15303dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15304 const gdb_byte *info_ptr,
3019eac3
DE
15305 struct die_info *comp_unit_die,
15306 int has_children,
15307 void *data)
15308{
15309 struct dwarf2_cu *cu = reader->cu;
15310 struct dwarf2_read_addr_index_data *aidata =
15311 (struct dwarf2_read_addr_index_data *) data;
15312
15313 aidata->addr_base = cu->addr_base;
15314 aidata->addr_size = cu->header.addr_size;
15315}
15316
15317/* Given an index in .debug_addr, fetch the value.
15318 NOTE: This can be called during dwarf expression evaluation,
15319 long after the debug information has been read, and thus per_cu->cu
15320 may no longer exist. */
15321
15322CORE_ADDR
15323dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15324 unsigned int addr_index)
15325{
15326 struct objfile *objfile = per_cu->objfile;
15327 struct dwarf2_cu *cu = per_cu->cu;
15328 ULONGEST addr_base;
15329 int addr_size;
15330
15331 /* This is intended to be called from outside this file. */
15332 dw2_setup (objfile);
15333
15334 /* We need addr_base and addr_size.
15335 If we don't have PER_CU->cu, we have to get it.
15336 Nasty, but the alternative is storing the needed info in PER_CU,
15337 which at this point doesn't seem justified: it's not clear how frequently
15338 it would get used and it would increase the size of every PER_CU.
15339 Entry points like dwarf2_per_cu_addr_size do a similar thing
15340 so we're not in uncharted territory here.
15341 Alas we need to be a bit more complicated as addr_base is contained
15342 in the DIE.
15343
15344 We don't need to read the entire CU(/TU).
15345 We just need the header and top level die.
a1b64ce1 15346
3019eac3 15347 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15348 For now we skip this optimization. */
3019eac3
DE
15349
15350 if (cu != NULL)
15351 {
15352 addr_base = cu->addr_base;
15353 addr_size = cu->header.addr_size;
15354 }
15355 else
15356 {
15357 struct dwarf2_read_addr_index_data aidata;
15358
a1b64ce1
DE
15359 /* Note: We can't use init_cutu_and_read_dies_simple here,
15360 we need addr_base. */
15361 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15362 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15363 addr_base = aidata.addr_base;
15364 addr_size = aidata.addr_size;
15365 }
15366
15367 return read_addr_index_1 (addr_index, addr_base, addr_size);
15368}
15369
15370/* Given a DW_AT_str_index, fetch the string. */
15371
d521ce57 15372static const char *
3019eac3
DE
15373read_str_index (const struct die_reader_specs *reader,
15374 struct dwarf2_cu *cu, ULONGEST str_index)
15375{
15376 struct objfile *objfile = dwarf2_per_objfile->objfile;
15377 const char *dwo_name = objfile->name;
15378 bfd *abfd = objfile->obfd;
15379 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15380 const gdb_byte *info_ptr;
3019eac3
DE
15381 ULONGEST str_offset;
15382
15383 dwarf2_read_section (objfile, &sections->str);
15384 dwarf2_read_section (objfile, &sections->str_offsets);
15385 if (sections->str.buffer == NULL)
15386 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15387 " in CU at offset 0x%lx [in module %s]"),
15388 (long) cu->header.offset.sect_off, dwo_name);
15389 if (sections->str_offsets.buffer == NULL)
15390 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15391 " in CU at offset 0x%lx [in module %s]"),
15392 (long) cu->header.offset.sect_off, dwo_name);
15393 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15394 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15395 " section in CU at offset 0x%lx [in module %s]"),
15396 (long) cu->header.offset.sect_off, dwo_name);
15397 info_ptr = (sections->str_offsets.buffer
15398 + str_index * cu->header.offset_size);
15399 if (cu->header.offset_size == 4)
15400 str_offset = bfd_get_32 (abfd, info_ptr);
15401 else
15402 str_offset = bfd_get_64 (abfd, info_ptr);
15403 if (str_offset >= sections->str.size)
15404 error (_("Offset from DW_FORM_str_index pointing outside of"
15405 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15406 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15407 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15408}
15409
3019eac3
DE
15410/* Return the length of an LEB128 number in BUF. */
15411
15412static int
15413leb128_size (const gdb_byte *buf)
15414{
15415 const gdb_byte *begin = buf;
15416 gdb_byte byte;
15417
15418 while (1)
15419 {
15420 byte = *buf++;
15421 if ((byte & 128) == 0)
15422 return buf - begin;
15423 }
15424}
15425
c906108c 15426static void
e142c38c 15427set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15428{
15429 switch (lang)
15430 {
15431 case DW_LANG_C89:
76bee0cc 15432 case DW_LANG_C99:
c906108c 15433 case DW_LANG_C:
d1be3247 15434 case DW_LANG_UPC:
e142c38c 15435 cu->language = language_c;
c906108c
SS
15436 break;
15437 case DW_LANG_C_plus_plus:
e142c38c 15438 cu->language = language_cplus;
c906108c 15439 break;
6aecb9c2
JB
15440 case DW_LANG_D:
15441 cu->language = language_d;
15442 break;
c906108c
SS
15443 case DW_LANG_Fortran77:
15444 case DW_LANG_Fortran90:
b21b22e0 15445 case DW_LANG_Fortran95:
e142c38c 15446 cu->language = language_fortran;
c906108c 15447 break;
a766d390
DE
15448 case DW_LANG_Go:
15449 cu->language = language_go;
15450 break;
c906108c 15451 case DW_LANG_Mips_Assembler:
e142c38c 15452 cu->language = language_asm;
c906108c 15453 break;
bebd888e 15454 case DW_LANG_Java:
e142c38c 15455 cu->language = language_java;
bebd888e 15456 break;
c906108c 15457 case DW_LANG_Ada83:
8aaf0b47 15458 case DW_LANG_Ada95:
bc5f45f8
JB
15459 cu->language = language_ada;
15460 break;
72019c9c
GM
15461 case DW_LANG_Modula2:
15462 cu->language = language_m2;
15463 break;
fe8e67fd
PM
15464 case DW_LANG_Pascal83:
15465 cu->language = language_pascal;
15466 break;
22566fbd
DJ
15467 case DW_LANG_ObjC:
15468 cu->language = language_objc;
15469 break;
c906108c
SS
15470 case DW_LANG_Cobol74:
15471 case DW_LANG_Cobol85:
c906108c 15472 default:
e142c38c 15473 cu->language = language_minimal;
c906108c
SS
15474 break;
15475 }
e142c38c 15476 cu->language_defn = language_def (cu->language);
c906108c
SS
15477}
15478
15479/* Return the named attribute or NULL if not there. */
15480
15481static struct attribute *
e142c38c 15482dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15483{
a48e046c 15484 for (;;)
c906108c 15485 {
a48e046c
TT
15486 unsigned int i;
15487 struct attribute *spec = NULL;
15488
15489 for (i = 0; i < die->num_attrs; ++i)
15490 {
15491 if (die->attrs[i].name == name)
15492 return &die->attrs[i];
15493 if (die->attrs[i].name == DW_AT_specification
15494 || die->attrs[i].name == DW_AT_abstract_origin)
15495 spec = &die->attrs[i];
15496 }
15497
15498 if (!spec)
15499 break;
c906108c 15500
f2f0e013 15501 die = follow_die_ref (die, spec, &cu);
f2f0e013 15502 }
c5aa993b 15503
c906108c
SS
15504 return NULL;
15505}
15506
348e048f
DE
15507/* Return the named attribute or NULL if not there,
15508 but do not follow DW_AT_specification, etc.
15509 This is for use in contexts where we're reading .debug_types dies.
15510 Following DW_AT_specification, DW_AT_abstract_origin will take us
15511 back up the chain, and we want to go down. */
15512
15513static struct attribute *
45e58e77 15514dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15515{
15516 unsigned int i;
15517
15518 for (i = 0; i < die->num_attrs; ++i)
15519 if (die->attrs[i].name == name)
15520 return &die->attrs[i];
15521
15522 return NULL;
15523}
15524
05cf31d1
JB
15525/* Return non-zero iff the attribute NAME is defined for the given DIE,
15526 and holds a non-zero value. This function should only be used for
2dc7f7b3 15527 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15528
15529static int
15530dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15531{
15532 struct attribute *attr = dwarf2_attr (die, name, cu);
15533
15534 return (attr && DW_UNSND (attr));
15535}
15536
3ca72b44 15537static int
e142c38c 15538die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15539{
05cf31d1
JB
15540 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15541 which value is non-zero. However, we have to be careful with
15542 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15543 (via dwarf2_flag_true_p) follows this attribute. So we may
15544 end up accidently finding a declaration attribute that belongs
15545 to a different DIE referenced by the specification attribute,
15546 even though the given DIE does not have a declaration attribute. */
15547 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15548 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15549}
15550
63d06c5c 15551/* Return the die giving the specification for DIE, if there is
f2f0e013 15552 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15553 containing the return value on output. If there is no
15554 specification, but there is an abstract origin, that is
15555 returned. */
63d06c5c
DC
15556
15557static struct die_info *
f2f0e013 15558die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15559{
f2f0e013
DJ
15560 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15561 *spec_cu);
63d06c5c 15562
edb3359d
DJ
15563 if (spec_attr == NULL)
15564 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15565
63d06c5c
DC
15566 if (spec_attr == NULL)
15567 return NULL;
15568 else
f2f0e013 15569 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15570}
c906108c 15571
debd256d 15572/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15573 refers to.
15574 NOTE: This is also used as a "cleanup" function. */
15575
debd256d
JB
15576static void
15577free_line_header (struct line_header *lh)
15578{
15579 if (lh->standard_opcode_lengths)
a8bc7b56 15580 xfree (lh->standard_opcode_lengths);
debd256d
JB
15581
15582 /* Remember that all the lh->file_names[i].name pointers are
15583 pointers into debug_line_buffer, and don't need to be freed. */
15584 if (lh->file_names)
a8bc7b56 15585 xfree (lh->file_names);
debd256d
JB
15586
15587 /* Similarly for the include directory names. */
15588 if (lh->include_dirs)
a8bc7b56 15589 xfree (lh->include_dirs);
debd256d 15590
a8bc7b56 15591 xfree (lh);
debd256d
JB
15592}
15593
debd256d 15594/* Add an entry to LH's include directory table. */
ae2de4f8 15595
debd256d 15596static void
d521ce57 15597add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15598{
debd256d
JB
15599 /* Grow the array if necessary. */
15600 if (lh->include_dirs_size == 0)
c5aa993b 15601 {
debd256d
JB
15602 lh->include_dirs_size = 1; /* for testing */
15603 lh->include_dirs = xmalloc (lh->include_dirs_size
15604 * sizeof (*lh->include_dirs));
15605 }
15606 else if (lh->num_include_dirs >= lh->include_dirs_size)
15607 {
15608 lh->include_dirs_size *= 2;
15609 lh->include_dirs = xrealloc (lh->include_dirs,
15610 (lh->include_dirs_size
15611 * sizeof (*lh->include_dirs)));
c5aa993b 15612 }
c906108c 15613
debd256d
JB
15614 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15615}
6e70227d 15616
debd256d 15617/* Add an entry to LH's file name table. */
ae2de4f8 15618
debd256d
JB
15619static void
15620add_file_name (struct line_header *lh,
d521ce57 15621 const char *name,
debd256d
JB
15622 unsigned int dir_index,
15623 unsigned int mod_time,
15624 unsigned int length)
15625{
15626 struct file_entry *fe;
15627
15628 /* Grow the array if necessary. */
15629 if (lh->file_names_size == 0)
15630 {
15631 lh->file_names_size = 1; /* for testing */
15632 lh->file_names = xmalloc (lh->file_names_size
15633 * sizeof (*lh->file_names));
15634 }
15635 else if (lh->num_file_names >= lh->file_names_size)
15636 {
15637 lh->file_names_size *= 2;
15638 lh->file_names = xrealloc (lh->file_names,
15639 (lh->file_names_size
15640 * sizeof (*lh->file_names)));
15641 }
15642
15643 fe = &lh->file_names[lh->num_file_names++];
15644 fe->name = name;
15645 fe->dir_index = dir_index;
15646 fe->mod_time = mod_time;
15647 fe->length = length;
aaa75496 15648 fe->included_p = 0;
cb1df416 15649 fe->symtab = NULL;
debd256d 15650}
6e70227d 15651
36586728
TT
15652/* A convenience function to find the proper .debug_line section for a
15653 CU. */
15654
15655static struct dwarf2_section_info *
15656get_debug_line_section (struct dwarf2_cu *cu)
15657{
15658 struct dwarf2_section_info *section;
15659
15660 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15661 DWO file. */
15662 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15663 section = &cu->dwo_unit->dwo_file->sections.line;
15664 else if (cu->per_cu->is_dwz)
15665 {
15666 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15667
15668 section = &dwz->line;
15669 }
15670 else
15671 section = &dwarf2_per_objfile->line;
15672
15673 return section;
15674}
15675
debd256d 15676/* Read the statement program header starting at OFFSET in
3019eac3 15677 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15678 to a struct line_header, allocated using xmalloc.
debd256d
JB
15679
15680 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15681 the returned object point into the dwarf line section buffer,
15682 and must not be freed. */
ae2de4f8 15683
debd256d 15684static struct line_header *
3019eac3 15685dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15686{
15687 struct cleanup *back_to;
15688 struct line_header *lh;
d521ce57 15689 const gdb_byte *line_ptr;
c764a876 15690 unsigned int bytes_read, offset_size;
debd256d 15691 int i;
d521ce57 15692 const char *cur_dir, *cur_file;
3019eac3
DE
15693 struct dwarf2_section_info *section;
15694 bfd *abfd;
15695
36586728 15696 section = get_debug_line_section (cu);
3019eac3
DE
15697 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15698 if (section->buffer == NULL)
debd256d 15699 {
3019eac3
DE
15700 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15701 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15702 else
15703 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15704 return 0;
15705 }
15706
fceca515
DE
15707 /* We can't do this until we know the section is non-empty.
15708 Only then do we know we have such a section. */
15709 abfd = section->asection->owner;
15710
a738430d
MK
15711 /* Make sure that at least there's room for the total_length field.
15712 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15713 if (offset + 4 >= section->size)
debd256d 15714 {
4d3c2250 15715 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15716 return 0;
15717 }
15718
15719 lh = xmalloc (sizeof (*lh));
15720 memset (lh, 0, sizeof (*lh));
15721 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15722 (void *) lh);
15723
3019eac3 15724 line_ptr = section->buffer + offset;
debd256d 15725
a738430d 15726 /* Read in the header. */
6e70227d 15727 lh->total_length =
c764a876
DE
15728 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15729 &bytes_read, &offset_size);
debd256d 15730 line_ptr += bytes_read;
3019eac3 15731 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15732 {
4d3c2250 15733 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15734 do_cleanups (back_to);
debd256d
JB
15735 return 0;
15736 }
15737 lh->statement_program_end = line_ptr + lh->total_length;
15738 lh->version = read_2_bytes (abfd, line_ptr);
15739 line_ptr += 2;
c764a876
DE
15740 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15741 line_ptr += offset_size;
debd256d
JB
15742 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15743 line_ptr += 1;
2dc7f7b3
TT
15744 if (lh->version >= 4)
15745 {
15746 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15747 line_ptr += 1;
15748 }
15749 else
15750 lh->maximum_ops_per_instruction = 1;
15751
15752 if (lh->maximum_ops_per_instruction == 0)
15753 {
15754 lh->maximum_ops_per_instruction = 1;
15755 complaint (&symfile_complaints,
3e43a32a
MS
15756 _("invalid maximum_ops_per_instruction "
15757 "in `.debug_line' section"));
2dc7f7b3
TT
15758 }
15759
debd256d
JB
15760 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15761 line_ptr += 1;
15762 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15763 line_ptr += 1;
15764 lh->line_range = read_1_byte (abfd, line_ptr);
15765 line_ptr += 1;
15766 lh->opcode_base = read_1_byte (abfd, line_ptr);
15767 line_ptr += 1;
15768 lh->standard_opcode_lengths
fe1b8b76 15769 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15770
15771 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15772 for (i = 1; i < lh->opcode_base; ++i)
15773 {
15774 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15775 line_ptr += 1;
15776 }
15777
a738430d 15778 /* Read directory table. */
9b1c24c8 15779 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15780 {
15781 line_ptr += bytes_read;
15782 add_include_dir (lh, cur_dir);
15783 }
15784 line_ptr += bytes_read;
15785
a738430d 15786 /* Read file name table. */
9b1c24c8 15787 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15788 {
15789 unsigned int dir_index, mod_time, length;
15790
15791 line_ptr += bytes_read;
15792 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15793 line_ptr += bytes_read;
15794 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15795 line_ptr += bytes_read;
15796 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15797 line_ptr += bytes_read;
15798
15799 add_file_name (lh, cur_file, dir_index, mod_time, length);
15800 }
15801 line_ptr += bytes_read;
6e70227d 15802 lh->statement_program_start = line_ptr;
debd256d 15803
3019eac3 15804 if (line_ptr > (section->buffer + section->size))
4d3c2250 15805 complaint (&symfile_complaints,
3e43a32a
MS
15806 _("line number info header doesn't "
15807 "fit in `.debug_line' section"));
debd256d
JB
15808
15809 discard_cleanups (back_to);
15810 return lh;
15811}
c906108c 15812
c6da4cef
DE
15813/* Subroutine of dwarf_decode_lines to simplify it.
15814 Return the file name of the psymtab for included file FILE_INDEX
15815 in line header LH of PST.
15816 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15817 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15818 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15819
15820 The function creates dangling cleanup registration. */
c6da4cef 15821
d521ce57 15822static const char *
c6da4cef
DE
15823psymtab_include_file_name (const struct line_header *lh, int file_index,
15824 const struct partial_symtab *pst,
15825 const char *comp_dir)
15826{
15827 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15828 const char *include_name = fe.name;
15829 const char *include_name_to_compare = include_name;
15830 const char *dir_name = NULL;
72b9f47f
TT
15831 const char *pst_filename;
15832 char *copied_name = NULL;
c6da4cef
DE
15833 int file_is_pst;
15834
15835 if (fe.dir_index)
15836 dir_name = lh->include_dirs[fe.dir_index - 1];
15837
15838 if (!IS_ABSOLUTE_PATH (include_name)
15839 && (dir_name != NULL || comp_dir != NULL))
15840 {
15841 /* Avoid creating a duplicate psymtab for PST.
15842 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15843 Before we do the comparison, however, we need to account
15844 for DIR_NAME and COMP_DIR.
15845 First prepend dir_name (if non-NULL). If we still don't
15846 have an absolute path prepend comp_dir (if non-NULL).
15847 However, the directory we record in the include-file's
15848 psymtab does not contain COMP_DIR (to match the
15849 corresponding symtab(s)).
15850
15851 Example:
15852
15853 bash$ cd /tmp
15854 bash$ gcc -g ./hello.c
15855 include_name = "hello.c"
15856 dir_name = "."
15857 DW_AT_comp_dir = comp_dir = "/tmp"
15858 DW_AT_name = "./hello.c" */
15859
15860 if (dir_name != NULL)
15861 {
d521ce57
TT
15862 char *tem = concat (dir_name, SLASH_STRING,
15863 include_name, (char *)NULL);
15864
15865 make_cleanup (xfree, tem);
15866 include_name = tem;
c6da4cef 15867 include_name_to_compare = include_name;
c6da4cef
DE
15868 }
15869 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15870 {
d521ce57
TT
15871 char *tem = concat (comp_dir, SLASH_STRING,
15872 include_name, (char *)NULL);
15873
15874 make_cleanup (xfree, tem);
15875 include_name_to_compare = tem;
c6da4cef
DE
15876 }
15877 }
15878
15879 pst_filename = pst->filename;
15880 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15881 {
72b9f47f
TT
15882 copied_name = concat (pst->dirname, SLASH_STRING,
15883 pst_filename, (char *)NULL);
15884 pst_filename = copied_name;
c6da4cef
DE
15885 }
15886
1e3fad37 15887 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15888
72b9f47f
TT
15889 if (copied_name != NULL)
15890 xfree (copied_name);
c6da4cef
DE
15891
15892 if (file_is_pst)
15893 return NULL;
15894 return include_name;
15895}
15896
c91513d8
PP
15897/* Ignore this record_line request. */
15898
15899static void
15900noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15901{
15902 return;
15903}
15904
f3f5162e
DE
15905/* Subroutine of dwarf_decode_lines to simplify it.
15906 Process the line number information in LH. */
debd256d 15907
c906108c 15908static void
f3f5162e
DE
15909dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15910 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15911{
d521ce57
TT
15912 const gdb_byte *line_ptr, *extended_end;
15913 const gdb_byte *line_end;
a8c50c1f 15914 unsigned int bytes_read, extended_len;
c906108c 15915 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15916 CORE_ADDR baseaddr;
15917 struct objfile *objfile = cu->objfile;
f3f5162e 15918 bfd *abfd = objfile->obfd;
fbf65064 15919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15920 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15921 struct subfile *last_subfile = NULL;
c91513d8
PP
15922 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15923 = record_line;
e142c38c
DJ
15924
15925 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15926
debd256d
JB
15927 line_ptr = lh->statement_program_start;
15928 line_end = lh->statement_program_end;
c906108c
SS
15929
15930 /* Read the statement sequences until there's nothing left. */
15931 while (line_ptr < line_end)
15932 {
15933 /* state machine registers */
15934 CORE_ADDR address = 0;
15935 unsigned int file = 1;
15936 unsigned int line = 1;
15937 unsigned int column = 0;
debd256d 15938 int is_stmt = lh->default_is_stmt;
c906108c
SS
15939 int basic_block = 0;
15940 int end_sequence = 0;
fbf65064 15941 CORE_ADDR addr;
2dc7f7b3 15942 unsigned char op_index = 0;
c906108c 15943
aaa75496 15944 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15945 {
aaa75496 15946 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15947 /* lh->include_dirs and lh->file_names are 0-based, but the
15948 directory and file name numbers in the statement program
15949 are 1-based. */
15950 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 15951 const char *dir = NULL;
a738430d 15952
debd256d
JB
15953 if (fe->dir_index)
15954 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
15955
15956 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
15957 }
15958
a738430d 15959 /* Decode the table. */
c5aa993b 15960 while (!end_sequence)
c906108c
SS
15961 {
15962 op_code = read_1_byte (abfd, line_ptr);
15963 line_ptr += 1;
59205f5a
JB
15964 if (line_ptr > line_end)
15965 {
15966 dwarf2_debug_line_missing_end_sequence_complaint ();
15967 break;
15968 }
9aa1fe7e 15969
debd256d 15970 if (op_code >= lh->opcode_base)
6e70227d 15971 {
a738430d 15972 /* Special operand. */
debd256d 15973 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
15974 address += (((op_index + (adj_opcode / lh->line_range))
15975 / lh->maximum_ops_per_instruction)
15976 * lh->minimum_instruction_length);
15977 op_index = ((op_index + (adj_opcode / lh->line_range))
15978 % lh->maximum_ops_per_instruction);
debd256d 15979 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 15980 if (lh->num_file_names < file || file == 0)
25e43795 15981 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
15982 /* For now we ignore lines not starting on an
15983 instruction boundary. */
15984 else if (op_index == 0)
25e43795
DJ
15985 {
15986 lh->file_names[file - 1].included_p = 1;
ca5f395d 15987 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
15988 {
15989 if (last_subfile != current_subfile)
15990 {
15991 addr = gdbarch_addr_bits_remove (gdbarch, address);
15992 if (last_subfile)
c91513d8 15993 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
15994 last_subfile = current_subfile;
15995 }
25e43795 15996 /* Append row to matrix using current values. */
7019d805 15997 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 15998 (*p_record_line) (current_subfile, line, addr);
366da635 15999 }
25e43795 16000 }
ca5f395d 16001 basic_block = 0;
9aa1fe7e
GK
16002 }
16003 else switch (op_code)
c906108c
SS
16004 {
16005 case DW_LNS_extended_op:
3e43a32a
MS
16006 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16007 &bytes_read);
473b7be6 16008 line_ptr += bytes_read;
a8c50c1f 16009 extended_end = line_ptr + extended_len;
c906108c
SS
16010 extended_op = read_1_byte (abfd, line_ptr);
16011 line_ptr += 1;
16012 switch (extended_op)
16013 {
16014 case DW_LNE_end_sequence:
c91513d8 16015 p_record_line = record_line;
c906108c 16016 end_sequence = 1;
c906108c
SS
16017 break;
16018 case DW_LNE_set_address:
e7c27a73 16019 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
16020
16021 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16022 {
16023 /* This line table is for a function which has been
16024 GCd by the linker. Ignore it. PR gdb/12528 */
16025
16026 long line_offset
36586728 16027 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16028
16029 complaint (&symfile_complaints,
16030 _(".debug_line address at offset 0x%lx is 0 "
16031 "[in module %s]"),
bb5ed363 16032 line_offset, objfile->name);
c91513d8
PP
16033 p_record_line = noop_record_line;
16034 }
16035
2dc7f7b3 16036 op_index = 0;
107d2387
AC
16037 line_ptr += bytes_read;
16038 address += baseaddr;
c906108c
SS
16039 break;
16040 case DW_LNE_define_file:
debd256d 16041 {
d521ce57 16042 const char *cur_file;
debd256d 16043 unsigned int dir_index, mod_time, length;
6e70227d 16044
3e43a32a
MS
16045 cur_file = read_direct_string (abfd, line_ptr,
16046 &bytes_read);
debd256d
JB
16047 line_ptr += bytes_read;
16048 dir_index =
16049 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16050 line_ptr += bytes_read;
16051 mod_time =
16052 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16053 line_ptr += bytes_read;
16054 length =
16055 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16056 line_ptr += bytes_read;
16057 add_file_name (lh, cur_file, dir_index, mod_time, length);
16058 }
c906108c 16059 break;
d0c6ba3d
CC
16060 case DW_LNE_set_discriminator:
16061 /* The discriminator is not interesting to the debugger;
16062 just ignore it. */
16063 line_ptr = extended_end;
16064 break;
c906108c 16065 default:
4d3c2250 16066 complaint (&symfile_complaints,
e2e0b3e5 16067 _("mangled .debug_line section"));
debd256d 16068 return;
c906108c 16069 }
a8c50c1f
DJ
16070 /* Make sure that we parsed the extended op correctly. If e.g.
16071 we expected a different address size than the producer used,
16072 we may have read the wrong number of bytes. */
16073 if (line_ptr != extended_end)
16074 {
16075 complaint (&symfile_complaints,
16076 _("mangled .debug_line section"));
16077 return;
16078 }
c906108c
SS
16079 break;
16080 case DW_LNS_copy:
59205f5a 16081 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16082 dwarf2_debug_line_missing_file_complaint ();
16083 else
366da635 16084 {
25e43795 16085 lh->file_names[file - 1].included_p = 1;
ca5f395d 16086 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16087 {
16088 if (last_subfile != current_subfile)
16089 {
16090 addr = gdbarch_addr_bits_remove (gdbarch, address);
16091 if (last_subfile)
c91513d8 16092 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16093 last_subfile = current_subfile;
16094 }
7019d805 16095 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16096 (*p_record_line) (current_subfile, line, addr);
fbf65064 16097 }
366da635 16098 }
c906108c
SS
16099 basic_block = 0;
16100 break;
16101 case DW_LNS_advance_pc:
2dc7f7b3
TT
16102 {
16103 CORE_ADDR adjust
16104 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16105
16106 address += (((op_index + adjust)
16107 / lh->maximum_ops_per_instruction)
16108 * lh->minimum_instruction_length);
16109 op_index = ((op_index + adjust)
16110 % lh->maximum_ops_per_instruction);
16111 line_ptr += bytes_read;
16112 }
c906108c
SS
16113 break;
16114 case DW_LNS_advance_line:
16115 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16116 line_ptr += bytes_read;
16117 break;
16118 case DW_LNS_set_file:
debd256d 16119 {
a738430d
MK
16120 /* The arrays lh->include_dirs and lh->file_names are
16121 0-based, but the directory and file name numbers in
16122 the statement program are 1-based. */
debd256d 16123 struct file_entry *fe;
d521ce57 16124 const char *dir = NULL;
a738430d 16125
debd256d
JB
16126 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16127 line_ptr += bytes_read;
59205f5a 16128 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16129 dwarf2_debug_line_missing_file_complaint ();
16130 else
16131 {
16132 fe = &lh->file_names[file - 1];
16133 if (fe->dir_index)
16134 dir = lh->include_dirs[fe->dir_index - 1];
16135 if (!decode_for_pst_p)
16136 {
16137 last_subfile = current_subfile;
16138 dwarf2_start_subfile (fe->name, dir, comp_dir);
16139 }
16140 }
debd256d 16141 }
c906108c
SS
16142 break;
16143 case DW_LNS_set_column:
16144 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16145 line_ptr += bytes_read;
16146 break;
16147 case DW_LNS_negate_stmt:
16148 is_stmt = (!is_stmt);
16149 break;
16150 case DW_LNS_set_basic_block:
16151 basic_block = 1;
16152 break;
c2c6d25f
JM
16153 /* Add to the address register of the state machine the
16154 address increment value corresponding to special opcode
a738430d
MK
16155 255. I.e., this value is scaled by the minimum
16156 instruction length since special opcode 255 would have
b021a221 16157 scaled the increment. */
c906108c 16158 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16159 {
16160 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16161
16162 address += (((op_index + adjust)
16163 / lh->maximum_ops_per_instruction)
16164 * lh->minimum_instruction_length);
16165 op_index = ((op_index + adjust)
16166 % lh->maximum_ops_per_instruction);
16167 }
c906108c
SS
16168 break;
16169 case DW_LNS_fixed_advance_pc:
16170 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16171 op_index = 0;
c906108c
SS
16172 line_ptr += 2;
16173 break;
9aa1fe7e 16174 default:
a738430d
MK
16175 {
16176 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16177 int i;
a738430d 16178
debd256d 16179 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16180 {
16181 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16182 line_ptr += bytes_read;
16183 }
16184 }
c906108c
SS
16185 }
16186 }
59205f5a
JB
16187 if (lh->num_file_names < file || file == 0)
16188 dwarf2_debug_line_missing_file_complaint ();
16189 else
16190 {
16191 lh->file_names[file - 1].included_p = 1;
16192 if (!decode_for_pst_p)
fbf65064
UW
16193 {
16194 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16195 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16196 }
59205f5a 16197 }
c906108c 16198 }
f3f5162e
DE
16199}
16200
16201/* Decode the Line Number Program (LNP) for the given line_header
16202 structure and CU. The actual information extracted and the type
16203 of structures created from the LNP depends on the value of PST.
16204
16205 1. If PST is NULL, then this procedure uses the data from the program
16206 to create all necessary symbol tables, and their linetables.
16207
16208 2. If PST is not NULL, this procedure reads the program to determine
16209 the list of files included by the unit represented by PST, and
16210 builds all the associated partial symbol tables.
16211
16212 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16213 It is used for relative paths in the line table.
16214 NOTE: When processing partial symtabs (pst != NULL),
16215 comp_dir == pst->dirname.
16216
16217 NOTE: It is important that psymtabs have the same file name (via strcmp)
16218 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16219 symtab we don't use it in the name of the psymtabs we create.
16220 E.g. expand_line_sal requires this when finding psymtabs to expand.
16221 A good testcase for this is mb-inline.exp. */
16222
16223static void
16224dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16225 struct dwarf2_cu *cu, struct partial_symtab *pst,
16226 int want_line_info)
16227{
16228 struct objfile *objfile = cu->objfile;
16229 const int decode_for_pst_p = (pst != NULL);
16230 struct subfile *first_subfile = current_subfile;
16231
16232 if (want_line_info)
16233 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16234
16235 if (decode_for_pst_p)
16236 {
16237 int file_index;
16238
16239 /* Now that we're done scanning the Line Header Program, we can
16240 create the psymtab of each included file. */
16241 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16242 if (lh->file_names[file_index].included_p == 1)
16243 {
d521ce57 16244 const char *include_name =
c6da4cef
DE
16245 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16246 if (include_name != NULL)
aaa75496
JB
16247 dwarf2_create_include_psymtab (include_name, pst, objfile);
16248 }
16249 }
cb1df416
DJ
16250 else
16251 {
16252 /* Make sure a symtab is created for every file, even files
16253 which contain only variables (i.e. no code with associated
16254 line numbers). */
cb1df416 16255 int i;
cb1df416
DJ
16256
16257 for (i = 0; i < lh->num_file_names; i++)
16258 {
d521ce57 16259 const char *dir = NULL;
f3f5162e 16260 struct file_entry *fe;
9a619af0 16261
cb1df416
DJ
16262 fe = &lh->file_names[i];
16263 if (fe->dir_index)
16264 dir = lh->include_dirs[fe->dir_index - 1];
16265 dwarf2_start_subfile (fe->name, dir, comp_dir);
16266
16267 /* Skip the main file; we don't need it, and it must be
16268 allocated last, so that it will show up before the
16269 non-primary symtabs in the objfile's symtab list. */
16270 if (current_subfile == first_subfile)
16271 continue;
16272
16273 if (current_subfile->symtab == NULL)
16274 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16275 objfile);
cb1df416
DJ
16276 fe->symtab = current_subfile->symtab;
16277 }
16278 }
c906108c
SS
16279}
16280
16281/* Start a subfile for DWARF. FILENAME is the name of the file and
16282 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16283 or NULL if not known. COMP_DIR is the compilation directory for the
16284 linetable's compilation unit or NULL if not known.
c906108c
SS
16285 This routine tries to keep line numbers from identical absolute and
16286 relative file names in a common subfile.
16287
16288 Using the `list' example from the GDB testsuite, which resides in
16289 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16290 of /srcdir/list0.c yields the following debugging information for list0.c:
16291
c5aa993b
JM
16292 DW_AT_name: /srcdir/list0.c
16293 DW_AT_comp_dir: /compdir
357e46e7 16294 files.files[0].name: list0.h
c5aa993b 16295 files.files[0].dir: /srcdir
357e46e7 16296 files.files[1].name: list0.c
c5aa993b 16297 files.files[1].dir: /srcdir
c906108c
SS
16298
16299 The line number information for list0.c has to end up in a single
4f1520fb
FR
16300 subfile, so that `break /srcdir/list0.c:1' works as expected.
16301 start_subfile will ensure that this happens provided that we pass the
16302 concatenation of files.files[1].dir and files.files[1].name as the
16303 subfile's name. */
c906108c
SS
16304
16305static void
d521ce57 16306dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16307 const char *comp_dir)
c906108c 16308{
d521ce57 16309 char *copy = NULL;
4f1520fb
FR
16310
16311 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16312 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16313 second argument to start_subfile. To be consistent, we do the
16314 same here. In order not to lose the line information directory,
16315 we concatenate it to the filename when it makes sense.
16316 Note that the Dwarf3 standard says (speaking of filenames in line
16317 information): ``The directory index is ignored for file names
16318 that represent full path names''. Thus ignoring dirname in the
16319 `else' branch below isn't an issue. */
c906108c 16320
d5166ae1 16321 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16322 {
16323 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16324 filename = copy;
16325 }
c906108c 16326
d521ce57 16327 start_subfile (filename, comp_dir);
4f1520fb 16328
d521ce57
TT
16329 if (copy != NULL)
16330 xfree (copy);
c906108c
SS
16331}
16332
f4dc4d17
DE
16333/* Start a symtab for DWARF.
16334 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16335
16336static void
16337dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16338 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16339{
16340 start_symtab (name, comp_dir, low_pc);
16341 record_debugformat ("DWARF 2");
16342 record_producer (cu->producer);
16343
16344 /* We assume that we're processing GCC output. */
16345 processing_gcc_compilation = 2;
16346
4d4ec4e5 16347 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16348}
16349
4c2df51b
DJ
16350static void
16351var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16352 struct dwarf2_cu *cu)
4c2df51b 16353{
e7c27a73
DJ
16354 struct objfile *objfile = cu->objfile;
16355 struct comp_unit_head *cu_header = &cu->header;
16356
4c2df51b
DJ
16357 /* NOTE drow/2003-01-30: There used to be a comment and some special
16358 code here to turn a symbol with DW_AT_external and a
16359 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16360 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16361 with some versions of binutils) where shared libraries could have
16362 relocations against symbols in their debug information - the
16363 minimal symbol would have the right address, but the debug info
16364 would not. It's no longer necessary, because we will explicitly
16365 apply relocations when we read in the debug information now. */
16366
16367 /* A DW_AT_location attribute with no contents indicates that a
16368 variable has been optimized away. */
16369 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16370 {
f1e6e072 16371 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16372 return;
16373 }
16374
16375 /* Handle one degenerate form of location expression specially, to
16376 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16377 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16378 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16379
16380 if (attr_form_is_block (attr)
3019eac3
DE
16381 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16382 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16383 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16384 && (DW_BLOCK (attr)->size
16385 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16386 {
891d2f0b 16387 unsigned int dummy;
4c2df51b 16388
3019eac3
DE
16389 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16390 SYMBOL_VALUE_ADDRESS (sym) =
16391 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16392 else
16393 SYMBOL_VALUE_ADDRESS (sym) =
16394 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16395 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16396 fixup_symbol_section (sym, objfile);
16397 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16398 SYMBOL_SECTION (sym));
4c2df51b
DJ
16399 return;
16400 }
16401
16402 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16403 expression evaluator, and use LOC_COMPUTED only when necessary
16404 (i.e. when the value of a register or memory location is
16405 referenced, or a thread-local block, etc.). Then again, it might
16406 not be worthwhile. I'm assuming that it isn't unless performance
16407 or memory numbers show me otherwise. */
16408
f1e6e072 16409 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16410
f1e6e072 16411 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16412 cu->has_loclist = 1;
4c2df51b
DJ
16413}
16414
c906108c
SS
16415/* Given a pointer to a DWARF information entry, figure out if we need
16416 to make a symbol table entry for it, and if so, create a new entry
16417 and return a pointer to it.
16418 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16419 used the passed type.
16420 If SPACE is not NULL, use it to hold the new symbol. If it is
16421 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16422
16423static struct symbol *
34eaf542
TT
16424new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16425 struct symbol *space)
c906108c 16426{
e7c27a73 16427 struct objfile *objfile = cu->objfile;
c906108c 16428 struct symbol *sym = NULL;
15d034d0 16429 const char *name;
c906108c
SS
16430 struct attribute *attr = NULL;
16431 struct attribute *attr2 = NULL;
e142c38c 16432 CORE_ADDR baseaddr;
e37fd15a
SW
16433 struct pending **list_to_add = NULL;
16434
edb3359d 16435 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16436
16437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16438
94af9270 16439 name = dwarf2_name (die, cu);
c906108c
SS
16440 if (name)
16441 {
94af9270 16442 const char *linkagename;
34eaf542 16443 int suppress_add = 0;
94af9270 16444
34eaf542
TT
16445 if (space)
16446 sym = space;
16447 else
e623cf5d 16448 sym = allocate_symbol (objfile);
c906108c 16449 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16450
16451 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16452 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16453 linkagename = dwarf2_physname (name, die, cu);
16454 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16455
f55ee35c
JK
16456 /* Fortran does not have mangling standard and the mangling does differ
16457 between gfortran, iFort etc. */
16458 if (cu->language == language_fortran
b250c185 16459 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16460 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16461 dwarf2_full_name (name, die, cu),
29df156d 16462 NULL);
f55ee35c 16463
c906108c 16464 /* Default assumptions.
c5aa993b 16465 Use the passed type or decode it from the die. */
176620f1 16466 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16467 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16468 if (type != NULL)
16469 SYMBOL_TYPE (sym) = type;
16470 else
e7c27a73 16471 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16472 attr = dwarf2_attr (die,
16473 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16474 cu);
c906108c
SS
16475 if (attr)
16476 {
16477 SYMBOL_LINE (sym) = DW_UNSND (attr);
16478 }
cb1df416 16479
edb3359d
DJ
16480 attr = dwarf2_attr (die,
16481 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16482 cu);
cb1df416
DJ
16483 if (attr)
16484 {
16485 int file_index = DW_UNSND (attr);
9a619af0 16486
cb1df416
DJ
16487 if (cu->line_header == NULL
16488 || file_index > cu->line_header->num_file_names)
16489 complaint (&symfile_complaints,
16490 _("file index out of range"));
1c3d648d 16491 else if (file_index > 0)
cb1df416
DJ
16492 {
16493 struct file_entry *fe;
9a619af0 16494
cb1df416
DJ
16495 fe = &cu->line_header->file_names[file_index - 1];
16496 SYMBOL_SYMTAB (sym) = fe->symtab;
16497 }
16498 }
16499
c906108c
SS
16500 switch (die->tag)
16501 {
16502 case DW_TAG_label:
e142c38c 16503 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16504 if (attr)
16505 {
16506 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16507 }
0f5238ed
TT
16508 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16509 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16510 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16511 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16512 break;
16513 case DW_TAG_subprogram:
16514 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16515 finish_block. */
f1e6e072 16516 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16517 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16518 if ((attr2 && (DW_UNSND (attr2) != 0))
16519 || cu->language == language_ada)
c906108c 16520 {
2cfa0c8d
JB
16521 /* Subprograms marked external are stored as a global symbol.
16522 Ada subprograms, whether marked external or not, are always
16523 stored as a global symbol, because we want to be able to
16524 access them globally. For instance, we want to be able
16525 to break on a nested subprogram without having to
16526 specify the context. */
e37fd15a 16527 list_to_add = &global_symbols;
c906108c
SS
16528 }
16529 else
16530 {
e37fd15a 16531 list_to_add = cu->list_in_scope;
c906108c
SS
16532 }
16533 break;
edb3359d
DJ
16534 case DW_TAG_inlined_subroutine:
16535 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16536 finish_block. */
f1e6e072 16537 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16538 SYMBOL_INLINED (sym) = 1;
481860b3 16539 list_to_add = cu->list_in_scope;
edb3359d 16540 break;
34eaf542
TT
16541 case DW_TAG_template_value_param:
16542 suppress_add = 1;
16543 /* Fall through. */
72929c62 16544 case DW_TAG_constant:
c906108c 16545 case DW_TAG_variable:
254e6b9e 16546 case DW_TAG_member:
0963b4bd
MS
16547 /* Compilation with minimal debug info may result in
16548 variables with missing type entries. Change the
16549 misleading `void' type to something sensible. */
c906108c 16550 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16551 SYMBOL_TYPE (sym)
46bf5051 16552 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16553
e142c38c 16554 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16555 /* In the case of DW_TAG_member, we should only be called for
16556 static const members. */
16557 if (die->tag == DW_TAG_member)
16558 {
3863f96c
DE
16559 /* dwarf2_add_field uses die_is_declaration,
16560 so we do the same. */
254e6b9e
DE
16561 gdb_assert (die_is_declaration (die, cu));
16562 gdb_assert (attr);
16563 }
c906108c
SS
16564 if (attr)
16565 {
e7c27a73 16566 dwarf2_const_value (attr, sym, cu);
e142c38c 16567 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16568 if (!suppress_add)
34eaf542
TT
16569 {
16570 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16571 list_to_add = &global_symbols;
34eaf542 16572 else
e37fd15a 16573 list_to_add = cu->list_in_scope;
34eaf542 16574 }
c906108c
SS
16575 break;
16576 }
e142c38c 16577 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16578 if (attr)
16579 {
e7c27a73 16580 var_decode_location (attr, sym, cu);
e142c38c 16581 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16582
16583 /* Fortran explicitly imports any global symbols to the local
16584 scope by DW_TAG_common_block. */
16585 if (cu->language == language_fortran && die->parent
16586 && die->parent->tag == DW_TAG_common_block)
16587 attr2 = NULL;
16588
caac4577
JG
16589 if (SYMBOL_CLASS (sym) == LOC_STATIC
16590 && SYMBOL_VALUE_ADDRESS (sym) == 0
16591 && !dwarf2_per_objfile->has_section_at_zero)
16592 {
16593 /* When a static variable is eliminated by the linker,
16594 the corresponding debug information is not stripped
16595 out, but the variable address is set to null;
16596 do not add such variables into symbol table. */
16597 }
16598 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16599 {
f55ee35c
JK
16600 /* Workaround gfortran PR debug/40040 - it uses
16601 DW_AT_location for variables in -fPIC libraries which may
16602 get overriden by other libraries/executable and get
16603 a different address. Resolve it by the minimal symbol
16604 which may come from inferior's executable using copy
16605 relocation. Make this workaround only for gfortran as for
16606 other compilers GDB cannot guess the minimal symbol
16607 Fortran mangling kind. */
16608 if (cu->language == language_fortran && die->parent
16609 && die->parent->tag == DW_TAG_module
16610 && cu->producer
16611 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16612 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16613
1c809c68
TT
16614 /* A variable with DW_AT_external is never static,
16615 but it may be block-scoped. */
16616 list_to_add = (cu->list_in_scope == &file_symbols
16617 ? &global_symbols : cu->list_in_scope);
1c809c68 16618 }
c906108c 16619 else
e37fd15a 16620 list_to_add = cu->list_in_scope;
c906108c
SS
16621 }
16622 else
16623 {
16624 /* We do not know the address of this symbol.
c5aa993b
JM
16625 If it is an external symbol and we have type information
16626 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16627 The address of the variable will then be determined from
16628 the minimal symbol table whenever the variable is
16629 referenced. */
e142c38c 16630 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16631
16632 /* Fortran explicitly imports any global symbols to the local
16633 scope by DW_TAG_common_block. */
16634 if (cu->language == language_fortran && die->parent
16635 && die->parent->tag == DW_TAG_common_block)
16636 {
16637 /* SYMBOL_CLASS doesn't matter here because
16638 read_common_block is going to reset it. */
16639 if (!suppress_add)
16640 list_to_add = cu->list_in_scope;
16641 }
16642 else if (attr2 && (DW_UNSND (attr2) != 0)
16643 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16644 {
0fe7935b
DJ
16645 /* A variable with DW_AT_external is never static, but it
16646 may be block-scoped. */
16647 list_to_add = (cu->list_in_scope == &file_symbols
16648 ? &global_symbols : cu->list_in_scope);
16649
f1e6e072 16650 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16651 }
442ddf59
JK
16652 else if (!die_is_declaration (die, cu))
16653 {
16654 /* Use the default LOC_OPTIMIZED_OUT class. */
16655 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16656 if (!suppress_add)
16657 list_to_add = cu->list_in_scope;
442ddf59 16658 }
c906108c
SS
16659 }
16660 break;
16661 case DW_TAG_formal_parameter:
edb3359d
DJ
16662 /* If we are inside a function, mark this as an argument. If
16663 not, we might be looking at an argument to an inlined function
16664 when we do not have enough information to show inlined frames;
16665 pretend it's a local variable in that case so that the user can
16666 still see it. */
16667 if (context_stack_depth > 0
16668 && context_stack[context_stack_depth - 1].name != NULL)
16669 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16670 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16671 if (attr)
16672 {
e7c27a73 16673 var_decode_location (attr, sym, cu);
c906108c 16674 }
e142c38c 16675 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16676 if (attr)
16677 {
e7c27a73 16678 dwarf2_const_value (attr, sym, cu);
c906108c 16679 }
f346a30d 16680
e37fd15a 16681 list_to_add = cu->list_in_scope;
c906108c
SS
16682 break;
16683 case DW_TAG_unspecified_parameters:
16684 /* From varargs functions; gdb doesn't seem to have any
16685 interest in this information, so just ignore it for now.
16686 (FIXME?) */
16687 break;
34eaf542
TT
16688 case DW_TAG_template_type_param:
16689 suppress_add = 1;
16690 /* Fall through. */
c906108c 16691 case DW_TAG_class_type:
680b30c7 16692 case DW_TAG_interface_type:
c906108c
SS
16693 case DW_TAG_structure_type:
16694 case DW_TAG_union_type:
72019c9c 16695 case DW_TAG_set_type:
c906108c 16696 case DW_TAG_enumeration_type:
f1e6e072 16697 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16698 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16699
63d06c5c 16700 {
987504bb 16701 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16702 really ever be static objects: otherwise, if you try
16703 to, say, break of a class's method and you're in a file
16704 which doesn't mention that class, it won't work unless
16705 the check for all static symbols in lookup_symbol_aux
16706 saves you. See the OtherFileClass tests in
16707 gdb.c++/namespace.exp. */
16708
e37fd15a 16709 if (!suppress_add)
34eaf542 16710 {
34eaf542
TT
16711 list_to_add = (cu->list_in_scope == &file_symbols
16712 && (cu->language == language_cplus
16713 || cu->language == language_java)
16714 ? &global_symbols : cu->list_in_scope);
63d06c5c 16715
64382290
TT
16716 /* The semantics of C++ state that "struct foo {
16717 ... }" also defines a typedef for "foo". A Java
16718 class declaration also defines a typedef for the
16719 class. */
16720 if (cu->language == language_cplus
16721 || cu->language == language_java
16722 || cu->language == language_ada)
16723 {
16724 /* The symbol's name is already allocated along
16725 with this objfile, so we don't need to
16726 duplicate it for the type. */
16727 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16728 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16729 }
63d06c5c
DC
16730 }
16731 }
c906108c
SS
16732 break;
16733 case DW_TAG_typedef:
f1e6e072 16734 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16735 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16736 list_to_add = cu->list_in_scope;
63d06c5c 16737 break;
c906108c 16738 case DW_TAG_base_type:
a02abb62 16739 case DW_TAG_subrange_type:
f1e6e072 16740 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16741 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16742 list_to_add = cu->list_in_scope;
c906108c
SS
16743 break;
16744 case DW_TAG_enumerator:
e142c38c 16745 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16746 if (attr)
16747 {
e7c27a73 16748 dwarf2_const_value (attr, sym, cu);
c906108c 16749 }
63d06c5c
DC
16750 {
16751 /* NOTE: carlton/2003-11-10: See comment above in the
16752 DW_TAG_class_type, etc. block. */
16753
e142c38c 16754 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16755 && (cu->language == language_cplus
16756 || cu->language == language_java)
e142c38c 16757 ? &global_symbols : cu->list_in_scope);
63d06c5c 16758 }
c906108c 16759 break;
5c4e30ca 16760 case DW_TAG_namespace:
f1e6e072 16761 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16762 list_to_add = &global_symbols;
5c4e30ca 16763 break;
4357ac6c 16764 case DW_TAG_common_block:
f1e6e072 16765 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16766 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16767 add_symbol_to_list (sym, cu->list_in_scope);
16768 break;
c906108c
SS
16769 default:
16770 /* Not a tag we recognize. Hopefully we aren't processing
16771 trash data, but since we must specifically ignore things
16772 we don't recognize, there is nothing else we should do at
0963b4bd 16773 this point. */
e2e0b3e5 16774 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16775 dwarf_tag_name (die->tag));
c906108c
SS
16776 break;
16777 }
df8a16a1 16778
e37fd15a
SW
16779 if (suppress_add)
16780 {
16781 sym->hash_next = objfile->template_symbols;
16782 objfile->template_symbols = sym;
16783 list_to_add = NULL;
16784 }
16785
16786 if (list_to_add != NULL)
16787 add_symbol_to_list (sym, list_to_add);
16788
df8a16a1
DJ
16789 /* For the benefit of old versions of GCC, check for anonymous
16790 namespaces based on the demangled name. */
4d4ec4e5 16791 if (!cu->processing_has_namespace_info
94af9270 16792 && cu->language == language_cplus)
a10964d1 16793 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16794 }
16795 return (sym);
16796}
16797
34eaf542
TT
16798/* A wrapper for new_symbol_full that always allocates a new symbol. */
16799
16800static struct symbol *
16801new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16802{
16803 return new_symbol_full (die, type, cu, NULL);
16804}
16805
98bfdba5
PA
16806/* Given an attr with a DW_FORM_dataN value in host byte order,
16807 zero-extend it as appropriate for the symbol's type. The DWARF
16808 standard (v4) is not entirely clear about the meaning of using
16809 DW_FORM_dataN for a constant with a signed type, where the type is
16810 wider than the data. The conclusion of a discussion on the DWARF
16811 list was that this is unspecified. We choose to always zero-extend
16812 because that is the interpretation long in use by GCC. */
c906108c 16813
98bfdba5 16814static gdb_byte *
ff39bb5e 16815dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16816 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16817{
e7c27a73 16818 struct objfile *objfile = cu->objfile;
e17a4113
UW
16819 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16820 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16821 LONGEST l = DW_UNSND (attr);
16822
16823 if (bits < sizeof (*value) * 8)
16824 {
16825 l &= ((LONGEST) 1 << bits) - 1;
16826 *value = l;
16827 }
16828 else if (bits == sizeof (*value) * 8)
16829 *value = l;
16830 else
16831 {
16832 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16833 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16834 return bytes;
16835 }
16836
16837 return NULL;
16838}
16839
16840/* Read a constant value from an attribute. Either set *VALUE, or if
16841 the value does not fit in *VALUE, set *BYTES - either already
16842 allocated on the objfile obstack, or newly allocated on OBSTACK,
16843 or, set *BATON, if we translated the constant to a location
16844 expression. */
16845
16846static void
ff39bb5e 16847dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16848 const char *name, struct obstack *obstack,
16849 struct dwarf2_cu *cu,
d521ce57 16850 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16851 struct dwarf2_locexpr_baton **baton)
16852{
16853 struct objfile *objfile = cu->objfile;
16854 struct comp_unit_head *cu_header = &cu->header;
c906108c 16855 struct dwarf_block *blk;
98bfdba5
PA
16856 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16857 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16858
16859 *value = 0;
16860 *bytes = NULL;
16861 *baton = NULL;
c906108c
SS
16862
16863 switch (attr->form)
16864 {
16865 case DW_FORM_addr:
3019eac3 16866 case DW_FORM_GNU_addr_index:
ac56253d 16867 {
ac56253d
TT
16868 gdb_byte *data;
16869
98bfdba5
PA
16870 if (TYPE_LENGTH (type) != cu_header->addr_size)
16871 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16872 cu_header->addr_size,
98bfdba5 16873 TYPE_LENGTH (type));
ac56253d
TT
16874 /* Symbols of this form are reasonably rare, so we just
16875 piggyback on the existing location code rather than writing
16876 a new implementation of symbol_computed_ops. */
7919a973 16877 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16878 (*baton)->per_cu = cu->per_cu;
16879 gdb_assert ((*baton)->per_cu);
ac56253d 16880
98bfdba5 16881 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16882 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16883 (*baton)->data = data;
ac56253d
TT
16884
16885 data[0] = DW_OP_addr;
16886 store_unsigned_integer (&data[1], cu_header->addr_size,
16887 byte_order, DW_ADDR (attr));
16888 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16889 }
c906108c 16890 break;
4ac36638 16891 case DW_FORM_string:
93b5768b 16892 case DW_FORM_strp:
3019eac3 16893 case DW_FORM_GNU_str_index:
36586728 16894 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16895 /* DW_STRING is already allocated on the objfile obstack, point
16896 directly to it. */
d521ce57 16897 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16898 break;
c906108c
SS
16899 case DW_FORM_block1:
16900 case DW_FORM_block2:
16901 case DW_FORM_block4:
16902 case DW_FORM_block:
2dc7f7b3 16903 case DW_FORM_exprloc:
c906108c 16904 blk = DW_BLOCK (attr);
98bfdba5
PA
16905 if (TYPE_LENGTH (type) != blk->size)
16906 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16907 TYPE_LENGTH (type));
16908 *bytes = blk->data;
c906108c 16909 break;
2df3850c
JM
16910
16911 /* The DW_AT_const_value attributes are supposed to carry the
16912 symbol's value "represented as it would be on the target
16913 architecture." By the time we get here, it's already been
16914 converted to host endianness, so we just need to sign- or
16915 zero-extend it as appropriate. */
16916 case DW_FORM_data1:
3aef2284 16917 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16918 break;
c906108c 16919 case DW_FORM_data2:
3aef2284 16920 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16921 break;
c906108c 16922 case DW_FORM_data4:
3aef2284 16923 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16924 break;
c906108c 16925 case DW_FORM_data8:
3aef2284 16926 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16927 break;
16928
c906108c 16929 case DW_FORM_sdata:
98bfdba5 16930 *value = DW_SND (attr);
2df3850c
JM
16931 break;
16932
c906108c 16933 case DW_FORM_udata:
98bfdba5 16934 *value = DW_UNSND (attr);
c906108c 16935 break;
2df3850c 16936
c906108c 16937 default:
4d3c2250 16938 complaint (&symfile_complaints,
e2e0b3e5 16939 _("unsupported const value attribute form: '%s'"),
4d3c2250 16940 dwarf_form_name (attr->form));
98bfdba5 16941 *value = 0;
c906108c
SS
16942 break;
16943 }
16944}
16945
2df3850c 16946
98bfdba5
PA
16947/* Copy constant value from an attribute to a symbol. */
16948
2df3850c 16949static void
ff39bb5e 16950dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 16951 struct dwarf2_cu *cu)
2df3850c 16952{
98bfdba5
PA
16953 struct objfile *objfile = cu->objfile;
16954 struct comp_unit_head *cu_header = &cu->header;
12df843f 16955 LONGEST value;
d521ce57 16956 const gdb_byte *bytes;
98bfdba5 16957 struct dwarf2_locexpr_baton *baton;
2df3850c 16958
98bfdba5
PA
16959 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16960 SYMBOL_PRINT_NAME (sym),
16961 &objfile->objfile_obstack, cu,
16962 &value, &bytes, &baton);
2df3850c 16963
98bfdba5
PA
16964 if (baton != NULL)
16965 {
98bfdba5 16966 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16967 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
16968 }
16969 else if (bytes != NULL)
16970 {
16971 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 16972 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
16973 }
16974 else
16975 {
16976 SYMBOL_VALUE (sym) = value;
f1e6e072 16977 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 16978 }
2df3850c
JM
16979}
16980
c906108c
SS
16981/* Return the type of the die in question using its DW_AT_type attribute. */
16982
16983static struct type *
e7c27a73 16984die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16985{
c906108c 16986 struct attribute *type_attr;
c906108c 16987
e142c38c 16988 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
16989 if (!type_attr)
16990 {
16991 /* A missing DW_AT_type represents a void type. */
46bf5051 16992 return objfile_type (cu->objfile)->builtin_void;
c906108c 16993 }
348e048f 16994
673bfd45 16995 return lookup_die_type (die, type_attr, cu);
c906108c
SS
16996}
16997
b4ba55a1
JB
16998/* True iff CU's producer generates GNAT Ada auxiliary information
16999 that allows to find parallel types through that information instead
17000 of having to do expensive parallel lookups by type name. */
17001
17002static int
17003need_gnat_info (struct dwarf2_cu *cu)
17004{
17005 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17006 of GNAT produces this auxiliary information, without any indication
17007 that it is produced. Part of enhancing the FSF version of GNAT
17008 to produce that information will be to put in place an indicator
17009 that we can use in order to determine whether the descriptive type
17010 info is available or not. One suggestion that has been made is
17011 to use a new attribute, attached to the CU die. For now, assume
17012 that the descriptive type info is not available. */
17013 return 0;
17014}
17015
b4ba55a1
JB
17016/* Return the auxiliary type of the die in question using its
17017 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17018 attribute is not present. */
17019
17020static struct type *
17021die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17022{
b4ba55a1 17023 struct attribute *type_attr;
b4ba55a1
JB
17024
17025 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17026 if (!type_attr)
17027 return NULL;
17028
673bfd45 17029 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17030}
17031
17032/* If DIE has a descriptive_type attribute, then set the TYPE's
17033 descriptive type accordingly. */
17034
17035static void
17036set_descriptive_type (struct type *type, struct die_info *die,
17037 struct dwarf2_cu *cu)
17038{
17039 struct type *descriptive_type = die_descriptive_type (die, cu);
17040
17041 if (descriptive_type)
17042 {
17043 ALLOCATE_GNAT_AUX_TYPE (type);
17044 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17045 }
17046}
17047
c906108c
SS
17048/* Return the containing type of the die in question using its
17049 DW_AT_containing_type attribute. */
17050
17051static struct type *
e7c27a73 17052die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17053{
c906108c 17054 struct attribute *type_attr;
c906108c 17055
e142c38c 17056 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17057 if (!type_attr)
17058 error (_("Dwarf Error: Problem turning containing type into gdb type "
17059 "[in module %s]"), cu->objfile->name);
17060
673bfd45 17061 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17062}
17063
ac9ec31b
DE
17064/* Return an error marker type to use for the ill formed type in DIE/CU. */
17065
17066static struct type *
17067build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17068{
17069 struct objfile *objfile = dwarf2_per_objfile->objfile;
17070 char *message, *saved;
17071
17072 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17073 objfile->name,
17074 cu->header.offset.sect_off,
17075 die->offset.sect_off);
17076 saved = obstack_copy0 (&objfile->objfile_obstack,
17077 message, strlen (message));
17078 xfree (message);
17079
17080 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17081}
17082
673bfd45 17083/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17084 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17085 DW_AT_containing_type.
673bfd45
DE
17086 If there is no type substitute an error marker. */
17087
c906108c 17088static struct type *
ff39bb5e 17089lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17090 struct dwarf2_cu *cu)
c906108c 17091{
bb5ed363 17092 struct objfile *objfile = cu->objfile;
f792889a
DJ
17093 struct type *this_type;
17094
ac9ec31b
DE
17095 gdb_assert (attr->name == DW_AT_type
17096 || attr->name == DW_AT_GNAT_descriptive_type
17097 || attr->name == DW_AT_containing_type);
17098
673bfd45
DE
17099 /* First see if we have it cached. */
17100
36586728
TT
17101 if (attr->form == DW_FORM_GNU_ref_alt)
17102 {
17103 struct dwarf2_per_cu_data *per_cu;
17104 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17105
17106 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17107 this_type = get_die_type_at_offset (offset, per_cu);
17108 }
7771576e 17109 else if (attr_form_is_ref (attr))
673bfd45 17110 {
b64f50a1 17111 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17112
17113 this_type = get_die_type_at_offset (offset, cu->per_cu);
17114 }
55f1336d 17115 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17116 {
ac9ec31b 17117 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17118
ac9ec31b 17119 return get_signatured_type (die, signature, cu);
673bfd45
DE
17120 }
17121 else
17122 {
ac9ec31b
DE
17123 complaint (&symfile_complaints,
17124 _("Dwarf Error: Bad type attribute %s in DIE"
17125 " at 0x%x [in module %s]"),
17126 dwarf_attr_name (attr->name), die->offset.sect_off,
17127 objfile->name);
17128 return build_error_marker_type (cu, die);
673bfd45
DE
17129 }
17130
17131 /* If not cached we need to read it in. */
17132
17133 if (this_type == NULL)
17134 {
ac9ec31b 17135 struct die_info *type_die = NULL;
673bfd45
DE
17136 struct dwarf2_cu *type_cu = cu;
17137
7771576e 17138 if (attr_form_is_ref (attr))
ac9ec31b
DE
17139 type_die = follow_die_ref (die, attr, &type_cu);
17140 if (type_die == NULL)
17141 return build_error_marker_type (cu, die);
17142 /* If we find the type now, it's probably because the type came
3019eac3
DE
17143 from an inter-CU reference and the type's CU got expanded before
17144 ours. */
ac9ec31b 17145 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17146 }
17147
17148 /* If we still don't have a type use an error marker. */
17149
17150 if (this_type == NULL)
ac9ec31b 17151 return build_error_marker_type (cu, die);
673bfd45 17152
f792889a 17153 return this_type;
c906108c
SS
17154}
17155
673bfd45
DE
17156/* Return the type in DIE, CU.
17157 Returns NULL for invalid types.
17158
02142a6c 17159 This first does a lookup in die_type_hash,
673bfd45
DE
17160 and only reads the die in if necessary.
17161
17162 NOTE: This can be called when reading in partial or full symbols. */
17163
f792889a 17164static struct type *
e7c27a73 17165read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17166{
f792889a
DJ
17167 struct type *this_type;
17168
17169 this_type = get_die_type (die, cu);
17170 if (this_type)
17171 return this_type;
17172
673bfd45
DE
17173 return read_type_die_1 (die, cu);
17174}
17175
17176/* Read the type in DIE, CU.
17177 Returns NULL for invalid types. */
17178
17179static struct type *
17180read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17181{
17182 struct type *this_type = NULL;
17183
c906108c
SS
17184 switch (die->tag)
17185 {
17186 case DW_TAG_class_type:
680b30c7 17187 case DW_TAG_interface_type:
c906108c
SS
17188 case DW_TAG_structure_type:
17189 case DW_TAG_union_type:
f792889a 17190 this_type = read_structure_type (die, cu);
c906108c
SS
17191 break;
17192 case DW_TAG_enumeration_type:
f792889a 17193 this_type = read_enumeration_type (die, cu);
c906108c
SS
17194 break;
17195 case DW_TAG_subprogram:
17196 case DW_TAG_subroutine_type:
edb3359d 17197 case DW_TAG_inlined_subroutine:
f792889a 17198 this_type = read_subroutine_type (die, cu);
c906108c
SS
17199 break;
17200 case DW_TAG_array_type:
f792889a 17201 this_type = read_array_type (die, cu);
c906108c 17202 break;
72019c9c 17203 case DW_TAG_set_type:
f792889a 17204 this_type = read_set_type (die, cu);
72019c9c 17205 break;
c906108c 17206 case DW_TAG_pointer_type:
f792889a 17207 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17208 break;
17209 case DW_TAG_ptr_to_member_type:
f792889a 17210 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17211 break;
17212 case DW_TAG_reference_type:
f792889a 17213 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17214 break;
17215 case DW_TAG_const_type:
f792889a 17216 this_type = read_tag_const_type (die, cu);
c906108c
SS
17217 break;
17218 case DW_TAG_volatile_type:
f792889a 17219 this_type = read_tag_volatile_type (die, cu);
c906108c 17220 break;
06d66ee9
TT
17221 case DW_TAG_restrict_type:
17222 this_type = read_tag_restrict_type (die, cu);
17223 break;
c906108c 17224 case DW_TAG_string_type:
f792889a 17225 this_type = read_tag_string_type (die, cu);
c906108c
SS
17226 break;
17227 case DW_TAG_typedef:
f792889a 17228 this_type = read_typedef (die, cu);
c906108c 17229 break;
a02abb62 17230 case DW_TAG_subrange_type:
f792889a 17231 this_type = read_subrange_type (die, cu);
a02abb62 17232 break;
c906108c 17233 case DW_TAG_base_type:
f792889a 17234 this_type = read_base_type (die, cu);
c906108c 17235 break;
81a17f79 17236 case DW_TAG_unspecified_type:
f792889a 17237 this_type = read_unspecified_type (die, cu);
81a17f79 17238 break;
0114d602
DJ
17239 case DW_TAG_namespace:
17240 this_type = read_namespace_type (die, cu);
17241 break;
f55ee35c
JK
17242 case DW_TAG_module:
17243 this_type = read_module_type (die, cu);
17244 break;
c906108c 17245 default:
3e43a32a
MS
17246 complaint (&symfile_complaints,
17247 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17248 dwarf_tag_name (die->tag));
c906108c
SS
17249 break;
17250 }
63d06c5c 17251
f792889a 17252 return this_type;
63d06c5c
DC
17253}
17254
abc72ce4
DE
17255/* See if we can figure out if the class lives in a namespace. We do
17256 this by looking for a member function; its demangled name will
17257 contain namespace info, if there is any.
17258 Return the computed name or NULL.
17259 Space for the result is allocated on the objfile's obstack.
17260 This is the full-die version of guess_partial_die_structure_name.
17261 In this case we know DIE has no useful parent. */
17262
17263static char *
17264guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17265{
17266 struct die_info *spec_die;
17267 struct dwarf2_cu *spec_cu;
17268 struct die_info *child;
17269
17270 spec_cu = cu;
17271 spec_die = die_specification (die, &spec_cu);
17272 if (spec_die != NULL)
17273 {
17274 die = spec_die;
17275 cu = spec_cu;
17276 }
17277
17278 for (child = die->child;
17279 child != NULL;
17280 child = child->sibling)
17281 {
17282 if (child->tag == DW_TAG_subprogram)
17283 {
17284 struct attribute *attr;
17285
17286 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17287 if (attr == NULL)
17288 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17289 if (attr != NULL)
17290 {
17291 char *actual_name
17292 = language_class_name_from_physname (cu->language_defn,
17293 DW_STRING (attr));
17294 char *name = NULL;
17295
17296 if (actual_name != NULL)
17297 {
15d034d0 17298 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17299
17300 if (die_name != NULL
17301 && strcmp (die_name, actual_name) != 0)
17302 {
17303 /* Strip off the class name from the full name.
17304 We want the prefix. */
17305 int die_name_len = strlen (die_name);
17306 int actual_name_len = strlen (actual_name);
17307
17308 /* Test for '::' as a sanity check. */
17309 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17310 && actual_name[actual_name_len
17311 - die_name_len - 1] == ':')
abc72ce4 17312 name =
10f0c4bb
TT
17313 obstack_copy0 (&cu->objfile->objfile_obstack,
17314 actual_name,
17315 actual_name_len - die_name_len - 2);
abc72ce4
DE
17316 }
17317 }
17318 xfree (actual_name);
17319 return name;
17320 }
17321 }
17322 }
17323
17324 return NULL;
17325}
17326
96408a79
SA
17327/* GCC might emit a nameless typedef that has a linkage name. Determine the
17328 prefix part in such case. See
17329 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17330
17331static char *
17332anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17333{
17334 struct attribute *attr;
17335 char *base;
17336
17337 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17338 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17339 return NULL;
17340
17341 attr = dwarf2_attr (die, DW_AT_name, cu);
17342 if (attr != NULL && DW_STRING (attr) != NULL)
17343 return NULL;
17344
17345 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17346 if (attr == NULL)
17347 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17348 if (attr == NULL || DW_STRING (attr) == NULL)
17349 return NULL;
17350
17351 /* dwarf2_name had to be already called. */
17352 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17353
17354 /* Strip the base name, keep any leading namespaces/classes. */
17355 base = strrchr (DW_STRING (attr), ':');
17356 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17357 return "";
17358
10f0c4bb
TT
17359 return obstack_copy0 (&cu->objfile->objfile_obstack,
17360 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17361}
17362
fdde2d81 17363/* Return the name of the namespace/class that DIE is defined within,
0114d602 17364 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17365
0114d602
DJ
17366 For example, if we're within the method foo() in the following
17367 code:
17368
17369 namespace N {
17370 class C {
17371 void foo () {
17372 }
17373 };
17374 }
17375
17376 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17377
0d5cff50 17378static const char *
e142c38c 17379determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17380{
0114d602
DJ
17381 struct die_info *parent, *spec_die;
17382 struct dwarf2_cu *spec_cu;
17383 struct type *parent_type;
96408a79 17384 char *retval;
63d06c5c 17385
f55ee35c
JK
17386 if (cu->language != language_cplus && cu->language != language_java
17387 && cu->language != language_fortran)
0114d602
DJ
17388 return "";
17389
96408a79
SA
17390 retval = anonymous_struct_prefix (die, cu);
17391 if (retval)
17392 return retval;
17393
0114d602
DJ
17394 /* We have to be careful in the presence of DW_AT_specification.
17395 For example, with GCC 3.4, given the code
17396
17397 namespace N {
17398 void foo() {
17399 // Definition of N::foo.
17400 }
17401 }
17402
17403 then we'll have a tree of DIEs like this:
17404
17405 1: DW_TAG_compile_unit
17406 2: DW_TAG_namespace // N
17407 3: DW_TAG_subprogram // declaration of N::foo
17408 4: DW_TAG_subprogram // definition of N::foo
17409 DW_AT_specification // refers to die #3
17410
17411 Thus, when processing die #4, we have to pretend that we're in
17412 the context of its DW_AT_specification, namely the contex of die
17413 #3. */
17414 spec_cu = cu;
17415 spec_die = die_specification (die, &spec_cu);
17416 if (spec_die == NULL)
17417 parent = die->parent;
17418 else
63d06c5c 17419 {
0114d602
DJ
17420 parent = spec_die->parent;
17421 cu = spec_cu;
63d06c5c 17422 }
0114d602
DJ
17423
17424 if (parent == NULL)
17425 return "";
98bfdba5
PA
17426 else if (parent->building_fullname)
17427 {
17428 const char *name;
17429 const char *parent_name;
17430
17431 /* It has been seen on RealView 2.2 built binaries,
17432 DW_TAG_template_type_param types actually _defined_ as
17433 children of the parent class:
17434
17435 enum E {};
17436 template class <class Enum> Class{};
17437 Class<enum E> class_e;
17438
17439 1: DW_TAG_class_type (Class)
17440 2: DW_TAG_enumeration_type (E)
17441 3: DW_TAG_enumerator (enum1:0)
17442 3: DW_TAG_enumerator (enum2:1)
17443 ...
17444 2: DW_TAG_template_type_param
17445 DW_AT_type DW_FORM_ref_udata (E)
17446
17447 Besides being broken debug info, it can put GDB into an
17448 infinite loop. Consider:
17449
17450 When we're building the full name for Class<E>, we'll start
17451 at Class, and go look over its template type parameters,
17452 finding E. We'll then try to build the full name of E, and
17453 reach here. We're now trying to build the full name of E,
17454 and look over the parent DIE for containing scope. In the
17455 broken case, if we followed the parent DIE of E, we'd again
17456 find Class, and once again go look at its template type
17457 arguments, etc., etc. Simply don't consider such parent die
17458 as source-level parent of this die (it can't be, the language
17459 doesn't allow it), and break the loop here. */
17460 name = dwarf2_name (die, cu);
17461 parent_name = dwarf2_name (parent, cu);
17462 complaint (&symfile_complaints,
17463 _("template param type '%s' defined within parent '%s'"),
17464 name ? name : "<unknown>",
17465 parent_name ? parent_name : "<unknown>");
17466 return "";
17467 }
63d06c5c 17468 else
0114d602
DJ
17469 switch (parent->tag)
17470 {
63d06c5c 17471 case DW_TAG_namespace:
0114d602 17472 parent_type = read_type_die (parent, cu);
acebe513
UW
17473 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17474 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17475 Work around this problem here. */
17476 if (cu->language == language_cplus
17477 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17478 return "";
0114d602
DJ
17479 /* We give a name to even anonymous namespaces. */
17480 return TYPE_TAG_NAME (parent_type);
63d06c5c 17481 case DW_TAG_class_type:
680b30c7 17482 case DW_TAG_interface_type:
63d06c5c 17483 case DW_TAG_structure_type:
0114d602 17484 case DW_TAG_union_type:
f55ee35c 17485 case DW_TAG_module:
0114d602
DJ
17486 parent_type = read_type_die (parent, cu);
17487 if (TYPE_TAG_NAME (parent_type) != NULL)
17488 return TYPE_TAG_NAME (parent_type);
17489 else
17490 /* An anonymous structure is only allowed non-static data
17491 members; no typedefs, no member functions, et cetera.
17492 So it does not need a prefix. */
17493 return "";
abc72ce4 17494 case DW_TAG_compile_unit:
95554aad 17495 case DW_TAG_partial_unit:
abc72ce4
DE
17496 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17497 if (cu->language == language_cplus
8b70b953 17498 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17499 && die->child != NULL
17500 && (die->tag == DW_TAG_class_type
17501 || die->tag == DW_TAG_structure_type
17502 || die->tag == DW_TAG_union_type))
17503 {
17504 char *name = guess_full_die_structure_name (die, cu);
17505 if (name != NULL)
17506 return name;
17507 }
17508 return "";
63d06c5c 17509 default:
8176b9b8 17510 return determine_prefix (parent, cu);
63d06c5c 17511 }
63d06c5c
DC
17512}
17513
3e43a32a
MS
17514/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17515 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17516 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17517 an obconcat, otherwise allocate storage for the result. The CU argument is
17518 used to determine the language and hence, the appropriate separator. */
987504bb 17519
f55ee35c 17520#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17521
17522static char *
f55ee35c
JK
17523typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17524 int physname, struct dwarf2_cu *cu)
63d06c5c 17525{
f55ee35c 17526 const char *lead = "";
5c315b68 17527 const char *sep;
63d06c5c 17528
3e43a32a
MS
17529 if (suffix == NULL || suffix[0] == '\0'
17530 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17531 sep = "";
17532 else if (cu->language == language_java)
17533 sep = ".";
f55ee35c
JK
17534 else if (cu->language == language_fortran && physname)
17535 {
17536 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17537 DW_AT_MIPS_linkage_name is preferred and used instead. */
17538
17539 lead = "__";
17540 sep = "_MOD_";
17541 }
987504bb
JJ
17542 else
17543 sep = "::";
63d06c5c 17544
6dd47d34
DE
17545 if (prefix == NULL)
17546 prefix = "";
17547 if (suffix == NULL)
17548 suffix = "";
17549
987504bb
JJ
17550 if (obs == NULL)
17551 {
3e43a32a
MS
17552 char *retval
17553 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17554
f55ee35c
JK
17555 strcpy (retval, lead);
17556 strcat (retval, prefix);
6dd47d34
DE
17557 strcat (retval, sep);
17558 strcat (retval, suffix);
63d06c5c
DC
17559 return retval;
17560 }
987504bb
JJ
17561 else
17562 {
17563 /* We have an obstack. */
f55ee35c 17564 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17565 }
63d06c5c
DC
17566}
17567
c906108c
SS
17568/* Return sibling of die, NULL if no sibling. */
17569
f9aca02d 17570static struct die_info *
fba45db2 17571sibling_die (struct die_info *die)
c906108c 17572{
639d11d3 17573 return die->sibling;
c906108c
SS
17574}
17575
71c25dea
TT
17576/* Get name of a die, return NULL if not found. */
17577
15d034d0
TT
17578static const char *
17579dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17580 struct obstack *obstack)
17581{
17582 if (name && cu->language == language_cplus)
17583 {
17584 char *canon_name = cp_canonicalize_string (name);
17585
17586 if (canon_name != NULL)
17587 {
17588 if (strcmp (canon_name, name) != 0)
10f0c4bb 17589 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17590 xfree (canon_name);
17591 }
17592 }
17593
17594 return name;
c906108c
SS
17595}
17596
9219021c
DC
17597/* Get name of a die, return NULL if not found. */
17598
15d034d0 17599static const char *
e142c38c 17600dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17601{
17602 struct attribute *attr;
17603
e142c38c 17604 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17605 if ((!attr || !DW_STRING (attr))
17606 && die->tag != DW_TAG_class_type
17607 && die->tag != DW_TAG_interface_type
17608 && die->tag != DW_TAG_structure_type
17609 && die->tag != DW_TAG_union_type)
71c25dea
TT
17610 return NULL;
17611
17612 switch (die->tag)
17613 {
17614 case DW_TAG_compile_unit:
95554aad 17615 case DW_TAG_partial_unit:
71c25dea
TT
17616 /* Compilation units have a DW_AT_name that is a filename, not
17617 a source language identifier. */
17618 case DW_TAG_enumeration_type:
17619 case DW_TAG_enumerator:
17620 /* These tags always have simple identifiers already; no need
17621 to canonicalize them. */
17622 return DW_STRING (attr);
907af001 17623
418835cc
KS
17624 case DW_TAG_subprogram:
17625 /* Java constructors will all be named "<init>", so return
17626 the class name when we see this special case. */
17627 if (cu->language == language_java
17628 && DW_STRING (attr) != NULL
17629 && strcmp (DW_STRING (attr), "<init>") == 0)
17630 {
17631 struct dwarf2_cu *spec_cu = cu;
17632 struct die_info *spec_die;
17633
17634 /* GCJ will output '<init>' for Java constructor names.
17635 For this special case, return the name of the parent class. */
17636
17637 /* GCJ may output suprogram DIEs with AT_specification set.
17638 If so, use the name of the specified DIE. */
17639 spec_die = die_specification (die, &spec_cu);
17640 if (spec_die != NULL)
17641 return dwarf2_name (spec_die, spec_cu);
17642
17643 do
17644 {
17645 die = die->parent;
17646 if (die->tag == DW_TAG_class_type)
17647 return dwarf2_name (die, cu);
17648 }
95554aad
TT
17649 while (die->tag != DW_TAG_compile_unit
17650 && die->tag != DW_TAG_partial_unit);
418835cc 17651 }
907af001
UW
17652 break;
17653
17654 case DW_TAG_class_type:
17655 case DW_TAG_interface_type:
17656 case DW_TAG_structure_type:
17657 case DW_TAG_union_type:
17658 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17659 structures or unions. These were of the form "._%d" in GCC 4.1,
17660 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17661 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17662 if (attr && DW_STRING (attr)
17663 && (strncmp (DW_STRING (attr), "._", 2) == 0
17664 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17665 return NULL;
53832f31
TT
17666
17667 /* GCC might emit a nameless typedef that has a linkage name. See
17668 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17669 if (!attr || DW_STRING (attr) == NULL)
17670 {
df5c6c50 17671 char *demangled = NULL;
53832f31
TT
17672
17673 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17674 if (attr == NULL)
17675 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17676
17677 if (attr == NULL || DW_STRING (attr) == NULL)
17678 return NULL;
17679
df5c6c50
JK
17680 /* Avoid demangling DW_STRING (attr) the second time on a second
17681 call for the same DIE. */
17682 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17683 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17684
17685 if (demangled)
17686 {
96408a79
SA
17687 char *base;
17688
53832f31 17689 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17690 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17691 demangled, strlen (demangled));
53832f31
TT
17692 DW_STRING_IS_CANONICAL (attr) = 1;
17693 xfree (demangled);
96408a79
SA
17694
17695 /* Strip any leading namespaces/classes, keep only the base name.
17696 DW_AT_name for named DIEs does not contain the prefixes. */
17697 base = strrchr (DW_STRING (attr), ':');
17698 if (base && base > DW_STRING (attr) && base[-1] == ':')
17699 return &base[1];
17700 else
17701 return DW_STRING (attr);
53832f31
TT
17702 }
17703 }
907af001
UW
17704 break;
17705
71c25dea 17706 default:
907af001
UW
17707 break;
17708 }
17709
17710 if (!DW_STRING_IS_CANONICAL (attr))
17711 {
17712 DW_STRING (attr)
17713 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17714 &cu->objfile->objfile_obstack);
17715 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17716 }
907af001 17717 return DW_STRING (attr);
9219021c
DC
17718}
17719
17720/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17721 is none. *EXT_CU is the CU containing DIE on input, and the CU
17722 containing the return value on output. */
9219021c
DC
17723
17724static struct die_info *
f2f0e013 17725dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17726{
17727 struct attribute *attr;
9219021c 17728
f2f0e013 17729 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17730 if (attr == NULL)
17731 return NULL;
17732
f2f0e013 17733 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17734}
17735
c906108c
SS
17736/* Convert a DIE tag into its string name. */
17737
f39c6ffd 17738static const char *
aa1ee363 17739dwarf_tag_name (unsigned tag)
c906108c 17740{
f39c6ffd
TT
17741 const char *name = get_DW_TAG_name (tag);
17742
17743 if (name == NULL)
17744 return "DW_TAG_<unknown>";
17745
17746 return name;
c906108c
SS
17747}
17748
17749/* Convert a DWARF attribute code into its string name. */
17750
f39c6ffd 17751static const char *
aa1ee363 17752dwarf_attr_name (unsigned attr)
c906108c 17753{
f39c6ffd
TT
17754 const char *name;
17755
c764a876 17756#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17757 if (attr == DW_AT_MIPS_fde)
17758 return "DW_AT_MIPS_fde";
17759#else
17760 if (attr == DW_AT_HP_block_index)
17761 return "DW_AT_HP_block_index";
c764a876 17762#endif
f39c6ffd
TT
17763
17764 name = get_DW_AT_name (attr);
17765
17766 if (name == NULL)
17767 return "DW_AT_<unknown>";
17768
17769 return name;
c906108c
SS
17770}
17771
17772/* Convert a DWARF value form code into its string name. */
17773
f39c6ffd 17774static const char *
aa1ee363 17775dwarf_form_name (unsigned form)
c906108c 17776{
f39c6ffd
TT
17777 const char *name = get_DW_FORM_name (form);
17778
17779 if (name == NULL)
17780 return "DW_FORM_<unknown>";
17781
17782 return name;
c906108c
SS
17783}
17784
17785static char *
fba45db2 17786dwarf_bool_name (unsigned mybool)
c906108c
SS
17787{
17788 if (mybool)
17789 return "TRUE";
17790 else
17791 return "FALSE";
17792}
17793
17794/* Convert a DWARF type code into its string name. */
17795
f39c6ffd 17796static const char *
aa1ee363 17797dwarf_type_encoding_name (unsigned enc)
c906108c 17798{
f39c6ffd 17799 const char *name = get_DW_ATE_name (enc);
c906108c 17800
f39c6ffd
TT
17801 if (name == NULL)
17802 return "DW_ATE_<unknown>";
c906108c 17803
f39c6ffd 17804 return name;
c906108c 17805}
c906108c 17806
f9aca02d 17807static void
d97bc12b 17808dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17809{
17810 unsigned int i;
17811
d97bc12b
DE
17812 print_spaces (indent, f);
17813 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17814 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17815
17816 if (die->parent != NULL)
17817 {
17818 print_spaces (indent, f);
17819 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17820 die->parent->offset.sect_off);
d97bc12b
DE
17821 }
17822
17823 print_spaces (indent, f);
17824 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17825 dwarf_bool_name (die->child != NULL));
c906108c 17826
d97bc12b
DE
17827 print_spaces (indent, f);
17828 fprintf_unfiltered (f, " attributes:\n");
17829
c906108c
SS
17830 for (i = 0; i < die->num_attrs; ++i)
17831 {
d97bc12b
DE
17832 print_spaces (indent, f);
17833 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17834 dwarf_attr_name (die->attrs[i].name),
17835 dwarf_form_name (die->attrs[i].form));
d97bc12b 17836
c906108c
SS
17837 switch (die->attrs[i].form)
17838 {
c906108c 17839 case DW_FORM_addr:
3019eac3 17840 case DW_FORM_GNU_addr_index:
d97bc12b 17841 fprintf_unfiltered (f, "address: ");
5af949e3 17842 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17843 break;
17844 case DW_FORM_block2:
17845 case DW_FORM_block4:
17846 case DW_FORM_block:
17847 case DW_FORM_block1:
56eb65bd
SP
17848 fprintf_unfiltered (f, "block: size %s",
17849 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17850 break;
2dc7f7b3 17851 case DW_FORM_exprloc:
56eb65bd
SP
17852 fprintf_unfiltered (f, "expression: size %s",
17853 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17854 break;
4568ecf9
DE
17855 case DW_FORM_ref_addr:
17856 fprintf_unfiltered (f, "ref address: ");
17857 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17858 break;
36586728
TT
17859 case DW_FORM_GNU_ref_alt:
17860 fprintf_unfiltered (f, "alt ref address: ");
17861 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17862 break;
10b3939b
DJ
17863 case DW_FORM_ref1:
17864 case DW_FORM_ref2:
17865 case DW_FORM_ref4:
4568ecf9
DE
17866 case DW_FORM_ref8:
17867 case DW_FORM_ref_udata:
d97bc12b 17868 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17869 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17870 break;
c906108c
SS
17871 case DW_FORM_data1:
17872 case DW_FORM_data2:
17873 case DW_FORM_data4:
ce5d95e1 17874 case DW_FORM_data8:
c906108c
SS
17875 case DW_FORM_udata:
17876 case DW_FORM_sdata:
43bbcdc2
PH
17877 fprintf_unfiltered (f, "constant: %s",
17878 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17879 break;
2dc7f7b3
TT
17880 case DW_FORM_sec_offset:
17881 fprintf_unfiltered (f, "section offset: %s",
17882 pulongest (DW_UNSND (&die->attrs[i])));
17883 break;
55f1336d 17884 case DW_FORM_ref_sig8:
ac9ec31b
DE
17885 fprintf_unfiltered (f, "signature: %s",
17886 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17887 break;
c906108c 17888 case DW_FORM_string:
4bdf3d34 17889 case DW_FORM_strp:
3019eac3 17890 case DW_FORM_GNU_str_index:
36586728 17891 case DW_FORM_GNU_strp_alt:
8285870a 17892 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17893 DW_STRING (&die->attrs[i])
8285870a
JK
17894 ? DW_STRING (&die->attrs[i]) : "",
17895 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17896 break;
17897 case DW_FORM_flag:
17898 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17899 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17900 else
d97bc12b 17901 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17902 break;
2dc7f7b3
TT
17903 case DW_FORM_flag_present:
17904 fprintf_unfiltered (f, "flag: TRUE");
17905 break;
a8329558 17906 case DW_FORM_indirect:
0963b4bd
MS
17907 /* The reader will have reduced the indirect form to
17908 the "base form" so this form should not occur. */
3e43a32a
MS
17909 fprintf_unfiltered (f,
17910 "unexpected attribute form: DW_FORM_indirect");
a8329558 17911 break;
c906108c 17912 default:
d97bc12b 17913 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17914 die->attrs[i].form);
d97bc12b 17915 break;
c906108c 17916 }
d97bc12b 17917 fprintf_unfiltered (f, "\n");
c906108c
SS
17918 }
17919}
17920
f9aca02d 17921static void
d97bc12b 17922dump_die_for_error (struct die_info *die)
c906108c 17923{
d97bc12b
DE
17924 dump_die_shallow (gdb_stderr, 0, die);
17925}
17926
17927static void
17928dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17929{
17930 int indent = level * 4;
17931
17932 gdb_assert (die != NULL);
17933
17934 if (level >= max_level)
17935 return;
17936
17937 dump_die_shallow (f, indent, die);
17938
17939 if (die->child != NULL)
c906108c 17940 {
d97bc12b
DE
17941 print_spaces (indent, f);
17942 fprintf_unfiltered (f, " Children:");
17943 if (level + 1 < max_level)
17944 {
17945 fprintf_unfiltered (f, "\n");
17946 dump_die_1 (f, level + 1, max_level, die->child);
17947 }
17948 else
17949 {
3e43a32a
MS
17950 fprintf_unfiltered (f,
17951 " [not printed, max nesting level reached]\n");
d97bc12b
DE
17952 }
17953 }
17954
17955 if (die->sibling != NULL && level > 0)
17956 {
17957 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
17958 }
17959}
17960
d97bc12b
DE
17961/* This is called from the pdie macro in gdbinit.in.
17962 It's not static so gcc will keep a copy callable from gdb. */
17963
17964void
17965dump_die (struct die_info *die, int max_level)
17966{
17967 dump_die_1 (gdb_stdlog, 0, max_level, die);
17968}
17969
f9aca02d 17970static void
51545339 17971store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17972{
51545339 17973 void **slot;
c906108c 17974
b64f50a1
JK
17975 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17976 INSERT);
51545339
DJ
17977
17978 *slot = die;
c906108c
SS
17979}
17980
b64f50a1
JK
17981/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17982 required kind. */
17983
17984static sect_offset
ff39bb5e 17985dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 17986{
4568ecf9 17987 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 17988
7771576e 17989 if (attr_form_is_ref (attr))
b64f50a1 17990 return retval;
93311388 17991
b64f50a1 17992 retval.sect_off = 0;
93311388
DE
17993 complaint (&symfile_complaints,
17994 _("unsupported die ref attribute form: '%s'"),
17995 dwarf_form_name (attr->form));
b64f50a1 17996 return retval;
c906108c
SS
17997}
17998
43bbcdc2
PH
17999/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18000 * the value held by the attribute is not constant. */
a02abb62 18001
43bbcdc2 18002static LONGEST
ff39bb5e 18003dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
18004{
18005 if (attr->form == DW_FORM_sdata)
18006 return DW_SND (attr);
18007 else if (attr->form == DW_FORM_udata
18008 || attr->form == DW_FORM_data1
18009 || attr->form == DW_FORM_data2
18010 || attr->form == DW_FORM_data4
18011 || attr->form == DW_FORM_data8)
18012 return DW_UNSND (attr);
18013 else
18014 {
3e43a32a
MS
18015 complaint (&symfile_complaints,
18016 _("Attribute value is not a constant (%s)"),
a02abb62
JB
18017 dwarf_form_name (attr->form));
18018 return default_value;
18019 }
18020}
18021
348e048f
DE
18022/* Follow reference or signature attribute ATTR of SRC_DIE.
18023 On entry *REF_CU is the CU of SRC_DIE.
18024 On exit *REF_CU is the CU of the result. */
18025
18026static struct die_info *
ff39bb5e 18027follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18028 struct dwarf2_cu **ref_cu)
18029{
18030 struct die_info *die;
18031
7771576e 18032 if (attr_form_is_ref (attr))
348e048f 18033 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18034 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18035 die = follow_die_sig (src_die, attr, ref_cu);
18036 else
18037 {
18038 dump_die_for_error (src_die);
18039 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18040 (*ref_cu)->objfile->name);
18041 }
18042
18043 return die;
03dd20cc
DJ
18044}
18045
5c631832 18046/* Follow reference OFFSET.
673bfd45
DE
18047 On entry *REF_CU is the CU of the source die referencing OFFSET.
18048 On exit *REF_CU is the CU of the result.
18049 Returns NULL if OFFSET is invalid. */
f504f079 18050
f9aca02d 18051static struct die_info *
36586728
TT
18052follow_die_offset (sect_offset offset, int offset_in_dwz,
18053 struct dwarf2_cu **ref_cu)
c906108c 18054{
10b3939b 18055 struct die_info temp_die;
f2f0e013 18056 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18057
348e048f
DE
18058 gdb_assert (cu->per_cu != NULL);
18059
98bfdba5
PA
18060 target_cu = cu;
18061
3019eac3 18062 if (cu->per_cu->is_debug_types)
348e048f
DE
18063 {
18064 /* .debug_types CUs cannot reference anything outside their CU.
18065 If they need to, they have to reference a signatured type via
55f1336d 18066 DW_FORM_ref_sig8. */
348e048f 18067 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18068 return NULL;
348e048f 18069 }
36586728
TT
18070 else if (offset_in_dwz != cu->per_cu->is_dwz
18071 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18072 {
18073 struct dwarf2_per_cu_data *per_cu;
9a619af0 18074
36586728
TT
18075 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18076 cu->objfile);
03dd20cc
DJ
18077
18078 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18079 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18080 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18081
10b3939b
DJ
18082 target_cu = per_cu->cu;
18083 }
98bfdba5
PA
18084 else if (cu->dies == NULL)
18085 {
18086 /* We're loading full DIEs during partial symbol reading. */
18087 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18088 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18089 }
c906108c 18090
f2f0e013 18091 *ref_cu = target_cu;
51545339 18092 temp_die.offset = offset;
b64f50a1 18093 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18094}
10b3939b 18095
5c631832
JK
18096/* Follow reference attribute ATTR of SRC_DIE.
18097 On entry *REF_CU is the CU of SRC_DIE.
18098 On exit *REF_CU is the CU of the result. */
18099
18100static struct die_info *
ff39bb5e 18101follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18102 struct dwarf2_cu **ref_cu)
18103{
b64f50a1 18104 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18105 struct dwarf2_cu *cu = *ref_cu;
18106 struct die_info *die;
18107
36586728
TT
18108 die = follow_die_offset (offset,
18109 (attr->form == DW_FORM_GNU_ref_alt
18110 || cu->per_cu->is_dwz),
18111 ref_cu);
5c631832
JK
18112 if (!die)
18113 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18114 "at 0x%x [in module %s]"),
b64f50a1 18115 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 18116
5c631832
JK
18117 return die;
18118}
18119
d83e736b
JK
18120/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18121 Returned value is intended for DW_OP_call*. Returned
18122 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18123
18124struct dwarf2_locexpr_baton
8b9737bf
TT
18125dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18126 struct dwarf2_per_cu_data *per_cu,
18127 CORE_ADDR (*get_frame_pc) (void *baton),
18128 void *baton)
5c631832 18129{
918dd910 18130 struct dwarf2_cu *cu;
5c631832
JK
18131 struct die_info *die;
18132 struct attribute *attr;
18133 struct dwarf2_locexpr_baton retval;
18134
8cf6f0b1
TT
18135 dw2_setup (per_cu->objfile);
18136
918dd910
JK
18137 if (per_cu->cu == NULL)
18138 load_cu (per_cu);
18139 cu = per_cu->cu;
18140
36586728 18141 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18142 if (!die)
18143 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 18144 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18145
18146 attr = dwarf2_attr (die, DW_AT_location, cu);
18147 if (!attr)
18148 {
e103e986
JK
18149 /* DWARF: "If there is no such attribute, then there is no effect.".
18150 DATA is ignored if SIZE is 0. */
5c631832 18151
e103e986 18152 retval.data = NULL;
5c631832
JK
18153 retval.size = 0;
18154 }
8cf6f0b1
TT
18155 else if (attr_form_is_section_offset (attr))
18156 {
18157 struct dwarf2_loclist_baton loclist_baton;
18158 CORE_ADDR pc = (*get_frame_pc) (baton);
18159 size_t size;
18160
18161 fill_in_loclist_baton (cu, &loclist_baton, attr);
18162
18163 retval.data = dwarf2_find_location_expression (&loclist_baton,
18164 &size, pc);
18165 retval.size = size;
18166 }
5c631832
JK
18167 else
18168 {
18169 if (!attr_form_is_block (attr))
18170 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18171 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 18172 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18173
18174 retval.data = DW_BLOCK (attr)->data;
18175 retval.size = DW_BLOCK (attr)->size;
18176 }
18177 retval.per_cu = cu->per_cu;
918dd910 18178
918dd910
JK
18179 age_cached_comp_units ();
18180
5c631832 18181 return retval;
348e048f
DE
18182}
18183
8b9737bf
TT
18184/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18185 offset. */
18186
18187struct dwarf2_locexpr_baton
18188dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18189 struct dwarf2_per_cu_data *per_cu,
18190 CORE_ADDR (*get_frame_pc) (void *baton),
18191 void *baton)
18192{
18193 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18194
18195 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18196}
18197
b6807d98
TT
18198/* Write a constant of a given type as target-ordered bytes into
18199 OBSTACK. */
18200
18201static const gdb_byte *
18202write_constant_as_bytes (struct obstack *obstack,
18203 enum bfd_endian byte_order,
18204 struct type *type,
18205 ULONGEST value,
18206 LONGEST *len)
18207{
18208 gdb_byte *result;
18209
18210 *len = TYPE_LENGTH (type);
18211 result = obstack_alloc (obstack, *len);
18212 store_unsigned_integer (result, *len, byte_order, value);
18213
18214 return result;
18215}
18216
18217/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18218 pointer to the constant bytes and set LEN to the length of the
18219 data. If memory is needed, allocate it on OBSTACK. If the DIE
18220 does not have a DW_AT_const_value, return NULL. */
18221
18222const gdb_byte *
18223dwarf2_fetch_constant_bytes (sect_offset offset,
18224 struct dwarf2_per_cu_data *per_cu,
18225 struct obstack *obstack,
18226 LONGEST *len)
18227{
18228 struct dwarf2_cu *cu;
18229 struct die_info *die;
18230 struct attribute *attr;
18231 const gdb_byte *result = NULL;
18232 struct type *type;
18233 LONGEST value;
18234 enum bfd_endian byte_order;
18235
18236 dw2_setup (per_cu->objfile);
18237
18238 if (per_cu->cu == NULL)
18239 load_cu (per_cu);
18240 cu = per_cu->cu;
18241
18242 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18243 if (!die)
18244 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18245 offset.sect_off, per_cu->objfile->name);
18246
18247
18248 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18249 if (attr == NULL)
18250 return NULL;
18251
18252 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18253 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18254
18255 switch (attr->form)
18256 {
18257 case DW_FORM_addr:
18258 case DW_FORM_GNU_addr_index:
18259 {
18260 gdb_byte *tem;
18261
18262 *len = cu->header.addr_size;
18263 tem = obstack_alloc (obstack, *len);
18264 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18265 result = tem;
18266 }
18267 break;
18268 case DW_FORM_string:
18269 case DW_FORM_strp:
18270 case DW_FORM_GNU_str_index:
18271 case DW_FORM_GNU_strp_alt:
18272 /* DW_STRING is already allocated on the objfile obstack, point
18273 directly to it. */
18274 result = (const gdb_byte *) DW_STRING (attr);
18275 *len = strlen (DW_STRING (attr));
18276 break;
18277 case DW_FORM_block1:
18278 case DW_FORM_block2:
18279 case DW_FORM_block4:
18280 case DW_FORM_block:
18281 case DW_FORM_exprloc:
18282 result = DW_BLOCK (attr)->data;
18283 *len = DW_BLOCK (attr)->size;
18284 break;
18285
18286 /* The DW_AT_const_value attributes are supposed to carry the
18287 symbol's value "represented as it would be on the target
18288 architecture." By the time we get here, it's already been
18289 converted to host endianness, so we just need to sign- or
18290 zero-extend it as appropriate. */
18291 case DW_FORM_data1:
18292 type = die_type (die, cu);
18293 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18294 if (result == NULL)
18295 result = write_constant_as_bytes (obstack, byte_order,
18296 type, value, len);
18297 break;
18298 case DW_FORM_data2:
18299 type = die_type (die, cu);
18300 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18301 if (result == NULL)
18302 result = write_constant_as_bytes (obstack, byte_order,
18303 type, value, len);
18304 break;
18305 case DW_FORM_data4:
18306 type = die_type (die, cu);
18307 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18308 if (result == NULL)
18309 result = write_constant_as_bytes (obstack, byte_order,
18310 type, value, len);
18311 break;
18312 case DW_FORM_data8:
18313 type = die_type (die, cu);
18314 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18315 if (result == NULL)
18316 result = write_constant_as_bytes (obstack, byte_order,
18317 type, value, len);
18318 break;
18319
18320 case DW_FORM_sdata:
18321 type = die_type (die, cu);
18322 result = write_constant_as_bytes (obstack, byte_order,
18323 type, DW_SND (attr), len);
18324 break;
18325
18326 case DW_FORM_udata:
18327 type = die_type (die, cu);
18328 result = write_constant_as_bytes (obstack, byte_order,
18329 type, DW_UNSND (attr), len);
18330 break;
18331
18332 default:
18333 complaint (&symfile_complaints,
18334 _("unsupported const value attribute form: '%s'"),
18335 dwarf_form_name (attr->form));
18336 break;
18337 }
18338
18339 return result;
18340}
18341
8a9b8146
TT
18342/* Return the type of the DIE at DIE_OFFSET in the CU named by
18343 PER_CU. */
18344
18345struct type *
b64f50a1 18346dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18347 struct dwarf2_per_cu_data *per_cu)
18348{
b64f50a1
JK
18349 sect_offset die_offset_sect;
18350
8a9b8146 18351 dw2_setup (per_cu->objfile);
b64f50a1
JK
18352
18353 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18354 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18355}
18356
ac9ec31b 18357/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18358 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18359 On exit *REF_CU is the CU of the result.
18360 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18361
18362static struct die_info *
ac9ec31b
DE
18363follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18364 struct dwarf2_cu **ref_cu)
348e048f
DE
18365{
18366 struct objfile *objfile = (*ref_cu)->objfile;
18367 struct die_info temp_die;
348e048f
DE
18368 struct dwarf2_cu *sig_cu;
18369 struct die_info *die;
18370
ac9ec31b
DE
18371 /* While it might be nice to assert sig_type->type == NULL here,
18372 we can get here for DW_AT_imported_declaration where we need
18373 the DIE not the type. */
348e048f
DE
18374
18375 /* If necessary, add it to the queue and load its DIEs. */
18376
95554aad 18377 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18378 read_signatured_type (sig_type);
348e048f
DE
18379
18380 gdb_assert (sig_type->per_cu.cu != NULL);
18381
18382 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
18383 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18384 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18385 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18386 temp_die.offset.sect_off);
348e048f
DE
18387 if (die)
18388 {
796a7ff8
DE
18389 /* For .gdb_index version 7 keep track of included TUs.
18390 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18391 if (dwarf2_per_objfile->index_table != NULL
18392 && dwarf2_per_objfile->index_table->version <= 7)
18393 {
18394 VEC_safe_push (dwarf2_per_cu_ptr,
18395 (*ref_cu)->per_cu->imported_symtabs,
18396 sig_cu->per_cu);
18397 }
18398
348e048f
DE
18399 *ref_cu = sig_cu;
18400 return die;
18401 }
18402
ac9ec31b
DE
18403 return NULL;
18404}
18405
18406/* Follow signatured type referenced by ATTR in SRC_DIE.
18407 On entry *REF_CU is the CU of SRC_DIE.
18408 On exit *REF_CU is the CU of the result.
18409 The result is the DIE of the type.
18410 If the referenced type cannot be found an error is thrown. */
18411
18412static struct die_info *
ff39bb5e 18413follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18414 struct dwarf2_cu **ref_cu)
18415{
18416 ULONGEST signature = DW_SIGNATURE (attr);
18417 struct signatured_type *sig_type;
18418 struct die_info *die;
18419
18420 gdb_assert (attr->form == DW_FORM_ref_sig8);
18421
a2ce51a0 18422 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18423 /* sig_type will be NULL if the signatured type is missing from
18424 the debug info. */
18425 if (sig_type == NULL)
18426 {
18427 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18428 " from DIE at 0x%x [in module %s]"),
18429 hex_string (signature), src_die->offset.sect_off,
18430 (*ref_cu)->objfile->name);
18431 }
18432
18433 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18434 if (die == NULL)
18435 {
18436 dump_die_for_error (src_die);
18437 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18438 " from DIE at 0x%x [in module %s]"),
18439 hex_string (signature), src_die->offset.sect_off,
18440 (*ref_cu)->objfile->name);
18441 }
18442
18443 return die;
18444}
18445
18446/* Get the type specified by SIGNATURE referenced in DIE/CU,
18447 reading in and processing the type unit if necessary. */
18448
18449static struct type *
18450get_signatured_type (struct die_info *die, ULONGEST signature,
18451 struct dwarf2_cu *cu)
18452{
18453 struct signatured_type *sig_type;
18454 struct dwarf2_cu *type_cu;
18455 struct die_info *type_die;
18456 struct type *type;
18457
a2ce51a0 18458 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18459 /* sig_type will be NULL if the signatured type is missing from
18460 the debug info. */
18461 if (sig_type == NULL)
18462 {
18463 complaint (&symfile_complaints,
18464 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18465 " from DIE at 0x%x [in module %s]"),
18466 hex_string (signature), die->offset.sect_off,
18467 dwarf2_per_objfile->objfile->name);
18468 return build_error_marker_type (cu, die);
18469 }
18470
18471 /* If we already know the type we're done. */
18472 if (sig_type->type != NULL)
18473 return sig_type->type;
18474
18475 type_cu = cu;
18476 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18477 if (type_die != NULL)
18478 {
18479 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18480 is created. This is important, for example, because for c++ classes
18481 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18482 type = read_type_die (type_die, type_cu);
18483 if (type == NULL)
18484 {
18485 complaint (&symfile_complaints,
18486 _("Dwarf Error: Cannot build signatured type %s"
18487 " referenced from DIE at 0x%x [in module %s]"),
18488 hex_string (signature), die->offset.sect_off,
18489 dwarf2_per_objfile->objfile->name);
18490 type = build_error_marker_type (cu, die);
18491 }
18492 }
18493 else
18494 {
18495 complaint (&symfile_complaints,
18496 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18497 " from DIE at 0x%x [in module %s]"),
18498 hex_string (signature), die->offset.sect_off,
18499 dwarf2_per_objfile->objfile->name);
18500 type = build_error_marker_type (cu, die);
18501 }
18502 sig_type->type = type;
18503
18504 return type;
18505}
18506
18507/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18508 reading in and processing the type unit if necessary. */
18509
18510static struct type *
ff39bb5e 18511get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18512 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18513{
18514 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18515 if (attr_form_is_ref (attr))
ac9ec31b
DE
18516 {
18517 struct dwarf2_cu *type_cu = cu;
18518 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18519
18520 return read_type_die (type_die, type_cu);
18521 }
18522 else if (attr->form == DW_FORM_ref_sig8)
18523 {
18524 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18525 }
18526 else
18527 {
18528 complaint (&symfile_complaints,
18529 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18530 " at 0x%x [in module %s]"),
18531 dwarf_form_name (attr->form), die->offset.sect_off,
18532 dwarf2_per_objfile->objfile->name);
18533 return build_error_marker_type (cu, die);
18534 }
348e048f
DE
18535}
18536
e5fe5e75 18537/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18538
18539static void
e5fe5e75 18540load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18541{
52dc124a 18542 struct signatured_type *sig_type;
348e048f 18543
f4dc4d17
DE
18544 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18545 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18546
6721b2ec
DE
18547 /* We have the per_cu, but we need the signatured_type.
18548 Fortunately this is an easy translation. */
18549 gdb_assert (per_cu->is_debug_types);
18550 sig_type = (struct signatured_type *) per_cu;
348e048f 18551
6721b2ec 18552 gdb_assert (per_cu->cu == NULL);
348e048f 18553
52dc124a 18554 read_signatured_type (sig_type);
348e048f 18555
6721b2ec 18556 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18557}
18558
dee91e82
DE
18559/* die_reader_func for read_signatured_type.
18560 This is identical to load_full_comp_unit_reader,
18561 but is kept separate for now. */
348e048f
DE
18562
18563static void
dee91e82 18564read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18565 const gdb_byte *info_ptr,
dee91e82
DE
18566 struct die_info *comp_unit_die,
18567 int has_children,
18568 void *data)
348e048f 18569{
dee91e82 18570 struct dwarf2_cu *cu = reader->cu;
348e048f 18571
dee91e82
DE
18572 gdb_assert (cu->die_hash == NULL);
18573 cu->die_hash =
18574 htab_create_alloc_ex (cu->header.length / 12,
18575 die_hash,
18576 die_eq,
18577 NULL,
18578 &cu->comp_unit_obstack,
18579 hashtab_obstack_allocate,
18580 dummy_obstack_deallocate);
348e048f 18581
dee91e82
DE
18582 if (has_children)
18583 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18584 &info_ptr, comp_unit_die);
18585 cu->dies = comp_unit_die;
18586 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18587
18588 /* We try not to read any attributes in this function, because not
9cdd5dbd 18589 all CUs needed for references have been loaded yet, and symbol
348e048f 18590 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18591 or we won't be able to build types correctly.
18592 Similarly, if we do not read the producer, we can not apply
18593 producer-specific interpretation. */
95554aad 18594 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18595}
348e048f 18596
3019eac3
DE
18597/* Read in a signatured type and build its CU and DIEs.
18598 If the type is a stub for the real type in a DWO file,
18599 read in the real type from the DWO file as well. */
dee91e82
DE
18600
18601static void
18602read_signatured_type (struct signatured_type *sig_type)
18603{
18604 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18605
3019eac3 18606 gdb_assert (per_cu->is_debug_types);
dee91e82 18607 gdb_assert (per_cu->cu == NULL);
348e048f 18608
f4dc4d17
DE
18609 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18610 read_signatured_type_reader, NULL);
c906108c
SS
18611}
18612
c906108c
SS
18613/* Decode simple location descriptions.
18614 Given a pointer to a dwarf block that defines a location, compute
18615 the location and return the value.
18616
4cecd739
DJ
18617 NOTE drow/2003-11-18: This function is called in two situations
18618 now: for the address of static or global variables (partial symbols
18619 only) and for offsets into structures which are expected to be
18620 (more or less) constant. The partial symbol case should go away,
18621 and only the constant case should remain. That will let this
18622 function complain more accurately. A few special modes are allowed
18623 without complaint for global variables (for instance, global
18624 register values and thread-local values).
c906108c
SS
18625
18626 A location description containing no operations indicates that the
4cecd739 18627 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18628 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18629 callers will only want a very basic result and this can become a
21ae7a4d
JK
18630 complaint.
18631
18632 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18633
18634static CORE_ADDR
e7c27a73 18635decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18636{
e7c27a73 18637 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18638 size_t i;
18639 size_t size = blk->size;
d521ce57 18640 const gdb_byte *data = blk->data;
21ae7a4d
JK
18641 CORE_ADDR stack[64];
18642 int stacki;
18643 unsigned int bytes_read, unsnd;
18644 gdb_byte op;
c906108c 18645
21ae7a4d
JK
18646 i = 0;
18647 stacki = 0;
18648 stack[stacki] = 0;
18649 stack[++stacki] = 0;
18650
18651 while (i < size)
18652 {
18653 op = data[i++];
18654 switch (op)
18655 {
18656 case DW_OP_lit0:
18657 case DW_OP_lit1:
18658 case DW_OP_lit2:
18659 case DW_OP_lit3:
18660 case DW_OP_lit4:
18661 case DW_OP_lit5:
18662 case DW_OP_lit6:
18663 case DW_OP_lit7:
18664 case DW_OP_lit8:
18665 case DW_OP_lit9:
18666 case DW_OP_lit10:
18667 case DW_OP_lit11:
18668 case DW_OP_lit12:
18669 case DW_OP_lit13:
18670 case DW_OP_lit14:
18671 case DW_OP_lit15:
18672 case DW_OP_lit16:
18673 case DW_OP_lit17:
18674 case DW_OP_lit18:
18675 case DW_OP_lit19:
18676 case DW_OP_lit20:
18677 case DW_OP_lit21:
18678 case DW_OP_lit22:
18679 case DW_OP_lit23:
18680 case DW_OP_lit24:
18681 case DW_OP_lit25:
18682 case DW_OP_lit26:
18683 case DW_OP_lit27:
18684 case DW_OP_lit28:
18685 case DW_OP_lit29:
18686 case DW_OP_lit30:
18687 case DW_OP_lit31:
18688 stack[++stacki] = op - DW_OP_lit0;
18689 break;
f1bea926 18690
21ae7a4d
JK
18691 case DW_OP_reg0:
18692 case DW_OP_reg1:
18693 case DW_OP_reg2:
18694 case DW_OP_reg3:
18695 case DW_OP_reg4:
18696 case DW_OP_reg5:
18697 case DW_OP_reg6:
18698 case DW_OP_reg7:
18699 case DW_OP_reg8:
18700 case DW_OP_reg9:
18701 case DW_OP_reg10:
18702 case DW_OP_reg11:
18703 case DW_OP_reg12:
18704 case DW_OP_reg13:
18705 case DW_OP_reg14:
18706 case DW_OP_reg15:
18707 case DW_OP_reg16:
18708 case DW_OP_reg17:
18709 case DW_OP_reg18:
18710 case DW_OP_reg19:
18711 case DW_OP_reg20:
18712 case DW_OP_reg21:
18713 case DW_OP_reg22:
18714 case DW_OP_reg23:
18715 case DW_OP_reg24:
18716 case DW_OP_reg25:
18717 case DW_OP_reg26:
18718 case DW_OP_reg27:
18719 case DW_OP_reg28:
18720 case DW_OP_reg29:
18721 case DW_OP_reg30:
18722 case DW_OP_reg31:
18723 stack[++stacki] = op - DW_OP_reg0;
18724 if (i < size)
18725 dwarf2_complex_location_expr_complaint ();
18726 break;
c906108c 18727
21ae7a4d
JK
18728 case DW_OP_regx:
18729 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18730 i += bytes_read;
18731 stack[++stacki] = unsnd;
18732 if (i < size)
18733 dwarf2_complex_location_expr_complaint ();
18734 break;
c906108c 18735
21ae7a4d
JK
18736 case DW_OP_addr:
18737 stack[++stacki] = read_address (objfile->obfd, &data[i],
18738 cu, &bytes_read);
18739 i += bytes_read;
18740 break;
d53d4ac5 18741
21ae7a4d
JK
18742 case DW_OP_const1u:
18743 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18744 i += 1;
18745 break;
18746
18747 case DW_OP_const1s:
18748 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18749 i += 1;
18750 break;
18751
18752 case DW_OP_const2u:
18753 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18754 i += 2;
18755 break;
18756
18757 case DW_OP_const2s:
18758 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18759 i += 2;
18760 break;
d53d4ac5 18761
21ae7a4d
JK
18762 case DW_OP_const4u:
18763 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18764 i += 4;
18765 break;
18766
18767 case DW_OP_const4s:
18768 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18769 i += 4;
18770 break;
18771
585861ea
JK
18772 case DW_OP_const8u:
18773 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18774 i += 8;
18775 break;
18776
21ae7a4d
JK
18777 case DW_OP_constu:
18778 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18779 &bytes_read);
18780 i += bytes_read;
18781 break;
18782
18783 case DW_OP_consts:
18784 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18785 i += bytes_read;
18786 break;
18787
18788 case DW_OP_dup:
18789 stack[stacki + 1] = stack[stacki];
18790 stacki++;
18791 break;
18792
18793 case DW_OP_plus:
18794 stack[stacki - 1] += stack[stacki];
18795 stacki--;
18796 break;
18797
18798 case DW_OP_plus_uconst:
18799 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18800 &bytes_read);
18801 i += bytes_read;
18802 break;
18803
18804 case DW_OP_minus:
18805 stack[stacki - 1] -= stack[stacki];
18806 stacki--;
18807 break;
18808
18809 case DW_OP_deref:
18810 /* If we're not the last op, then we definitely can't encode
18811 this using GDB's address_class enum. This is valid for partial
18812 global symbols, although the variable's address will be bogus
18813 in the psymtab. */
18814 if (i < size)
18815 dwarf2_complex_location_expr_complaint ();
18816 break;
18817
18818 case DW_OP_GNU_push_tls_address:
18819 /* The top of the stack has the offset from the beginning
18820 of the thread control block at which the variable is located. */
18821 /* Nothing should follow this operator, so the top of stack would
18822 be returned. */
18823 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18824 address will be bogus in the psymtab. Make it always at least
18825 non-zero to not look as a variable garbage collected by linker
18826 which have DW_OP_addr 0. */
21ae7a4d
JK
18827 if (i < size)
18828 dwarf2_complex_location_expr_complaint ();
585861ea 18829 stack[stacki]++;
21ae7a4d
JK
18830 break;
18831
18832 case DW_OP_GNU_uninit:
18833 break;
18834
3019eac3 18835 case DW_OP_GNU_addr_index:
49f6c839 18836 case DW_OP_GNU_const_index:
3019eac3
DE
18837 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18838 &bytes_read);
18839 i += bytes_read;
18840 break;
18841
21ae7a4d
JK
18842 default:
18843 {
f39c6ffd 18844 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18845
18846 if (name)
18847 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18848 name);
18849 else
18850 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18851 op);
18852 }
18853
18854 return (stack[stacki]);
d53d4ac5 18855 }
3c6e0cb3 18856
21ae7a4d
JK
18857 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18858 outside of the allocated space. Also enforce minimum>0. */
18859 if (stacki >= ARRAY_SIZE (stack) - 1)
18860 {
18861 complaint (&symfile_complaints,
18862 _("location description stack overflow"));
18863 return 0;
18864 }
18865
18866 if (stacki <= 0)
18867 {
18868 complaint (&symfile_complaints,
18869 _("location description stack underflow"));
18870 return 0;
18871 }
18872 }
18873 return (stack[stacki]);
c906108c
SS
18874}
18875
18876/* memory allocation interface */
18877
c906108c 18878static struct dwarf_block *
7b5a2f43 18879dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18880{
18881 struct dwarf_block *blk;
18882
18883 blk = (struct dwarf_block *)
7b5a2f43 18884 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18885 return (blk);
18886}
18887
c906108c 18888static struct die_info *
b60c80d6 18889dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18890{
18891 struct die_info *die;
b60c80d6
DJ
18892 size_t size = sizeof (struct die_info);
18893
18894 if (num_attrs > 1)
18895 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18896
b60c80d6 18897 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18898 memset (die, 0, sizeof (struct die_info));
18899 return (die);
18900}
2e276125
JB
18901
18902\f
18903/* Macro support. */
18904
233d95b5
JK
18905/* Return file name relative to the compilation directory of file number I in
18906 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18907 responsible for freeing it. */
233d95b5 18908
2e276125 18909static char *
233d95b5 18910file_file_name (int file, struct line_header *lh)
2e276125 18911{
6a83a1e6
EZ
18912 /* Is the file number a valid index into the line header's file name
18913 table? Remember that file numbers start with one, not zero. */
18914 if (1 <= file && file <= lh->num_file_names)
18915 {
18916 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18917
233d95b5 18918 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18919 return xstrdup (fe->name);
233d95b5
JK
18920 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18921 fe->name, NULL);
6a83a1e6 18922 }
2e276125
JB
18923 else
18924 {
6a83a1e6
EZ
18925 /* The compiler produced a bogus file number. We can at least
18926 record the macro definitions made in the file, even if we
18927 won't be able to find the file by name. */
18928 char fake_name[80];
9a619af0 18929
8c042590
PM
18930 xsnprintf (fake_name, sizeof (fake_name),
18931 "<bad macro file number %d>", file);
2e276125 18932
6e70227d 18933 complaint (&symfile_complaints,
6a83a1e6
EZ
18934 _("bad file number in macro information (%d)"),
18935 file);
2e276125 18936
6a83a1e6 18937 return xstrdup (fake_name);
2e276125
JB
18938 }
18939}
18940
233d95b5
JK
18941/* Return the full name of file number I in *LH's file name table.
18942 Use COMP_DIR as the name of the current directory of the
18943 compilation. The result is allocated using xmalloc; the caller is
18944 responsible for freeing it. */
18945static char *
18946file_full_name (int file, struct line_header *lh, const char *comp_dir)
18947{
18948 /* Is the file number a valid index into the line header's file name
18949 table? Remember that file numbers start with one, not zero. */
18950 if (1 <= file && file <= lh->num_file_names)
18951 {
18952 char *relative = file_file_name (file, lh);
18953
18954 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18955 return relative;
18956 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18957 }
18958 else
18959 return file_file_name (file, lh);
18960}
18961
2e276125
JB
18962
18963static struct macro_source_file *
18964macro_start_file (int file, int line,
18965 struct macro_source_file *current_file,
18966 const char *comp_dir,
18967 struct line_header *lh, struct objfile *objfile)
18968{
233d95b5
JK
18969 /* File name relative to the compilation directory of this source file. */
18970 char *file_name = file_file_name (file, lh);
2e276125
JB
18971
18972 /* We don't create a macro table for this compilation unit
18973 at all until we actually get a filename. */
18974 if (! pending_macros)
6532ff36 18975 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
233d95b5
JK
18976 objfile->per_bfd->macro_cache,
18977 comp_dir);
2e276125
JB
18978
18979 if (! current_file)
abc9d0dc
TT
18980 {
18981 /* If we have no current file, then this must be the start_file
18982 directive for the compilation unit's main source file. */
233d95b5 18983 current_file = macro_set_main (pending_macros, file_name);
abc9d0dc
TT
18984 macro_define_special (pending_macros);
18985 }
2e276125 18986 else
233d95b5 18987 current_file = macro_include (current_file, line, file_name);
2e276125 18988
233d95b5 18989 xfree (file_name);
6e70227d 18990
2e276125
JB
18991 return current_file;
18992}
18993
18994
18995/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18996 followed by a null byte. */
18997static char *
18998copy_string (const char *buf, int len)
18999{
19000 char *s = xmalloc (len + 1);
9a619af0 19001
2e276125
JB
19002 memcpy (s, buf, len);
19003 s[len] = '\0';
2e276125
JB
19004 return s;
19005}
19006
19007
19008static const char *
19009consume_improper_spaces (const char *p, const char *body)
19010{
19011 if (*p == ' ')
19012 {
4d3c2250 19013 complaint (&symfile_complaints,
3e43a32a
MS
19014 _("macro definition contains spaces "
19015 "in formal argument list:\n`%s'"),
4d3c2250 19016 body);
2e276125
JB
19017
19018 while (*p == ' ')
19019 p++;
19020 }
19021
19022 return p;
19023}
19024
19025
19026static void
19027parse_macro_definition (struct macro_source_file *file, int line,
19028 const char *body)
19029{
19030 const char *p;
19031
19032 /* The body string takes one of two forms. For object-like macro
19033 definitions, it should be:
19034
19035 <macro name> " " <definition>
19036
19037 For function-like macro definitions, it should be:
19038
19039 <macro name> "() " <definition>
19040 or
19041 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19042
19043 Spaces may appear only where explicitly indicated, and in the
19044 <definition>.
19045
19046 The Dwarf 2 spec says that an object-like macro's name is always
19047 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19048 the space when the macro's definition is the empty string.
2e276125
JB
19049
19050 The Dwarf 2 spec says that there should be no spaces between the
19051 formal arguments in a function-like macro's formal argument list,
19052 but versions of GCC around March 2002 include spaces after the
19053 commas. */
19054
19055
19056 /* Find the extent of the macro name. The macro name is terminated
19057 by either a space or null character (for an object-like macro) or
19058 an opening paren (for a function-like macro). */
19059 for (p = body; *p; p++)
19060 if (*p == ' ' || *p == '(')
19061 break;
19062
19063 if (*p == ' ' || *p == '\0')
19064 {
19065 /* It's an object-like macro. */
19066 int name_len = p - body;
19067 char *name = copy_string (body, name_len);
19068 const char *replacement;
19069
19070 if (*p == ' ')
19071 replacement = body + name_len + 1;
19072 else
19073 {
4d3c2250 19074 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19075 replacement = body + name_len;
19076 }
6e70227d 19077
2e276125
JB
19078 macro_define_object (file, line, name, replacement);
19079
19080 xfree (name);
19081 }
19082 else if (*p == '(')
19083 {
19084 /* It's a function-like macro. */
19085 char *name = copy_string (body, p - body);
19086 int argc = 0;
19087 int argv_size = 1;
19088 char **argv = xmalloc (argv_size * sizeof (*argv));
19089
19090 p++;
19091
19092 p = consume_improper_spaces (p, body);
19093
19094 /* Parse the formal argument list. */
19095 while (*p && *p != ')')
19096 {
19097 /* Find the extent of the current argument name. */
19098 const char *arg_start = p;
19099
19100 while (*p && *p != ',' && *p != ')' && *p != ' ')
19101 p++;
19102
19103 if (! *p || p == arg_start)
4d3c2250 19104 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19105 else
19106 {
19107 /* Make sure argv has room for the new argument. */
19108 if (argc >= argv_size)
19109 {
19110 argv_size *= 2;
19111 argv = xrealloc (argv, argv_size * sizeof (*argv));
19112 }
19113
19114 argv[argc++] = copy_string (arg_start, p - arg_start);
19115 }
19116
19117 p = consume_improper_spaces (p, body);
19118
19119 /* Consume the comma, if present. */
19120 if (*p == ',')
19121 {
19122 p++;
19123
19124 p = consume_improper_spaces (p, body);
19125 }
19126 }
19127
19128 if (*p == ')')
19129 {
19130 p++;
19131
19132 if (*p == ' ')
19133 /* Perfectly formed definition, no complaints. */
19134 macro_define_function (file, line, name,
6e70227d 19135 argc, (const char **) argv,
2e276125
JB
19136 p + 1);
19137 else if (*p == '\0')
19138 {
19139 /* Complain, but do define it. */
4d3c2250 19140 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19141 macro_define_function (file, line, name,
6e70227d 19142 argc, (const char **) argv,
2e276125
JB
19143 p);
19144 }
19145 else
19146 /* Just complain. */
4d3c2250 19147 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19148 }
19149 else
19150 /* Just complain. */
4d3c2250 19151 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19152
19153 xfree (name);
19154 {
19155 int i;
19156
19157 for (i = 0; i < argc; i++)
19158 xfree (argv[i]);
19159 }
19160 xfree (argv);
19161 }
19162 else
4d3c2250 19163 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19164}
19165
cf2c3c16
TT
19166/* Skip some bytes from BYTES according to the form given in FORM.
19167 Returns the new pointer. */
2e276125 19168
d521ce57
TT
19169static const gdb_byte *
19170skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19171 enum dwarf_form form,
19172 unsigned int offset_size,
19173 struct dwarf2_section_info *section)
2e276125 19174{
cf2c3c16 19175 unsigned int bytes_read;
2e276125 19176
cf2c3c16 19177 switch (form)
2e276125 19178 {
cf2c3c16
TT
19179 case DW_FORM_data1:
19180 case DW_FORM_flag:
19181 ++bytes;
19182 break;
19183
19184 case DW_FORM_data2:
19185 bytes += 2;
19186 break;
19187
19188 case DW_FORM_data4:
19189 bytes += 4;
19190 break;
19191
19192 case DW_FORM_data8:
19193 bytes += 8;
19194 break;
19195
19196 case DW_FORM_string:
19197 read_direct_string (abfd, bytes, &bytes_read);
19198 bytes += bytes_read;
19199 break;
19200
19201 case DW_FORM_sec_offset:
19202 case DW_FORM_strp:
36586728 19203 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19204 bytes += offset_size;
19205 break;
19206
19207 case DW_FORM_block:
19208 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19209 bytes += bytes_read;
19210 break;
19211
19212 case DW_FORM_block1:
19213 bytes += 1 + read_1_byte (abfd, bytes);
19214 break;
19215 case DW_FORM_block2:
19216 bytes += 2 + read_2_bytes (abfd, bytes);
19217 break;
19218 case DW_FORM_block4:
19219 bytes += 4 + read_4_bytes (abfd, bytes);
19220 break;
19221
19222 case DW_FORM_sdata:
19223 case DW_FORM_udata:
3019eac3
DE
19224 case DW_FORM_GNU_addr_index:
19225 case DW_FORM_GNU_str_index:
d521ce57 19226 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19227 if (bytes == NULL)
19228 {
19229 dwarf2_section_buffer_overflow_complaint (section);
19230 return NULL;
19231 }
cf2c3c16
TT
19232 break;
19233
19234 default:
19235 {
19236 complain:
19237 complaint (&symfile_complaints,
19238 _("invalid form 0x%x in `%s'"),
19239 form,
19240 section->asection->name);
19241 return NULL;
19242 }
2e276125
JB
19243 }
19244
cf2c3c16
TT
19245 return bytes;
19246}
757a13d0 19247
cf2c3c16
TT
19248/* A helper for dwarf_decode_macros that handles skipping an unknown
19249 opcode. Returns an updated pointer to the macro data buffer; or,
19250 on error, issues a complaint and returns NULL. */
757a13d0 19251
d521ce57 19252static const gdb_byte *
cf2c3c16 19253skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19254 const gdb_byte **opcode_definitions,
19255 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19256 bfd *abfd,
19257 unsigned int offset_size,
19258 struct dwarf2_section_info *section)
19259{
19260 unsigned int bytes_read, i;
19261 unsigned long arg;
d521ce57 19262 const gdb_byte *defn;
2e276125 19263
cf2c3c16 19264 if (opcode_definitions[opcode] == NULL)
2e276125 19265 {
cf2c3c16
TT
19266 complaint (&symfile_complaints,
19267 _("unrecognized DW_MACFINO opcode 0x%x"),
19268 opcode);
19269 return NULL;
19270 }
2e276125 19271
cf2c3c16
TT
19272 defn = opcode_definitions[opcode];
19273 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19274 defn += bytes_read;
2e276125 19275
cf2c3c16
TT
19276 for (i = 0; i < arg; ++i)
19277 {
f664829e
DE
19278 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19279 section);
cf2c3c16
TT
19280 if (mac_ptr == NULL)
19281 {
19282 /* skip_form_bytes already issued the complaint. */
19283 return NULL;
19284 }
19285 }
757a13d0 19286
cf2c3c16
TT
19287 return mac_ptr;
19288}
757a13d0 19289
cf2c3c16
TT
19290/* A helper function which parses the header of a macro section.
19291 If the macro section is the extended (for now called "GNU") type,
19292 then this updates *OFFSET_SIZE. Returns a pointer to just after
19293 the header, or issues a complaint and returns NULL on error. */
757a13d0 19294
d521ce57
TT
19295static const gdb_byte *
19296dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19297 bfd *abfd,
d521ce57 19298 const gdb_byte *mac_ptr,
cf2c3c16
TT
19299 unsigned int *offset_size,
19300 int section_is_gnu)
19301{
19302 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19303
cf2c3c16
TT
19304 if (section_is_gnu)
19305 {
19306 unsigned int version, flags;
757a13d0 19307
cf2c3c16
TT
19308 version = read_2_bytes (abfd, mac_ptr);
19309 if (version != 4)
19310 {
19311 complaint (&symfile_complaints,
19312 _("unrecognized version `%d' in .debug_macro section"),
19313 version);
19314 return NULL;
19315 }
19316 mac_ptr += 2;
757a13d0 19317
cf2c3c16
TT
19318 flags = read_1_byte (abfd, mac_ptr);
19319 ++mac_ptr;
19320 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19321
cf2c3c16
TT
19322 if ((flags & 2) != 0)
19323 /* We don't need the line table offset. */
19324 mac_ptr += *offset_size;
757a13d0 19325
cf2c3c16
TT
19326 /* Vendor opcode descriptions. */
19327 if ((flags & 4) != 0)
19328 {
19329 unsigned int i, count;
757a13d0 19330
cf2c3c16
TT
19331 count = read_1_byte (abfd, mac_ptr);
19332 ++mac_ptr;
19333 for (i = 0; i < count; ++i)
19334 {
19335 unsigned int opcode, bytes_read;
19336 unsigned long arg;
19337
19338 opcode = read_1_byte (abfd, mac_ptr);
19339 ++mac_ptr;
19340 opcode_definitions[opcode] = mac_ptr;
19341 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19342 mac_ptr += bytes_read;
19343 mac_ptr += arg;
19344 }
757a13d0 19345 }
cf2c3c16 19346 }
757a13d0 19347
cf2c3c16
TT
19348 return mac_ptr;
19349}
757a13d0 19350
cf2c3c16 19351/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19352 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19353
19354static void
d521ce57
TT
19355dwarf_decode_macro_bytes (bfd *abfd,
19356 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19357 struct macro_source_file *current_file,
15d034d0 19358 struct line_header *lh, const char *comp_dir,
cf2c3c16 19359 struct dwarf2_section_info *section,
36586728 19360 int section_is_gnu, int section_is_dwz,
cf2c3c16 19361 unsigned int offset_size,
8fc3fc34
TT
19362 struct objfile *objfile,
19363 htab_t include_hash)
cf2c3c16
TT
19364{
19365 enum dwarf_macro_record_type macinfo_type;
19366 int at_commandline;
d521ce57 19367 const gdb_byte *opcode_definitions[256];
757a13d0 19368
cf2c3c16
TT
19369 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19370 &offset_size, section_is_gnu);
19371 if (mac_ptr == NULL)
19372 {
19373 /* We already issued a complaint. */
19374 return;
19375 }
757a13d0
JK
19376
19377 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19378 GDB is still reading the definitions from command line. First
19379 DW_MACINFO_start_file will need to be ignored as it was already executed
19380 to create CURRENT_FILE for the main source holding also the command line
19381 definitions. On first met DW_MACINFO_start_file this flag is reset to
19382 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19383
19384 at_commandline = 1;
19385
19386 do
19387 {
19388 /* Do we at least have room for a macinfo type byte? */
19389 if (mac_ptr >= mac_end)
19390 {
f664829e 19391 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19392 break;
19393 }
19394
19395 macinfo_type = read_1_byte (abfd, mac_ptr);
19396 mac_ptr++;
19397
cf2c3c16
TT
19398 /* Note that we rely on the fact that the corresponding GNU and
19399 DWARF constants are the same. */
757a13d0
JK
19400 switch (macinfo_type)
19401 {
19402 /* A zero macinfo type indicates the end of the macro
19403 information. */
19404 case 0:
19405 break;
2e276125 19406
cf2c3c16
TT
19407 case DW_MACRO_GNU_define:
19408 case DW_MACRO_GNU_undef:
19409 case DW_MACRO_GNU_define_indirect:
19410 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19411 case DW_MACRO_GNU_define_indirect_alt:
19412 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19413 {
891d2f0b 19414 unsigned int bytes_read;
2e276125 19415 int line;
d521ce57 19416 const char *body;
cf2c3c16 19417 int is_define;
2e276125 19418
cf2c3c16
TT
19419 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19420 mac_ptr += bytes_read;
19421
19422 if (macinfo_type == DW_MACRO_GNU_define
19423 || macinfo_type == DW_MACRO_GNU_undef)
19424 {
19425 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19426 mac_ptr += bytes_read;
19427 }
19428 else
19429 {
19430 LONGEST str_offset;
19431
19432 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19433 mac_ptr += offset_size;
2e276125 19434
36586728 19435 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19436 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19437 || section_is_dwz)
36586728
TT
19438 {
19439 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19440
19441 body = read_indirect_string_from_dwz (dwz, str_offset);
19442 }
19443 else
19444 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19445 }
19446
19447 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19448 || macinfo_type == DW_MACRO_GNU_define_indirect
19449 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19450 if (! current_file)
757a13d0
JK
19451 {
19452 /* DWARF violation as no main source is present. */
19453 complaint (&symfile_complaints,
19454 _("debug info with no main source gives macro %s "
19455 "on line %d: %s"),
cf2c3c16
TT
19456 is_define ? _("definition") : _("undefinition"),
19457 line, body);
757a13d0
JK
19458 break;
19459 }
3e43a32a
MS
19460 if ((line == 0 && !at_commandline)
19461 || (line != 0 && at_commandline))
4d3c2250 19462 complaint (&symfile_complaints,
757a13d0
JK
19463 _("debug info gives %s macro %s with %s line %d: %s"),
19464 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19465 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19466 line == 0 ? _("zero") : _("non-zero"), line, body);
19467
cf2c3c16 19468 if (is_define)
757a13d0 19469 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19470 else
19471 {
19472 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19473 || macinfo_type == DW_MACRO_GNU_undef_indirect
19474 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19475 macro_undef (current_file, line, body);
19476 }
2e276125
JB
19477 }
19478 break;
19479
cf2c3c16 19480 case DW_MACRO_GNU_start_file:
2e276125 19481 {
891d2f0b 19482 unsigned int bytes_read;
2e276125
JB
19483 int line, file;
19484
19485 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19486 mac_ptr += bytes_read;
19487 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19488 mac_ptr += bytes_read;
19489
3e43a32a
MS
19490 if ((line == 0 && !at_commandline)
19491 || (line != 0 && at_commandline))
757a13d0
JK
19492 complaint (&symfile_complaints,
19493 _("debug info gives source %d included "
19494 "from %s at %s line %d"),
19495 file, at_commandline ? _("command-line") : _("file"),
19496 line == 0 ? _("zero") : _("non-zero"), line);
19497
19498 if (at_commandline)
19499 {
cf2c3c16
TT
19500 /* This DW_MACRO_GNU_start_file was executed in the
19501 pass one. */
757a13d0
JK
19502 at_commandline = 0;
19503 }
19504 else
19505 current_file = macro_start_file (file, line,
19506 current_file, comp_dir,
cf2c3c16 19507 lh, objfile);
2e276125
JB
19508 }
19509 break;
19510
cf2c3c16 19511 case DW_MACRO_GNU_end_file:
2e276125 19512 if (! current_file)
4d3c2250 19513 complaint (&symfile_complaints,
3e43a32a
MS
19514 _("macro debug info has an unmatched "
19515 "`close_file' directive"));
2e276125
JB
19516 else
19517 {
19518 current_file = current_file->included_by;
19519 if (! current_file)
19520 {
cf2c3c16 19521 enum dwarf_macro_record_type next_type;
2e276125
JB
19522
19523 /* GCC circa March 2002 doesn't produce the zero
19524 type byte marking the end of the compilation
19525 unit. Complain if it's not there, but exit no
19526 matter what. */
19527
19528 /* Do we at least have room for a macinfo type byte? */
19529 if (mac_ptr >= mac_end)
19530 {
f664829e 19531 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19532 return;
19533 }
19534
19535 /* We don't increment mac_ptr here, so this is just
19536 a look-ahead. */
19537 next_type = read_1_byte (abfd, mac_ptr);
19538 if (next_type != 0)
4d3c2250 19539 complaint (&symfile_complaints,
3e43a32a
MS
19540 _("no terminating 0-type entry for "
19541 "macros in `.debug_macinfo' section"));
2e276125
JB
19542
19543 return;
19544 }
19545 }
19546 break;
19547
cf2c3c16 19548 case DW_MACRO_GNU_transparent_include:
36586728 19549 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19550 {
19551 LONGEST offset;
8fc3fc34 19552 void **slot;
a036ba48
TT
19553 bfd *include_bfd = abfd;
19554 struct dwarf2_section_info *include_section = section;
19555 struct dwarf2_section_info alt_section;
d521ce57 19556 const gdb_byte *include_mac_end = mac_end;
a036ba48 19557 int is_dwz = section_is_dwz;
d521ce57 19558 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19559
19560 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19561 mac_ptr += offset_size;
19562
a036ba48
TT
19563 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19564 {
19565 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19566
19567 dwarf2_read_section (dwarf2_per_objfile->objfile,
19568 &dwz->macro);
19569
19570 include_bfd = dwz->macro.asection->owner;
19571 include_section = &dwz->macro;
19572 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19573 is_dwz = 1;
19574 }
19575
19576 new_mac_ptr = include_section->buffer + offset;
19577 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19578
8fc3fc34
TT
19579 if (*slot != NULL)
19580 {
19581 /* This has actually happened; see
19582 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19583 complaint (&symfile_complaints,
19584 _("recursive DW_MACRO_GNU_transparent_include in "
19585 ".debug_macro section"));
19586 }
19587 else
19588 {
d521ce57 19589 *slot = (void *) new_mac_ptr;
36586728 19590
a036ba48 19591 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19592 include_mac_end, current_file,
8fc3fc34 19593 lh, comp_dir,
36586728 19594 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19595 offset_size, objfile, include_hash);
19596
d521ce57 19597 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19598 }
cf2c3c16
TT
19599 }
19600 break;
19601
2e276125 19602 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19603 if (!section_is_gnu)
19604 {
19605 unsigned int bytes_read;
19606 int constant;
2e276125 19607
cf2c3c16
TT
19608 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19609 mac_ptr += bytes_read;
19610 read_direct_string (abfd, mac_ptr, &bytes_read);
19611 mac_ptr += bytes_read;
2e276125 19612
cf2c3c16
TT
19613 /* We don't recognize any vendor extensions. */
19614 break;
19615 }
19616 /* FALLTHROUGH */
19617
19618 default:
19619 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19620 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19621 section);
19622 if (mac_ptr == NULL)
19623 return;
19624 break;
2e276125 19625 }
757a13d0 19626 } while (macinfo_type != 0);
2e276125 19627}
8e19ed76 19628
cf2c3c16 19629static void
09262596 19630dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19631 const char *comp_dir, int section_is_gnu)
cf2c3c16 19632{
bb5ed363 19633 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19634 struct line_header *lh = cu->line_header;
19635 bfd *abfd;
d521ce57 19636 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19637 struct macro_source_file *current_file = 0;
19638 enum dwarf_macro_record_type macinfo_type;
19639 unsigned int offset_size = cu->header.offset_size;
d521ce57 19640 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19641 struct cleanup *cleanup;
19642 htab_t include_hash;
19643 void **slot;
09262596
DE
19644 struct dwarf2_section_info *section;
19645 const char *section_name;
19646
19647 if (cu->dwo_unit != NULL)
19648 {
19649 if (section_is_gnu)
19650 {
19651 section = &cu->dwo_unit->dwo_file->sections.macro;
19652 section_name = ".debug_macro.dwo";
19653 }
19654 else
19655 {
19656 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19657 section_name = ".debug_macinfo.dwo";
19658 }
19659 }
19660 else
19661 {
19662 if (section_is_gnu)
19663 {
19664 section = &dwarf2_per_objfile->macro;
19665 section_name = ".debug_macro";
19666 }
19667 else
19668 {
19669 section = &dwarf2_per_objfile->macinfo;
19670 section_name = ".debug_macinfo";
19671 }
19672 }
cf2c3c16 19673
bb5ed363 19674 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19675 if (section->buffer == NULL)
19676 {
fceca515 19677 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19678 return;
19679 }
09262596 19680 abfd = section->asection->owner;
cf2c3c16
TT
19681
19682 /* First pass: Find the name of the base filename.
19683 This filename is needed in order to process all macros whose definition
19684 (or undefinition) comes from the command line. These macros are defined
19685 before the first DW_MACINFO_start_file entry, and yet still need to be
19686 associated to the base file.
19687
19688 To determine the base file name, we scan the macro definitions until we
19689 reach the first DW_MACINFO_start_file entry. We then initialize
19690 CURRENT_FILE accordingly so that any macro definition found before the
19691 first DW_MACINFO_start_file can still be associated to the base file. */
19692
19693 mac_ptr = section->buffer + offset;
19694 mac_end = section->buffer + section->size;
19695
19696 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19697 &offset_size, section_is_gnu);
19698 if (mac_ptr == NULL)
19699 {
19700 /* We already issued a complaint. */
19701 return;
19702 }
19703
19704 do
19705 {
19706 /* Do we at least have room for a macinfo type byte? */
19707 if (mac_ptr >= mac_end)
19708 {
19709 /* Complaint is printed during the second pass as GDB will probably
19710 stop the first pass earlier upon finding
19711 DW_MACINFO_start_file. */
19712 break;
19713 }
19714
19715 macinfo_type = read_1_byte (abfd, mac_ptr);
19716 mac_ptr++;
19717
19718 /* Note that we rely on the fact that the corresponding GNU and
19719 DWARF constants are the same. */
19720 switch (macinfo_type)
19721 {
19722 /* A zero macinfo type indicates the end of the macro
19723 information. */
19724 case 0:
19725 break;
19726
19727 case DW_MACRO_GNU_define:
19728 case DW_MACRO_GNU_undef:
19729 /* Only skip the data by MAC_PTR. */
19730 {
19731 unsigned int bytes_read;
19732
19733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19734 mac_ptr += bytes_read;
19735 read_direct_string (abfd, mac_ptr, &bytes_read);
19736 mac_ptr += bytes_read;
19737 }
19738 break;
19739
19740 case DW_MACRO_GNU_start_file:
19741 {
19742 unsigned int bytes_read;
19743 int line, file;
19744
19745 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19746 mac_ptr += bytes_read;
19747 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19748 mac_ptr += bytes_read;
19749
19750 current_file = macro_start_file (file, line, current_file,
bb5ed363 19751 comp_dir, lh, objfile);
cf2c3c16
TT
19752 }
19753 break;
19754
19755 case DW_MACRO_GNU_end_file:
19756 /* No data to skip by MAC_PTR. */
19757 break;
19758
19759 case DW_MACRO_GNU_define_indirect:
19760 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19761 case DW_MACRO_GNU_define_indirect_alt:
19762 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19763 {
19764 unsigned int bytes_read;
19765
19766 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19767 mac_ptr += bytes_read;
19768 mac_ptr += offset_size;
19769 }
19770 break;
19771
19772 case DW_MACRO_GNU_transparent_include:
f7a35f02 19773 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19774 /* Note that, according to the spec, a transparent include
19775 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19776 skip this opcode. */
19777 mac_ptr += offset_size;
19778 break;
19779
19780 case DW_MACINFO_vendor_ext:
19781 /* Only skip the data by MAC_PTR. */
19782 if (!section_is_gnu)
19783 {
19784 unsigned int bytes_read;
19785
19786 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19787 mac_ptr += bytes_read;
19788 read_direct_string (abfd, mac_ptr, &bytes_read);
19789 mac_ptr += bytes_read;
19790 }
19791 /* FALLTHROUGH */
19792
19793 default:
19794 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19795 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19796 section);
19797 if (mac_ptr == NULL)
19798 return;
19799 break;
19800 }
19801 } while (macinfo_type != 0 && current_file == NULL);
19802
19803 /* Second pass: Process all entries.
19804
19805 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19806 command-line macro definitions/undefinitions. This flag is unset when we
19807 reach the first DW_MACINFO_start_file entry. */
19808
8fc3fc34
TT
19809 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19810 NULL, xcalloc, xfree);
19811 cleanup = make_cleanup_htab_delete (include_hash);
19812 mac_ptr = section->buffer + offset;
19813 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19814 *slot = (void *) mac_ptr;
8fc3fc34 19815 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19816 current_file, lh, comp_dir, section,
19817 section_is_gnu, 0,
8fc3fc34
TT
19818 offset_size, objfile, include_hash);
19819 do_cleanups (cleanup);
cf2c3c16
TT
19820}
19821
8e19ed76 19822/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19823 if so return true else false. */
380bca97 19824
8e19ed76 19825static int
6e5a29e1 19826attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
19827{
19828 return (attr == NULL ? 0 :
19829 attr->form == DW_FORM_block1
19830 || attr->form == DW_FORM_block2
19831 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19832 || attr->form == DW_FORM_block
19833 || attr->form == DW_FORM_exprloc);
8e19ed76 19834}
4c2df51b 19835
c6a0999f
JB
19836/* Return non-zero if ATTR's value is a section offset --- classes
19837 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19838 You may use DW_UNSND (attr) to retrieve such offsets.
19839
19840 Section 7.5.4, "Attribute Encodings", explains that no attribute
19841 may have a value that belongs to more than one of these classes; it
19842 would be ambiguous if we did, because we use the same forms for all
19843 of them. */
380bca97 19844
3690dd37 19845static int
6e5a29e1 19846attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
19847{
19848 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19849 || attr->form == DW_FORM_data8
19850 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19851}
19852
3690dd37
JB
19853/* Return non-zero if ATTR's value falls in the 'constant' class, or
19854 zero otherwise. When this function returns true, you can apply
19855 dwarf2_get_attr_constant_value to it.
19856
19857 However, note that for some attributes you must check
19858 attr_form_is_section_offset before using this test. DW_FORM_data4
19859 and DW_FORM_data8 are members of both the constant class, and of
19860 the classes that contain offsets into other debug sections
19861 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19862 that, if an attribute's can be either a constant or one of the
19863 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19864 taken as section offsets, not constants. */
380bca97 19865
3690dd37 19866static int
6e5a29e1 19867attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
19868{
19869 switch (attr->form)
19870 {
19871 case DW_FORM_sdata:
19872 case DW_FORM_udata:
19873 case DW_FORM_data1:
19874 case DW_FORM_data2:
19875 case DW_FORM_data4:
19876 case DW_FORM_data8:
19877 return 1;
19878 default:
19879 return 0;
19880 }
19881}
19882
7771576e
SA
19883
19884/* DW_ADDR is always stored already as sect_offset; despite for the forms
19885 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19886
19887static int
6e5a29e1 19888attr_form_is_ref (const struct attribute *attr)
7771576e
SA
19889{
19890 switch (attr->form)
19891 {
19892 case DW_FORM_ref_addr:
19893 case DW_FORM_ref1:
19894 case DW_FORM_ref2:
19895 case DW_FORM_ref4:
19896 case DW_FORM_ref8:
19897 case DW_FORM_ref_udata:
19898 case DW_FORM_GNU_ref_alt:
19899 return 1;
19900 default:
19901 return 0;
19902 }
19903}
19904
3019eac3
DE
19905/* Return the .debug_loc section to use for CU.
19906 For DWO files use .debug_loc.dwo. */
19907
19908static struct dwarf2_section_info *
19909cu_debug_loc_section (struct dwarf2_cu *cu)
19910{
19911 if (cu->dwo_unit)
19912 return &cu->dwo_unit->dwo_file->sections.loc;
19913 return &dwarf2_per_objfile->loc;
19914}
19915
8cf6f0b1
TT
19916/* A helper function that fills in a dwarf2_loclist_baton. */
19917
19918static void
19919fill_in_loclist_baton (struct dwarf2_cu *cu,
19920 struct dwarf2_loclist_baton *baton,
ff39bb5e 19921 const struct attribute *attr)
8cf6f0b1 19922{
3019eac3
DE
19923 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19924
19925 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19926
19927 baton->per_cu = cu->per_cu;
19928 gdb_assert (baton->per_cu);
19929 /* We don't know how long the location list is, but make sure we
19930 don't run off the edge of the section. */
3019eac3
DE
19931 baton->size = section->size - DW_UNSND (attr);
19932 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19933 baton->base_address = cu->base_address;
f664829e 19934 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19935}
19936
4c2df51b 19937static void
ff39bb5e 19938dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 19939 struct dwarf2_cu *cu, int is_block)
4c2df51b 19940{
bb5ed363 19941 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19942 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19943
3690dd37 19944 if (attr_form_is_section_offset (attr)
3019eac3 19945 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19946 the section. If so, fall through to the complaint in the
19947 other branch. */
3019eac3 19948 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19949 {
0d53c4c4 19950 struct dwarf2_loclist_baton *baton;
4c2df51b 19951
bb5ed363 19952 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19953 sizeof (struct dwarf2_loclist_baton));
4c2df51b 19954
8cf6f0b1 19955 fill_in_loclist_baton (cu, baton, attr);
be391dca 19956
d00adf39 19957 if (cu->base_known == 0)
0d53c4c4 19958 complaint (&symfile_complaints,
3e43a32a
MS
19959 _("Location list used without "
19960 "specifying the CU base address."));
4c2df51b 19961
f1e6e072
TT
19962 SYMBOL_ACLASS_INDEX (sym) = (is_block
19963 ? dwarf2_loclist_block_index
19964 : dwarf2_loclist_index);
0d53c4c4
DJ
19965 SYMBOL_LOCATION_BATON (sym) = baton;
19966 }
19967 else
19968 {
19969 struct dwarf2_locexpr_baton *baton;
19970
bb5ed363 19971 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 19972 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
19973 baton->per_cu = cu->per_cu;
19974 gdb_assert (baton->per_cu);
0d53c4c4
DJ
19975
19976 if (attr_form_is_block (attr))
19977 {
19978 /* Note that we're just copying the block's data pointer
19979 here, not the actual data. We're still pointing into the
6502dd73
DJ
19980 info_buffer for SYM's objfile; right now we never release
19981 that buffer, but when we do clean up properly this may
19982 need to change. */
0d53c4c4
DJ
19983 baton->size = DW_BLOCK (attr)->size;
19984 baton->data = DW_BLOCK (attr)->data;
19985 }
19986 else
19987 {
19988 dwarf2_invalid_attrib_class_complaint ("location description",
19989 SYMBOL_NATURAL_NAME (sym));
19990 baton->size = 0;
0d53c4c4 19991 }
6e70227d 19992
f1e6e072
TT
19993 SYMBOL_ACLASS_INDEX (sym) = (is_block
19994 ? dwarf2_locexpr_block_index
19995 : dwarf2_locexpr_index);
0d53c4c4
DJ
19996 SYMBOL_LOCATION_BATON (sym) = baton;
19997 }
4c2df51b 19998}
6502dd73 19999
9aa1f1e3
TT
20000/* Return the OBJFILE associated with the compilation unit CU. If CU
20001 came from a separate debuginfo file, then the master objfile is
20002 returned. */
ae0d2f24
UW
20003
20004struct objfile *
20005dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20006{
9291a0cd 20007 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
20008
20009 /* Return the master objfile, so that we can report and look up the
20010 correct file containing this variable. */
20011 if (objfile->separate_debug_objfile_backlink)
20012 objfile = objfile->separate_debug_objfile_backlink;
20013
20014 return objfile;
20015}
20016
96408a79
SA
20017/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20018 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20019 CU_HEADERP first. */
20020
20021static const struct comp_unit_head *
20022per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20023 struct dwarf2_per_cu_data *per_cu)
20024{
d521ce57 20025 const gdb_byte *info_ptr;
96408a79
SA
20026
20027 if (per_cu->cu)
20028 return &per_cu->cu->header;
20029
8a0459fd 20030 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20031
20032 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20033 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20034
20035 return cu_headerp;
20036}
20037
ae0d2f24
UW
20038/* Return the address size given in the compilation unit header for CU. */
20039
98714339 20040int
ae0d2f24
UW
20041dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20042{
96408a79
SA
20043 struct comp_unit_head cu_header_local;
20044 const struct comp_unit_head *cu_headerp;
c471e790 20045
96408a79
SA
20046 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20047
20048 return cu_headerp->addr_size;
ae0d2f24
UW
20049}
20050
9eae7c52
TT
20051/* Return the offset size given in the compilation unit header for CU. */
20052
20053int
20054dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20055{
96408a79
SA
20056 struct comp_unit_head cu_header_local;
20057 const struct comp_unit_head *cu_headerp;
9c6c53f7 20058
96408a79
SA
20059 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20060
20061 return cu_headerp->offset_size;
20062}
20063
20064/* See its dwarf2loc.h declaration. */
20065
20066int
20067dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20068{
20069 struct comp_unit_head cu_header_local;
20070 const struct comp_unit_head *cu_headerp;
20071
20072 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20073
20074 if (cu_headerp->version == 2)
20075 return cu_headerp->addr_size;
20076 else
20077 return cu_headerp->offset_size;
181cebd4
JK
20078}
20079
9aa1f1e3
TT
20080/* Return the text offset of the CU. The returned offset comes from
20081 this CU's objfile. If this objfile came from a separate debuginfo
20082 file, then the offset may be different from the corresponding
20083 offset in the parent objfile. */
20084
20085CORE_ADDR
20086dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20087{
bb3fa9d0 20088 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20089
20090 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20091}
20092
348e048f
DE
20093/* Locate the .debug_info compilation unit from CU's objfile which contains
20094 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20095
20096static struct dwarf2_per_cu_data *
b64f50a1 20097dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20098 unsigned int offset_in_dwz,
ae038cb0
DJ
20099 struct objfile *objfile)
20100{
20101 struct dwarf2_per_cu_data *this_cu;
20102 int low, high;
36586728 20103 const sect_offset *cu_off;
ae038cb0 20104
ae038cb0
DJ
20105 low = 0;
20106 high = dwarf2_per_objfile->n_comp_units - 1;
20107 while (high > low)
20108 {
36586728 20109 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20110 int mid = low + (high - low) / 2;
9a619af0 20111
36586728
TT
20112 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20113 cu_off = &mid_cu->offset;
20114 if (mid_cu->is_dwz > offset_in_dwz
20115 || (mid_cu->is_dwz == offset_in_dwz
20116 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20117 high = mid;
20118 else
20119 low = mid + 1;
20120 }
20121 gdb_assert (low == high);
36586728
TT
20122 this_cu = dwarf2_per_objfile->all_comp_units[low];
20123 cu_off = &this_cu->offset;
20124 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20125 {
36586728 20126 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20127 error (_("Dwarf Error: could not find partial DIE containing "
20128 "offset 0x%lx [in module %s]"),
b64f50a1 20129 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20130
b64f50a1
JK
20131 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20132 <= offset.sect_off);
ae038cb0
DJ
20133 return dwarf2_per_objfile->all_comp_units[low-1];
20134 }
20135 else
20136 {
20137 this_cu = dwarf2_per_objfile->all_comp_units[low];
20138 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20139 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20140 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20141 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20142 return this_cu;
20143 }
20144}
20145
23745b47 20146/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20147
9816fde3 20148static void
23745b47 20149init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20150{
9816fde3 20151 memset (cu, 0, sizeof (*cu));
23745b47
DE
20152 per_cu->cu = cu;
20153 cu->per_cu = per_cu;
20154 cu->objfile = per_cu->objfile;
93311388 20155 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20156}
20157
20158/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20159
20160static void
95554aad
TT
20161prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20162 enum language pretend_language)
9816fde3
JK
20163{
20164 struct attribute *attr;
20165
20166 /* Set the language we're debugging. */
20167 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20168 if (attr)
20169 set_cu_language (DW_UNSND (attr), cu);
20170 else
9cded63f 20171 {
95554aad 20172 cu->language = pretend_language;
9cded63f
TT
20173 cu->language_defn = language_def (cu->language);
20174 }
dee91e82
DE
20175
20176 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20177 if (attr)
20178 cu->producer = DW_STRING (attr);
93311388
DE
20179}
20180
ae038cb0
DJ
20181/* Release one cached compilation unit, CU. We unlink it from the tree
20182 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20183 the caller is responsible for that.
20184 NOTE: DATA is a void * because this function is also used as a
20185 cleanup routine. */
ae038cb0
DJ
20186
20187static void
68dc6402 20188free_heap_comp_unit (void *data)
ae038cb0
DJ
20189{
20190 struct dwarf2_cu *cu = data;
20191
23745b47
DE
20192 gdb_assert (cu->per_cu != NULL);
20193 cu->per_cu->cu = NULL;
ae038cb0
DJ
20194 cu->per_cu = NULL;
20195
20196 obstack_free (&cu->comp_unit_obstack, NULL);
20197
20198 xfree (cu);
20199}
20200
72bf9492 20201/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20202 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20203 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20204
20205static void
20206free_stack_comp_unit (void *data)
20207{
20208 struct dwarf2_cu *cu = data;
20209
23745b47
DE
20210 gdb_assert (cu->per_cu != NULL);
20211 cu->per_cu->cu = NULL;
20212 cu->per_cu = NULL;
20213
72bf9492
DJ
20214 obstack_free (&cu->comp_unit_obstack, NULL);
20215 cu->partial_dies = NULL;
ae038cb0
DJ
20216}
20217
20218/* Free all cached compilation units. */
20219
20220static void
20221free_cached_comp_units (void *data)
20222{
20223 struct dwarf2_per_cu_data *per_cu, **last_chain;
20224
20225 per_cu = dwarf2_per_objfile->read_in_chain;
20226 last_chain = &dwarf2_per_objfile->read_in_chain;
20227 while (per_cu != NULL)
20228 {
20229 struct dwarf2_per_cu_data *next_cu;
20230
20231 next_cu = per_cu->cu->read_in_chain;
20232
68dc6402 20233 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20234 *last_chain = next_cu;
20235
20236 per_cu = next_cu;
20237 }
20238}
20239
20240/* Increase the age counter on each cached compilation unit, and free
20241 any that are too old. */
20242
20243static void
20244age_cached_comp_units (void)
20245{
20246 struct dwarf2_per_cu_data *per_cu, **last_chain;
20247
20248 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20249 per_cu = dwarf2_per_objfile->read_in_chain;
20250 while (per_cu != NULL)
20251 {
20252 per_cu->cu->last_used ++;
20253 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20254 dwarf2_mark (per_cu->cu);
20255 per_cu = per_cu->cu->read_in_chain;
20256 }
20257
20258 per_cu = dwarf2_per_objfile->read_in_chain;
20259 last_chain = &dwarf2_per_objfile->read_in_chain;
20260 while (per_cu != NULL)
20261 {
20262 struct dwarf2_per_cu_data *next_cu;
20263
20264 next_cu = per_cu->cu->read_in_chain;
20265
20266 if (!per_cu->cu->mark)
20267 {
68dc6402 20268 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20269 *last_chain = next_cu;
20270 }
20271 else
20272 last_chain = &per_cu->cu->read_in_chain;
20273
20274 per_cu = next_cu;
20275 }
20276}
20277
20278/* Remove a single compilation unit from the cache. */
20279
20280static void
dee91e82 20281free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20282{
20283 struct dwarf2_per_cu_data *per_cu, **last_chain;
20284
20285 per_cu = dwarf2_per_objfile->read_in_chain;
20286 last_chain = &dwarf2_per_objfile->read_in_chain;
20287 while (per_cu != NULL)
20288 {
20289 struct dwarf2_per_cu_data *next_cu;
20290
20291 next_cu = per_cu->cu->read_in_chain;
20292
dee91e82 20293 if (per_cu == target_per_cu)
ae038cb0 20294 {
68dc6402 20295 free_heap_comp_unit (per_cu->cu);
dee91e82 20296 per_cu->cu = NULL;
ae038cb0
DJ
20297 *last_chain = next_cu;
20298 break;
20299 }
20300 else
20301 last_chain = &per_cu->cu->read_in_chain;
20302
20303 per_cu = next_cu;
20304 }
20305}
20306
fe3e1990
DJ
20307/* Release all extra memory associated with OBJFILE. */
20308
20309void
20310dwarf2_free_objfile (struct objfile *objfile)
20311{
20312 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20313
20314 if (dwarf2_per_objfile == NULL)
20315 return;
20316
20317 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20318 free_cached_comp_units (NULL);
20319
7b9f3c50
DE
20320 if (dwarf2_per_objfile->quick_file_names_table)
20321 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20322
fe3e1990
DJ
20323 /* Everything else should be on the objfile obstack. */
20324}
20325
dee91e82
DE
20326/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20327 We store these in a hash table separate from the DIEs, and preserve them
20328 when the DIEs are flushed out of cache.
20329
20330 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20331 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20332 or the type may come from a DWO file. Furthermore, while it's more logical
20333 to use per_cu->section+offset, with Fission the section with the data is in
20334 the DWO file but we don't know that section at the point we need it.
20335 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20336 because we can enter the lookup routine, get_die_type_at_offset, from
20337 outside this file, and thus won't necessarily have PER_CU->cu.
20338 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20339
dee91e82 20340struct dwarf2_per_cu_offset_and_type
1c379e20 20341{
dee91e82 20342 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20343 sect_offset offset;
1c379e20
DJ
20344 struct type *type;
20345};
20346
dee91e82 20347/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20348
20349static hashval_t
dee91e82 20350per_cu_offset_and_type_hash (const void *item)
1c379e20 20351{
dee91e82 20352 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20353
dee91e82 20354 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20355}
20356
dee91e82 20357/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20358
20359static int
dee91e82 20360per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20361{
dee91e82
DE
20362 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20363 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20364
dee91e82
DE
20365 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20366 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20367}
20368
20369/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20370 table if necessary. For convenience, return TYPE.
20371
20372 The DIEs reading must have careful ordering to:
20373 * Not cause infite loops trying to read in DIEs as a prerequisite for
20374 reading current DIE.
20375 * Not trying to dereference contents of still incompletely read in types
20376 while reading in other DIEs.
20377 * Enable referencing still incompletely read in types just by a pointer to
20378 the type without accessing its fields.
20379
20380 Therefore caller should follow these rules:
20381 * Try to fetch any prerequisite types we may need to build this DIE type
20382 before building the type and calling set_die_type.
e71ec853 20383 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20384 possible before fetching more types to complete the current type.
20385 * Make the type as complete as possible before fetching more types. */
1c379e20 20386
f792889a 20387static struct type *
1c379e20
DJ
20388set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20389{
dee91e82 20390 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20391 struct objfile *objfile = cu->objfile;
1c379e20 20392
b4ba55a1
JB
20393 /* For Ada types, make sure that the gnat-specific data is always
20394 initialized (if not already set). There are a few types where
20395 we should not be doing so, because the type-specific area is
20396 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20397 where the type-specific area is used to store the floatformat).
20398 But this is not a problem, because the gnat-specific information
20399 is actually not needed for these types. */
20400 if (need_gnat_info (cu)
20401 && TYPE_CODE (type) != TYPE_CODE_FUNC
20402 && TYPE_CODE (type) != TYPE_CODE_FLT
20403 && !HAVE_GNAT_AUX_INFO (type))
20404 INIT_GNAT_SPECIFIC (type);
20405
dee91e82 20406 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20407 {
dee91e82
DE
20408 dwarf2_per_objfile->die_type_hash =
20409 htab_create_alloc_ex (127,
20410 per_cu_offset_and_type_hash,
20411 per_cu_offset_and_type_eq,
20412 NULL,
20413 &objfile->objfile_obstack,
20414 hashtab_obstack_allocate,
20415 dummy_obstack_deallocate);
f792889a 20416 }
1c379e20 20417
dee91e82 20418 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20419 ofs.offset = die->offset;
20420 ofs.type = type;
dee91e82
DE
20421 slot = (struct dwarf2_per_cu_offset_and_type **)
20422 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20423 if (*slot)
20424 complaint (&symfile_complaints,
20425 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20426 die->offset.sect_off);
673bfd45 20427 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20428 **slot = ofs;
f792889a 20429 return type;
1c379e20
DJ
20430}
20431
02142a6c
DE
20432/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20433 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20434
20435static struct type *
b64f50a1 20436get_die_type_at_offset (sect_offset offset,
673bfd45 20437 struct dwarf2_per_cu_data *per_cu)
1c379e20 20438{
dee91e82 20439 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20440
dee91e82 20441 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20442 return NULL;
1c379e20 20443
dee91e82 20444 ofs.per_cu = per_cu;
673bfd45 20445 ofs.offset = offset;
dee91e82 20446 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20447 if (slot)
20448 return slot->type;
20449 else
20450 return NULL;
20451}
20452
02142a6c 20453/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20454 or return NULL if DIE does not have a saved type. */
20455
20456static struct type *
20457get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20458{
20459 return get_die_type_at_offset (die->offset, cu->per_cu);
20460}
20461
10b3939b
DJ
20462/* Add a dependence relationship from CU to REF_PER_CU. */
20463
20464static void
20465dwarf2_add_dependence (struct dwarf2_cu *cu,
20466 struct dwarf2_per_cu_data *ref_per_cu)
20467{
20468 void **slot;
20469
20470 if (cu->dependencies == NULL)
20471 cu->dependencies
20472 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20473 NULL, &cu->comp_unit_obstack,
20474 hashtab_obstack_allocate,
20475 dummy_obstack_deallocate);
20476
20477 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20478 if (*slot == NULL)
20479 *slot = ref_per_cu;
20480}
1c379e20 20481
f504f079
DE
20482/* Subroutine of dwarf2_mark to pass to htab_traverse.
20483 Set the mark field in every compilation unit in the
ae038cb0
DJ
20484 cache that we must keep because we are keeping CU. */
20485
10b3939b
DJ
20486static int
20487dwarf2_mark_helper (void **slot, void *data)
20488{
20489 struct dwarf2_per_cu_data *per_cu;
20490
20491 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20492
20493 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20494 reading of the chain. As such dependencies remain valid it is not much
20495 useful to track and undo them during QUIT cleanups. */
20496 if (per_cu->cu == NULL)
20497 return 1;
20498
10b3939b
DJ
20499 if (per_cu->cu->mark)
20500 return 1;
20501 per_cu->cu->mark = 1;
20502
20503 if (per_cu->cu->dependencies != NULL)
20504 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20505
20506 return 1;
20507}
20508
f504f079
DE
20509/* Set the mark field in CU and in every other compilation unit in the
20510 cache that we must keep because we are keeping CU. */
20511
ae038cb0
DJ
20512static void
20513dwarf2_mark (struct dwarf2_cu *cu)
20514{
20515 if (cu->mark)
20516 return;
20517 cu->mark = 1;
10b3939b
DJ
20518 if (cu->dependencies != NULL)
20519 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20520}
20521
20522static void
20523dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20524{
20525 while (per_cu)
20526 {
20527 per_cu->cu->mark = 0;
20528 per_cu = per_cu->cu->read_in_chain;
20529 }
72bf9492
DJ
20530}
20531
72bf9492
DJ
20532/* Trivial hash function for partial_die_info: the hash value of a DIE
20533 is its offset in .debug_info for this objfile. */
20534
20535static hashval_t
20536partial_die_hash (const void *item)
20537{
20538 const struct partial_die_info *part_die = item;
9a619af0 20539
b64f50a1 20540 return part_die->offset.sect_off;
72bf9492
DJ
20541}
20542
20543/* Trivial comparison function for partial_die_info structures: two DIEs
20544 are equal if they have the same offset. */
20545
20546static int
20547partial_die_eq (const void *item_lhs, const void *item_rhs)
20548{
20549 const struct partial_die_info *part_die_lhs = item_lhs;
20550 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20551
b64f50a1 20552 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20553}
20554
ae038cb0
DJ
20555static struct cmd_list_element *set_dwarf2_cmdlist;
20556static struct cmd_list_element *show_dwarf2_cmdlist;
20557
20558static void
20559set_dwarf2_cmd (char *args, int from_tty)
20560{
20561 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20562}
20563
20564static void
20565show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20566{
ae038cb0
DJ
20567 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20568}
20569
4bf44c1c 20570/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20571
20572static void
c1bd65d0 20573dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20574{
20575 struct dwarf2_per_objfile *data = d;
8b70b953 20576 int ix;
8b70b953 20577
626f2d1c
TT
20578 /* Make sure we don't accidentally use dwarf2_per_objfile while
20579 cleaning up. */
20580 dwarf2_per_objfile = NULL;
20581
59b0c7c1
JB
20582 for (ix = 0; ix < data->n_comp_units; ++ix)
20583 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 20584
59b0c7c1 20585 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 20586 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
20587 data->all_type_units[ix]->per_cu.imported_symtabs);
20588 xfree (data->all_type_units);
95554aad 20589
8b70b953 20590 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20591
20592 if (data->dwo_files)
20593 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20594 if (data->dwp_file)
20595 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20596
20597 if (data->dwz_file && data->dwz_file->dwz_bfd)
20598 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20599}
20600
20601\f
ae2de4f8 20602/* The "save gdb-index" command. */
9291a0cd
TT
20603
20604/* The contents of the hash table we create when building the string
20605 table. */
20606struct strtab_entry
20607{
20608 offset_type offset;
20609 const char *str;
20610};
20611
559a7a62
JK
20612/* Hash function for a strtab_entry.
20613
20614 Function is used only during write_hash_table so no index format backward
20615 compatibility is needed. */
b89be57b 20616
9291a0cd
TT
20617static hashval_t
20618hash_strtab_entry (const void *e)
20619{
20620 const struct strtab_entry *entry = e;
559a7a62 20621 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20622}
20623
20624/* Equality function for a strtab_entry. */
b89be57b 20625
9291a0cd
TT
20626static int
20627eq_strtab_entry (const void *a, const void *b)
20628{
20629 const struct strtab_entry *ea = a;
20630 const struct strtab_entry *eb = b;
20631 return !strcmp (ea->str, eb->str);
20632}
20633
20634/* Create a strtab_entry hash table. */
b89be57b 20635
9291a0cd
TT
20636static htab_t
20637create_strtab (void)
20638{
20639 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20640 xfree, xcalloc, xfree);
20641}
20642
20643/* Add a string to the constant pool. Return the string's offset in
20644 host order. */
b89be57b 20645
9291a0cd
TT
20646static offset_type
20647add_string (htab_t table, struct obstack *cpool, const char *str)
20648{
20649 void **slot;
20650 struct strtab_entry entry;
20651 struct strtab_entry *result;
20652
20653 entry.str = str;
20654 slot = htab_find_slot (table, &entry, INSERT);
20655 if (*slot)
20656 result = *slot;
20657 else
20658 {
20659 result = XNEW (struct strtab_entry);
20660 result->offset = obstack_object_size (cpool);
20661 result->str = str;
20662 obstack_grow_str0 (cpool, str);
20663 *slot = result;
20664 }
20665 return result->offset;
20666}
20667
20668/* An entry in the symbol table. */
20669struct symtab_index_entry
20670{
20671 /* The name of the symbol. */
20672 const char *name;
20673 /* The offset of the name in the constant pool. */
20674 offset_type index_offset;
20675 /* A sorted vector of the indices of all the CUs that hold an object
20676 of this name. */
20677 VEC (offset_type) *cu_indices;
20678};
20679
20680/* The symbol table. This is a power-of-2-sized hash table. */
20681struct mapped_symtab
20682{
20683 offset_type n_elements;
20684 offset_type size;
20685 struct symtab_index_entry **data;
20686};
20687
20688/* Hash function for a symtab_index_entry. */
b89be57b 20689
9291a0cd
TT
20690static hashval_t
20691hash_symtab_entry (const void *e)
20692{
20693 const struct symtab_index_entry *entry = e;
20694 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20695 sizeof (offset_type) * VEC_length (offset_type,
20696 entry->cu_indices),
20697 0);
20698}
20699
20700/* Equality function for a symtab_index_entry. */
b89be57b 20701
9291a0cd
TT
20702static int
20703eq_symtab_entry (const void *a, const void *b)
20704{
20705 const struct symtab_index_entry *ea = a;
20706 const struct symtab_index_entry *eb = b;
20707 int len = VEC_length (offset_type, ea->cu_indices);
20708 if (len != VEC_length (offset_type, eb->cu_indices))
20709 return 0;
20710 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20711 VEC_address (offset_type, eb->cu_indices),
20712 sizeof (offset_type) * len);
20713}
20714
20715/* Destroy a symtab_index_entry. */
b89be57b 20716
9291a0cd
TT
20717static void
20718delete_symtab_entry (void *p)
20719{
20720 struct symtab_index_entry *entry = p;
20721 VEC_free (offset_type, entry->cu_indices);
20722 xfree (entry);
20723}
20724
20725/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20726
9291a0cd 20727static htab_t
3876f04e 20728create_symbol_hash_table (void)
9291a0cd
TT
20729{
20730 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20731 delete_symtab_entry, xcalloc, xfree);
20732}
20733
20734/* Create a new mapped symtab object. */
b89be57b 20735
9291a0cd
TT
20736static struct mapped_symtab *
20737create_mapped_symtab (void)
20738{
20739 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20740 symtab->n_elements = 0;
20741 symtab->size = 1024;
20742 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20743 return symtab;
20744}
20745
20746/* Destroy a mapped_symtab. */
b89be57b 20747
9291a0cd
TT
20748static void
20749cleanup_mapped_symtab (void *p)
20750{
20751 struct mapped_symtab *symtab = p;
20752 /* The contents of the array are freed when the other hash table is
20753 destroyed. */
20754 xfree (symtab->data);
20755 xfree (symtab);
20756}
20757
20758/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20759 the slot.
20760
20761 Function is used only during write_hash_table so no index format backward
20762 compatibility is needed. */
b89be57b 20763
9291a0cd
TT
20764static struct symtab_index_entry **
20765find_slot (struct mapped_symtab *symtab, const char *name)
20766{
559a7a62 20767 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20768
20769 index = hash & (symtab->size - 1);
20770 step = ((hash * 17) & (symtab->size - 1)) | 1;
20771
20772 for (;;)
20773 {
20774 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20775 return &symtab->data[index];
20776 index = (index + step) & (symtab->size - 1);
20777 }
20778}
20779
20780/* Expand SYMTAB's hash table. */
b89be57b 20781
9291a0cd
TT
20782static void
20783hash_expand (struct mapped_symtab *symtab)
20784{
20785 offset_type old_size = symtab->size;
20786 offset_type i;
20787 struct symtab_index_entry **old_entries = symtab->data;
20788
20789 symtab->size *= 2;
20790 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20791
20792 for (i = 0; i < old_size; ++i)
20793 {
20794 if (old_entries[i])
20795 {
20796 struct symtab_index_entry **slot = find_slot (symtab,
20797 old_entries[i]->name);
20798 *slot = old_entries[i];
20799 }
20800 }
20801
20802 xfree (old_entries);
20803}
20804
156942c7
DE
20805/* Add an entry to SYMTAB. NAME is the name of the symbol.
20806 CU_INDEX is the index of the CU in which the symbol appears.
20807 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20808
9291a0cd
TT
20809static void
20810add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20811 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20812 offset_type cu_index)
20813{
20814 struct symtab_index_entry **slot;
156942c7 20815 offset_type cu_index_and_attrs;
9291a0cd
TT
20816
20817 ++symtab->n_elements;
20818 if (4 * symtab->n_elements / 3 >= symtab->size)
20819 hash_expand (symtab);
20820
20821 slot = find_slot (symtab, name);
20822 if (!*slot)
20823 {
20824 *slot = XNEW (struct symtab_index_entry);
20825 (*slot)->name = name;
156942c7 20826 /* index_offset is set later. */
9291a0cd
TT
20827 (*slot)->cu_indices = NULL;
20828 }
156942c7
DE
20829
20830 cu_index_and_attrs = 0;
20831 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20832 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20833 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20834
20835 /* We don't want to record an index value twice as we want to avoid the
20836 duplication.
20837 We process all global symbols and then all static symbols
20838 (which would allow us to avoid the duplication by only having to check
20839 the last entry pushed), but a symbol could have multiple kinds in one CU.
20840 To keep things simple we don't worry about the duplication here and
20841 sort and uniqufy the list after we've processed all symbols. */
20842 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20843}
20844
20845/* qsort helper routine for uniquify_cu_indices. */
20846
20847static int
20848offset_type_compare (const void *ap, const void *bp)
20849{
20850 offset_type a = *(offset_type *) ap;
20851 offset_type b = *(offset_type *) bp;
20852
20853 return (a > b) - (b > a);
20854}
20855
20856/* Sort and remove duplicates of all symbols' cu_indices lists. */
20857
20858static void
20859uniquify_cu_indices (struct mapped_symtab *symtab)
20860{
20861 int i;
20862
20863 for (i = 0; i < symtab->size; ++i)
20864 {
20865 struct symtab_index_entry *entry = symtab->data[i];
20866
20867 if (entry
20868 && entry->cu_indices != NULL)
20869 {
20870 unsigned int next_to_insert, next_to_check;
20871 offset_type last_value;
20872
20873 qsort (VEC_address (offset_type, entry->cu_indices),
20874 VEC_length (offset_type, entry->cu_indices),
20875 sizeof (offset_type), offset_type_compare);
20876
20877 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20878 next_to_insert = 1;
20879 for (next_to_check = 1;
20880 next_to_check < VEC_length (offset_type, entry->cu_indices);
20881 ++next_to_check)
20882 {
20883 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20884 != last_value)
20885 {
20886 last_value = VEC_index (offset_type, entry->cu_indices,
20887 next_to_check);
20888 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20889 last_value);
20890 ++next_to_insert;
20891 }
20892 }
20893 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20894 }
20895 }
9291a0cd
TT
20896}
20897
20898/* Add a vector of indices to the constant pool. */
b89be57b 20899
9291a0cd 20900static offset_type
3876f04e 20901add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20902 struct symtab_index_entry *entry)
20903{
20904 void **slot;
20905
3876f04e 20906 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20907 if (!*slot)
20908 {
20909 offset_type len = VEC_length (offset_type, entry->cu_indices);
20910 offset_type val = MAYBE_SWAP (len);
20911 offset_type iter;
20912 int i;
20913
20914 *slot = entry;
20915 entry->index_offset = obstack_object_size (cpool);
20916
20917 obstack_grow (cpool, &val, sizeof (val));
20918 for (i = 0;
20919 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20920 ++i)
20921 {
20922 val = MAYBE_SWAP (iter);
20923 obstack_grow (cpool, &val, sizeof (val));
20924 }
20925 }
20926 else
20927 {
20928 struct symtab_index_entry *old_entry = *slot;
20929 entry->index_offset = old_entry->index_offset;
20930 entry = old_entry;
20931 }
20932 return entry->index_offset;
20933}
20934
20935/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20936 constant pool entries going into the obstack CPOOL. */
b89be57b 20937
9291a0cd
TT
20938static void
20939write_hash_table (struct mapped_symtab *symtab,
20940 struct obstack *output, struct obstack *cpool)
20941{
20942 offset_type i;
3876f04e 20943 htab_t symbol_hash_table;
9291a0cd
TT
20944 htab_t str_table;
20945
3876f04e 20946 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20947 str_table = create_strtab ();
3876f04e 20948
9291a0cd
TT
20949 /* We add all the index vectors to the constant pool first, to
20950 ensure alignment is ok. */
20951 for (i = 0; i < symtab->size; ++i)
20952 {
20953 if (symtab->data[i])
3876f04e 20954 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
20955 }
20956
20957 /* Now write out the hash table. */
20958 for (i = 0; i < symtab->size; ++i)
20959 {
20960 offset_type str_off, vec_off;
20961
20962 if (symtab->data[i])
20963 {
20964 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20965 vec_off = symtab->data[i]->index_offset;
20966 }
20967 else
20968 {
20969 /* While 0 is a valid constant pool index, it is not valid
20970 to have 0 for both offsets. */
20971 str_off = 0;
20972 vec_off = 0;
20973 }
20974
20975 str_off = MAYBE_SWAP (str_off);
20976 vec_off = MAYBE_SWAP (vec_off);
20977
20978 obstack_grow (output, &str_off, sizeof (str_off));
20979 obstack_grow (output, &vec_off, sizeof (vec_off));
20980 }
20981
20982 htab_delete (str_table);
3876f04e 20983 htab_delete (symbol_hash_table);
9291a0cd
TT
20984}
20985
0a5429f6
DE
20986/* Struct to map psymtab to CU index in the index file. */
20987struct psymtab_cu_index_map
20988{
20989 struct partial_symtab *psymtab;
20990 unsigned int cu_index;
20991};
20992
20993static hashval_t
20994hash_psymtab_cu_index (const void *item)
20995{
20996 const struct psymtab_cu_index_map *map = item;
20997
20998 return htab_hash_pointer (map->psymtab);
20999}
21000
21001static int
21002eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21003{
21004 const struct psymtab_cu_index_map *lhs = item_lhs;
21005 const struct psymtab_cu_index_map *rhs = item_rhs;
21006
21007 return lhs->psymtab == rhs->psymtab;
21008}
21009
21010/* Helper struct for building the address table. */
21011struct addrmap_index_data
21012{
21013 struct objfile *objfile;
21014 struct obstack *addr_obstack;
21015 htab_t cu_index_htab;
21016
21017 /* Non-zero if the previous_* fields are valid.
21018 We can't write an entry until we see the next entry (since it is only then
21019 that we know the end of the entry). */
21020 int previous_valid;
21021 /* Index of the CU in the table of all CUs in the index file. */
21022 unsigned int previous_cu_index;
0963b4bd 21023 /* Start address of the CU. */
0a5429f6
DE
21024 CORE_ADDR previous_cu_start;
21025};
21026
21027/* Write an address entry to OBSTACK. */
b89be57b 21028
9291a0cd 21029static void
0a5429f6
DE
21030add_address_entry (struct objfile *objfile, struct obstack *obstack,
21031 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21032{
0a5429f6 21033 offset_type cu_index_to_write;
948f8e3d 21034 gdb_byte addr[8];
9291a0cd
TT
21035 CORE_ADDR baseaddr;
21036
21037 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21038
0a5429f6
DE
21039 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21040 obstack_grow (obstack, addr, 8);
21041 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21042 obstack_grow (obstack, addr, 8);
21043 cu_index_to_write = MAYBE_SWAP (cu_index);
21044 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21045}
21046
21047/* Worker function for traversing an addrmap to build the address table. */
21048
21049static int
21050add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21051{
21052 struct addrmap_index_data *data = datap;
21053 struct partial_symtab *pst = obj;
0a5429f6
DE
21054
21055 if (data->previous_valid)
21056 add_address_entry (data->objfile, data->addr_obstack,
21057 data->previous_cu_start, start_addr,
21058 data->previous_cu_index);
21059
21060 data->previous_cu_start = start_addr;
21061 if (pst != NULL)
21062 {
21063 struct psymtab_cu_index_map find_map, *map;
21064 find_map.psymtab = pst;
21065 map = htab_find (data->cu_index_htab, &find_map);
21066 gdb_assert (map != NULL);
21067 data->previous_cu_index = map->cu_index;
21068 data->previous_valid = 1;
21069 }
21070 else
21071 data->previous_valid = 0;
21072
21073 return 0;
21074}
21075
21076/* Write OBJFILE's address map to OBSTACK.
21077 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21078 in the index file. */
21079
21080static void
21081write_address_map (struct objfile *objfile, struct obstack *obstack,
21082 htab_t cu_index_htab)
21083{
21084 struct addrmap_index_data addrmap_index_data;
21085
21086 /* When writing the address table, we have to cope with the fact that
21087 the addrmap iterator only provides the start of a region; we have to
21088 wait until the next invocation to get the start of the next region. */
21089
21090 addrmap_index_data.objfile = objfile;
21091 addrmap_index_data.addr_obstack = obstack;
21092 addrmap_index_data.cu_index_htab = cu_index_htab;
21093 addrmap_index_data.previous_valid = 0;
21094
21095 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21096 &addrmap_index_data);
21097
21098 /* It's highly unlikely the last entry (end address = 0xff...ff)
21099 is valid, but we should still handle it.
21100 The end address is recorded as the start of the next region, but that
21101 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21102 anyway. */
21103 if (addrmap_index_data.previous_valid)
21104 add_address_entry (objfile, obstack,
21105 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21106 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21107}
21108
156942c7
DE
21109/* Return the symbol kind of PSYM. */
21110
21111static gdb_index_symbol_kind
21112symbol_kind (struct partial_symbol *psym)
21113{
21114 domain_enum domain = PSYMBOL_DOMAIN (psym);
21115 enum address_class aclass = PSYMBOL_CLASS (psym);
21116
21117 switch (domain)
21118 {
21119 case VAR_DOMAIN:
21120 switch (aclass)
21121 {
21122 case LOC_BLOCK:
21123 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21124 case LOC_TYPEDEF:
21125 return GDB_INDEX_SYMBOL_KIND_TYPE;
21126 case LOC_COMPUTED:
21127 case LOC_CONST_BYTES:
21128 case LOC_OPTIMIZED_OUT:
21129 case LOC_STATIC:
21130 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21131 case LOC_CONST:
21132 /* Note: It's currently impossible to recognize psyms as enum values
21133 short of reading the type info. For now punt. */
21134 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21135 default:
21136 /* There are other LOC_FOO values that one might want to classify
21137 as variables, but dwarf2read.c doesn't currently use them. */
21138 return GDB_INDEX_SYMBOL_KIND_OTHER;
21139 }
21140 case STRUCT_DOMAIN:
21141 return GDB_INDEX_SYMBOL_KIND_TYPE;
21142 default:
21143 return GDB_INDEX_SYMBOL_KIND_OTHER;
21144 }
21145}
21146
9291a0cd 21147/* Add a list of partial symbols to SYMTAB. */
b89be57b 21148
9291a0cd
TT
21149static void
21150write_psymbols (struct mapped_symtab *symtab,
987d643c 21151 htab_t psyms_seen,
9291a0cd
TT
21152 struct partial_symbol **psymp,
21153 int count,
987d643c
TT
21154 offset_type cu_index,
21155 int is_static)
9291a0cd
TT
21156{
21157 for (; count-- > 0; ++psymp)
21158 {
156942c7
DE
21159 struct partial_symbol *psym = *psymp;
21160 void **slot;
987d643c 21161
156942c7 21162 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21163 error (_("Ada is not currently supported by the index"));
987d643c 21164
987d643c 21165 /* Only add a given psymbol once. */
156942c7 21166 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21167 if (!*slot)
21168 {
156942c7
DE
21169 gdb_index_symbol_kind kind = symbol_kind (psym);
21170
21171 *slot = psym;
21172 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21173 is_static, kind, cu_index);
987d643c 21174 }
9291a0cd
TT
21175 }
21176}
21177
21178/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21179 exception if there is an error. */
b89be57b 21180
9291a0cd
TT
21181static void
21182write_obstack (FILE *file, struct obstack *obstack)
21183{
21184 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21185 file)
21186 != obstack_object_size (obstack))
21187 error (_("couldn't data write to file"));
21188}
21189
21190/* Unlink a file if the argument is not NULL. */
b89be57b 21191
9291a0cd
TT
21192static void
21193unlink_if_set (void *p)
21194{
21195 char **filename = p;
21196 if (*filename)
21197 unlink (*filename);
21198}
21199
1fd400ff
TT
21200/* A helper struct used when iterating over debug_types. */
21201struct signatured_type_index_data
21202{
21203 struct objfile *objfile;
21204 struct mapped_symtab *symtab;
21205 struct obstack *types_list;
987d643c 21206 htab_t psyms_seen;
1fd400ff
TT
21207 int cu_index;
21208};
21209
21210/* A helper function that writes a single signatured_type to an
21211 obstack. */
b89be57b 21212
1fd400ff
TT
21213static int
21214write_one_signatured_type (void **slot, void *d)
21215{
21216 struct signatured_type_index_data *info = d;
21217 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21218 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21219 gdb_byte val[8];
21220
21221 write_psymbols (info->symtab,
987d643c 21222 info->psyms_seen,
3e43a32a
MS
21223 info->objfile->global_psymbols.list
21224 + psymtab->globals_offset,
987d643c
TT
21225 psymtab->n_global_syms, info->cu_index,
21226 0);
1fd400ff 21227 write_psymbols (info->symtab,
987d643c 21228 info->psyms_seen,
3e43a32a
MS
21229 info->objfile->static_psymbols.list
21230 + psymtab->statics_offset,
987d643c
TT
21231 psymtab->n_static_syms, info->cu_index,
21232 1);
1fd400ff 21233
b64f50a1
JK
21234 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21235 entry->per_cu.offset.sect_off);
1fd400ff 21236 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21237 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21238 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21239 obstack_grow (info->types_list, val, 8);
21240 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21241 obstack_grow (info->types_list, val, 8);
21242
21243 ++info->cu_index;
21244
21245 return 1;
21246}
21247
95554aad
TT
21248/* Recurse into all "included" dependencies and write their symbols as
21249 if they appeared in this psymtab. */
21250
21251static void
21252recursively_write_psymbols (struct objfile *objfile,
21253 struct partial_symtab *psymtab,
21254 struct mapped_symtab *symtab,
21255 htab_t psyms_seen,
21256 offset_type cu_index)
21257{
21258 int i;
21259
21260 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21261 if (psymtab->dependencies[i]->user != NULL)
21262 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21263 symtab, psyms_seen, cu_index);
21264
21265 write_psymbols (symtab,
21266 psyms_seen,
21267 objfile->global_psymbols.list + psymtab->globals_offset,
21268 psymtab->n_global_syms, cu_index,
21269 0);
21270 write_psymbols (symtab,
21271 psyms_seen,
21272 objfile->static_psymbols.list + psymtab->statics_offset,
21273 psymtab->n_static_syms, cu_index,
21274 1);
21275}
21276
9291a0cd 21277/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21278
9291a0cd
TT
21279static void
21280write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21281{
21282 struct cleanup *cleanup;
21283 char *filename, *cleanup_filename;
1fd400ff
TT
21284 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21285 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21286 int i;
21287 FILE *out_file;
21288 struct mapped_symtab *symtab;
21289 offset_type val, size_of_contents, total_len;
21290 struct stat st;
987d643c 21291 htab_t psyms_seen;
0a5429f6
DE
21292 htab_t cu_index_htab;
21293 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21294
b4f2f049 21295 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 21296 return;
b4f2f049 21297
9291a0cd
TT
21298 if (dwarf2_per_objfile->using_index)
21299 error (_("Cannot use an index to create the index"));
21300
8b70b953
TT
21301 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21302 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21303
9291a0cd 21304 if (stat (objfile->name, &st) < 0)
7e17e088 21305 perror_with_name (objfile->name);
9291a0cd
TT
21306
21307 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21308 INDEX_SUFFIX, (char *) NULL);
21309 cleanup = make_cleanup (xfree, filename);
21310
614c279d 21311 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21312 if (!out_file)
21313 error (_("Can't open `%s' for writing"), filename);
21314
21315 cleanup_filename = filename;
21316 make_cleanup (unlink_if_set, &cleanup_filename);
21317
21318 symtab = create_mapped_symtab ();
21319 make_cleanup (cleanup_mapped_symtab, symtab);
21320
21321 obstack_init (&addr_obstack);
21322 make_cleanup_obstack_free (&addr_obstack);
21323
21324 obstack_init (&cu_list);
21325 make_cleanup_obstack_free (&cu_list);
21326
1fd400ff
TT
21327 obstack_init (&types_cu_list);
21328 make_cleanup_obstack_free (&types_cu_list);
21329
987d643c
TT
21330 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21331 NULL, xcalloc, xfree);
96408a79 21332 make_cleanup_htab_delete (psyms_seen);
987d643c 21333
0a5429f6
DE
21334 /* While we're scanning CU's create a table that maps a psymtab pointer
21335 (which is what addrmap records) to its index (which is what is recorded
21336 in the index file). This will later be needed to write the address
21337 table. */
21338 cu_index_htab = htab_create_alloc (100,
21339 hash_psymtab_cu_index,
21340 eq_psymtab_cu_index,
21341 NULL, xcalloc, xfree);
96408a79 21342 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21343 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21344 xmalloc (sizeof (struct psymtab_cu_index_map)
21345 * dwarf2_per_objfile->n_comp_units);
21346 make_cleanup (xfree, psymtab_cu_index_map);
21347
21348 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21349 work here. Also, the debug_types entries do not appear in
21350 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21351 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21352 {
3e43a32a
MS
21353 struct dwarf2_per_cu_data *per_cu
21354 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21355 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21356 gdb_byte val[8];
0a5429f6
DE
21357 struct psymtab_cu_index_map *map;
21358 void **slot;
9291a0cd 21359
92fac807
JK
21360 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21361 It may be referenced from a local scope but in such case it does not
21362 need to be present in .gdb_index. */
21363 if (psymtab == NULL)
21364 continue;
21365
95554aad
TT
21366 if (psymtab->user == NULL)
21367 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21368
0a5429f6
DE
21369 map = &psymtab_cu_index_map[i];
21370 map->psymtab = psymtab;
21371 map->cu_index = i;
21372 slot = htab_find_slot (cu_index_htab, map, INSERT);
21373 gdb_assert (slot != NULL);
21374 gdb_assert (*slot == NULL);
21375 *slot = map;
9291a0cd 21376
b64f50a1
JK
21377 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21378 per_cu->offset.sect_off);
9291a0cd 21379 obstack_grow (&cu_list, val, 8);
e254ef6a 21380 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21381 obstack_grow (&cu_list, val, 8);
21382 }
21383
0a5429f6
DE
21384 /* Dump the address map. */
21385 write_address_map (objfile, &addr_obstack, cu_index_htab);
21386
1fd400ff
TT
21387 /* Write out the .debug_type entries, if any. */
21388 if (dwarf2_per_objfile->signatured_types)
21389 {
21390 struct signatured_type_index_data sig_data;
21391
21392 sig_data.objfile = objfile;
21393 sig_data.symtab = symtab;
21394 sig_data.types_list = &types_cu_list;
987d643c 21395 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21396 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21397 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21398 write_one_signatured_type, &sig_data);
21399 }
21400
156942c7
DE
21401 /* Now that we've processed all symbols we can shrink their cu_indices
21402 lists. */
21403 uniquify_cu_indices (symtab);
21404
9291a0cd
TT
21405 obstack_init (&constant_pool);
21406 make_cleanup_obstack_free (&constant_pool);
21407 obstack_init (&symtab_obstack);
21408 make_cleanup_obstack_free (&symtab_obstack);
21409 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21410
21411 obstack_init (&contents);
21412 make_cleanup_obstack_free (&contents);
1fd400ff 21413 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21414 total_len = size_of_contents;
21415
21416 /* The version number. */
796a7ff8 21417 val = MAYBE_SWAP (8);
9291a0cd
TT
21418 obstack_grow (&contents, &val, sizeof (val));
21419
21420 /* The offset of the CU list 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 (&cu_list);
21424
1fd400ff
TT
21425 /* The offset of the types CU list 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 (&types_cu_list);
21429
9291a0cd
TT
21430 /* The offset of the address table from the start of the file. */
21431 val = MAYBE_SWAP (total_len);
21432 obstack_grow (&contents, &val, sizeof (val));
21433 total_len += obstack_object_size (&addr_obstack);
21434
21435 /* The offset of the symbol table from the start of the file. */
21436 val = MAYBE_SWAP (total_len);
21437 obstack_grow (&contents, &val, sizeof (val));
21438 total_len += obstack_object_size (&symtab_obstack);
21439
21440 /* The offset of the constant pool from the start of the file. */
21441 val = MAYBE_SWAP (total_len);
21442 obstack_grow (&contents, &val, sizeof (val));
21443 total_len += obstack_object_size (&constant_pool);
21444
21445 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21446
21447 write_obstack (out_file, &contents);
21448 write_obstack (out_file, &cu_list);
1fd400ff 21449 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21450 write_obstack (out_file, &addr_obstack);
21451 write_obstack (out_file, &symtab_obstack);
21452 write_obstack (out_file, &constant_pool);
21453
21454 fclose (out_file);
21455
21456 /* We want to keep the file, so we set cleanup_filename to NULL
21457 here. See unlink_if_set. */
21458 cleanup_filename = NULL;
21459
21460 do_cleanups (cleanup);
21461}
21462
90476074
TT
21463/* Implementation of the `save gdb-index' command.
21464
21465 Note that the file format used by this command is documented in the
21466 GDB manual. Any changes here must be documented there. */
11570e71 21467
9291a0cd
TT
21468static void
21469save_gdb_index_command (char *arg, int from_tty)
21470{
21471 struct objfile *objfile;
21472
21473 if (!arg || !*arg)
96d19272 21474 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21475
21476 ALL_OBJFILES (objfile)
21477 {
21478 struct stat st;
21479
21480 /* If the objfile does not correspond to an actual file, skip it. */
21481 if (stat (objfile->name, &st) < 0)
21482 continue;
21483
21484 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21485 if (dwarf2_per_objfile)
21486 {
21487 volatile struct gdb_exception except;
21488
21489 TRY_CATCH (except, RETURN_MASK_ERROR)
21490 {
21491 write_psymtabs_to_index (objfile, arg);
21492 }
21493 if (except.reason < 0)
21494 exception_fprintf (gdb_stderr, except,
21495 _("Error while writing index for `%s': "),
21496 objfile->name);
21497 }
21498 }
dce234bc
PP
21499}
21500
9291a0cd
TT
21501\f
21502
9eae7c52
TT
21503int dwarf2_always_disassemble;
21504
21505static void
21506show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21507 struct cmd_list_element *c, const char *value)
21508{
3e43a32a
MS
21509 fprintf_filtered (file,
21510 _("Whether to always disassemble "
21511 "DWARF expressions is %s.\n"),
9eae7c52
TT
21512 value);
21513}
21514
900e11f9
JK
21515static void
21516show_check_physname (struct ui_file *file, int from_tty,
21517 struct cmd_list_element *c, const char *value)
21518{
21519 fprintf_filtered (file,
21520 _("Whether to check \"physname\" is %s.\n"),
21521 value);
21522}
21523
6502dd73
DJ
21524void _initialize_dwarf2_read (void);
21525
21526void
21527_initialize_dwarf2_read (void)
21528{
96d19272
JK
21529 struct cmd_list_element *c;
21530
dce234bc 21531 dwarf2_objfile_data_key
c1bd65d0 21532 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21533
1bedd215
AC
21534 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21535Set DWARF 2 specific variables.\n\
21536Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21537 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21538 0/*allow-unknown*/, &maintenance_set_cmdlist);
21539
1bedd215
AC
21540 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21541Show DWARF 2 specific variables\n\
21542Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21543 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21544 0/*allow-unknown*/, &maintenance_show_cmdlist);
21545
21546 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21547 &dwarf2_max_cache_age, _("\
21548Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21549Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21550A higher limit means that cached compilation units will be stored\n\
21551in memory longer, and more total memory will be used. Zero disables\n\
21552caching, which can slow down startup."),
2c5b56ce 21553 NULL,
920d2a44 21554 show_dwarf2_max_cache_age,
2c5b56ce 21555 &set_dwarf2_cmdlist,
ae038cb0 21556 &show_dwarf2_cmdlist);
d97bc12b 21557
9eae7c52
TT
21558 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21559 &dwarf2_always_disassemble, _("\
21560Set whether `info address' always disassembles DWARF expressions."), _("\
21561Show whether `info address' always disassembles DWARF expressions."), _("\
21562When enabled, DWARF expressions are always printed in an assembly-like\n\
21563syntax. When disabled, expressions will be printed in a more\n\
21564conversational style, when possible."),
21565 NULL,
21566 show_dwarf2_always_disassemble,
21567 &set_dwarf2_cmdlist,
21568 &show_dwarf2_cmdlist);
21569
45cfd468
DE
21570 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21571Set debugging of the dwarf2 reader."), _("\
21572Show debugging of the dwarf2 reader."), _("\
21573When enabled, debugging messages are printed during dwarf2 reading\n\
21574and symtab expansion."),
21575 NULL,
21576 NULL,
21577 &setdebuglist, &showdebuglist);
21578
ccce17b0 21579 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21580Set debugging of the dwarf2 DIE reader."), _("\
21581Show debugging of the dwarf2 DIE reader."), _("\
21582When enabled (non-zero), DIEs are dumped after they are read in.\n\
21583The value is the maximum depth to print."),
ccce17b0
YQ
21584 NULL,
21585 NULL,
21586 &setdebuglist, &showdebuglist);
9291a0cd 21587
900e11f9
JK
21588 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21589Set cross-checking of \"physname\" code against demangler."), _("\
21590Show cross-checking of \"physname\" code against demangler."), _("\
21591When enabled, GDB's internal \"physname\" code is checked against\n\
21592the demangler."),
21593 NULL, show_check_physname,
21594 &setdebuglist, &showdebuglist);
21595
e615022a
DE
21596 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21597 no_class, &use_deprecated_index_sections, _("\
21598Set whether to use deprecated gdb_index sections."), _("\
21599Show whether to use deprecated gdb_index sections."), _("\
21600When enabled, deprecated .gdb_index sections are used anyway.\n\
21601Normally they are ignored either because of a missing feature or\n\
21602performance issue.\n\
21603Warning: This option must be enabled before gdb reads the file."),
21604 NULL,
21605 NULL,
21606 &setlist, &showlist);
21607
96d19272 21608 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21609 _("\
fc1a9d6e 21610Save a gdb-index file.\n\
11570e71 21611Usage: save gdb-index DIRECTORY"),
96d19272
JK
21612 &save_cmdlist);
21613 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21614
21615 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21616 &dwarf2_locexpr_funcs);
21617 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21618 &dwarf2_loclist_funcs);
21619
21620 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21621 &dwarf2_block_frame_base_locexpr_funcs);
21622 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21623 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21624}
This page took 4.227771 seconds and 4 git commands to generate.