* dwarf2read.c: Move definitions of complaint functions to after
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
28e7fd62 3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
80626a55 33#include "elf-bfd.h"
c906108c
SS
34#include "symtab.h"
35#include "gdbtypes.h"
c906108c 36#include "objfiles.h"
fa8f86ff 37#include "dwarf2.h"
c906108c
SS
38#include "buildsym.h"
39#include "demangle.h"
50f182aa 40#include "gdb-demangle.h"
c906108c 41#include "expression.h"
d5166ae1 42#include "filenames.h" /* for DOSish file names */
2e276125 43#include "macrotab.h"
c906108c
SS
44#include "language.h"
45#include "complaints.h"
357e46e7 46#include "bcache.h"
4c2df51b
DJ
47#include "dwarf2expr.h"
48#include "dwarf2loc.h"
9219021c 49#include "cp-support.h"
72bf9492 50#include "hashtab.h"
ae038cb0
DJ
51#include "command.h"
52#include "gdbcmd.h"
edb3359d 53#include "block.h"
ff013f42 54#include "addrmap.h"
94af9270
KS
55#include "typeprint.h"
56#include "jv-lang.h"
ccefe4c4 57#include "psympriv.h"
9291a0cd
TT
58#include "exceptions.h"
59#include "gdb_stat.h"
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
4c2df51b 72
c906108c
SS
73#include <fcntl.h>
74#include "gdb_string.h"
4bdf3d34 75#include "gdb_assert.h"
c906108c 76#include <sys/types.h>
d8151005 77
34eaf542
TT
78typedef struct symbol *symbolp;
79DEF_VEC_P (symbolp);
80
45cfd468
DE
81/* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83static int dwarf2_read_debug = 0;
84
d97bc12b 85/* When non-zero, dump DIEs after they are read in. */
ccce17b0 86static unsigned int dwarf2_die_debug = 0;
d97bc12b 87
900e11f9
JK
88/* When non-zero, cross-check physname against demangler. */
89static int check_physname = 0;
90
481860b3 91/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 92static int use_deprecated_index_sections = 0;
481860b3 93
6502dd73
DJ
94static const struct objfile_data *dwarf2_objfile_data_key;
95
f1e6e072
TT
96/* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98static int dwarf2_locexpr_index;
99static int dwarf2_loclist_index;
100static int dwarf2_locexpr_block_index;
101static int dwarf2_loclist_block_index;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
d521ce57 106 const gdb_byte *buffer;
dce234bc 107 bfd_size_type size;
be391dca
TT
108 /* True if we have tried to read this section. */
109 int readin;
dce234bc
PP
110};
111
8b70b953
TT
112typedef struct dwarf2_section_info dwarf2_section_info_def;
113DEF_VEC_O (dwarf2_section_info_def);
114
9291a0cd
TT
115/* All offsets in the index are of this type. It must be
116 architecture-independent. */
117typedef uint32_t offset_type;
118
119DEF_VEC_I (offset_type);
120
156942c7
DE
121/* Ensure only legit values are used. */
122#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128/* Ensure only legit values are used. */
129#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136/* Ensure we don't use more than the alloted nuber of bits for the CU. */
137#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
9291a0cd
TT
143/* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145struct mapped_index
146{
559a7a62
JK
147 /* Index data format version. */
148 int version;
149
9291a0cd
TT
150 /* The total length of the buffer. */
151 off_t total_size;
b11b1f88 152
9291a0cd
TT
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
b11b1f88 155
9291a0cd
TT
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
b11b1f88 158
3876f04e
DE
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
b11b1f88 161
9291a0cd 162 /* Size in slots, each slot is 2 offset_types. */
3876f04e 163 offset_type symbol_table_slots;
b11b1f88 164
9291a0cd
TT
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167};
168
95554aad
TT
169typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170DEF_VEC_P (dwarf2_per_cu_ptr);
171
9cdd5dbd
DE
172/* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
6502dd73
DJ
175struct dwarf2_per_objfile
176{
dce234bc
PP
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
dce234bc
PP
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
cf2c3c16 182 struct dwarf2_section_info macro;
dce234bc
PP
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
3019eac3 185 struct dwarf2_section_info addr;
dce234bc
PP
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
9291a0cd 188 struct dwarf2_section_info gdb_index;
ae038cb0 189
8b70b953
TT
190 VEC (dwarf2_section_info_def) *types;
191
be391dca
TT
192 /* Back link. */
193 struct objfile *objfile;
194
d467dd73 195 /* Table of all the compilation units. This is used to locate
10b3939b 196 the target compilation unit of a particular reference. */
ae038cb0
DJ
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
1fd400ff 202 /* The number of .debug_types-related CUs. */
d467dd73 203 int n_type_units;
1fd400ff 204
a2ce51a0
DE
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
b4dd5633 207 struct signatured_type **all_type_units;
1fd400ff 208
f4dc4d17
DE
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
f4dc4d17
DE
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
3019eac3
DE
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
80626a55
DE
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
36586728
TT
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
72dca2f5
FR
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
9291a0cd 255
ae2de4f8
DE
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
258 unsigned char using_index;
259
ae2de4f8 260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 261 struct mapped_index *index_table;
98bfdba5 262
7b9f3c50 263 /* When using index_table, this keeps track of all quick_file_names entries.
56e64610
DE
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
7b9f3c50
DE
270 htab_t quick_file_names_table;
271
98bfdba5
PA
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
673bfd45 275
dee91e82 276 /* Table mapping type DIEs to their struct type *.
673bfd45 277 This is NULL if not allocated yet.
02142a6c 278 The mapping is done via (CU/TU + DIE offset) -> type. */
dee91e82 279 htab_t die_type_hash;
95554aad
TT
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
283};
284
285static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 299 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
3019eac3 303 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
24d3216f
TT
306 { ".gdb_index", ".zgdb_index" },
307 23
251d32d9 308};
c906108c 309
80626a55 310/* List of DWO/DWP sections. */
3019eac3 311
80626a55 312static const struct dwop_section_names
3019eac3
DE
313{
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
09262596
DE
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
3019eac3
DE
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
80626a55
DE
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
3019eac3 325}
80626a55 326dwop_section_names =
3019eac3
DE
327{
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
339};
340
c906108c
SS
341/* local data types */
342
107d2387
AC
343/* The data in a compilation unit header, after target2host
344 translation, looks like this. */
c906108c 345struct comp_unit_head
a738430d 346{
c764a876 347 unsigned int length;
a738430d 348 short version;
a738430d
MK
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
b64f50a1 351 sect_offset abbrev_offset;
57349743 352
a738430d
MK
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
57349743 355
a738430d
MK
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
57349743 358
a738430d
MK
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
b64f50a1 361 sect_offset offset;
57349743 362
d00adf39
DE
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
b64f50a1 365 cu_offset first_die_offset;
a738430d 366};
c906108c 367
3da10d80
KS
368/* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370struct delayed_method_info
371{
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386};
387
388typedef struct delayed_method_info delayed_method_info;
389DEF_VEC_O (delayed_method_info);
390
e7c27a73
DJ
391/* Internal state when decoding a particular compilation unit. */
392struct dwarf2_cu
393{
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
d00adf39 397 /* The header of the compilation unit. */
e7c27a73 398 struct comp_unit_head header;
e142c38c 399
d00adf39
DE
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
e142c38c
DJ
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
b0f35d58
DL
410 const char *producer;
411
e142c38c
DJ
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
433df2d4
DE
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
72bf9492 427
b64f50a1
JK
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
ae038cb0
DJ
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
69d751e3 442 /* Backlink to our per_cu entry. */
ae038cb0
DJ
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
b64f50a1
JK
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
51545339 450 htab_t die_hash;
10b3939b
DJ
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
cb1df416
DJ
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
96408a79
SA
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
3019eac3
DE
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
2e3cf129
DE
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
2e3cf129
DE
496 ULONGEST ranges_base;
497
ae038cb0
DJ
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
8be455d7
JK
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 505 unsigned int has_loclist : 1;
ba919b58 506
1b80a9fa
JK
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
ba919b58
TT
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 513 unsigned int producer_is_gcc_lt_4_3 : 1;
685b1105 514 unsigned int producer_is_icc : 1;
4d4ec4e5
TT
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
e7c27a73
DJ
521};
522
10b3939b
DJ
523/* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
28dee7f5 525 read_symtab_private field of the psymtab. */
10b3939b 526
ae038cb0
DJ
527struct dwarf2_per_cu_data
528{
36586728 529 /* The start offset and length of this compilation unit.
45452591 530 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
b64f50a1 534 sect_offset offset;
36586728 535 unsigned int length;
ae038cb0
DJ
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
c764a876 539 unsigned int queued : 1;
ae038cb0 540
0d99eb77
DE
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
0186c6a7
DE
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
3019eac3
DE
550 unsigned int is_debug_types : 1;
551
36586728
TT
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
a2ce51a0
DE
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
7ee85ab1
DE
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
567 "midflight").
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
570
3019eac3
DE
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
573 not the DWO file. */
8a0459fd 574 struct dwarf2_section_info *section;
348e048f 575
17ea53c3
JK
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
ae038cb0 578 struct dwarf2_cu *cu;
1c379e20 579
9cdd5dbd
DE
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
583 struct objfile *objfile;
584
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
587 union
588 {
589 /* The partial symbol table associated with this compilation unit,
95554aad 590 or NULL for unread partial units. */
9291a0cd
TT
591 struct partial_symtab *psymtab;
592
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
595 } v;
95554aad 596
796a7ff8
DE
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
601
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
69d751e3
DE
613 indices so we only pay a price for gold generated indices.
614 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
796a7ff8 615 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
616};
617
348e048f
DE
618/* Entry in the signatured_types hash table. */
619
620struct signatured_type
621{
42e7ad6c 622 /* The "per_cu" object of this type.
ac9ec31b 623 This struct is used iff per_cu.is_debug_types.
42e7ad6c
DE
624 N.B.: This is the first member so that it's easy to convert pointers
625 between them. */
626 struct dwarf2_per_cu_data per_cu;
627
3019eac3 628 /* The type's signature. */
348e048f
DE
629 ULONGEST signature;
630
3019eac3 631 /* Offset in the TU of the type's DIE, as read from the TU header.
c88ee1f0
DE
632 If this TU is a DWO stub and the definition lives in a DWO file
633 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
3019eac3
DE
634 cu_offset type_offset_in_tu;
635
636 /* Offset in the section of the type's DIE.
637 If the definition lives in a DWO file, this is the offset in the
638 .debug_types.dwo section.
639 The value is zero until the actual value is known.
640 Zero is otherwise not a valid section offset. */
641 sect_offset type_offset_in_section;
0186c6a7
DE
642
643 /* Type units are grouped by their DW_AT_stmt_list entry so that they
644 can share them. This points to the containing symtab. */
645 struct type_unit_group *type_unit_group;
ac9ec31b
DE
646
647 /* The type.
648 The first time we encounter this type we fully read it in and install it
649 in the symbol tables. Subsequent times we only need the type. */
650 struct type *type;
a2ce51a0
DE
651
652 /* Containing DWO unit.
653 This field is valid iff per_cu.reading_dwo_directly. */
654 struct dwo_unit *dwo_unit;
348e048f
DE
655};
656
0186c6a7
DE
657typedef struct signatured_type *sig_type_ptr;
658DEF_VEC_P (sig_type_ptr);
659
094b34ac
DE
660/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
661 This includes type_unit_group and quick_file_names. */
662
663struct stmt_list_hash
664{
665 /* The DWO unit this table is from or NULL if there is none. */
666 struct dwo_unit *dwo_unit;
667
668 /* Offset in .debug_line or .debug_line.dwo. */
669 sect_offset line_offset;
670};
671
f4dc4d17
DE
672/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
673 an object of this type. */
674
675struct type_unit_group
676{
0186c6a7 677 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
678 To simplify things we create an artificial CU that "includes" all the
679 type units using this stmt_list so that the rest of the code still has
680 a "per_cu" handle on the symtab.
681 This PER_CU is recognized by having no section. */
8a0459fd 682#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
683 struct dwarf2_per_cu_data per_cu;
684
0186c6a7
DE
685 /* The TUs that share this DW_AT_stmt_list entry.
686 This is added to while parsing type units to build partial symtabs,
687 and is deleted afterwards and not used again. */
688 VEC (sig_type_ptr) *tus;
f4dc4d17
DE
689
690 /* The primary symtab.
094b34ac
DE
691 Type units in a group needn't all be defined in the same source file,
692 so we create an essentially anonymous symtab as the primary symtab. */
f4dc4d17
DE
693 struct symtab *primary_symtab;
694
094b34ac
DE
695 /* The data used to construct the hash key. */
696 struct stmt_list_hash hash;
f4dc4d17
DE
697
698 /* The number of symtabs from the line header.
699 The value here must match line_header.num_file_names. */
700 unsigned int num_symtabs;
701
702 /* The symbol tables for this TU (obtained from the files listed in
703 DW_AT_stmt_list).
704 WARNING: The order of entries here must match the order of entries
705 in the line header. After the first TU using this type_unit_group, the
706 line header for the subsequent TUs is recreated from this. This is done
707 because we need to use the same symtabs for each TU using the same
708 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
709 there's no guarantee the line header doesn't have duplicate entries. */
710 struct symtab **symtabs;
711};
712
80626a55 713/* These sections are what may appear in a DWO file. */
3019eac3
DE
714
715struct dwo_sections
716{
717 struct dwarf2_section_info abbrev;
3019eac3
DE
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
09262596
DE
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
3019eac3
DE
722 struct dwarf2_section_info str;
723 struct dwarf2_section_info str_offsets;
80626a55
DE
724 /* In the case of a virtual DWO file, these two are unused. */
725 struct dwarf2_section_info info;
3019eac3
DE
726 VEC (dwarf2_section_info_def) *types;
727};
728
c88ee1f0 729/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
730
731struct dwo_unit
732{
733 /* Backlink to the containing struct dwo_file. */
734 struct dwo_file *dwo_file;
735
736 /* The "id" that distinguishes this CU/TU.
737 .debug_info calls this "dwo_id", .debug_types calls this "signature".
738 Since signatures came first, we stick with it for consistency. */
739 ULONGEST signature;
740
741 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 742 struct dwarf2_section_info *section;
3019eac3
DE
743
744 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
745 sect_offset offset;
746 unsigned int length;
747
748 /* For types, offset in the type's DIE of the type defined by this TU. */
749 cu_offset type_offset_in_tu;
750};
751
80626a55
DE
752/* Data for one DWO file.
753 This includes virtual DWO files that have been packaged into a
754 DWP file. */
3019eac3
DE
755
756struct dwo_file
757{
0ac5b59e 758 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
759 For virtual DWO files the name is constructed from the section offsets
760 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
761 from related CU+TUs. */
0ac5b59e
DE
762 const char *dwo_name;
763
764 /* The DW_AT_comp_dir attribute. */
765 const char *comp_dir;
3019eac3 766
80626a55
DE
767 /* The bfd, when the file is open. Otherwise this is NULL.
768 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
769 bfd *dbfd;
3019eac3
DE
770
771 /* Section info for this file. */
772 struct dwo_sections sections;
773
19c3d4c9
DE
774 /* The CU in the file.
775 We only support one because having more than one requires hacking the
776 dwo_name of each to match, which is highly unlikely to happen.
777 Doing this means all TUs can share comp_dir: We also assume that
778 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
779 struct dwo_unit *cu;
3019eac3
DE
780
781 /* Table of TUs in the file.
782 Each element is a struct dwo_unit. */
783 htab_t tus;
784};
785
80626a55
DE
786/* These sections are what may appear in a DWP file. */
787
788struct dwp_sections
789{
790 struct dwarf2_section_info str;
791 struct dwarf2_section_info cu_index;
792 struct dwarf2_section_info tu_index;
793 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
794 by section number. We don't need to record them here. */
795};
796
797/* These sections are what may appear in a virtual DWO file. */
798
799struct virtual_dwo_sections
800{
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info line;
803 struct dwarf2_section_info loc;
804 struct dwarf2_section_info macinfo;
805 struct dwarf2_section_info macro;
806 struct dwarf2_section_info str_offsets;
807 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 808 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
809 struct dwarf2_section_info info_or_types;
810};
811
812/* Contents of DWP hash tables. */
813
814struct dwp_hash_table
815{
816 uint32_t nr_units, nr_slots;
817 const gdb_byte *hash_table, *unit_table, *section_pool;
818};
819
820/* Data for one DWP file. */
821
822struct dwp_file
823{
824 /* Name of the file. */
825 const char *name;
826
93417882 827 /* The bfd. */
80626a55
DE
828 bfd *dbfd;
829
830 /* Section info for this file. */
831 struct dwp_sections sections;
832
833 /* Table of CUs in the file. */
834 const struct dwp_hash_table *cus;
835
836 /* Table of TUs in the file. */
837 const struct dwp_hash_table *tus;
838
839 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
840 htab_t loaded_cutus;
841
842 /* Table to map ELF section numbers to their sections. */
843 unsigned int num_sections;
844 asection **elf_sections;
845};
846
36586728
TT
847/* This represents a '.dwz' file. */
848
849struct dwz_file
850{
851 /* A dwz file can only contain a few sections. */
852 struct dwarf2_section_info abbrev;
853 struct dwarf2_section_info info;
854 struct dwarf2_section_info str;
855 struct dwarf2_section_info line;
856 struct dwarf2_section_info macro;
2ec9a5e0 857 struct dwarf2_section_info gdb_index;
36586728
TT
858
859 /* The dwz's BFD. */
860 bfd *dwz_bfd;
861};
862
0963b4bd
MS
863/* Struct used to pass misc. parameters to read_die_and_children, et
864 al. which are used for both .debug_info and .debug_types dies.
865 All parameters here are unchanging for the life of the call. This
dee91e82 866 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
867
868struct die_reader_specs
869{
dee91e82 870 /* die_section->asection->owner. */
93311388
DE
871 bfd* abfd;
872
873 /* The CU of the DIE we are parsing. */
874 struct dwarf2_cu *cu;
875
80626a55 876 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
877 struct dwo_file *dwo_file;
878
dee91e82 879 /* The section the die comes from.
3019eac3 880 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
881 struct dwarf2_section_info *die_section;
882
883 /* die_section->buffer. */
d521ce57 884 const gdb_byte *buffer;
f664829e
DE
885
886 /* The end of the buffer. */
887 const gdb_byte *buffer_end;
a2ce51a0
DE
888
889 /* The value of the DW_AT_comp_dir attribute. */
890 const char *comp_dir;
93311388
DE
891};
892
fd820528 893/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 894typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 895 const gdb_byte *info_ptr,
dee91e82
DE
896 struct die_info *comp_unit_die,
897 int has_children,
898 void *data);
899
debd256d
JB
900/* The line number information for a compilation unit (found in the
901 .debug_line section) begins with a "statement program header",
902 which contains the following information. */
903struct line_header
904{
905 unsigned int total_length;
906 unsigned short version;
907 unsigned int header_length;
908 unsigned char minimum_instruction_length;
2dc7f7b3 909 unsigned char maximum_ops_per_instruction;
debd256d
JB
910 unsigned char default_is_stmt;
911 int line_base;
912 unsigned char line_range;
913 unsigned char opcode_base;
914
915 /* standard_opcode_lengths[i] is the number of operands for the
916 standard opcode whose value is i. This means that
917 standard_opcode_lengths[0] is unused, and the last meaningful
918 element is standard_opcode_lengths[opcode_base - 1]. */
919 unsigned char *standard_opcode_lengths;
920
921 /* The include_directories table. NOTE! These strings are not
922 allocated with xmalloc; instead, they are pointers into
923 debug_line_buffer. If you try to free them, `free' will get
924 indigestion. */
925 unsigned int num_include_dirs, include_dirs_size;
d521ce57 926 const char **include_dirs;
debd256d
JB
927
928 /* The file_names table. NOTE! These strings are not allocated
929 with xmalloc; instead, they are pointers into debug_line_buffer.
930 Don't try to free them directly. */
931 unsigned int num_file_names, file_names_size;
932 struct file_entry
c906108c 933 {
d521ce57 934 const char *name;
debd256d
JB
935 unsigned int dir_index;
936 unsigned int mod_time;
937 unsigned int length;
aaa75496 938 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 939 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
940 } *file_names;
941
942 /* The start and end of the statement program following this
6502dd73 943 header. These point into dwarf2_per_objfile->line_buffer. */
d521ce57 944 const gdb_byte *statement_program_start, *statement_program_end;
debd256d 945};
c906108c
SS
946
947/* When we construct a partial symbol table entry we only
0963b4bd 948 need this much information. */
c906108c
SS
949struct partial_die_info
950 {
72bf9492 951 /* Offset of this DIE. */
b64f50a1 952 sect_offset offset;
72bf9492
DJ
953
954 /* DWARF-2 tag for this DIE. */
955 ENUM_BITFIELD(dwarf_tag) tag : 16;
956
72bf9492
DJ
957 /* Assorted flags describing the data found in this DIE. */
958 unsigned int has_children : 1;
959 unsigned int is_external : 1;
960 unsigned int is_declaration : 1;
961 unsigned int has_type : 1;
962 unsigned int has_specification : 1;
963 unsigned int has_pc_info : 1;
481860b3 964 unsigned int may_be_inlined : 1;
72bf9492
DJ
965
966 /* Flag set if the SCOPE field of this structure has been
967 computed. */
968 unsigned int scope_set : 1;
969
fa4028e9
JB
970 /* Flag set if the DIE has a byte_size attribute. */
971 unsigned int has_byte_size : 1;
972
98bfdba5
PA
973 /* Flag set if any of the DIE's children are template arguments. */
974 unsigned int has_template_arguments : 1;
975
abc72ce4
DE
976 /* Flag set if fixup_partial_die has been called on this die. */
977 unsigned int fixup_called : 1;
978
36586728
TT
979 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
980 unsigned int is_dwz : 1;
981
982 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
983 unsigned int spec_is_dwz : 1;
984
72bf9492 985 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 986 sometimes a default name for unnamed DIEs. */
15d034d0 987 const char *name;
72bf9492 988
abc72ce4
DE
989 /* The linkage name, if present. */
990 const char *linkage_name;
991
72bf9492
DJ
992 /* The scope to prepend to our children. This is generally
993 allocated on the comp_unit_obstack, so will disappear
994 when this compilation unit leaves the cache. */
15d034d0 995 const char *scope;
72bf9492 996
95554aad
TT
997 /* Some data associated with the partial DIE. The tag determines
998 which field is live. */
999 union
1000 {
1001 /* The location description associated with this DIE, if any. */
1002 struct dwarf_block *locdesc;
1003 /* The offset of an import, for DW_TAG_imported_unit. */
1004 sect_offset offset;
1005 } d;
72bf9492
DJ
1006
1007 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
1008 CORE_ADDR lowpc;
1009 CORE_ADDR highpc;
72bf9492 1010
93311388 1011 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1012 DW_AT_sibling, if any. */
abc72ce4
DE
1013 /* NOTE: This member isn't strictly necessary, read_partial_die could
1014 return DW_AT_sibling values to its caller load_partial_dies. */
d521ce57 1015 const gdb_byte *sibling;
72bf9492
DJ
1016
1017 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1018 DW_AT_specification (or DW_AT_abstract_origin or
1019 DW_AT_extension). */
b64f50a1 1020 sect_offset spec_offset;
72bf9492
DJ
1021
1022 /* Pointers to this DIE's parent, first child, and next sibling,
1023 if any. */
1024 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
1025 };
1026
0963b4bd 1027/* This data structure holds the information of an abbrev. */
c906108c
SS
1028struct abbrev_info
1029 {
1030 unsigned int number; /* number identifying abbrev */
1031 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1032 unsigned short has_children; /* boolean */
1033 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1034 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1035 struct abbrev_info *next; /* next in chain */
1036 };
1037
1038struct attr_abbrev
1039 {
9d25dd43
DE
1040 ENUM_BITFIELD(dwarf_attribute) name : 16;
1041 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
1042 };
1043
433df2d4
DE
1044/* Size of abbrev_table.abbrev_hash_table. */
1045#define ABBREV_HASH_SIZE 121
1046
1047/* Top level data structure to contain an abbreviation table. */
1048
1049struct abbrev_table
1050{
f4dc4d17
DE
1051 /* Where the abbrev table came from.
1052 This is used as a sanity check when the table is used. */
433df2d4
DE
1053 sect_offset offset;
1054
1055 /* Storage for the abbrev table. */
1056 struct obstack abbrev_obstack;
1057
1058 /* Hash table of abbrevs.
1059 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1060 It could be statically allocated, but the previous code didn't so we
1061 don't either. */
1062 struct abbrev_info **abbrevs;
1063};
1064
0963b4bd 1065/* Attributes have a name and a value. */
b60c80d6
DJ
1066struct attribute
1067 {
9d25dd43 1068 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1069 ENUM_BITFIELD(dwarf_form) form : 15;
1070
1071 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1072 field should be in u.str (existing only for DW_STRING) but it is kept
1073 here for better struct attribute alignment. */
1074 unsigned int string_is_canonical : 1;
1075
b60c80d6
DJ
1076 union
1077 {
15d034d0 1078 const char *str;
b60c80d6 1079 struct dwarf_block *blk;
43bbcdc2
PH
1080 ULONGEST unsnd;
1081 LONGEST snd;
b60c80d6 1082 CORE_ADDR addr;
ac9ec31b 1083 ULONGEST signature;
b60c80d6
DJ
1084 }
1085 u;
1086 };
1087
0963b4bd 1088/* This data structure holds a complete die structure. */
c906108c
SS
1089struct die_info
1090 {
76815b17
DE
1091 /* DWARF-2 tag for this DIE. */
1092 ENUM_BITFIELD(dwarf_tag) tag : 16;
1093
1094 /* Number of attributes */
98bfdba5
PA
1095 unsigned char num_attrs;
1096
1097 /* True if we're presently building the full type name for the
1098 type derived from this DIE. */
1099 unsigned char building_fullname : 1;
76815b17
DE
1100
1101 /* Abbrev number */
1102 unsigned int abbrev;
1103
93311388 1104 /* Offset in .debug_info or .debug_types section. */
b64f50a1 1105 sect_offset offset;
78ba4af6
JB
1106
1107 /* The dies in a compilation unit form an n-ary tree. PARENT
1108 points to this die's parent; CHILD points to the first child of
1109 this node; and all the children of a given node are chained
4950bc1c 1110 together via their SIBLING fields. */
639d11d3
DC
1111 struct die_info *child; /* Its first child, if any. */
1112 struct die_info *sibling; /* Its next sibling, if any. */
1113 struct die_info *parent; /* Its parent, if any. */
c906108c 1114
b60c80d6
DJ
1115 /* An array of attributes, with NUM_ATTRS elements. There may be
1116 zero, but it's not common and zero-sized arrays are not
1117 sufficiently portable C. */
1118 struct attribute attrs[1];
c906108c
SS
1119 };
1120
0963b4bd 1121/* Get at parts of an attribute structure. */
c906108c
SS
1122
1123#define DW_STRING(attr) ((attr)->u.str)
8285870a 1124#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1125#define DW_UNSND(attr) ((attr)->u.unsnd)
1126#define DW_BLOCK(attr) ((attr)->u.blk)
1127#define DW_SND(attr) ((attr)->u.snd)
1128#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1129#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1130
0963b4bd 1131/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1132struct dwarf_block
1133 {
56eb65bd 1134 size_t size;
1d6edc3c
JK
1135
1136 /* Valid only if SIZE is not zero. */
d521ce57 1137 const gdb_byte *data;
c906108c
SS
1138 };
1139
c906108c
SS
1140#ifndef ATTR_ALLOC_CHUNK
1141#define ATTR_ALLOC_CHUNK 4
1142#endif
1143
c906108c
SS
1144/* Allocate fields for structs, unions and enums in this size. */
1145#ifndef DW_FIELD_ALLOC_CHUNK
1146#define DW_FIELD_ALLOC_CHUNK 4
1147#endif
1148
c906108c
SS
1149/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1150 but this would require a corresponding change in unpack_field_as_long
1151 and friends. */
1152static int bits_per_byte = 8;
1153
1154/* The routines that read and process dies for a C struct or C++ class
1155 pass lists of data member fields and lists of member function fields
1156 in an instance of a field_info structure, as defined below. */
1157struct field_info
c5aa993b 1158 {
0963b4bd 1159 /* List of data member and baseclasses fields. */
c5aa993b
JM
1160 struct nextfield
1161 {
1162 struct nextfield *next;
1163 int accessibility;
1164 int virtuality;
1165 struct field field;
1166 }
7d0ccb61 1167 *fields, *baseclasses;
c906108c 1168
7d0ccb61 1169 /* Number of fields (including baseclasses). */
c5aa993b 1170 int nfields;
c906108c 1171
c5aa993b
JM
1172 /* Number of baseclasses. */
1173 int nbaseclasses;
c906108c 1174
c5aa993b
JM
1175 /* Set if the accesibility of one of the fields is not public. */
1176 int non_public_fields;
c906108c 1177
c5aa993b
JM
1178 /* Member function fields array, entries are allocated in the order they
1179 are encountered in the object file. */
1180 struct nextfnfield
1181 {
1182 struct nextfnfield *next;
1183 struct fn_field fnfield;
1184 }
1185 *fnfields;
c906108c 1186
c5aa993b
JM
1187 /* Member function fieldlist array, contains name of possibly overloaded
1188 member function, number of overloaded member functions and a pointer
1189 to the head of the member function field chain. */
1190 struct fnfieldlist
1191 {
15d034d0 1192 const char *name;
c5aa993b
JM
1193 int length;
1194 struct nextfnfield *head;
1195 }
1196 *fnfieldlists;
c906108c 1197
c5aa993b
JM
1198 /* Number of entries in the fnfieldlists array. */
1199 int nfnfields;
98751a41
JK
1200
1201 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1202 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1203 struct typedef_field_list
1204 {
1205 struct typedef_field field;
1206 struct typedef_field_list *next;
1207 }
1208 *typedef_field_list;
1209 unsigned typedef_field_list_count;
c5aa993b 1210 };
c906108c 1211
10b3939b
DJ
1212/* One item on the queue of compilation units to read in full symbols
1213 for. */
1214struct dwarf2_queue_item
1215{
1216 struct dwarf2_per_cu_data *per_cu;
95554aad 1217 enum language pretend_language;
10b3939b
DJ
1218 struct dwarf2_queue_item *next;
1219};
1220
1221/* The current queue. */
1222static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1223
ae038cb0
DJ
1224/* Loaded secondary compilation units are kept in memory until they
1225 have not been referenced for the processing of this many
1226 compilation units. Set this to zero to disable caching. Cache
1227 sizes of up to at least twenty will improve startup time for
1228 typical inter-CU-reference binaries, at an obvious memory cost. */
1229static int dwarf2_max_cache_age = 5;
920d2a44
AC
1230static void
1231show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1232 struct cmd_list_element *c, const char *value)
1233{
3e43a32a
MS
1234 fprintf_filtered (file, _("The upper bound on the age of cached "
1235 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
1236 value);
1237}
4390d890 1238\f
c906108c
SS
1239/* local function prototypes */
1240
4efb68b1 1241static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1242
918dd910
JK
1243static void dwarf2_find_base_address (struct die_info *die,
1244 struct dwarf2_cu *cu);
1245
0018ea6f
DE
1246static struct partial_symtab *create_partial_symtab
1247 (struct dwarf2_per_cu_data *per_cu, const char *name);
1248
c67a9c90 1249static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1250
72bf9492
DJ
1251static void scan_partial_symbols (struct partial_die_info *,
1252 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1253 int, struct dwarf2_cu *);
c906108c 1254
72bf9492
DJ
1255static void add_partial_symbol (struct partial_die_info *,
1256 struct dwarf2_cu *);
63d06c5c 1257
72bf9492
DJ
1258static void add_partial_namespace (struct partial_die_info *pdi,
1259 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1260 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1261
5d7cb8df
JK
1262static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1263 CORE_ADDR *highpc, int need_pc,
1264 struct dwarf2_cu *cu);
1265
72bf9492
DJ
1266static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1267 struct dwarf2_cu *cu);
91c24f0a 1268
bc30ff58
JB
1269static void add_partial_subprogram (struct partial_die_info *pdi,
1270 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1271 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1272
257e7a09
YQ
1273static void dwarf2_read_symtab (struct partial_symtab *,
1274 struct objfile *);
c906108c 1275
a14ed312 1276static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1277
433df2d4
DE
1278static struct abbrev_info *abbrev_table_lookup_abbrev
1279 (const struct abbrev_table *, unsigned int);
1280
1281static struct abbrev_table *abbrev_table_read_table
1282 (struct dwarf2_section_info *, sect_offset);
1283
1284static void abbrev_table_free (struct abbrev_table *);
1285
f4dc4d17
DE
1286static void abbrev_table_free_cleanup (void *);
1287
dee91e82
DE
1288static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1289 struct dwarf2_section_info *);
c906108c 1290
f3dd6933 1291static void dwarf2_free_abbrev_table (void *);
c906108c 1292
d521ce57 1293static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1294
dee91e82 1295static struct partial_die_info *load_partial_dies
d521ce57 1296 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1297
d521ce57
TT
1298static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1299 struct partial_die_info *,
1300 struct abbrev_info *,
1301 unsigned int,
1302 const gdb_byte *);
c906108c 1303
36586728 1304static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1305 struct dwarf2_cu *);
72bf9492
DJ
1306
1307static void fixup_partial_die (struct partial_die_info *,
1308 struct dwarf2_cu *);
1309
d521ce57
TT
1310static const gdb_byte *read_attribute (const struct die_reader_specs *,
1311 struct attribute *, struct attr_abbrev *,
1312 const gdb_byte *);
a8329558 1313
a1855c1d 1314static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1315
a1855c1d 1316static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1317
a1855c1d 1318static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1319
a1855c1d 1320static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1321
a1855c1d 1322static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1323
d521ce57 1324static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1325 unsigned int *);
c906108c 1326
d521ce57 1327static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1328
1329static LONGEST read_checked_initial_length_and_offset
d521ce57 1330 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1331 unsigned int *, unsigned int *);
613e1657 1332
d521ce57
TT
1333static LONGEST read_offset (bfd *, const gdb_byte *,
1334 const struct comp_unit_head *,
c764a876
DE
1335 unsigned int *);
1336
d521ce57 1337static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1338
f4dc4d17
DE
1339static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1340 sect_offset);
1341
d521ce57 1342static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1343
d521ce57 1344static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1345
d521ce57
TT
1346static const char *read_indirect_string (bfd *, const gdb_byte *,
1347 const struct comp_unit_head *,
1348 unsigned int *);
4bdf3d34 1349
d521ce57 1350static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
36586728 1351
d521ce57 1352static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1353
d521ce57 1354static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1355
d521ce57
TT
1356static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1357 const gdb_byte *,
3019eac3
DE
1358 unsigned int *);
1359
d521ce57
TT
1360static const char *read_str_index (const struct die_reader_specs *reader,
1361 struct dwarf2_cu *cu, ULONGEST str_index);
3019eac3 1362
e142c38c 1363static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1364
e142c38c
DJ
1365static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1366 struct dwarf2_cu *);
c906108c 1367
348e048f 1368static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1369 unsigned int);
348e048f 1370
05cf31d1
JB
1371static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1372 struct dwarf2_cu *cu);
1373
e142c38c 1374static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1375
e142c38c 1376static struct die_info *die_specification (struct die_info *die,
f2f0e013 1377 struct dwarf2_cu **);
63d06c5c 1378
debd256d
JB
1379static void free_line_header (struct line_header *lh);
1380
3019eac3
DE
1381static struct line_header *dwarf_decode_line_header (unsigned int offset,
1382 struct dwarf2_cu *cu);
debd256d 1383
f3f5162e
DE
1384static void dwarf_decode_lines (struct line_header *, const char *,
1385 struct dwarf2_cu *, struct partial_symtab *,
1386 int);
c906108c 1387
d521ce57 1388static void dwarf2_start_subfile (const char *, const char *, const char *);
c906108c 1389
f4dc4d17 1390static void dwarf2_start_symtab (struct dwarf2_cu *,
15d034d0 1391 const char *, const char *, CORE_ADDR);
f4dc4d17 1392
a14ed312 1393static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1394 struct dwarf2_cu *);
c906108c 1395
34eaf542
TT
1396static struct symbol *new_symbol_full (struct die_info *, struct type *,
1397 struct dwarf2_cu *, struct symbol *);
1398
ff39bb5e 1399static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1400 struct dwarf2_cu *);
c906108c 1401
ff39bb5e 1402static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1403 struct type *type,
1404 const char *name,
1405 struct obstack *obstack,
12df843f 1406 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1407 const gdb_byte **bytes,
98bfdba5 1408 struct dwarf2_locexpr_baton **baton);
2df3850c 1409
e7c27a73 1410static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1411
b4ba55a1
JB
1412static int need_gnat_info (struct dwarf2_cu *);
1413
3e43a32a
MS
1414static struct type *die_descriptive_type (struct die_info *,
1415 struct dwarf2_cu *);
b4ba55a1
JB
1416
1417static void set_descriptive_type (struct type *, struct die_info *,
1418 struct dwarf2_cu *);
1419
e7c27a73
DJ
1420static struct type *die_containing_type (struct die_info *,
1421 struct dwarf2_cu *);
c906108c 1422
ff39bb5e 1423static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1424 struct dwarf2_cu *);
c906108c 1425
f792889a 1426static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1427
673bfd45
DE
1428static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1429
0d5cff50 1430static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1431
6e70227d 1432static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1433 const char *suffix, int physname,
1434 struct dwarf2_cu *cu);
63d06c5c 1435
e7c27a73 1436static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1437
348e048f
DE
1438static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1439
e7c27a73 1440static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1441
e7c27a73 1442static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1443
96408a79
SA
1444static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1445
ff013f42
JK
1446static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1447 struct dwarf2_cu *, struct partial_symtab *);
1448
a14ed312 1449static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1450 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1451 struct partial_symtab *);
c906108c 1452
fae299cd
DC
1453static void get_scope_pc_bounds (struct die_info *,
1454 CORE_ADDR *, CORE_ADDR *,
1455 struct dwarf2_cu *);
1456
801e3a5b
JB
1457static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1458 CORE_ADDR, struct dwarf2_cu *);
1459
a14ed312 1460static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1461 struct dwarf2_cu *);
c906108c 1462
a14ed312 1463static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1464 struct type *, struct dwarf2_cu *);
c906108c 1465
a14ed312 1466static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1467 struct die_info *, struct type *,
e7c27a73 1468 struct dwarf2_cu *);
c906108c 1469
a14ed312 1470static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1471 struct type *,
1472 struct dwarf2_cu *);
c906108c 1473
134d01f1 1474static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1475
e7c27a73 1476static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1477
e7c27a73 1478static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1479
5d7cb8df
JK
1480static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1481
27aa8d6a
SW
1482static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1483
f55ee35c
JK
1484static struct type *read_module_type (struct die_info *die,
1485 struct dwarf2_cu *cu);
1486
38d518c9 1487static const char *namespace_name (struct die_info *die,
e142c38c 1488 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1489
134d01f1 1490static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1491
e7c27a73 1492static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1493
6e70227d 1494static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1495 struct dwarf2_cu *);
1496
bf6af496 1497static struct die_info *read_die_and_siblings_1
d521ce57 1498 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1499 struct die_info *);
639d11d3 1500
dee91e82 1501static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1502 const gdb_byte *info_ptr,
1503 const gdb_byte **new_info_ptr,
639d11d3
DC
1504 struct die_info *parent);
1505
d521ce57
TT
1506static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1507 struct die_info **, const gdb_byte *,
1508 int *, int);
3019eac3 1509
d521ce57
TT
1510static const gdb_byte *read_full_die (const struct die_reader_specs *,
1511 struct die_info **, const gdb_byte *,
1512 int *);
93311388 1513
e7c27a73 1514static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1515
15d034d0
TT
1516static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1517 struct obstack *);
71c25dea 1518
15d034d0 1519static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1520
15d034d0 1521static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1522 struct die_info *die,
1523 struct dwarf2_cu *cu);
1524
ca69b9e6
DE
1525static const char *dwarf2_physname (const char *name, struct die_info *die,
1526 struct dwarf2_cu *cu);
1527
e142c38c 1528static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1529 struct dwarf2_cu **);
9219021c 1530
f39c6ffd 1531static const char *dwarf_tag_name (unsigned int);
c906108c 1532
f39c6ffd 1533static const char *dwarf_attr_name (unsigned int);
c906108c 1534
f39c6ffd 1535static const char *dwarf_form_name (unsigned int);
c906108c 1536
a14ed312 1537static char *dwarf_bool_name (unsigned int);
c906108c 1538
f39c6ffd 1539static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1540
f9aca02d 1541static struct die_info *sibling_die (struct die_info *);
c906108c 1542
d97bc12b
DE
1543static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1544
1545static void dump_die_for_error (struct die_info *);
1546
1547static void dump_die_1 (struct ui_file *, int level, int max_level,
1548 struct die_info *);
c906108c 1549
d97bc12b 1550/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1551
51545339 1552static void store_in_ref_table (struct die_info *,
10b3939b 1553 struct dwarf2_cu *);
c906108c 1554
ff39bb5e 1555static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1556
ff39bb5e 1557static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1558
348e048f 1559static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1560 const struct attribute *,
348e048f
DE
1561 struct dwarf2_cu **);
1562
10b3939b 1563static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1564 const struct attribute *,
f2f0e013 1565 struct dwarf2_cu **);
c906108c 1566
348e048f 1567static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1568 const struct attribute *,
348e048f
DE
1569 struct dwarf2_cu **);
1570
ac9ec31b
DE
1571static struct type *get_signatured_type (struct die_info *, ULONGEST,
1572 struct dwarf2_cu *);
1573
1574static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1575 const struct attribute *,
ac9ec31b
DE
1576 struct dwarf2_cu *);
1577
e5fe5e75 1578static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1579
52dc124a 1580static void read_signatured_type (struct signatured_type *);
348e048f 1581
f4dc4d17 1582static struct type_unit_group *get_type_unit_group
ff39bb5e 1583 (struct dwarf2_cu *, const struct attribute *);
f4dc4d17
DE
1584
1585static void build_type_unit_groups (die_reader_func_ftype *, void *);
1586
c906108c
SS
1587/* memory allocation interface */
1588
7b5a2f43 1589static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1590
b60c80d6 1591static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1592
09262596 1593static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
15d034d0 1594 const char *, int);
2e276125 1595
6e5a29e1 1596static int attr_form_is_block (const struct attribute *);
8e19ed76 1597
6e5a29e1 1598static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1599
6e5a29e1 1600static int attr_form_is_constant (const struct attribute *);
3690dd37 1601
6e5a29e1 1602static int attr_form_is_ref (const struct attribute *);
7771576e 1603
8cf6f0b1
TT
1604static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1605 struct dwarf2_loclist_baton *baton,
ff39bb5e 1606 const struct attribute *attr);
8cf6f0b1 1607
ff39bb5e 1608static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1609 struct symbol *sym,
f1e6e072
TT
1610 struct dwarf2_cu *cu,
1611 int is_block);
4c2df51b 1612
d521ce57
TT
1613static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1614 const gdb_byte *info_ptr,
1615 struct abbrev_info *abbrev);
4bb7a0a7 1616
72bf9492
DJ
1617static void free_stack_comp_unit (void *);
1618
72bf9492
DJ
1619static hashval_t partial_die_hash (const void *item);
1620
1621static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1622
ae038cb0 1623static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
36586728 1624 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
ae038cb0 1625
9816fde3 1626static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1627 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1628
1629static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1630 struct die_info *comp_unit_die,
1631 enum language pretend_language);
93311388 1632
68dc6402 1633static void free_heap_comp_unit (void *);
ae038cb0
DJ
1634
1635static void free_cached_comp_units (void *);
1636
1637static void age_cached_comp_units (void);
1638
dee91e82 1639static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1640
f792889a
DJ
1641static struct type *set_die_type (struct die_info *, struct type *,
1642 struct dwarf2_cu *);
1c379e20 1643
ae038cb0
DJ
1644static void create_all_comp_units (struct objfile *);
1645
0e50663e 1646static int create_all_type_units (struct objfile *);
1fd400ff 1647
95554aad
TT
1648static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1649 enum language);
10b3939b 1650
95554aad
TT
1651static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1652 enum language);
10b3939b 1653
f4dc4d17
DE
1654static void process_full_type_unit (struct dwarf2_per_cu_data *,
1655 enum language);
1656
10b3939b
DJ
1657static void dwarf2_add_dependence (struct dwarf2_cu *,
1658 struct dwarf2_per_cu_data *);
1659
ae038cb0
DJ
1660static void dwarf2_mark (struct dwarf2_cu *);
1661
1662static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1663
b64f50a1 1664static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1665 struct dwarf2_per_cu_data *);
673bfd45 1666
f792889a 1667static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1668
9291a0cd
TT
1669static void dwarf2_release_queue (void *dummy);
1670
95554aad
TT
1671static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1672 enum language pretend_language);
1673
a0f42c21 1674static void process_queue (void);
9291a0cd
TT
1675
1676static void find_file_and_directory (struct die_info *die,
1677 struct dwarf2_cu *cu,
15d034d0 1678 const char **name, const char **comp_dir);
9291a0cd
TT
1679
1680static char *file_full_name (int file, struct line_header *lh,
1681 const char *comp_dir);
1682
d521ce57 1683static const gdb_byte *read_and_check_comp_unit_head
36586728
TT
1684 (struct comp_unit_head *header,
1685 struct dwarf2_section_info *section,
d521ce57 1686 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
36586728
TT
1687 int is_debug_types_section);
1688
fd820528 1689static void init_cutu_and_read_dies
f4dc4d17
DE
1690 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1691 int use_existing_cu, int keep,
3019eac3
DE
1692 die_reader_func_ftype *die_reader_func, void *data);
1693
dee91e82
DE
1694static void init_cutu_and_read_dies_simple
1695 (struct dwarf2_per_cu_data *this_cu,
1696 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1697
673bfd45 1698static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1699
3019eac3
DE
1700static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1701
a2ce51a0
DE
1702static struct dwo_unit *lookup_dwo_in_dwp
1703 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1704 const char *comp_dir, ULONGEST signature, int is_debug_types);
1705
1706static struct dwp_file *get_dwp_file (void);
1707
3019eac3 1708static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1709 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1710
1711static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1712 (struct signatured_type *, const char *, const char *);
3019eac3 1713
89e63ee4
DE
1714static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1715
3019eac3
DE
1716static void free_dwo_file_cleanup (void *);
1717
95554aad
TT
1718static void process_cu_includes (void);
1719
1b80a9fa 1720static void check_producer (struct dwarf2_cu *cu);
4390d890
DE
1721\f
1722/* Various complaints about symbol reading that don't abort the process. */
1723
1724static void
1725dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1726{
1727 complaint (&symfile_complaints,
1728 _("statement list doesn't fit in .debug_line section"));
1729}
1730
1731static void
1732dwarf2_debug_line_missing_file_complaint (void)
1733{
1734 complaint (&symfile_complaints,
1735 _(".debug_line section has line data without a file"));
1736}
1737
1738static void
1739dwarf2_debug_line_missing_end_sequence_complaint (void)
1740{
1741 complaint (&symfile_complaints,
1742 _(".debug_line section has line "
1743 "program sequence without an end"));
1744}
1745
1746static void
1747dwarf2_complex_location_expr_complaint (void)
1748{
1749 complaint (&symfile_complaints, _("location expression too complex"));
1750}
1751
1752static void
1753dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1754 int arg3)
1755{
1756 complaint (&symfile_complaints,
1757 _("const value length mismatch for '%s', got %d, expected %d"),
1758 arg1, arg2, arg3);
1759}
1760
1761static void
1762dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1763{
1764 complaint (&symfile_complaints,
1765 _("debug info runs off end of %s section"
1766 " [in module %s]"),
1767 section->asection->name,
1768 bfd_get_filename (section->asection->owner));
1769}
1b80a9fa 1770
4390d890
DE
1771static void
1772dwarf2_macro_malformed_definition_complaint (const char *arg1)
1773{
1774 complaint (&symfile_complaints,
1775 _("macro debug info contains a "
1776 "malformed macro definition:\n`%s'"),
1777 arg1);
1778}
1779
1780static void
1781dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1782{
1783 complaint (&symfile_complaints,
1784 _("invalid attribute class or form for '%s' in '%s'"),
1785 arg1, arg2);
1786}
1787\f
9291a0cd
TT
1788#if WORDS_BIGENDIAN
1789
1790/* Convert VALUE between big- and little-endian. */
1791static offset_type
1792byte_swap (offset_type value)
1793{
1794 offset_type result;
1795
1796 result = (value & 0xff) << 24;
1797 result |= (value & 0xff00) << 8;
1798 result |= (value & 0xff0000) >> 8;
1799 result |= (value & 0xff000000) >> 24;
1800 return result;
1801}
1802
1803#define MAYBE_SWAP(V) byte_swap (V)
1804
1805#else
1806#define MAYBE_SWAP(V) (V)
1807#endif /* WORDS_BIGENDIAN */
1808
1809/* The suffix for an index file. */
1810#define INDEX_SUFFIX ".gdb-index"
1811
c906108c 1812/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1813 information and return true if we have enough to do something.
1814 NAMES points to the dwarf2 section names, or is NULL if the standard
1815 ELF names are used. */
c906108c
SS
1816
1817int
251d32d9
TG
1818dwarf2_has_info (struct objfile *objfile,
1819 const struct dwarf2_debug_sections *names)
c906108c 1820{
be391dca
TT
1821 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1822 if (!dwarf2_per_objfile)
1823 {
1824 /* Initialize per-objfile state. */
1825 struct dwarf2_per_objfile *data
1826 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1827
be391dca
TT
1828 memset (data, 0, sizeof (*data));
1829 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1830 dwarf2_per_objfile = data;
6502dd73 1831
251d32d9
TG
1832 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1833 (void *) names);
be391dca
TT
1834 dwarf2_per_objfile->objfile = objfile;
1835 }
1836 return (dwarf2_per_objfile->info.asection != NULL
1837 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1838}
1839
251d32d9
TG
1840/* When loading sections, we look either for uncompressed section or for
1841 compressed section names. */
233a11ab
CS
1842
1843static int
251d32d9
TG
1844section_is_p (const char *section_name,
1845 const struct dwarf2_section_names *names)
233a11ab 1846{
251d32d9
TG
1847 if (names->normal != NULL
1848 && strcmp (section_name, names->normal) == 0)
1849 return 1;
1850 if (names->compressed != NULL
1851 && strcmp (section_name, names->compressed) == 0)
1852 return 1;
1853 return 0;
233a11ab
CS
1854}
1855
c906108c
SS
1856/* This function is mapped across the sections and remembers the
1857 offset and size of each of the debugging sections we are interested
1858 in. */
1859
1860static void
251d32d9 1861dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1862{
251d32d9 1863 const struct dwarf2_debug_sections *names;
dc7650b8 1864 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9
TG
1865
1866 if (vnames == NULL)
1867 names = &dwarf2_elf_names;
1868 else
1869 names = (const struct dwarf2_debug_sections *) vnames;
1870
dc7650b8
JK
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
1874 else if (section_is_p (sectp->name, &names->info))
c906108c 1875 {
dce234bc
PP
1876 dwarf2_per_objfile->info.asection = sectp;
1877 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1878 }
251d32d9 1879 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1880 {
dce234bc
PP
1881 dwarf2_per_objfile->abbrev.asection = sectp;
1882 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1883 }
251d32d9 1884 else if (section_is_p (sectp->name, &names->line))
c906108c 1885 {
dce234bc
PP
1886 dwarf2_per_objfile->line.asection = sectp;
1887 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1888 }
251d32d9 1889 else if (section_is_p (sectp->name, &names->loc))
c906108c 1890 {
dce234bc
PP
1891 dwarf2_per_objfile->loc.asection = sectp;
1892 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1893 }
251d32d9 1894 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1895 {
dce234bc
PP
1896 dwarf2_per_objfile->macinfo.asection = sectp;
1897 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1898 }
cf2c3c16
TT
1899 else if (section_is_p (sectp->name, &names->macro))
1900 {
1901 dwarf2_per_objfile->macro.asection = sectp;
1902 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1903 }
251d32d9 1904 else if (section_is_p (sectp->name, &names->str))
c906108c 1905 {
dce234bc
PP
1906 dwarf2_per_objfile->str.asection = sectp;
1907 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1908 }
3019eac3
DE
1909 else if (section_is_p (sectp->name, &names->addr))
1910 {
1911 dwarf2_per_objfile->addr.asection = sectp;
1912 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1913 }
251d32d9 1914 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1915 {
dce234bc
PP
1916 dwarf2_per_objfile->frame.asection = sectp;
1917 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1918 }
251d32d9 1919 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1920 {
dc7650b8
JK
1921 dwarf2_per_objfile->eh_frame.asection = sectp;
1922 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 1923 }
251d32d9 1924 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1925 {
dce234bc
PP
1926 dwarf2_per_objfile->ranges.asection = sectp;
1927 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1928 }
251d32d9 1929 else if (section_is_p (sectp->name, &names->types))
348e048f 1930 {
8b70b953
TT
1931 struct dwarf2_section_info type_section;
1932
1933 memset (&type_section, 0, sizeof (type_section));
1934 type_section.asection = sectp;
1935 type_section.size = bfd_get_section_size (sectp);
1936
1937 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1938 &type_section);
348e048f 1939 }
251d32d9 1940 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1941 {
1942 dwarf2_per_objfile->gdb_index.asection = sectp;
1943 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1944 }
dce234bc 1945
72dca2f5
FR
1946 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1947 && bfd_section_vma (abfd, sectp) == 0)
1948 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1949}
1950
fceca515
DE
1951/* A helper function that decides whether a section is empty,
1952 or not present. */
9e0ac564
TT
1953
1954static int
1955dwarf2_section_empty_p (struct dwarf2_section_info *info)
1956{
1957 return info->asection == NULL || info->size == 0;
1958}
1959
3019eac3
DE
1960/* Read the contents of the section INFO.
1961 OBJFILE is the main object file, but not necessarily the file where
1962 the section comes from. E.g., for DWO files INFO->asection->owner
1963 is the bfd of the DWO file.
dce234bc 1964 If the section is compressed, uncompress it before returning. */
c906108c 1965
dce234bc
PP
1966static void
1967dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1968{
dce234bc 1969 asection *sectp = info->asection;
3019eac3 1970 bfd *abfd;
dce234bc
PP
1971 gdb_byte *buf, *retbuf;
1972 unsigned char header[4];
c906108c 1973
be391dca
TT
1974 if (info->readin)
1975 return;
dce234bc 1976 info->buffer = NULL;
be391dca 1977 info->readin = 1;
188dd5d6 1978
9e0ac564 1979 if (dwarf2_section_empty_p (info))
dce234bc 1980 return;
c906108c 1981
3019eac3
DE
1982 abfd = sectp->owner;
1983
4bf44c1c
TT
1984 /* If the section has relocations, we must read it ourselves.
1985 Otherwise we attach it to the BFD. */
1986 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 1987 {
d521ce57 1988 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 1989 return;
dce234bc 1990 }
dce234bc 1991
4bf44c1c
TT
1992 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1993 info->buffer = buf;
dce234bc
PP
1994
1995 /* When debugging .o files, we may need to apply relocations; see
1996 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1997 We never compress sections in .o files, so we only need to
1998 try this when the section is not compressed. */
ac8035ab 1999 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2000 if (retbuf != NULL)
2001 {
2002 info->buffer = retbuf;
2003 return;
2004 }
2005
2006 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2007 || bfd_bread (buf, info->size, abfd) != info->size)
2008 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2009 bfd_get_filename (abfd));
2010}
2011
9e0ac564
TT
2012/* A helper function that returns the size of a section in a safe way.
2013 If you are positive that the section has been read before using the
2014 size, then it is safe to refer to the dwarf2_section_info object's
2015 "size" field directly. In other cases, you must call this
2016 function, because for compressed sections the size field is not set
2017 correctly until the section has been read. */
2018
2019static bfd_size_type
2020dwarf2_section_size (struct objfile *objfile,
2021 struct dwarf2_section_info *info)
2022{
2023 if (!info->readin)
2024 dwarf2_read_section (objfile, info);
2025 return info->size;
2026}
2027
dce234bc 2028/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2029 SECTION_NAME. */
af34e669 2030
dce234bc 2031void
3017a003
TG
2032dwarf2_get_section_info (struct objfile *objfile,
2033 enum dwarf2_section_enum sect,
d521ce57 2034 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2035 bfd_size_type *sizep)
2036{
2037 struct dwarf2_per_objfile *data
2038 = objfile_data (objfile, dwarf2_objfile_data_key);
2039 struct dwarf2_section_info *info;
a3b2a86b
TT
2040
2041 /* We may see an objfile without any DWARF, in which case we just
2042 return nothing. */
2043 if (data == NULL)
2044 {
2045 *sectp = NULL;
2046 *bufp = NULL;
2047 *sizep = 0;
2048 return;
2049 }
3017a003
TG
2050 switch (sect)
2051 {
2052 case DWARF2_DEBUG_FRAME:
2053 info = &data->frame;
2054 break;
2055 case DWARF2_EH_FRAME:
2056 info = &data->eh_frame;
2057 break;
2058 default:
2059 gdb_assert_not_reached ("unexpected section");
2060 }
dce234bc 2061
9e0ac564 2062 dwarf2_read_section (objfile, info);
dce234bc
PP
2063
2064 *sectp = info->asection;
2065 *bufp = info->buffer;
2066 *sizep = info->size;
2067}
2068
36586728
TT
2069/* A helper function to find the sections for a .dwz file. */
2070
2071static void
2072locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2073{
2074 struct dwz_file *dwz_file = arg;
2075
2076 /* Note that we only support the standard ELF names, because .dwz
2077 is ELF-only (at the time of writing). */
2078 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2079 {
2080 dwz_file->abbrev.asection = sectp;
2081 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2082 }
2083 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2084 {
2085 dwz_file->info.asection = sectp;
2086 dwz_file->info.size = bfd_get_section_size (sectp);
2087 }
2088 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2089 {
2090 dwz_file->str.asection = sectp;
2091 dwz_file->str.size = bfd_get_section_size (sectp);
2092 }
2093 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2094 {
2095 dwz_file->line.asection = sectp;
2096 dwz_file->line.size = bfd_get_section_size (sectp);
2097 }
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2099 {
2100 dwz_file->macro.asection = sectp;
2101 dwz_file->macro.size = bfd_get_section_size (sectp);
2102 }
2ec9a5e0
TT
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2104 {
2105 dwz_file->gdb_index.asection = sectp;
2106 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2107 }
36586728
TT
2108}
2109
4db1a1dc
TT
2110/* Open the separate '.dwz' debug file, if needed. Return NULL if
2111 there is no .gnu_debugaltlink section in the file. Error if there
2112 is such a section but the file cannot be found. */
36586728
TT
2113
2114static struct dwz_file *
2115dwarf2_get_dwz_file (void)
2116{
4db1a1dc
TT
2117 bfd *dwz_bfd;
2118 char *data;
36586728
TT
2119 struct cleanup *cleanup;
2120 const char *filename;
2121 struct dwz_file *result;
4db1a1dc 2122 unsigned long buildid;
36586728
TT
2123
2124 if (dwarf2_per_objfile->dwz_file != NULL)
2125 return dwarf2_per_objfile->dwz_file;
2126
4db1a1dc
TT
2127 bfd_set_error (bfd_error_no_error);
2128 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2129 &buildid);
2130 if (data == NULL)
2131 {
2132 if (bfd_get_error () == bfd_error_no_error)
2133 return NULL;
2134 error (_("could not read '.gnu_debugaltlink' section: %s"),
2135 bfd_errmsg (bfd_get_error ()));
2136 }
36586728
TT
2137 cleanup = make_cleanup (xfree, data);
2138
f9d83a0b 2139 filename = (const char *) data;
36586728
TT
2140 if (!IS_ABSOLUTE_PATH (filename))
2141 {
2142 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2143 char *rel;
2144
2145 make_cleanup (xfree, abs);
2146 abs = ldirname (abs);
2147 make_cleanup (xfree, abs);
2148
2149 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2150 make_cleanup (xfree, rel);
2151 filename = rel;
2152 }
2153
2154 /* The format is just a NUL-terminated file name, followed by the
2155 build-id. For now, though, we ignore the build-id. */
2156 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2157 if (dwz_bfd == NULL)
2158 error (_("could not read '%s': %s"), filename,
2159 bfd_errmsg (bfd_get_error ()));
2160
2161 if (!bfd_check_format (dwz_bfd, bfd_object))
2162 {
2163 gdb_bfd_unref (dwz_bfd);
2164 error (_("file '%s' was not usable: %s"), filename,
2165 bfd_errmsg (bfd_get_error ()));
2166 }
2167
2168 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2169 struct dwz_file);
2170 result->dwz_bfd = dwz_bfd;
2171
2172 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2173
2174 do_cleanups (cleanup);
2175
8d2cc612 2176 dwarf2_per_objfile->dwz_file = result;
36586728
TT
2177 return result;
2178}
9291a0cd 2179\f
7b9f3c50
DE
2180/* DWARF quick_symbols_functions support. */
2181
2182/* TUs can share .debug_line entries, and there can be a lot more TUs than
2183 unique line tables, so we maintain a separate table of all .debug_line
2184 derived entries to support the sharing.
2185 All the quick functions need is the list of file names. We discard the
2186 line_header when we're done and don't need to record it here. */
2187struct quick_file_names
2188{
094b34ac
DE
2189 /* The data used to construct the hash key. */
2190 struct stmt_list_hash hash;
7b9f3c50
DE
2191
2192 /* The number of entries in file_names, real_names. */
2193 unsigned int num_file_names;
2194
2195 /* The file names from the line table, after being run through
2196 file_full_name. */
2197 const char **file_names;
2198
2199 /* The file names from the line table after being run through
2200 gdb_realpath. These are computed lazily. */
2201 const char **real_names;
2202};
2203
2204/* When using the index (and thus not using psymtabs), each CU has an
2205 object of this type. This is used to hold information needed by
2206 the various "quick" methods. */
2207struct dwarf2_per_cu_quick_data
2208{
2209 /* The file table. This can be NULL if there was no file table
2210 or it's currently not read in.
2211 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2212 struct quick_file_names *file_names;
2213
2214 /* The corresponding symbol table. This is NULL if symbols for this
2215 CU have not yet been read. */
2216 struct symtab *symtab;
2217
2218 /* A temporary mark bit used when iterating over all CUs in
2219 expand_symtabs_matching. */
2220 unsigned int mark : 1;
2221
2222 /* True if we've tried to read the file table and found there isn't one.
2223 There will be no point in trying to read it again next time. */
2224 unsigned int no_file_data : 1;
2225};
2226
094b34ac
DE
2227/* Utility hash function for a stmt_list_hash. */
2228
2229static hashval_t
2230hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2231{
2232 hashval_t v = 0;
2233
2234 if (stmt_list_hash->dwo_unit != NULL)
2235 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2236 v += stmt_list_hash->line_offset.sect_off;
2237 return v;
2238}
2239
2240/* Utility equality function for a stmt_list_hash. */
2241
2242static int
2243eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2244 const struct stmt_list_hash *rhs)
2245{
2246 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2247 return 0;
2248 if (lhs->dwo_unit != NULL
2249 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2250 return 0;
2251
2252 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2253}
2254
7b9f3c50
DE
2255/* Hash function for a quick_file_names. */
2256
2257static hashval_t
2258hash_file_name_entry (const void *e)
2259{
2260 const struct quick_file_names *file_data = e;
2261
094b34ac 2262 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2263}
2264
2265/* Equality function for a quick_file_names. */
2266
2267static int
2268eq_file_name_entry (const void *a, const void *b)
2269{
2270 const struct quick_file_names *ea = a;
2271 const struct quick_file_names *eb = b;
2272
094b34ac 2273 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2274}
2275
2276/* Delete function for a quick_file_names. */
2277
2278static void
2279delete_file_name_entry (void *e)
2280{
2281 struct quick_file_names *file_data = e;
2282 int i;
2283
2284 for (i = 0; i < file_data->num_file_names; ++i)
2285 {
2286 xfree ((void*) file_data->file_names[i]);
2287 if (file_data->real_names)
2288 xfree ((void*) file_data->real_names[i]);
2289 }
2290
2291 /* The space for the struct itself lives on objfile_obstack,
2292 so we don't free it here. */
2293}
2294
2295/* Create a quick_file_names hash table. */
2296
2297static htab_t
2298create_quick_file_names_table (unsigned int nr_initial_entries)
2299{
2300 return htab_create_alloc (nr_initial_entries,
2301 hash_file_name_entry, eq_file_name_entry,
2302 delete_file_name_entry, xcalloc, xfree);
2303}
9291a0cd 2304
918dd910
JK
2305/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2306 have to be created afterwards. You should call age_cached_comp_units after
2307 processing PER_CU->CU. dw2_setup must have been already called. */
2308
2309static void
2310load_cu (struct dwarf2_per_cu_data *per_cu)
2311{
3019eac3 2312 if (per_cu->is_debug_types)
e5fe5e75 2313 load_full_type_unit (per_cu);
918dd910 2314 else
95554aad 2315 load_full_comp_unit (per_cu, language_minimal);
918dd910 2316
918dd910 2317 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2318
2319 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2320}
2321
a0f42c21 2322/* Read in the symbols for PER_CU. */
2fdf6df6 2323
9291a0cd 2324static void
a0f42c21 2325dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2326{
2327 struct cleanup *back_to;
2328
f4dc4d17
DE
2329 /* Skip type_unit_groups, reading the type units they contain
2330 is handled elsewhere. */
2331 if (IS_TYPE_UNIT_GROUP (per_cu))
2332 return;
2333
9291a0cd
TT
2334 back_to = make_cleanup (dwarf2_release_queue, NULL);
2335
95554aad
TT
2336 if (dwarf2_per_objfile->using_index
2337 ? per_cu->v.quick->symtab == NULL
2338 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2339 {
2340 queue_comp_unit (per_cu, language_minimal);
2341 load_cu (per_cu);
89e63ee4
DE
2342
2343 /* If we just loaded a CU from a DWO, and we're working with an index
2344 that may badly handle TUs, load all the TUs in that DWO as well.
2345 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2346 if (!per_cu->is_debug_types
2347 && per_cu->cu->dwo_unit != NULL
2348 && dwarf2_per_objfile->index_table != NULL
2349 && dwarf2_per_objfile->index_table->version <= 7
2350 /* DWP files aren't supported yet. */
2351 && get_dwp_file () == NULL)
2352 queue_and_load_all_dwo_tus (per_cu);
95554aad 2353 }
9291a0cd 2354
a0f42c21 2355 process_queue ();
9291a0cd
TT
2356
2357 /* Age the cache, releasing compilation units that have not
2358 been used recently. */
2359 age_cached_comp_units ();
2360
2361 do_cleanups (back_to);
2362}
2363
2364/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2365 the objfile from which this CU came. Returns the resulting symbol
2366 table. */
2fdf6df6 2367
9291a0cd 2368static struct symtab *
a0f42c21 2369dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2370{
95554aad 2371 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2372 if (!per_cu->v.quick->symtab)
2373 {
2374 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2375 increment_reading_symtab ();
a0f42c21 2376 dw2_do_instantiate_symtab (per_cu);
95554aad 2377 process_cu_includes ();
9291a0cd
TT
2378 do_cleanups (back_to);
2379 }
2380 return per_cu->v.quick->symtab;
2381}
2382
f4dc4d17
DE
2383/* Return the CU given its index.
2384
2385 This is intended for loops like:
2386
2387 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2388 + dwarf2_per_objfile->n_type_units); ++i)
2389 {
2390 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2391
2392 ...;
2393 }
2394*/
2fdf6df6 2395
1fd400ff
TT
2396static struct dwarf2_per_cu_data *
2397dw2_get_cu (int index)
2398{
2399 if (index >= dwarf2_per_objfile->n_comp_units)
2400 {
f4dc4d17 2401 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2402 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2403 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
f4dc4d17
DE
2404 }
2405
2406 return dwarf2_per_objfile->all_comp_units[index];
2407}
2408
2409/* Return the primary CU given its index.
2410 The difference between this function and dw2_get_cu is in the handling
2411 of type units (TUs). Here we return the type_unit_group object.
2412
2413 This is intended for loops like:
2414
2415 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2416 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2417 {
2418 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2419
2420 ...;
2421 }
2422*/
2423
2424static struct dwarf2_per_cu_data *
2425dw2_get_primary_cu (int index)
2426{
2427 if (index >= dwarf2_per_objfile->n_comp_units)
2428 {
1fd400ff 2429 index -= dwarf2_per_objfile->n_comp_units;
094b34ac
DE
2430 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2431 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
1fd400ff 2432 }
f4dc4d17 2433
1fd400ff
TT
2434 return dwarf2_per_objfile->all_comp_units[index];
2435}
2436
2ec9a5e0
TT
2437/* A helper for create_cus_from_index that handles a given list of
2438 CUs. */
2fdf6df6 2439
74a0d9f6 2440static void
2ec9a5e0
TT
2441create_cus_from_index_list (struct objfile *objfile,
2442 const gdb_byte *cu_list, offset_type n_elements,
2443 struct dwarf2_section_info *section,
2444 int is_dwz,
2445 int base_offset)
9291a0cd
TT
2446{
2447 offset_type i;
9291a0cd 2448
2ec9a5e0 2449 for (i = 0; i < n_elements; i += 2)
9291a0cd
TT
2450 {
2451 struct dwarf2_per_cu_data *the_cu;
2452 ULONGEST offset, length;
2453
74a0d9f6
JK
2454 gdb_static_assert (sizeof (ULONGEST) >= 8);
2455 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2456 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2457 cu_list += 2 * 8;
2458
2459 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2460 struct dwarf2_per_cu_data);
b64f50a1 2461 the_cu->offset.sect_off = offset;
9291a0cd
TT
2462 the_cu->length = length;
2463 the_cu->objfile = objfile;
8a0459fd 2464 the_cu->section = section;
9291a0cd
TT
2465 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2466 struct dwarf2_per_cu_quick_data);
2ec9a5e0
TT
2467 the_cu->is_dwz = is_dwz;
2468 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
9291a0cd 2469 }
9291a0cd
TT
2470}
2471
2ec9a5e0 2472/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2473 the CU objects for this objfile. */
2ec9a5e0 2474
74a0d9f6 2475static void
2ec9a5e0
TT
2476create_cus_from_index (struct objfile *objfile,
2477 const gdb_byte *cu_list, offset_type cu_list_elements,
2478 const gdb_byte *dwz_list, offset_type dwz_elements)
2479{
2480 struct dwz_file *dwz;
2481
2482 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2483 dwarf2_per_objfile->all_comp_units
2484 = obstack_alloc (&objfile->objfile_obstack,
2485 dwarf2_per_objfile->n_comp_units
2486 * sizeof (struct dwarf2_per_cu_data *));
2487
74a0d9f6
JK
2488 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2489 &dwarf2_per_objfile->info, 0, 0);
2ec9a5e0
TT
2490
2491 if (dwz_elements == 0)
74a0d9f6 2492 return;
2ec9a5e0
TT
2493
2494 dwz = dwarf2_get_dwz_file ();
74a0d9f6
JK
2495 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2496 cu_list_elements / 2);
2ec9a5e0
TT
2497}
2498
1fd400ff 2499/* Create the signatured type hash table from the index. */
673bfd45 2500
74a0d9f6 2501static void
673bfd45 2502create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2503 struct dwarf2_section_info *section,
673bfd45
DE
2504 const gdb_byte *bytes,
2505 offset_type elements)
1fd400ff
TT
2506{
2507 offset_type i;
673bfd45 2508 htab_t sig_types_hash;
1fd400ff 2509
d467dd73
DE
2510 dwarf2_per_objfile->n_type_units = elements / 3;
2511 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
2512 = xmalloc (dwarf2_per_objfile->n_type_units
2513 * sizeof (struct signatured_type *));
1fd400ff 2514
673bfd45 2515 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2516
2517 for (i = 0; i < elements; i += 3)
2518 {
52dc124a
DE
2519 struct signatured_type *sig_type;
2520 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2521 void **slot;
2522
74a0d9f6
JK
2523 gdb_static_assert (sizeof (ULONGEST) >= 8);
2524 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2525 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2526 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2527 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2528 bytes += 3 * 8;
2529
52dc124a 2530 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2531 struct signatured_type);
52dc124a 2532 sig_type->signature = signature;
3019eac3
DE
2533 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2534 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2535 sig_type->per_cu.section = section;
52dc124a
DE
2536 sig_type->per_cu.offset.sect_off = offset;
2537 sig_type->per_cu.objfile = objfile;
2538 sig_type->per_cu.v.quick
1fd400ff
TT
2539 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2540 struct dwarf2_per_cu_quick_data);
2541
52dc124a
DE
2542 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2543 *slot = sig_type;
1fd400ff 2544
b4dd5633 2545 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
1fd400ff
TT
2546 }
2547
673bfd45 2548 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2549}
2550
9291a0cd
TT
2551/* Read the address map data from the mapped index, and use it to
2552 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2553
9291a0cd
TT
2554static void
2555create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2556{
2557 const gdb_byte *iter, *end;
2558 struct obstack temp_obstack;
2559 struct addrmap *mutable_map;
2560 struct cleanup *cleanup;
2561 CORE_ADDR baseaddr;
2562
2563 obstack_init (&temp_obstack);
2564 cleanup = make_cleanup_obstack_free (&temp_obstack);
2565 mutable_map = addrmap_create_mutable (&temp_obstack);
2566
2567 iter = index->address_table;
2568 end = iter + index->address_table_size;
2569
2570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2571
2572 while (iter < end)
2573 {
2574 ULONGEST hi, lo, cu_index;
2575 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2576 iter += 8;
2577 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2578 iter += 8;
2579 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2580 iter += 4;
f652bce2 2581
24a55014 2582 if (lo > hi)
f652bce2 2583 {
24a55014
DE
2584 complaint (&symfile_complaints,
2585 _(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2586 hex_string (lo), hex_string (hi));
24a55014 2587 continue;
f652bce2 2588 }
24a55014
DE
2589
2590 if (cu_index >= dwarf2_per_objfile->n_comp_units)
f652bce2
DE
2591 {
2592 complaint (&symfile_complaints,
2593 _(".gdb_index address table has invalid CU number %u"),
2594 (unsigned) cu_index);
24a55014 2595 continue;
f652bce2 2596 }
24a55014
DE
2597
2598 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2599 dw2_get_cu (cu_index));
9291a0cd
TT
2600 }
2601
2602 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2603 &objfile->objfile_obstack);
2604 do_cleanups (cleanup);
2605}
2606
59d7bcaf
JK
2607/* The hash function for strings in the mapped index. This is the same as
2608 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2609 implementation. This is necessary because the hash function is tied to the
2610 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2611 SYMBOL_HASH_NEXT.
2612
2613 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2614
9291a0cd 2615static hashval_t
559a7a62 2616mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2617{
2618 const unsigned char *str = (const unsigned char *) p;
2619 hashval_t r = 0;
2620 unsigned char c;
2621
2622 while ((c = *str++) != 0)
559a7a62
JK
2623 {
2624 if (index_version >= 5)
2625 c = tolower (c);
2626 r = r * 67 + c - 113;
2627 }
9291a0cd
TT
2628
2629 return r;
2630}
2631
2632/* Find a slot in the mapped index INDEX for the object named NAME.
2633 If NAME is found, set *VEC_OUT to point to the CU vector in the
2634 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2635
9291a0cd
TT
2636static int
2637find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2638 offset_type **vec_out)
2639{
0cf03b49
JK
2640 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2641 offset_type hash;
9291a0cd 2642 offset_type slot, step;
559a7a62 2643 int (*cmp) (const char *, const char *);
9291a0cd 2644
0cf03b49
JK
2645 if (current_language->la_language == language_cplus
2646 || current_language->la_language == language_java
2647 || current_language->la_language == language_fortran)
2648 {
2649 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2650 not contain any. */
2651 const char *paren = strchr (name, '(');
2652
2653 if (paren)
2654 {
2655 char *dup;
2656
2657 dup = xmalloc (paren - name + 1);
2658 memcpy (dup, name, paren - name);
2659 dup[paren - name] = 0;
2660
2661 make_cleanup (xfree, dup);
2662 name = dup;
2663 }
2664 }
2665
559a7a62 2666 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2667 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2668 simulate our NAME being searched is also lowercased. */
2669 hash = mapped_index_string_hash ((index->version == 4
2670 && case_sensitivity == case_sensitive_off
2671 ? 5 : index->version),
2672 name);
2673
3876f04e
DE
2674 slot = hash & (index->symbol_table_slots - 1);
2675 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2676 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2677
2678 for (;;)
2679 {
2680 /* Convert a slot number to an offset into the table. */
2681 offset_type i = 2 * slot;
2682 const char *str;
3876f04e 2683 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2684 {
2685 do_cleanups (back_to);
2686 return 0;
2687 }
9291a0cd 2688
3876f04e 2689 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2690 if (!cmp (name, str))
9291a0cd
TT
2691 {
2692 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2693 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2694 do_cleanups (back_to);
9291a0cd
TT
2695 return 1;
2696 }
2697
3876f04e 2698 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2699 }
2700}
2701
2ec9a5e0
TT
2702/* A helper function that reads the .gdb_index from SECTION and fills
2703 in MAP. FILENAME is the name of the file containing the section;
2704 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2705 ok to use deprecated sections.
2706
2707 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2708 out parameters that are filled in with information about the CU and
2709 TU lists in the section.
2710
2711 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 2712
9291a0cd 2713static int
2ec9a5e0
TT
2714read_index_from_section (struct objfile *objfile,
2715 const char *filename,
2716 int deprecated_ok,
2717 struct dwarf2_section_info *section,
2718 struct mapped_index *map,
2719 const gdb_byte **cu_list,
2720 offset_type *cu_list_elements,
2721 const gdb_byte **types_list,
2722 offset_type *types_list_elements)
9291a0cd 2723{
948f8e3d 2724 const gdb_byte *addr;
2ec9a5e0 2725 offset_type version;
b3b272e1 2726 offset_type *metadata;
1fd400ff 2727 int i;
9291a0cd 2728
2ec9a5e0 2729 if (dwarf2_section_empty_p (section))
9291a0cd 2730 return 0;
82430852
JK
2731
2732 /* Older elfutils strip versions could keep the section in the main
2733 executable while splitting it for the separate debug info file. */
2ec9a5e0 2734 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
82430852
JK
2735 return 0;
2736
2ec9a5e0 2737 dwarf2_read_section (objfile, section);
9291a0cd 2738
2ec9a5e0 2739 addr = section->buffer;
9291a0cd 2740 /* Version check. */
1fd400ff 2741 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2742 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2743 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2744 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2745 indices. */
831adc1f 2746 if (version < 4)
481860b3
GB
2747 {
2748 static int warning_printed = 0;
2749 if (!warning_printed)
2750 {
2751 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2752 filename);
481860b3
GB
2753 warning_printed = 1;
2754 }
2755 return 0;
2756 }
2757 /* Index version 4 uses a different hash function than index version
2758 5 and later.
2759
2760 Versions earlier than 6 did not emit psymbols for inlined
2761 functions. Using these files will cause GDB not to be able to
2762 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2763 indices unless the user has done
2764 "set use-deprecated-index-sections on". */
2ec9a5e0 2765 if (version < 6 && !deprecated_ok)
481860b3
GB
2766 {
2767 static int warning_printed = 0;
2768 if (!warning_printed)
2769 {
e615022a
DE
2770 warning (_("\
2771Skipping deprecated .gdb_index section in %s.\n\
2772Do \"set use-deprecated-index-sections on\" before the file is read\n\
2773to use the section anyway."),
2ec9a5e0 2774 filename);
481860b3
GB
2775 warning_printed = 1;
2776 }
2777 return 0;
2778 }
796a7ff8
DE
2779 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2780 of the TU (for symbols coming from TUs). It's just a performance bug, and
2781 we can't distinguish gdb-generated indices from gold-generated ones, so
2782 nothing to do here. */
2783
481860b3 2784 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2785 longer backward compatible. */
796a7ff8 2786 if (version > 8)
594e8718 2787 return 0;
9291a0cd 2788
559a7a62 2789 map->version = version;
2ec9a5e0 2790 map->total_size = section->size;
9291a0cd
TT
2791
2792 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2793
2794 i = 0;
2ec9a5e0
TT
2795 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2796 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2797 / 8);
1fd400ff
TT
2798 ++i;
2799
2ec9a5e0
TT
2800 *types_list = addr + MAYBE_SWAP (metadata[i]);
2801 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2802 - MAYBE_SWAP (metadata[i]))
2803 / 8);
987d643c 2804 ++i;
1fd400ff
TT
2805
2806 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2807 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2808 - MAYBE_SWAP (metadata[i]));
2809 ++i;
2810
3876f04e
DE
2811 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2812 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2813 - MAYBE_SWAP (metadata[i]))
2814 / (2 * sizeof (offset_type)));
1fd400ff 2815 ++i;
9291a0cd 2816
f9d83a0b 2817 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2818
2ec9a5e0
TT
2819 return 1;
2820}
2821
2822
2823/* Read the index file. If everything went ok, initialize the "quick"
2824 elements of all the CUs and return 1. Otherwise, return 0. */
2825
2826static int
2827dwarf2_read_index (struct objfile *objfile)
2828{
2829 struct mapped_index local_map, *map;
2830 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2831 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2832 struct dwz_file *dwz;
2ec9a5e0
TT
2833
2834 if (!read_index_from_section (objfile, objfile->name,
2835 use_deprecated_index_sections,
2836 &dwarf2_per_objfile->gdb_index, &local_map,
2837 &cu_list, &cu_list_elements,
2838 &types_list, &types_list_elements))
2839 return 0;
2840
0fefef59 2841 /* Don't use the index if it's empty. */
2ec9a5e0 2842 if (local_map.symbol_table_slots == 0)
0fefef59
DE
2843 return 0;
2844
2ec9a5e0
TT
2845 /* If there is a .dwz file, read it so we can get its CU list as
2846 well. */
4db1a1dc
TT
2847 dwz = dwarf2_get_dwz_file ();
2848 if (dwz != NULL)
2ec9a5e0 2849 {
2ec9a5e0
TT
2850 struct mapped_index dwz_map;
2851 const gdb_byte *dwz_types_ignore;
2852 offset_type dwz_types_elements_ignore;
2853
2854 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2855 1,
2856 &dwz->gdb_index, &dwz_map,
2857 &dwz_list, &dwz_list_elements,
2858 &dwz_types_ignore,
2859 &dwz_types_elements_ignore))
2860 {
2861 warning (_("could not read '.gdb_index' section from %s; skipping"),
2862 bfd_get_filename (dwz->dwz_bfd));
2863 return 0;
2864 }
2865 }
2866
74a0d9f6
JK
2867 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2868 dwz_list_elements);
1fd400ff 2869
8b70b953
TT
2870 if (types_list_elements)
2871 {
2872 struct dwarf2_section_info *section;
2873
2874 /* We can only handle a single .debug_types when we have an
2875 index. */
2876 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2877 return 0;
2878
2879 section = VEC_index (dwarf2_section_info_def,
2880 dwarf2_per_objfile->types, 0);
2881
74a0d9f6
JK
2882 create_signatured_type_table_from_index (objfile, section, types_list,
2883 types_list_elements);
8b70b953 2884 }
9291a0cd 2885
2ec9a5e0
TT
2886 create_addrmap_from_index (objfile, &local_map);
2887
2888 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2889 *map = local_map;
9291a0cd
TT
2890
2891 dwarf2_per_objfile->index_table = map;
2892 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2893 dwarf2_per_objfile->quick_file_names_table =
2894 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2895
2896 return 1;
2897}
2898
2899/* A helper for the "quick" functions which sets the global
2900 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2901
9291a0cd
TT
2902static void
2903dw2_setup (struct objfile *objfile)
2904{
2905 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2906 gdb_assert (dwarf2_per_objfile);
2907}
2908
dee91e82 2909/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2910
dee91e82
DE
2911static void
2912dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 2913 const gdb_byte *info_ptr,
dee91e82
DE
2914 struct die_info *comp_unit_die,
2915 int has_children,
2916 void *data)
9291a0cd 2917{
dee91e82
DE
2918 struct dwarf2_cu *cu = reader->cu;
2919 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2920 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 2921 struct dwarf2_per_cu_data *lh_cu;
7b9f3c50 2922 struct line_header *lh;
9291a0cd 2923 struct attribute *attr;
dee91e82 2924 int i;
15d034d0 2925 const char *name, *comp_dir;
7b9f3c50
DE
2926 void **slot;
2927 struct quick_file_names *qfn;
2928 unsigned int line_offset;
9291a0cd 2929
0186c6a7
DE
2930 gdb_assert (! this_cu->is_debug_types);
2931
07261596
TT
2932 /* Our callers never want to match partial units -- instead they
2933 will match the enclosing full CU. */
2934 if (comp_unit_die->tag == DW_TAG_partial_unit)
2935 {
2936 this_cu->v.quick->no_file_data = 1;
2937 return;
2938 }
2939
0186c6a7 2940 lh_cu = this_cu;
7b9f3c50
DE
2941 lh = NULL;
2942 slot = NULL;
2943 line_offset = 0;
dee91e82
DE
2944
2945 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2946 if (attr)
2947 {
7b9f3c50
DE
2948 struct quick_file_names find_entry;
2949
2950 line_offset = DW_UNSND (attr);
2951
2952 /* We may have already read in this line header (TU line header sharing).
2953 If we have we're done. */
094b34ac
DE
2954 find_entry.hash.dwo_unit = cu->dwo_unit;
2955 find_entry.hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2956 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2957 &find_entry, INSERT);
2958 if (*slot != NULL)
2959 {
094b34ac 2960 lh_cu->v.quick->file_names = *slot;
dee91e82 2961 return;
7b9f3c50
DE
2962 }
2963
3019eac3 2964 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2965 }
2966 if (lh == NULL)
2967 {
094b34ac 2968 lh_cu->v.quick->no_file_data = 1;
dee91e82 2969 return;
9291a0cd
TT
2970 }
2971
7b9f3c50 2972 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
094b34ac
DE
2973 qfn->hash.dwo_unit = cu->dwo_unit;
2974 qfn->hash.line_offset.sect_off = line_offset;
7b9f3c50
DE
2975 gdb_assert (slot != NULL);
2976 *slot = qfn;
9291a0cd 2977
dee91e82 2978 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2979
7b9f3c50
DE
2980 qfn->num_file_names = lh->num_file_names;
2981 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2982 lh->num_file_names * sizeof (char *));
9291a0cd 2983 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2984 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2985 qfn->real_names = NULL;
9291a0cd 2986
7b9f3c50 2987 free_line_header (lh);
7b9f3c50 2988
094b34ac 2989 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2990}
2991
2992/* A helper for the "quick" functions which attempts to read the line
2993 table for THIS_CU. */
2994
2995static struct quick_file_names *
e4a48d9d 2996dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 2997{
0186c6a7
DE
2998 /* This should never be called for TUs. */
2999 gdb_assert (! this_cu->is_debug_types);
3000 /* Nor type unit groups. */
3001 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3002
dee91e82
DE
3003 if (this_cu->v.quick->file_names != NULL)
3004 return this_cu->v.quick->file_names;
3005 /* If we know there is no line data, no point in looking again. */
3006 if (this_cu->v.quick->no_file_data)
3007 return NULL;
3008
0186c6a7 3009 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3010
3011 if (this_cu->v.quick->no_file_data)
3012 return NULL;
3013 return this_cu->v.quick->file_names;
9291a0cd
TT
3014}
3015
3016/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3017 real path for a given file name from the line table. */
2fdf6df6 3018
9291a0cd 3019static const char *
7b9f3c50
DE
3020dw2_get_real_path (struct objfile *objfile,
3021 struct quick_file_names *qfn, int index)
9291a0cd 3022{
7b9f3c50
DE
3023 if (qfn->real_names == NULL)
3024 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3025 qfn->num_file_names, sizeof (char *));
9291a0cd 3026
7b9f3c50
DE
3027 if (qfn->real_names[index] == NULL)
3028 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 3029
7b9f3c50 3030 return qfn->real_names[index];
9291a0cd
TT
3031}
3032
3033static struct symtab *
3034dw2_find_last_source_symtab (struct objfile *objfile)
3035{
3036 int index;
ae2de4f8 3037
9291a0cd
TT
3038 dw2_setup (objfile);
3039 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 3040 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
3041}
3042
7b9f3c50
DE
3043/* Traversal function for dw2_forget_cached_source_info. */
3044
3045static int
3046dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3047{
7b9f3c50 3048 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3049
7b9f3c50 3050 if (file_data->real_names)
9291a0cd 3051 {
7b9f3c50 3052 int i;
9291a0cd 3053
7b9f3c50 3054 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3055 {
7b9f3c50
DE
3056 xfree ((void*) file_data->real_names[i]);
3057 file_data->real_names[i] = NULL;
9291a0cd
TT
3058 }
3059 }
7b9f3c50
DE
3060
3061 return 1;
3062}
3063
3064static void
3065dw2_forget_cached_source_info (struct objfile *objfile)
3066{
3067 dw2_setup (objfile);
3068
3069 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3070 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3071}
3072
f8eba3c6
TT
3073/* Helper function for dw2_map_symtabs_matching_filename that expands
3074 the symtabs and calls the iterator. */
3075
3076static int
3077dw2_map_expand_apply (struct objfile *objfile,
3078 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3079 const char *name, const char *real_path,
f8eba3c6
TT
3080 int (*callback) (struct symtab *, void *),
3081 void *data)
3082{
3083 struct symtab *last_made = objfile->symtabs;
3084
3085 /* Don't visit already-expanded CUs. */
3086 if (per_cu->v.quick->symtab)
3087 return 0;
3088
3089 /* This may expand more than one symtab, and we want to iterate over
3090 all of them. */
a0f42c21 3091 dw2_instantiate_symtab (per_cu);
f8eba3c6 3092
f5b95b50 3093 return iterate_over_some_symtabs (name, real_path, callback, data,
f8eba3c6
TT
3094 objfile->symtabs, last_made);
3095}
3096
3097/* Implementation of the map_symtabs_matching_filename method. */
3098
9291a0cd 3099static int
f8eba3c6 3100dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
f5b95b50 3101 const char *real_path,
f8eba3c6
TT
3102 int (*callback) (struct symtab *, void *),
3103 void *data)
9291a0cd
TT
3104{
3105 int i;
c011a4f4 3106 const char *name_basename = lbasename (name);
9291a0cd
TT
3107
3108 dw2_setup (objfile);
ae2de4f8 3109
848e3e78
DE
3110 /* The rule is CUs specify all the files, including those used by
3111 any TU, so there's no need to scan TUs here. */
f4dc4d17 3112
848e3e78 3113 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3114 {
3115 int j;
f4dc4d17 3116 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3117 struct quick_file_names *file_data;
9291a0cd 3118
3d7bb9d9 3119 /* We only need to look at symtabs not already expanded. */
e254ef6a 3120 if (per_cu->v.quick->symtab)
9291a0cd
TT
3121 continue;
3122
e4a48d9d 3123 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3124 if (file_data == NULL)
9291a0cd
TT
3125 continue;
3126
7b9f3c50 3127 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3128 {
7b9f3c50 3129 const char *this_name = file_data->file_names[j];
da235a7c 3130 const char *this_real_name;
9291a0cd 3131
af529f8f 3132 if (compare_filenames_for_search (this_name, name))
9291a0cd 3133 {
f5b95b50 3134 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3135 callback, data))
3136 return 1;
288e77a7 3137 continue;
4aac40c8 3138 }
9291a0cd 3139
c011a4f4
DE
3140 /* Before we invoke realpath, which can get expensive when many
3141 files are involved, do a quick comparison of the basenames. */
3142 if (! basenames_may_differ
3143 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3144 continue;
3145
da235a7c
JK
3146 this_real_name = dw2_get_real_path (objfile, file_data, j);
3147 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3148 {
da235a7c
JK
3149 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3150 callback, data))
3151 return 1;
288e77a7 3152 continue;
da235a7c 3153 }
9291a0cd 3154
da235a7c
JK
3155 if (real_path != NULL)
3156 {
af529f8f
JK
3157 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3158 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3159 if (this_real_name != NULL
af529f8f 3160 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3161 {
f5b95b50 3162 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
f8eba3c6
TT
3163 callback, data))
3164 return 1;
288e77a7 3165 continue;
9291a0cd
TT
3166 }
3167 }
3168 }
3169 }
3170
9291a0cd
TT
3171 return 0;
3172}
3173
da51c347
DE
3174/* Struct used to manage iterating over all CUs looking for a symbol. */
3175
3176struct dw2_symtab_iterator
9291a0cd 3177{
da51c347
DE
3178 /* The internalized form of .gdb_index. */
3179 struct mapped_index *index;
3180 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3181 int want_specific_block;
3182 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3183 Unused if !WANT_SPECIFIC_BLOCK. */
3184 int block_index;
3185 /* The kind of symbol we're looking for. */
3186 domain_enum domain;
3187 /* The list of CUs from the index entry of the symbol,
3188 or NULL if not found. */
3189 offset_type *vec;
3190 /* The next element in VEC to look at. */
3191 int next;
3192 /* The number of elements in VEC, or zero if there is no match. */
3193 int length;
3194};
9291a0cd 3195
da51c347
DE
3196/* Initialize the index symtab iterator ITER.
3197 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3198 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3199
9291a0cd 3200static void
da51c347
DE
3201dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3202 struct mapped_index *index,
3203 int want_specific_block,
3204 int block_index,
3205 domain_enum domain,
3206 const char *name)
3207{
3208 iter->index = index;
3209 iter->want_specific_block = want_specific_block;
3210 iter->block_index = block_index;
3211 iter->domain = domain;
3212 iter->next = 0;
3213
3214 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3215 iter->length = MAYBE_SWAP (*iter->vec);
3216 else
3217 {
3218 iter->vec = NULL;
3219 iter->length = 0;
3220 }
3221}
3222
3223/* Return the next matching CU or NULL if there are no more. */
3224
3225static struct dwarf2_per_cu_data *
3226dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3227{
3228 for ( ; iter->next < iter->length; ++iter->next)
3229 {
3230 offset_type cu_index_and_attrs =
3231 MAYBE_SWAP (iter->vec[iter->next + 1]);
3232 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6 3233 struct dwarf2_per_cu_data *per_cu;
da51c347
DE
3234 int want_static = iter->block_index != GLOBAL_BLOCK;
3235 /* This value is only valid for index versions >= 7. */
3236 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3237 gdb_index_symbol_kind symbol_kind =
3238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3239 /* Only check the symbol attributes if they're present.
3240 Indices prior to version 7 don't record them,
3241 and indices >= 7 may elide them for certain symbols
3242 (gold does this). */
3243 int attrs_valid =
3244 (iter->index->version >= 7
3245 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3246
3190f0c6
DE
3247 /* Don't crash on bad data. */
3248 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3249 + dwarf2_per_objfile->n_type_units))
3250 {
3251 complaint (&symfile_complaints,
3252 _(".gdb_index entry has bad CU index"
3253 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3254 continue;
3255 }
3256
3257 per_cu = dw2_get_cu (cu_index);
3258
da51c347
DE
3259 /* Skip if already read in. */
3260 if (per_cu->v.quick->symtab)
3261 continue;
3262
3263 if (attrs_valid
3264 && iter->want_specific_block
3265 && want_static != is_static)
3266 continue;
3267
3268 /* Only check the symbol's kind if it has one. */
3269 if (attrs_valid)
3270 {
3271 switch (iter->domain)
3272 {
3273 case VAR_DOMAIN:
3274 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3275 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3276 /* Some types are also in VAR_DOMAIN. */
3277 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3278 continue;
3279 break;
3280 case STRUCT_DOMAIN:
3281 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3282 continue;
3283 break;
3284 case LABEL_DOMAIN:
3285 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3286 continue;
3287 break;
3288 default:
3289 break;
3290 }
3291 }
3292
3293 ++iter->next;
3294 return per_cu;
3295 }
3296
3297 return NULL;
3298}
3299
3300static struct symtab *
3301dw2_lookup_symbol (struct objfile *objfile, int block_index,
3302 const char *name, domain_enum domain)
9291a0cd 3303{
da51c347 3304 struct symtab *stab_best = NULL;
156942c7
DE
3305 struct mapped_index *index;
3306
9291a0cd
TT
3307 dw2_setup (objfile);
3308
156942c7
DE
3309 index = dwarf2_per_objfile->index_table;
3310
da51c347 3311 /* index is NULL if OBJF_READNOW. */
156942c7 3312 if (index)
9291a0cd 3313 {
da51c347
DE
3314 struct dw2_symtab_iterator iter;
3315 struct dwarf2_per_cu_data *per_cu;
3316
3317 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
9291a0cd 3318
da51c347 3319 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
9291a0cd 3320 {
da51c347
DE
3321 struct symbol *sym = NULL;
3322 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3323
3324 /* Some caution must be observed with overloaded functions
3325 and methods, since the index will not contain any overload
3326 information (but NAME might contain it). */
3327 if (stab->primary)
9291a0cd 3328 {
da51c347
DE
3329 struct blockvector *bv = BLOCKVECTOR (stab);
3330 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
156942c7 3331
da51c347
DE
3332 sym = lookup_block_symbol (block, name, domain);
3333 }
1fd400ff 3334
da51c347
DE
3335 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3336 {
3337 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3338 return stab;
3339
3340 stab_best = stab;
9291a0cd 3341 }
da51c347
DE
3342
3343 /* Keep looking through other CUs. */
9291a0cd
TT
3344 }
3345 }
9291a0cd 3346
da51c347 3347 return stab_best;
9291a0cd
TT
3348}
3349
3350static void
3351dw2_print_stats (struct objfile *objfile)
3352{
e4a48d9d 3353 int i, total, count;
9291a0cd
TT
3354
3355 dw2_setup (objfile);
e4a48d9d 3356 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
9291a0cd 3357 count = 0;
e4a48d9d 3358 for (i = 0; i < total; ++i)
9291a0cd 3359 {
e254ef6a 3360 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3361
e254ef6a 3362 if (!per_cu->v.quick->symtab)
9291a0cd
TT
3363 ++count;
3364 }
e4a48d9d 3365 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3366 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3367}
3368
779bd270
DE
3369/* This dumps minimal information about the index.
3370 It is called via "mt print objfiles".
3371 One use is to verify .gdb_index has been loaded by the
3372 gdb.dwarf2/gdb-index.exp testcase. */
3373
9291a0cd
TT
3374static void
3375dw2_dump (struct objfile *objfile)
3376{
779bd270
DE
3377 dw2_setup (objfile);
3378 gdb_assert (dwarf2_per_objfile->using_index);
3379 printf_filtered (".gdb_index:");
3380 if (dwarf2_per_objfile->index_table != NULL)
3381 {
3382 printf_filtered (" version %d\n",
3383 dwarf2_per_objfile->index_table->version);
3384 }
3385 else
3386 printf_filtered (" faked for \"readnow\"\n");
3387 printf_filtered ("\n");
9291a0cd
TT
3388}
3389
3390static void
3189cb12
DE
3391dw2_relocate (struct objfile *objfile,
3392 const struct section_offsets *new_offsets,
3393 const struct section_offsets *delta)
9291a0cd
TT
3394{
3395 /* There's nothing to relocate here. */
3396}
3397
3398static void
3399dw2_expand_symtabs_for_function (struct objfile *objfile,
3400 const char *func_name)
3401{
da51c347
DE
3402 struct mapped_index *index;
3403
3404 dw2_setup (objfile);
3405
3406 index = dwarf2_per_objfile->index_table;
3407
3408 /* index is NULL if OBJF_READNOW. */
3409 if (index)
3410 {
3411 struct dw2_symtab_iterator iter;
3412 struct dwarf2_per_cu_data *per_cu;
3413
3414 /* Note: It doesn't matter what we pass for block_index here. */
3415 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3416 func_name);
3417
3418 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3419 dw2_instantiate_symtab (per_cu);
3420 }
9291a0cd
TT
3421}
3422
3423static void
3424dw2_expand_all_symtabs (struct objfile *objfile)
3425{
3426 int i;
3427
3428 dw2_setup (objfile);
1fd400ff
TT
3429
3430 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3431 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3432 {
e254ef6a 3433 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3434
a0f42c21 3435 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3436 }
3437}
3438
3439static void
652a8996
JK
3440dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3441 const char *fullname)
9291a0cd
TT
3442{
3443 int i;
3444
3445 dw2_setup (objfile);
d4637a04
DE
3446
3447 /* We don't need to consider type units here.
3448 This is only called for examining code, e.g. expand_line_sal.
3449 There can be an order of magnitude (or more) more type units
3450 than comp units, and we avoid them if we can. */
3451
3452 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3453 {
3454 int j;
e254ef6a 3455 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3456 struct quick_file_names *file_data;
9291a0cd 3457
3d7bb9d9 3458 /* We only need to look at symtabs not already expanded. */
e254ef6a 3459 if (per_cu->v.quick->symtab)
9291a0cd
TT
3460 continue;
3461
e4a48d9d 3462 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3463 if (file_data == NULL)
9291a0cd
TT
3464 continue;
3465
7b9f3c50 3466 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3467 {
652a8996
JK
3468 const char *this_fullname = file_data->file_names[j];
3469
3470 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3471 {
a0f42c21 3472 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3473 break;
3474 }
3475 }
3476 }
3477}
3478
9291a0cd 3479static void
40658b94
PH
3480dw2_map_matching_symbols (const char * name, domain_enum namespace,
3481 struct objfile *objfile, int global,
3482 int (*callback) (struct block *,
3483 struct symbol *, void *),
2edb89d3
JK
3484 void *data, symbol_compare_ftype *match,
3485 symbol_compare_ftype *ordered_compare)
9291a0cd 3486{
40658b94 3487 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3488 current language is Ada for a non-Ada objfile using GNU index. As Ada
3489 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3490}
3491
3492static void
f8eba3c6
TT
3493dw2_expand_symtabs_matching
3494 (struct objfile *objfile,
fbd9ab74 3495 int (*file_matcher) (const char *, void *, int basenames),
e078317b 3496 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3497 enum search_domain kind,
3498 void *data)
9291a0cd
TT
3499{
3500 int i;
3501 offset_type iter;
4b5246aa 3502 struct mapped_index *index;
9291a0cd
TT
3503
3504 dw2_setup (objfile);
ae2de4f8
DE
3505
3506 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3507 if (!dwarf2_per_objfile->index_table)
3508 return;
4b5246aa 3509 index = dwarf2_per_objfile->index_table;
9291a0cd 3510
7b08b9eb 3511 if (file_matcher != NULL)
24c79950
TT
3512 {
3513 struct cleanup *cleanup;
3514 htab_t visited_found, visited_not_found;
3515
3516 visited_found = htab_create_alloc (10,
3517 htab_hash_pointer, htab_eq_pointer,
3518 NULL, xcalloc, xfree);
3519 cleanup = make_cleanup_htab_delete (visited_found);
3520 visited_not_found = htab_create_alloc (10,
3521 htab_hash_pointer, htab_eq_pointer,
3522 NULL, xcalloc, xfree);
3523 make_cleanup_htab_delete (visited_not_found);
3524
848e3e78
DE
3525 /* The rule is CUs specify all the files, including those used by
3526 any TU, so there's no need to scan TUs here. */
3527
3528 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3529 {
3530 int j;
f4dc4d17 3531 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
24c79950
TT
3532 struct quick_file_names *file_data;
3533 void **slot;
7b08b9eb 3534
24c79950 3535 per_cu->v.quick->mark = 0;
3d7bb9d9 3536
24c79950
TT
3537 /* We only need to look at symtabs not already expanded. */
3538 if (per_cu->v.quick->symtab)
3539 continue;
7b08b9eb 3540
e4a48d9d 3541 file_data = dw2_get_file_names (per_cu);
24c79950
TT
3542 if (file_data == NULL)
3543 continue;
7b08b9eb 3544
24c79950
TT
3545 if (htab_find (visited_not_found, file_data) != NULL)
3546 continue;
3547 else if (htab_find (visited_found, file_data) != NULL)
3548 {
3549 per_cu->v.quick->mark = 1;
3550 continue;
3551 }
3552
3553 for (j = 0; j < file_data->num_file_names; ++j)
3554 {
da235a7c
JK
3555 const char *this_real_name;
3556
fbd9ab74 3557 if (file_matcher (file_data->file_names[j], data, 0))
24c79950
TT
3558 {
3559 per_cu->v.quick->mark = 1;
3560 break;
3561 }
da235a7c
JK
3562
3563 /* Before we invoke realpath, which can get expensive when many
3564 files are involved, do a quick comparison of the basenames. */
3565 if (!basenames_may_differ
3566 && !file_matcher (lbasename (file_data->file_names[j]),
3567 data, 1))
3568 continue;
3569
3570 this_real_name = dw2_get_real_path (objfile, file_data, j);
3571 if (file_matcher (this_real_name, data, 0))
3572 {
3573 per_cu->v.quick->mark = 1;
3574 break;
3575 }
24c79950
TT
3576 }
3577
3578 slot = htab_find_slot (per_cu->v.quick->mark
3579 ? visited_found
3580 : visited_not_found,
3581 file_data, INSERT);
3582 *slot = file_data;
3583 }
3584
3585 do_cleanups (cleanup);
3586 }
9291a0cd 3587
3876f04e 3588 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3589 {
3590 offset_type idx = 2 * iter;
3591 const char *name;
3592 offset_type *vec, vec_len, vec_idx;
3593
3876f04e 3594 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3595 continue;
3596
3876f04e 3597 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3598
e078317b 3599 if (! (*name_matcher) (name, data))
9291a0cd
TT
3600 continue;
3601
3602 /* The name was matched, now expand corresponding CUs that were
3603 marked. */
4b5246aa 3604 vec = (offset_type *) (index->constant_pool
3876f04e 3605 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3606 vec_len = MAYBE_SWAP (vec[0]);
3607 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3608 {
e254ef6a 3609 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3610 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3611 gdb_index_symbol_kind symbol_kind =
3612 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3613 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3190f0c6
DE
3614 /* Only check the symbol attributes if they're present.
3615 Indices prior to version 7 don't record them,
3616 and indices >= 7 may elide them for certain symbols
3617 (gold does this). */
3618 int attrs_valid =
3619 (index->version >= 7
3620 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3621
3622 /* Only check the symbol's kind if it has one. */
3623 if (attrs_valid)
156942c7
DE
3624 {
3625 switch (kind)
3626 {
3627 case VARIABLES_DOMAIN:
3628 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3629 continue;
3630 break;
3631 case FUNCTIONS_DOMAIN:
3632 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3633 continue;
3634 break;
3635 case TYPES_DOMAIN:
3636 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3637 continue;
3638 break;
3639 default:
3640 break;
3641 }
3642 }
3643
3190f0c6
DE
3644 /* Don't crash on bad data. */
3645 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3646 + dwarf2_per_objfile->n_type_units))
3647 {
3648 complaint (&symfile_complaints,
3649 _(".gdb_index entry has bad CU index"
3650 " [in module %s]"), objfile->name);
3651 continue;
3652 }
3653
156942c7 3654 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3655 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3656 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3657 }
3658 }
3659}
3660
9703b513
TT
3661/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3662 symtab. */
3663
3664static struct symtab *
3665recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3666{
3667 int i;
3668
3669 if (BLOCKVECTOR (symtab) != NULL
3670 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3671 return symtab;
3672
a3ec0bb1
DE
3673 if (symtab->includes == NULL)
3674 return NULL;
3675
9703b513
TT
3676 for (i = 0; symtab->includes[i]; ++i)
3677 {
a3ec0bb1 3678 struct symtab *s = symtab->includes[i];
9703b513
TT
3679
3680 s = recursively_find_pc_sect_symtab (s, pc);
3681 if (s != NULL)
3682 return s;
3683 }
3684
3685 return NULL;
3686}
3687
9291a0cd
TT
3688static struct symtab *
3689dw2_find_pc_sect_symtab (struct objfile *objfile,
3690 struct minimal_symbol *msymbol,
3691 CORE_ADDR pc,
3692 struct obj_section *section,
3693 int warn_if_readin)
3694{
3695 struct dwarf2_per_cu_data *data;
9703b513 3696 struct symtab *result;
9291a0cd
TT
3697
3698 dw2_setup (objfile);
3699
3700 if (!objfile->psymtabs_addrmap)
3701 return NULL;
3702
3703 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3704 if (!data)
3705 return NULL;
3706
3707 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3708 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3709 paddress (get_objfile_arch (objfile), pc));
3710
9703b513
TT
3711 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3712 gdb_assert (result != NULL);
3713 return result;
9291a0cd
TT
3714}
3715
9291a0cd 3716static void
44b13c5a 3717dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3718 void *data, int need_fullname)
9291a0cd
TT
3719{
3720 int i;
24c79950
TT
3721 struct cleanup *cleanup;
3722 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3723 NULL, xcalloc, xfree);
9291a0cd 3724
24c79950 3725 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3726 dw2_setup (objfile);
ae2de4f8 3727
848e3e78
DE
3728 /* The rule is CUs specify all the files, including those used by
3729 any TU, so there's no need to scan TUs here.
3730 We can ignore file names coming from already-expanded CUs. */
f4dc4d17 3731
848e3e78 3732 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
24c79950
TT
3733 {
3734 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3735
3736 if (per_cu->v.quick->symtab)
3737 {
3738 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3739 INSERT);
3740
3741 *slot = per_cu->v.quick->file_names;
3742 }
3743 }
3744
848e3e78 3745 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
3746 {
3747 int j;
f4dc4d17 3748 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
7b9f3c50 3749 struct quick_file_names *file_data;
24c79950 3750 void **slot;
9291a0cd 3751
3d7bb9d9 3752 /* We only need to look at symtabs not already expanded. */
e254ef6a 3753 if (per_cu->v.quick->symtab)
9291a0cd
TT
3754 continue;
3755
e4a48d9d 3756 file_data = dw2_get_file_names (per_cu);
7b9f3c50 3757 if (file_data == NULL)
9291a0cd
TT
3758 continue;
3759
24c79950
TT
3760 slot = htab_find_slot (visited, file_data, INSERT);
3761 if (*slot)
3762 {
3763 /* Already visited. */
3764 continue;
3765 }
3766 *slot = file_data;
3767
7b9f3c50 3768 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3769 {
74e2f255
DE
3770 const char *this_real_name;
3771
3772 if (need_fullname)
3773 this_real_name = dw2_get_real_path (objfile, file_data, j);
3774 else
3775 this_real_name = NULL;
7b9f3c50 3776 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3777 }
3778 }
24c79950
TT
3779
3780 do_cleanups (cleanup);
9291a0cd
TT
3781}
3782
3783static int
3784dw2_has_symbols (struct objfile *objfile)
3785{
3786 return 1;
3787}
3788
3789const struct quick_symbol_functions dwarf2_gdb_index_functions =
3790{
3791 dw2_has_symbols,
3792 dw2_find_last_source_symtab,
3793 dw2_forget_cached_source_info,
f8eba3c6 3794 dw2_map_symtabs_matching_filename,
9291a0cd 3795 dw2_lookup_symbol,
9291a0cd
TT
3796 dw2_print_stats,
3797 dw2_dump,
3798 dw2_relocate,
3799 dw2_expand_symtabs_for_function,
3800 dw2_expand_all_symtabs,
652a8996 3801 dw2_expand_symtabs_with_fullname,
40658b94 3802 dw2_map_matching_symbols,
9291a0cd
TT
3803 dw2_expand_symtabs_matching,
3804 dw2_find_pc_sect_symtab,
9291a0cd
TT
3805 dw2_map_symbol_filenames
3806};
3807
3808/* Initialize for reading DWARF for this objfile. Return 0 if this
3809 file will use psymtabs, or 1 if using the GNU index. */
3810
3811int
3812dwarf2_initialize_objfile (struct objfile *objfile)
3813{
3814 /* If we're about to read full symbols, don't bother with the
3815 indices. In this case we also don't care if some other debug
3816 format is making psymtabs, because they are all about to be
3817 expanded anyway. */
3818 if ((objfile->flags & OBJF_READNOW))
3819 {
3820 int i;
3821
3822 dwarf2_per_objfile->using_index = 1;
3823 create_all_comp_units (objfile);
0e50663e 3824 create_all_type_units (objfile);
7b9f3c50
DE
3825 dwarf2_per_objfile->quick_file_names_table =
3826 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3827
1fd400ff 3828 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3829 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3830 {
e254ef6a 3831 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3832
e254ef6a
DE
3833 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3834 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3835 }
3836
3837 /* Return 1 so that gdb sees the "quick" functions. However,
3838 these functions will be no-ops because we will have expanded
3839 all symtabs. */
3840 return 1;
3841 }
3842
3843 if (dwarf2_read_index (objfile))
3844 return 1;
3845
9291a0cd
TT
3846 return 0;
3847}
3848
3849\f
3850
dce234bc
PP
3851/* Build a partial symbol table. */
3852
3853void
f29dff0a 3854dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3855{
c9bf0622
TT
3856 volatile struct gdb_exception except;
3857
f29dff0a 3858 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3859 {
3860 init_psymbol_list (objfile, 1024);
3861 }
3862
c9bf0622
TT
3863 TRY_CATCH (except, RETURN_MASK_ERROR)
3864 {
3865 /* This isn't really ideal: all the data we allocate on the
3866 objfile's obstack is still uselessly kept around. However,
3867 freeing it seems unsafe. */
3868 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3869
3870 dwarf2_build_psymtabs_hard (objfile);
3871 discard_cleanups (cleanups);
3872 }
3873 if (except.reason < 0)
3874 exception_print (gdb_stderr, except);
c906108c 3875}
c906108c 3876
1ce1cefd
DE
3877/* Return the total length of the CU described by HEADER. */
3878
3879static unsigned int
3880get_cu_length (const struct comp_unit_head *header)
3881{
3882 return header->initial_length_size + header->length;
3883}
3884
45452591
DE
3885/* Return TRUE if OFFSET is within CU_HEADER. */
3886
3887static inline int
b64f50a1 3888offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3889{
b64f50a1 3890 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3891 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3892
b64f50a1 3893 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3894}
3895
3b80fe9b
DE
3896/* Find the base address of the compilation unit for range lists and
3897 location lists. It will normally be specified by DW_AT_low_pc.
3898 In DWARF-3 draft 4, the base address could be overridden by
3899 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3900 compilation units with discontinuous ranges. */
3901
3902static void
3903dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3904{
3905 struct attribute *attr;
3906
3907 cu->base_known = 0;
3908 cu->base_address = 0;
3909
3910 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3911 if (attr)
3912 {
3913 cu->base_address = DW_ADDR (attr);
3914 cu->base_known = 1;
3915 }
3916 else
3917 {
3918 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3919 if (attr)
3920 {
3921 cu->base_address = DW_ADDR (attr);
3922 cu->base_known = 1;
3923 }
3924 }
3925}
3926
93311388
DE
3927/* Read in the comp unit header information from the debug_info at info_ptr.
3928 NOTE: This leaves members offset, first_die_offset to be filled in
3929 by the caller. */
107d2387 3930
d521ce57 3931static const gdb_byte *
107d2387 3932read_comp_unit_head (struct comp_unit_head *cu_header,
d521ce57 3933 const gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3934{
3935 int signed_addr;
891d2f0b 3936 unsigned int bytes_read;
c764a876
DE
3937
3938 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3939 cu_header->initial_length_size = bytes_read;
3940 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3941 info_ptr += bytes_read;
107d2387
AC
3942 cu_header->version = read_2_bytes (abfd, info_ptr);
3943 info_ptr += 2;
b64f50a1
JK
3944 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3945 &bytes_read);
613e1657 3946 info_ptr += bytes_read;
107d2387
AC
3947 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3948 info_ptr += 1;
3949 signed_addr = bfd_get_sign_extend_vma (abfd);
3950 if (signed_addr < 0)
8e65ff28 3951 internal_error (__FILE__, __LINE__,
e2e0b3e5 3952 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3953 cu_header->signed_addr_p = signed_addr;
c764a876 3954
107d2387
AC
3955 return info_ptr;
3956}
3957
36586728
TT
3958/* Helper function that returns the proper abbrev section for
3959 THIS_CU. */
3960
3961static struct dwarf2_section_info *
3962get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3963{
3964 struct dwarf2_section_info *abbrev;
3965
3966 if (this_cu->is_dwz)
3967 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3968 else
3969 abbrev = &dwarf2_per_objfile->abbrev;
3970
3971 return abbrev;
3972}
3973
9ff913ba
DE
3974/* Subroutine of read_and_check_comp_unit_head and
3975 read_and_check_type_unit_head to simplify them.
3976 Perform various error checking on the header. */
3977
3978static void
3979error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3980 struct dwarf2_section_info *section,
3981 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3982{
3983 bfd *abfd = section->asection->owner;
3984 const char *filename = bfd_get_filename (abfd);
3985
3986 if (header->version != 2 && header->version != 3 && header->version != 4)
3987 error (_("Dwarf Error: wrong version in compilation unit header "
3988 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3989 filename);
3990
b64f50a1 3991 if (header->abbrev_offset.sect_off
36586728 3992 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9ff913ba
DE
3993 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3994 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3995 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3996 filename);
3997
3998 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3999 avoid potential 32-bit overflow. */
1ce1cefd 4000 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
4001 > section->size)
4002 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4003 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 4004 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
4005 filename);
4006}
4007
4008/* Read in a CU/TU header and perform some basic error checking.
4009 The contents of the header are stored in HEADER.
4010 The result is a pointer to the start of the first DIE. */
adabb602 4011
d521ce57 4012static const gdb_byte *
9ff913ba
DE
4013read_and_check_comp_unit_head (struct comp_unit_head *header,
4014 struct dwarf2_section_info *section,
4bdcc0c1 4015 struct dwarf2_section_info *abbrev_section,
d521ce57 4016 const gdb_byte *info_ptr,
9ff913ba 4017 int is_debug_types_section)
72bf9492 4018{
d521ce57 4019 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4020 bfd *abfd = section->asection->owner;
72bf9492 4021
b64f50a1 4022 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 4023
72bf9492
DJ
4024 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4025
460c1c54
CC
4026 /* If we're reading a type unit, skip over the signature and
4027 type_offset fields. */
b0df02fd 4028 if (is_debug_types_section)
460c1c54
CC
4029 info_ptr += 8 /*signature*/ + header->offset_size;
4030
b64f50a1 4031 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 4032
4bdcc0c1 4033 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
4034
4035 return info_ptr;
4036}
4037
348e048f
DE
4038/* Read in the types comp unit header information from .debug_types entry at
4039 types_ptr. The result is a pointer to one past the end of the header. */
4040
d521ce57 4041static const gdb_byte *
9ff913ba
DE
4042read_and_check_type_unit_head (struct comp_unit_head *header,
4043 struct dwarf2_section_info *section,
4bdcc0c1 4044 struct dwarf2_section_info *abbrev_section,
d521ce57 4045 const gdb_byte *info_ptr,
dee91e82
DE
4046 ULONGEST *signature,
4047 cu_offset *type_offset_in_tu)
348e048f 4048{
d521ce57 4049 const gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 4050 bfd *abfd = section->asection->owner;
348e048f 4051
b64f50a1 4052 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 4053
9ff913ba 4054 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 4055
9ff913ba
DE
4056 /* If we're reading a type unit, skip over the signature and
4057 type_offset fields. */
4058 if (signature != NULL)
4059 *signature = read_8_bytes (abfd, info_ptr);
4060 info_ptr += 8;
dee91e82
DE
4061 if (type_offset_in_tu != NULL)
4062 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4063 header->offset_size);
9ff913ba
DE
4064 info_ptr += header->offset_size;
4065
b64f50a1 4066 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 4067
4bdcc0c1 4068 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
4069
4070 return info_ptr;
348e048f
DE
4071}
4072
f4dc4d17
DE
4073/* Fetch the abbreviation table offset from a comp or type unit header. */
4074
4075static sect_offset
4076read_abbrev_offset (struct dwarf2_section_info *section,
4077 sect_offset offset)
4078{
4079 bfd *abfd = section->asection->owner;
d521ce57 4080 const gdb_byte *info_ptr;
f4dc4d17
DE
4081 unsigned int length, initial_length_size, offset_size;
4082 sect_offset abbrev_offset;
4083
4084 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4085 info_ptr = section->buffer + offset.sect_off;
4086 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4087 offset_size = initial_length_size == 4 ? 4 : 8;
4088 info_ptr += initial_length_size + 2 /*version*/;
4089 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4090 return abbrev_offset;
4091}
4092
aaa75496
JB
4093/* Allocate a new partial symtab for file named NAME and mark this new
4094 partial symtab as being an include of PST. */
4095
4096static void
d521ce57 4097dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
4098 struct objfile *objfile)
4099{
4100 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4101
fbd9ab74
JK
4102 if (!IS_ABSOLUTE_PATH (subpst->filename))
4103 {
4104 /* It shares objfile->objfile_obstack. */
4105 subpst->dirname = pst->dirname;
4106 }
4107
aaa75496
JB
4108 subpst->section_offsets = pst->section_offsets;
4109 subpst->textlow = 0;
4110 subpst->texthigh = 0;
4111
4112 subpst->dependencies = (struct partial_symtab **)
4113 obstack_alloc (&objfile->objfile_obstack,
4114 sizeof (struct partial_symtab *));
4115 subpst->dependencies[0] = pst;
4116 subpst->number_of_dependencies = 1;
4117
4118 subpst->globals_offset = 0;
4119 subpst->n_global_syms = 0;
4120 subpst->statics_offset = 0;
4121 subpst->n_static_syms = 0;
4122 subpst->symtab = NULL;
4123 subpst->read_symtab = pst->read_symtab;
4124 subpst->readin = 0;
4125
4126 /* No private part is necessary for include psymtabs. This property
4127 can be used to differentiate between such include psymtabs and
10b3939b 4128 the regular ones. */
58a9656e 4129 subpst->read_symtab_private = NULL;
aaa75496
JB
4130}
4131
4132/* Read the Line Number Program data and extract the list of files
4133 included by the source file represented by PST. Build an include
d85a05f0 4134 partial symtab for each of these included files. */
aaa75496
JB
4135
4136static void
4137dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
4138 struct die_info *die,
4139 struct partial_symtab *pst)
aaa75496 4140{
d85a05f0
DJ
4141 struct line_header *lh = NULL;
4142 struct attribute *attr;
aaa75496 4143
d85a05f0
DJ
4144 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4145 if (attr)
3019eac3 4146 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
4147 if (lh == NULL)
4148 return; /* No linetable, so no includes. */
4149
c6da4cef 4150 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 4151 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
4152
4153 free_line_header (lh);
4154}
4155
348e048f 4156static hashval_t
52dc124a 4157hash_signatured_type (const void *item)
348e048f 4158{
52dc124a 4159 const struct signatured_type *sig_type = item;
9a619af0 4160
348e048f 4161 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 4162 return sig_type->signature;
348e048f
DE
4163}
4164
4165static int
52dc124a 4166eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
4167{
4168 const struct signatured_type *lhs = item_lhs;
4169 const struct signatured_type *rhs = item_rhs;
9a619af0 4170
348e048f
DE
4171 return lhs->signature == rhs->signature;
4172}
4173
1fd400ff
TT
4174/* Allocate a hash table for signatured types. */
4175
4176static htab_t
673bfd45 4177allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
4178{
4179 return htab_create_alloc_ex (41,
52dc124a
DE
4180 hash_signatured_type,
4181 eq_signatured_type,
1fd400ff
TT
4182 NULL,
4183 &objfile->objfile_obstack,
4184 hashtab_obstack_allocate,
4185 dummy_obstack_deallocate);
4186}
4187
d467dd73 4188/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
4189
4190static int
d467dd73 4191add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
4192{
4193 struct signatured_type *sigt = *slot;
b4dd5633 4194 struct signatured_type ***datap = datum;
1fd400ff 4195
b4dd5633 4196 **datap = sigt;
1fd400ff
TT
4197 ++*datap;
4198
4199 return 1;
4200}
4201
c88ee1f0
DE
4202/* Create the hash table of all entries in the .debug_types
4203 (or .debug_types.dwo) section(s).
4204 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4205 otherwise it is NULL.
4206
4207 The result is a pointer to the hash table or NULL if there are no types.
4208
4209 Note: This function processes DWO files only, not DWP files. */
348e048f 4210
3019eac3
DE
4211static htab_t
4212create_debug_types_hash_table (struct dwo_file *dwo_file,
4213 VEC (dwarf2_section_info_def) *types)
348e048f 4214{
3019eac3 4215 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 4216 htab_t types_htab = NULL;
8b70b953
TT
4217 int ix;
4218 struct dwarf2_section_info *section;
4bdcc0c1 4219 struct dwarf2_section_info *abbrev_section;
348e048f 4220
3019eac3
DE
4221 if (VEC_empty (dwarf2_section_info_def, types))
4222 return NULL;
348e048f 4223
4bdcc0c1
DE
4224 abbrev_section = (dwo_file != NULL
4225 ? &dwo_file->sections.abbrev
4226 : &dwarf2_per_objfile->abbrev);
4227
09406207
DE
4228 if (dwarf2_read_debug)
4229 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4230 dwo_file ? ".dwo" : "",
4231 bfd_get_filename (abbrev_section->asection->owner));
4232
8b70b953 4233 for (ix = 0;
3019eac3 4234 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
4235 ++ix)
4236 {
3019eac3 4237 bfd *abfd;
d521ce57 4238 const gdb_byte *info_ptr, *end_ptr;
36586728 4239 struct dwarf2_section_info *abbrev_section;
348e048f 4240
8b70b953
TT
4241 dwarf2_read_section (objfile, section);
4242 info_ptr = section->buffer;
348e048f 4243
8b70b953
TT
4244 if (info_ptr == NULL)
4245 continue;
348e048f 4246
3019eac3
DE
4247 /* We can't set abfd until now because the section may be empty or
4248 not present, in which case section->asection will be NULL. */
4249 abfd = section->asection->owner;
4250
36586728
TT
4251 if (dwo_file)
4252 abbrev_section = &dwo_file->sections.abbrev;
4253 else
4254 abbrev_section = &dwarf2_per_objfile->abbrev;
4255
dee91e82
DE
4256 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4257 because we don't need to read any dies: the signature is in the
4258 header. */
8b70b953
TT
4259
4260 end_ptr = info_ptr + section->size;
4261 while (info_ptr < end_ptr)
4262 {
b64f50a1 4263 sect_offset offset;
3019eac3 4264 cu_offset type_offset_in_tu;
8b70b953 4265 ULONGEST signature;
52dc124a 4266 struct signatured_type *sig_type;
3019eac3 4267 struct dwo_unit *dwo_tu;
8b70b953 4268 void **slot;
d521ce57 4269 const gdb_byte *ptr = info_ptr;
9ff913ba 4270 struct comp_unit_head header;
dee91e82 4271 unsigned int length;
348e048f 4272
b64f50a1 4273 offset.sect_off = ptr - section->buffer;
348e048f 4274
8b70b953 4275 /* We need to read the type's signature in order to build the hash
9ff913ba 4276 table, but we don't need anything else just yet. */
348e048f 4277
4bdcc0c1
DE
4278 ptr = read_and_check_type_unit_head (&header, section,
4279 abbrev_section, ptr,
3019eac3 4280 &signature, &type_offset_in_tu);
6caca83c 4281
1ce1cefd 4282 length = get_cu_length (&header);
dee91e82 4283
6caca83c 4284 /* Skip dummy type units. */
dee91e82
DE
4285 if (ptr >= info_ptr + length
4286 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 4287 {
1ce1cefd 4288 info_ptr += length;
6caca83c
CC
4289 continue;
4290 }
8b70b953 4291
0349ea22
DE
4292 if (types_htab == NULL)
4293 {
4294 if (dwo_file)
4295 types_htab = allocate_dwo_unit_table (objfile);
4296 else
4297 types_htab = allocate_signatured_type_table (objfile);
4298 }
4299
3019eac3
DE
4300 if (dwo_file)
4301 {
4302 sig_type = NULL;
4303 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4304 struct dwo_unit);
4305 dwo_tu->dwo_file = dwo_file;
4306 dwo_tu->signature = signature;
4307 dwo_tu->type_offset_in_tu = type_offset_in_tu;
8a0459fd 4308 dwo_tu->section = section;
3019eac3
DE
4309 dwo_tu->offset = offset;
4310 dwo_tu->length = length;
4311 }
4312 else
4313 {
4314 /* N.B.: type_offset is not usable if this type uses a DWO file.
4315 The real type_offset is in the DWO file. */
4316 dwo_tu = NULL;
4317 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4318 struct signatured_type);
4319 sig_type->signature = signature;
4320 sig_type->type_offset_in_tu = type_offset_in_tu;
4321 sig_type->per_cu.objfile = objfile;
4322 sig_type->per_cu.is_debug_types = 1;
8a0459fd 4323 sig_type->per_cu.section = section;
3019eac3
DE
4324 sig_type->per_cu.offset = offset;
4325 sig_type->per_cu.length = length;
4326 }
8b70b953 4327
3019eac3
DE
4328 slot = htab_find_slot (types_htab,
4329 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4330 INSERT);
8b70b953
TT
4331 gdb_assert (slot != NULL);
4332 if (*slot != NULL)
4333 {
3019eac3
DE
4334 sect_offset dup_offset;
4335
4336 if (dwo_file)
4337 {
4338 const struct dwo_unit *dup_tu = *slot;
4339
4340 dup_offset = dup_tu->offset;
4341 }
4342 else
4343 {
4344 const struct signatured_type *dup_tu = *slot;
4345
4346 dup_offset = dup_tu->per_cu.offset;
4347 }
b3c8eb43 4348
8b70b953 4349 complaint (&symfile_complaints,
c88ee1f0 4350 _("debug type entry at offset 0x%x is duplicate to"
4031ecc5 4351 " the entry at offset 0x%x, signature %s"),
3019eac3 4352 offset.sect_off, dup_offset.sect_off,
4031ecc5 4353 hex_string (signature));
8b70b953 4354 }
3019eac3 4355 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 4356
09406207 4357 if (dwarf2_read_debug)
4031ecc5 4358 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
b64f50a1 4359 offset.sect_off,
4031ecc5 4360 hex_string (signature));
348e048f 4361
dee91e82 4362 info_ptr += length;
8b70b953 4363 }
348e048f
DE
4364 }
4365
3019eac3
DE
4366 return types_htab;
4367}
4368
4369/* Create the hash table of all entries in the .debug_types section,
4370 and initialize all_type_units.
4371 The result is zero if there is an error (e.g. missing .debug_types section),
4372 otherwise non-zero. */
4373
4374static int
4375create_all_type_units (struct objfile *objfile)
4376{
4377 htab_t types_htab;
b4dd5633 4378 struct signatured_type **iter;
3019eac3
DE
4379
4380 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4381 if (types_htab == NULL)
4382 {
4383 dwarf2_per_objfile->signatured_types = NULL;
4384 return 0;
4385 }
4386
348e048f
DE
4387 dwarf2_per_objfile->signatured_types = types_htab;
4388
d467dd73
DE
4389 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4390 dwarf2_per_objfile->all_type_units
a2ce51a0
DE
4391 = xmalloc (dwarf2_per_objfile->n_type_units
4392 * sizeof (struct signatured_type *));
d467dd73
DE
4393 iter = &dwarf2_per_objfile->all_type_units[0];
4394 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4395 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4396 == dwarf2_per_objfile->n_type_units);
1fd400ff 4397
348e048f
DE
4398 return 1;
4399}
4400
a2ce51a0
DE
4401/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4402 Fill in SIG_ENTRY with DWO_ENTRY. */
4403
4404static void
4405fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4406 struct signatured_type *sig_entry,
4407 struct dwo_unit *dwo_entry)
4408{
7ee85ab1 4409 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
4410 gdb_assert (! sig_entry->per_cu.queued);
4411 gdb_assert (sig_entry->per_cu.cu == NULL);
4412 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4413 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4414 gdb_assert (sig_entry->signature == dwo_entry->signature);
4415 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4416 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
4417 gdb_assert (sig_entry->dwo_unit == NULL);
4418
4419 sig_entry->per_cu.section = dwo_entry->section;
4420 sig_entry->per_cu.offset = dwo_entry->offset;
4421 sig_entry->per_cu.length = dwo_entry->length;
4422 sig_entry->per_cu.reading_dwo_directly = 1;
4423 sig_entry->per_cu.objfile = objfile;
a2ce51a0
DE
4424 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4425 sig_entry->dwo_unit = dwo_entry;
4426}
4427
4428/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
4429 If we haven't read the TU yet, create the signatured_type data structure
4430 for a TU to be read in directly from a DWO file, bypassing the stub.
4431 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4432 using .gdb_index, then when reading a CU we want to stay in the DWO file
4433 containing that CU. Otherwise we could end up reading several other DWO
4434 files (due to comdat folding) to process the transitive closure of all the
4435 mentioned TUs, and that can be slow. The current DWO file will have every
4436 type signature that it needs.
a2ce51a0
DE
4437 We only do this for .gdb_index because in the psymtab case we already have
4438 to read all the DWOs to build the type unit groups. */
4439
4440static struct signatured_type *
4441lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4442{
4443 struct objfile *objfile = dwarf2_per_objfile->objfile;
4444 struct dwo_file *dwo_file;
4445 struct dwo_unit find_dwo_entry, *dwo_entry;
4446 struct signatured_type find_sig_entry, *sig_entry;
4447
4448 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4449
4450 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4451 dwo_unit of the TU itself. */
4452 dwo_file = cu->dwo_unit->dwo_file;
4453
4454 /* We only ever need to read in one copy of a signatured type.
4455 Just use the global signatured_types array. If this is the first time
4456 we're reading this type, replace the recorded data from .gdb_index with
4457 this TU. */
4458
4459 if (dwarf2_per_objfile->signatured_types == NULL)
4460 return NULL;
4461 find_sig_entry.signature = sig;
4462 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4463 if (sig_entry == NULL)
4464 return NULL;
7ee85ab1
DE
4465
4466 /* We can get here with the TU already read, *or* in the process of being
4467 read. Don't reassign it if that's the case. Also note that if the TU is
4468 already being read, it may not have come from a DWO, the program may be
4469 a mix of Fission-compiled code and non-Fission-compiled code. */
a2ce51a0 4470 /* Have we already tried to read this TU? */
7ee85ab1 4471 if (sig_entry->per_cu.tu_read)
a2ce51a0
DE
4472 return sig_entry;
4473
4474 /* Ok, this is the first time we're reading this TU. */
4475 if (dwo_file->tus == NULL)
4476 return NULL;
4477 find_dwo_entry.signature = sig;
4478 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4479 if (dwo_entry == NULL)
4480 return NULL;
4481
4482 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
89e63ee4 4483 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
4484 return sig_entry;
4485}
4486
4487/* Subroutine of lookup_dwp_signatured_type.
4488 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4489
4490static struct signatured_type *
4491add_type_unit (ULONGEST sig)
4492{
4493 struct objfile *objfile = dwarf2_per_objfile->objfile;
4494 int n_type_units = dwarf2_per_objfile->n_type_units;
4495 struct signatured_type *sig_type;
4496 void **slot;
4497
4498 ++n_type_units;
4499 dwarf2_per_objfile->all_type_units =
4500 xrealloc (dwarf2_per_objfile->all_type_units,
4501 n_type_units * sizeof (struct signatured_type *));
4502 dwarf2_per_objfile->n_type_units = n_type_units;
4503 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4504 struct signatured_type);
4505 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4506 sig_type->signature = sig;
4507 sig_type->per_cu.is_debug_types = 1;
4508 sig_type->per_cu.v.quick =
4509 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4510 struct dwarf2_per_cu_quick_data);
4511 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4512 sig_type, INSERT);
4513 gdb_assert (*slot == NULL);
4514 *slot = sig_type;
4515 /* The rest of sig_type must be filled in by the caller. */
4516 return sig_type;
4517}
4518
4519/* Subroutine of lookup_signatured_type.
4520 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4521 then try the DWP file.
4522 Normally this "can't happen", but if there's a bug in signature
4523 generation and/or the DWP file is built incorrectly, it can happen.
4524 Using the type directly from the DWP file means we don't have the stub
4525 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4526 not critical. [Eventually the stub may go away for type units anyway.] */
4527
4528static struct signatured_type *
4529lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4530{
4531 struct objfile *objfile = dwarf2_per_objfile->objfile;
4532 struct dwp_file *dwp_file = get_dwp_file ();
4533 struct dwo_unit *dwo_entry;
4534 struct signatured_type find_sig_entry, *sig_entry;
4535
4536 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4537 gdb_assert (dwp_file != NULL);
4538
4539 if (dwarf2_per_objfile->signatured_types != NULL)
4540 {
4541 find_sig_entry.signature = sig;
4542 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4543 &find_sig_entry);
4544 if (sig_entry != NULL)
4545 return sig_entry;
4546 }
4547
4548 /* This is the "shouldn't happen" case.
4549 Try the DWP file and hope for the best. */
4550 if (dwp_file->tus == NULL)
4551 return NULL;
4552 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4553 sig, 1 /* is_debug_types */);
4554 if (dwo_entry == NULL)
4555 return NULL;
4556
4557 sig_entry = add_type_unit (sig);
4558 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4559
4560 /* The caller will signal a complaint if we return NULL.
4561 Here we don't return NULL but we still want to complain. */
4562 complaint (&symfile_complaints,
4563 _("Bad type signature %s referenced by %s at 0x%x,"
4564 " coping by using copy in DWP [in module %s]"),
4565 hex_string (sig),
4566 cu->per_cu->is_debug_types ? "TU" : "CU",
4567 cu->per_cu->offset.sect_off,
4568 objfile->name);
4569
4570 return sig_entry;
4571}
4572
380bca97 4573/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
4574 Returns NULL if signature SIG is not present in the table.
4575 It is up to the caller to complain about this. */
348e048f
DE
4576
4577static struct signatured_type *
a2ce51a0 4578lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 4579{
a2ce51a0
DE
4580 if (cu->dwo_unit
4581 && dwarf2_per_objfile->using_index)
4582 {
4583 /* We're in a DWO/DWP file, and we're using .gdb_index.
4584 These cases require special processing. */
4585 if (get_dwp_file () == NULL)
4586 return lookup_dwo_signatured_type (cu, sig);
4587 else
4588 return lookup_dwp_signatured_type (cu, sig);
4589 }
4590 else
4591 {
4592 struct signatured_type find_entry, *entry;
348e048f 4593
a2ce51a0
DE
4594 if (dwarf2_per_objfile->signatured_types == NULL)
4595 return NULL;
4596 find_entry.signature = sig;
4597 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4598 return entry;
4599 }
348e048f 4600}
42e7ad6c
DE
4601\f
4602/* Low level DIE reading support. */
348e048f 4603
d85a05f0
DJ
4604/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4605
4606static void
4607init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 4608 struct dwarf2_cu *cu,
3019eac3
DE
4609 struct dwarf2_section_info *section,
4610 struct dwo_file *dwo_file)
d85a05f0 4611{
fceca515 4612 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 4613 reader->abfd = section->asection->owner;
d85a05f0 4614 reader->cu = cu;
3019eac3 4615 reader->dwo_file = dwo_file;
dee91e82
DE
4616 reader->die_section = section;
4617 reader->buffer = section->buffer;
f664829e 4618 reader->buffer_end = section->buffer + section->size;
a2ce51a0 4619 reader->comp_dir = NULL;
d85a05f0
DJ
4620}
4621
b0c7bfa9
DE
4622/* Subroutine of init_cutu_and_read_dies to simplify it.
4623 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4624 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4625 already.
4626
4627 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4628 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
4629 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4630 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4631 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4632 COMP_DIR must be non-NULL.
b0c7bfa9
DE
4633 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4634 are filled in with the info of the DIE from the DWO file.
4635 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4636 provided an abbrev table to use.
4637 The result is non-zero if a valid (non-dummy) DIE was found. */
4638
4639static int
4640read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4641 struct dwo_unit *dwo_unit,
4642 int abbrev_table_provided,
4643 struct die_info *stub_comp_unit_die,
a2ce51a0 4644 const char *stub_comp_dir,
b0c7bfa9 4645 struct die_reader_specs *result_reader,
d521ce57 4646 const gdb_byte **result_info_ptr,
b0c7bfa9
DE
4647 struct die_info **result_comp_unit_die,
4648 int *result_has_children)
4649{
4650 struct objfile *objfile = dwarf2_per_objfile->objfile;
4651 struct dwarf2_cu *cu = this_cu->cu;
4652 struct dwarf2_section_info *section;
4653 bfd *abfd;
d521ce57 4654 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
4655 const char *comp_dir_string;
4656 ULONGEST signature; /* Or dwo_id. */
4657 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4658 int i,num_extra_attrs;
4659 struct dwarf2_section_info *dwo_abbrev_section;
4660 struct attribute *attr;
a2ce51a0 4661 struct attribute comp_dir_attr;
b0c7bfa9
DE
4662 struct die_info *comp_unit_die;
4663
a2ce51a0
DE
4664 /* Both can't be provided. */
4665 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4666
b0c7bfa9
DE
4667 /* These attributes aren't processed until later:
4668 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4669 However, the attribute is found in the stub which we won't have later.
4670 In order to not impose this complication on the rest of the code,
4671 we read them here and copy them to the DWO CU/TU die. */
4672
4673 stmt_list = NULL;
4674 low_pc = NULL;
4675 high_pc = NULL;
4676 ranges = NULL;
4677 comp_dir = NULL;
4678
4679 if (stub_comp_unit_die != NULL)
4680 {
4681 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4682 DWO file. */
4683 if (! this_cu->is_debug_types)
4684 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4685 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4686 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4687 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4688 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4689
4690 /* There should be a DW_AT_addr_base attribute here (if needed).
4691 We need the value before we can process DW_FORM_GNU_addr_index. */
4692 cu->addr_base = 0;
4693 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4694 if (attr)
4695 cu->addr_base = DW_UNSND (attr);
4696
4697 /* There should be a DW_AT_ranges_base attribute here (if needed).
4698 We need the value before we can process DW_AT_ranges. */
4699 cu->ranges_base = 0;
4700 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4701 if (attr)
4702 cu->ranges_base = DW_UNSND (attr);
4703 }
a2ce51a0
DE
4704 else if (stub_comp_dir != NULL)
4705 {
4706 /* Reconstruct the comp_dir attribute to simplify the code below. */
4707 comp_dir = (struct attribute *)
4708 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4709 comp_dir->name = DW_AT_comp_dir;
4710 comp_dir->form = DW_FORM_string;
4711 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4712 DW_STRING (comp_dir) = stub_comp_dir;
4713 }
b0c7bfa9
DE
4714
4715 /* Set up for reading the DWO CU/TU. */
4716 cu->dwo_unit = dwo_unit;
4717 section = dwo_unit->section;
4718 dwarf2_read_section (objfile, section);
4719 abfd = section->asection->owner;
4720 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4721 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4722 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4723
4724 if (this_cu->is_debug_types)
4725 {
4726 ULONGEST header_signature;
4727 cu_offset type_offset_in_tu;
4728 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4729
4730 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4731 dwo_abbrev_section,
4732 info_ptr,
4733 &header_signature,
4734 &type_offset_in_tu);
a2ce51a0
DE
4735 /* This is not an assert because it can be caused by bad debug info. */
4736 if (sig_type->signature != header_signature)
4737 {
4738 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4739 " TU at offset 0x%x [in module %s]"),
4740 hex_string (sig_type->signature),
4741 hex_string (header_signature),
4742 dwo_unit->offset.sect_off,
4743 bfd_get_filename (abfd));
4744 }
b0c7bfa9
DE
4745 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4746 /* For DWOs coming from DWP files, we don't know the CU length
4747 nor the type's offset in the TU until now. */
4748 dwo_unit->length = get_cu_length (&cu->header);
4749 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4750
4751 /* Establish the type offset that can be used to lookup the type.
4752 For DWO files, we don't know it until now. */
4753 sig_type->type_offset_in_section.sect_off =
4754 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4755 }
4756 else
4757 {
4758 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4759 dwo_abbrev_section,
4760 info_ptr, 0);
4761 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4762 /* For DWOs coming from DWP files, we don't know the CU length
4763 until now. */
4764 dwo_unit->length = get_cu_length (&cu->header);
4765 }
4766
02142a6c
DE
4767 /* Replace the CU's original abbrev table with the DWO's.
4768 Reminder: We can't read the abbrev table until we've read the header. */
b0c7bfa9
DE
4769 if (abbrev_table_provided)
4770 {
4771 /* Don't free the provided abbrev table, the caller of
4772 init_cutu_and_read_dies owns it. */
4773 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4774 /* Ensure the DWO abbrev table gets freed. */
b0c7bfa9
DE
4775 make_cleanup (dwarf2_free_abbrev_table, cu);
4776 }
4777 else
4778 {
4779 dwarf2_free_abbrev_table (cu);
4780 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
02142a6c 4781 /* Leave any existing abbrev table cleanup as is. */
b0c7bfa9
DE
4782 }
4783
4784 /* Read in the die, but leave space to copy over the attributes
4785 from the stub. This has the benefit of simplifying the rest of
4786 the code - all the work to maintain the illusion of a single
4787 DW_TAG_{compile,type}_unit DIE is done here. */
4788 num_extra_attrs = ((stmt_list != NULL)
4789 + (low_pc != NULL)
4790 + (high_pc != NULL)
4791 + (ranges != NULL)
4792 + (comp_dir != NULL));
4793 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4794 result_has_children, num_extra_attrs);
4795
4796 /* Copy over the attributes from the stub to the DIE we just read in. */
4797 comp_unit_die = *result_comp_unit_die;
4798 i = comp_unit_die->num_attrs;
4799 if (stmt_list != NULL)
4800 comp_unit_die->attrs[i++] = *stmt_list;
4801 if (low_pc != NULL)
4802 comp_unit_die->attrs[i++] = *low_pc;
4803 if (high_pc != NULL)
4804 comp_unit_die->attrs[i++] = *high_pc;
4805 if (ranges != NULL)
4806 comp_unit_die->attrs[i++] = *ranges;
4807 if (comp_dir != NULL)
4808 comp_unit_die->attrs[i++] = *comp_dir;
4809 comp_unit_die->num_attrs += num_extra_attrs;
4810
bf6af496
DE
4811 if (dwarf2_die_debug)
4812 {
4813 fprintf_unfiltered (gdb_stdlog,
4814 "Read die from %s@0x%x of %s:\n",
4815 bfd_section_name (abfd, section->asection),
4816 (unsigned) (begin_info_ptr - section->buffer),
4817 bfd_get_filename (abfd));
4818 dump_die (comp_unit_die, dwarf2_die_debug);
4819 }
4820
a2ce51a0
DE
4821 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4822 TUs by skipping the stub and going directly to the entry in the DWO file.
4823 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4824 to get it via circuitous means. Blech. */
4825 if (comp_dir != NULL)
4826 result_reader->comp_dir = DW_STRING (comp_dir);
4827
b0c7bfa9
DE
4828 /* Skip dummy compilation units. */
4829 if (info_ptr >= begin_info_ptr + dwo_unit->length
4830 || peek_abbrev_code (abfd, info_ptr) == 0)
4831 return 0;
4832
4833 *result_info_ptr = info_ptr;
4834 return 1;
4835}
4836
4837/* Subroutine of init_cutu_and_read_dies to simplify it.
4838 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 4839 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
4840
4841static struct dwo_unit *
4842lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4843 struct die_info *comp_unit_die)
4844{
4845 struct dwarf2_cu *cu = this_cu->cu;
4846 struct attribute *attr;
4847 ULONGEST signature;
4848 struct dwo_unit *dwo_unit;
4849 const char *comp_dir, *dwo_name;
4850
a2ce51a0
DE
4851 gdb_assert (cu != NULL);
4852
b0c7bfa9
DE
4853 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4854 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4855 gdb_assert (attr != NULL);
4856 dwo_name = DW_STRING (attr);
4857 comp_dir = NULL;
4858 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4859 if (attr)
4860 comp_dir = DW_STRING (attr);
4861
4862 if (this_cu->is_debug_types)
4863 {
4864 struct signatured_type *sig_type;
4865
4866 /* Since this_cu is the first member of struct signatured_type,
4867 we can go from a pointer to one to a pointer to the other. */
4868 sig_type = (struct signatured_type *) this_cu;
4869 signature = sig_type->signature;
4870 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4871 }
4872 else
4873 {
4874 struct attribute *attr;
4875
4876 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4877 if (! attr)
4878 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4879 " [in module %s]"),
4880 dwo_name, this_cu->objfile->name);
4881 signature = DW_UNSND (attr);
4882 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4883 signature);
4884 }
4885
b0c7bfa9
DE
4886 return dwo_unit;
4887}
4888
a2ce51a0
DE
4889/* Subroutine of init_cutu_and_read_dies to simplify it.
4890 Read a TU directly from a DWO file, bypassing the stub. */
4891
4892static void
4893init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4894 die_reader_func_ftype *die_reader_func,
4895 void *data)
4896{
4897 struct dwarf2_cu *cu;
4898 struct signatured_type *sig_type;
4899 struct cleanup *cleanups, *free_cu_cleanup;
4900 struct die_reader_specs reader;
4901 const gdb_byte *info_ptr;
4902 struct die_info *comp_unit_die;
4903 int has_children;
4904
4905 /* Verify we can do the following downcast, and that we have the
4906 data we need. */
4907 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4908 sig_type = (struct signatured_type *) this_cu;
4909 gdb_assert (sig_type->dwo_unit != NULL);
4910
4911 cleanups = make_cleanup (null_cleanup, NULL);
4912
4913 gdb_assert (this_cu->cu == NULL);
4914 cu = xmalloc (sizeof (*cu));
4915 init_one_comp_unit (cu, this_cu);
4916 /* If an error occurs while loading, release our storage. */
4917 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4918
4919 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4920 0 /* abbrev_table_provided */,
4921 NULL /* stub_comp_unit_die */,
4922 sig_type->dwo_unit->dwo_file->comp_dir,
4923 &reader, &info_ptr,
4924 &comp_unit_die, &has_children) == 0)
4925 {
4926 /* Dummy die. */
4927 do_cleanups (cleanups);
4928 return;
4929 }
4930
4931 /* All the "real" work is done here. */
4932 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4933
4934 /* This duplicates some code in init_cutu_and_read_dies,
4935 but the alternative is making the latter more complex.
4936 This function is only for the special case of using DWO files directly:
4937 no point in overly complicating the general case just to handle this. */
4938 if (keep)
4939 {
4940 /* We've successfully allocated this compilation unit. Let our
4941 caller clean it up when finished with it. */
4942 discard_cleanups (free_cu_cleanup);
4943
4944 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4945 So we have to manually free the abbrev table. */
4946 dwarf2_free_abbrev_table (cu);
4947
4948 /* Link this CU into read_in_chain. */
4949 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4950 dwarf2_per_objfile->read_in_chain = this_cu;
4951 }
4952 else
4953 do_cleanups (free_cu_cleanup);
4954
4955 do_cleanups (cleanups);
4956}
4957
fd820528 4958/* Initialize a CU (or TU) and read its DIEs.
3019eac3 4959 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 4960
f4dc4d17
DE
4961 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4962 Otherwise the table specified in the comp unit header is read in and used.
4963 This is an optimization for when we already have the abbrev table.
4964
dee91e82
DE
4965 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4966 Otherwise, a new CU is allocated with xmalloc.
4967
4968 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4969 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4970
4971 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 4972 linker) then DIE_READER_FUNC will not get called. */
aaa75496 4973
70221824 4974static void
fd820528 4975init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 4976 struct abbrev_table *abbrev_table,
fd820528
DE
4977 int use_existing_cu, int keep,
4978 die_reader_func_ftype *die_reader_func,
4979 void *data)
c906108c 4980{
dee91e82 4981 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 4982 struct dwarf2_section_info *section = this_cu->section;
3019eac3 4983 bfd *abfd = section->asection->owner;
dee91e82 4984 struct dwarf2_cu *cu;
d521ce57 4985 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 4986 struct die_reader_specs reader;
d85a05f0 4987 struct die_info *comp_unit_die;
dee91e82 4988 int has_children;
d85a05f0 4989 struct attribute *attr;
365156ad 4990 struct cleanup *cleanups, *free_cu_cleanup = NULL;
dee91e82 4991 struct signatured_type *sig_type = NULL;
4bdcc0c1 4992 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
4993 /* Non-zero if CU currently points to a DWO file and we need to
4994 reread it. When this happens we need to reread the skeleton die
a2ce51a0 4995 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 4996 int rereading_dwo_cu = 0;
c906108c 4997
09406207
DE
4998 if (dwarf2_die_debug)
4999 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5000 this_cu->is_debug_types ? "type" : "comp",
5001 this_cu->offset.sect_off);
5002
dee91e82
DE
5003 if (use_existing_cu)
5004 gdb_assert (keep);
23745b47 5005
a2ce51a0
DE
5006 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5007 file (instead of going through the stub), short-circuit all of this. */
5008 if (this_cu->reading_dwo_directly)
5009 {
5010 /* Narrow down the scope of possibilities to have to understand. */
5011 gdb_assert (this_cu->is_debug_types);
5012 gdb_assert (abbrev_table == NULL);
5013 gdb_assert (!use_existing_cu);
5014 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5015 return;
5016 }
5017
dee91e82
DE
5018 cleanups = make_cleanup (null_cleanup, NULL);
5019
5020 /* This is cheap if the section is already read in. */
5021 dwarf2_read_section (objfile, section);
5022
5023 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
36586728
TT
5024
5025 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
5026
5027 if (use_existing_cu && this_cu->cu != NULL)
5028 {
5029 cu = this_cu->cu;
42e7ad6c
DE
5030
5031 /* If this CU is from a DWO file we need to start over, we need to
5032 refetch the attributes from the skeleton CU.
5033 This could be optimized by retrieving those attributes from when we
5034 were here the first time: the previous comp_unit_die was stored in
5035 comp_unit_obstack. But there's no data yet that we need this
5036 optimization. */
5037 if (cu->dwo_unit != NULL)
5038 rereading_dwo_cu = 1;
dee91e82
DE
5039 }
5040 else
5041 {
5042 /* If !use_existing_cu, this_cu->cu must be NULL. */
5043 gdb_assert (this_cu->cu == NULL);
5044
5045 cu = xmalloc (sizeof (*cu));
5046 init_one_comp_unit (cu, this_cu);
5047
5048 /* If an error occurs while loading, release our storage. */
365156ad 5049 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 5050 }
dee91e82 5051
b0c7bfa9 5052 /* Get the header. */
42e7ad6c
DE
5053 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5054 {
5055 /* We already have the header, there's no need to read it in again. */
5056 info_ptr += cu->header.first_die_offset.cu_off;
5057 }
5058 else
5059 {
3019eac3 5060 if (this_cu->is_debug_types)
dee91e82
DE
5061 {
5062 ULONGEST signature;
42e7ad6c 5063 cu_offset type_offset_in_tu;
dee91e82 5064
4bdcc0c1
DE
5065 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5066 abbrev_section, info_ptr,
42e7ad6c
DE
5067 &signature,
5068 &type_offset_in_tu);
dee91e82 5069
42e7ad6c
DE
5070 /* Since per_cu is the first member of struct signatured_type,
5071 we can go from a pointer to one to a pointer to the other. */
5072 sig_type = (struct signatured_type *) this_cu;
5073 gdb_assert (sig_type->signature == signature);
5074 gdb_assert (sig_type->type_offset_in_tu.cu_off
5075 == type_offset_in_tu.cu_off);
dee91e82
DE
5076 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5077
42e7ad6c
DE
5078 /* LENGTH has not been set yet for type units if we're
5079 using .gdb_index. */
1ce1cefd 5080 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
5081
5082 /* Establish the type offset that can be used to lookup the type. */
5083 sig_type->type_offset_in_section.sect_off =
5084 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
5085 }
5086 else
5087 {
4bdcc0c1
DE
5088 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5089 abbrev_section,
5090 info_ptr, 0);
dee91e82
DE
5091
5092 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 5093 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
5094 }
5095 }
10b3939b 5096
6caca83c 5097 /* Skip dummy compilation units. */
dee91e82 5098 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
5099 || peek_abbrev_code (abfd, info_ptr) == 0)
5100 {
dee91e82 5101 do_cleanups (cleanups);
21b2bd31 5102 return;
6caca83c
CC
5103 }
5104
433df2d4
DE
5105 /* If we don't have them yet, read the abbrevs for this compilation unit.
5106 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
5107 done. Note that it's important that if the CU had an abbrev table
5108 on entry we don't free it when we're done: Somewhere up the call stack
5109 it may be in use. */
f4dc4d17
DE
5110 if (abbrev_table != NULL)
5111 {
5112 gdb_assert (cu->abbrev_table == NULL);
5113 gdb_assert (cu->header.abbrev_offset.sect_off
5114 == abbrev_table->offset.sect_off);
5115 cu->abbrev_table = abbrev_table;
5116 }
5117 else if (cu->abbrev_table == NULL)
dee91e82 5118 {
4bdcc0c1 5119 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
5120 make_cleanup (dwarf2_free_abbrev_table, cu);
5121 }
42e7ad6c
DE
5122 else if (rereading_dwo_cu)
5123 {
5124 dwarf2_free_abbrev_table (cu);
5125 dwarf2_read_abbrevs (cu, abbrev_section);
5126 }
af703f96 5127
dee91e82 5128 /* Read the top level CU/TU die. */
3019eac3 5129 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 5130 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 5131
b0c7bfa9
DE
5132 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5133 from the DWO file.
5134 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5135 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3
DE
5136 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5137 if (attr)
5138 {
3019eac3 5139 struct dwo_unit *dwo_unit;
b0c7bfa9 5140 struct die_info *dwo_comp_unit_die;
3019eac3
DE
5141
5142 if (has_children)
6a506a2d
DE
5143 {
5144 complaint (&symfile_complaints,
5145 _("compilation unit with DW_AT_GNU_dwo_name"
5146 " has children (offset 0x%x) [in module %s]"),
5147 this_cu->offset.sect_off, bfd_get_filename (abfd));
5148 }
b0c7bfa9 5149 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 5150 if (dwo_unit != NULL)
3019eac3 5151 {
6a506a2d
DE
5152 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5153 abbrev_table != NULL,
a2ce51a0 5154 comp_unit_die, NULL,
6a506a2d
DE
5155 &reader, &info_ptr,
5156 &dwo_comp_unit_die, &has_children) == 0)
5157 {
5158 /* Dummy die. */
5159 do_cleanups (cleanups);
5160 return;
5161 }
5162 comp_unit_die = dwo_comp_unit_die;
5163 }
5164 else
5165 {
5166 /* Yikes, we couldn't find the rest of the DIE, we only have
5167 the stub. A complaint has already been logged. There's
5168 not much more we can do except pass on the stub DIE to
5169 die_reader_func. We don't want to throw an error on bad
5170 debug info. */
3019eac3
DE
5171 }
5172 }
5173
b0c7bfa9 5174 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
5175 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5176
b0c7bfa9 5177 /* Done, clean up. */
365156ad 5178 if (free_cu_cleanup != NULL)
348e048f 5179 {
365156ad
TT
5180 if (keep)
5181 {
5182 /* We've successfully allocated this compilation unit. Let our
5183 caller clean it up when finished with it. */
5184 discard_cleanups (free_cu_cleanup);
dee91e82 5185
365156ad
TT
5186 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5187 So we have to manually free the abbrev table. */
5188 dwarf2_free_abbrev_table (cu);
dee91e82 5189
365156ad
TT
5190 /* Link this CU into read_in_chain. */
5191 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5192 dwarf2_per_objfile->read_in_chain = this_cu;
5193 }
5194 else
5195 do_cleanups (free_cu_cleanup);
348e048f 5196 }
365156ad
TT
5197
5198 do_cleanups (cleanups);
dee91e82
DE
5199}
5200
3019eac3
DE
5201/* Read CU/TU THIS_CU in section SECTION,
5202 but do not follow DW_AT_GNU_dwo_name if present.
80626a55
DE
5203 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5204 to have already done the lookup to find the DWO/DWP file).
dee91e82
DE
5205
5206 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 5207 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
5208
5209 We fill in THIS_CU->length.
5210
5211 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5212 linker) then DIE_READER_FUNC will not get called.
5213
5214 THIS_CU->cu is always freed when done.
3019eac3
DE
5215 This is done in order to not leave THIS_CU->cu in a state where we have
5216 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
5217
5218static void
5219init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5220 struct dwarf2_section_info *abbrev_section,
3019eac3 5221 struct dwo_file *dwo_file,
dee91e82
DE
5222 die_reader_func_ftype *die_reader_func,
5223 void *data)
5224{
5225 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 5226 struct dwarf2_section_info *section = this_cu->section;
3019eac3 5227 bfd *abfd = section->asection->owner;
dee91e82 5228 struct dwarf2_cu cu;
d521ce57 5229 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
5230 struct die_reader_specs reader;
5231 struct cleanup *cleanups;
5232 struct die_info *comp_unit_die;
5233 int has_children;
5234
09406207
DE
5235 if (dwarf2_die_debug)
5236 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5237 this_cu->is_debug_types ? "type" : "comp",
5238 this_cu->offset.sect_off);
5239
dee91e82
DE
5240 gdb_assert (this_cu->cu == NULL);
5241
dee91e82
DE
5242 /* This is cheap if the section is already read in. */
5243 dwarf2_read_section (objfile, section);
5244
5245 init_one_comp_unit (&cu, this_cu);
5246
5247 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5248
5249 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
5250 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5251 abbrev_section, info_ptr,
3019eac3 5252 this_cu->is_debug_types);
dee91e82 5253
1ce1cefd 5254 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
5255
5256 /* Skip dummy compilation units. */
5257 if (info_ptr >= begin_info_ptr + this_cu->length
5258 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 5259 {
dee91e82 5260 do_cleanups (cleanups);
21b2bd31 5261 return;
93311388 5262 }
72bf9492 5263
dee91e82
DE
5264 dwarf2_read_abbrevs (&cu, abbrev_section);
5265 make_cleanup (dwarf2_free_abbrev_table, &cu);
5266
3019eac3 5267 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
5268 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5269
5270 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5271
5272 do_cleanups (cleanups);
5273}
5274
3019eac3
DE
5275/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5276 does not lookup the specified DWO file.
5277 This cannot be used to read DWO files.
dee91e82
DE
5278
5279 THIS_CU->cu is always freed when done.
3019eac3
DE
5280 This is done in order to not leave THIS_CU->cu in a state where we have
5281 to care whether it refers to the "main" CU or the DWO CU.
5282 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
5283
5284static void
5285init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5286 die_reader_func_ftype *die_reader_func,
5287 void *data)
5288{
5289 init_cutu_and_read_dies_no_follow (this_cu,
36586728 5290 get_abbrev_section_for_cu (this_cu),
3019eac3 5291 NULL,
dee91e82
DE
5292 die_reader_func, data);
5293}
0018ea6f
DE
5294\f
5295/* Type Unit Groups.
dee91e82 5296
0018ea6f
DE
5297 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5298 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5299 so that all types coming from the same compilation (.o file) are grouped
5300 together. A future step could be to put the types in the same symtab as
5301 the CU the types ultimately came from. */
ff013f42 5302
f4dc4d17
DE
5303static hashval_t
5304hash_type_unit_group (const void *item)
5305{
094b34ac 5306 const struct type_unit_group *tu_group = item;
f4dc4d17 5307
094b34ac 5308 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 5309}
348e048f
DE
5310
5311static int
f4dc4d17 5312eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 5313{
f4dc4d17
DE
5314 const struct type_unit_group *lhs = item_lhs;
5315 const struct type_unit_group *rhs = item_rhs;
348e048f 5316
094b34ac 5317 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 5318}
348e048f 5319
f4dc4d17
DE
5320/* Allocate a hash table for type unit groups. */
5321
5322static htab_t
5323allocate_type_unit_groups_table (void)
5324{
5325 return htab_create_alloc_ex (3,
5326 hash_type_unit_group,
5327 eq_type_unit_group,
5328 NULL,
5329 &dwarf2_per_objfile->objfile->objfile_obstack,
5330 hashtab_obstack_allocate,
5331 dummy_obstack_deallocate);
5332}
dee91e82 5333
f4dc4d17
DE
5334/* Type units that don't have DW_AT_stmt_list are grouped into their own
5335 partial symtabs. We combine several TUs per psymtab to not let the size
5336 of any one psymtab grow too big. */
5337#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5338#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 5339
094b34ac 5340/* Helper routine for get_type_unit_group.
f4dc4d17
DE
5341 Create the type_unit_group object used to hold one or more TUs. */
5342
5343static struct type_unit_group *
094b34ac 5344create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17
DE
5345{
5346 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 5347 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 5348 struct type_unit_group *tu_group;
f4dc4d17
DE
5349
5350 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5351 struct type_unit_group);
094b34ac 5352 per_cu = &tu_group->per_cu;
f4dc4d17 5353 per_cu->objfile = objfile;
f4dc4d17 5354
094b34ac
DE
5355 if (dwarf2_per_objfile->using_index)
5356 {
5357 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5358 struct dwarf2_per_cu_quick_data);
094b34ac
DE
5359 }
5360 else
5361 {
5362 unsigned int line_offset = line_offset_struct.sect_off;
5363 struct partial_symtab *pst;
5364 char *name;
5365
5366 /* Give the symtab a useful name for debug purposes. */
5367 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5368 name = xstrprintf ("<type_units_%d>",
5369 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5370 else
5371 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5372
5373 pst = create_partial_symtab (per_cu, name);
5374 pst->anonymous = 1;
f4dc4d17 5375
094b34ac
DE
5376 xfree (name);
5377 }
f4dc4d17 5378
094b34ac
DE
5379 tu_group->hash.dwo_unit = cu->dwo_unit;
5380 tu_group->hash.line_offset = line_offset_struct;
f4dc4d17
DE
5381
5382 return tu_group;
5383}
5384
094b34ac
DE
5385/* Look up the type_unit_group for type unit CU, and create it if necessary.
5386 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
5387
5388static struct type_unit_group *
ff39bb5e 5389get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17
DE
5390{
5391 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5392 struct type_unit_group *tu_group;
5393 void **slot;
5394 unsigned int line_offset;
5395 struct type_unit_group type_unit_group_for_lookup;
5396
5397 if (dwarf2_per_objfile->type_unit_groups == NULL)
5398 {
5399 dwarf2_per_objfile->type_unit_groups =
5400 allocate_type_unit_groups_table ();
5401 }
5402
5403 /* Do we need to create a new group, or can we use an existing one? */
5404
5405 if (stmt_list)
5406 {
5407 line_offset = DW_UNSND (stmt_list);
5408 ++tu_stats->nr_symtab_sharers;
5409 }
5410 else
5411 {
5412 /* Ugh, no stmt_list. Rare, but we have to handle it.
5413 We can do various things here like create one group per TU or
5414 spread them over multiple groups to split up the expansion work.
5415 To avoid worst case scenarios (too many groups or too large groups)
5416 we, umm, group them in bunches. */
5417 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5418 | (tu_stats->nr_stmt_less_type_units
5419 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5420 ++tu_stats->nr_stmt_less_type_units;
5421 }
5422
094b34ac
DE
5423 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5424 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
f4dc4d17
DE
5425 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5426 &type_unit_group_for_lookup, INSERT);
5427 if (*slot != NULL)
5428 {
5429 tu_group = *slot;
5430 gdb_assert (tu_group != NULL);
5431 }
5432 else
5433 {
5434 sect_offset line_offset_struct;
5435
5436 line_offset_struct.sect_off = line_offset;
094b34ac 5437 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
5438 *slot = tu_group;
5439 ++tu_stats->nr_symtabs;
5440 }
5441
5442 return tu_group;
5443}
5444
5445/* Struct used to sort TUs by their abbreviation table offset. */
5446
5447struct tu_abbrev_offset
5448{
5449 struct signatured_type *sig_type;
5450 sect_offset abbrev_offset;
5451};
5452
5453/* Helper routine for build_type_unit_groups, passed to qsort. */
5454
5455static int
5456sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5457{
5458 const struct tu_abbrev_offset * const *a = ap;
5459 const struct tu_abbrev_offset * const *b = bp;
5460 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5461 unsigned int boff = (*b)->abbrev_offset.sect_off;
5462
5463 return (aoff > boff) - (aoff < boff);
5464}
5465
5466/* A helper function to add a type_unit_group to a table. */
5467
5468static int
5469add_type_unit_group_to_table (void **slot, void *datum)
5470{
5471 struct type_unit_group *tu_group = *slot;
5472 struct type_unit_group ***datap = datum;
5473
5474 **datap = tu_group;
5475 ++*datap;
5476
5477 return 1;
5478}
5479
5480/* Efficiently read all the type units, calling init_cutu_and_read_dies on
5481 each one passing FUNC,DATA.
5482
5483 The efficiency is because we sort TUs by the abbrev table they use and
5484 only read each abbrev table once. In one program there are 200K TUs
5485 sharing 8K abbrev tables.
5486
5487 The main purpose of this function is to support building the
5488 dwarf2_per_objfile->type_unit_groups table.
5489 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5490 can collapse the search space by grouping them by stmt_list.
5491 The savings can be significant, in the same program from above the 200K TUs
5492 share 8K stmt_list tables.
5493
5494 FUNC is expected to call get_type_unit_group, which will create the
5495 struct type_unit_group if necessary and add it to
5496 dwarf2_per_objfile->type_unit_groups. */
5497
5498static void
5499build_type_unit_groups (die_reader_func_ftype *func, void *data)
5500{
5501 struct objfile *objfile = dwarf2_per_objfile->objfile;
5502 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5503 struct cleanup *cleanups;
5504 struct abbrev_table *abbrev_table;
5505 sect_offset abbrev_offset;
5506 struct tu_abbrev_offset *sorted_by_abbrev;
5507 struct type_unit_group **iter;
5508 int i;
5509
5510 /* It's up to the caller to not call us multiple times. */
5511 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5512
5513 if (dwarf2_per_objfile->n_type_units == 0)
5514 return;
5515
5516 /* TUs typically share abbrev tables, and there can be way more TUs than
5517 abbrev tables. Sort by abbrev table to reduce the number of times we
5518 read each abbrev table in.
5519 Alternatives are to punt or to maintain a cache of abbrev tables.
5520 This is simpler and efficient enough for now.
5521
5522 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5523 symtab to use). Typically TUs with the same abbrev offset have the same
5524 stmt_list value too so in practice this should work well.
5525
5526 The basic algorithm here is:
5527
5528 sort TUs by abbrev table
5529 for each TU with same abbrev table:
5530 read abbrev table if first user
5531 read TU top level DIE
5532 [IWBN if DWO skeletons had DW_AT_stmt_list]
5533 call FUNC */
5534
5535 if (dwarf2_read_debug)
5536 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5537
5538 /* Sort in a separate table to maintain the order of all_type_units
5539 for .gdb_index: TU indices directly index all_type_units. */
5540 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5541 dwarf2_per_objfile->n_type_units);
5542 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5543 {
5544 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5545
5546 sorted_by_abbrev[i].sig_type = sig_type;
5547 sorted_by_abbrev[i].abbrev_offset =
8a0459fd 5548 read_abbrev_offset (sig_type->per_cu.section,
f4dc4d17
DE
5549 sig_type->per_cu.offset);
5550 }
5551 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5552 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5553 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5554
094b34ac
DE
5555 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5556 called any number of times, so we don't reset tu_stats here. */
5557
f4dc4d17
DE
5558 abbrev_offset.sect_off = ~(unsigned) 0;
5559 abbrev_table = NULL;
5560 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5561
5562 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5563 {
5564 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5565
5566 /* Switch to the next abbrev table if necessary. */
5567 if (abbrev_table == NULL
5568 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5569 {
5570 if (abbrev_table != NULL)
5571 {
5572 abbrev_table_free (abbrev_table);
5573 /* Reset to NULL in case abbrev_table_read_table throws
5574 an error: abbrev_table_free_cleanup will get called. */
5575 abbrev_table = NULL;
5576 }
5577 abbrev_offset = tu->abbrev_offset;
5578 abbrev_table =
5579 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5580 abbrev_offset);
5581 ++tu_stats->nr_uniq_abbrev_tables;
5582 }
5583
5584 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5585 func, data);
5586 }
5587
a2ce51a0
DE
5588 /* type_unit_groups can be NULL if there is an error in the debug info.
5589 Just create an empty table so the rest of gdb doesn't have to watch
5590 for this error case. */
5591 if (dwarf2_per_objfile->type_unit_groups == NULL)
5592 {
5593 dwarf2_per_objfile->type_unit_groups =
5594 allocate_type_unit_groups_table ();
5595 dwarf2_per_objfile->n_type_unit_groups = 0;
5596 }
5597
f4dc4d17
DE
5598 /* Create a vector of pointers to primary type units to make it easy to
5599 iterate over them and CUs. See dw2_get_primary_cu. */
5600 dwarf2_per_objfile->n_type_unit_groups =
5601 htab_elements (dwarf2_per_objfile->type_unit_groups);
5602 dwarf2_per_objfile->all_type_unit_groups =
5603 obstack_alloc (&objfile->objfile_obstack,
5604 dwarf2_per_objfile->n_type_unit_groups
5605 * sizeof (struct type_unit_group *));
5606 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5607 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5608 add_type_unit_group_to_table, &iter);
5609 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5610 == dwarf2_per_objfile->n_type_unit_groups);
5611
5612 do_cleanups (cleanups);
5613
5614 if (dwarf2_read_debug)
5615 {
5616 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5617 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5618 dwarf2_per_objfile->n_type_units);
5619 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5620 tu_stats->nr_uniq_abbrev_tables);
5621 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5622 tu_stats->nr_symtabs);
5623 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5624 tu_stats->nr_symtab_sharers);
5625 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5626 tu_stats->nr_stmt_less_type_units);
5627 }
5628}
0018ea6f
DE
5629\f
5630/* Partial symbol tables. */
5631
5632/* Create a psymtab named NAME and assign it to PER_CU.
5633
5634 The caller must fill in the following details:
5635 dirname, textlow, texthigh. */
5636
5637static struct partial_symtab *
5638create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5639{
5640 struct objfile *objfile = per_cu->objfile;
5641 struct partial_symtab *pst;
5642
5643 pst = start_psymtab_common (objfile, objfile->section_offsets,
5644 name, 0,
5645 objfile->global_psymbols.next,
5646 objfile->static_psymbols.next);
5647
5648 pst->psymtabs_addrmap_supported = 1;
5649
5650 /* This is the glue that links PST into GDB's symbol API. */
5651 pst->read_symtab_private = per_cu;
5652 pst->read_symtab = dwarf2_read_symtab;
5653 per_cu->v.psymtab = pst;
5654
5655 return pst;
5656}
5657
b93601f3
TT
5658/* The DATA object passed to process_psymtab_comp_unit_reader has this
5659 type. */
5660
5661struct process_psymtab_comp_unit_data
5662{
5663 /* True if we are reading a DW_TAG_partial_unit. */
5664
5665 int want_partial_unit;
5666
5667 /* The "pretend" language that is used if the CU doesn't declare a
5668 language. */
5669
5670 enum language pretend_language;
5671};
5672
0018ea6f
DE
5673/* die_reader_func for process_psymtab_comp_unit. */
5674
5675static void
5676process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 5677 const gdb_byte *info_ptr,
0018ea6f
DE
5678 struct die_info *comp_unit_die,
5679 int has_children,
5680 void *data)
5681{
5682 struct dwarf2_cu *cu = reader->cu;
5683 struct objfile *objfile = cu->objfile;
5684 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5685 struct attribute *attr;
5686 CORE_ADDR baseaddr;
5687 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5688 struct partial_symtab *pst;
5689 int has_pc_info;
5690 const char *filename;
b93601f3 5691 struct process_psymtab_comp_unit_data *info = data;
0018ea6f 5692
b93601f3 5693 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
5694 return;
5695
5696 gdb_assert (! per_cu->is_debug_types);
5697
b93601f3 5698 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
5699
5700 cu->list_in_scope = &file_symbols;
5701
5702 /* Allocate a new partial symbol table structure. */
5703 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5704 if (attr == NULL || !DW_STRING (attr))
5705 filename = "";
5706 else
5707 filename = DW_STRING (attr);
5708
5709 pst = create_partial_symtab (per_cu, filename);
5710
5711 /* This must be done before calling dwarf2_build_include_psymtabs. */
5712 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5713 if (attr != NULL)
5714 pst->dirname = DW_STRING (attr);
5715
5716 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5717
5718 dwarf2_find_base_address (comp_unit_die, cu);
5719
5720 /* Possibly set the default values of LOWPC and HIGHPC from
5721 `DW_AT_ranges'. */
5722 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5723 &best_highpc, cu, pst);
5724 if (has_pc_info == 1 && best_lowpc < best_highpc)
5725 /* Store the contiguous range if it is not empty; it can be empty for
5726 CUs with no code. */
5727 addrmap_set_empty (objfile->psymtabs_addrmap,
5728 best_lowpc + baseaddr,
5729 best_highpc + baseaddr - 1, pst);
5730
5731 /* Check if comp unit has_children.
5732 If so, read the rest of the partial symbols from this comp unit.
5733 If not, there's no more debug_info for this comp unit. */
5734 if (has_children)
5735 {
5736 struct partial_die_info *first_die;
5737 CORE_ADDR lowpc, highpc;
5738
5739 lowpc = ((CORE_ADDR) -1);
5740 highpc = ((CORE_ADDR) 0);
5741
5742 first_die = load_partial_dies (reader, info_ptr, 1);
5743
5744 scan_partial_symbols (first_die, &lowpc, &highpc,
5745 ! has_pc_info, cu);
5746
5747 /* If we didn't find a lowpc, set it to highpc to avoid
5748 complaints from `maint check'. */
5749 if (lowpc == ((CORE_ADDR) -1))
5750 lowpc = highpc;
5751
5752 /* If the compilation unit didn't have an explicit address range,
5753 then use the information extracted from its child dies. */
5754 if (! has_pc_info)
5755 {
5756 best_lowpc = lowpc;
5757 best_highpc = highpc;
5758 }
5759 }
5760 pst->textlow = best_lowpc + baseaddr;
5761 pst->texthigh = best_highpc + baseaddr;
5762
5763 pst->n_global_syms = objfile->global_psymbols.next -
5764 (objfile->global_psymbols.list + pst->globals_offset);
5765 pst->n_static_syms = objfile->static_psymbols.next -
5766 (objfile->static_psymbols.list + pst->statics_offset);
5767 sort_pst_symbols (objfile, pst);
5768
5769 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5770 {
5771 int i;
5772 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5773 struct dwarf2_per_cu_data *iter;
5774
5775 /* Fill in 'dependencies' here; we fill in 'users' in a
5776 post-pass. */
5777 pst->number_of_dependencies = len;
5778 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5779 len * sizeof (struct symtab *));
5780 for (i = 0;
5781 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5782 i, iter);
5783 ++i)
5784 pst->dependencies[i] = iter->v.psymtab;
5785
5786 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5787 }
5788
5789 /* Get the list of files included in the current compilation unit,
5790 and build a psymtab for each of them. */
5791 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5792
5793 if (dwarf2_read_debug)
5794 {
5795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5796
5797 fprintf_unfiltered (gdb_stdlog,
5798 "Psymtab for %s unit @0x%x: %s - %s"
5799 ", %d global, %d static syms\n",
5800 per_cu->is_debug_types ? "type" : "comp",
5801 per_cu->offset.sect_off,
5802 paddress (gdbarch, pst->textlow),
5803 paddress (gdbarch, pst->texthigh),
5804 pst->n_global_syms, pst->n_static_syms);
5805 }
5806}
5807
5808/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5809 Process compilation unit THIS_CU for a psymtab. */
5810
5811static void
5812process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
5813 int want_partial_unit,
5814 enum language pretend_language)
0018ea6f 5815{
b93601f3
TT
5816 struct process_psymtab_comp_unit_data info;
5817
0018ea6f
DE
5818 /* If this compilation unit was already read in, free the
5819 cached copy in order to read it in again. This is
5820 necessary because we skipped some symbols when we first
5821 read in the compilation unit (see load_partial_dies).
5822 This problem could be avoided, but the benefit is unclear. */
5823 if (this_cu->cu != NULL)
5824 free_one_cached_comp_unit (this_cu);
5825
5826 gdb_assert (! this_cu->is_debug_types);
b93601f3
TT
5827 info.want_partial_unit = want_partial_unit;
5828 info.pretend_language = pretend_language;
0018ea6f
DE
5829 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5830 process_psymtab_comp_unit_reader,
b93601f3 5831 &info);
0018ea6f
DE
5832
5833 /* Age out any secondary CUs. */
5834 age_cached_comp_units ();
5835}
f4dc4d17
DE
5836
5837/* Reader function for build_type_psymtabs. */
5838
5839static void
5840build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 5841 const gdb_byte *info_ptr,
f4dc4d17
DE
5842 struct die_info *type_unit_die,
5843 int has_children,
5844 void *data)
5845{
5846 struct objfile *objfile = dwarf2_per_objfile->objfile;
5847 struct dwarf2_cu *cu = reader->cu;
5848 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 5849 struct signatured_type *sig_type;
f4dc4d17
DE
5850 struct type_unit_group *tu_group;
5851 struct attribute *attr;
5852 struct partial_die_info *first_die;
5853 CORE_ADDR lowpc, highpc;
5854 struct partial_symtab *pst;
5855
5856 gdb_assert (data == NULL);
0186c6a7
DE
5857 gdb_assert (per_cu->is_debug_types);
5858 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
5859
5860 if (! has_children)
5861 return;
5862
5863 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 5864 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 5865
0186c6a7 5866 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
5867
5868 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5869 cu->list_in_scope = &file_symbols;
5870 pst = create_partial_symtab (per_cu, "");
5871 pst->anonymous = 1;
5872
5873 first_die = load_partial_dies (reader, info_ptr, 1);
5874
5875 lowpc = (CORE_ADDR) -1;
5876 highpc = (CORE_ADDR) 0;
5877 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5878
5879 pst->n_global_syms = objfile->global_psymbols.next -
5880 (objfile->global_psymbols.list + pst->globals_offset);
5881 pst->n_static_syms = objfile->static_psymbols.next -
5882 (objfile->static_psymbols.list + pst->statics_offset);
5c80ed9d 5883 sort_pst_symbols (objfile, pst);
f4dc4d17
DE
5884}
5885
5886/* Traversal function for build_type_psymtabs. */
5887
5888static int
5889build_type_psymtab_dependencies (void **slot, void *info)
5890{
5891 struct objfile *objfile = dwarf2_per_objfile->objfile;
5892 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 5893 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 5894 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
5895 int len = VEC_length (sig_type_ptr, tu_group->tus);
5896 struct signatured_type *iter;
f4dc4d17
DE
5897 int i;
5898
5899 gdb_assert (len > 0);
0186c6a7 5900 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
5901
5902 pst->number_of_dependencies = len;
5903 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5904 len * sizeof (struct psymtab *));
5905 for (i = 0;
0186c6a7 5906 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
5907 ++i)
5908 {
0186c6a7
DE
5909 gdb_assert (iter->per_cu.is_debug_types);
5910 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 5911 iter->type_unit_group = tu_group;
f4dc4d17
DE
5912 }
5913
0186c6a7 5914 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
5915
5916 return 1;
5917}
5918
5919/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5920 Build partial symbol tables for the .debug_types comp-units. */
5921
5922static void
5923build_type_psymtabs (struct objfile *objfile)
5924{
0e50663e 5925 if (! create_all_type_units (objfile))
348e048f
DE
5926 return;
5927
f4dc4d17
DE
5928 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5929
5930 /* Now that all TUs have been processed we can fill in the dependencies. */
5931 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5932 build_type_psymtab_dependencies, NULL);
348e048f
DE
5933}
5934
60606b2c
TT
5935/* A cleanup function that clears objfile's psymtabs_addrmap field. */
5936
5937static void
5938psymtabs_addrmap_cleanup (void *o)
5939{
5940 struct objfile *objfile = o;
ec61707d 5941
60606b2c
TT
5942 objfile->psymtabs_addrmap = NULL;
5943}
5944
95554aad
TT
5945/* Compute the 'user' field for each psymtab in OBJFILE. */
5946
5947static void
5948set_partial_user (struct objfile *objfile)
5949{
5950 int i;
5951
5952 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5953 {
5954 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5955 struct partial_symtab *pst = per_cu->v.psymtab;
5956 int j;
5957
36586728
TT
5958 if (pst == NULL)
5959 continue;
5960
95554aad
TT
5961 for (j = 0; j < pst->number_of_dependencies; ++j)
5962 {
5963 /* Set the 'user' field only if it is not already set. */
5964 if (pst->dependencies[j]->user == NULL)
5965 pst->dependencies[j]->user = pst;
5966 }
5967 }
5968}
5969
93311388
DE
5970/* Build the partial symbol table by doing a quick pass through the
5971 .debug_info and .debug_abbrev sections. */
72bf9492 5972
93311388 5973static void
c67a9c90 5974dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 5975{
60606b2c
TT
5976 struct cleanup *back_to, *addrmap_cleanup;
5977 struct obstack temp_obstack;
21b2bd31 5978 int i;
93311388 5979
45cfd468
DE
5980 if (dwarf2_read_debug)
5981 {
5982 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5983 objfile->name);
5984 }
5985
98bfdba5
PA
5986 dwarf2_per_objfile->reading_partial_symbols = 1;
5987
be391dca 5988 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 5989
93311388
DE
5990 /* Any cached compilation units will be linked by the per-objfile
5991 read_in_chain. Make sure to free them when we're done. */
5992 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 5993
348e048f
DE
5994 build_type_psymtabs (objfile);
5995
93311388 5996 create_all_comp_units (objfile);
c906108c 5997
60606b2c
TT
5998 /* Create a temporary address map on a temporary obstack. We later
5999 copy this to the final obstack. */
6000 obstack_init (&temp_obstack);
6001 make_cleanup_obstack_free (&temp_obstack);
6002 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6003 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 6004
21b2bd31 6005 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 6006 {
21b2bd31 6007 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 6008
b93601f3 6009 process_psymtab_comp_unit (per_cu, 0, language_minimal);
c906108c 6010 }
ff013f42 6011
95554aad
TT
6012 set_partial_user (objfile);
6013
ff013f42
JK
6014 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6015 &objfile->objfile_obstack);
60606b2c 6016 discard_cleanups (addrmap_cleanup);
ff013f42 6017
ae038cb0 6018 do_cleanups (back_to);
45cfd468
DE
6019
6020 if (dwarf2_read_debug)
6021 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6022 objfile->name);
ae038cb0
DJ
6023}
6024
3019eac3 6025/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
6026
6027static void
dee91e82 6028load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6029 const gdb_byte *info_ptr,
dee91e82
DE
6030 struct die_info *comp_unit_die,
6031 int has_children,
6032 void *data)
ae038cb0 6033{
dee91e82 6034 struct dwarf2_cu *cu = reader->cu;
ae038cb0 6035
95554aad 6036 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 6037
ae038cb0
DJ
6038 /* Check if comp unit has_children.
6039 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 6040 If not, there's no more debug_info for this comp unit. */
d85a05f0 6041 if (has_children)
dee91e82
DE
6042 load_partial_dies (reader, info_ptr, 0);
6043}
98bfdba5 6044
dee91e82
DE
6045/* Load the partial DIEs for a secondary CU into memory.
6046 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 6047
dee91e82
DE
6048static void
6049load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6050{
f4dc4d17
DE
6051 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6052 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
6053}
6054
ae038cb0 6055static void
36586728
TT
6056read_comp_units_from_section (struct objfile *objfile,
6057 struct dwarf2_section_info *section,
6058 unsigned int is_dwz,
6059 int *n_allocated,
6060 int *n_comp_units,
6061 struct dwarf2_per_cu_data ***all_comp_units)
ae038cb0 6062{
d521ce57 6063 const gdb_byte *info_ptr;
36586728 6064 bfd *abfd = section->asection->owner;
be391dca 6065
bf6af496
DE
6066 if (dwarf2_read_debug)
6067 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6068 section->asection->name, bfd_get_filename (abfd));
6069
36586728 6070 dwarf2_read_section (objfile, section);
ae038cb0 6071
36586728 6072 info_ptr = section->buffer;
6e70227d 6073
36586728 6074 while (info_ptr < section->buffer + section->size)
ae038cb0 6075 {
c764a876 6076 unsigned int length, initial_length_size;
ae038cb0 6077 struct dwarf2_per_cu_data *this_cu;
b64f50a1 6078 sect_offset offset;
ae038cb0 6079
36586728 6080 offset.sect_off = info_ptr - section->buffer;
ae038cb0
DJ
6081
6082 /* Read just enough information to find out where the next
6083 compilation unit is. */
36586728 6084 length = read_initial_length (abfd, info_ptr, &initial_length_size);
ae038cb0
DJ
6085
6086 /* Save the compilation unit for later lookup. */
6087 this_cu = obstack_alloc (&objfile->objfile_obstack,
6088 sizeof (struct dwarf2_per_cu_data));
6089 memset (this_cu, 0, sizeof (*this_cu));
6090 this_cu->offset = offset;
c764a876 6091 this_cu->length = length + initial_length_size;
36586728 6092 this_cu->is_dwz = is_dwz;
9291a0cd 6093 this_cu->objfile = objfile;
8a0459fd 6094 this_cu->section = section;
ae038cb0 6095
36586728 6096 if (*n_comp_units == *n_allocated)
ae038cb0 6097 {
36586728
TT
6098 *n_allocated *= 2;
6099 *all_comp_units = xrealloc (*all_comp_units,
6100 *n_allocated
6101 * sizeof (struct dwarf2_per_cu_data *));
ae038cb0 6102 }
36586728
TT
6103 (*all_comp_units)[*n_comp_units] = this_cu;
6104 ++*n_comp_units;
ae038cb0
DJ
6105
6106 info_ptr = info_ptr + this_cu->length;
6107 }
36586728
TT
6108}
6109
6110/* Create a list of all compilation units in OBJFILE.
6111 This is only done for -readnow and building partial symtabs. */
6112
6113static void
6114create_all_comp_units (struct objfile *objfile)
6115{
6116 int n_allocated;
6117 int n_comp_units;
6118 struct dwarf2_per_cu_data **all_comp_units;
4db1a1dc 6119 struct dwz_file *dwz;
36586728
TT
6120
6121 n_comp_units = 0;
6122 n_allocated = 10;
6123 all_comp_units = xmalloc (n_allocated
6124 * sizeof (struct dwarf2_per_cu_data *));
6125
6126 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6127 &n_allocated, &n_comp_units, &all_comp_units);
6128
4db1a1dc
TT
6129 dwz = dwarf2_get_dwz_file ();
6130 if (dwz != NULL)
6131 read_comp_units_from_section (objfile, &dwz->info, 1,
6132 &n_allocated, &n_comp_units,
6133 &all_comp_units);
ae038cb0
DJ
6134
6135 dwarf2_per_objfile->all_comp_units
6136 = obstack_alloc (&objfile->objfile_obstack,
6137 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6138 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6139 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6140 xfree (all_comp_units);
6141 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
6142}
6143
5734ee8b
DJ
6144/* Process all loaded DIEs for compilation unit CU, starting at
6145 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6146 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6147 DW_AT_ranges). If NEED_PC is set, then this function will set
6148 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6149 and record the covered ranges in the addrmap. */
c906108c 6150
72bf9492
DJ
6151static void
6152scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 6153 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 6154{
72bf9492 6155 struct partial_die_info *pdi;
c906108c 6156
91c24f0a
DC
6157 /* Now, march along the PDI's, descending into ones which have
6158 interesting children but skipping the children of the other ones,
6159 until we reach the end of the compilation unit. */
c906108c 6160
72bf9492 6161 pdi = first_die;
91c24f0a 6162
72bf9492
DJ
6163 while (pdi != NULL)
6164 {
6165 fixup_partial_die (pdi, cu);
c906108c 6166
f55ee35c 6167 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
6168 children, so we need to look at them. Ditto for anonymous
6169 enums. */
933c6fe4 6170
72bf9492 6171 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
6172 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6173 || pdi->tag == DW_TAG_imported_unit)
c906108c 6174 {
72bf9492 6175 switch (pdi->tag)
c906108c
SS
6176 {
6177 case DW_TAG_subprogram:
5734ee8b 6178 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 6179 break;
72929c62 6180 case DW_TAG_constant:
c906108c
SS
6181 case DW_TAG_variable:
6182 case DW_TAG_typedef:
91c24f0a 6183 case DW_TAG_union_type:
72bf9492 6184 if (!pdi->is_declaration)
63d06c5c 6185 {
72bf9492 6186 add_partial_symbol (pdi, cu);
63d06c5c
DC
6187 }
6188 break;
c906108c 6189 case DW_TAG_class_type:
680b30c7 6190 case DW_TAG_interface_type:
c906108c 6191 case DW_TAG_structure_type:
72bf9492 6192 if (!pdi->is_declaration)
c906108c 6193 {
72bf9492 6194 add_partial_symbol (pdi, cu);
c906108c
SS
6195 }
6196 break;
91c24f0a 6197 case DW_TAG_enumeration_type:
72bf9492
DJ
6198 if (!pdi->is_declaration)
6199 add_partial_enumeration (pdi, cu);
c906108c
SS
6200 break;
6201 case DW_TAG_base_type:
a02abb62 6202 case DW_TAG_subrange_type:
c906108c 6203 /* File scope base type definitions are added to the partial
c5aa993b 6204 symbol table. */
72bf9492 6205 add_partial_symbol (pdi, cu);
c906108c 6206 break;
d9fa45fe 6207 case DW_TAG_namespace:
5734ee8b 6208 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 6209 break;
5d7cb8df
JK
6210 case DW_TAG_module:
6211 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6212 break;
95554aad
TT
6213 case DW_TAG_imported_unit:
6214 {
6215 struct dwarf2_per_cu_data *per_cu;
6216
f4dc4d17
DE
6217 /* For now we don't handle imported units in type units. */
6218 if (cu->per_cu->is_debug_types)
6219 {
6220 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6221 " supported in type units [in module %s]"),
6222 cu->objfile->name);
6223 }
6224
95554aad 6225 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
36586728 6226 pdi->is_dwz,
95554aad
TT
6227 cu->objfile);
6228
6229 /* Go read the partial unit, if needed. */
6230 if (per_cu->v.psymtab == NULL)
b93601f3 6231 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 6232
f4dc4d17 6233 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 6234 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
6235 }
6236 break;
c906108c
SS
6237 default:
6238 break;
6239 }
6240 }
6241
72bf9492
DJ
6242 /* If the die has a sibling, skip to the sibling. */
6243
6244 pdi = pdi->die_sibling;
6245 }
6246}
6247
6248/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 6249
72bf9492 6250 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
6251 name is concatenated with "::" and the partial DIE's name. For
6252 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
6253 Enumerators are an exception; they use the scope of their parent
6254 enumeration type, i.e. the name of the enumeration type is not
6255 prepended to the enumerator.
91c24f0a 6256
72bf9492
DJ
6257 There are two complexities. One is DW_AT_specification; in this
6258 case "parent" means the parent of the target of the specification,
6259 instead of the direct parent of the DIE. The other is compilers
6260 which do not emit DW_TAG_namespace; in this case we try to guess
6261 the fully qualified name of structure types from their members'
6262 linkage names. This must be done using the DIE's children rather
6263 than the children of any DW_AT_specification target. We only need
6264 to do this for structures at the top level, i.e. if the target of
6265 any DW_AT_specification (if any; otherwise the DIE itself) does not
6266 have a parent. */
6267
6268/* Compute the scope prefix associated with PDI's parent, in
6269 compilation unit CU. The result will be allocated on CU's
6270 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6271 field. NULL is returned if no prefix is necessary. */
15d034d0 6272static const char *
72bf9492
DJ
6273partial_die_parent_scope (struct partial_die_info *pdi,
6274 struct dwarf2_cu *cu)
6275{
15d034d0 6276 const char *grandparent_scope;
72bf9492 6277 struct partial_die_info *parent, *real_pdi;
91c24f0a 6278
72bf9492
DJ
6279 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6280 then this means the parent of the specification DIE. */
6281
6282 real_pdi = pdi;
72bf9492 6283 while (real_pdi->has_specification)
36586728
TT
6284 real_pdi = find_partial_die (real_pdi->spec_offset,
6285 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
6286
6287 parent = real_pdi->die_parent;
6288 if (parent == NULL)
6289 return NULL;
6290
6291 if (parent->scope_set)
6292 return parent->scope;
6293
6294 fixup_partial_die (parent, cu);
6295
10b3939b 6296 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 6297
acebe513
UW
6298 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6299 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6300 Work around this problem here. */
6301 if (cu->language == language_cplus
6e70227d 6302 && parent->tag == DW_TAG_namespace
acebe513
UW
6303 && strcmp (parent->name, "::") == 0
6304 && grandparent_scope == NULL)
6305 {
6306 parent->scope = NULL;
6307 parent->scope_set = 1;
6308 return NULL;
6309 }
6310
9c6c53f7
SA
6311 if (pdi->tag == DW_TAG_enumerator)
6312 /* Enumerators should not get the name of the enumeration as a prefix. */
6313 parent->scope = grandparent_scope;
6314 else if (parent->tag == DW_TAG_namespace
f55ee35c 6315 || parent->tag == DW_TAG_module
72bf9492
DJ
6316 || parent->tag == DW_TAG_structure_type
6317 || parent->tag == DW_TAG_class_type
680b30c7 6318 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
6319 || parent->tag == DW_TAG_union_type
6320 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
6321 {
6322 if (grandparent_scope == NULL)
6323 parent->scope = parent->name;
6324 else
3e43a32a
MS
6325 parent->scope = typename_concat (&cu->comp_unit_obstack,
6326 grandparent_scope,
f55ee35c 6327 parent->name, 0, cu);
72bf9492 6328 }
72bf9492
DJ
6329 else
6330 {
6331 /* FIXME drow/2004-04-01: What should we be doing with
6332 function-local names? For partial symbols, we should probably be
6333 ignoring them. */
6334 complaint (&symfile_complaints,
e2e0b3e5 6335 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 6336 parent->tag, pdi->offset.sect_off);
72bf9492 6337 parent->scope = grandparent_scope;
c906108c
SS
6338 }
6339
72bf9492
DJ
6340 parent->scope_set = 1;
6341 return parent->scope;
6342}
6343
6344/* Return the fully scoped name associated with PDI, from compilation unit
6345 CU. The result will be allocated with malloc. */
4568ecf9 6346
72bf9492
DJ
6347static char *
6348partial_die_full_name (struct partial_die_info *pdi,
6349 struct dwarf2_cu *cu)
6350{
15d034d0 6351 const char *parent_scope;
72bf9492 6352
98bfdba5
PA
6353 /* If this is a template instantiation, we can not work out the
6354 template arguments from partial DIEs. So, unfortunately, we have
6355 to go through the full DIEs. At least any work we do building
6356 types here will be reused if full symbols are loaded later. */
6357 if (pdi->has_template_arguments)
6358 {
6359 fixup_partial_die (pdi, cu);
6360
6361 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6362 {
6363 struct die_info *die;
6364 struct attribute attr;
6365 struct dwarf2_cu *ref_cu = cu;
6366
b64f50a1 6367 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
6368 attr.name = 0;
6369 attr.form = DW_FORM_ref_addr;
4568ecf9 6370 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
6371 die = follow_die_ref (NULL, &attr, &ref_cu);
6372
6373 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6374 }
6375 }
6376
72bf9492
DJ
6377 parent_scope = partial_die_parent_scope (pdi, cu);
6378 if (parent_scope == NULL)
6379 return NULL;
6380 else
f55ee35c 6381 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
6382}
6383
6384static void
72bf9492 6385add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 6386{
e7c27a73 6387 struct objfile *objfile = cu->objfile;
c906108c 6388 CORE_ADDR addr = 0;
15d034d0 6389 const char *actual_name = NULL;
e142c38c 6390 CORE_ADDR baseaddr;
15d034d0 6391 char *built_actual_name;
e142c38c
DJ
6392
6393 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6394
15d034d0
TT
6395 built_actual_name = partial_die_full_name (pdi, cu);
6396 if (built_actual_name != NULL)
6397 actual_name = built_actual_name;
63d06c5c 6398
72bf9492
DJ
6399 if (actual_name == NULL)
6400 actual_name = pdi->name;
6401
c906108c
SS
6402 switch (pdi->tag)
6403 {
6404 case DW_TAG_subprogram:
2cfa0c8d 6405 if (pdi->is_external || cu->language == language_ada)
c906108c 6406 {
2cfa0c8d
JB
6407 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6408 of the global scope. But in Ada, we want to be able to access
6409 nested procedures globally. So all Ada subprograms are stored
6410 in the global scope. */
f47fb265 6411 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6412 mst_text, objfile); */
f47fb265 6413 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6414 built_actual_name != NULL,
f47fb265
MS
6415 VAR_DOMAIN, LOC_BLOCK,
6416 &objfile->global_psymbols,
6417 0, pdi->lowpc + baseaddr,
6418 cu->language, objfile);
c906108c
SS
6419 }
6420 else
6421 {
f47fb265 6422 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 6423 mst_file_text, objfile); */
f47fb265 6424 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6425 built_actual_name != NULL,
f47fb265
MS
6426 VAR_DOMAIN, LOC_BLOCK,
6427 &objfile->static_psymbols,
6428 0, pdi->lowpc + baseaddr,
6429 cu->language, objfile);
c906108c
SS
6430 }
6431 break;
72929c62
JB
6432 case DW_TAG_constant:
6433 {
6434 struct psymbol_allocation_list *list;
6435
6436 if (pdi->is_external)
6437 list = &objfile->global_psymbols;
6438 else
6439 list = &objfile->static_psymbols;
f47fb265 6440 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6441 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
f47fb265 6442 list, 0, 0, cu->language, objfile);
72929c62
JB
6443 }
6444 break;
c906108c 6445 case DW_TAG_variable:
95554aad
TT
6446 if (pdi->d.locdesc)
6447 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 6448
95554aad 6449 if (pdi->d.locdesc
caac4577
JG
6450 && addr == 0
6451 && !dwarf2_per_objfile->has_section_at_zero)
6452 {
6453 /* A global or static variable may also have been stripped
6454 out by the linker if unused, in which case its address
6455 will be nullified; do not add such variables into partial
6456 symbol table then. */
6457 }
6458 else if (pdi->is_external)
c906108c
SS
6459 {
6460 /* Global Variable.
6461 Don't enter into the minimal symbol tables as there is
6462 a minimal symbol table entry from the ELF symbols already.
6463 Enter into partial symbol table if it has a location
6464 descriptor or a type.
6465 If the location descriptor is missing, new_symbol will create
6466 a LOC_UNRESOLVED symbol, the address of the variable will then
6467 be determined from the minimal symbol table whenever the variable
6468 is referenced.
6469 The address for the partial symbol table entry is not
6470 used by GDB, but it comes in handy for debugging partial symbol
6471 table building. */
6472
95554aad 6473 if (pdi->d.locdesc || pdi->has_type)
f47fb265 6474 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6475 built_actual_name != NULL,
f47fb265
MS
6476 VAR_DOMAIN, LOC_STATIC,
6477 &objfile->global_psymbols,
6478 0, addr + baseaddr,
6479 cu->language, objfile);
c906108c
SS
6480 }
6481 else
6482 {
0963b4bd 6483 /* Static Variable. Skip symbols without location descriptors. */
95554aad 6484 if (pdi->d.locdesc == NULL)
decbce07 6485 {
15d034d0 6486 xfree (built_actual_name);
decbce07
MS
6487 return;
6488 }
f47fb265 6489 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 6490 mst_file_data, objfile); */
f47fb265 6491 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6492 built_actual_name != NULL,
f47fb265
MS
6493 VAR_DOMAIN, LOC_STATIC,
6494 &objfile->static_psymbols,
6495 0, addr + baseaddr,
6496 cu->language, objfile);
c906108c
SS
6497 }
6498 break;
6499 case DW_TAG_typedef:
6500 case DW_TAG_base_type:
a02abb62 6501 case DW_TAG_subrange_type:
38d518c9 6502 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6503 built_actual_name != NULL,
176620f1 6504 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 6505 &objfile->static_psymbols,
e142c38c 6506 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6507 break;
72bf9492
DJ
6508 case DW_TAG_namespace:
6509 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6510 built_actual_name != NULL,
72bf9492
DJ
6511 VAR_DOMAIN, LOC_TYPEDEF,
6512 &objfile->global_psymbols,
6513 0, (CORE_ADDR) 0, cu->language, objfile);
6514 break;
c906108c 6515 case DW_TAG_class_type:
680b30c7 6516 case DW_TAG_interface_type:
c906108c
SS
6517 case DW_TAG_structure_type:
6518 case DW_TAG_union_type:
6519 case DW_TAG_enumeration_type:
fa4028e9
JB
6520 /* Skip external references. The DWARF standard says in the section
6521 about "Structure, Union, and Class Type Entries": "An incomplete
6522 structure, union or class type is represented by a structure,
6523 union or class entry that does not have a byte size attribute
6524 and that has a DW_AT_declaration attribute." */
6525 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 6526 {
15d034d0 6527 xfree (built_actual_name);
decbce07
MS
6528 return;
6529 }
fa4028e9 6530
63d06c5c
DC
6531 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6532 static vs. global. */
38d518c9 6533 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6534 built_actual_name != NULL,
176620f1 6535 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
6536 (cu->language == language_cplus
6537 || cu->language == language_java)
63d06c5c
DC
6538 ? &objfile->global_psymbols
6539 : &objfile->static_psymbols,
e142c38c 6540 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 6541
c906108c
SS
6542 break;
6543 case DW_TAG_enumerator:
38d518c9 6544 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 6545 built_actual_name != NULL,
176620f1 6546 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6547 (cu->language == language_cplus
6548 || cu->language == language_java)
f6fe98ef
DJ
6549 ? &objfile->global_psymbols
6550 : &objfile->static_psymbols,
e142c38c 6551 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
6552 break;
6553 default:
6554 break;
6555 }
5c4e30ca 6556
15d034d0 6557 xfree (built_actual_name);
c906108c
SS
6558}
6559
5c4e30ca
DC
6560/* Read a partial die corresponding to a namespace; also, add a symbol
6561 corresponding to that namespace to the symbol table. NAMESPACE is
6562 the name of the enclosing namespace. */
91c24f0a 6563
72bf9492
DJ
6564static void
6565add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 6566 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6567 int need_pc, struct dwarf2_cu *cu)
91c24f0a 6568{
72bf9492 6569 /* Add a symbol for the namespace. */
e7c27a73 6570
72bf9492 6571 add_partial_symbol (pdi, cu);
5c4e30ca
DC
6572
6573 /* Now scan partial symbols in that namespace. */
6574
91c24f0a 6575 if (pdi->has_children)
5734ee8b 6576 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
6577}
6578
5d7cb8df
JK
6579/* Read a partial die corresponding to a Fortran module. */
6580
6581static void
6582add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6583 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6584{
f55ee35c 6585 /* Now scan partial symbols in that module. */
5d7cb8df
JK
6586
6587 if (pdi->has_children)
6588 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6589}
6590
bc30ff58
JB
6591/* Read a partial die corresponding to a subprogram and create a partial
6592 symbol for that subprogram. When the CU language allows it, this
6593 routine also defines a partial symbol for each nested subprogram
6594 that this subprogram contains.
6e70227d 6595
bc30ff58
JB
6596 DIE my also be a lexical block, in which case we simply search
6597 recursively for suprograms defined inside that lexical block.
6598 Again, this is only performed when the CU language allows this
6599 type of definitions. */
6600
6601static void
6602add_partial_subprogram (struct partial_die_info *pdi,
6603 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 6604 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
6605{
6606 if (pdi->tag == DW_TAG_subprogram)
6607 {
6608 if (pdi->has_pc_info)
6609 {
6610 if (pdi->lowpc < *lowpc)
6611 *lowpc = pdi->lowpc;
6612 if (pdi->highpc > *highpc)
6613 *highpc = pdi->highpc;
5734ee8b
DJ
6614 if (need_pc)
6615 {
6616 CORE_ADDR baseaddr;
6617 struct objfile *objfile = cu->objfile;
6618
6619 baseaddr = ANOFFSET (objfile->section_offsets,
6620 SECT_OFF_TEXT (objfile));
6621 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
6622 pdi->lowpc + baseaddr,
6623 pdi->highpc - 1 + baseaddr,
9291a0cd 6624 cu->per_cu->v.psymtab);
5734ee8b 6625 }
481860b3
GB
6626 }
6627
6628 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6629 {
bc30ff58 6630 if (!pdi->is_declaration)
e8d05480
JB
6631 /* Ignore subprogram DIEs that do not have a name, they are
6632 illegal. Do not emit a complaint at this point, we will
6633 do so when we convert this psymtab into a symtab. */
6634 if (pdi->name)
6635 add_partial_symbol (pdi, cu);
bc30ff58
JB
6636 }
6637 }
6e70227d 6638
bc30ff58
JB
6639 if (! pdi->has_children)
6640 return;
6641
6642 if (cu->language == language_ada)
6643 {
6644 pdi = pdi->die_child;
6645 while (pdi != NULL)
6646 {
6647 fixup_partial_die (pdi, cu);
6648 if (pdi->tag == DW_TAG_subprogram
6649 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 6650 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
6651 pdi = pdi->die_sibling;
6652 }
6653 }
6654}
6655
91c24f0a
DC
6656/* Read a partial die corresponding to an enumeration type. */
6657
72bf9492
DJ
6658static void
6659add_partial_enumeration (struct partial_die_info *enum_pdi,
6660 struct dwarf2_cu *cu)
91c24f0a 6661{
72bf9492 6662 struct partial_die_info *pdi;
91c24f0a
DC
6663
6664 if (enum_pdi->name != NULL)
72bf9492
DJ
6665 add_partial_symbol (enum_pdi, cu);
6666
6667 pdi = enum_pdi->die_child;
6668 while (pdi)
91c24f0a 6669 {
72bf9492 6670 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 6671 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 6672 else
72bf9492
DJ
6673 add_partial_symbol (pdi, cu);
6674 pdi = pdi->die_sibling;
91c24f0a 6675 }
91c24f0a
DC
6676}
6677
6caca83c
CC
6678/* Return the initial uleb128 in the die at INFO_PTR. */
6679
6680static unsigned int
d521ce57 6681peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
6682{
6683 unsigned int bytes_read;
6684
6685 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6686}
6687
4bb7a0a7
DJ
6688/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6689 Return the corresponding abbrev, or NULL if the number is zero (indicating
6690 an empty DIE). In either case *BYTES_READ will be set to the length of
6691 the initial number. */
6692
6693static struct abbrev_info *
d521ce57 6694peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 6695 struct dwarf2_cu *cu)
4bb7a0a7
DJ
6696{
6697 bfd *abfd = cu->objfile->obfd;
6698 unsigned int abbrev_number;
6699 struct abbrev_info *abbrev;
6700
6701 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6702
6703 if (abbrev_number == 0)
6704 return NULL;
6705
433df2d4 6706 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
6707 if (!abbrev)
6708 {
3e43a32a
MS
6709 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6710 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
6711 }
6712
6713 return abbrev;
6714}
6715
93311388
DE
6716/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6717 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
6718 DIE. Any children of the skipped DIEs will also be skipped. */
6719
d521ce57
TT
6720static const gdb_byte *
6721skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 6722{
dee91e82 6723 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
6724 struct abbrev_info *abbrev;
6725 unsigned int bytes_read;
6726
6727 while (1)
6728 {
6729 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6730 if (abbrev == NULL)
6731 return info_ptr + bytes_read;
6732 else
dee91e82 6733 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
6734 }
6735}
6736
93311388
DE
6737/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6738 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
6739 abbrev corresponding to that skipped uleb128 should be passed in
6740 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6741 children. */
6742
d521ce57
TT
6743static const gdb_byte *
6744skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 6745 struct abbrev_info *abbrev)
4bb7a0a7
DJ
6746{
6747 unsigned int bytes_read;
6748 struct attribute attr;
dee91e82
DE
6749 bfd *abfd = reader->abfd;
6750 struct dwarf2_cu *cu = reader->cu;
d521ce57 6751 const gdb_byte *buffer = reader->buffer;
f664829e 6752 const gdb_byte *buffer_end = reader->buffer_end;
d521ce57 6753 const gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
6754 unsigned int form, i;
6755
6756 for (i = 0; i < abbrev->num_attrs; i++)
6757 {
6758 /* The only abbrev we care about is DW_AT_sibling. */
6759 if (abbrev->attrs[i].name == DW_AT_sibling)
6760 {
dee91e82 6761 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 6762 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
6763 complaint (&symfile_complaints,
6764 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 6765 else
b64f50a1 6766 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
6767 }
6768
6769 /* If it isn't DW_AT_sibling, skip this attribute. */
6770 form = abbrev->attrs[i].form;
6771 skip_attribute:
6772 switch (form)
6773 {
4bb7a0a7 6774 case DW_FORM_ref_addr:
ae411497
TT
6775 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6776 and later it is offset sized. */
6777 if (cu->header.version == 2)
6778 info_ptr += cu->header.addr_size;
6779 else
6780 info_ptr += cu->header.offset_size;
6781 break;
36586728
TT
6782 case DW_FORM_GNU_ref_alt:
6783 info_ptr += cu->header.offset_size;
6784 break;
ae411497 6785 case DW_FORM_addr:
4bb7a0a7
DJ
6786 info_ptr += cu->header.addr_size;
6787 break;
6788 case DW_FORM_data1:
6789 case DW_FORM_ref1:
6790 case DW_FORM_flag:
6791 info_ptr += 1;
6792 break;
2dc7f7b3
TT
6793 case DW_FORM_flag_present:
6794 break;
4bb7a0a7
DJ
6795 case DW_FORM_data2:
6796 case DW_FORM_ref2:
6797 info_ptr += 2;
6798 break;
6799 case DW_FORM_data4:
6800 case DW_FORM_ref4:
6801 info_ptr += 4;
6802 break;
6803 case DW_FORM_data8:
6804 case DW_FORM_ref8:
55f1336d 6805 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
6806 info_ptr += 8;
6807 break;
6808 case DW_FORM_string:
9b1c24c8 6809 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
6810 info_ptr += bytes_read;
6811 break;
2dc7f7b3 6812 case DW_FORM_sec_offset:
4bb7a0a7 6813 case DW_FORM_strp:
36586728 6814 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
6815 info_ptr += cu->header.offset_size;
6816 break;
2dc7f7b3 6817 case DW_FORM_exprloc:
4bb7a0a7
DJ
6818 case DW_FORM_block:
6819 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6820 info_ptr += bytes_read;
6821 break;
6822 case DW_FORM_block1:
6823 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6824 break;
6825 case DW_FORM_block2:
6826 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6827 break;
6828 case DW_FORM_block4:
6829 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6830 break;
6831 case DW_FORM_sdata:
6832 case DW_FORM_udata:
6833 case DW_FORM_ref_udata:
3019eac3
DE
6834 case DW_FORM_GNU_addr_index:
6835 case DW_FORM_GNU_str_index:
d521ce57 6836 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
6837 break;
6838 case DW_FORM_indirect:
6839 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6840 info_ptr += bytes_read;
6841 /* We need to continue parsing from here, so just go back to
6842 the top. */
6843 goto skip_attribute;
6844
6845 default:
3e43a32a
MS
6846 error (_("Dwarf Error: Cannot handle %s "
6847 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
6848 dwarf_form_name (form),
6849 bfd_get_filename (abfd));
6850 }
6851 }
6852
6853 if (abbrev->has_children)
dee91e82 6854 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
6855 else
6856 return info_ptr;
6857}
6858
93311388 6859/* Locate ORIG_PDI's sibling.
dee91e82 6860 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 6861
d521ce57 6862static const gdb_byte *
dee91e82
DE
6863locate_pdi_sibling (const struct die_reader_specs *reader,
6864 struct partial_die_info *orig_pdi,
d521ce57 6865 const gdb_byte *info_ptr)
91c24f0a
DC
6866{
6867 /* Do we know the sibling already? */
72bf9492 6868
91c24f0a
DC
6869 if (orig_pdi->sibling)
6870 return orig_pdi->sibling;
6871
6872 /* Are there any children to deal with? */
6873
6874 if (!orig_pdi->has_children)
6875 return info_ptr;
6876
4bb7a0a7 6877 /* Skip the children the long way. */
91c24f0a 6878
dee91e82 6879 return skip_children (reader, info_ptr);
91c24f0a
DC
6880}
6881
257e7a09 6882/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 6883 not NULL. */
c906108c
SS
6884
6885static void
257e7a09
YQ
6886dwarf2_read_symtab (struct partial_symtab *self,
6887 struct objfile *objfile)
c906108c 6888{
257e7a09 6889 if (self->readin)
c906108c 6890 {
442e4d9c 6891 warning (_("bug: psymtab for %s is already read in."),
257e7a09 6892 self->filename);
442e4d9c
YQ
6893 }
6894 else
6895 {
6896 if (info_verbose)
c906108c 6897 {
442e4d9c 6898 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 6899 self->filename);
442e4d9c 6900 gdb_flush (gdb_stdout);
c906108c 6901 }
c906108c 6902
442e4d9c
YQ
6903 /* Restore our global data. */
6904 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10b3939b 6905
442e4d9c
YQ
6906 /* If this psymtab is constructed from a debug-only objfile, the
6907 has_section_at_zero flag will not necessarily be correct. We
6908 can get the correct value for this flag by looking at the data
6909 associated with the (presumably stripped) associated objfile. */
6910 if (objfile->separate_debug_objfile_backlink)
6911 {
6912 struct dwarf2_per_objfile *dpo_backlink
6913 = objfile_data (objfile->separate_debug_objfile_backlink,
6914 dwarf2_objfile_data_key);
9a619af0 6915
442e4d9c
YQ
6916 dwarf2_per_objfile->has_section_at_zero
6917 = dpo_backlink->has_section_at_zero;
6918 }
b2ab525c 6919
442e4d9c 6920 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 6921
257e7a09 6922 psymtab_to_symtab_1 (self);
c906108c 6923
442e4d9c
YQ
6924 /* Finish up the debug error message. */
6925 if (info_verbose)
6926 printf_filtered (_("done.\n"));
c906108c 6927 }
95554aad
TT
6928
6929 process_cu_includes ();
c906108c 6930}
9cdd5dbd
DE
6931\f
6932/* Reading in full CUs. */
c906108c 6933
10b3939b
DJ
6934/* Add PER_CU to the queue. */
6935
6936static void
95554aad
TT
6937queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6938 enum language pretend_language)
10b3939b
DJ
6939{
6940 struct dwarf2_queue_item *item;
6941
6942 per_cu->queued = 1;
6943 item = xmalloc (sizeof (*item));
6944 item->per_cu = per_cu;
95554aad 6945 item->pretend_language = pretend_language;
10b3939b
DJ
6946 item->next = NULL;
6947
6948 if (dwarf2_queue == NULL)
6949 dwarf2_queue = item;
6950 else
6951 dwarf2_queue_tail->next = item;
6952
6953 dwarf2_queue_tail = item;
6954}
6955
89e63ee4
DE
6956/* If PER_CU is not yet queued, add it to the queue.
6957 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
6958 dependency.
0907af0c 6959 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
6960 meaning either PER_CU is already queued or it is already loaded.
6961
6962 N.B. There is an invariant here that if a CU is queued then it is loaded.
6963 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
6964
6965static int
89e63ee4 6966maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
6967 struct dwarf2_per_cu_data *per_cu,
6968 enum language pretend_language)
6969{
6970 /* We may arrive here during partial symbol reading, if we need full
6971 DIEs to process an unusual case (e.g. template arguments). Do
6972 not queue PER_CU, just tell our caller to load its DIEs. */
6973 if (dwarf2_per_objfile->reading_partial_symbols)
6974 {
6975 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6976 return 1;
6977 return 0;
6978 }
6979
6980 /* Mark the dependence relation so that we don't flush PER_CU
6981 too early. */
89e63ee4
DE
6982 if (dependent_cu != NULL)
6983 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
6984
6985 /* If it's already on the queue, we have nothing to do. */
6986 if (per_cu->queued)
6987 return 0;
6988
6989 /* If the compilation unit is already loaded, just mark it as
6990 used. */
6991 if (per_cu->cu != NULL)
6992 {
6993 per_cu->cu->last_used = 0;
6994 return 0;
6995 }
6996
6997 /* Add it to the queue. */
6998 queue_comp_unit (per_cu, pretend_language);
6999
7000 return 1;
7001}
7002
10b3939b
DJ
7003/* Process the queue. */
7004
7005static void
a0f42c21 7006process_queue (void)
10b3939b
DJ
7007{
7008 struct dwarf2_queue_item *item, *next_item;
7009
45cfd468
DE
7010 if (dwarf2_read_debug)
7011 {
7012 fprintf_unfiltered (gdb_stdlog,
7013 "Expanding one or more symtabs of objfile %s ...\n",
7014 dwarf2_per_objfile->objfile->name);
7015 }
7016
03dd20cc
DJ
7017 /* The queue starts out with one item, but following a DIE reference
7018 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
7019 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7020 {
9291a0cd
TT
7021 if (dwarf2_per_objfile->using_index
7022 ? !item->per_cu->v.quick->symtab
7023 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
f4dc4d17
DE
7024 {
7025 struct dwarf2_per_cu_data *per_cu = item->per_cu;
247f5c4f 7026 char buf[100];
f4dc4d17 7027
247f5c4f 7028 if (per_cu->is_debug_types)
f4dc4d17 7029 {
247f5c4f
DE
7030 struct signatured_type *sig_type =
7031 (struct signatured_type *) per_cu;
7032
7033 sprintf (buf, "TU %s at offset 0x%x",
7034 hex_string (sig_type->signature), per_cu->offset.sect_off);
f4dc4d17 7035 }
247f5c4f
DE
7036 else
7037 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7038
7039 if (dwarf2_read_debug)
7040 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
7041
7042 if (per_cu->is_debug_types)
7043 process_full_type_unit (per_cu, item->pretend_language);
7044 else
7045 process_full_comp_unit (per_cu, item->pretend_language);
7046
7047 if (dwarf2_read_debug)
247f5c4f 7048 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 7049 }
10b3939b
DJ
7050
7051 item->per_cu->queued = 0;
7052 next_item = item->next;
7053 xfree (item);
7054 }
7055
7056 dwarf2_queue_tail = NULL;
45cfd468
DE
7057
7058 if (dwarf2_read_debug)
7059 {
7060 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7061 dwarf2_per_objfile->objfile->name);
7062 }
10b3939b
DJ
7063}
7064
7065/* Free all allocated queue entries. This function only releases anything if
7066 an error was thrown; if the queue was processed then it would have been
7067 freed as we went along. */
7068
7069static void
7070dwarf2_release_queue (void *dummy)
7071{
7072 struct dwarf2_queue_item *item, *last;
7073
7074 item = dwarf2_queue;
7075 while (item)
7076 {
7077 /* Anything still marked queued is likely to be in an
7078 inconsistent state, so discard it. */
7079 if (item->per_cu->queued)
7080 {
7081 if (item->per_cu->cu != NULL)
dee91e82 7082 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
7083 item->per_cu->queued = 0;
7084 }
7085
7086 last = item;
7087 item = item->next;
7088 xfree (last);
7089 }
7090
7091 dwarf2_queue = dwarf2_queue_tail = NULL;
7092}
7093
7094/* Read in full symbols for PST, and anything it depends on. */
7095
c906108c 7096static void
fba45db2 7097psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 7098{
10b3939b 7099 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
7100 int i;
7101
95554aad
TT
7102 if (pst->readin)
7103 return;
7104
aaa75496 7105 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
7106 if (!pst->dependencies[i]->readin
7107 && pst->dependencies[i]->user == NULL)
aaa75496
JB
7108 {
7109 /* Inform about additional files that need to be read in. */
7110 if (info_verbose)
7111 {
a3f17187 7112 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
7113 fputs_filtered (" ", gdb_stdout);
7114 wrap_here ("");
7115 fputs_filtered ("and ", gdb_stdout);
7116 wrap_here ("");
7117 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 7118 wrap_here (""); /* Flush output. */
aaa75496
JB
7119 gdb_flush (gdb_stdout);
7120 }
7121 psymtab_to_symtab_1 (pst->dependencies[i]);
7122 }
7123
e38df1d0 7124 per_cu = pst->read_symtab_private;
10b3939b
DJ
7125
7126 if (per_cu == NULL)
aaa75496
JB
7127 {
7128 /* It's an include file, no symbols to read for it.
7129 Everything is in the parent symtab. */
7130 pst->readin = 1;
7131 return;
7132 }
c906108c 7133
a0f42c21 7134 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
7135}
7136
dee91e82
DE
7137/* Trivial hash function for die_info: the hash value of a DIE
7138 is its offset in .debug_info for this objfile. */
10b3939b 7139
dee91e82
DE
7140static hashval_t
7141die_hash (const void *item)
10b3939b 7142{
dee91e82 7143 const struct die_info *die = item;
6502dd73 7144
dee91e82
DE
7145 return die->offset.sect_off;
7146}
63d06c5c 7147
dee91e82
DE
7148/* Trivial comparison function for die_info structures: two DIEs
7149 are equal if they have the same offset. */
98bfdba5 7150
dee91e82
DE
7151static int
7152die_eq (const void *item_lhs, const void *item_rhs)
7153{
7154 const struct die_info *die_lhs = item_lhs;
7155 const struct die_info *die_rhs = item_rhs;
c906108c 7156
dee91e82
DE
7157 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7158}
c906108c 7159
dee91e82
DE
7160/* die_reader_func for load_full_comp_unit.
7161 This is identical to read_signatured_type_reader,
7162 but is kept separate for now. */
c906108c 7163
dee91e82
DE
7164static void
7165load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7166 const gdb_byte *info_ptr,
dee91e82
DE
7167 struct die_info *comp_unit_die,
7168 int has_children,
7169 void *data)
7170{
7171 struct dwarf2_cu *cu = reader->cu;
95554aad 7172 enum language *language_ptr = data;
6caca83c 7173
dee91e82
DE
7174 gdb_assert (cu->die_hash == NULL);
7175 cu->die_hash =
7176 htab_create_alloc_ex (cu->header.length / 12,
7177 die_hash,
7178 die_eq,
7179 NULL,
7180 &cu->comp_unit_obstack,
7181 hashtab_obstack_allocate,
7182 dummy_obstack_deallocate);
e142c38c 7183
dee91e82
DE
7184 if (has_children)
7185 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7186 &info_ptr, comp_unit_die);
7187 cu->dies = comp_unit_die;
7188 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
7189
7190 /* We try not to read any attributes in this function, because not
9cdd5dbd 7191 all CUs needed for references have been loaded yet, and symbol
10b3939b 7192 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
7193 or we won't be able to build types correctly.
7194 Similarly, if we do not read the producer, we can not apply
7195 producer-specific interpretation. */
95554aad 7196 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 7197}
10b3939b 7198
dee91e82 7199/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 7200
dee91e82 7201static void
95554aad
TT
7202load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7203 enum language pretend_language)
dee91e82 7204{
3019eac3 7205 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 7206
f4dc4d17
DE
7207 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7208 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
7209}
7210
3da10d80
KS
7211/* Add a DIE to the delayed physname list. */
7212
7213static void
7214add_to_method_list (struct type *type, int fnfield_index, int index,
7215 const char *name, struct die_info *die,
7216 struct dwarf2_cu *cu)
7217{
7218 struct delayed_method_info mi;
7219 mi.type = type;
7220 mi.fnfield_index = fnfield_index;
7221 mi.index = index;
7222 mi.name = name;
7223 mi.die = die;
7224 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7225}
7226
7227/* A cleanup for freeing the delayed method list. */
7228
7229static void
7230free_delayed_list (void *ptr)
7231{
7232 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7233 if (cu->method_list != NULL)
7234 {
7235 VEC_free (delayed_method_info, cu->method_list);
7236 cu->method_list = NULL;
7237 }
7238}
7239
7240/* Compute the physnames of any methods on the CU's method list.
7241
7242 The computation of method physnames is delayed in order to avoid the
7243 (bad) condition that one of the method's formal parameters is of an as yet
7244 incomplete type. */
7245
7246static void
7247compute_delayed_physnames (struct dwarf2_cu *cu)
7248{
7249 int i;
7250 struct delayed_method_info *mi;
7251 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7252 {
1d06ead6 7253 const char *physname;
3da10d80
KS
7254 struct fn_fieldlist *fn_flp
7255 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7d455152 7256 physname = dwarf2_physname (mi->name, mi->die, cu);
3da10d80
KS
7257 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7258 }
7259}
7260
a766d390
DE
7261/* Go objects should be embedded in a DW_TAG_module DIE,
7262 and it's not clear if/how imported objects will appear.
7263 To keep Go support simple until that's worked out,
7264 go back through what we've read and create something usable.
7265 We could do this while processing each DIE, and feels kinda cleaner,
7266 but that way is more invasive.
7267 This is to, for example, allow the user to type "p var" or "b main"
7268 without having to specify the package name, and allow lookups
7269 of module.object to work in contexts that use the expression
7270 parser. */
7271
7272static void
7273fixup_go_packaging (struct dwarf2_cu *cu)
7274{
7275 char *package_name = NULL;
7276 struct pending *list;
7277 int i;
7278
7279 for (list = global_symbols; list != NULL; list = list->next)
7280 {
7281 for (i = 0; i < list->nsyms; ++i)
7282 {
7283 struct symbol *sym = list->symbol[i];
7284
7285 if (SYMBOL_LANGUAGE (sym) == language_go
7286 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7287 {
7288 char *this_package_name = go_symbol_package_name (sym);
7289
7290 if (this_package_name == NULL)
7291 continue;
7292 if (package_name == NULL)
7293 package_name = this_package_name;
7294 else
7295 {
7296 if (strcmp (package_name, this_package_name) != 0)
7297 complaint (&symfile_complaints,
7298 _("Symtab %s has objects from two different Go packages: %s and %s"),
210bbc17 7299 (SYMBOL_SYMTAB (sym)
05cba821 7300 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
a766d390
DE
7301 : cu->objfile->name),
7302 this_package_name, package_name);
7303 xfree (this_package_name);
7304 }
7305 }
7306 }
7307 }
7308
7309 if (package_name != NULL)
7310 {
7311 struct objfile *objfile = cu->objfile;
10f0c4bb
TT
7312 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7313 package_name,
7314 strlen (package_name));
a766d390 7315 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
86f62fd7 7316 saved_package_name, objfile);
a766d390
DE
7317 struct symbol *sym;
7318
7319 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7320
e623cf5d 7321 sym = allocate_symbol (objfile);
f85f34ed 7322 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
7323 SYMBOL_SET_NAMES (sym, saved_package_name,
7324 strlen (saved_package_name), 0, objfile);
a766d390
DE
7325 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7326 e.g., "main" finds the "main" module and not C's main(). */
7327 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 7328 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
7329 SYMBOL_TYPE (sym) = type;
7330
7331 add_symbol_to_list (sym, &global_symbols);
7332
7333 xfree (package_name);
7334 }
7335}
7336
95554aad
TT
7337/* Return the symtab for PER_CU. This works properly regardless of
7338 whether we're using the index or psymtabs. */
7339
7340static struct symtab *
7341get_symtab (struct dwarf2_per_cu_data *per_cu)
7342{
7343 return (dwarf2_per_objfile->using_index
7344 ? per_cu->v.quick->symtab
7345 : per_cu->v.psymtab->symtab);
7346}
7347
7348/* A helper function for computing the list of all symbol tables
7349 included by PER_CU. */
7350
7351static void
ec94af83
DE
7352recursively_compute_inclusions (VEC (symtab_ptr) **result,
7353 htab_t all_children, htab_t all_type_symtabs,
f9125b6c
TT
7354 struct dwarf2_per_cu_data *per_cu,
7355 struct symtab *immediate_parent)
95554aad
TT
7356{
7357 void **slot;
7358 int ix;
ec94af83 7359 struct symtab *symtab;
95554aad
TT
7360 struct dwarf2_per_cu_data *iter;
7361
7362 slot = htab_find_slot (all_children, per_cu, INSERT);
7363 if (*slot != NULL)
7364 {
7365 /* This inclusion and its children have been processed. */
7366 return;
7367 }
7368
7369 *slot = per_cu;
7370 /* Only add a CU if it has a symbol table. */
ec94af83
DE
7371 symtab = get_symtab (per_cu);
7372 if (symtab != NULL)
7373 {
7374 /* If this is a type unit only add its symbol table if we haven't
7375 seen it yet (type unit per_cu's can share symtabs). */
7376 if (per_cu->is_debug_types)
7377 {
7378 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7379 if (*slot == NULL)
7380 {
7381 *slot = symtab;
7382 VEC_safe_push (symtab_ptr, *result, symtab);
f9125b6c
TT
7383 if (symtab->user == NULL)
7384 symtab->user = immediate_parent;
ec94af83
DE
7385 }
7386 }
7387 else
f9125b6c
TT
7388 {
7389 VEC_safe_push (symtab_ptr, *result, symtab);
7390 if (symtab->user == NULL)
7391 symtab->user = immediate_parent;
7392 }
ec94af83 7393 }
95554aad
TT
7394
7395 for (ix = 0;
796a7ff8 7396 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 7397 ++ix)
ec94af83
DE
7398 {
7399 recursively_compute_inclusions (result, all_children,
f9125b6c 7400 all_type_symtabs, iter, symtab);
ec94af83 7401 }
95554aad
TT
7402}
7403
7404/* Compute the symtab 'includes' fields for the symtab related to
7405 PER_CU. */
7406
7407static void
7408compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7409{
f4dc4d17
DE
7410 gdb_assert (! per_cu->is_debug_types);
7411
796a7ff8 7412 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
7413 {
7414 int ix, len;
ec94af83
DE
7415 struct dwarf2_per_cu_data *per_cu_iter;
7416 struct symtab *symtab_iter;
7417 VEC (symtab_ptr) *result_symtabs = NULL;
7418 htab_t all_children, all_type_symtabs;
95554aad
TT
7419 struct symtab *symtab = get_symtab (per_cu);
7420
7421 /* If we don't have a symtab, we can just skip this case. */
7422 if (symtab == NULL)
7423 return;
7424
7425 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7426 NULL, xcalloc, xfree);
ec94af83
DE
7427 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7428 NULL, xcalloc, xfree);
95554aad
TT
7429
7430 for (ix = 0;
796a7ff8 7431 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 7432 ix, per_cu_iter);
95554aad 7433 ++ix)
ec94af83
DE
7434 {
7435 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c
TT
7436 all_type_symtabs, per_cu_iter,
7437 symtab);
ec94af83 7438 }
95554aad 7439
ec94af83
DE
7440 /* Now we have a transitive closure of all the included symtabs. */
7441 len = VEC_length (symtab_ptr, result_symtabs);
95554aad
TT
7442 symtab->includes
7443 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7444 (len + 1) * sizeof (struct symtab *));
7445 for (ix = 0;
ec94af83 7446 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
95554aad 7447 ++ix)
ec94af83 7448 symtab->includes[ix] = symtab_iter;
95554aad
TT
7449 symtab->includes[len] = NULL;
7450
ec94af83 7451 VEC_free (symtab_ptr, result_symtabs);
95554aad 7452 htab_delete (all_children);
ec94af83 7453 htab_delete (all_type_symtabs);
95554aad
TT
7454 }
7455}
7456
7457/* Compute the 'includes' field for the symtabs of all the CUs we just
7458 read. */
7459
7460static void
7461process_cu_includes (void)
7462{
7463 int ix;
7464 struct dwarf2_per_cu_data *iter;
7465
7466 for (ix = 0;
7467 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7468 ix, iter);
7469 ++ix)
f4dc4d17
DE
7470 {
7471 if (! iter->is_debug_types)
7472 compute_symtab_includes (iter);
7473 }
95554aad
TT
7474
7475 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7476}
7477
9cdd5dbd 7478/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
7479 already been loaded into memory. */
7480
7481static void
95554aad
TT
7482process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7483 enum language pretend_language)
10b3939b 7484{
10b3939b 7485 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 7486 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
7487 CORE_ADDR lowpc, highpc;
7488 struct symtab *symtab;
3da10d80 7489 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b 7490 CORE_ADDR baseaddr;
4359dff1 7491 struct block *static_block;
10b3939b
DJ
7492
7493 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7494
10b3939b
DJ
7495 buildsym_init ();
7496 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 7497 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
7498
7499 cu->list_in_scope = &file_symbols;
c906108c 7500
95554aad
TT
7501 cu->language = pretend_language;
7502 cu->language_defn = language_def (cu->language);
7503
c906108c 7504 /* Do line number decoding in read_file_scope () */
10b3939b 7505 process_die (cu->dies, cu);
c906108c 7506
a766d390
DE
7507 /* For now fudge the Go package. */
7508 if (cu->language == language_go)
7509 fixup_go_packaging (cu);
7510
3da10d80
KS
7511 /* Now that we have processed all the DIEs in the CU, all the types
7512 should be complete, and it should now be safe to compute all of the
7513 physnames. */
7514 compute_delayed_physnames (cu);
7515 do_cleanups (delayed_list_cleanup);
7516
fae299cd
DC
7517 /* Some compilers don't define a DW_AT_high_pc attribute for the
7518 compilation unit. If the DW_AT_high_pc is missing, synthesize
7519 it, by scanning the DIE's below the compilation unit. */
10b3939b 7520 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 7521
36586728 7522 static_block
ff546935 7523 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
4359dff1
JK
7524
7525 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7526 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7527 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7528 addrmap to help ensure it has an accurate map of pc values belonging to
7529 this comp unit. */
7530 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7531
7532 symtab = end_symtab_from_static_block (static_block, objfile,
7533 SECT_OFF_TEXT (objfile), 0);
c906108c 7534
8be455d7 7535 if (symtab != NULL)
c906108c 7536 {
df15bd07 7537 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 7538
8be455d7
JK
7539 /* Set symtab language to language from DW_AT_language. If the
7540 compilation is from a C file generated by language preprocessors, do
7541 not set the language if it was already deduced by start_subfile. */
7542 if (!(cu->language == language_c && symtab->language != language_c))
7543 symtab->language = cu->language;
7544
7545 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7546 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
7547 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7548 there were bugs in prologue debug info, fixed later in GCC-4.5
7549 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
7550
7551 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7552 needed, it would be wrong due to missing DW_AT_producer there.
7553
7554 Still one can confuse GDB by using non-standard GCC compilation
7555 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7556 */
ab260dad 7557 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 7558 symtab->locations_valid = 1;
e0d00bc7
JK
7559
7560 if (gcc_4_minor >= 5)
7561 symtab->epilogue_unwind_valid = 1;
96408a79
SA
7562
7563 symtab->call_site_htab = cu->call_site_htab;
c906108c 7564 }
9291a0cd
TT
7565
7566 if (dwarf2_per_objfile->using_index)
7567 per_cu->v.quick->symtab = symtab;
7568 else
7569 {
7570 struct partial_symtab *pst = per_cu->v.psymtab;
7571 pst->symtab = symtab;
7572 pst->readin = 1;
7573 }
c906108c 7574
95554aad
TT
7575 /* Push it for inclusion processing later. */
7576 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7577
c906108c 7578 do_cleanups (back_to);
f4dc4d17 7579}
45cfd468 7580
f4dc4d17
DE
7581/* Generate full symbol information for type unit PER_CU, whose DIEs have
7582 already been loaded into memory. */
7583
7584static void
7585process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7586 enum language pretend_language)
7587{
7588 struct dwarf2_cu *cu = per_cu->cu;
7589 struct objfile *objfile = per_cu->objfile;
7590 struct symtab *symtab;
7591 struct cleanup *back_to, *delayed_list_cleanup;
0186c6a7
DE
7592 struct signatured_type *sig_type;
7593
7594 gdb_assert (per_cu->is_debug_types);
7595 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
7596
7597 buildsym_init ();
7598 back_to = make_cleanup (really_free_pendings, NULL);
7599 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7600
7601 cu->list_in_scope = &file_symbols;
7602
7603 cu->language = pretend_language;
7604 cu->language_defn = language_def (cu->language);
7605
7606 /* The symbol tables are set up in read_type_unit_scope. */
7607 process_die (cu->dies, cu);
7608
7609 /* For now fudge the Go package. */
7610 if (cu->language == language_go)
7611 fixup_go_packaging (cu);
7612
7613 /* Now that we have processed all the DIEs in the CU, all the types
7614 should be complete, and it should now be safe to compute all of the
7615 physnames. */
7616 compute_delayed_physnames (cu);
7617 do_cleanups (delayed_list_cleanup);
7618
7619 /* TUs share symbol tables.
7620 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
7621 of it with end_expandable_symtab. Otherwise, complete the addition of
7622 this TU's symbols to the existing symtab. */
0186c6a7 7623 if (sig_type->type_unit_group->primary_symtab == NULL)
45cfd468 7624 {
f4dc4d17 7625 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
0186c6a7 7626 sig_type->type_unit_group->primary_symtab = symtab;
f4dc4d17
DE
7627
7628 if (symtab != NULL)
7629 {
7630 /* Set symtab language to language from DW_AT_language. If the
7631 compilation is from a C file generated by language preprocessors,
7632 do not set the language if it was already deduced by
7633 start_subfile. */
7634 if (!(cu->language == language_c && symtab->language != language_c))
7635 symtab->language = cu->language;
7636 }
7637 }
7638 else
7639 {
7640 augment_type_symtab (objfile,
0186c6a7
DE
7641 sig_type->type_unit_group->primary_symtab);
7642 symtab = sig_type->type_unit_group->primary_symtab;
f4dc4d17
DE
7643 }
7644
7645 if (dwarf2_per_objfile->using_index)
7646 per_cu->v.quick->symtab = symtab;
7647 else
7648 {
7649 struct partial_symtab *pst = per_cu->v.psymtab;
7650 pst->symtab = symtab;
7651 pst->readin = 1;
45cfd468 7652 }
f4dc4d17
DE
7653
7654 do_cleanups (back_to);
c906108c
SS
7655}
7656
95554aad
TT
7657/* Process an imported unit DIE. */
7658
7659static void
7660process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7661{
7662 struct attribute *attr;
7663
f4dc4d17
DE
7664 /* For now we don't handle imported units in type units. */
7665 if (cu->per_cu->is_debug_types)
7666 {
7667 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7668 " supported in type units [in module %s]"),
7669 cu->objfile->name);
7670 }
7671
95554aad
TT
7672 attr = dwarf2_attr (die, DW_AT_import, cu);
7673 if (attr != NULL)
7674 {
7675 struct dwarf2_per_cu_data *per_cu;
7676 struct symtab *imported_symtab;
7677 sect_offset offset;
36586728 7678 int is_dwz;
95554aad
TT
7679
7680 offset = dwarf2_get_ref_die_offset (attr);
36586728
TT
7681 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7682 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
95554aad 7683
69d751e3 7684 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
7685 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7686 load_full_comp_unit (per_cu, cu->language);
7687
796a7ff8 7688 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
7689 per_cu);
7690 }
7691}
7692
c906108c
SS
7693/* Process a die and its children. */
7694
7695static void
e7c27a73 7696process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7697{
7698 switch (die->tag)
7699 {
7700 case DW_TAG_padding:
7701 break;
7702 case DW_TAG_compile_unit:
95554aad 7703 case DW_TAG_partial_unit:
e7c27a73 7704 read_file_scope (die, cu);
c906108c 7705 break;
348e048f
DE
7706 case DW_TAG_type_unit:
7707 read_type_unit_scope (die, cu);
7708 break;
c906108c 7709 case DW_TAG_subprogram:
c906108c 7710 case DW_TAG_inlined_subroutine:
edb3359d 7711 read_func_scope (die, cu);
c906108c
SS
7712 break;
7713 case DW_TAG_lexical_block:
14898363
L
7714 case DW_TAG_try_block:
7715 case DW_TAG_catch_block:
e7c27a73 7716 read_lexical_block_scope (die, cu);
c906108c 7717 break;
96408a79
SA
7718 case DW_TAG_GNU_call_site:
7719 read_call_site_scope (die, cu);
7720 break;
c906108c 7721 case DW_TAG_class_type:
680b30c7 7722 case DW_TAG_interface_type:
c906108c
SS
7723 case DW_TAG_structure_type:
7724 case DW_TAG_union_type:
134d01f1 7725 process_structure_scope (die, cu);
c906108c
SS
7726 break;
7727 case DW_TAG_enumeration_type:
134d01f1 7728 process_enumeration_scope (die, cu);
c906108c 7729 break;
134d01f1 7730
f792889a
DJ
7731 /* These dies have a type, but processing them does not create
7732 a symbol or recurse to process the children. Therefore we can
7733 read them on-demand through read_type_die. */
c906108c 7734 case DW_TAG_subroutine_type:
72019c9c 7735 case DW_TAG_set_type:
c906108c 7736 case DW_TAG_array_type:
c906108c 7737 case DW_TAG_pointer_type:
c906108c 7738 case DW_TAG_ptr_to_member_type:
c906108c 7739 case DW_TAG_reference_type:
c906108c 7740 case DW_TAG_string_type:
c906108c 7741 break;
134d01f1 7742
c906108c 7743 case DW_TAG_base_type:
a02abb62 7744 case DW_TAG_subrange_type:
cb249c71 7745 case DW_TAG_typedef:
134d01f1
DJ
7746 /* Add a typedef symbol for the type definition, if it has a
7747 DW_AT_name. */
f792889a 7748 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 7749 break;
c906108c 7750 case DW_TAG_common_block:
e7c27a73 7751 read_common_block (die, cu);
c906108c
SS
7752 break;
7753 case DW_TAG_common_inclusion:
7754 break;
d9fa45fe 7755 case DW_TAG_namespace:
4d4ec4e5 7756 cu->processing_has_namespace_info = 1;
e7c27a73 7757 read_namespace (die, cu);
d9fa45fe 7758 break;
5d7cb8df 7759 case DW_TAG_module:
4d4ec4e5 7760 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
7761 read_module (die, cu);
7762 break;
d9fa45fe
DC
7763 case DW_TAG_imported_declaration:
7764 case DW_TAG_imported_module:
4d4ec4e5 7765 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
7766 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7767 || cu->language != language_fortran))
7768 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7769 dwarf_tag_name (die->tag));
7770 read_import_statement (die, cu);
d9fa45fe 7771 break;
95554aad
TT
7772
7773 case DW_TAG_imported_unit:
7774 process_imported_unit_die (die, cu);
7775 break;
7776
c906108c 7777 default:
e7c27a73 7778 new_symbol (die, NULL, cu);
c906108c
SS
7779 break;
7780 }
7781}
ca69b9e6
DE
7782\f
7783/* DWARF name computation. */
c906108c 7784
94af9270
KS
7785/* A helper function for dwarf2_compute_name which determines whether DIE
7786 needs to have the name of the scope prepended to the name listed in the
7787 die. */
7788
7789static int
7790die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7791{
1c809c68
TT
7792 struct attribute *attr;
7793
94af9270
KS
7794 switch (die->tag)
7795 {
7796 case DW_TAG_namespace:
7797 case DW_TAG_typedef:
7798 case DW_TAG_class_type:
7799 case DW_TAG_interface_type:
7800 case DW_TAG_structure_type:
7801 case DW_TAG_union_type:
7802 case DW_TAG_enumeration_type:
7803 case DW_TAG_enumerator:
7804 case DW_TAG_subprogram:
7805 case DW_TAG_member:
7806 return 1;
7807
7808 case DW_TAG_variable:
c2b0a229 7809 case DW_TAG_constant:
94af9270
KS
7810 /* We only need to prefix "globally" visible variables. These include
7811 any variable marked with DW_AT_external or any variable that
7812 lives in a namespace. [Variables in anonymous namespaces
7813 require prefixing, but they are not DW_AT_external.] */
7814
7815 if (dwarf2_attr (die, DW_AT_specification, cu))
7816 {
7817 struct dwarf2_cu *spec_cu = cu;
9a619af0 7818
94af9270
KS
7819 return die_needs_namespace (die_specification (die, &spec_cu),
7820 spec_cu);
7821 }
7822
1c809c68 7823 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
7824 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7825 && die->parent->tag != DW_TAG_module)
1c809c68
TT
7826 return 0;
7827 /* A variable in a lexical block of some kind does not need a
7828 namespace, even though in C++ such variables may be external
7829 and have a mangled name. */
7830 if (die->parent->tag == DW_TAG_lexical_block
7831 || die->parent->tag == DW_TAG_try_block
1054b214
TT
7832 || die->parent->tag == DW_TAG_catch_block
7833 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
7834 return 0;
7835 return 1;
94af9270
KS
7836
7837 default:
7838 return 0;
7839 }
7840}
7841
98bfdba5
PA
7842/* Retrieve the last character from a mem_file. */
7843
7844static void
7845do_ui_file_peek_last (void *object, const char *buffer, long length)
7846{
7847 char *last_char_p = (char *) object;
7848
7849 if (length > 0)
7850 *last_char_p = buffer[length - 1];
7851}
7852
94af9270 7853/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
7854 compute the physname for the object, which include a method's:
7855 - formal parameters (C++/Java),
7856 - receiver type (Go),
7857 - return type (Java).
7858
7859 The term "physname" is a bit confusing.
7860 For C++, for example, it is the demangled name.
7861 For Go, for example, it's the mangled name.
94af9270 7862
af6b7be1
JB
7863 For Ada, return the DIE's linkage name rather than the fully qualified
7864 name. PHYSNAME is ignored..
7865
94af9270
KS
7866 The result is allocated on the objfile_obstack and canonicalized. */
7867
7868static const char *
15d034d0
TT
7869dwarf2_compute_name (const char *name,
7870 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
7871 int physname)
7872{
bb5ed363
DE
7873 struct objfile *objfile = cu->objfile;
7874
94af9270
KS
7875 if (name == NULL)
7876 name = dwarf2_name (die, cu);
7877
f55ee35c
JK
7878 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7879 compute it by typename_concat inside GDB. */
7880 if (cu->language == language_ada
7881 || (cu->language == language_fortran && physname))
7882 {
7883 /* For Ada unit, we prefer the linkage name over the name, as
7884 the former contains the exported name, which the user expects
7885 to be able to reference. Ideally, we want the user to be able
7886 to reference this entity using either natural or linkage name,
7887 but we haven't started looking at this enhancement yet. */
7888 struct attribute *attr;
7889
7890 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7891 if (attr == NULL)
7892 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7893 if (attr && DW_STRING (attr))
7894 return DW_STRING (attr);
7895 }
7896
94af9270
KS
7897 /* These are the only languages we know how to qualify names in. */
7898 if (name != NULL
f55ee35c
JK
7899 && (cu->language == language_cplus || cu->language == language_java
7900 || cu->language == language_fortran))
94af9270
KS
7901 {
7902 if (die_needs_namespace (die, cu))
7903 {
7904 long length;
0d5cff50 7905 const char *prefix;
94af9270
KS
7906 struct ui_file *buf;
7907
7908 prefix = determine_prefix (die, cu);
7909 buf = mem_fileopen ();
7910 if (*prefix != '\0')
7911 {
f55ee35c
JK
7912 char *prefixed_name = typename_concat (NULL, prefix, name,
7913 physname, cu);
9a619af0 7914
94af9270
KS
7915 fputs_unfiltered (prefixed_name, buf);
7916 xfree (prefixed_name);
7917 }
7918 else
62d5b8da 7919 fputs_unfiltered (name, buf);
94af9270 7920
98bfdba5
PA
7921 /* Template parameters may be specified in the DIE's DW_AT_name, or
7922 as children with DW_TAG_template_type_param or
7923 DW_TAG_value_type_param. If the latter, add them to the name
7924 here. If the name already has template parameters, then
7925 skip this step; some versions of GCC emit both, and
7926 it is more efficient to use the pre-computed name.
7927
7928 Something to keep in mind about this process: it is very
7929 unlikely, or in some cases downright impossible, to produce
7930 something that will match the mangled name of a function.
7931 If the definition of the function has the same debug info,
7932 we should be able to match up with it anyway. But fallbacks
7933 using the minimal symbol, for instance to find a method
7934 implemented in a stripped copy of libstdc++, will not work.
7935 If we do not have debug info for the definition, we will have to
7936 match them up some other way.
7937
7938 When we do name matching there is a related problem with function
7939 templates; two instantiated function templates are allowed to
7940 differ only by their return types, which we do not add here. */
7941
7942 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7943 {
7944 struct attribute *attr;
7945 struct die_info *child;
7946 int first = 1;
7947
7948 die->building_fullname = 1;
7949
7950 for (child = die->child; child != NULL; child = child->sibling)
7951 {
7952 struct type *type;
12df843f 7953 LONGEST value;
d521ce57 7954 const gdb_byte *bytes;
98bfdba5
PA
7955 struct dwarf2_locexpr_baton *baton;
7956 struct value *v;
7957
7958 if (child->tag != DW_TAG_template_type_param
7959 && child->tag != DW_TAG_template_value_param)
7960 continue;
7961
7962 if (first)
7963 {
7964 fputs_unfiltered ("<", buf);
7965 first = 0;
7966 }
7967 else
7968 fputs_unfiltered (", ", buf);
7969
7970 attr = dwarf2_attr (child, DW_AT_type, cu);
7971 if (attr == NULL)
7972 {
7973 complaint (&symfile_complaints,
7974 _("template parameter missing DW_AT_type"));
7975 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7976 continue;
7977 }
7978 type = die_type (child, cu);
7979
7980 if (child->tag == DW_TAG_template_type_param)
7981 {
79d43c61 7982 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
98bfdba5
PA
7983 continue;
7984 }
7985
7986 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7987 if (attr == NULL)
7988 {
7989 complaint (&symfile_complaints,
3e43a32a
MS
7990 _("template parameter missing "
7991 "DW_AT_const_value"));
98bfdba5
PA
7992 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7993 continue;
7994 }
7995
7996 dwarf2_const_value_attr (attr, type, name,
7997 &cu->comp_unit_obstack, cu,
7998 &value, &bytes, &baton);
7999
8000 if (TYPE_NOSIGN (type))
8001 /* GDB prints characters as NUMBER 'CHAR'. If that's
8002 changed, this can use value_print instead. */
8003 c_printchar (value, type, buf);
8004 else
8005 {
8006 struct value_print_options opts;
8007
8008 if (baton != NULL)
8009 v = dwarf2_evaluate_loc_desc (type, NULL,
8010 baton->data,
8011 baton->size,
8012 baton->per_cu);
8013 else if (bytes != NULL)
8014 {
8015 v = allocate_value (type);
8016 memcpy (value_contents_writeable (v), bytes,
8017 TYPE_LENGTH (type));
8018 }
8019 else
8020 v = value_from_longest (type, value);
8021
3e43a32a
MS
8022 /* Specify decimal so that we do not depend on
8023 the radix. */
98bfdba5
PA
8024 get_formatted_print_options (&opts, 'd');
8025 opts.raw = 1;
8026 value_print (v, buf, &opts);
8027 release_value (v);
8028 value_free (v);
8029 }
8030 }
8031
8032 die->building_fullname = 0;
8033
8034 if (!first)
8035 {
8036 /* Close the argument list, with a space if necessary
8037 (nested templates). */
8038 char last_char = '\0';
8039 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8040 if (last_char == '>')
8041 fputs_unfiltered (" >", buf);
8042 else
8043 fputs_unfiltered (">", buf);
8044 }
8045 }
8046
94af9270
KS
8047 /* For Java and C++ methods, append formal parameter type
8048 information, if PHYSNAME. */
6e70227d 8049
94af9270
KS
8050 if (physname && die->tag == DW_TAG_subprogram
8051 && (cu->language == language_cplus
8052 || cu->language == language_java))
8053 {
8054 struct type *type = read_type_die (die, cu);
8055
79d43c61
TT
8056 c_type_print_args (type, buf, 1, cu->language,
8057 &type_print_raw_options);
94af9270
KS
8058
8059 if (cu->language == language_java)
8060 {
8061 /* For java, we must append the return type to method
0963b4bd 8062 names. */
94af9270
KS
8063 if (die->tag == DW_TAG_subprogram)
8064 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
79d43c61 8065 0, 0, &type_print_raw_options);
94af9270
KS
8066 }
8067 else if (cu->language == language_cplus)
8068 {
60430eff
DJ
8069 /* Assume that an artificial first parameter is
8070 "this", but do not crash if it is not. RealView
8071 marks unnamed (and thus unused) parameters as
8072 artificial; there is no way to differentiate
8073 the two cases. */
94af9270
KS
8074 if (TYPE_NFIELDS (type) > 0
8075 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 8076 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
8077 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8078 0))))
94af9270
KS
8079 fputs_unfiltered (" const", buf);
8080 }
8081 }
8082
bb5ed363 8083 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
8084 &length);
8085 ui_file_delete (buf);
8086
8087 if (cu->language == language_cplus)
8088 {
15d034d0 8089 const char *cname
94af9270 8090 = dwarf2_canonicalize_name (name, cu,
bb5ed363 8091 &objfile->objfile_obstack);
9a619af0 8092
94af9270
KS
8093 if (cname != NULL)
8094 name = cname;
8095 }
8096 }
8097 }
8098
8099 return name;
8100}
8101
0114d602
DJ
8102/* Return the fully qualified name of DIE, based on its DW_AT_name.
8103 If scope qualifiers are appropriate they will be added. The result
8104 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
8105 not have a name. NAME may either be from a previous call to
8106 dwarf2_name or NULL.
8107
0963b4bd 8108 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
8109
8110static const char *
15d034d0 8111dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 8112{
94af9270
KS
8113 return dwarf2_compute_name (name, die, cu, 0);
8114}
0114d602 8115
94af9270
KS
8116/* Construct a physname for the given DIE in CU. NAME may either be
8117 from a previous call to dwarf2_name or NULL. The result will be
8118 allocated on the objfile_objstack or NULL if the DIE does not have a
8119 name.
0114d602 8120
94af9270 8121 The output string will be canonicalized (if C++/Java). */
0114d602 8122
94af9270 8123static const char *
15d034d0 8124dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 8125{
bb5ed363 8126 struct objfile *objfile = cu->objfile;
900e11f9
JK
8127 struct attribute *attr;
8128 const char *retval, *mangled = NULL, *canon = NULL;
8129 struct cleanup *back_to;
8130 int need_copy = 1;
8131
8132 /* In this case dwarf2_compute_name is just a shortcut not building anything
8133 on its own. */
8134 if (!die_needs_namespace (die, cu))
8135 return dwarf2_compute_name (name, die, cu, 1);
8136
8137 back_to = make_cleanup (null_cleanup, NULL);
8138
8139 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8140 if (!attr)
8141 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8142
8143 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8144 has computed. */
8145 if (attr && DW_STRING (attr))
8146 {
8147 char *demangled;
8148
8149 mangled = DW_STRING (attr);
8150
8151 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8152 type. It is easier for GDB users to search for such functions as
8153 `name(params)' than `long name(params)'. In such case the minimal
8154 symbol names do not match the full symbol names but for template
8155 functions there is never a need to look up their definition from their
8156 declaration so the only disadvantage remains the minimal symbol
8157 variant `long name(params)' does not have the proper inferior type.
8158 */
8159
a766d390
DE
8160 if (cu->language == language_go)
8161 {
8162 /* This is a lie, but we already lie to the caller new_symbol_full.
8163 new_symbol_full assumes we return the mangled name.
8164 This just undoes that lie until things are cleaned up. */
8165 demangled = NULL;
8166 }
8167 else
8168 {
8de20a37
TT
8169 demangled = gdb_demangle (mangled,
8170 (DMGL_PARAMS | DMGL_ANSI
8171 | (cu->language == language_java
8172 ? DMGL_JAVA | DMGL_RET_POSTFIX
8173 : DMGL_RET_DROP)));
a766d390 8174 }
900e11f9
JK
8175 if (demangled)
8176 {
8177 make_cleanup (xfree, demangled);
8178 canon = demangled;
8179 }
8180 else
8181 {
8182 canon = mangled;
8183 need_copy = 0;
8184 }
8185 }
8186
8187 if (canon == NULL || check_physname)
8188 {
8189 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8190
8191 if (canon != NULL && strcmp (physname, canon) != 0)
8192 {
8193 /* It may not mean a bug in GDB. The compiler could also
8194 compute DW_AT_linkage_name incorrectly. But in such case
8195 GDB would need to be bug-to-bug compatible. */
8196
8197 complaint (&symfile_complaints,
8198 _("Computed physname <%s> does not match demangled <%s> "
8199 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 8200 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
8201
8202 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8203 is available here - over computed PHYSNAME. It is safer
8204 against both buggy GDB and buggy compilers. */
8205
8206 retval = canon;
8207 }
8208 else
8209 {
8210 retval = physname;
8211 need_copy = 0;
8212 }
8213 }
8214 else
8215 retval = canon;
8216
8217 if (need_copy)
10f0c4bb 8218 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
900e11f9
JK
8219
8220 do_cleanups (back_to);
8221 return retval;
0114d602
DJ
8222}
8223
27aa8d6a
SW
8224/* Read the import statement specified by the given die and record it. */
8225
8226static void
8227read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8228{
bb5ed363 8229 struct objfile *objfile = cu->objfile;
27aa8d6a 8230 struct attribute *import_attr;
32019081 8231 struct die_info *imported_die, *child_die;
de4affc9 8232 struct dwarf2_cu *imported_cu;
27aa8d6a 8233 const char *imported_name;
794684b6 8234 const char *imported_name_prefix;
13387711
SW
8235 const char *canonical_name;
8236 const char *import_alias;
8237 const char *imported_declaration = NULL;
794684b6 8238 const char *import_prefix;
32019081
JK
8239 VEC (const_char_ptr) *excludes = NULL;
8240 struct cleanup *cleanups;
13387711 8241
27aa8d6a
SW
8242 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8243 if (import_attr == NULL)
8244 {
8245 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8246 dwarf_tag_name (die->tag));
8247 return;
8248 }
8249
de4affc9
CC
8250 imported_cu = cu;
8251 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8252 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
8253 if (imported_name == NULL)
8254 {
8255 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8256
8257 The import in the following code:
8258 namespace A
8259 {
8260 typedef int B;
8261 }
8262
8263 int main ()
8264 {
8265 using A::B;
8266 B b;
8267 return b;
8268 }
8269
8270 ...
8271 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8272 <52> DW_AT_decl_file : 1
8273 <53> DW_AT_decl_line : 6
8274 <54> DW_AT_import : <0x75>
8275 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8276 <59> DW_AT_name : B
8277 <5b> DW_AT_decl_file : 1
8278 <5c> DW_AT_decl_line : 2
8279 <5d> DW_AT_type : <0x6e>
8280 ...
8281 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8282 <76> DW_AT_byte_size : 4
8283 <77> DW_AT_encoding : 5 (signed)
8284
8285 imports the wrong die ( 0x75 instead of 0x58 ).
8286 This case will be ignored until the gcc bug is fixed. */
8287 return;
8288 }
8289
82856980
SW
8290 /* Figure out the local name after import. */
8291 import_alias = dwarf2_name (die, cu);
27aa8d6a 8292
794684b6
SW
8293 /* Figure out where the statement is being imported to. */
8294 import_prefix = determine_prefix (die, cu);
8295
8296 /* Figure out what the scope of the imported die is and prepend it
8297 to the name of the imported die. */
de4affc9 8298 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 8299
f55ee35c
JK
8300 if (imported_die->tag != DW_TAG_namespace
8301 && imported_die->tag != DW_TAG_module)
794684b6 8302 {
13387711
SW
8303 imported_declaration = imported_name;
8304 canonical_name = imported_name_prefix;
794684b6 8305 }
13387711 8306 else if (strlen (imported_name_prefix) > 0)
12aaed36
TT
8307 canonical_name = obconcat (&objfile->objfile_obstack,
8308 imported_name_prefix, "::", imported_name,
8309 (char *) NULL);
13387711
SW
8310 else
8311 canonical_name = imported_name;
794684b6 8312
32019081
JK
8313 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8314
8315 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8316 for (child_die = die->child; child_die && child_die->tag;
8317 child_die = sibling_die (child_die))
8318 {
8319 /* DWARF-4: A Fortran use statement with a “rename list” may be
8320 represented by an imported module entry with an import attribute
8321 referring to the module and owned entries corresponding to those
8322 entities that are renamed as part of being imported. */
8323
8324 if (child_die->tag != DW_TAG_imported_declaration)
8325 {
8326 complaint (&symfile_complaints,
8327 _("child DW_TAG_imported_declaration expected "
8328 "- DIE at 0x%x [in module %s]"),
b64f50a1 8329 child_die->offset.sect_off, objfile->name);
32019081
JK
8330 continue;
8331 }
8332
8333 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8334 if (import_attr == NULL)
8335 {
8336 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8337 dwarf_tag_name (child_die->tag));
8338 continue;
8339 }
8340
8341 imported_cu = cu;
8342 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8343 &imported_cu);
8344 imported_name = dwarf2_name (imported_die, imported_cu);
8345 if (imported_name == NULL)
8346 {
8347 complaint (&symfile_complaints,
8348 _("child DW_TAG_imported_declaration has unknown "
8349 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 8350 child_die->offset.sect_off, objfile->name);
32019081
JK
8351 continue;
8352 }
8353
8354 VEC_safe_push (const_char_ptr, excludes, imported_name);
8355
8356 process_die (child_die, cu);
8357 }
8358
c0cc3a76
SW
8359 cp_add_using_directive (import_prefix,
8360 canonical_name,
8361 import_alias,
13387711 8362 imported_declaration,
32019081 8363 excludes,
12aaed36 8364 0,
bb5ed363 8365 &objfile->objfile_obstack);
32019081
JK
8366
8367 do_cleanups (cleanups);
27aa8d6a
SW
8368}
8369
f4dc4d17 8370/* Cleanup function for handle_DW_AT_stmt_list. */
ae2de4f8 8371
cb1df416
DJ
8372static void
8373free_cu_line_header (void *arg)
8374{
8375 struct dwarf2_cu *cu = arg;
8376
8377 free_line_header (cu->line_header);
8378 cu->line_header = NULL;
8379}
8380
1b80a9fa
JK
8381/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8382 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8383 this, it was first present in GCC release 4.3.0. */
8384
8385static int
8386producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8387{
8388 if (!cu->checked_producer)
8389 check_producer (cu);
8390
8391 return cu->producer_is_gcc_lt_4_3;
8392}
8393
9291a0cd
TT
8394static void
8395find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
15d034d0 8396 const char **name, const char **comp_dir)
9291a0cd
TT
8397{
8398 struct attribute *attr;
8399
8400 *name = NULL;
8401 *comp_dir = NULL;
8402
8403 /* Find the filename. Do not use dwarf2_name here, since the filename
8404 is not a source language identifier. */
8405 attr = dwarf2_attr (die, DW_AT_name, cu);
8406 if (attr)
8407 {
8408 *name = DW_STRING (attr);
8409 }
8410
8411 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8412 if (attr)
8413 *comp_dir = DW_STRING (attr);
1b80a9fa
JK
8414 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8415 && IS_ABSOLUTE_PATH (*name))
9291a0cd 8416 {
15d034d0
TT
8417 char *d = ldirname (*name);
8418
8419 *comp_dir = d;
8420 if (d != NULL)
8421 make_cleanup (xfree, d);
9291a0cd
TT
8422 }
8423 if (*comp_dir != NULL)
8424 {
8425 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8426 directory, get rid of it. */
8427 char *cp = strchr (*comp_dir, ':');
8428
8429 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8430 *comp_dir = cp + 1;
8431 }
8432
8433 if (*name == NULL)
8434 *name = "<unknown>";
8435}
8436
f4dc4d17
DE
8437/* Handle DW_AT_stmt_list for a compilation unit.
8438 DIE is the DW_TAG_compile_unit die for CU.
f3f5162e
DE
8439 COMP_DIR is the compilation directory.
8440 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
8441
8442static void
8443handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
b385a60d 8444 const char *comp_dir) /* ARI: editCase function */
2ab95328
TT
8445{
8446 struct attribute *attr;
2ab95328 8447
f4dc4d17
DE
8448 gdb_assert (! cu->per_cu->is_debug_types);
8449
2ab95328
TT
8450 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8451 if (attr)
8452 {
8453 unsigned int line_offset = DW_UNSND (attr);
8454 struct line_header *line_header
3019eac3 8455 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
8456
8457 if (line_header)
dee91e82
DE
8458 {
8459 cu->line_header = line_header;
8460 make_cleanup (free_cu_line_header, cu);
f4dc4d17 8461 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
dee91e82 8462 }
2ab95328
TT
8463 }
8464}
8465
95554aad 8466/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 8467
c906108c 8468static void
e7c27a73 8469read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8470{
dee91e82 8471 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 8472 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 8473 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
8474 CORE_ADDR highpc = ((CORE_ADDR) 0);
8475 struct attribute *attr;
15d034d0
TT
8476 const char *name = NULL;
8477 const char *comp_dir = NULL;
c906108c
SS
8478 struct die_info *child_die;
8479 bfd *abfd = objfile->obfd;
e142c38c 8480 CORE_ADDR baseaddr;
6e70227d 8481
e142c38c 8482 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8483
fae299cd 8484 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
8485
8486 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8487 from finish_block. */
2acceee2 8488 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
8489 lowpc = highpc;
8490 lowpc += baseaddr;
8491 highpc += baseaddr;
8492
9291a0cd 8493 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 8494
95554aad 8495 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 8496
f4b8a18d
KW
8497 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8498 standardised yet. As a workaround for the language detection we fall
8499 back to the DW_AT_producer string. */
8500 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8501 cu->language = language_opencl;
8502
3019eac3
DE
8503 /* Similar hack for Go. */
8504 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8505 set_cu_language (DW_LANG_Go, cu);
8506
f4dc4d17 8507 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
3019eac3
DE
8508
8509 /* Decode line number information if present. We do this before
8510 processing child DIEs, so that the line header table is available
8511 for DW_AT_decl_file. */
f4dc4d17 8512 handle_DW_AT_stmt_list (die, cu, comp_dir);
3019eac3
DE
8513
8514 /* Process all dies in compilation unit. */
8515 if (die->child != NULL)
8516 {
8517 child_die = die->child;
8518 while (child_die && child_die->tag)
8519 {
8520 process_die (child_die, cu);
8521 child_die = sibling_die (child_die);
8522 }
8523 }
8524
8525 /* Decode macro information, if present. Dwarf 2 macro information
8526 refers to information in the line number info statement program
8527 header, so we can only read it if we've read the header
8528 successfully. */
8529 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8530 if (attr && cu->line_header)
8531 {
8532 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8533 complaint (&symfile_complaints,
8534 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8535
09262596 8536 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
8537 }
8538 else
8539 {
8540 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8541 if (attr && cu->line_header)
8542 {
8543 unsigned int macro_offset = DW_UNSND (attr);
8544
09262596 8545 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
8546 }
8547 }
8548
8549 do_cleanups (back_to);
8550}
8551
f4dc4d17
DE
8552/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8553 Create the set of symtabs used by this TU, or if this TU is sharing
8554 symtabs with another TU and the symtabs have already been created
8555 then restore those symtabs in the line header.
8556 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
8557
8558static void
f4dc4d17 8559setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 8560{
f4dc4d17
DE
8561 struct objfile *objfile = dwarf2_per_objfile->objfile;
8562 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8563 struct type_unit_group *tu_group;
8564 int first_time;
8565 struct line_header *lh;
3019eac3 8566 struct attribute *attr;
f4dc4d17 8567 unsigned int i, line_offset;
0186c6a7 8568 struct signatured_type *sig_type;
3019eac3 8569
f4dc4d17 8570 gdb_assert (per_cu->is_debug_types);
0186c6a7 8571 sig_type = (struct signatured_type *) per_cu;
3019eac3 8572
f4dc4d17 8573 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 8574
f4dc4d17 8575 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 8576 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
8577 if (sig_type->type_unit_group == NULL)
8578 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8579 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
8580
8581 /* If we've already processed this stmt_list there's no real need to
8582 do it again, we could fake it and just recreate the part we need
8583 (file name,index -> symtab mapping). If data shows this optimization
8584 is useful we can do it then. */
8585 first_time = tu_group->primary_symtab == NULL;
8586
8587 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8588 debug info. */
8589 lh = NULL;
8590 if (attr != NULL)
3019eac3 8591 {
f4dc4d17
DE
8592 line_offset = DW_UNSND (attr);
8593 lh = dwarf_decode_line_header (line_offset, cu);
8594 }
8595 if (lh == NULL)
8596 {
8597 if (first_time)
8598 dwarf2_start_symtab (cu, "", NULL, 0);
8599 else
8600 {
8601 gdb_assert (tu_group->symtabs == NULL);
8602 restart_symtab (0);
8603 }
8604 /* Note: The primary symtab will get allocated at the end. */
8605 return;
3019eac3
DE
8606 }
8607
f4dc4d17
DE
8608 cu->line_header = lh;
8609 make_cleanup (free_cu_line_header, cu);
3019eac3 8610
f4dc4d17
DE
8611 if (first_time)
8612 {
8613 dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 8614
f4dc4d17
DE
8615 tu_group->num_symtabs = lh->num_file_names;
8616 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
3019eac3 8617
f4dc4d17
DE
8618 for (i = 0; i < lh->num_file_names; ++i)
8619 {
d521ce57 8620 const char *dir = NULL;
f4dc4d17 8621 struct file_entry *fe = &lh->file_names[i];
3019eac3 8622
f4dc4d17
DE
8623 if (fe->dir_index)
8624 dir = lh->include_dirs[fe->dir_index - 1];
8625 dwarf2_start_subfile (fe->name, dir, NULL);
3019eac3 8626
f4dc4d17
DE
8627 /* Note: We don't have to watch for the main subfile here, type units
8628 don't have DW_AT_name. */
3019eac3 8629
f4dc4d17
DE
8630 if (current_subfile->symtab == NULL)
8631 {
8632 /* NOTE: start_subfile will recognize when it's been passed
8633 a file it has already seen. So we can't assume there's a
8634 simple mapping from lh->file_names to subfiles,
8635 lh->file_names may contain dups. */
8636 current_subfile->symtab = allocate_symtab (current_subfile->name,
8637 objfile);
8638 }
8639
8640 fe->symtab = current_subfile->symtab;
8641 tu_group->symtabs[i] = fe->symtab;
8642 }
8643 }
8644 else
3019eac3 8645 {
f4dc4d17
DE
8646 restart_symtab (0);
8647
8648 for (i = 0; i < lh->num_file_names; ++i)
8649 {
8650 struct file_entry *fe = &lh->file_names[i];
8651
8652 fe->symtab = tu_group->symtabs[i];
8653 }
3019eac3
DE
8654 }
8655
f4dc4d17
DE
8656 /* The main symtab is allocated last. Type units don't have DW_AT_name
8657 so they don't have a "real" (so to speak) symtab anyway.
8658 There is later code that will assign the main symtab to all symbols
8659 that don't have one. We need to handle the case of a symbol with a
8660 missing symtab (DW_AT_decl_file) anyway. */
8661}
3019eac3 8662
f4dc4d17
DE
8663/* Process DW_TAG_type_unit.
8664 For TUs we want to skip the first top level sibling if it's not the
8665 actual type being defined by this TU. In this case the first top
8666 level sibling is there to provide context only. */
3019eac3 8667
f4dc4d17
DE
8668static void
8669read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8670{
8671 struct die_info *child_die;
3019eac3 8672
f4dc4d17
DE
8673 prepare_one_comp_unit (cu, die, language_minimal);
8674
8675 /* Initialize (or reinitialize) the machinery for building symtabs.
8676 We do this before processing child DIEs, so that the line header table
8677 is available for DW_AT_decl_file. */
8678 setup_type_unit_groups (die, cu);
8679
8680 if (die->child != NULL)
8681 {
8682 child_die = die->child;
8683 while (child_die && child_die->tag)
8684 {
8685 process_die (child_die, cu);
8686 child_die = sibling_die (child_die);
8687 }
8688 }
3019eac3
DE
8689}
8690\f
80626a55
DE
8691/* DWO/DWP files.
8692
8693 http://gcc.gnu.org/wiki/DebugFission
8694 http://gcc.gnu.org/wiki/DebugFissionDWP
8695
8696 To simplify handling of both DWO files ("object" files with the DWARF info)
8697 and DWP files (a file with the DWOs packaged up into one file), we treat
8698 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
8699
8700static hashval_t
8701hash_dwo_file (const void *item)
8702{
8703 const struct dwo_file *dwo_file = item;
a2ce51a0 8704 hashval_t hash;
3019eac3 8705
a2ce51a0
DE
8706 hash = htab_hash_string (dwo_file->dwo_name);
8707 if (dwo_file->comp_dir != NULL)
8708 hash += htab_hash_string (dwo_file->comp_dir);
8709 return hash;
3019eac3
DE
8710}
8711
8712static int
8713eq_dwo_file (const void *item_lhs, const void *item_rhs)
8714{
8715 const struct dwo_file *lhs = item_lhs;
8716 const struct dwo_file *rhs = item_rhs;
8717
a2ce51a0
DE
8718 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8719 return 0;
8720 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8721 return lhs->comp_dir == rhs->comp_dir;
8722 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
8723}
8724
8725/* Allocate a hash table for DWO files. */
8726
8727static htab_t
8728allocate_dwo_file_hash_table (void)
8729{
8730 struct objfile *objfile = dwarf2_per_objfile->objfile;
8731
8732 return htab_create_alloc_ex (41,
8733 hash_dwo_file,
8734 eq_dwo_file,
8735 NULL,
8736 &objfile->objfile_obstack,
8737 hashtab_obstack_allocate,
8738 dummy_obstack_deallocate);
8739}
8740
80626a55
DE
8741/* Lookup DWO file DWO_NAME. */
8742
8743static void **
0ac5b59e 8744lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
80626a55
DE
8745{
8746 struct dwo_file find_entry;
8747 void **slot;
8748
8749 if (dwarf2_per_objfile->dwo_files == NULL)
8750 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8751
8752 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
8753 find_entry.dwo_name = dwo_name;
8754 find_entry.comp_dir = comp_dir;
80626a55
DE
8755 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8756
8757 return slot;
8758}
8759
3019eac3
DE
8760static hashval_t
8761hash_dwo_unit (const void *item)
8762{
8763 const struct dwo_unit *dwo_unit = item;
8764
8765 /* This drops the top 32 bits of the id, but is ok for a hash. */
8766 return dwo_unit->signature;
8767}
8768
8769static int
8770eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8771{
8772 const struct dwo_unit *lhs = item_lhs;
8773 const struct dwo_unit *rhs = item_rhs;
8774
8775 /* The signature is assumed to be unique within the DWO file.
8776 So while object file CU dwo_id's always have the value zero,
8777 that's OK, assuming each object file DWO file has only one CU,
8778 and that's the rule for now. */
8779 return lhs->signature == rhs->signature;
8780}
8781
8782/* Allocate a hash table for DWO CUs,TUs.
8783 There is one of these tables for each of CUs,TUs for each DWO file. */
8784
8785static htab_t
8786allocate_dwo_unit_table (struct objfile *objfile)
8787{
8788 /* Start out with a pretty small number.
8789 Generally DWO files contain only one CU and maybe some TUs. */
8790 return htab_create_alloc_ex (3,
8791 hash_dwo_unit,
8792 eq_dwo_unit,
8793 NULL,
8794 &objfile->objfile_obstack,
8795 hashtab_obstack_allocate,
8796 dummy_obstack_deallocate);
8797}
8798
80626a55 8799/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 8800
19c3d4c9 8801struct create_dwo_cu_data
3019eac3
DE
8802{
8803 struct dwo_file *dwo_file;
19c3d4c9 8804 struct dwo_unit dwo_unit;
3019eac3
DE
8805};
8806
19c3d4c9 8807/* die_reader_func for create_dwo_cu. */
3019eac3
DE
8808
8809static void
19c3d4c9
DE
8810create_dwo_cu_reader (const struct die_reader_specs *reader,
8811 const gdb_byte *info_ptr,
8812 struct die_info *comp_unit_die,
8813 int has_children,
8814 void *datap)
3019eac3
DE
8815{
8816 struct dwarf2_cu *cu = reader->cu;
8817 struct objfile *objfile = dwarf2_per_objfile->objfile;
8818 sect_offset offset = cu->per_cu->offset;
8a0459fd 8819 struct dwarf2_section_info *section = cu->per_cu->section;
19c3d4c9 8820 struct create_dwo_cu_data *data = datap;
3019eac3 8821 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 8822 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 8823 struct attribute *attr;
3019eac3
DE
8824
8825 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8826 if (attr == NULL)
8827 {
19c3d4c9
DE
8828 complaint (&symfile_complaints,
8829 _("Dwarf Error: debug entry at offset 0x%x is missing"
8830 " its dwo_id [in module %s]"),
8831 offset.sect_off, dwo_file->dwo_name);
3019eac3
DE
8832 return;
8833 }
8834
3019eac3
DE
8835 dwo_unit->dwo_file = dwo_file;
8836 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 8837 dwo_unit->section = section;
3019eac3
DE
8838 dwo_unit->offset = offset;
8839 dwo_unit->length = cu->per_cu->length;
8840
09406207 8841 if (dwarf2_read_debug)
4031ecc5
DE
8842 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8843 offset.sect_off, hex_string (dwo_unit->signature));
3019eac3
DE
8844}
8845
19c3d4c9
DE
8846/* Create the dwo_unit for the lone CU in DWO_FILE.
8847 Note: This function processes DWO files only, not DWP files. */
3019eac3 8848
19c3d4c9
DE
8849static struct dwo_unit *
8850create_dwo_cu (struct dwo_file *dwo_file)
3019eac3
DE
8851{
8852 struct objfile *objfile = dwarf2_per_objfile->objfile;
8853 struct dwarf2_section_info *section = &dwo_file->sections.info;
8854 bfd *abfd;
8855 htab_t cu_htab;
d521ce57 8856 const gdb_byte *info_ptr, *end_ptr;
19c3d4c9
DE
8857 struct create_dwo_cu_data create_dwo_cu_data;
8858 struct dwo_unit *dwo_unit;
3019eac3
DE
8859
8860 dwarf2_read_section (objfile, section);
8861 info_ptr = section->buffer;
8862
8863 if (info_ptr == NULL)
8864 return NULL;
8865
8866 /* We can't set abfd until now because the section may be empty or
8867 not present, in which case section->asection will be NULL. */
8868 abfd = section->asection->owner;
8869
09406207 8870 if (dwarf2_read_debug)
19c3d4c9
DE
8871 {
8872 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8873 bfd_section_name (abfd, section->asection),
8874 bfd_get_filename (abfd));
8875 }
3019eac3 8876
19c3d4c9
DE
8877 create_dwo_cu_data.dwo_file = dwo_file;
8878 dwo_unit = NULL;
3019eac3
DE
8879
8880 end_ptr = info_ptr + section->size;
8881 while (info_ptr < end_ptr)
8882 {
8883 struct dwarf2_per_cu_data per_cu;
8884
19c3d4c9
DE
8885 memset (&create_dwo_cu_data.dwo_unit, 0,
8886 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3
DE
8887 memset (&per_cu, 0, sizeof (per_cu));
8888 per_cu.objfile = objfile;
8889 per_cu.is_debug_types = 0;
8890 per_cu.offset.sect_off = info_ptr - section->buffer;
8a0459fd 8891 per_cu.section = section;
3019eac3
DE
8892
8893 init_cutu_and_read_dies_no_follow (&per_cu,
8894 &dwo_file->sections.abbrev,
8895 dwo_file,
19c3d4c9
DE
8896 create_dwo_cu_reader,
8897 &create_dwo_cu_data);
8898
8899 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8900 {
8901 /* If we've already found one, complain. We only support one
8902 because having more than one requires hacking the dwo_name of
8903 each to match, which is highly unlikely to happen. */
8904 if (dwo_unit != NULL)
8905 {
8906 complaint (&symfile_complaints,
8907 _("Multiple CUs in DWO file %s [in module %s]"),
8908 dwo_file->dwo_name, objfile->name);
8909 break;
8910 }
8911
8912 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8913 *dwo_unit = create_dwo_cu_data.dwo_unit;
8914 }
3019eac3
DE
8915
8916 info_ptr += per_cu.length;
8917 }
8918
19c3d4c9 8919 return dwo_unit;
3019eac3
DE
8920}
8921
80626a55
DE
8922/* DWP file .debug_{cu,tu}_index section format:
8923 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8924
d2415c6c
DE
8925 DWP Version 1:
8926
80626a55
DE
8927 Both index sections have the same format, and serve to map a 64-bit
8928 signature to a set of section numbers. Each section begins with a header,
8929 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8930 indexes, and a pool of 32-bit section numbers. The index sections will be
8931 aligned at 8-byte boundaries in the file.
8932
d2415c6c
DE
8933 The index section header consists of:
8934
8935 V, 32 bit version number
8936 -, 32 bits unused
8937 N, 32 bit number of compilation units or type units in the index
8938 M, 32 bit number of slots in the hash table
80626a55 8939
d2415c6c 8940 Numbers are recorded using the byte order of the application binary.
80626a55 8941
d2415c6c 8942 We assume that N and M will not exceed 2^32 - 1.
80626a55 8943
d2415c6c 8944 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
80626a55 8945
d2415c6c
DE
8946 The hash table begins at offset 16 in the section, and consists of an array
8947 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8948 order of the application binary). Unused slots in the hash table are 0.
8949 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 8950
d2415c6c
DE
8951 The parallel table begins immediately after the hash table
8952 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8953 array of 32-bit indexes (using the byte order of the application binary),
8954 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8955 table contains a 32-bit index into the pool of section numbers. For unused
8956 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 8957
d2415c6c
DE
8958 Given a 64-bit compilation unit signature or a type signature S, an entry
8959 in the hash table is located as follows:
80626a55 8960
d2415c6c
DE
8961 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8962 the low-order k bits all set to 1.
80626a55 8963
d2415c6c 8964 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 8965
d2415c6c
DE
8966 3) If the hash table entry at index H matches the signature, use that
8967 entry. If the hash table entry at index H is unused (all zeroes),
8968 terminate the search: the signature is not present in the table.
80626a55 8969
d2415c6c 8970 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 8971
d2415c6c
DE
8972 Because M > N and H' and M are relatively prime, the search is guaranteed
8973 to stop at an unused slot or find the match.
80626a55 8974
d2415c6c
DE
8975 The pool of section numbers begins immediately following the hash table
8976 (at offset 16 + 12 * M from the beginning of the section). The pool of
8977 section numbers consists of an array of 32-bit words (using the byte order
8978 of the application binary). Each item in the array is indexed starting
8979 from 0. The hash table entry provides the index of the first section
8980 number in the set. Additional section numbers in the set follow, and the
8981 set is terminated by a 0 entry (section number 0 is not used in ELF).
80626a55 8982
d2415c6c
DE
8983 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8984 section must be the first entry in the set, and the .debug_abbrev.dwo must
8985 be the second entry. Other members of the set may follow in any order. */
80626a55
DE
8986
8987/* Create a hash table to map DWO IDs to their CU/TU entry in
8988 .debug_{info,types}.dwo in DWP_FILE.
8989 Returns NULL if there isn't one.
8990 Note: This function processes DWP files only, not DWO files. */
8991
8992static struct dwp_hash_table *
8993create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8994{
8995 struct objfile *objfile = dwarf2_per_objfile->objfile;
8996 bfd *dbfd = dwp_file->dbfd;
948f8e3d 8997 const gdb_byte *index_ptr, *index_end;
80626a55
DE
8998 struct dwarf2_section_info *index;
8999 uint32_t version, nr_units, nr_slots;
9000 struct dwp_hash_table *htab;
9001
9002 if (is_debug_types)
9003 index = &dwp_file->sections.tu_index;
9004 else
9005 index = &dwp_file->sections.cu_index;
9006
9007 if (dwarf2_section_empty_p (index))
9008 return NULL;
9009 dwarf2_read_section (objfile, index);
9010
9011 index_ptr = index->buffer;
9012 index_end = index_ptr + index->size;
9013
9014 version = read_4_bytes (dbfd, index_ptr);
9015 index_ptr += 8; /* Skip the unused word. */
9016 nr_units = read_4_bytes (dbfd, index_ptr);
9017 index_ptr += 4;
9018 nr_slots = read_4_bytes (dbfd, index_ptr);
9019 index_ptr += 4;
9020
9021 if (version != 1)
9022 {
21aa081e 9023 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 9024 " [in module %s]"),
21aa081e 9025 pulongest (version), dwp_file->name);
80626a55
DE
9026 }
9027 if (nr_slots != (nr_slots & -nr_slots))
9028 {
21aa081e 9029 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 9030 " is not power of 2 [in module %s]"),
21aa081e 9031 pulongest (nr_slots), dwp_file->name);
80626a55
DE
9032 }
9033
9034 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9035 htab->nr_units = nr_units;
9036 htab->nr_slots = nr_slots;
9037 htab->hash_table = index_ptr;
9038 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9039 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9040
9041 return htab;
9042}
9043
9044/* Update SECTIONS with the data from SECTP.
9045
9046 This function is like the other "locate" section routines that are
9047 passed to bfd_map_over_sections, but in this context the sections to
9048 read comes from the DWP hash table, not the full ELF section table.
9049
9050 The result is non-zero for success, or zero if an error was found. */
9051
9052static int
9053locate_virtual_dwo_sections (asection *sectp,
9054 struct virtual_dwo_sections *sections)
9055{
9056 const struct dwop_section_names *names = &dwop_section_names;
9057
9058 if (section_is_p (sectp->name, &names->abbrev_dwo))
9059 {
9060 /* There can be only one. */
9061 if (sections->abbrev.asection != NULL)
9062 return 0;
9063 sections->abbrev.asection = sectp;
9064 sections->abbrev.size = bfd_get_section_size (sectp);
9065 }
9066 else if (section_is_p (sectp->name, &names->info_dwo)
9067 || section_is_p (sectp->name, &names->types_dwo))
9068 {
9069 /* There can be only one. */
9070 if (sections->info_or_types.asection != NULL)
9071 return 0;
9072 sections->info_or_types.asection = sectp;
9073 sections->info_or_types.size = bfd_get_section_size (sectp);
9074 }
9075 else if (section_is_p (sectp->name, &names->line_dwo))
9076 {
9077 /* There can be only one. */
9078 if (sections->line.asection != NULL)
9079 return 0;
9080 sections->line.asection = sectp;
9081 sections->line.size = bfd_get_section_size (sectp);
9082 }
9083 else if (section_is_p (sectp->name, &names->loc_dwo))
9084 {
9085 /* There can be only one. */
9086 if (sections->loc.asection != NULL)
9087 return 0;
9088 sections->loc.asection = sectp;
9089 sections->loc.size = bfd_get_section_size (sectp);
9090 }
9091 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9092 {
9093 /* There can be only one. */
9094 if (sections->macinfo.asection != NULL)
9095 return 0;
9096 sections->macinfo.asection = sectp;
9097 sections->macinfo.size = bfd_get_section_size (sectp);
9098 }
9099 else if (section_is_p (sectp->name, &names->macro_dwo))
9100 {
9101 /* There can be only one. */
9102 if (sections->macro.asection != NULL)
9103 return 0;
9104 sections->macro.asection = sectp;
9105 sections->macro.size = bfd_get_section_size (sectp);
9106 }
9107 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9108 {
9109 /* There can be only one. */
9110 if (sections->str_offsets.asection != NULL)
9111 return 0;
9112 sections->str_offsets.asection = sectp;
9113 sections->str_offsets.size = bfd_get_section_size (sectp);
9114 }
9115 else
9116 {
9117 /* No other kind of section is valid. */
9118 return 0;
9119 }
9120
9121 return 1;
9122}
9123
9124/* Create a dwo_unit object for the DWO with signature SIGNATURE.
9125 HTAB is the hash table from the DWP file.
0ac5b59e
DE
9126 SECTION_INDEX is the index of the DWO in HTAB.
9127 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
80626a55
DE
9128
9129static struct dwo_unit *
9130create_dwo_in_dwp (struct dwp_file *dwp_file,
9131 const struct dwp_hash_table *htab,
9132 uint32_t section_index,
0ac5b59e 9133 const char *comp_dir,
80626a55
DE
9134 ULONGEST signature, int is_debug_types)
9135{
9136 struct objfile *objfile = dwarf2_per_objfile->objfile;
9137 bfd *dbfd = dwp_file->dbfd;
9138 const char *kind = is_debug_types ? "TU" : "CU";
9139 struct dwo_file *dwo_file;
9140 struct dwo_unit *dwo_unit;
9141 struct virtual_dwo_sections sections;
9142 void **dwo_file_slot;
9143 char *virtual_dwo_name;
9144 struct dwarf2_section_info *cutu;
9145 struct cleanup *cleanups;
9146 int i;
9147
9148 if (dwarf2_read_debug)
9149 {
21aa081e 9150 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
80626a55 9151 kind,
21aa081e 9152 pulongest (section_index), hex_string (signature),
80626a55
DE
9153 dwp_file->name);
9154 }
9155
9156 /* Fetch the sections of this DWO.
9157 Put a limit on the number of sections we look for so that bad data
9158 doesn't cause us to loop forever. */
9159
9160#define MAX_NR_DWO_SECTIONS \
9161 (1 /* .debug_info or .debug_types */ \
9162 + 1 /* .debug_abbrev */ \
9163 + 1 /* .debug_line */ \
9164 + 1 /* .debug_loc */ \
9165 + 1 /* .debug_str_offsets */ \
9166 + 1 /* .debug_macro */ \
9167 + 1 /* .debug_macinfo */ \
9168 + 1 /* trailing zero */)
9169
9170 memset (&sections, 0, sizeof (sections));
9171 cleanups = make_cleanup (null_cleanup, 0);
9172
9173 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9174 {
9175 asection *sectp;
9176 uint32_t section_nr =
9177 read_4_bytes (dbfd,
9178 htab->section_pool
9179 + (section_index + i) * sizeof (uint32_t));
9180
9181 if (section_nr == 0)
9182 break;
9183 if (section_nr >= dwp_file->num_sections)
9184 {
9185 error (_("Dwarf Error: bad DWP hash table, section number too large"
9186 " [in module %s]"),
9187 dwp_file->name);
9188 }
9189
9190 sectp = dwp_file->elf_sections[section_nr];
9191 if (! locate_virtual_dwo_sections (sectp, &sections))
9192 {
9193 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9194 " [in module %s]"),
9195 dwp_file->name);
9196 }
9197 }
9198
9199 if (i < 2
9200 || sections.info_or_types.asection == NULL
9201 || sections.abbrev.asection == NULL)
9202 {
9203 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9204 " [in module %s]"),
9205 dwp_file->name);
9206 }
9207 if (i == MAX_NR_DWO_SECTIONS)
9208 {
9209 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9210 " [in module %s]"),
9211 dwp_file->name);
9212 }
9213
9214 /* It's easier for the rest of the code if we fake a struct dwo_file and
9215 have dwo_unit "live" in that. At least for now.
9216
9217 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec
DE
9218 However, for each CU + set of TUs that came from the same original DWO
9219 file, we want to combine them back into a virtual DWO file to save space
80626a55
DE
9220 (fewer struct dwo_file objects to allocated). Remember that for really
9221 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9222
2792b94d
PM
9223 virtual_dwo_name =
9224 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9225 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9226 sections.line.asection ? sections.line.asection->id : 0,
9227 sections.loc.asection ? sections.loc.asection->id : 0,
9228 (sections.str_offsets.asection
9229 ? sections.str_offsets.asection->id
9230 : 0));
80626a55
DE
9231 make_cleanup (xfree, virtual_dwo_name);
9232 /* Can we use an existing virtual DWO file? */
0ac5b59e 9233 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
80626a55
DE
9234 /* Create one if necessary. */
9235 if (*dwo_file_slot == NULL)
9236 {
9237 if (dwarf2_read_debug)
9238 {
9239 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9240 virtual_dwo_name);
9241 }
9242 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9243 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9244 virtual_dwo_name,
9245 strlen (virtual_dwo_name));
9246 dwo_file->comp_dir = comp_dir;
80626a55
DE
9247 dwo_file->sections.abbrev = sections.abbrev;
9248 dwo_file->sections.line = sections.line;
9249 dwo_file->sections.loc = sections.loc;
9250 dwo_file->sections.macinfo = sections.macinfo;
9251 dwo_file->sections.macro = sections.macro;
9252 dwo_file->sections.str_offsets = sections.str_offsets;
9253 /* The "str" section is global to the entire DWP file. */
9254 dwo_file->sections.str = dwp_file->sections.str;
9255 /* The info or types section is assigned later to dwo_unit,
9256 there's no need to record it in dwo_file.
9257 Also, we can't simply record type sections in dwo_file because
9258 we record a pointer into the vector in dwo_unit. As we collect more
9259 types we'll grow the vector and eventually have to reallocate space
9260 for it, invalidating all the pointers into the current copy. */
9261 *dwo_file_slot = dwo_file;
9262 }
9263 else
9264 {
9265 if (dwarf2_read_debug)
9266 {
9267 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9268 virtual_dwo_name);
9269 }
9270 dwo_file = *dwo_file_slot;
9271 }
9272 do_cleanups (cleanups);
9273
9274 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9275 dwo_unit->dwo_file = dwo_file;
9276 dwo_unit->signature = signature;
8a0459fd
DE
9277 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9278 sizeof (struct dwarf2_section_info));
9279 *dwo_unit->section = sections.info_or_types;
80626a55
DE
9280 /* offset, length, type_offset_in_tu are set later. */
9281
9282 return dwo_unit;
9283}
9284
9285/* Lookup the DWO with SIGNATURE in DWP_FILE. */
9286
9287static struct dwo_unit *
9288lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9289 const struct dwp_hash_table *htab,
0ac5b59e 9290 const char *comp_dir,
80626a55
DE
9291 ULONGEST signature, int is_debug_types)
9292{
9293 bfd *dbfd = dwp_file->dbfd;
9294 uint32_t mask = htab->nr_slots - 1;
9295 uint32_t hash = signature & mask;
9296 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9297 unsigned int i;
9298 void **slot;
9299 struct dwo_unit find_dwo_cu, *dwo_cu;
9300
9301 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9302 find_dwo_cu.signature = signature;
9303 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9304
9305 if (*slot != NULL)
9306 return *slot;
9307
9308 /* Use a for loop so that we don't loop forever on bad debug info. */
9309 for (i = 0; i < htab->nr_slots; ++i)
9310 {
9311 ULONGEST signature_in_table;
9312
9313 signature_in_table =
9314 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9315 if (signature_in_table == signature)
9316 {
9317 uint32_t section_index =
9318 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9319
9320 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
0ac5b59e 9321 comp_dir, signature, is_debug_types);
80626a55
DE
9322 return *slot;
9323 }
9324 if (signature_in_table == 0)
9325 return NULL;
9326 hash = (hash + hash2) & mask;
9327 }
9328
9329 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9330 " [in module %s]"),
9331 dwp_file->name);
9332}
9333
ab5088bf 9334/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
9335 Open the file specified by FILE_NAME and hand it off to BFD for
9336 preliminary analysis. Return a newly initialized bfd *, which
9337 includes a canonicalized copy of FILE_NAME.
80626a55 9338 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
9339 SEARCH_CWD is true if the current directory is to be searched.
9340 It will be searched before debug-file-directory.
9341 If unable to find/open the file, return NULL.
3019eac3
DE
9342 NOTE: This function is derived from symfile_bfd_open. */
9343
9344static bfd *
6ac97d4c 9345try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
3019eac3
DE
9346{
9347 bfd *sym_bfd;
80626a55 9348 int desc, flags;
3019eac3 9349 char *absolute_name;
9c02c129
DE
9350 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9351 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9352 to debug_file_directory. */
9353 char *search_path;
9354 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9355
6ac97d4c
DE
9356 if (search_cwd)
9357 {
9358 if (*debug_file_directory != '\0')
9359 search_path = concat (".", dirname_separator_string,
9360 debug_file_directory, NULL);
9361 else
9362 search_path = xstrdup (".");
9363 }
9c02c129 9364 else
6ac97d4c 9365 search_path = xstrdup (debug_file_directory);
3019eac3 9366
492c0ab7 9367 flags = OPF_RETURN_REALPATH;
80626a55
DE
9368 if (is_dwp)
9369 flags |= OPF_SEARCH_IN_PATH;
9c02c129 9370 desc = openp (search_path, flags, file_name,
3019eac3 9371 O_RDONLY | O_BINARY, &absolute_name);
9c02c129 9372 xfree (search_path);
3019eac3
DE
9373 if (desc < 0)
9374 return NULL;
9375
bb397797 9376 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
a4453b7e 9377 xfree (absolute_name);
9c02c129
DE
9378 if (sym_bfd == NULL)
9379 return NULL;
3019eac3
DE
9380 bfd_set_cacheable (sym_bfd, 1);
9381
9382 if (!bfd_check_format (sym_bfd, bfd_object))
9383 {
cbb099e8 9384 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
3019eac3
DE
9385 return NULL;
9386 }
9387
3019eac3
DE
9388 return sym_bfd;
9389}
9390
ab5088bf 9391/* Try to open DWO file FILE_NAME.
3019eac3
DE
9392 COMP_DIR is the DW_AT_comp_dir attribute.
9393 The result is the bfd handle of the file.
9394 If there is a problem finding or opening the file, return NULL.
9395 Upon success, the canonicalized path of the file is stored in the bfd,
9396 same as symfile_bfd_open. */
9397
9398static bfd *
ab5088bf 9399open_dwo_file (const char *file_name, const char *comp_dir)
3019eac3
DE
9400{
9401 bfd *abfd;
3019eac3 9402
80626a55 9403 if (IS_ABSOLUTE_PATH (file_name))
6ac97d4c 9404 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
9405
9406 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9407
9408 if (comp_dir != NULL)
9409 {
80626a55 9410 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
3019eac3
DE
9411
9412 /* NOTE: If comp_dir is a relative path, this will also try the
9413 search path, which seems useful. */
6ac97d4c 9414 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9415 xfree (path_to_try);
9416 if (abfd != NULL)
9417 return abfd;
9418 }
9419
9420 /* That didn't work, try debug-file-directory, which, despite its name,
9421 is a list of paths. */
9422
9423 if (*debug_file_directory == '\0')
9424 return NULL;
9425
6ac97d4c 9426 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
9427}
9428
80626a55
DE
9429/* This function is mapped across the sections and remembers the offset and
9430 size of each of the DWO debugging sections we are interested in. */
9431
9432static void
9433dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9434{
9435 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9436 const struct dwop_section_names *names = &dwop_section_names;
9437
9438 if (section_is_p (sectp->name, &names->abbrev_dwo))
9439 {
9440 dwo_sections->abbrev.asection = sectp;
9441 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9442 }
9443 else if (section_is_p (sectp->name, &names->info_dwo))
9444 {
9445 dwo_sections->info.asection = sectp;
9446 dwo_sections->info.size = bfd_get_section_size (sectp);
9447 }
9448 else if (section_is_p (sectp->name, &names->line_dwo))
9449 {
9450 dwo_sections->line.asection = sectp;
9451 dwo_sections->line.size = bfd_get_section_size (sectp);
9452 }
9453 else if (section_is_p (sectp->name, &names->loc_dwo))
9454 {
9455 dwo_sections->loc.asection = sectp;
9456 dwo_sections->loc.size = bfd_get_section_size (sectp);
9457 }
9458 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9459 {
9460 dwo_sections->macinfo.asection = sectp;
9461 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9462 }
9463 else if (section_is_p (sectp->name, &names->macro_dwo))
9464 {
9465 dwo_sections->macro.asection = sectp;
9466 dwo_sections->macro.size = bfd_get_section_size (sectp);
9467 }
9468 else if (section_is_p (sectp->name, &names->str_dwo))
9469 {
9470 dwo_sections->str.asection = sectp;
9471 dwo_sections->str.size = bfd_get_section_size (sectp);
9472 }
9473 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9474 {
9475 dwo_sections->str_offsets.asection = sectp;
9476 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9477 }
9478 else if (section_is_p (sectp->name, &names->types_dwo))
9479 {
9480 struct dwarf2_section_info type_section;
9481
9482 memset (&type_section, 0, sizeof (type_section));
9483 type_section.asection = sectp;
9484 type_section.size = bfd_get_section_size (sectp);
9485 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9486 &type_section);
9487 }
9488}
9489
ab5088bf 9490/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 9491 by PER_CU. This is for the non-DWP case.
80626a55 9492 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
9493
9494static struct dwo_file *
0ac5b59e
DE
9495open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9496 const char *dwo_name, const char *comp_dir)
3019eac3
DE
9497{
9498 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9499 struct dwo_file *dwo_file;
9500 bfd *dbfd;
3019eac3
DE
9501 struct cleanup *cleanups;
9502
ab5088bf 9503 dbfd = open_dwo_file (dwo_name, comp_dir);
80626a55
DE
9504 if (dbfd == NULL)
9505 {
9506 if (dwarf2_read_debug)
9507 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9508 return NULL;
9509 }
9510 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
0ac5b59e
DE
9511 dwo_file->dwo_name = dwo_name;
9512 dwo_file->comp_dir = comp_dir;
80626a55 9513 dwo_file->dbfd = dbfd;
3019eac3
DE
9514
9515 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9516
80626a55 9517 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
3019eac3 9518
19c3d4c9 9519 dwo_file->cu = create_dwo_cu (dwo_file);
3019eac3
DE
9520
9521 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9522 dwo_file->sections.types);
9523
9524 discard_cleanups (cleanups);
9525
80626a55
DE
9526 if (dwarf2_read_debug)
9527 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9528
3019eac3
DE
9529 return dwo_file;
9530}
9531
80626a55
DE
9532/* This function is mapped across the sections and remembers the offset and
9533 size of each of the DWP debugging sections we are interested in. */
3019eac3 9534
80626a55
DE
9535static void
9536dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
3019eac3 9537{
80626a55
DE
9538 struct dwp_file *dwp_file = dwp_file_ptr;
9539 const struct dwop_section_names *names = &dwop_section_names;
9540 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 9541
80626a55
DE
9542 /* Record the ELF section number for later lookup: this is what the
9543 .debug_cu_index,.debug_tu_index tables use. */
9544 gdb_assert (elf_section_nr < dwp_file->num_sections);
9545 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 9546
80626a55
DE
9547 /* Look for specific sections that we need. */
9548 if (section_is_p (sectp->name, &names->str_dwo))
9549 {
9550 dwp_file->sections.str.asection = sectp;
9551 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9552 }
9553 else if (section_is_p (sectp->name, &names->cu_index))
9554 {
9555 dwp_file->sections.cu_index.asection = sectp;
9556 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9557 }
9558 else if (section_is_p (sectp->name, &names->tu_index))
9559 {
9560 dwp_file->sections.tu_index.asection = sectp;
9561 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9562 }
9563}
3019eac3 9564
80626a55 9565/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 9566
80626a55
DE
9567static hashval_t
9568hash_dwp_loaded_cutus (const void *item)
9569{
9570 const struct dwo_unit *dwo_unit = item;
3019eac3 9571
80626a55
DE
9572 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9573 return dwo_unit->signature;
3019eac3
DE
9574}
9575
80626a55 9576/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 9577
80626a55
DE
9578static int
9579eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 9580{
80626a55
DE
9581 const struct dwo_unit *dua = a;
9582 const struct dwo_unit *dub = b;
3019eac3 9583
80626a55
DE
9584 return dua->signature == dub->signature;
9585}
3019eac3 9586
80626a55 9587/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 9588
80626a55
DE
9589static htab_t
9590allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9591{
9592 return htab_create_alloc_ex (3,
9593 hash_dwp_loaded_cutus,
9594 eq_dwp_loaded_cutus,
9595 NULL,
9596 &objfile->objfile_obstack,
9597 hashtab_obstack_allocate,
9598 dummy_obstack_deallocate);
9599}
3019eac3 9600
ab5088bf
DE
9601/* Try to open DWP file FILE_NAME.
9602 The result is the bfd handle of the file.
9603 If there is a problem finding or opening the file, return NULL.
9604 Upon success, the canonicalized path of the file is stored in the bfd,
9605 same as symfile_bfd_open. */
9606
9607static bfd *
9608open_dwp_file (const char *file_name)
9609{
6ac97d4c
DE
9610 bfd *abfd;
9611
9612 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9613 if (abfd != NULL)
9614 return abfd;
9615
9616 /* Work around upstream bug 15652.
9617 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9618 [Whether that's a "bug" is debatable, but it is getting in our way.]
9619 We have no real idea where the dwp file is, because gdb's realpath-ing
9620 of the executable's path may have discarded the needed info.
9621 [IWBN if the dwp file name was recorded in the executable, akin to
9622 .gnu_debuglink, but that doesn't exist yet.]
9623 Strip the directory from FILE_NAME and search again. */
9624 if (*debug_file_directory != '\0')
9625 {
9626 /* Don't implicitly search the current directory here.
9627 If the user wants to search "." to handle this case,
9628 it must be added to debug-file-directory. */
9629 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9630 0 /*search_cwd*/);
9631 }
9632
9633 return NULL;
ab5088bf
DE
9634}
9635
80626a55
DE
9636/* Initialize the use of the DWP file for the current objfile.
9637 By convention the name of the DWP file is ${objfile}.dwp.
9638 The result is NULL if it can't be found. */
a766d390 9639
80626a55 9640static struct dwp_file *
ab5088bf 9641open_and_init_dwp_file (void)
80626a55
DE
9642{
9643 struct objfile *objfile = dwarf2_per_objfile->objfile;
9644 struct dwp_file *dwp_file;
9645 char *dwp_name;
9646 bfd *dbfd;
9647 struct cleanup *cleanups;
9648
2792b94d 9649 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
80626a55
DE
9650 cleanups = make_cleanup (xfree, dwp_name);
9651
ab5088bf 9652 dbfd = open_dwp_file (dwp_name);
80626a55
DE
9653 if (dbfd == NULL)
9654 {
9655 if (dwarf2_read_debug)
9656 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9657 do_cleanups (cleanups);
9658 return NULL;
3019eac3 9659 }
80626a55 9660 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
93417882 9661 dwp_file->name = bfd_get_filename (dbfd);
80626a55
DE
9662 dwp_file->dbfd = dbfd;
9663 do_cleanups (cleanups);
c906108c 9664
80626a55
DE
9665 /* +1: section 0 is unused */
9666 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9667 dwp_file->elf_sections =
9668 OBSTACK_CALLOC (&objfile->objfile_obstack,
9669 dwp_file->num_sections, asection *);
9670
9671 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9672
9673 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9674
9675 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9676
9677 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9678
80626a55
DE
9679 if (dwarf2_read_debug)
9680 {
9681 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9682 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
9683 " %s CUs, %s TUs\n",
9684 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9685 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
9686 }
9687
9688 return dwp_file;
3019eac3 9689}
c906108c 9690
ab5088bf
DE
9691/* Wrapper around open_and_init_dwp_file, only open it once. */
9692
9693static struct dwp_file *
9694get_dwp_file (void)
9695{
9696 if (! dwarf2_per_objfile->dwp_checked)
9697 {
9698 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9699 dwarf2_per_objfile->dwp_checked = 1;
9700 }
9701 return dwarf2_per_objfile->dwp_file;
9702}
9703
80626a55
DE
9704/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9705 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9706 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 9707 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
9708 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9709
9710 This is called, for example, when wanting to read a variable with a
9711 complex location. Therefore we don't want to do file i/o for every call.
9712 Therefore we don't want to look for a DWO file on every call.
9713 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9714 then we check if we've already seen DWO_NAME, and only THEN do we check
9715 for a DWO file.
9716
1c658ad5 9717 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 9718 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 9719
3019eac3 9720static struct dwo_unit *
80626a55
DE
9721lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9722 const char *dwo_name, const char *comp_dir,
9723 ULONGEST signature, int is_debug_types)
3019eac3
DE
9724{
9725 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
9726 const char *kind = is_debug_types ? "TU" : "CU";
9727 void **dwo_file_slot;
3019eac3 9728 struct dwo_file *dwo_file;
80626a55 9729 struct dwp_file *dwp_file;
cb1df416 9730
6a506a2d
DE
9731 /* First see if there's a DWP file.
9732 If we have a DWP file but didn't find the DWO inside it, don't
9733 look for the original DWO file. It makes gdb behave differently
9734 depending on whether one is debugging in the build tree. */
cf2c3c16 9735
ab5088bf 9736 dwp_file = get_dwp_file ();
80626a55 9737 if (dwp_file != NULL)
cf2c3c16 9738 {
80626a55
DE
9739 const struct dwp_hash_table *dwp_htab =
9740 is_debug_types ? dwp_file->tus : dwp_file->cus;
9741
9742 if (dwp_htab != NULL)
9743 {
9744 struct dwo_unit *dwo_cutu =
0ac5b59e
DE
9745 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9746 signature, is_debug_types);
80626a55
DE
9747
9748 if (dwo_cutu != NULL)
9749 {
9750 if (dwarf2_read_debug)
9751 {
9752 fprintf_unfiltered (gdb_stdlog,
9753 "Virtual DWO %s %s found: @%s\n",
9754 kind, hex_string (signature),
9755 host_address_to_string (dwo_cutu));
9756 }
9757 return dwo_cutu;
9758 }
9759 }
9760 }
6a506a2d 9761 else
80626a55 9762 {
6a506a2d 9763 /* No DWP file, look for the DWO file. */
80626a55 9764
6a506a2d
DE
9765 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9766 if (*dwo_file_slot == NULL)
80626a55 9767 {
6a506a2d
DE
9768 /* Read in the file and build a table of the CUs/TUs it contains. */
9769 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 9770 }
6a506a2d
DE
9771 /* NOTE: This will be NULL if unable to open the file. */
9772 dwo_file = *dwo_file_slot;
3019eac3 9773
6a506a2d 9774 if (dwo_file != NULL)
19c3d4c9 9775 {
6a506a2d
DE
9776 struct dwo_unit *dwo_cutu = NULL;
9777
9778 if (is_debug_types && dwo_file->tus)
9779 {
9780 struct dwo_unit find_dwo_cutu;
9781
9782 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9783 find_dwo_cutu.signature = signature;
9784 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9785 }
9786 else if (!is_debug_types && dwo_file->cu)
80626a55 9787 {
6a506a2d
DE
9788 if (signature == dwo_file->cu->signature)
9789 dwo_cutu = dwo_file->cu;
9790 }
9791
9792 if (dwo_cutu != NULL)
9793 {
9794 if (dwarf2_read_debug)
9795 {
9796 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9797 kind, dwo_name, hex_string (signature),
9798 host_address_to_string (dwo_cutu));
9799 }
9800 return dwo_cutu;
80626a55
DE
9801 }
9802 }
2e276125 9803 }
9cdd5dbd 9804
80626a55
DE
9805 /* We didn't find it. This could mean a dwo_id mismatch, or
9806 someone deleted the DWO/DWP file, or the search path isn't set up
9807 correctly to find the file. */
9808
9809 if (dwarf2_read_debug)
9810 {
9811 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9812 kind, dwo_name, hex_string (signature));
9813 }
3019eac3 9814
6656a72d
DE
9815 /* This is a warning and not a complaint because it can be caused by
9816 pilot error (e.g., user accidentally deleting the DWO). */
9817 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9818 " [in module %s]"),
9819 kind, dwo_name, hex_string (signature),
9820 this_unit->is_debug_types ? "TU" : "CU",
9821 this_unit->offset.sect_off, objfile->name);
3019eac3 9822 return NULL;
5fb290d7
DJ
9823}
9824
80626a55
DE
9825/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9826 See lookup_dwo_cutu_unit for details. */
9827
9828static struct dwo_unit *
9829lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9830 const char *dwo_name, const char *comp_dir,
9831 ULONGEST signature)
9832{
9833 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9834}
9835
9836/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9837 See lookup_dwo_cutu_unit for details. */
9838
9839static struct dwo_unit *
9840lookup_dwo_type_unit (struct signatured_type *this_tu,
9841 const char *dwo_name, const char *comp_dir)
9842{
9843 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9844}
9845
89e63ee4
DE
9846/* Traversal function for queue_and_load_all_dwo_tus. */
9847
9848static int
9849queue_and_load_dwo_tu (void **slot, void *info)
9850{
9851 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9852 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
9853 ULONGEST signature = dwo_unit->signature;
9854 struct signatured_type *sig_type =
9855 lookup_dwo_signatured_type (per_cu->cu, signature);
9856
9857 if (sig_type != NULL)
9858 {
9859 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
9860
9861 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9862 a real dependency of PER_CU on SIG_TYPE. That is detected later
9863 while processing PER_CU. */
9864 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
9865 load_full_type_unit (sig_cu);
9866 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
9867 }
9868
9869 return 1;
9870}
9871
9872/* Queue all TUs contained in the DWO of PER_CU to be read in.
9873 The DWO may have the only definition of the type, though it may not be
9874 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9875 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9876
9877static void
9878queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
9879{
9880 struct dwo_unit *dwo_unit;
9881 struct dwo_file *dwo_file;
9882
9883 gdb_assert (!per_cu->is_debug_types);
9884 gdb_assert (get_dwp_file () == NULL);
9885 gdb_assert (per_cu->cu != NULL);
9886
9887 dwo_unit = per_cu->cu->dwo_unit;
9888 gdb_assert (dwo_unit != NULL);
9889
9890 dwo_file = dwo_unit->dwo_file;
9891 if (dwo_file->tus != NULL)
9892 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
9893}
9894
3019eac3
DE
9895/* Free all resources associated with DWO_FILE.
9896 Close the DWO file and munmap the sections.
9897 All memory should be on the objfile obstack. */
348e048f
DE
9898
9899static void
3019eac3 9900free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 9901{
3019eac3
DE
9902 int ix;
9903 struct dwarf2_section_info *section;
348e048f 9904
5c6fa7ab 9905 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 9906 gdb_bfd_unref (dwo_file->dbfd);
348e048f 9907
3019eac3
DE
9908 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9909}
348e048f 9910
3019eac3 9911/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 9912
3019eac3
DE
9913static void
9914free_dwo_file_cleanup (void *arg)
9915{
9916 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9917 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 9918
3019eac3
DE
9919 free_dwo_file (dwo_file, objfile);
9920}
348e048f 9921
3019eac3 9922/* Traversal function for free_dwo_files. */
2ab95328 9923
3019eac3
DE
9924static int
9925free_dwo_file_from_slot (void **slot, void *info)
9926{
9927 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9928 struct objfile *objfile = (struct objfile *) info;
348e048f 9929
3019eac3 9930 free_dwo_file (dwo_file, objfile);
348e048f 9931
3019eac3
DE
9932 return 1;
9933}
348e048f 9934
3019eac3 9935/* Free all resources associated with DWO_FILES. */
348e048f 9936
3019eac3
DE
9937static void
9938free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9939{
9940 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 9941}
3019eac3
DE
9942\f
9943/* Read in various DIEs. */
348e048f 9944
d389af10
JK
9945/* qsort helper for inherit_abstract_dies. */
9946
9947static int
9948unsigned_int_compar (const void *ap, const void *bp)
9949{
9950 unsigned int a = *(unsigned int *) ap;
9951 unsigned int b = *(unsigned int *) bp;
9952
9953 return (a > b) - (b > a);
9954}
9955
9956/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
9957 Inherit only the children of the DW_AT_abstract_origin DIE not being
9958 already referenced by DW_AT_abstract_origin from the children of the
9959 current DIE. */
d389af10
JK
9960
9961static void
9962inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9963{
9964 struct die_info *child_die;
9965 unsigned die_children_count;
9966 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
9967 sect_offset *offsets;
9968 sect_offset *offsets_end, *offsetp;
d389af10
JK
9969 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9970 struct die_info *origin_die;
9971 /* Iterator of the ORIGIN_DIE children. */
9972 struct die_info *origin_child_die;
9973 struct cleanup *cleanups;
9974 struct attribute *attr;
cd02d79d
PA
9975 struct dwarf2_cu *origin_cu;
9976 struct pending **origin_previous_list_in_scope;
d389af10
JK
9977
9978 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9979 if (!attr)
9980 return;
9981
cd02d79d
PA
9982 /* Note that following die references may follow to a die in a
9983 different cu. */
9984
9985 origin_cu = cu;
9986 origin_die = follow_die_ref (die, attr, &origin_cu);
9987
9988 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9989 symbols in. */
9990 origin_previous_list_in_scope = origin_cu->list_in_scope;
9991 origin_cu->list_in_scope = cu->list_in_scope;
9992
edb3359d
DJ
9993 if (die->tag != origin_die->tag
9994 && !(die->tag == DW_TAG_inlined_subroutine
9995 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
9996 complaint (&symfile_complaints,
9997 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 9998 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
9999
10000 child_die = die->child;
10001 die_children_count = 0;
10002 while (child_die && child_die->tag)
10003 {
10004 child_die = sibling_die (child_die);
10005 die_children_count++;
10006 }
10007 offsets = xmalloc (sizeof (*offsets) * die_children_count);
10008 cleanups = make_cleanup (xfree, offsets);
10009
10010 offsets_end = offsets;
10011 child_die = die->child;
10012 while (child_die && child_die->tag)
10013 {
c38f313d
DJ
10014 /* For each CHILD_DIE, find the corresponding child of
10015 ORIGIN_DIE. If there is more than one layer of
10016 DW_AT_abstract_origin, follow them all; there shouldn't be,
10017 but GCC versions at least through 4.4 generate this (GCC PR
10018 40573). */
10019 struct die_info *child_origin_die = child_die;
cd02d79d 10020 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 10021
c38f313d
DJ
10022 while (1)
10023 {
cd02d79d
PA
10024 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10025 child_origin_cu);
c38f313d
DJ
10026 if (attr == NULL)
10027 break;
cd02d79d
PA
10028 child_origin_die = follow_die_ref (child_origin_die, attr,
10029 &child_origin_cu);
c38f313d
DJ
10030 }
10031
d389af10
JK
10032 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10033 counterpart may exist. */
c38f313d 10034 if (child_origin_die != child_die)
d389af10 10035 {
edb3359d
DJ
10036 if (child_die->tag != child_origin_die->tag
10037 && !(child_die->tag == DW_TAG_inlined_subroutine
10038 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
10039 complaint (&symfile_complaints,
10040 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10041 "different tags"), child_die->offset.sect_off,
10042 child_origin_die->offset.sect_off);
c38f313d
DJ
10043 if (child_origin_die->parent != origin_die)
10044 complaint (&symfile_complaints,
10045 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
10046 "different parents"), child_die->offset.sect_off,
10047 child_origin_die->offset.sect_off);
c38f313d
DJ
10048 else
10049 *offsets_end++ = child_origin_die->offset;
d389af10
JK
10050 }
10051 child_die = sibling_die (child_die);
10052 }
10053 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10054 unsigned_int_compar);
10055 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 10056 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
10057 complaint (&symfile_complaints,
10058 _("Multiple children of DIE 0x%x refer "
10059 "to DIE 0x%x as their abstract origin"),
b64f50a1 10060 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
10061
10062 offsetp = offsets;
10063 origin_child_die = origin_die->child;
10064 while (origin_child_die && origin_child_die->tag)
10065 {
10066 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
10067 while (offsetp < offsets_end
10068 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 10069 offsetp++;
b64f50a1
JK
10070 if (offsetp >= offsets_end
10071 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
10072 {
10073 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 10074 process_die (origin_child_die, origin_cu);
d389af10
JK
10075 }
10076 origin_child_die = sibling_die (origin_child_die);
10077 }
cd02d79d 10078 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
10079
10080 do_cleanups (cleanups);
10081}
10082
c906108c 10083static void
e7c27a73 10084read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10085{
e7c27a73 10086 struct objfile *objfile = cu->objfile;
52f0bd74 10087 struct context_stack *new;
c906108c
SS
10088 CORE_ADDR lowpc;
10089 CORE_ADDR highpc;
10090 struct die_info *child_die;
edb3359d 10091 struct attribute *attr, *call_line, *call_file;
15d034d0 10092 const char *name;
e142c38c 10093 CORE_ADDR baseaddr;
801e3a5b 10094 struct block *block;
edb3359d 10095 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
10096 VEC (symbolp) *template_args = NULL;
10097 struct template_symbol *templ_func = NULL;
edb3359d
DJ
10098
10099 if (inlined_func)
10100 {
10101 /* If we do not have call site information, we can't show the
10102 caller of this inlined function. That's too confusing, so
10103 only use the scope for local variables. */
10104 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10105 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10106 if (call_line == NULL || call_file == NULL)
10107 {
10108 read_lexical_block_scope (die, cu);
10109 return;
10110 }
10111 }
c906108c 10112
e142c38c
DJ
10113 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10114
94af9270 10115 name = dwarf2_name (die, cu);
c906108c 10116
e8d05480
JB
10117 /* Ignore functions with missing or empty names. These are actually
10118 illegal according to the DWARF standard. */
10119 if (name == NULL)
10120 {
10121 complaint (&symfile_complaints,
b64f50a1
JK
10122 _("missing name for subprogram DIE at %d"),
10123 die->offset.sect_off);
e8d05480
JB
10124 return;
10125 }
10126
10127 /* Ignore functions with missing or invalid low and high pc attributes. */
10128 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10129 {
ae4d0c03
PM
10130 attr = dwarf2_attr (die, DW_AT_external, cu);
10131 if (!attr || !DW_UNSND (attr))
10132 complaint (&symfile_complaints,
3e43a32a
MS
10133 _("cannot get low and high bounds "
10134 "for subprogram DIE at %d"),
b64f50a1 10135 die->offset.sect_off);
e8d05480
JB
10136 return;
10137 }
c906108c
SS
10138
10139 lowpc += baseaddr;
10140 highpc += baseaddr;
10141
34eaf542
TT
10142 /* If we have any template arguments, then we must allocate a
10143 different sort of symbol. */
10144 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10145 {
10146 if (child_die->tag == DW_TAG_template_type_param
10147 || child_die->tag == DW_TAG_template_value_param)
10148 {
e623cf5d 10149 templ_func = allocate_template_symbol (objfile);
34eaf542
TT
10150 templ_func->base.is_cplus_template_function = 1;
10151 break;
10152 }
10153 }
10154
c906108c 10155 new = push_context (0, lowpc);
34eaf542
TT
10156 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10157 (struct symbol *) templ_func);
4c2df51b 10158
4cecd739
DJ
10159 /* If there is a location expression for DW_AT_frame_base, record
10160 it. */
e142c38c 10161 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 10162 if (attr)
f1e6e072 10163 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
4c2df51b 10164
e142c38c 10165 cu->list_in_scope = &local_symbols;
c906108c 10166
639d11d3 10167 if (die->child != NULL)
c906108c 10168 {
639d11d3 10169 child_die = die->child;
c906108c
SS
10170 while (child_die && child_die->tag)
10171 {
34eaf542
TT
10172 if (child_die->tag == DW_TAG_template_type_param
10173 || child_die->tag == DW_TAG_template_value_param)
10174 {
10175 struct symbol *arg = new_symbol (child_die, NULL, cu);
10176
f1078f66
DJ
10177 if (arg != NULL)
10178 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
10179 }
10180 else
10181 process_die (child_die, cu);
c906108c
SS
10182 child_die = sibling_die (child_die);
10183 }
10184 }
10185
d389af10
JK
10186 inherit_abstract_dies (die, cu);
10187
4a811a97
UW
10188 /* If we have a DW_AT_specification, we might need to import using
10189 directives from the context of the specification DIE. See the
10190 comment in determine_prefix. */
10191 if (cu->language == language_cplus
10192 && dwarf2_attr (die, DW_AT_specification, cu))
10193 {
10194 struct dwarf2_cu *spec_cu = cu;
10195 struct die_info *spec_die = die_specification (die, &spec_cu);
10196
10197 while (spec_die)
10198 {
10199 child_die = spec_die->child;
10200 while (child_die && child_die->tag)
10201 {
10202 if (child_die->tag == DW_TAG_imported_module)
10203 process_die (child_die, spec_cu);
10204 child_die = sibling_die (child_die);
10205 }
10206
10207 /* In some cases, GCC generates specification DIEs that
10208 themselves contain DW_AT_specification attributes. */
10209 spec_die = die_specification (spec_die, &spec_cu);
10210 }
10211 }
10212
c906108c
SS
10213 new = pop_context ();
10214 /* Make a block for the local symbols within. */
801e3a5b
JB
10215 block = finish_block (new->name, &local_symbols, new->old_blocks,
10216 lowpc, highpc, objfile);
10217
df8a16a1 10218 /* For C++, set the block's scope. */
195a3f6c 10219 if ((cu->language == language_cplus || cu->language == language_fortran)
4d4ec4e5 10220 && cu->processing_has_namespace_info)
195a3f6c
TT
10221 block_set_scope (block, determine_prefix (die, cu),
10222 &objfile->objfile_obstack);
df8a16a1 10223
801e3a5b
JB
10224 /* If we have address ranges, record them. */
10225 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 10226
34eaf542
TT
10227 /* Attach template arguments to function. */
10228 if (! VEC_empty (symbolp, template_args))
10229 {
10230 gdb_assert (templ_func != NULL);
10231
10232 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10233 templ_func->template_arguments
10234 = obstack_alloc (&objfile->objfile_obstack,
10235 (templ_func->n_template_arguments
10236 * sizeof (struct symbol *)));
10237 memcpy (templ_func->template_arguments,
10238 VEC_address (symbolp, template_args),
10239 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10240 VEC_free (symbolp, template_args);
10241 }
10242
208d8187
JB
10243 /* In C++, we can have functions nested inside functions (e.g., when
10244 a function declares a class that has methods). This means that
10245 when we finish processing a function scope, we may need to go
10246 back to building a containing block's symbol lists. */
10247 local_symbols = new->locals;
27aa8d6a 10248 using_directives = new->using_directives;
208d8187 10249
921e78cf
JB
10250 /* If we've finished processing a top-level function, subsequent
10251 symbols go in the file symbol list. */
10252 if (outermost_context_p ())
e142c38c 10253 cu->list_in_scope = &file_symbols;
c906108c
SS
10254}
10255
10256/* Process all the DIES contained within a lexical block scope. Start
10257 a new scope, process the dies, and then close the scope. */
10258
10259static void
e7c27a73 10260read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10261{
e7c27a73 10262 struct objfile *objfile = cu->objfile;
52f0bd74 10263 struct context_stack *new;
c906108c
SS
10264 CORE_ADDR lowpc, highpc;
10265 struct die_info *child_die;
e142c38c
DJ
10266 CORE_ADDR baseaddr;
10267
10268 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
10269
10270 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
10271 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10272 as multiple lexical blocks? Handling children in a sane way would
6e70227d 10273 be nasty. Might be easier to properly extend generic blocks to
af34e669 10274 describe ranges. */
d85a05f0 10275 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
10276 return;
10277 lowpc += baseaddr;
10278 highpc += baseaddr;
10279
10280 push_context (0, lowpc);
639d11d3 10281 if (die->child != NULL)
c906108c 10282 {
639d11d3 10283 child_die = die->child;
c906108c
SS
10284 while (child_die && child_die->tag)
10285 {
e7c27a73 10286 process_die (child_die, cu);
c906108c
SS
10287 child_die = sibling_die (child_die);
10288 }
10289 }
10290 new = pop_context ();
10291
8540c487 10292 if (local_symbols != NULL || using_directives != NULL)
c906108c 10293 {
801e3a5b
JB
10294 struct block *block
10295 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10296 highpc, objfile);
10297
10298 /* Note that recording ranges after traversing children, as we
10299 do here, means that recording a parent's ranges entails
10300 walking across all its children's ranges as they appear in
10301 the address map, which is quadratic behavior.
10302
10303 It would be nicer to record the parent's ranges before
10304 traversing its children, simply overriding whatever you find
10305 there. But since we don't even decide whether to create a
10306 block until after we've traversed its children, that's hard
10307 to do. */
10308 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
10309 }
10310 local_symbols = new->locals;
27aa8d6a 10311 using_directives = new->using_directives;
c906108c
SS
10312}
10313
96408a79
SA
10314/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10315
10316static void
10317read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10318{
10319 struct objfile *objfile = cu->objfile;
10320 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10321 CORE_ADDR pc, baseaddr;
10322 struct attribute *attr;
10323 struct call_site *call_site, call_site_local;
10324 void **slot;
10325 int nparams;
10326 struct die_info *child_die;
10327
10328 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10329
10330 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10331 if (!attr)
10332 {
10333 complaint (&symfile_complaints,
10334 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10335 "DIE 0x%x [in module %s]"),
b64f50a1 10336 die->offset.sect_off, objfile->name);
96408a79
SA
10337 return;
10338 }
10339 pc = DW_ADDR (attr) + baseaddr;
10340
10341 if (cu->call_site_htab == NULL)
10342 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10343 NULL, &objfile->objfile_obstack,
10344 hashtab_obstack_allocate, NULL);
10345 call_site_local.pc = pc;
10346 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10347 if (*slot != NULL)
10348 {
10349 complaint (&symfile_complaints,
10350 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10351 "DIE 0x%x [in module %s]"),
b64f50a1 10352 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
10353 return;
10354 }
10355
10356 /* Count parameters at the caller. */
10357
10358 nparams = 0;
10359 for (child_die = die->child; child_die && child_die->tag;
10360 child_die = sibling_die (child_die))
10361 {
10362 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10363 {
10364 complaint (&symfile_complaints,
10365 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10366 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10367 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
10368 continue;
10369 }
10370
10371 nparams++;
10372 }
10373
10374 call_site = obstack_alloc (&objfile->objfile_obstack,
10375 (sizeof (*call_site)
10376 + (sizeof (*call_site->parameter)
10377 * (nparams - 1))));
10378 *slot = call_site;
10379 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10380 call_site->pc = pc;
10381
10382 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10383 {
10384 struct die_info *func_die;
10385
10386 /* Skip also over DW_TAG_inlined_subroutine. */
10387 for (func_die = die->parent;
10388 func_die && func_die->tag != DW_TAG_subprogram
10389 && func_die->tag != DW_TAG_subroutine_type;
10390 func_die = func_die->parent);
10391
10392 /* DW_AT_GNU_all_call_sites is a superset
10393 of DW_AT_GNU_all_tail_call_sites. */
10394 if (func_die
10395 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10396 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10397 {
10398 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10399 not complete. But keep CALL_SITE for look ups via call_site_htab,
10400 both the initial caller containing the real return address PC and
10401 the final callee containing the current PC of a chain of tail
10402 calls do not need to have the tail call list complete. But any
10403 function candidate for a virtual tail call frame searched via
10404 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10405 determined unambiguously. */
10406 }
10407 else
10408 {
10409 struct type *func_type = NULL;
10410
10411 if (func_die)
10412 func_type = get_die_type (func_die, cu);
10413 if (func_type != NULL)
10414 {
10415 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10416
10417 /* Enlist this call site to the function. */
10418 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10419 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10420 }
10421 else
10422 complaint (&symfile_complaints,
10423 _("Cannot find function owning DW_TAG_GNU_call_site "
10424 "DIE 0x%x [in module %s]"),
b64f50a1 10425 die->offset.sect_off, objfile->name);
96408a79
SA
10426 }
10427 }
10428
10429 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10430 if (attr == NULL)
10431 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10432 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10433 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10434 /* Keep NULL DWARF_BLOCK. */;
10435 else if (attr_form_is_block (attr))
10436 {
10437 struct dwarf2_locexpr_baton *dlbaton;
10438
10439 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10440 dlbaton->data = DW_BLOCK (attr)->data;
10441 dlbaton->size = DW_BLOCK (attr)->size;
10442 dlbaton->per_cu = cu->per_cu;
10443
10444 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10445 }
7771576e 10446 else if (attr_form_is_ref (attr))
96408a79 10447 {
96408a79
SA
10448 struct dwarf2_cu *target_cu = cu;
10449 struct die_info *target_die;
10450
ac9ec31b 10451 target_die = follow_die_ref (die, attr, &target_cu);
96408a79
SA
10452 gdb_assert (target_cu->objfile == objfile);
10453 if (die_is_declaration (target_die, target_cu))
10454 {
9112db09
JK
10455 const char *target_physname = NULL;
10456 struct attribute *target_attr;
10457
10458 /* Prefer the mangled name; otherwise compute the demangled one. */
10459 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10460 if (target_attr == NULL)
10461 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10462 target_cu);
10463 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10464 target_physname = DW_STRING (target_attr);
10465 else
10466 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79
SA
10467 if (target_physname == NULL)
10468 complaint (&symfile_complaints,
10469 _("DW_AT_GNU_call_site_target target DIE has invalid "
10470 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10471 die->offset.sect_off, objfile->name);
96408a79 10472 else
7d455152 10473 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
10474 }
10475 else
10476 {
10477 CORE_ADDR lowpc;
10478
10479 /* DW_AT_entry_pc should be preferred. */
10480 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10481 complaint (&symfile_complaints,
10482 _("DW_AT_GNU_call_site_target target DIE has invalid "
10483 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 10484 die->offset.sect_off, objfile->name);
96408a79
SA
10485 else
10486 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10487 }
10488 }
10489 else
10490 complaint (&symfile_complaints,
10491 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10492 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 10493 die->offset.sect_off, objfile->name);
96408a79
SA
10494
10495 call_site->per_cu = cu->per_cu;
10496
10497 for (child_die = die->child;
10498 child_die && child_die->tag;
10499 child_die = sibling_die (child_die))
10500 {
96408a79 10501 struct call_site_parameter *parameter;
1788b2d3 10502 struct attribute *loc, *origin;
96408a79
SA
10503
10504 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10505 {
10506 /* Already printed the complaint above. */
10507 continue;
10508 }
10509
10510 gdb_assert (call_site->parameter_count < nparams);
10511 parameter = &call_site->parameter[call_site->parameter_count];
10512
1788b2d3
JK
10513 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10514 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10515 register is contained in DW_AT_GNU_call_site_value. */
96408a79 10516
24c5c679 10517 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3 10518 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7771576e 10519 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3
JK
10520 {
10521 sect_offset offset;
10522
10523 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10524 offset = dwarf2_get_ref_die_offset (origin);
d76b7dbc
JK
10525 if (!offset_in_cu_p (&cu->header, offset))
10526 {
10527 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10528 binding can be done only inside one CU. Such referenced DIE
10529 therefore cannot be even moved to DW_TAG_partial_unit. */
10530 complaint (&symfile_complaints,
10531 _("DW_AT_abstract_origin offset is not in CU for "
10532 "DW_TAG_GNU_call_site child DIE 0x%x "
10533 "[in module %s]"),
10534 child_die->offset.sect_off, objfile->name);
10535 continue;
10536 }
1788b2d3
JK
10537 parameter->u.param_offset.cu_off = (offset.sect_off
10538 - cu->header.offset.sect_off);
10539 }
10540 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
10541 {
10542 complaint (&symfile_complaints,
10543 _("No DW_FORM_block* DW_AT_location for "
10544 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10545 child_die->offset.sect_off, objfile->name);
96408a79
SA
10546 continue;
10547 }
24c5c679 10548 else
96408a79 10549 {
24c5c679
JK
10550 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10551 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10552 if (parameter->u.dwarf_reg != -1)
10553 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10554 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10555 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10556 &parameter->u.fb_offset))
10557 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10558 else
10559 {
10560 complaint (&symfile_complaints,
10561 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10562 "for DW_FORM_block* DW_AT_location is supported for "
10563 "DW_TAG_GNU_call_site child DIE 0x%x "
10564 "[in module %s]"),
10565 child_die->offset.sect_off, objfile->name);
10566 continue;
10567 }
96408a79
SA
10568 }
10569
10570 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10571 if (!attr_form_is_block (attr))
10572 {
10573 complaint (&symfile_complaints,
10574 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10575 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10576 child_die->offset.sect_off, objfile->name);
96408a79
SA
10577 continue;
10578 }
10579 parameter->value = DW_BLOCK (attr)->data;
10580 parameter->value_size = DW_BLOCK (attr)->size;
10581
10582 /* Parameters are not pre-cleared by memset above. */
10583 parameter->data_value = NULL;
10584 parameter->data_value_size = 0;
10585 call_site->parameter_count++;
10586
10587 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10588 if (attr)
10589 {
10590 if (!attr_form_is_block (attr))
10591 complaint (&symfile_complaints,
10592 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10593 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 10594 child_die->offset.sect_off, objfile->name);
96408a79
SA
10595 else
10596 {
10597 parameter->data_value = DW_BLOCK (attr)->data;
10598 parameter->data_value_size = DW_BLOCK (attr)->size;
10599 }
10600 }
10601 }
10602}
10603
43039443 10604/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
10605 Return 1 if the attributes are present and valid, otherwise, return 0.
10606 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
10607
10608static int
10609dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
10610 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10611 struct partial_symtab *ranges_pst)
43039443
JK
10612{
10613 struct objfile *objfile = cu->objfile;
10614 struct comp_unit_head *cu_header = &cu->header;
10615 bfd *obfd = objfile->obfd;
10616 unsigned int addr_size = cu_header->addr_size;
10617 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10618 /* Base address selection entry. */
10619 CORE_ADDR base;
10620 int found_base;
10621 unsigned int dummy;
d521ce57 10622 const gdb_byte *buffer;
43039443
JK
10623 CORE_ADDR marker;
10624 int low_set;
10625 CORE_ADDR low = 0;
10626 CORE_ADDR high = 0;
ff013f42 10627 CORE_ADDR baseaddr;
43039443 10628
d00adf39
DE
10629 found_base = cu->base_known;
10630 base = cu->base_address;
43039443 10631
be391dca 10632 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10633 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
10634 {
10635 complaint (&symfile_complaints,
10636 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10637 offset);
10638 return 0;
10639 }
dce234bc 10640 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
10641
10642 /* Read in the largest possible address. */
10643 marker = read_address (obfd, buffer, cu, &dummy);
10644 if ((marker & mask) == mask)
10645 {
10646 /* If we found the largest possible address, then
10647 read the base address. */
10648 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10649 buffer += 2 * addr_size;
10650 offset += 2 * addr_size;
10651 found_base = 1;
10652 }
10653
10654 low_set = 0;
10655
e7030f15 10656 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 10657
43039443
JK
10658 while (1)
10659 {
10660 CORE_ADDR range_beginning, range_end;
10661
10662 range_beginning = read_address (obfd, buffer, cu, &dummy);
10663 buffer += addr_size;
10664 range_end = read_address (obfd, buffer, cu, &dummy);
10665 buffer += addr_size;
10666 offset += 2 * addr_size;
10667
10668 /* An end of list marker is a pair of zero addresses. */
10669 if (range_beginning == 0 && range_end == 0)
10670 /* Found the end of list entry. */
10671 break;
10672
10673 /* Each base address selection entry is a pair of 2 values.
10674 The first is the largest possible address, the second is
10675 the base address. Check for a base address here. */
10676 if ((range_beginning & mask) == mask)
10677 {
10678 /* If we found the largest possible address, then
10679 read the base address. */
10680 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10681 found_base = 1;
10682 continue;
10683 }
10684
10685 if (!found_base)
10686 {
10687 /* We have no valid base address for the ranges
10688 data. */
10689 complaint (&symfile_complaints,
10690 _("Invalid .debug_ranges data (no base address)"));
10691 return 0;
10692 }
10693
9277c30c
UW
10694 if (range_beginning > range_end)
10695 {
10696 /* Inverted range entries are invalid. */
10697 complaint (&symfile_complaints,
10698 _("Invalid .debug_ranges data (inverted range)"));
10699 return 0;
10700 }
10701
10702 /* Empty range entries have no effect. */
10703 if (range_beginning == range_end)
10704 continue;
10705
43039443
JK
10706 range_beginning += base;
10707 range_end += base;
10708
01093045
DE
10709 /* A not-uncommon case of bad debug info.
10710 Don't pollute the addrmap with bad data. */
10711 if (range_beginning + baseaddr == 0
10712 && !dwarf2_per_objfile->has_section_at_zero)
10713 {
10714 complaint (&symfile_complaints,
10715 _(".debug_ranges entry has start address of zero"
10716 " [in module %s]"), objfile->name);
10717 continue;
10718 }
10719
9277c30c 10720 if (ranges_pst != NULL)
ff013f42 10721 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
10722 range_beginning + baseaddr,
10723 range_end - 1 + baseaddr,
ff013f42
JK
10724 ranges_pst);
10725
43039443
JK
10726 /* FIXME: This is recording everything as a low-high
10727 segment of consecutive addresses. We should have a
10728 data structure for discontiguous block ranges
10729 instead. */
10730 if (! low_set)
10731 {
10732 low = range_beginning;
10733 high = range_end;
10734 low_set = 1;
10735 }
10736 else
10737 {
10738 if (range_beginning < low)
10739 low = range_beginning;
10740 if (range_end > high)
10741 high = range_end;
10742 }
10743 }
10744
10745 if (! low_set)
10746 /* If the first entry is an end-of-list marker, the range
10747 describes an empty scope, i.e. no instructions. */
10748 return 0;
10749
10750 if (low_return)
10751 *low_return = low;
10752 if (high_return)
10753 *high_return = high;
10754 return 1;
10755}
10756
af34e669
DJ
10757/* Get low and high pc attributes from a die. Return 1 if the attributes
10758 are present and valid, otherwise, return 0. Return -1 if the range is
10759 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 10760
c906108c 10761static int
af34e669 10762dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
10763 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10764 struct partial_symtab *pst)
c906108c
SS
10765{
10766 struct attribute *attr;
91da1414 10767 struct attribute *attr_high;
af34e669
DJ
10768 CORE_ADDR low = 0;
10769 CORE_ADDR high = 0;
10770 int ret = 0;
c906108c 10771
91da1414
MW
10772 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10773 if (attr_high)
af34e669 10774 {
e142c38c 10775 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 10776 if (attr)
91da1414
MW
10777 {
10778 low = DW_ADDR (attr);
3019eac3
DE
10779 if (attr_high->form == DW_FORM_addr
10780 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10781 high = DW_ADDR (attr_high);
10782 else
10783 high = low + DW_UNSND (attr_high);
10784 }
af34e669
DJ
10785 else
10786 /* Found high w/o low attribute. */
10787 return 0;
10788
10789 /* Found consecutive range of addresses. */
10790 ret = 1;
10791 }
c906108c 10792 else
af34e669 10793 {
e142c38c 10794 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
10795 if (attr != NULL)
10796 {
ab435259
DE
10797 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10798 We take advantage of the fact that DW_AT_ranges does not appear
10799 in DW_TAG_compile_unit of DWO files. */
10800 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10801 unsigned int ranges_offset = (DW_UNSND (attr)
10802 + (need_ranges_base
10803 ? cu->ranges_base
10804 : 0));
2e3cf129 10805
af34e669 10806 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 10807 .debug_ranges section. */
2e3cf129 10808 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 10809 return 0;
43039443 10810 /* Found discontinuous range of addresses. */
af34e669
DJ
10811 ret = -1;
10812 }
10813 }
c906108c 10814
9373cf26
JK
10815 /* read_partial_die has also the strict LOW < HIGH requirement. */
10816 if (high <= low)
c906108c
SS
10817 return 0;
10818
10819 /* When using the GNU linker, .gnu.linkonce. sections are used to
10820 eliminate duplicate copies of functions and vtables and such.
10821 The linker will arbitrarily choose one and discard the others.
10822 The AT_*_pc values for such functions refer to local labels in
10823 these sections. If the section from that file was discarded, the
10824 labels are not in the output, so the relocs get a value of 0.
10825 If this is a discarded function, mark the pc bounds as invalid,
10826 so that GDB will ignore it. */
72dca2f5 10827 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
10828 return 0;
10829
10830 *lowpc = low;
96408a79
SA
10831 if (highpc)
10832 *highpc = high;
af34e669 10833 return ret;
c906108c
SS
10834}
10835
b084d499
JB
10836/* Assuming that DIE represents a subprogram DIE or a lexical block, get
10837 its low and high PC addresses. Do nothing if these addresses could not
10838 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10839 and HIGHPC to the high address if greater than HIGHPC. */
10840
10841static void
10842dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10843 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10844 struct dwarf2_cu *cu)
10845{
10846 CORE_ADDR low, high;
10847 struct die_info *child = die->child;
10848
d85a05f0 10849 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
10850 {
10851 *lowpc = min (*lowpc, low);
10852 *highpc = max (*highpc, high);
10853 }
10854
10855 /* If the language does not allow nested subprograms (either inside
10856 subprograms or lexical blocks), we're done. */
10857 if (cu->language != language_ada)
10858 return;
6e70227d 10859
b084d499
JB
10860 /* Check all the children of the given DIE. If it contains nested
10861 subprograms, then check their pc bounds. Likewise, we need to
10862 check lexical blocks as well, as they may also contain subprogram
10863 definitions. */
10864 while (child && child->tag)
10865 {
10866 if (child->tag == DW_TAG_subprogram
10867 || child->tag == DW_TAG_lexical_block)
10868 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10869 child = sibling_die (child);
10870 }
10871}
10872
fae299cd
DC
10873/* Get the low and high pc's represented by the scope DIE, and store
10874 them in *LOWPC and *HIGHPC. If the correct values can't be
10875 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10876
10877static void
10878get_scope_pc_bounds (struct die_info *die,
10879 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10880 struct dwarf2_cu *cu)
10881{
10882 CORE_ADDR best_low = (CORE_ADDR) -1;
10883 CORE_ADDR best_high = (CORE_ADDR) 0;
10884 CORE_ADDR current_low, current_high;
10885
d85a05f0 10886 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
10887 {
10888 best_low = current_low;
10889 best_high = current_high;
10890 }
10891 else
10892 {
10893 struct die_info *child = die->child;
10894
10895 while (child && child->tag)
10896 {
10897 switch (child->tag) {
10898 case DW_TAG_subprogram:
b084d499 10899 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
10900 break;
10901 case DW_TAG_namespace:
f55ee35c 10902 case DW_TAG_module:
fae299cd
DC
10903 /* FIXME: carlton/2004-01-16: Should we do this for
10904 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10905 that current GCC's always emit the DIEs corresponding
10906 to definitions of methods of classes as children of a
10907 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10908 the DIEs giving the declarations, which could be
10909 anywhere). But I don't see any reason why the
10910 standards says that they have to be there. */
10911 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10912
10913 if (current_low != ((CORE_ADDR) -1))
10914 {
10915 best_low = min (best_low, current_low);
10916 best_high = max (best_high, current_high);
10917 }
10918 break;
10919 default:
0963b4bd 10920 /* Ignore. */
fae299cd
DC
10921 break;
10922 }
10923
10924 child = sibling_die (child);
10925 }
10926 }
10927
10928 *lowpc = best_low;
10929 *highpc = best_high;
10930}
10931
801e3a5b
JB
10932/* Record the address ranges for BLOCK, offset by BASEADDR, as given
10933 in DIE. */
380bca97 10934
801e3a5b
JB
10935static void
10936dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10937 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10938{
bb5ed363 10939 struct objfile *objfile = cu->objfile;
801e3a5b 10940 struct attribute *attr;
91da1414 10941 struct attribute *attr_high;
801e3a5b 10942
91da1414
MW
10943 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10944 if (attr_high)
801e3a5b 10945 {
801e3a5b
JB
10946 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10947 if (attr)
10948 {
10949 CORE_ADDR low = DW_ADDR (attr);
91da1414 10950 CORE_ADDR high;
3019eac3
DE
10951 if (attr_high->form == DW_FORM_addr
10952 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
10953 high = DW_ADDR (attr_high);
10954 else
10955 high = low + DW_UNSND (attr_high);
9a619af0 10956
801e3a5b
JB
10957 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10958 }
10959 }
10960
10961 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10962 if (attr)
10963 {
bb5ed363 10964 bfd *obfd = objfile->obfd;
ab435259
DE
10965 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10966 We take advantage of the fact that DW_AT_ranges does not appear
10967 in DW_TAG_compile_unit of DWO files. */
10968 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
10969
10970 /* The value of the DW_AT_ranges attribute is the offset of the
10971 address range list in the .debug_ranges section. */
ab435259
DE
10972 unsigned long offset = (DW_UNSND (attr)
10973 + (need_ranges_base ? cu->ranges_base : 0));
d62bfeaf 10974 const gdb_byte *buffer;
801e3a5b
JB
10975
10976 /* For some target architectures, but not others, the
10977 read_address function sign-extends the addresses it returns.
10978 To recognize base address selection entries, we need a
10979 mask. */
10980 unsigned int addr_size = cu->header.addr_size;
10981 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10982
10983 /* The base address, to which the next pair is relative. Note
10984 that this 'base' is a DWARF concept: most entries in a range
10985 list are relative, to reduce the number of relocs against the
10986 debugging information. This is separate from this function's
10987 'baseaddr' argument, which GDB uses to relocate debugging
10988 information from a shared library based on the address at
10989 which the library was loaded. */
d00adf39
DE
10990 CORE_ADDR base = cu->base_address;
10991 int base_known = cu->base_known;
801e3a5b 10992
d62bfeaf 10993 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 10994 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
10995 {
10996 complaint (&symfile_complaints,
10997 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10998 offset);
10999 return;
11000 }
d62bfeaf 11001 buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
11002
11003 for (;;)
11004 {
11005 unsigned int bytes_read;
11006 CORE_ADDR start, end;
11007
11008 start = read_address (obfd, buffer, cu, &bytes_read);
11009 buffer += bytes_read;
11010 end = read_address (obfd, buffer, cu, &bytes_read);
11011 buffer += bytes_read;
11012
11013 /* Did we find the end of the range list? */
11014 if (start == 0 && end == 0)
11015 break;
11016
11017 /* Did we find a base address selection entry? */
11018 else if ((start & base_select_mask) == base_select_mask)
11019 {
11020 base = end;
11021 base_known = 1;
11022 }
11023
11024 /* We found an ordinary address range. */
11025 else
11026 {
11027 if (!base_known)
11028 {
11029 complaint (&symfile_complaints,
3e43a32a
MS
11030 _("Invalid .debug_ranges data "
11031 "(no base address)"));
801e3a5b
JB
11032 return;
11033 }
11034
9277c30c
UW
11035 if (start > end)
11036 {
11037 /* Inverted range entries are invalid. */
11038 complaint (&symfile_complaints,
11039 _("Invalid .debug_ranges data "
11040 "(inverted range)"));
11041 return;
11042 }
11043
11044 /* Empty range entries have no effect. */
11045 if (start == end)
11046 continue;
11047
01093045
DE
11048 start += base + baseaddr;
11049 end += base + baseaddr;
11050
11051 /* A not-uncommon case of bad debug info.
11052 Don't pollute the addrmap with bad data. */
11053 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11054 {
11055 complaint (&symfile_complaints,
11056 _(".debug_ranges entry has start address of zero"
11057 " [in module %s]"), objfile->name);
11058 continue;
11059 }
11060
11061 record_block_range (block, start, end - 1);
801e3a5b
JB
11062 }
11063 }
11064 }
11065}
11066
685b1105
JK
11067/* Check whether the producer field indicates either of GCC < 4.6, or the
11068 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 11069
685b1105
JK
11070static void
11071check_producer (struct dwarf2_cu *cu)
60d5a603
JK
11072{
11073 const char *cs;
11074 int major, minor, release;
11075
11076 if (cu->producer == NULL)
11077 {
11078 /* For unknown compilers expect their behavior is DWARF version
11079 compliant.
11080
11081 GCC started to support .debug_types sections by -gdwarf-4 since
11082 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11083 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11084 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11085 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 11086 }
685b1105 11087 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 11088 {
685b1105
JK
11089 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11090
ba919b58
TT
11091 cs = &cu->producer[strlen ("GNU ")];
11092 while (*cs && !isdigit (*cs))
11093 cs++;
11094 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11095 {
11096 /* Not recognized as GCC. */
11097 }
11098 else
1b80a9fa
JK
11099 {
11100 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11101 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11102 }
685b1105
JK
11103 }
11104 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11105 cu->producer_is_icc = 1;
11106 else
11107 {
11108 /* For other non-GCC compilers, expect their behavior is DWARF version
11109 compliant. */
60d5a603
JK
11110 }
11111
ba919b58 11112 cu->checked_producer = 1;
685b1105 11113}
ba919b58 11114
685b1105
JK
11115/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11116 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11117 during 4.6.0 experimental. */
11118
11119static int
11120producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11121{
11122 if (!cu->checked_producer)
11123 check_producer (cu);
11124
11125 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
11126}
11127
11128/* Return the default accessibility type if it is not overriden by
11129 DW_AT_accessibility. */
11130
11131static enum dwarf_access_attribute
11132dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11133{
11134 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11135 {
11136 /* The default DWARF 2 accessibility for members is public, the default
11137 accessibility for inheritance is private. */
11138
11139 if (die->tag != DW_TAG_inheritance)
11140 return DW_ACCESS_public;
11141 else
11142 return DW_ACCESS_private;
11143 }
11144 else
11145 {
11146 /* DWARF 3+ defines the default accessibility a different way. The same
11147 rules apply now for DW_TAG_inheritance as for the members and it only
11148 depends on the container kind. */
11149
11150 if (die->parent->tag == DW_TAG_class_type)
11151 return DW_ACCESS_private;
11152 else
11153 return DW_ACCESS_public;
11154 }
11155}
11156
74ac6d43
TT
11157/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11158 offset. If the attribute was not found return 0, otherwise return
11159 1. If it was found but could not properly be handled, set *OFFSET
11160 to 0. */
11161
11162static int
11163handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11164 LONGEST *offset)
11165{
11166 struct attribute *attr;
11167
11168 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11169 if (attr != NULL)
11170 {
11171 *offset = 0;
11172
11173 /* Note that we do not check for a section offset first here.
11174 This is because DW_AT_data_member_location is new in DWARF 4,
11175 so if we see it, we can assume that a constant form is really
11176 a constant and not a section offset. */
11177 if (attr_form_is_constant (attr))
11178 *offset = dwarf2_get_attr_constant_value (attr, 0);
11179 else if (attr_form_is_section_offset (attr))
11180 dwarf2_complex_location_expr_complaint ();
11181 else if (attr_form_is_block (attr))
11182 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11183 else
11184 dwarf2_complex_location_expr_complaint ();
11185
11186 return 1;
11187 }
11188
11189 return 0;
11190}
11191
c906108c
SS
11192/* Add an aggregate field to the field list. */
11193
11194static void
107d2387 11195dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 11196 struct dwarf2_cu *cu)
6e70227d 11197{
e7c27a73 11198 struct objfile *objfile = cu->objfile;
5e2b427d 11199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
11200 struct nextfield *new_field;
11201 struct attribute *attr;
11202 struct field *fp;
15d034d0 11203 const char *fieldname = "";
c906108c
SS
11204
11205 /* Allocate a new field list entry and link it in. */
11206 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 11207 make_cleanup (xfree, new_field);
c906108c 11208 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
11209
11210 if (die->tag == DW_TAG_inheritance)
11211 {
11212 new_field->next = fip->baseclasses;
11213 fip->baseclasses = new_field;
11214 }
11215 else
11216 {
11217 new_field->next = fip->fields;
11218 fip->fields = new_field;
11219 }
c906108c
SS
11220 fip->nfields++;
11221
e142c38c 11222 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
11223 if (attr)
11224 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
11225 else
11226 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
11227 if (new_field->accessibility != DW_ACCESS_public)
11228 fip->non_public_fields = 1;
60d5a603 11229
e142c38c 11230 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
11231 if (attr)
11232 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
11233 else
11234 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
11235
11236 fp = &new_field->field;
a9a9bd0f 11237
e142c38c 11238 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 11239 {
74ac6d43
TT
11240 LONGEST offset;
11241
a9a9bd0f 11242 /* Data member other than a C++ static data member. */
6e70227d 11243
c906108c 11244 /* Get type of field. */
e7c27a73 11245 fp->type = die_type (die, cu);
c906108c 11246
d6a843b5 11247 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 11248
c906108c 11249 /* Get bit size of field (zero if none). */
e142c38c 11250 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
11251 if (attr)
11252 {
11253 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11254 }
11255 else
11256 {
11257 FIELD_BITSIZE (*fp) = 0;
11258 }
11259
11260 /* Get bit offset of field. */
74ac6d43
TT
11261 if (handle_data_member_location (die, cu, &offset))
11262 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 11263 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
11264 if (attr)
11265 {
5e2b427d 11266 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
11267 {
11268 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
11269 additional bit offset from the MSB of the containing
11270 anonymous object to the MSB of the field. We don't
11271 have to do anything special since we don't need to
11272 know the size of the anonymous object. */
f41f5e61 11273 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
11274 }
11275 else
11276 {
11277 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
11278 MSB of the anonymous object, subtract off the number of
11279 bits from the MSB of the field to the MSB of the
11280 object, and then subtract off the number of bits of
11281 the field itself. The result is the bit offset of
11282 the LSB of the field. */
c906108c
SS
11283 int anonymous_size;
11284 int bit_offset = DW_UNSND (attr);
11285
e142c38c 11286 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11287 if (attr)
11288 {
11289 /* The size of the anonymous object containing
11290 the bit field is explicit, so use the
11291 indicated size (in bytes). */
11292 anonymous_size = DW_UNSND (attr);
11293 }
11294 else
11295 {
11296 /* The size of the anonymous object containing
11297 the bit field must be inferred from the type
11298 attribute of the data member containing the
11299 bit field. */
11300 anonymous_size = TYPE_LENGTH (fp->type);
11301 }
f41f5e61
PA
11302 SET_FIELD_BITPOS (*fp,
11303 (FIELD_BITPOS (*fp)
11304 + anonymous_size * bits_per_byte
11305 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
11306 }
11307 }
11308
11309 /* Get name of field. */
39cbfefa
DJ
11310 fieldname = dwarf2_name (die, cu);
11311 if (fieldname == NULL)
11312 fieldname = "";
d8151005
DJ
11313
11314 /* The name is already allocated along with this objfile, so we don't
11315 need to duplicate it for the type. */
11316 fp->name = fieldname;
c906108c
SS
11317
11318 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 11319 pointer or virtual base class pointer) to private. */
e142c38c 11320 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 11321 {
d48cc9dd 11322 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
11323 new_field->accessibility = DW_ACCESS_private;
11324 fip->non_public_fields = 1;
11325 }
11326 }
a9a9bd0f 11327 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 11328 {
a9a9bd0f
DC
11329 /* C++ static member. */
11330
11331 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11332 is a declaration, but all versions of G++ as of this writing
11333 (so through at least 3.2.1) incorrectly generate
11334 DW_TAG_variable tags. */
6e70227d 11335
ff355380 11336 const char *physname;
c906108c 11337
a9a9bd0f 11338 /* Get name of field. */
39cbfefa
DJ
11339 fieldname = dwarf2_name (die, cu);
11340 if (fieldname == NULL)
c906108c
SS
11341 return;
11342
254e6b9e 11343 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
11344 if (attr
11345 /* Only create a symbol if this is an external value.
11346 new_symbol checks this and puts the value in the global symbol
11347 table, which we want. If it is not external, new_symbol
11348 will try to put the value in cu->list_in_scope which is wrong. */
11349 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
11350 {
11351 /* A static const member, not much different than an enum as far as
11352 we're concerned, except that we can support more types. */
11353 new_symbol (die, NULL, cu);
11354 }
11355
2df3850c 11356 /* Get physical name. */
ff355380 11357 physname = dwarf2_physname (fieldname, die, cu);
c906108c 11358
d8151005
DJ
11359 /* The name is already allocated along with this objfile, so we don't
11360 need to duplicate it for the type. */
11361 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 11362 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 11363 FIELD_NAME (*fp) = fieldname;
c906108c
SS
11364 }
11365 else if (die->tag == DW_TAG_inheritance)
11366 {
74ac6d43 11367 LONGEST offset;
d4b96c9a 11368
74ac6d43
TT
11369 /* C++ base class field. */
11370 if (handle_data_member_location (die, cu, &offset))
11371 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 11372 FIELD_BITSIZE (*fp) = 0;
e7c27a73 11373 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
11374 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11375 fip->nbaseclasses++;
11376 }
11377}
11378
98751a41
JK
11379/* Add a typedef defined in the scope of the FIP's class. */
11380
11381static void
11382dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11383 struct dwarf2_cu *cu)
6e70227d 11384{
98751a41 11385 struct objfile *objfile = cu->objfile;
98751a41
JK
11386 struct typedef_field_list *new_field;
11387 struct attribute *attr;
11388 struct typedef_field *fp;
11389 char *fieldname = "";
11390
11391 /* Allocate a new field list entry and link it in. */
11392 new_field = xzalloc (sizeof (*new_field));
11393 make_cleanup (xfree, new_field);
11394
11395 gdb_assert (die->tag == DW_TAG_typedef);
11396
11397 fp = &new_field->field;
11398
11399 /* Get name of field. */
11400 fp->name = dwarf2_name (die, cu);
11401 if (fp->name == NULL)
11402 return;
11403
11404 fp->type = read_type_die (die, cu);
11405
11406 new_field->next = fip->typedef_field_list;
11407 fip->typedef_field_list = new_field;
11408 fip->typedef_field_list_count++;
11409}
11410
c906108c
SS
11411/* Create the vector of fields, and attach it to the type. */
11412
11413static void
fba45db2 11414dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11415 struct dwarf2_cu *cu)
c906108c
SS
11416{
11417 int nfields = fip->nfields;
11418
11419 /* Record the field count, allocate space for the array of fields,
11420 and create blank accessibility bitfields if necessary. */
11421 TYPE_NFIELDS (type) = nfields;
11422 TYPE_FIELDS (type) = (struct field *)
11423 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11424 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11425
b4ba55a1 11426 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
11427 {
11428 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11429
11430 TYPE_FIELD_PRIVATE_BITS (type) =
11431 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11432 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11433
11434 TYPE_FIELD_PROTECTED_BITS (type) =
11435 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11436 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11437
774b6a14
TT
11438 TYPE_FIELD_IGNORE_BITS (type) =
11439 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11440 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
11441 }
11442
11443 /* If the type has baseclasses, allocate and clear a bit vector for
11444 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 11445 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
11446 {
11447 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 11448 unsigned char *pointer;
c906108c
SS
11449
11450 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
11451 pointer = TYPE_ALLOC (type, num_bytes);
11452 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
11453 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11454 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11455 }
11456
3e43a32a
MS
11457 /* Copy the saved-up fields into the field vector. Start from the head of
11458 the list, adding to the tail of the field array, so that they end up in
11459 the same order in the array in which they were added to the list. */
c906108c
SS
11460 while (nfields-- > 0)
11461 {
7d0ccb61
DJ
11462 struct nextfield *fieldp;
11463
11464 if (fip->fields)
11465 {
11466 fieldp = fip->fields;
11467 fip->fields = fieldp->next;
11468 }
11469 else
11470 {
11471 fieldp = fip->baseclasses;
11472 fip->baseclasses = fieldp->next;
11473 }
11474
11475 TYPE_FIELD (type, nfields) = fieldp->field;
11476 switch (fieldp->accessibility)
c906108c 11477 {
c5aa993b 11478 case DW_ACCESS_private:
b4ba55a1
JB
11479 if (cu->language != language_ada)
11480 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 11481 break;
c906108c 11482
c5aa993b 11483 case DW_ACCESS_protected:
b4ba55a1
JB
11484 if (cu->language != language_ada)
11485 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 11486 break;
c906108c 11487
c5aa993b
JM
11488 case DW_ACCESS_public:
11489 break;
c906108c 11490
c5aa993b
JM
11491 default:
11492 /* Unknown accessibility. Complain and treat it as public. */
11493 {
e2e0b3e5 11494 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 11495 fieldp->accessibility);
c5aa993b
JM
11496 }
11497 break;
c906108c
SS
11498 }
11499 if (nfields < fip->nbaseclasses)
11500 {
7d0ccb61 11501 switch (fieldp->virtuality)
c906108c 11502 {
c5aa993b
JM
11503 case DW_VIRTUALITY_virtual:
11504 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 11505 if (cu->language == language_ada)
a73c6dcd 11506 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
11507 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11508 break;
c906108c
SS
11509 }
11510 }
c906108c
SS
11511 }
11512}
11513
7d27a96d
TT
11514/* Return true if this member function is a constructor, false
11515 otherwise. */
11516
11517static int
11518dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11519{
11520 const char *fieldname;
11521 const char *typename;
11522 int len;
11523
11524 if (die->parent == NULL)
11525 return 0;
11526
11527 if (die->parent->tag != DW_TAG_structure_type
11528 && die->parent->tag != DW_TAG_union_type
11529 && die->parent->tag != DW_TAG_class_type)
11530 return 0;
11531
11532 fieldname = dwarf2_name (die, cu);
11533 typename = dwarf2_name (die->parent, cu);
11534 if (fieldname == NULL || typename == NULL)
11535 return 0;
11536
11537 len = strlen (fieldname);
11538 return (strncmp (fieldname, typename, len) == 0
11539 && (typename[len] == '\0' || typename[len] == '<'));
11540}
11541
c906108c
SS
11542/* Add a member function to the proper fieldlist. */
11543
11544static void
107d2387 11545dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 11546 struct type *type, struct dwarf2_cu *cu)
c906108c 11547{
e7c27a73 11548 struct objfile *objfile = cu->objfile;
c906108c
SS
11549 struct attribute *attr;
11550 struct fnfieldlist *flp;
11551 int i;
11552 struct fn_field *fnp;
15d034d0 11553 const char *fieldname;
c906108c 11554 struct nextfnfield *new_fnfield;
f792889a 11555 struct type *this_type;
60d5a603 11556 enum dwarf_access_attribute accessibility;
c906108c 11557
b4ba55a1 11558 if (cu->language == language_ada)
a73c6dcd 11559 error (_("unexpected member function in Ada type"));
b4ba55a1 11560
2df3850c 11561 /* Get name of member function. */
39cbfefa
DJ
11562 fieldname = dwarf2_name (die, cu);
11563 if (fieldname == NULL)
2df3850c 11564 return;
c906108c 11565
c906108c
SS
11566 /* Look up member function name in fieldlist. */
11567 for (i = 0; i < fip->nfnfields; i++)
11568 {
27bfe10e 11569 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
11570 break;
11571 }
11572
11573 /* Create new list element if necessary. */
11574 if (i < fip->nfnfields)
11575 flp = &fip->fnfieldlists[i];
11576 else
11577 {
11578 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11579 {
11580 fip->fnfieldlists = (struct fnfieldlist *)
11581 xrealloc (fip->fnfieldlists,
11582 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 11583 * sizeof (struct fnfieldlist));
c906108c 11584 if (fip->nfnfields == 0)
c13c43fd 11585 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
11586 }
11587 flp = &fip->fnfieldlists[fip->nfnfields];
11588 flp->name = fieldname;
11589 flp->length = 0;
11590 flp->head = NULL;
3da10d80 11591 i = fip->nfnfields++;
c906108c
SS
11592 }
11593
11594 /* Create a new member function field and chain it to the field list
0963b4bd 11595 entry. */
c906108c 11596 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 11597 make_cleanup (xfree, new_fnfield);
c906108c
SS
11598 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11599 new_fnfield->next = flp->head;
11600 flp->head = new_fnfield;
11601 flp->length++;
11602
11603 /* Fill in the member function field info. */
11604 fnp = &new_fnfield->fnfield;
3da10d80
KS
11605
11606 /* Delay processing of the physname until later. */
11607 if (cu->language == language_cplus || cu->language == language_java)
11608 {
11609 add_to_method_list (type, i, flp->length - 1, fieldname,
11610 die, cu);
11611 }
11612 else
11613 {
1d06ead6 11614 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
11615 fnp->physname = physname ? physname : "";
11616 }
11617
c906108c 11618 fnp->type = alloc_type (objfile);
f792889a
DJ
11619 this_type = read_type_die (die, cu);
11620 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 11621 {
f792889a 11622 int nparams = TYPE_NFIELDS (this_type);
c906108c 11623
f792889a 11624 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
11625 of the method itself (TYPE_CODE_METHOD). */
11626 smash_to_method_type (fnp->type, type,
f792889a
DJ
11627 TYPE_TARGET_TYPE (this_type),
11628 TYPE_FIELDS (this_type),
11629 TYPE_NFIELDS (this_type),
11630 TYPE_VARARGS (this_type));
c906108c
SS
11631
11632 /* Handle static member functions.
c5aa993b 11633 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
11634 member functions. G++ helps GDB by marking the first
11635 parameter for non-static member functions (which is the this
11636 pointer) as artificial. We obtain this information from
11637 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 11638 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
11639 fnp->voffset = VOFFSET_STATIC;
11640 }
11641 else
e2e0b3e5 11642 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 11643 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
11644
11645 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 11646 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 11647 fnp->fcontext = die_containing_type (die, cu);
c906108c 11648
3e43a32a
MS
11649 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11650 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
11651
11652 /* Get accessibility. */
e142c38c 11653 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 11654 if (attr)
60d5a603
JK
11655 accessibility = DW_UNSND (attr);
11656 else
11657 accessibility = dwarf2_default_access_attribute (die, cu);
11658 switch (accessibility)
c906108c 11659 {
60d5a603
JK
11660 case DW_ACCESS_private:
11661 fnp->is_private = 1;
11662 break;
11663 case DW_ACCESS_protected:
11664 fnp->is_protected = 1;
11665 break;
c906108c
SS
11666 }
11667
b02dede2 11668 /* Check for artificial methods. */
e142c38c 11669 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
11670 if (attr && DW_UNSND (attr) != 0)
11671 fnp->is_artificial = 1;
11672
7d27a96d
TT
11673 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11674
0d564a31 11675 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
11676 function. For older versions of GCC, this is an offset in the
11677 appropriate virtual table, as specified by DW_AT_containing_type.
11678 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
11679 to the object address. */
11680
e142c38c 11681 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 11682 if (attr)
8e19ed76 11683 {
aec5aa8b 11684 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 11685 {
aec5aa8b
TT
11686 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11687 {
11688 /* Old-style GCC. */
11689 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11690 }
11691 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11692 || (DW_BLOCK (attr)->size > 1
11693 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11694 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11695 {
11696 struct dwarf_block blk;
11697 int offset;
11698
11699 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11700 ? 1 : 2);
11701 blk.size = DW_BLOCK (attr)->size - offset;
11702 blk.data = DW_BLOCK (attr)->data + offset;
11703 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11704 if ((fnp->voffset % cu->header.addr_size) != 0)
11705 dwarf2_complex_location_expr_complaint ();
11706 else
11707 fnp->voffset /= cu->header.addr_size;
11708 fnp->voffset += 2;
11709 }
11710 else
11711 dwarf2_complex_location_expr_complaint ();
11712
11713 if (!fnp->fcontext)
11714 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11715 }
3690dd37 11716 else if (attr_form_is_section_offset (attr))
8e19ed76 11717 {
4d3c2250 11718 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11719 }
11720 else
11721 {
4d3c2250
KB
11722 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11723 fieldname);
8e19ed76 11724 }
0d564a31 11725 }
d48cc9dd
DJ
11726 else
11727 {
11728 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11729 if (attr && DW_UNSND (attr))
11730 {
11731 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11732 complaint (&symfile_complaints,
3e43a32a
MS
11733 _("Member function \"%s\" (offset %d) is virtual "
11734 "but the vtable offset is not specified"),
b64f50a1 11735 fieldname, die->offset.sect_off);
9655fd1a 11736 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
11737 TYPE_CPLUS_DYNAMIC (type) = 1;
11738 }
11739 }
c906108c
SS
11740}
11741
11742/* Create the vector of member function fields, and attach it to the type. */
11743
11744static void
fba45db2 11745dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 11746 struct dwarf2_cu *cu)
c906108c
SS
11747{
11748 struct fnfieldlist *flp;
c906108c
SS
11749 int i;
11750
b4ba55a1 11751 if (cu->language == language_ada)
a73c6dcd 11752 error (_("unexpected member functions in Ada type"));
b4ba55a1 11753
c906108c
SS
11754 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11755 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11756 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11757
11758 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11759 {
11760 struct nextfnfield *nfp = flp->head;
11761 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11762 int k;
11763
11764 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11765 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11766 fn_flp->fn_fields = (struct fn_field *)
11767 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11768 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 11769 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
11770 }
11771
11772 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
11773}
11774
1168df01
JB
11775/* Returns non-zero if NAME is the name of a vtable member in CU's
11776 language, zero otherwise. */
11777static int
11778is_vtable_name (const char *name, struct dwarf2_cu *cu)
11779{
11780 static const char vptr[] = "_vptr";
987504bb 11781 static const char vtable[] = "vtable";
1168df01 11782
987504bb
JJ
11783 /* Look for the C++ and Java forms of the vtable. */
11784 if ((cu->language == language_java
11785 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11786 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11787 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
11788 return 1;
11789
11790 return 0;
11791}
11792
c0dd20ea 11793/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
11794 functions, with the ABI-specified layout. If TYPE describes
11795 such a structure, smash it into a member function type.
61049d3b
DJ
11796
11797 GCC shouldn't do this; it should just output pointer to member DIEs.
11798 This is GCC PR debug/28767. */
c0dd20ea 11799
0b92b5bb
TT
11800static void
11801quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 11802{
0b92b5bb 11803 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
11804
11805 /* Check for a structure with no name and two children. */
0b92b5bb
TT
11806 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11807 return;
c0dd20ea
DJ
11808
11809 /* Check for __pfn and __delta members. */
0b92b5bb
TT
11810 if (TYPE_FIELD_NAME (type, 0) == NULL
11811 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11812 || TYPE_FIELD_NAME (type, 1) == NULL
11813 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11814 return;
c0dd20ea
DJ
11815
11816 /* Find the type of the method. */
0b92b5bb 11817 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
11818 if (pfn_type == NULL
11819 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11820 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 11821 return;
c0dd20ea
DJ
11822
11823 /* Look for the "this" argument. */
11824 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11825 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 11826 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 11827 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 11828 return;
c0dd20ea
DJ
11829
11830 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
11831 new_type = alloc_type (objfile);
11832 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
11833 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11834 TYPE_VARARGS (pfn_type));
0b92b5bb 11835 smash_to_methodptr_type (type, new_type);
c0dd20ea 11836}
1168df01 11837
685b1105
JK
11838/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11839 (icc). */
11840
11841static int
11842producer_is_icc (struct dwarf2_cu *cu)
11843{
11844 if (!cu->checked_producer)
11845 check_producer (cu);
11846
11847 return cu->producer_is_icc;
11848}
11849
c906108c 11850/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
11851 (definition) to create a type for the structure or union. Fill in
11852 the type's name and general properties; the members will not be
3d1d5ea3 11853 processed until process_structure_scope.
c906108c 11854
c767944b
DJ
11855 NOTE: we need to call these functions regardless of whether or not the
11856 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
11857 structure or union. This gets the type entered into our set of
11858 user defined types.
11859
11860 However, if the structure is incomplete (an opaque struct/union)
11861 then suppress creating a symbol table entry for it since gdb only
11862 wants to find the one with the complete definition. Note that if
11863 it is complete, we just call new_symbol, which does it's own
11864 checking about whether the struct/union is anonymous or not (and
11865 suppresses creating a symbol table entry itself). */
11866
f792889a 11867static struct type *
134d01f1 11868read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11869{
e7c27a73 11870 struct objfile *objfile = cu->objfile;
c906108c
SS
11871 struct type *type;
11872 struct attribute *attr;
15d034d0 11873 const char *name;
c906108c 11874
348e048f
DE
11875 /* If the definition of this type lives in .debug_types, read that type.
11876 Don't follow DW_AT_specification though, that will take us back up
11877 the chain and we want to go down. */
45e58e77 11878 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
11879 if (attr)
11880 {
ac9ec31b 11881 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 11882
ac9ec31b 11883 /* The type's CU may not be the same as CU.
02142a6c 11884 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
11885 return set_die_type (die, type, cu);
11886 }
11887
c0dd20ea 11888 type = alloc_type (objfile);
c906108c 11889 INIT_CPLUS_SPECIFIC (type);
93311388 11890
39cbfefa
DJ
11891 name = dwarf2_name (die, cu);
11892 if (name != NULL)
c906108c 11893 {
987504bb
JJ
11894 if (cu->language == language_cplus
11895 || cu->language == language_java)
63d06c5c 11896 {
15d034d0 11897 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
11898
11899 /* dwarf2_full_name might have already finished building the DIE's
11900 type. If so, there is no need to continue. */
11901 if (get_die_type (die, cu) != NULL)
11902 return get_die_type (die, cu);
11903
11904 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
11905 if (die->tag == DW_TAG_structure_type
11906 || die->tag == DW_TAG_class_type)
11907 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
11908 }
11909 else
11910 {
d8151005
DJ
11911 /* The name is already allocated along with this objfile, so
11912 we don't need to duplicate it for the type. */
7d455152 11913 TYPE_TAG_NAME (type) = name;
94af9270
KS
11914 if (die->tag == DW_TAG_class_type)
11915 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 11916 }
c906108c
SS
11917 }
11918
11919 if (die->tag == DW_TAG_structure_type)
11920 {
11921 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11922 }
11923 else if (die->tag == DW_TAG_union_type)
11924 {
11925 TYPE_CODE (type) = TYPE_CODE_UNION;
11926 }
11927 else
11928 {
c906108c
SS
11929 TYPE_CODE (type) = TYPE_CODE_CLASS;
11930 }
11931
0cc2414c
TT
11932 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11933 TYPE_DECLARED_CLASS (type) = 1;
11934
e142c38c 11935 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
11936 if (attr)
11937 {
11938 TYPE_LENGTH (type) = DW_UNSND (attr);
11939 }
11940 else
11941 {
11942 TYPE_LENGTH (type) = 0;
11943 }
11944
685b1105
JK
11945 if (producer_is_icc (cu))
11946 {
11947 /* ICC does not output the required DW_AT_declaration
11948 on incomplete types, but gives them a size of zero. */
11949 }
11950 else
11951 TYPE_STUB_SUPPORTED (type) = 1;
11952
dc718098 11953 if (die_is_declaration (die, cu))
876cecd0 11954 TYPE_STUB (type) = 1;
a6c727b2
DJ
11955 else if (attr == NULL && die->child == NULL
11956 && producer_is_realview (cu->producer))
11957 /* RealView does not output the required DW_AT_declaration
11958 on incomplete types. */
11959 TYPE_STUB (type) = 1;
dc718098 11960
c906108c
SS
11961 /* We need to add the type field to the die immediately so we don't
11962 infinitely recurse when dealing with pointers to the structure
0963b4bd 11963 type within the structure itself. */
1c379e20 11964 set_die_type (die, type, cu);
c906108c 11965
7e314c57
JK
11966 /* set_die_type should be already done. */
11967 set_descriptive_type (type, die, cu);
11968
c767944b
DJ
11969 return type;
11970}
11971
11972/* Finish creating a structure or union type, including filling in
11973 its members and creating a symbol for it. */
11974
11975static void
11976process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11977{
11978 struct objfile *objfile = cu->objfile;
11979 struct die_info *child_die = die->child;
11980 struct type *type;
11981
11982 type = get_die_type (die, cu);
11983 if (type == NULL)
11984 type = read_structure_type (die, cu);
11985
e142c38c 11986 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
11987 {
11988 struct field_info fi;
11989 struct die_info *child_die;
34eaf542 11990 VEC (symbolp) *template_args = NULL;
c767944b 11991 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
11992
11993 memset (&fi, 0, sizeof (struct field_info));
11994
639d11d3 11995 child_die = die->child;
c906108c
SS
11996
11997 while (child_die && child_die->tag)
11998 {
a9a9bd0f
DC
11999 if (child_die->tag == DW_TAG_member
12000 || child_die->tag == DW_TAG_variable)
c906108c 12001 {
a9a9bd0f
DC
12002 /* NOTE: carlton/2002-11-05: A C++ static data member
12003 should be a DW_TAG_member that is a declaration, but
12004 all versions of G++ as of this writing (so through at
12005 least 3.2.1) incorrectly generate DW_TAG_variable
12006 tags for them instead. */
e7c27a73 12007 dwarf2_add_field (&fi, child_die, cu);
c906108c 12008 }
8713b1b1 12009 else if (child_die->tag == DW_TAG_subprogram)
c906108c 12010 {
0963b4bd 12011 /* C++ member function. */
e7c27a73 12012 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
12013 }
12014 else if (child_die->tag == DW_TAG_inheritance)
12015 {
12016 /* C++ base class field. */
e7c27a73 12017 dwarf2_add_field (&fi, child_die, cu);
c906108c 12018 }
98751a41
JK
12019 else if (child_die->tag == DW_TAG_typedef)
12020 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
12021 else if (child_die->tag == DW_TAG_template_type_param
12022 || child_die->tag == DW_TAG_template_value_param)
12023 {
12024 struct symbol *arg = new_symbol (child_die, NULL, cu);
12025
f1078f66
DJ
12026 if (arg != NULL)
12027 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
12028 }
12029
c906108c
SS
12030 child_die = sibling_die (child_die);
12031 }
12032
34eaf542
TT
12033 /* Attach template arguments to type. */
12034 if (! VEC_empty (symbolp, template_args))
12035 {
12036 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12037 TYPE_N_TEMPLATE_ARGUMENTS (type)
12038 = VEC_length (symbolp, template_args);
12039 TYPE_TEMPLATE_ARGUMENTS (type)
12040 = obstack_alloc (&objfile->objfile_obstack,
12041 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12042 * sizeof (struct symbol *)));
12043 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12044 VEC_address (symbolp, template_args),
12045 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12046 * sizeof (struct symbol *)));
12047 VEC_free (symbolp, template_args);
12048 }
12049
c906108c
SS
12050 /* Attach fields and member functions to the type. */
12051 if (fi.nfields)
e7c27a73 12052 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
12053 if (fi.nfnfields)
12054 {
e7c27a73 12055 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 12056
c5aa993b 12057 /* Get the type which refers to the base class (possibly this
c906108c 12058 class itself) which contains the vtable pointer for the current
0d564a31
DJ
12059 class from the DW_AT_containing_type attribute. This use of
12060 DW_AT_containing_type is a GNU extension. */
c906108c 12061
e142c38c 12062 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 12063 {
e7c27a73 12064 struct type *t = die_containing_type (die, cu);
c906108c
SS
12065
12066 TYPE_VPTR_BASETYPE (type) = t;
12067 if (type == t)
12068 {
c906108c
SS
12069 int i;
12070
12071 /* Our own class provides vtbl ptr. */
12072 for (i = TYPE_NFIELDS (t) - 1;
12073 i >= TYPE_N_BASECLASSES (t);
12074 --i)
12075 {
0d5cff50 12076 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 12077
1168df01 12078 if (is_vtable_name (fieldname, cu))
c906108c
SS
12079 {
12080 TYPE_VPTR_FIELDNO (type) = i;
12081 break;
12082 }
12083 }
12084
12085 /* Complain if virtual function table field not found. */
12086 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 12087 complaint (&symfile_complaints,
3e43a32a
MS
12088 _("virtual function table pointer "
12089 "not found when defining class '%s'"),
4d3c2250
KB
12090 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12091 "");
c906108c
SS
12092 }
12093 else
12094 {
12095 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12096 }
12097 }
f6235d4c
EZ
12098 else if (cu->producer
12099 && strncmp (cu->producer,
12100 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12101 {
12102 /* The IBM XLC compiler does not provide direct indication
12103 of the containing type, but the vtable pointer is
12104 always named __vfp. */
12105
12106 int i;
12107
12108 for (i = TYPE_NFIELDS (type) - 1;
12109 i >= TYPE_N_BASECLASSES (type);
12110 --i)
12111 {
12112 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12113 {
12114 TYPE_VPTR_FIELDNO (type) = i;
12115 TYPE_VPTR_BASETYPE (type) = type;
12116 break;
12117 }
12118 }
12119 }
c906108c 12120 }
98751a41
JK
12121
12122 /* Copy fi.typedef_field_list linked list elements content into the
12123 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12124 if (fi.typedef_field_list)
12125 {
12126 int i = fi.typedef_field_list_count;
12127
a0d7a4ff 12128 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
12129 TYPE_TYPEDEF_FIELD_ARRAY (type)
12130 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12131 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12132
12133 /* Reverse the list order to keep the debug info elements order. */
12134 while (--i >= 0)
12135 {
12136 struct typedef_field *dest, *src;
6e70227d 12137
98751a41
JK
12138 dest = &TYPE_TYPEDEF_FIELD (type, i);
12139 src = &fi.typedef_field_list->field;
12140 fi.typedef_field_list = fi.typedef_field_list->next;
12141 *dest = *src;
12142 }
12143 }
c767944b
DJ
12144
12145 do_cleanups (back_to);
eb2a6f42
TT
12146
12147 if (HAVE_CPLUS_STRUCT (type))
12148 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 12149 }
63d06c5c 12150
bb5ed363 12151 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 12152
90aeadfc
DC
12153 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12154 snapshots) has been known to create a die giving a declaration
12155 for a class that has, as a child, a die giving a definition for a
12156 nested class. So we have to process our children even if the
12157 current die is a declaration. Normally, of course, a declaration
12158 won't have any children at all. */
134d01f1 12159
90aeadfc
DC
12160 while (child_die != NULL && child_die->tag)
12161 {
12162 if (child_die->tag == DW_TAG_member
12163 || child_die->tag == DW_TAG_variable
34eaf542
TT
12164 || child_die->tag == DW_TAG_inheritance
12165 || child_die->tag == DW_TAG_template_value_param
12166 || child_die->tag == DW_TAG_template_type_param)
134d01f1 12167 {
90aeadfc 12168 /* Do nothing. */
134d01f1 12169 }
90aeadfc
DC
12170 else
12171 process_die (child_die, cu);
134d01f1 12172
90aeadfc 12173 child_die = sibling_die (child_die);
134d01f1
DJ
12174 }
12175
fa4028e9
JB
12176 /* Do not consider external references. According to the DWARF standard,
12177 these DIEs are identified by the fact that they have no byte_size
12178 attribute, and a declaration attribute. */
12179 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12180 || !die_is_declaration (die, cu))
c767944b 12181 new_symbol (die, type, cu);
134d01f1
DJ
12182}
12183
12184/* Given a DW_AT_enumeration_type die, set its type. We do not
12185 complete the type's fields yet, or create any symbols. */
c906108c 12186
f792889a 12187static struct type *
134d01f1 12188read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12189{
e7c27a73 12190 struct objfile *objfile = cu->objfile;
c906108c 12191 struct type *type;
c906108c 12192 struct attribute *attr;
0114d602 12193 const char *name;
134d01f1 12194
348e048f
DE
12195 /* If the definition of this type lives in .debug_types, read that type.
12196 Don't follow DW_AT_specification though, that will take us back up
12197 the chain and we want to go down. */
45e58e77 12198 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
12199 if (attr)
12200 {
ac9ec31b 12201 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 12202
ac9ec31b 12203 /* The type's CU may not be the same as CU.
02142a6c 12204 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
12205 return set_die_type (die, type, cu);
12206 }
12207
c906108c
SS
12208 type = alloc_type (objfile);
12209
12210 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 12211 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 12212 if (name != NULL)
7d455152 12213 TYPE_TAG_NAME (type) = name;
c906108c 12214
e142c38c 12215 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12216 if (attr)
12217 {
12218 TYPE_LENGTH (type) = DW_UNSND (attr);
12219 }
12220 else
12221 {
12222 TYPE_LENGTH (type) = 0;
12223 }
12224
137033e9
JB
12225 /* The enumeration DIE can be incomplete. In Ada, any type can be
12226 declared as private in the package spec, and then defined only
12227 inside the package body. Such types are known as Taft Amendment
12228 Types. When another package uses such a type, an incomplete DIE
12229 may be generated by the compiler. */
02eb380e 12230 if (die_is_declaration (die, cu))
876cecd0 12231 TYPE_STUB (type) = 1;
02eb380e 12232
f792889a 12233 return set_die_type (die, type, cu);
134d01f1
DJ
12234}
12235
12236/* Given a pointer to a die which begins an enumeration, process all
12237 the dies that define the members of the enumeration, and create the
12238 symbol for the enumeration type.
12239
12240 NOTE: We reverse the order of the element list. */
12241
12242static void
12243process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12244{
f792889a 12245 struct type *this_type;
134d01f1 12246
f792889a
DJ
12247 this_type = get_die_type (die, cu);
12248 if (this_type == NULL)
12249 this_type = read_enumeration_type (die, cu);
9dc481d3 12250
639d11d3 12251 if (die->child != NULL)
c906108c 12252 {
9dc481d3
DE
12253 struct die_info *child_die;
12254 struct symbol *sym;
12255 struct field *fields = NULL;
12256 int num_fields = 0;
12257 int unsigned_enum = 1;
15d034d0 12258 const char *name;
cafec441
TT
12259 int flag_enum = 1;
12260 ULONGEST mask = 0;
9dc481d3 12261
639d11d3 12262 child_die = die->child;
c906108c
SS
12263 while (child_die && child_die->tag)
12264 {
12265 if (child_die->tag != DW_TAG_enumerator)
12266 {
e7c27a73 12267 process_die (child_die, cu);
c906108c
SS
12268 }
12269 else
12270 {
39cbfefa
DJ
12271 name = dwarf2_name (child_die, cu);
12272 if (name)
c906108c 12273 {
f792889a 12274 sym = new_symbol (child_die, this_type, cu);
c906108c 12275 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
12276 {
12277 unsigned_enum = 0;
12278 flag_enum = 0;
12279 }
12280 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12281 flag_enum = 0;
12282 else
12283 mask |= SYMBOL_VALUE (sym);
c906108c
SS
12284
12285 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12286 {
12287 fields = (struct field *)
12288 xrealloc (fields,
12289 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 12290 * sizeof (struct field));
c906108c
SS
12291 }
12292
3567439c 12293 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 12294 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 12295 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
12296 FIELD_BITSIZE (fields[num_fields]) = 0;
12297
12298 num_fields++;
12299 }
12300 }
12301
12302 child_die = sibling_die (child_die);
12303 }
12304
12305 if (num_fields)
12306 {
f792889a
DJ
12307 TYPE_NFIELDS (this_type) = num_fields;
12308 TYPE_FIELDS (this_type) = (struct field *)
12309 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12310 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 12311 sizeof (struct field) * num_fields);
b8c9b27d 12312 xfree (fields);
c906108c
SS
12313 }
12314 if (unsigned_enum)
876cecd0 12315 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
12316 if (flag_enum)
12317 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 12318 }
134d01f1 12319
6c83ed52
TT
12320 /* If we are reading an enum from a .debug_types unit, and the enum
12321 is a declaration, and the enum is not the signatured type in the
12322 unit, then we do not want to add a symbol for it. Adding a
12323 symbol would in some cases obscure the true definition of the
12324 enum, giving users an incomplete type when the definition is
12325 actually available. Note that we do not want to do this for all
12326 enums which are just declarations, because C++0x allows forward
12327 enum declarations. */
3019eac3 12328 if (cu->per_cu->is_debug_types
6c83ed52
TT
12329 && die_is_declaration (die, cu))
12330 {
52dc124a 12331 struct signatured_type *sig_type;
6c83ed52 12332
c0f78cd4 12333 sig_type = (struct signatured_type *) cu->per_cu;
3019eac3
DE
12334 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12335 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
12336 return;
12337 }
12338
f792889a 12339 new_symbol (die, this_type, cu);
c906108c
SS
12340}
12341
12342/* Extract all information from a DW_TAG_array_type DIE and put it in
12343 the DIE's type field. For now, this only handles one dimensional
12344 arrays. */
12345
f792889a 12346static struct type *
e7c27a73 12347read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12348{
e7c27a73 12349 struct objfile *objfile = cu->objfile;
c906108c 12350 struct die_info *child_die;
7e314c57 12351 struct type *type;
c906108c
SS
12352 struct type *element_type, *range_type, *index_type;
12353 struct type **range_types = NULL;
12354 struct attribute *attr;
12355 int ndim = 0;
12356 struct cleanup *back_to;
15d034d0 12357 const char *name;
c906108c 12358
e7c27a73 12359 element_type = die_type (die, cu);
c906108c 12360
7e314c57
JK
12361 /* The die_type call above may have already set the type for this DIE. */
12362 type = get_die_type (die, cu);
12363 if (type)
12364 return type;
12365
c906108c
SS
12366 /* Irix 6.2 native cc creates array types without children for
12367 arrays with unspecified length. */
639d11d3 12368 if (die->child == NULL)
c906108c 12369 {
46bf5051 12370 index_type = objfile_type (objfile)->builtin_int;
c906108c 12371 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
12372 type = create_array_type (NULL, element_type, range_type);
12373 return set_die_type (die, type, cu);
c906108c
SS
12374 }
12375
12376 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 12377 child_die = die->child;
c906108c
SS
12378 while (child_die && child_die->tag)
12379 {
12380 if (child_die->tag == DW_TAG_subrange_type)
12381 {
f792889a 12382 struct type *child_type = read_type_die (child_die, cu);
9a619af0 12383
f792889a 12384 if (child_type != NULL)
a02abb62 12385 {
0963b4bd
MS
12386 /* The range type was succesfully read. Save it for the
12387 array type creation. */
a02abb62
JB
12388 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12389 {
12390 range_types = (struct type **)
12391 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12392 * sizeof (struct type *));
12393 if (ndim == 0)
12394 make_cleanup (free_current_contents, &range_types);
12395 }
f792889a 12396 range_types[ndim++] = child_type;
a02abb62 12397 }
c906108c
SS
12398 }
12399 child_die = sibling_die (child_die);
12400 }
12401
12402 /* Dwarf2 dimensions are output from left to right, create the
12403 necessary array types in backwards order. */
7ca2d3a3 12404
c906108c 12405 type = element_type;
7ca2d3a3
DL
12406
12407 if (read_array_order (die, cu) == DW_ORD_col_major)
12408 {
12409 int i = 0;
9a619af0 12410
7ca2d3a3
DL
12411 while (i < ndim)
12412 type = create_array_type (NULL, type, range_types[i++]);
12413 }
12414 else
12415 {
12416 while (ndim-- > 0)
12417 type = create_array_type (NULL, type, range_types[ndim]);
12418 }
c906108c 12419
f5f8a009
EZ
12420 /* Understand Dwarf2 support for vector types (like they occur on
12421 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12422 array type. This is not part of the Dwarf2/3 standard yet, but a
12423 custom vendor extension. The main difference between a regular
12424 array and the vector variant is that vectors are passed by value
12425 to functions. */
e142c38c 12426 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 12427 if (attr)
ea37ba09 12428 make_vector_type (type);
f5f8a009 12429
dbc98a8b
KW
12430 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12431 implementation may choose to implement triple vectors using this
12432 attribute. */
12433 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12434 if (attr)
12435 {
12436 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12437 TYPE_LENGTH (type) = DW_UNSND (attr);
12438 else
3e43a32a
MS
12439 complaint (&symfile_complaints,
12440 _("DW_AT_byte_size for array type smaller "
12441 "than the total size of elements"));
dbc98a8b
KW
12442 }
12443
39cbfefa
DJ
12444 name = dwarf2_name (die, cu);
12445 if (name)
12446 TYPE_NAME (type) = name;
6e70227d 12447
0963b4bd 12448 /* Install the type in the die. */
7e314c57
JK
12449 set_die_type (die, type, cu);
12450
12451 /* set_die_type should be already done. */
b4ba55a1
JB
12452 set_descriptive_type (type, die, cu);
12453
c906108c
SS
12454 do_cleanups (back_to);
12455
7e314c57 12456 return type;
c906108c
SS
12457}
12458
7ca2d3a3 12459static enum dwarf_array_dim_ordering
6e70227d 12460read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
12461{
12462 struct attribute *attr;
12463
12464 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12465
12466 if (attr) return DW_SND (attr);
12467
0963b4bd
MS
12468 /* GNU F77 is a special case, as at 08/2004 array type info is the
12469 opposite order to the dwarf2 specification, but data is still
12470 laid out as per normal fortran.
7ca2d3a3 12471
0963b4bd
MS
12472 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12473 version checking. */
7ca2d3a3 12474
905e0470
PM
12475 if (cu->language == language_fortran
12476 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
12477 {
12478 return DW_ORD_row_major;
12479 }
12480
6e70227d 12481 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
12482 {
12483 case array_column_major:
12484 return DW_ORD_col_major;
12485 case array_row_major:
12486 default:
12487 return DW_ORD_row_major;
12488 };
12489}
12490
72019c9c 12491/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 12492 the DIE's type field. */
72019c9c 12493
f792889a 12494static struct type *
72019c9c
GM
12495read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12496{
7e314c57
JK
12497 struct type *domain_type, *set_type;
12498 struct attribute *attr;
f792889a 12499
7e314c57
JK
12500 domain_type = die_type (die, cu);
12501
12502 /* The die_type call above may have already set the type for this DIE. */
12503 set_type = get_die_type (die, cu);
12504 if (set_type)
12505 return set_type;
12506
12507 set_type = create_set_type (NULL, domain_type);
12508
12509 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
12510 if (attr)
12511 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 12512
f792889a 12513 return set_die_type (die, set_type, cu);
72019c9c 12514}
7ca2d3a3 12515
0971de02
TT
12516/* A helper for read_common_block that creates a locexpr baton.
12517 SYM is the symbol which we are marking as computed.
12518 COMMON_DIE is the DIE for the common block.
12519 COMMON_LOC is the location expression attribute for the common
12520 block itself.
12521 MEMBER_LOC is the location expression attribute for the particular
12522 member of the common block that we are processing.
12523 CU is the CU from which the above come. */
12524
12525static void
12526mark_common_block_symbol_computed (struct symbol *sym,
12527 struct die_info *common_die,
12528 struct attribute *common_loc,
12529 struct attribute *member_loc,
12530 struct dwarf2_cu *cu)
12531{
12532 struct objfile *objfile = dwarf2_per_objfile->objfile;
12533 struct dwarf2_locexpr_baton *baton;
12534 gdb_byte *ptr;
12535 unsigned int cu_off;
12536 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12537 LONGEST offset = 0;
12538
12539 gdb_assert (common_loc && member_loc);
12540 gdb_assert (attr_form_is_block (common_loc));
12541 gdb_assert (attr_form_is_block (member_loc)
12542 || attr_form_is_constant (member_loc));
12543
12544 baton = obstack_alloc (&objfile->objfile_obstack,
12545 sizeof (struct dwarf2_locexpr_baton));
12546 baton->per_cu = cu->per_cu;
12547 gdb_assert (baton->per_cu);
12548
12549 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12550
12551 if (attr_form_is_constant (member_loc))
12552 {
12553 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12554 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12555 }
12556 else
12557 baton->size += DW_BLOCK (member_loc)->size;
12558
12559 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12560 baton->data = ptr;
12561
12562 *ptr++ = DW_OP_call4;
12563 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12564 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12565 ptr += 4;
12566
12567 if (attr_form_is_constant (member_loc))
12568 {
12569 *ptr++ = DW_OP_addr;
12570 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12571 ptr += cu->header.addr_size;
12572 }
12573 else
12574 {
12575 /* We have to copy the data here, because DW_OP_call4 will only
12576 use a DW_AT_location attribute. */
12577 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12578 ptr += DW_BLOCK (member_loc)->size;
12579 }
12580
12581 *ptr++ = DW_OP_plus;
12582 gdb_assert (ptr - baton->data == baton->size);
12583
0971de02 12584 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 12585 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
12586}
12587
4357ac6c
TT
12588/* Create appropriate locally-scoped variables for all the
12589 DW_TAG_common_block entries. Also create a struct common_block
12590 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12591 is used to sepate the common blocks name namespace from regular
12592 variable names. */
c906108c
SS
12593
12594static void
e7c27a73 12595read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12596{
0971de02
TT
12597 struct attribute *attr;
12598
12599 attr = dwarf2_attr (die, DW_AT_location, cu);
12600 if (attr)
12601 {
12602 /* Support the .debug_loc offsets. */
12603 if (attr_form_is_block (attr))
12604 {
12605 /* Ok. */
12606 }
12607 else if (attr_form_is_section_offset (attr))
12608 {
12609 dwarf2_complex_location_expr_complaint ();
12610 attr = NULL;
12611 }
12612 else
12613 {
12614 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12615 "common block member");
12616 attr = NULL;
12617 }
12618 }
12619
639d11d3 12620 if (die->child != NULL)
c906108c 12621 {
4357ac6c
TT
12622 struct objfile *objfile = cu->objfile;
12623 struct die_info *child_die;
12624 size_t n_entries = 0, size;
12625 struct common_block *common_block;
12626 struct symbol *sym;
74ac6d43 12627
4357ac6c
TT
12628 for (child_die = die->child;
12629 child_die && child_die->tag;
12630 child_die = sibling_die (child_die))
12631 ++n_entries;
12632
12633 size = (sizeof (struct common_block)
12634 + (n_entries - 1) * sizeof (struct symbol *));
12635 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12636 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12637 common_block->n_entries = 0;
12638
12639 for (child_die = die->child;
12640 child_die && child_die->tag;
12641 child_die = sibling_die (child_die))
12642 {
12643 /* Create the symbol in the DW_TAG_common_block block in the current
12644 symbol scope. */
e7c27a73 12645 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
12646 if (sym != NULL)
12647 {
12648 struct attribute *member_loc;
12649
12650 common_block->contents[common_block->n_entries++] = sym;
12651
12652 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12653 cu);
12654 if (member_loc)
12655 {
12656 /* GDB has handled this for a long time, but it is
12657 not specified by DWARF. It seems to have been
12658 emitted by gfortran at least as recently as:
12659 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12660 complaint (&symfile_complaints,
12661 _("Variable in common block has "
12662 "DW_AT_data_member_location "
12663 "- DIE at 0x%x [in module %s]"),
12664 child_die->offset.sect_off, cu->objfile->name);
12665
12666 if (attr_form_is_section_offset (member_loc))
12667 dwarf2_complex_location_expr_complaint ();
12668 else if (attr_form_is_constant (member_loc)
12669 || attr_form_is_block (member_loc))
12670 {
12671 if (attr)
12672 mark_common_block_symbol_computed (sym, die, attr,
12673 member_loc, cu);
12674 }
12675 else
12676 dwarf2_complex_location_expr_complaint ();
12677 }
12678 }
c906108c 12679 }
4357ac6c
TT
12680
12681 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12682 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
12683 }
12684}
12685
0114d602 12686/* Create a type for a C++ namespace. */
d9fa45fe 12687
0114d602
DJ
12688static struct type *
12689read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 12690{
e7c27a73 12691 struct objfile *objfile = cu->objfile;
0114d602 12692 const char *previous_prefix, *name;
9219021c 12693 int is_anonymous;
0114d602
DJ
12694 struct type *type;
12695
12696 /* For extensions, reuse the type of the original namespace. */
12697 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12698 {
12699 struct die_info *ext_die;
12700 struct dwarf2_cu *ext_cu = cu;
9a619af0 12701
0114d602
DJ
12702 ext_die = dwarf2_extension (die, &ext_cu);
12703 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
12704
12705 /* EXT_CU may not be the same as CU.
02142a6c 12706 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
12707 return set_die_type (die, type, cu);
12708 }
9219021c 12709
e142c38c 12710 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
12711
12712 /* Now build the name of the current namespace. */
12713
0114d602
DJ
12714 previous_prefix = determine_prefix (die, cu);
12715 if (previous_prefix[0] != '\0')
12716 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 12717 previous_prefix, name, 0, cu);
0114d602
DJ
12718
12719 /* Create the type. */
12720 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12721 objfile);
abee88f2 12722 TYPE_NAME (type) = name;
0114d602
DJ
12723 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12724
60531b24 12725 return set_die_type (die, type, cu);
0114d602
DJ
12726}
12727
12728/* Read a C++ namespace. */
12729
12730static void
12731read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12732{
12733 struct objfile *objfile = cu->objfile;
0114d602 12734 int is_anonymous;
9219021c 12735
5c4e30ca
DC
12736 /* Add a symbol associated to this if we haven't seen the namespace
12737 before. Also, add a using directive if it's an anonymous
12738 namespace. */
9219021c 12739
f2f0e013 12740 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
12741 {
12742 struct type *type;
12743
0114d602 12744 type = read_type_die (die, cu);
e7c27a73 12745 new_symbol (die, type, cu);
5c4e30ca 12746
e8e80198 12747 namespace_name (die, &is_anonymous, cu);
5c4e30ca 12748 if (is_anonymous)
0114d602
DJ
12749 {
12750 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 12751
c0cc3a76 12752 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12aaed36 12753 NULL, NULL, 0, &objfile->objfile_obstack);
0114d602 12754 }
5c4e30ca 12755 }
9219021c 12756
639d11d3 12757 if (die->child != NULL)
d9fa45fe 12758 {
639d11d3 12759 struct die_info *child_die = die->child;
6e70227d 12760
d9fa45fe
DC
12761 while (child_die && child_die->tag)
12762 {
e7c27a73 12763 process_die (child_die, cu);
d9fa45fe
DC
12764 child_die = sibling_die (child_die);
12765 }
12766 }
38d518c9
EZ
12767}
12768
f55ee35c
JK
12769/* Read a Fortran module as type. This DIE can be only a declaration used for
12770 imported module. Still we need that type as local Fortran "use ... only"
12771 declaration imports depend on the created type in determine_prefix. */
12772
12773static struct type *
12774read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12775{
12776 struct objfile *objfile = cu->objfile;
15d034d0 12777 const char *module_name;
f55ee35c
JK
12778 struct type *type;
12779
12780 module_name = dwarf2_name (die, cu);
12781 if (!module_name)
3e43a32a
MS
12782 complaint (&symfile_complaints,
12783 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 12784 die->offset.sect_off);
f55ee35c
JK
12785 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12786
12787 /* determine_prefix uses TYPE_TAG_NAME. */
12788 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12789
12790 return set_die_type (die, type, cu);
12791}
12792
5d7cb8df
JK
12793/* Read a Fortran module. */
12794
12795static void
12796read_module (struct die_info *die, struct dwarf2_cu *cu)
12797{
12798 struct die_info *child_die = die->child;
12799
5d7cb8df
JK
12800 while (child_die && child_die->tag)
12801 {
12802 process_die (child_die, cu);
12803 child_die = sibling_die (child_die);
12804 }
12805}
12806
38d518c9
EZ
12807/* Return the name of the namespace represented by DIE. Set
12808 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12809 namespace. */
12810
12811static const char *
e142c38c 12812namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
12813{
12814 struct die_info *current_die;
12815 const char *name = NULL;
12816
12817 /* Loop through the extensions until we find a name. */
12818
12819 for (current_die = die;
12820 current_die != NULL;
f2f0e013 12821 current_die = dwarf2_extension (die, &cu))
38d518c9 12822 {
e142c38c 12823 name = dwarf2_name (current_die, cu);
38d518c9
EZ
12824 if (name != NULL)
12825 break;
12826 }
12827
12828 /* Is it an anonymous namespace? */
12829
12830 *is_anonymous = (name == NULL);
12831 if (*is_anonymous)
2b1dbab0 12832 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
12833
12834 return name;
d9fa45fe
DC
12835}
12836
c906108c
SS
12837/* Extract all information from a DW_TAG_pointer_type DIE and add to
12838 the user defined type vector. */
12839
f792889a 12840static struct type *
e7c27a73 12841read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12842{
5e2b427d 12843 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 12844 struct comp_unit_head *cu_header = &cu->header;
c906108c 12845 struct type *type;
8b2dbe47
KB
12846 struct attribute *attr_byte_size;
12847 struct attribute *attr_address_class;
12848 int byte_size, addr_class;
7e314c57
JK
12849 struct type *target_type;
12850
12851 target_type = die_type (die, cu);
c906108c 12852
7e314c57
JK
12853 /* The die_type call above may have already set the type for this DIE. */
12854 type = get_die_type (die, cu);
12855 if (type)
12856 return type;
12857
12858 type = lookup_pointer_type (target_type);
8b2dbe47 12859
e142c38c 12860 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
12861 if (attr_byte_size)
12862 byte_size = DW_UNSND (attr_byte_size);
c906108c 12863 else
8b2dbe47
KB
12864 byte_size = cu_header->addr_size;
12865
e142c38c 12866 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
12867 if (attr_address_class)
12868 addr_class = DW_UNSND (attr_address_class);
12869 else
12870 addr_class = DW_ADDR_none;
12871
12872 /* If the pointer size or address class is different than the
12873 default, create a type variant marked as such and set the
12874 length accordingly. */
12875 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 12876 {
5e2b427d 12877 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
12878 {
12879 int type_flags;
12880
849957d9 12881 type_flags = gdbarch_address_class_type_flags
5e2b427d 12882 (gdbarch, byte_size, addr_class);
876cecd0
TT
12883 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12884 == 0);
8b2dbe47
KB
12885 type = make_type_with_address_space (type, type_flags);
12886 }
12887 else if (TYPE_LENGTH (type) != byte_size)
12888 {
3e43a32a
MS
12889 complaint (&symfile_complaints,
12890 _("invalid pointer size %d"), byte_size);
8b2dbe47 12891 }
6e70227d 12892 else
9a619af0
MS
12893 {
12894 /* Should we also complain about unhandled address classes? */
12895 }
c906108c 12896 }
8b2dbe47
KB
12897
12898 TYPE_LENGTH (type) = byte_size;
f792889a 12899 return set_die_type (die, type, cu);
c906108c
SS
12900}
12901
12902/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12903 the user defined type vector. */
12904
f792889a 12905static struct type *
e7c27a73 12906read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
12907{
12908 struct type *type;
12909 struct type *to_type;
12910 struct type *domain;
12911
e7c27a73
DJ
12912 to_type = die_type (die, cu);
12913 domain = die_containing_type (die, cu);
0d5de010 12914
7e314c57
JK
12915 /* The calls above may have already set the type for this DIE. */
12916 type = get_die_type (die, cu);
12917 if (type)
12918 return type;
12919
0d5de010
DJ
12920 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12921 type = lookup_methodptr_type (to_type);
7078baeb
TT
12922 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12923 {
12924 struct type *new_type = alloc_type (cu->objfile);
12925
12926 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12927 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12928 TYPE_VARARGS (to_type));
12929 type = lookup_methodptr_type (new_type);
12930 }
0d5de010
DJ
12931 else
12932 type = lookup_memberptr_type (to_type, domain);
c906108c 12933
f792889a 12934 return set_die_type (die, type, cu);
c906108c
SS
12935}
12936
12937/* Extract all information from a DW_TAG_reference_type DIE and add to
12938 the user defined type vector. */
12939
f792889a 12940static struct type *
e7c27a73 12941read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12942{
e7c27a73 12943 struct comp_unit_head *cu_header = &cu->header;
7e314c57 12944 struct type *type, *target_type;
c906108c
SS
12945 struct attribute *attr;
12946
7e314c57
JK
12947 target_type = die_type (die, cu);
12948
12949 /* The die_type call above may have already set the type for this DIE. */
12950 type = get_die_type (die, cu);
12951 if (type)
12952 return type;
12953
12954 type = lookup_reference_type (target_type);
e142c38c 12955 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
12956 if (attr)
12957 {
12958 TYPE_LENGTH (type) = DW_UNSND (attr);
12959 }
12960 else
12961 {
107d2387 12962 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 12963 }
f792889a 12964 return set_die_type (die, type, cu);
c906108c
SS
12965}
12966
f792889a 12967static struct type *
e7c27a73 12968read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12969{
f792889a 12970 struct type *base_type, *cv_type;
c906108c 12971
e7c27a73 12972 base_type = die_type (die, cu);
7e314c57
JK
12973
12974 /* The die_type call above may have already set the type for this DIE. */
12975 cv_type = get_die_type (die, cu);
12976 if (cv_type)
12977 return cv_type;
12978
2f608a3a
KW
12979 /* In case the const qualifier is applied to an array type, the element type
12980 is so qualified, not the array type (section 6.7.3 of C99). */
12981 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12982 {
12983 struct type *el_type, *inner_array;
12984
12985 base_type = copy_type (base_type);
12986 inner_array = base_type;
12987
12988 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12989 {
12990 TYPE_TARGET_TYPE (inner_array) =
12991 copy_type (TYPE_TARGET_TYPE (inner_array));
12992 inner_array = TYPE_TARGET_TYPE (inner_array);
12993 }
12994
12995 el_type = TYPE_TARGET_TYPE (inner_array);
12996 TYPE_TARGET_TYPE (inner_array) =
12997 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12998
12999 return set_die_type (die, base_type, cu);
13000 }
13001
f792889a
DJ
13002 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13003 return set_die_type (die, cv_type, cu);
c906108c
SS
13004}
13005
f792889a 13006static struct type *
e7c27a73 13007read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13008{
f792889a 13009 struct type *base_type, *cv_type;
c906108c 13010
e7c27a73 13011 base_type = die_type (die, cu);
7e314c57
JK
13012
13013 /* The die_type call above may have already set the type for this DIE. */
13014 cv_type = get_die_type (die, cu);
13015 if (cv_type)
13016 return cv_type;
13017
f792889a
DJ
13018 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13019 return set_die_type (die, cv_type, cu);
c906108c
SS
13020}
13021
06d66ee9
TT
13022/* Handle DW_TAG_restrict_type. */
13023
13024static struct type *
13025read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13026{
13027 struct type *base_type, *cv_type;
13028
13029 base_type = die_type (die, cu);
13030
13031 /* The die_type call above may have already set the type for this DIE. */
13032 cv_type = get_die_type (die, cu);
13033 if (cv_type)
13034 return cv_type;
13035
13036 cv_type = make_restrict_type (base_type);
13037 return set_die_type (die, cv_type, cu);
13038}
13039
c906108c
SS
13040/* Extract all information from a DW_TAG_string_type DIE and add to
13041 the user defined type vector. It isn't really a user defined type,
13042 but it behaves like one, with other DIE's using an AT_user_def_type
13043 attribute to reference it. */
13044
f792889a 13045static struct type *
e7c27a73 13046read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13047{
e7c27a73 13048 struct objfile *objfile = cu->objfile;
3b7538c0 13049 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13050 struct type *type, *range_type, *index_type, *char_type;
13051 struct attribute *attr;
13052 unsigned int length;
13053
e142c38c 13054 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
13055 if (attr)
13056 {
13057 length = DW_UNSND (attr);
13058 }
13059 else
13060 {
0963b4bd 13061 /* Check for the DW_AT_byte_size attribute. */
e142c38c 13062 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
13063 if (attr)
13064 {
13065 length = DW_UNSND (attr);
13066 }
13067 else
13068 {
13069 length = 1;
13070 }
c906108c 13071 }
6ccb9162 13072
46bf5051 13073 index_type = objfile_type (objfile)->builtin_int;
c906108c 13074 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
13075 char_type = language_string_char_type (cu->language_defn, gdbarch);
13076 type = create_string_type (NULL, char_type, range_type);
6ccb9162 13077
f792889a 13078 return set_die_type (die, type, cu);
c906108c
SS
13079}
13080
4d804846
JB
13081/* Assuming that DIE corresponds to a function, returns nonzero
13082 if the function is prototyped. */
13083
13084static int
13085prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13086{
13087 struct attribute *attr;
13088
13089 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13090 if (attr && (DW_UNSND (attr) != 0))
13091 return 1;
13092
13093 /* The DWARF standard implies that the DW_AT_prototyped attribute
13094 is only meaninful for C, but the concept also extends to other
13095 languages that allow unprototyped functions (Eg: Objective C).
13096 For all other languages, assume that functions are always
13097 prototyped. */
13098 if (cu->language != language_c
13099 && cu->language != language_objc
13100 && cu->language != language_opencl)
13101 return 1;
13102
13103 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13104 prototyped and unprototyped functions; default to prototyped,
13105 since that is more common in modern code (and RealView warns
13106 about unprototyped functions). */
13107 if (producer_is_realview (cu->producer))
13108 return 1;
13109
13110 return 0;
13111}
13112
c906108c
SS
13113/* Handle DIES due to C code like:
13114
13115 struct foo
c5aa993b
JM
13116 {
13117 int (*funcp)(int a, long l);
13118 int b;
13119 };
c906108c 13120
0963b4bd 13121 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 13122
f792889a 13123static struct type *
e7c27a73 13124read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13125{
bb5ed363 13126 struct objfile *objfile = cu->objfile;
0963b4bd
MS
13127 struct type *type; /* Type that this function returns. */
13128 struct type *ftype; /* Function that returns above type. */
c906108c
SS
13129 struct attribute *attr;
13130
e7c27a73 13131 type = die_type (die, cu);
7e314c57
JK
13132
13133 /* The die_type call above may have already set the type for this DIE. */
13134 ftype = get_die_type (die, cu);
13135 if (ftype)
13136 return ftype;
13137
0c8b41f1 13138 ftype = lookup_function_type (type);
c906108c 13139
4d804846 13140 if (prototyped_function_p (die, cu))
a6c727b2 13141 TYPE_PROTOTYPED (ftype) = 1;
c906108c 13142
c055b101
CV
13143 /* Store the calling convention in the type if it's available in
13144 the subroutine die. Otherwise set the calling convention to
13145 the default value DW_CC_normal. */
13146 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
13147 if (attr)
13148 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13149 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13150 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13151 else
13152 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
13153
13154 /* We need to add the subroutine type to the die immediately so
13155 we don't infinitely recurse when dealing with parameters
0963b4bd 13156 declared as the same subroutine type. */
76c10ea2 13157 set_die_type (die, ftype, cu);
6e70227d 13158
639d11d3 13159 if (die->child != NULL)
c906108c 13160 {
bb5ed363 13161 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 13162 struct die_info *child_die;
8072405b 13163 int nparams, iparams;
c906108c
SS
13164
13165 /* Count the number of parameters.
13166 FIXME: GDB currently ignores vararg functions, but knows about
13167 vararg member functions. */
8072405b 13168 nparams = 0;
639d11d3 13169 child_die = die->child;
c906108c
SS
13170 while (child_die && child_die->tag)
13171 {
13172 if (child_die->tag == DW_TAG_formal_parameter)
13173 nparams++;
13174 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 13175 TYPE_VARARGS (ftype) = 1;
c906108c
SS
13176 child_die = sibling_die (child_die);
13177 }
13178
13179 /* Allocate storage for parameters and fill them in. */
13180 TYPE_NFIELDS (ftype) = nparams;
13181 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 13182 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 13183
8072405b
JK
13184 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13185 even if we error out during the parameters reading below. */
13186 for (iparams = 0; iparams < nparams; iparams++)
13187 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13188
13189 iparams = 0;
639d11d3 13190 child_die = die->child;
c906108c
SS
13191 while (child_die && child_die->tag)
13192 {
13193 if (child_die->tag == DW_TAG_formal_parameter)
13194 {
3ce3b1ba
PA
13195 struct type *arg_type;
13196
13197 /* DWARF version 2 has no clean way to discern C++
13198 static and non-static member functions. G++ helps
13199 GDB by marking the first parameter for non-static
13200 member functions (which is the this pointer) as
13201 artificial. We pass this information to
13202 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13203
13204 DWARF version 3 added DW_AT_object_pointer, which GCC
13205 4.5 does not yet generate. */
e142c38c 13206 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
13207 if (attr)
13208 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13209 else
418835cc
KS
13210 {
13211 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13212
13213 /* GCC/43521: In java, the formal parameter
13214 "this" is sometimes not marked with DW_AT_artificial. */
13215 if (cu->language == language_java)
13216 {
13217 const char *name = dwarf2_name (child_die, cu);
9a619af0 13218
418835cc
KS
13219 if (name && !strcmp (name, "this"))
13220 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13221 }
13222 }
3ce3b1ba
PA
13223 arg_type = die_type (child_die, cu);
13224
13225 /* RealView does not mark THIS as const, which the testsuite
13226 expects. GCC marks THIS as const in method definitions,
13227 but not in the class specifications (GCC PR 43053). */
13228 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13229 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13230 {
13231 int is_this = 0;
13232 struct dwarf2_cu *arg_cu = cu;
13233 const char *name = dwarf2_name (child_die, cu);
13234
13235 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13236 if (attr)
13237 {
13238 /* If the compiler emits this, use it. */
13239 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13240 is_this = 1;
13241 }
13242 else if (name && strcmp (name, "this") == 0)
13243 /* Function definitions will have the argument names. */
13244 is_this = 1;
13245 else if (name == NULL && iparams == 0)
13246 /* Declarations may not have the names, so like
13247 elsewhere in GDB, assume an artificial first
13248 argument is "this". */
13249 is_this = 1;
13250
13251 if (is_this)
13252 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13253 arg_type, 0);
13254 }
13255
13256 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
13257 iparams++;
13258 }
13259 child_die = sibling_die (child_die);
13260 }
13261 }
13262
76c10ea2 13263 return ftype;
c906108c
SS
13264}
13265
f792889a 13266static struct type *
e7c27a73 13267read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13268{
e7c27a73 13269 struct objfile *objfile = cu->objfile;
0114d602 13270 const char *name = NULL;
3c8e0968 13271 struct type *this_type, *target_type;
c906108c 13272
94af9270 13273 name = dwarf2_full_name (NULL, die, cu);
f792889a 13274 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602 13275 TYPE_FLAG_TARGET_STUB, NULL, objfile);
abee88f2 13276 TYPE_NAME (this_type) = name;
f792889a 13277 set_die_type (die, this_type, cu);
3c8e0968
DE
13278 target_type = die_type (die, cu);
13279 if (target_type != this_type)
13280 TYPE_TARGET_TYPE (this_type) = target_type;
13281 else
13282 {
13283 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13284 spec and cause infinite loops in GDB. */
13285 complaint (&symfile_complaints,
13286 _("Self-referential DW_TAG_typedef "
13287 "- DIE at 0x%x [in module %s]"),
b64f50a1 13288 die->offset.sect_off, objfile->name);
3c8e0968
DE
13289 TYPE_TARGET_TYPE (this_type) = NULL;
13290 }
f792889a 13291 return this_type;
c906108c
SS
13292}
13293
13294/* Find a representation of a given base type and install
13295 it in the TYPE field of the die. */
13296
f792889a 13297static struct type *
e7c27a73 13298read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13299{
e7c27a73 13300 struct objfile *objfile = cu->objfile;
c906108c
SS
13301 struct type *type;
13302 struct attribute *attr;
13303 int encoding = 0, size = 0;
15d034d0 13304 const char *name;
6ccb9162
UW
13305 enum type_code code = TYPE_CODE_INT;
13306 int type_flags = 0;
13307 struct type *target_type = NULL;
c906108c 13308
e142c38c 13309 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
13310 if (attr)
13311 {
13312 encoding = DW_UNSND (attr);
13313 }
e142c38c 13314 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
13315 if (attr)
13316 {
13317 size = DW_UNSND (attr);
13318 }
39cbfefa 13319 name = dwarf2_name (die, cu);
6ccb9162 13320 if (!name)
c906108c 13321 {
6ccb9162
UW
13322 complaint (&symfile_complaints,
13323 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 13324 }
6ccb9162
UW
13325
13326 switch (encoding)
c906108c 13327 {
6ccb9162
UW
13328 case DW_ATE_address:
13329 /* Turn DW_ATE_address into a void * pointer. */
13330 code = TYPE_CODE_PTR;
13331 type_flags |= TYPE_FLAG_UNSIGNED;
13332 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13333 break;
13334 case DW_ATE_boolean:
13335 code = TYPE_CODE_BOOL;
13336 type_flags |= TYPE_FLAG_UNSIGNED;
13337 break;
13338 case DW_ATE_complex_float:
13339 code = TYPE_CODE_COMPLEX;
13340 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13341 break;
13342 case DW_ATE_decimal_float:
13343 code = TYPE_CODE_DECFLOAT;
13344 break;
13345 case DW_ATE_float:
13346 code = TYPE_CODE_FLT;
13347 break;
13348 case DW_ATE_signed:
13349 break;
13350 case DW_ATE_unsigned:
13351 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
13352 if (cu->language == language_fortran
13353 && name
13354 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13355 code = TYPE_CODE_CHAR;
6ccb9162
UW
13356 break;
13357 case DW_ATE_signed_char:
6e70227d 13358 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13359 || cu->language == language_pascal
13360 || cu->language == language_fortran)
6ccb9162
UW
13361 code = TYPE_CODE_CHAR;
13362 break;
13363 case DW_ATE_unsigned_char:
868a0084 13364 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
13365 || cu->language == language_pascal
13366 || cu->language == language_fortran)
6ccb9162
UW
13367 code = TYPE_CODE_CHAR;
13368 type_flags |= TYPE_FLAG_UNSIGNED;
13369 break;
75079b2b
TT
13370 case DW_ATE_UTF:
13371 /* We just treat this as an integer and then recognize the
13372 type by name elsewhere. */
13373 break;
13374
6ccb9162
UW
13375 default:
13376 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13377 dwarf_type_encoding_name (encoding));
13378 break;
c906108c 13379 }
6ccb9162 13380
0114d602
DJ
13381 type = init_type (code, size, type_flags, NULL, objfile);
13382 TYPE_NAME (type) = name;
6ccb9162
UW
13383 TYPE_TARGET_TYPE (type) = target_type;
13384
0114d602 13385 if (name && strcmp (name, "char") == 0)
876cecd0 13386 TYPE_NOSIGN (type) = 1;
0114d602 13387
f792889a 13388 return set_die_type (die, type, cu);
c906108c
SS
13389}
13390
a02abb62
JB
13391/* Read the given DW_AT_subrange DIE. */
13392
f792889a 13393static struct type *
a02abb62
JB
13394read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13395{
4c9ad8c2 13396 struct type *base_type, *orig_base_type;
a02abb62
JB
13397 struct type *range_type;
13398 struct attribute *attr;
4fae6e18
JK
13399 LONGEST low, high;
13400 int low_default_is_valid;
15d034d0 13401 const char *name;
43bbcdc2 13402 LONGEST negative_mask;
e77813c8 13403
4c9ad8c2
TT
13404 orig_base_type = die_type (die, cu);
13405 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13406 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13407 creating the range type, but we use the result of check_typedef
13408 when examining properties of the type. */
13409 base_type = check_typedef (orig_base_type);
a02abb62 13410
7e314c57
JK
13411 /* The die_type call above may have already set the type for this DIE. */
13412 range_type = get_die_type (die, cu);
13413 if (range_type)
13414 return range_type;
13415
4fae6e18
JK
13416 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13417 omitting DW_AT_lower_bound. */
13418 switch (cu->language)
6e70227d 13419 {
4fae6e18
JK
13420 case language_c:
13421 case language_cplus:
13422 low = 0;
13423 low_default_is_valid = 1;
13424 break;
13425 case language_fortran:
13426 low = 1;
13427 low_default_is_valid = 1;
13428 break;
13429 case language_d:
13430 case language_java:
13431 case language_objc:
13432 low = 0;
13433 low_default_is_valid = (cu->header.version >= 4);
13434 break;
13435 case language_ada:
13436 case language_m2:
13437 case language_pascal:
a02abb62 13438 low = 1;
4fae6e18
JK
13439 low_default_is_valid = (cu->header.version >= 4);
13440 break;
13441 default:
13442 low = 0;
13443 low_default_is_valid = 0;
13444 break;
a02abb62
JB
13445 }
13446
dd5e6932
DJ
13447 /* FIXME: For variable sized arrays either of these could be
13448 a variable rather than a constant value. We'll allow it,
13449 but we don't know how to handle it. */
e142c38c 13450 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 13451 if (attr)
4fae6e18
JK
13452 low = dwarf2_get_attr_constant_value (attr, low);
13453 else if (!low_default_is_valid)
13454 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13455 "- DIE at 0x%x [in module %s]"),
13456 die->offset.sect_off, cu->objfile->name);
a02abb62 13457
e142c38c 13458 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 13459 if (attr)
6e70227d 13460 {
7771576e 13461 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
a02abb62
JB
13462 {
13463 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 13464 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
13465 FIXME: GDB does not yet know how to handle dynamic
13466 arrays properly, treat them as arrays with unspecified
13467 length for now.
13468
13469 FIXME: jimb/2003-09-22: GDB does not really know
13470 how to handle arrays of unspecified length
13471 either; we just represent them as zero-length
13472 arrays. Choose an appropriate upper bound given
13473 the lower bound we've computed above. */
13474 high = low - 1;
13475 }
13476 else
13477 high = dwarf2_get_attr_constant_value (attr, 1);
13478 }
e77813c8
PM
13479 else
13480 {
13481 attr = dwarf2_attr (die, DW_AT_count, cu);
13482 if (attr)
13483 {
13484 int count = dwarf2_get_attr_constant_value (attr, 1);
13485 high = low + count - 1;
13486 }
c2ff108b
JK
13487 else
13488 {
13489 /* Unspecified array length. */
13490 high = low - 1;
13491 }
e77813c8
PM
13492 }
13493
13494 /* Dwarf-2 specifications explicitly allows to create subrange types
13495 without specifying a base type.
13496 In that case, the base type must be set to the type of
13497 the lower bound, upper bound or count, in that order, if any of these
13498 three attributes references an object that has a type.
13499 If no base type is found, the Dwarf-2 specifications say that
13500 a signed integer type of size equal to the size of an address should
13501 be used.
13502 For the following C code: `extern char gdb_int [];'
13503 GCC produces an empty range DIE.
13504 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 13505 high bound or count are not yet handled by this code. */
e77813c8
PM
13506 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13507 {
13508 struct objfile *objfile = cu->objfile;
13509 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13510 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13511 struct type *int_type = objfile_type (objfile)->builtin_int;
13512
13513 /* Test "int", "long int", and "long long int" objfile types,
13514 and select the first one having a size above or equal to the
13515 architecture address size. */
13516 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13517 base_type = int_type;
13518 else
13519 {
13520 int_type = objfile_type (objfile)->builtin_long;
13521 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13522 base_type = int_type;
13523 else
13524 {
13525 int_type = objfile_type (objfile)->builtin_long_long;
13526 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13527 base_type = int_type;
13528 }
13529 }
13530 }
a02abb62 13531
6e70227d 13532 negative_mask =
43bbcdc2
PH
13533 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13534 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13535 low |= negative_mask;
13536 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13537 high |= negative_mask;
13538
4c9ad8c2 13539 range_type = create_range_type (NULL, orig_base_type, low, high);
a02abb62 13540
bbb0eef6
JK
13541 /* Mark arrays with dynamic length at least as an array of unspecified
13542 length. GDB could check the boundary but before it gets implemented at
13543 least allow accessing the array elements. */
d48323d8 13544 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
13545 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13546
c2ff108b
JK
13547 /* Ada expects an empty array on no boundary attributes. */
13548 if (attr == NULL && cu->language != language_ada)
13549 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13550
39cbfefa
DJ
13551 name = dwarf2_name (die, cu);
13552 if (name)
13553 TYPE_NAME (range_type) = name;
6e70227d 13554
e142c38c 13555 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
13556 if (attr)
13557 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13558
7e314c57
JK
13559 set_die_type (die, range_type, cu);
13560
13561 /* set_die_type should be already done. */
b4ba55a1
JB
13562 set_descriptive_type (range_type, die, cu);
13563
7e314c57 13564 return range_type;
a02abb62 13565}
6e70227d 13566
f792889a 13567static struct type *
81a17f79
JB
13568read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13569{
13570 struct type *type;
81a17f79 13571
81a17f79
JB
13572 /* For now, we only support the C meaning of an unspecified type: void. */
13573
0114d602
DJ
13574 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13575 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 13576
f792889a 13577 return set_die_type (die, type, cu);
81a17f79 13578}
a02abb62 13579
639d11d3
DC
13580/* Read a single die and all its descendents. Set the die's sibling
13581 field to NULL; set other fields in the die correctly, and set all
13582 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13583 location of the info_ptr after reading all of those dies. PARENT
13584 is the parent of the die in question. */
13585
13586static struct die_info *
dee91e82 13587read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
13588 const gdb_byte *info_ptr,
13589 const gdb_byte **new_info_ptr,
dee91e82 13590 struct die_info *parent)
639d11d3
DC
13591{
13592 struct die_info *die;
d521ce57 13593 const gdb_byte *cur_ptr;
639d11d3
DC
13594 int has_children;
13595
bf6af496 13596 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
13597 if (die == NULL)
13598 {
13599 *new_info_ptr = cur_ptr;
13600 return NULL;
13601 }
93311388 13602 store_in_ref_table (die, reader->cu);
639d11d3
DC
13603
13604 if (has_children)
bf6af496 13605 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
13606 else
13607 {
13608 die->child = NULL;
13609 *new_info_ptr = cur_ptr;
13610 }
13611
13612 die->sibling = NULL;
13613 die->parent = parent;
13614 return die;
13615}
13616
13617/* Read a die, all of its descendents, and all of its siblings; set
13618 all of the fields of all of the dies correctly. Arguments are as
13619 in read_die_and_children. */
13620
13621static struct die_info *
bf6af496 13622read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
13623 const gdb_byte *info_ptr,
13624 const gdb_byte **new_info_ptr,
bf6af496 13625 struct die_info *parent)
639d11d3
DC
13626{
13627 struct die_info *first_die, *last_sibling;
d521ce57 13628 const gdb_byte *cur_ptr;
639d11d3 13629
c906108c 13630 cur_ptr = info_ptr;
639d11d3
DC
13631 first_die = last_sibling = NULL;
13632
13633 while (1)
c906108c 13634 {
639d11d3 13635 struct die_info *die
dee91e82 13636 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 13637
1d325ec1 13638 if (die == NULL)
c906108c 13639 {
639d11d3
DC
13640 *new_info_ptr = cur_ptr;
13641 return first_die;
c906108c 13642 }
1d325ec1
DJ
13643
13644 if (!first_die)
13645 first_die = die;
c906108c 13646 else
1d325ec1
DJ
13647 last_sibling->sibling = die;
13648
13649 last_sibling = die;
c906108c 13650 }
c906108c
SS
13651}
13652
bf6af496
DE
13653/* Read a die, all of its descendents, and all of its siblings; set
13654 all of the fields of all of the dies correctly. Arguments are as
13655 in read_die_and_children.
13656 This the main entry point for reading a DIE and all its children. */
13657
13658static struct die_info *
13659read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
13660 const gdb_byte *info_ptr,
13661 const gdb_byte **new_info_ptr,
bf6af496
DE
13662 struct die_info *parent)
13663{
13664 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13665 new_info_ptr, parent);
13666
13667 if (dwarf2_die_debug)
13668 {
13669 fprintf_unfiltered (gdb_stdlog,
13670 "Read die from %s@0x%x of %s:\n",
13671 bfd_section_name (reader->abfd,
13672 reader->die_section->asection),
13673 (unsigned) (info_ptr - reader->die_section->buffer),
13674 bfd_get_filename (reader->abfd));
13675 dump_die (die, dwarf2_die_debug);
13676 }
13677
13678 return die;
13679}
13680
3019eac3
DE
13681/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13682 attributes.
13683 The caller is responsible for filling in the extra attributes
13684 and updating (*DIEP)->num_attrs.
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. */
93311388 13688
d521ce57 13689static const gdb_byte *
3019eac3 13690read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 13691 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 13692 int *has_children, int num_extra_attrs)
93311388 13693{
b64f50a1
JK
13694 unsigned int abbrev_number, bytes_read, i;
13695 sect_offset offset;
93311388
DE
13696 struct abbrev_info *abbrev;
13697 struct die_info *die;
13698 struct dwarf2_cu *cu = reader->cu;
13699 bfd *abfd = reader->abfd;
13700
b64f50a1 13701 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
13702 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13703 info_ptr += bytes_read;
13704 if (!abbrev_number)
13705 {
13706 *diep = NULL;
13707 *has_children = 0;
13708 return info_ptr;
13709 }
13710
433df2d4 13711 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 13712 if (!abbrev)
348e048f
DE
13713 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13714 abbrev_number,
13715 bfd_get_filename (abfd));
13716
3019eac3 13717 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
13718 die->offset = offset;
13719 die->tag = abbrev->tag;
13720 die->abbrev = abbrev_number;
13721
3019eac3
DE
13722 /* Make the result usable.
13723 The caller needs to update num_attrs after adding the extra
13724 attributes. */
93311388
DE
13725 die->num_attrs = abbrev->num_attrs;
13726
13727 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
13728 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13729 info_ptr);
93311388
DE
13730
13731 *diep = die;
13732 *has_children = abbrev->has_children;
13733 return info_ptr;
13734}
13735
3019eac3
DE
13736/* Read a die and all its attributes.
13737 Set DIEP to point to a newly allocated die with its information,
13738 except for its child, sibling, and parent fields.
13739 Set HAS_CHILDREN to tell whether the die has children or not. */
13740
d521ce57 13741static const gdb_byte *
3019eac3 13742read_full_die (const struct die_reader_specs *reader,
d521ce57 13743 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
13744 int *has_children)
13745{
d521ce57 13746 const gdb_byte *result;
bf6af496
DE
13747
13748 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13749
13750 if (dwarf2_die_debug)
13751 {
13752 fprintf_unfiltered (gdb_stdlog,
13753 "Read die from %s@0x%x of %s:\n",
13754 bfd_section_name (reader->abfd,
13755 reader->die_section->asection),
13756 (unsigned) (info_ptr - reader->die_section->buffer),
13757 bfd_get_filename (reader->abfd));
13758 dump_die (*diep, dwarf2_die_debug);
13759 }
13760
13761 return result;
3019eac3 13762}
433df2d4
DE
13763\f
13764/* Abbreviation tables.
3019eac3 13765
433df2d4 13766 In DWARF version 2, the description of the debugging information is
c906108c
SS
13767 stored in a separate .debug_abbrev section. Before we read any
13768 dies from a section we read in all abbreviations and install them
433df2d4
DE
13769 in a hash table. */
13770
13771/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13772
13773static struct abbrev_info *
13774abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13775{
13776 struct abbrev_info *abbrev;
13777
13778 abbrev = (struct abbrev_info *)
13779 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13780 memset (abbrev, 0, sizeof (struct abbrev_info));
13781 return abbrev;
13782}
13783
13784/* Add an abbreviation to the table. */
c906108c
SS
13785
13786static void
433df2d4
DE
13787abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13788 unsigned int abbrev_number,
13789 struct abbrev_info *abbrev)
13790{
13791 unsigned int hash_number;
13792
13793 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13794 abbrev->next = abbrev_table->abbrevs[hash_number];
13795 abbrev_table->abbrevs[hash_number] = abbrev;
13796}
dee91e82 13797
433df2d4
DE
13798/* Look up an abbrev in the table.
13799 Returns NULL if the abbrev is not found. */
13800
13801static struct abbrev_info *
13802abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13803 unsigned int abbrev_number)
c906108c 13804{
433df2d4
DE
13805 unsigned int hash_number;
13806 struct abbrev_info *abbrev;
13807
13808 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13809 abbrev = abbrev_table->abbrevs[hash_number];
13810
13811 while (abbrev)
13812 {
13813 if (abbrev->number == abbrev_number)
13814 return abbrev;
13815 abbrev = abbrev->next;
13816 }
13817 return NULL;
13818}
13819
13820/* Read in an abbrev table. */
13821
13822static struct abbrev_table *
13823abbrev_table_read_table (struct dwarf2_section_info *section,
13824 sect_offset offset)
13825{
13826 struct objfile *objfile = dwarf2_per_objfile->objfile;
13827 bfd *abfd = section->asection->owner;
13828 struct abbrev_table *abbrev_table;
d521ce57 13829 const gdb_byte *abbrev_ptr;
c906108c
SS
13830 struct abbrev_info *cur_abbrev;
13831 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 13832 unsigned int abbrev_form;
f3dd6933
DJ
13833 struct attr_abbrev *cur_attrs;
13834 unsigned int allocated_attrs;
c906108c 13835
433df2d4 13836 abbrev_table = XMALLOC (struct abbrev_table);
f4dc4d17 13837 abbrev_table->offset = offset;
433df2d4
DE
13838 obstack_init (&abbrev_table->abbrev_obstack);
13839 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13840 (ABBREV_HASH_SIZE
13841 * sizeof (struct abbrev_info *)));
13842 memset (abbrev_table->abbrevs, 0,
13843 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 13844
433df2d4
DE
13845 dwarf2_read_section (objfile, section);
13846 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
13847 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13848 abbrev_ptr += bytes_read;
13849
f3dd6933
DJ
13850 allocated_attrs = ATTR_ALLOC_CHUNK;
13851 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 13852
0963b4bd 13853 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
13854 while (abbrev_number)
13855 {
433df2d4 13856 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
13857
13858 /* read in abbrev header */
13859 cur_abbrev->number = abbrev_number;
13860 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13861 abbrev_ptr += bytes_read;
13862 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13863 abbrev_ptr += 1;
13864
13865 /* now read in declarations */
13866 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13867 abbrev_ptr += bytes_read;
13868 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13869 abbrev_ptr += bytes_read;
13870 while (abbrev_name)
13871 {
f3dd6933 13872 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 13873 {
f3dd6933
DJ
13874 allocated_attrs += ATTR_ALLOC_CHUNK;
13875 cur_attrs
13876 = xrealloc (cur_attrs, (allocated_attrs
13877 * sizeof (struct attr_abbrev)));
c906108c 13878 }
ae038cb0 13879
f3dd6933
DJ
13880 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13881 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
13882 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13883 abbrev_ptr += bytes_read;
13884 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13885 abbrev_ptr += bytes_read;
13886 }
13887
433df2d4 13888 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
13889 (cur_abbrev->num_attrs
13890 * sizeof (struct attr_abbrev)));
13891 memcpy (cur_abbrev->attrs, cur_attrs,
13892 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13893
433df2d4 13894 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
13895
13896 /* Get next abbreviation.
13897 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
13898 always properly terminated with an abbrev number of 0.
13899 Exit loop if we encounter an abbreviation which we have
13900 already read (which means we are about to read the abbreviations
13901 for the next compile unit) or if the end of the abbreviation
13902 table is reached. */
433df2d4 13903 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
13904 break;
13905 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13906 abbrev_ptr += bytes_read;
433df2d4 13907 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
13908 break;
13909 }
f3dd6933
DJ
13910
13911 xfree (cur_attrs);
433df2d4 13912 return abbrev_table;
c906108c
SS
13913}
13914
433df2d4 13915/* Free the resources held by ABBREV_TABLE. */
c906108c 13916
c906108c 13917static void
433df2d4 13918abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 13919{
433df2d4
DE
13920 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13921 xfree (abbrev_table);
c906108c
SS
13922}
13923
f4dc4d17
DE
13924/* Same as abbrev_table_free but as a cleanup.
13925 We pass in a pointer to the pointer to the table so that we can
13926 set the pointer to NULL when we're done. It also simplifies
13927 build_type_unit_groups. */
13928
13929static void
13930abbrev_table_free_cleanup (void *table_ptr)
13931{
13932 struct abbrev_table **abbrev_table_ptr = table_ptr;
13933
13934 if (*abbrev_table_ptr != NULL)
13935 abbrev_table_free (*abbrev_table_ptr);
13936 *abbrev_table_ptr = NULL;
13937}
13938
433df2d4
DE
13939/* Read the abbrev table for CU from ABBREV_SECTION. */
13940
13941static void
13942dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13943 struct dwarf2_section_info *abbrev_section)
c906108c 13944{
433df2d4
DE
13945 cu->abbrev_table =
13946 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13947}
c906108c 13948
433df2d4 13949/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 13950
433df2d4
DE
13951static void
13952dwarf2_free_abbrev_table (void *ptr_to_cu)
13953{
13954 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 13955
a2ce51a0
DE
13956 if (cu->abbrev_table != NULL)
13957 abbrev_table_free (cu->abbrev_table);
433df2d4
DE
13958 /* Set this to NULL so that we SEGV if we try to read it later,
13959 and also because free_comp_unit verifies this is NULL. */
13960 cu->abbrev_table = NULL;
13961}
13962\f
72bf9492
DJ
13963/* Returns nonzero if TAG represents a type that we might generate a partial
13964 symbol for. */
13965
13966static int
13967is_type_tag_for_partial (int tag)
13968{
13969 switch (tag)
13970 {
13971#if 0
13972 /* Some types that would be reasonable to generate partial symbols for,
13973 that we don't at present. */
13974 case DW_TAG_array_type:
13975 case DW_TAG_file_type:
13976 case DW_TAG_ptr_to_member_type:
13977 case DW_TAG_set_type:
13978 case DW_TAG_string_type:
13979 case DW_TAG_subroutine_type:
13980#endif
13981 case DW_TAG_base_type:
13982 case DW_TAG_class_type:
680b30c7 13983 case DW_TAG_interface_type:
72bf9492
DJ
13984 case DW_TAG_enumeration_type:
13985 case DW_TAG_structure_type:
13986 case DW_TAG_subrange_type:
13987 case DW_TAG_typedef:
13988 case DW_TAG_union_type:
13989 return 1;
13990 default:
13991 return 0;
13992 }
13993}
13994
13995/* Load all DIEs that are interesting for partial symbols into memory. */
13996
13997static struct partial_die_info *
dee91e82 13998load_partial_dies (const struct die_reader_specs *reader,
d521ce57 13999 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 14000{
dee91e82 14001 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14002 struct objfile *objfile = cu->objfile;
72bf9492
DJ
14003 struct partial_die_info *part_die;
14004 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14005 struct abbrev_info *abbrev;
14006 unsigned int bytes_read;
5afb4e99 14007 unsigned int load_all = 0;
72bf9492
DJ
14008 int nesting_level = 1;
14009
14010 parent_die = NULL;
14011 last_die = NULL;
14012
7adf1e79
DE
14013 gdb_assert (cu->per_cu != NULL);
14014 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
14015 load_all = 1;
14016
72bf9492
DJ
14017 cu->partial_dies
14018 = htab_create_alloc_ex (cu->header.length / 12,
14019 partial_die_hash,
14020 partial_die_eq,
14021 NULL,
14022 &cu->comp_unit_obstack,
14023 hashtab_obstack_allocate,
14024 dummy_obstack_deallocate);
14025
14026 part_die = obstack_alloc (&cu->comp_unit_obstack,
14027 sizeof (struct partial_die_info));
14028
14029 while (1)
14030 {
14031 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14032
14033 /* A NULL abbrev means the end of a series of children. */
14034 if (abbrev == NULL)
14035 {
14036 if (--nesting_level == 0)
14037 {
14038 /* PART_DIE was probably the last thing allocated on the
14039 comp_unit_obstack, so we could call obstack_free
14040 here. We don't do that because the waste is small,
14041 and will be cleaned up when we're done with this
14042 compilation unit. This way, we're also more robust
14043 against other users of the comp_unit_obstack. */
14044 return first_die;
14045 }
14046 info_ptr += bytes_read;
14047 last_die = parent_die;
14048 parent_die = parent_die->die_parent;
14049 continue;
14050 }
14051
98bfdba5
PA
14052 /* Check for template arguments. We never save these; if
14053 they're seen, we just mark the parent, and go on our way. */
14054 if (parent_die != NULL
14055 && cu->language == language_cplus
14056 && (abbrev->tag == DW_TAG_template_type_param
14057 || abbrev->tag == DW_TAG_template_value_param))
14058 {
14059 parent_die->has_template_arguments = 1;
14060
14061 if (!load_all)
14062 {
14063 /* We don't need a partial DIE for the template argument. */
dee91e82 14064 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14065 continue;
14066 }
14067 }
14068
0d99eb77 14069 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
14070 Skip their other children. */
14071 if (!load_all
14072 && cu->language == language_cplus
14073 && parent_die != NULL
14074 && parent_die->tag == DW_TAG_subprogram)
14075 {
dee91e82 14076 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
14077 continue;
14078 }
14079
5afb4e99
DJ
14080 /* Check whether this DIE is interesting enough to save. Normally
14081 we would not be interested in members here, but there may be
14082 later variables referencing them via DW_AT_specification (for
14083 static members). */
14084 if (!load_all
14085 && !is_type_tag_for_partial (abbrev->tag)
72929c62 14086 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
14087 && abbrev->tag != DW_TAG_enumerator
14088 && abbrev->tag != DW_TAG_subprogram
bc30ff58 14089 && abbrev->tag != DW_TAG_lexical_block
72bf9492 14090 && abbrev->tag != DW_TAG_variable
5afb4e99 14091 && abbrev->tag != DW_TAG_namespace
f55ee35c 14092 && abbrev->tag != DW_TAG_module
95554aad
TT
14093 && abbrev->tag != DW_TAG_member
14094 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
14095 {
14096 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14097 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
14098 continue;
14099 }
14100
dee91e82
DE
14101 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14102 info_ptr);
72bf9492
DJ
14103
14104 /* This two-pass algorithm for processing partial symbols has a
14105 high cost in cache pressure. Thus, handle some simple cases
14106 here which cover the majority of C partial symbols. DIEs
14107 which neither have specification tags in them, nor could have
14108 specification tags elsewhere pointing at them, can simply be
14109 processed and discarded.
14110
14111 This segment is also optional; scan_partial_symbols and
14112 add_partial_symbol will handle these DIEs if we chain
14113 them in normally. When compilers which do not emit large
14114 quantities of duplicate debug information are more common,
14115 this code can probably be removed. */
14116
14117 /* Any complete simple types at the top level (pretty much all
14118 of them, for a language without namespaces), can be processed
14119 directly. */
14120 if (parent_die == NULL
14121 && part_die->has_specification == 0
14122 && part_die->is_declaration == 0
d8228535 14123 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
14124 || part_die->tag == DW_TAG_base_type
14125 || part_die->tag == DW_TAG_subrange_type))
14126 {
14127 if (building_psymtab && part_die->name != NULL)
04a679b8 14128 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14129 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
14130 &objfile->static_psymbols,
14131 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 14132 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14133 continue;
14134 }
14135
d8228535
JK
14136 /* The exception for DW_TAG_typedef with has_children above is
14137 a workaround of GCC PR debug/47510. In the case of this complaint
14138 type_name_no_tag_or_error will error on such types later.
14139
14140 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14141 it could not find the child DIEs referenced later, this is checked
14142 above. In correct DWARF DW_TAG_typedef should have no children. */
14143
14144 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14145 complaint (&symfile_complaints,
14146 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14147 "- DIE at 0x%x [in module %s]"),
b64f50a1 14148 part_die->offset.sect_off, objfile->name);
d8228535 14149
72bf9492
DJ
14150 /* If we're at the second level, and we're an enumerator, and
14151 our parent has no specification (meaning possibly lives in a
14152 namespace elsewhere), then we can add the partial symbol now
14153 instead of queueing it. */
14154 if (part_die->tag == DW_TAG_enumerator
14155 && parent_die != NULL
14156 && parent_die->die_parent == NULL
14157 && parent_die->tag == DW_TAG_enumeration_type
14158 && parent_die->has_specification == 0)
14159 {
14160 if (part_die->name == NULL)
3e43a32a
MS
14161 complaint (&symfile_complaints,
14162 _("malformed enumerator DIE ignored"));
72bf9492 14163 else if (building_psymtab)
04a679b8 14164 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 14165 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
14166 (cu->language == language_cplus
14167 || cu->language == language_java)
bb5ed363
DE
14168 ? &objfile->global_psymbols
14169 : &objfile->static_psymbols,
14170 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 14171
dee91e82 14172 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
14173 continue;
14174 }
14175
14176 /* We'll save this DIE so link it in. */
14177 part_die->die_parent = parent_die;
14178 part_die->die_sibling = NULL;
14179 part_die->die_child = NULL;
14180
14181 if (last_die && last_die == parent_die)
14182 last_die->die_child = part_die;
14183 else if (last_die)
14184 last_die->die_sibling = part_die;
14185
14186 last_die = part_die;
14187
14188 if (first_die == NULL)
14189 first_die = part_die;
14190
14191 /* Maybe add the DIE to the hash table. Not all DIEs that we
14192 find interesting need to be in the hash table, because we
14193 also have the parent/sibling/child chains; only those that we
14194 might refer to by offset later during partial symbol reading.
14195
14196 For now this means things that might have be the target of a
14197 DW_AT_specification, DW_AT_abstract_origin, or
14198 DW_AT_extension. DW_AT_extension will refer only to
14199 namespaces; DW_AT_abstract_origin refers to functions (and
14200 many things under the function DIE, but we do not recurse
14201 into function DIEs during partial symbol reading) and
14202 possibly variables as well; DW_AT_specification refers to
14203 declarations. Declarations ought to have the DW_AT_declaration
14204 flag. It happens that GCC forgets to put it in sometimes, but
14205 only for functions, not for types.
14206
14207 Adding more things than necessary to the hash table is harmless
14208 except for the performance cost. Adding too few will result in
5afb4e99
DJ
14209 wasted time in find_partial_die, when we reread the compilation
14210 unit with load_all_dies set. */
72bf9492 14211
5afb4e99 14212 if (load_all
72929c62 14213 || abbrev->tag == DW_TAG_constant
5afb4e99 14214 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
14215 || abbrev->tag == DW_TAG_variable
14216 || abbrev->tag == DW_TAG_namespace
14217 || part_die->is_declaration)
14218 {
14219 void **slot;
14220
14221 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 14222 part_die->offset.sect_off, INSERT);
72bf9492
DJ
14223 *slot = part_die;
14224 }
14225
14226 part_die = obstack_alloc (&cu->comp_unit_obstack,
14227 sizeof (struct partial_die_info));
14228
14229 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 14230 we have no reason to follow the children of structures; for other
98bfdba5
PA
14231 languages we have to, so that we can get at method physnames
14232 to infer fully qualified class names, for DW_AT_specification,
14233 and for C++ template arguments. For C++, we also look one level
14234 inside functions to find template arguments (if the name of the
14235 function does not already contain the template arguments).
bc30ff58
JB
14236
14237 For Ada, we need to scan the children of subprograms and lexical
14238 blocks as well because Ada allows the definition of nested
14239 entities that could be interesting for the debugger, such as
14240 nested subprograms for instance. */
72bf9492 14241 if (last_die->has_children
5afb4e99
DJ
14242 && (load_all
14243 || last_die->tag == DW_TAG_namespace
f55ee35c 14244 || last_die->tag == DW_TAG_module
72bf9492 14245 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
14246 || (cu->language == language_cplus
14247 && last_die->tag == DW_TAG_subprogram
14248 && (last_die->name == NULL
14249 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
14250 || (cu->language != language_c
14251 && (last_die->tag == DW_TAG_class_type
680b30c7 14252 || last_die->tag == DW_TAG_interface_type
72bf9492 14253 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
14254 || last_die->tag == DW_TAG_union_type))
14255 || (cu->language == language_ada
14256 && (last_die->tag == DW_TAG_subprogram
14257 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
14258 {
14259 nesting_level++;
14260 parent_die = last_die;
14261 continue;
14262 }
14263
14264 /* Otherwise we skip to the next sibling, if any. */
dee91e82 14265 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
14266
14267 /* Back to the top, do it again. */
14268 }
14269}
14270
c906108c
SS
14271/* Read a minimal amount of information into the minimal die structure. */
14272
d521ce57 14273static const gdb_byte *
dee91e82
DE
14274read_partial_die (const struct die_reader_specs *reader,
14275 struct partial_die_info *part_die,
14276 struct abbrev_info *abbrev, unsigned int abbrev_len,
d521ce57 14277 const gdb_byte *info_ptr)
c906108c 14278{
dee91e82 14279 struct dwarf2_cu *cu = reader->cu;
bb5ed363 14280 struct objfile *objfile = cu->objfile;
d521ce57 14281 const gdb_byte *buffer = reader->buffer;
fa238c03 14282 unsigned int i;
c906108c 14283 struct attribute attr;
c5aa993b 14284 int has_low_pc_attr = 0;
c906108c 14285 int has_high_pc_attr = 0;
91da1414 14286 int high_pc_relative = 0;
c906108c 14287
72bf9492 14288 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 14289
b64f50a1 14290 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
14291
14292 info_ptr += abbrev_len;
14293
14294 if (abbrev == NULL)
14295 return info_ptr;
14296
c906108c
SS
14297 part_die->tag = abbrev->tag;
14298 part_die->has_children = abbrev->has_children;
c906108c
SS
14299
14300 for (i = 0; i < abbrev->num_attrs; ++i)
14301 {
dee91e82 14302 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
14303
14304 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 14305 partial symbol table. */
c906108c
SS
14306 switch (attr.name)
14307 {
14308 case DW_AT_name:
71c25dea
TT
14309 switch (part_die->tag)
14310 {
14311 case DW_TAG_compile_unit:
95554aad 14312 case DW_TAG_partial_unit:
348e048f 14313 case DW_TAG_type_unit:
71c25dea
TT
14314 /* Compilation units have a DW_AT_name that is a filename, not
14315 a source language identifier. */
14316 case DW_TAG_enumeration_type:
14317 case DW_TAG_enumerator:
14318 /* These tags always have simple identifiers already; no need
14319 to canonicalize them. */
14320 part_die->name = DW_STRING (&attr);
14321 break;
14322 default:
14323 part_die->name
14324 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 14325 &objfile->objfile_obstack);
71c25dea
TT
14326 break;
14327 }
c906108c 14328 break;
31ef98ae 14329 case DW_AT_linkage_name:
c906108c 14330 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
14331 /* Note that both forms of linkage name might appear. We
14332 assume they will be the same, and we only store the last
14333 one we see. */
94af9270
KS
14334 if (cu->language == language_ada)
14335 part_die->name = DW_STRING (&attr);
abc72ce4 14336 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
14337 break;
14338 case DW_AT_low_pc:
14339 has_low_pc_attr = 1;
14340 part_die->lowpc = DW_ADDR (&attr);
14341 break;
14342 case DW_AT_high_pc:
14343 has_high_pc_attr = 1;
3019eac3
DE
14344 if (attr.form == DW_FORM_addr
14345 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
14346 part_die->highpc = DW_ADDR (&attr);
14347 else
14348 {
14349 high_pc_relative = 1;
14350 part_die->highpc = DW_UNSND (&attr);
14351 }
c906108c
SS
14352 break;
14353 case DW_AT_location:
0963b4bd 14354 /* Support the .debug_loc offsets. */
8e19ed76
PS
14355 if (attr_form_is_block (&attr))
14356 {
95554aad 14357 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 14358 }
3690dd37 14359 else if (attr_form_is_section_offset (&attr))
8e19ed76 14360 {
4d3c2250 14361 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14362 }
14363 else
14364 {
4d3c2250
KB
14365 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14366 "partial symbol information");
8e19ed76 14367 }
c906108c 14368 break;
c906108c
SS
14369 case DW_AT_external:
14370 part_die->is_external = DW_UNSND (&attr);
14371 break;
14372 case DW_AT_declaration:
14373 part_die->is_declaration = DW_UNSND (&attr);
14374 break;
14375 case DW_AT_type:
14376 part_die->has_type = 1;
14377 break;
14378 case DW_AT_abstract_origin:
14379 case DW_AT_specification:
72bf9492
DJ
14380 case DW_AT_extension:
14381 part_die->has_specification = 1;
c764a876 14382 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
36586728
TT
14383 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14384 || cu->per_cu->is_dwz);
c906108c
SS
14385 break;
14386 case DW_AT_sibling:
14387 /* Ignore absolute siblings, they might point outside of
14388 the current compile unit. */
14389 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
14390 complaint (&symfile_complaints,
14391 _("ignoring absolute DW_AT_sibling"));
c906108c 14392 else
b64f50a1 14393 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 14394 break;
fa4028e9
JB
14395 case DW_AT_byte_size:
14396 part_die->has_byte_size = 1;
14397 break;
68511cec
CES
14398 case DW_AT_calling_convention:
14399 /* DWARF doesn't provide a way to identify a program's source-level
14400 entry point. DW_AT_calling_convention attributes are only meant
14401 to describe functions' calling conventions.
14402
14403 However, because it's a necessary piece of information in
14404 Fortran, and because DW_CC_program is the only piece of debugging
14405 information whose definition refers to a 'main program' at all,
14406 several compilers have begun marking Fortran main programs with
14407 DW_CC_program --- even when those functions use the standard
14408 calling conventions.
14409
14410 So until DWARF specifies a way to provide this information and
14411 compilers pick up the new representation, we'll support this
14412 practice. */
14413 if (DW_UNSND (&attr) == DW_CC_program
14414 && cu->language == language_fortran)
01f8c46d
JK
14415 {
14416 set_main_name (part_die->name);
14417
14418 /* As this DIE has a static linkage the name would be difficult
14419 to look up later. */
14420 language_of_main = language_fortran;
14421 }
68511cec 14422 break;
481860b3
GB
14423 case DW_AT_inline:
14424 if (DW_UNSND (&attr) == DW_INL_inlined
14425 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14426 part_die->may_be_inlined = 1;
14427 break;
95554aad
TT
14428
14429 case DW_AT_import:
14430 if (part_die->tag == DW_TAG_imported_unit)
36586728
TT
14431 {
14432 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14433 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14434 || cu->per_cu->is_dwz);
14435 }
95554aad
TT
14436 break;
14437
c906108c
SS
14438 default:
14439 break;
14440 }
14441 }
14442
91da1414
MW
14443 if (high_pc_relative)
14444 part_die->highpc += part_die->lowpc;
14445
9373cf26
JK
14446 if (has_low_pc_attr && has_high_pc_attr)
14447 {
14448 /* When using the GNU linker, .gnu.linkonce. sections are used to
14449 eliminate duplicate copies of functions and vtables and such.
14450 The linker will arbitrarily choose one and discard the others.
14451 The AT_*_pc values for such functions refer to local labels in
14452 these sections. If the section from that file was discarded, the
14453 labels are not in the output, so the relocs get a value of 0.
14454 If this is a discarded function, mark the pc bounds as invalid,
14455 so that GDB will ignore it. */
14456 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14457 {
bb5ed363 14458 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14459
14460 complaint (&symfile_complaints,
14461 _("DW_AT_low_pc %s is zero "
14462 "for DIE at 0x%x [in module %s]"),
14463 paddress (gdbarch, part_die->lowpc),
b64f50a1 14464 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14465 }
14466 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14467 else if (part_die->lowpc >= part_die->highpc)
14468 {
bb5ed363 14469 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
14470
14471 complaint (&symfile_complaints,
14472 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14473 "for DIE at 0x%x [in module %s]"),
14474 paddress (gdbarch, part_die->lowpc),
14475 paddress (gdbarch, part_die->highpc),
b64f50a1 14476 part_die->offset.sect_off, objfile->name);
9373cf26
JK
14477 }
14478 else
14479 part_die->has_pc_info = 1;
14480 }
85cbf3d3 14481
c906108c
SS
14482 return info_ptr;
14483}
14484
72bf9492
DJ
14485/* Find a cached partial DIE at OFFSET in CU. */
14486
14487static struct partial_die_info *
b64f50a1 14488find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
14489{
14490 struct partial_die_info *lookup_die = NULL;
14491 struct partial_die_info part_die;
14492
14493 part_die.offset = offset;
b64f50a1
JK
14494 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14495 offset.sect_off);
72bf9492 14496
72bf9492
DJ
14497 return lookup_die;
14498}
14499
348e048f
DE
14500/* Find a partial DIE at OFFSET, which may or may not be in CU,
14501 except in the case of .debug_types DIEs which do not reference
14502 outside their CU (they do however referencing other types via
55f1336d 14503 DW_FORM_ref_sig8). */
72bf9492
DJ
14504
14505static struct partial_die_info *
36586728 14506find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 14507{
bb5ed363 14508 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
14509 struct dwarf2_per_cu_data *per_cu = NULL;
14510 struct partial_die_info *pd = NULL;
72bf9492 14511
36586728
TT
14512 if (offset_in_dwz == cu->per_cu->is_dwz
14513 && offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
14514 {
14515 pd = find_partial_die_in_comp_unit (offset, cu);
14516 if (pd != NULL)
14517 return pd;
0d99eb77
DE
14518 /* We missed recording what we needed.
14519 Load all dies and try again. */
14520 per_cu = cu->per_cu;
5afb4e99 14521 }
0d99eb77
DE
14522 else
14523 {
14524 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 14525 if (cu->per_cu->is_debug_types)
0d99eb77
DE
14526 {
14527 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14528 " external reference to offset 0x%lx [in module %s].\n"),
14529 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14530 bfd_get_filename (objfile->obfd));
14531 }
36586728
TT
14532 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14533 objfile);
72bf9492 14534
0d99eb77
DE
14535 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14536 load_partial_comp_unit (per_cu);
ae038cb0 14537
0d99eb77
DE
14538 per_cu->cu->last_used = 0;
14539 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14540 }
5afb4e99 14541
dee91e82
DE
14542 /* If we didn't find it, and not all dies have been loaded,
14543 load them all and try again. */
14544
5afb4e99
DJ
14545 if (pd == NULL && per_cu->load_all_dies == 0)
14546 {
5afb4e99 14547 per_cu->load_all_dies = 1;
fd820528
DE
14548
14549 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14550 THIS_CU->cu may already be in use. So we can't just free it and
14551 replace its DIEs with the ones we read in. Instead, we leave those
14552 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14553 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14554 set. */
dee91e82 14555 load_partial_comp_unit (per_cu);
5afb4e99
DJ
14556
14557 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14558 }
14559
14560 if (pd == NULL)
14561 internal_error (__FILE__, __LINE__,
3e43a32a
MS
14562 _("could not find partial DIE 0x%x "
14563 "in cache [from module %s]\n"),
b64f50a1 14564 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 14565 return pd;
72bf9492
DJ
14566}
14567
abc72ce4
DE
14568/* See if we can figure out if the class lives in a namespace. We do
14569 this by looking for a member function; its demangled name will
14570 contain namespace info, if there is any. */
14571
14572static void
14573guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14574 struct dwarf2_cu *cu)
14575{
14576 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14577 what template types look like, because the demangler
14578 frequently doesn't give the same name as the debug info. We
14579 could fix this by only using the demangled name to get the
14580 prefix (but see comment in read_structure_type). */
14581
14582 struct partial_die_info *real_pdi;
14583 struct partial_die_info *child_pdi;
14584
14585 /* If this DIE (this DIE's specification, if any) has a parent, then
14586 we should not do this. We'll prepend the parent's fully qualified
14587 name when we create the partial symbol. */
14588
14589 real_pdi = struct_pdi;
14590 while (real_pdi->has_specification)
36586728
TT
14591 real_pdi = find_partial_die (real_pdi->spec_offset,
14592 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
14593
14594 if (real_pdi->die_parent != NULL)
14595 return;
14596
14597 for (child_pdi = struct_pdi->die_child;
14598 child_pdi != NULL;
14599 child_pdi = child_pdi->die_sibling)
14600 {
14601 if (child_pdi->tag == DW_TAG_subprogram
14602 && child_pdi->linkage_name != NULL)
14603 {
14604 char *actual_class_name
14605 = language_class_name_from_physname (cu->language_defn,
14606 child_pdi->linkage_name);
14607 if (actual_class_name != NULL)
14608 {
14609 struct_pdi->name
10f0c4bb
TT
14610 = obstack_copy0 (&cu->objfile->objfile_obstack,
14611 actual_class_name,
14612 strlen (actual_class_name));
abc72ce4
DE
14613 xfree (actual_class_name);
14614 }
14615 break;
14616 }
14617 }
14618}
14619
72bf9492
DJ
14620/* Adjust PART_DIE before generating a symbol for it. This function
14621 may set the is_external flag or change the DIE's name. */
14622
14623static void
14624fixup_partial_die (struct partial_die_info *part_die,
14625 struct dwarf2_cu *cu)
14626{
abc72ce4
DE
14627 /* Once we've fixed up a die, there's no point in doing so again.
14628 This also avoids a memory leak if we were to call
14629 guess_partial_die_structure_name multiple times. */
14630 if (part_die->fixup_called)
14631 return;
14632
72bf9492
DJ
14633 /* If we found a reference attribute and the DIE has no name, try
14634 to find a name in the referred to DIE. */
14635
14636 if (part_die->name == NULL && part_die->has_specification)
14637 {
14638 struct partial_die_info *spec_die;
72bf9492 14639
36586728
TT
14640 spec_die = find_partial_die (part_die->spec_offset,
14641 part_die->spec_is_dwz, cu);
72bf9492 14642
10b3939b 14643 fixup_partial_die (spec_die, cu);
72bf9492
DJ
14644
14645 if (spec_die->name)
14646 {
14647 part_die->name = spec_die->name;
14648
14649 /* Copy DW_AT_external attribute if it is set. */
14650 if (spec_die->is_external)
14651 part_die->is_external = spec_die->is_external;
14652 }
14653 }
14654
14655 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
14656
14657 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 14658 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 14659
abc72ce4
DE
14660 /* If there is no parent die to provide a namespace, and there are
14661 children, see if we can determine the namespace from their linkage
122d1940 14662 name. */
abc72ce4 14663 if (cu->language == language_cplus
8b70b953 14664 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14665 && part_die->die_parent == NULL
14666 && part_die->has_children
14667 && (part_die->tag == DW_TAG_class_type
14668 || part_die->tag == DW_TAG_structure_type
14669 || part_die->tag == DW_TAG_union_type))
14670 guess_partial_die_structure_name (part_die, cu);
14671
53832f31
TT
14672 /* GCC might emit a nameless struct or union that has a linkage
14673 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14674 if (part_die->name == NULL
96408a79
SA
14675 && (part_die->tag == DW_TAG_class_type
14676 || part_die->tag == DW_TAG_interface_type
14677 || part_die->tag == DW_TAG_structure_type
14678 || part_die->tag == DW_TAG_union_type)
53832f31
TT
14679 && part_die->linkage_name != NULL)
14680 {
14681 char *demangled;
14682
8de20a37 14683 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
53832f31
TT
14684 if (demangled)
14685 {
96408a79
SA
14686 const char *base;
14687
14688 /* Strip any leading namespaces/classes, keep only the base name.
14689 DW_AT_name for named DIEs does not contain the prefixes. */
14690 base = strrchr (demangled, ':');
14691 if (base && base > demangled && base[-1] == ':')
14692 base++;
14693 else
14694 base = demangled;
14695
10f0c4bb
TT
14696 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14697 base, strlen (base));
53832f31
TT
14698 xfree (demangled);
14699 }
14700 }
14701
abc72ce4 14702 part_die->fixup_called = 1;
72bf9492
DJ
14703}
14704
a8329558 14705/* Read an attribute value described by an attribute form. */
c906108c 14706
d521ce57 14707static const gdb_byte *
dee91e82
DE
14708read_attribute_value (const struct die_reader_specs *reader,
14709 struct attribute *attr, unsigned form,
d521ce57 14710 const gdb_byte *info_ptr)
c906108c 14711{
dee91e82
DE
14712 struct dwarf2_cu *cu = reader->cu;
14713 bfd *abfd = reader->abfd;
e7c27a73 14714 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14715 unsigned int bytes_read;
14716 struct dwarf_block *blk;
14717
a8329558
KW
14718 attr->form = form;
14719 switch (form)
c906108c 14720 {
c906108c 14721 case DW_FORM_ref_addr:
ae411497 14722 if (cu->header.version == 2)
4568ecf9 14723 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 14724 else
4568ecf9
DE
14725 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14726 &cu->header, &bytes_read);
ae411497
TT
14727 info_ptr += bytes_read;
14728 break;
36586728
TT
14729 case DW_FORM_GNU_ref_alt:
14730 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14731 info_ptr += bytes_read;
14732 break;
ae411497 14733 case DW_FORM_addr:
e7c27a73 14734 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 14735 info_ptr += bytes_read;
c906108c
SS
14736 break;
14737 case DW_FORM_block2:
7b5a2f43 14738 blk = dwarf_alloc_block (cu);
c906108c
SS
14739 blk->size = read_2_bytes (abfd, info_ptr);
14740 info_ptr += 2;
14741 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14742 info_ptr += blk->size;
14743 DW_BLOCK (attr) = blk;
14744 break;
14745 case DW_FORM_block4:
7b5a2f43 14746 blk = dwarf_alloc_block (cu);
c906108c
SS
14747 blk->size = read_4_bytes (abfd, info_ptr);
14748 info_ptr += 4;
14749 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14750 info_ptr += blk->size;
14751 DW_BLOCK (attr) = blk;
14752 break;
14753 case DW_FORM_data2:
14754 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14755 info_ptr += 2;
14756 break;
14757 case DW_FORM_data4:
14758 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14759 info_ptr += 4;
14760 break;
14761 case DW_FORM_data8:
14762 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14763 info_ptr += 8;
14764 break;
2dc7f7b3
TT
14765 case DW_FORM_sec_offset:
14766 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14767 info_ptr += bytes_read;
14768 break;
c906108c 14769 case DW_FORM_string:
9b1c24c8 14770 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 14771 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
14772 info_ptr += bytes_read;
14773 break;
4bdf3d34 14774 case DW_FORM_strp:
36586728
TT
14775 if (!cu->per_cu->is_dwz)
14776 {
14777 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14778 &bytes_read);
14779 DW_STRING_IS_CANONICAL (attr) = 0;
14780 info_ptr += bytes_read;
14781 break;
14782 }
14783 /* FALLTHROUGH */
14784 case DW_FORM_GNU_strp_alt:
14785 {
14786 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14787 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14788 &bytes_read);
14789
14790 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14791 DW_STRING_IS_CANONICAL (attr) = 0;
14792 info_ptr += bytes_read;
14793 }
4bdf3d34 14794 break;
2dc7f7b3 14795 case DW_FORM_exprloc:
c906108c 14796 case DW_FORM_block:
7b5a2f43 14797 blk = dwarf_alloc_block (cu);
c906108c
SS
14798 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14799 info_ptr += bytes_read;
14800 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14801 info_ptr += blk->size;
14802 DW_BLOCK (attr) = blk;
14803 break;
14804 case DW_FORM_block1:
7b5a2f43 14805 blk = dwarf_alloc_block (cu);
c906108c
SS
14806 blk->size = read_1_byte (abfd, info_ptr);
14807 info_ptr += 1;
14808 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14809 info_ptr += blk->size;
14810 DW_BLOCK (attr) = blk;
14811 break;
14812 case DW_FORM_data1:
14813 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14814 info_ptr += 1;
14815 break;
14816 case DW_FORM_flag:
14817 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14818 info_ptr += 1;
14819 break;
2dc7f7b3
TT
14820 case DW_FORM_flag_present:
14821 DW_UNSND (attr) = 1;
14822 break;
c906108c
SS
14823 case DW_FORM_sdata:
14824 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14825 info_ptr += bytes_read;
14826 break;
14827 case DW_FORM_udata:
14828 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14829 info_ptr += bytes_read;
14830 break;
14831 case DW_FORM_ref1:
4568ecf9
DE
14832 DW_UNSND (attr) = (cu->header.offset.sect_off
14833 + read_1_byte (abfd, info_ptr));
c906108c
SS
14834 info_ptr += 1;
14835 break;
14836 case DW_FORM_ref2:
4568ecf9
DE
14837 DW_UNSND (attr) = (cu->header.offset.sect_off
14838 + read_2_bytes (abfd, info_ptr));
c906108c
SS
14839 info_ptr += 2;
14840 break;
14841 case DW_FORM_ref4:
4568ecf9
DE
14842 DW_UNSND (attr) = (cu->header.offset.sect_off
14843 + read_4_bytes (abfd, info_ptr));
c906108c
SS
14844 info_ptr += 4;
14845 break;
613e1657 14846 case DW_FORM_ref8:
4568ecf9
DE
14847 DW_UNSND (attr) = (cu->header.offset.sect_off
14848 + read_8_bytes (abfd, info_ptr));
613e1657
KB
14849 info_ptr += 8;
14850 break;
55f1336d 14851 case DW_FORM_ref_sig8:
ac9ec31b 14852 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
14853 info_ptr += 8;
14854 break;
c906108c 14855 case DW_FORM_ref_udata:
4568ecf9
DE
14856 DW_UNSND (attr) = (cu->header.offset.sect_off
14857 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
14858 info_ptr += bytes_read;
14859 break;
c906108c 14860 case DW_FORM_indirect:
a8329558
KW
14861 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14862 info_ptr += bytes_read;
dee91e82 14863 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 14864 break;
3019eac3
DE
14865 case DW_FORM_GNU_addr_index:
14866 if (reader->dwo_file == NULL)
14867 {
14868 /* For now flag a hard error.
14869 Later we can turn this into a complaint. */
14870 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14871 dwarf_form_name (form),
14872 bfd_get_filename (abfd));
14873 }
14874 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14875 info_ptr += bytes_read;
14876 break;
14877 case DW_FORM_GNU_str_index:
14878 if (reader->dwo_file == NULL)
14879 {
14880 /* For now flag a hard error.
14881 Later we can turn this into a complaint if warranted. */
14882 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14883 dwarf_form_name (form),
14884 bfd_get_filename (abfd));
14885 }
14886 {
14887 ULONGEST str_index =
14888 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14889
14890 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14891 DW_STRING_IS_CANONICAL (attr) = 0;
14892 info_ptr += bytes_read;
14893 }
14894 break;
c906108c 14895 default:
8a3fe4f8 14896 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
14897 dwarf_form_name (form),
14898 bfd_get_filename (abfd));
c906108c 14899 }
28e94949 14900
36586728 14901 /* Super hack. */
7771576e 14902 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
14903 attr->form = DW_FORM_GNU_ref_alt;
14904
28e94949
JB
14905 /* We have seen instances where the compiler tried to emit a byte
14906 size attribute of -1 which ended up being encoded as an unsigned
14907 0xffffffff. Although 0xffffffff is technically a valid size value,
14908 an object of this size seems pretty unlikely so we can relatively
14909 safely treat these cases as if the size attribute was invalid and
14910 treat them as zero by default. */
14911 if (attr->name == DW_AT_byte_size
14912 && form == DW_FORM_data4
14913 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
14914 {
14915 complaint
14916 (&symfile_complaints,
43bbcdc2
PH
14917 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14918 hex_string (DW_UNSND (attr)));
01c66ae6
JB
14919 DW_UNSND (attr) = 0;
14920 }
28e94949 14921
c906108c
SS
14922 return info_ptr;
14923}
14924
a8329558
KW
14925/* Read an attribute described by an abbreviated attribute. */
14926
d521ce57 14927static const gdb_byte *
dee91e82
DE
14928read_attribute (const struct die_reader_specs *reader,
14929 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 14930 const gdb_byte *info_ptr)
a8329558
KW
14931{
14932 attr->name = abbrev->name;
dee91e82 14933 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
14934}
14935
0963b4bd 14936/* Read dwarf information from a buffer. */
c906108c
SS
14937
14938static unsigned int
a1855c1d 14939read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14940{
fe1b8b76 14941 return bfd_get_8 (abfd, buf);
c906108c
SS
14942}
14943
14944static int
a1855c1d 14945read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 14946{
fe1b8b76 14947 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
14948}
14949
14950static unsigned int
a1855c1d 14951read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14952{
fe1b8b76 14953 return bfd_get_16 (abfd, buf);
c906108c
SS
14954}
14955
21ae7a4d 14956static int
a1855c1d 14957read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14958{
14959 return bfd_get_signed_16 (abfd, buf);
14960}
14961
c906108c 14962static unsigned int
a1855c1d 14963read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14964{
fe1b8b76 14965 return bfd_get_32 (abfd, buf);
c906108c
SS
14966}
14967
21ae7a4d 14968static int
a1855c1d 14969read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
14970{
14971 return bfd_get_signed_32 (abfd, buf);
14972}
14973
93311388 14974static ULONGEST
a1855c1d 14975read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 14976{
fe1b8b76 14977 return bfd_get_64 (abfd, buf);
c906108c
SS
14978}
14979
14980static CORE_ADDR
d521ce57 14981read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 14982 unsigned int *bytes_read)
c906108c 14983{
e7c27a73 14984 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
14985 CORE_ADDR retval = 0;
14986
107d2387 14987 if (cu_header->signed_addr_p)
c906108c 14988 {
107d2387
AC
14989 switch (cu_header->addr_size)
14990 {
14991 case 2:
fe1b8b76 14992 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
14993 break;
14994 case 4:
fe1b8b76 14995 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
14996 break;
14997 case 8:
fe1b8b76 14998 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
14999 break;
15000 default:
8e65ff28 15001 internal_error (__FILE__, __LINE__,
e2e0b3e5 15002 _("read_address: bad switch, signed [in module %s]"),
659b0389 15003 bfd_get_filename (abfd));
107d2387
AC
15004 }
15005 }
15006 else
15007 {
15008 switch (cu_header->addr_size)
15009 {
15010 case 2:
fe1b8b76 15011 retval = bfd_get_16 (abfd, buf);
107d2387
AC
15012 break;
15013 case 4:
fe1b8b76 15014 retval = bfd_get_32 (abfd, buf);
107d2387
AC
15015 break;
15016 case 8:
fe1b8b76 15017 retval = bfd_get_64 (abfd, buf);
107d2387
AC
15018 break;
15019 default:
8e65ff28 15020 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
15021 _("read_address: bad switch, "
15022 "unsigned [in module %s]"),
659b0389 15023 bfd_get_filename (abfd));
107d2387 15024 }
c906108c 15025 }
64367e0a 15026
107d2387
AC
15027 *bytes_read = cu_header->addr_size;
15028 return retval;
c906108c
SS
15029}
15030
f7ef9339 15031/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
15032 specification allows the initial length to take up either 4 bytes
15033 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15034 bytes describe the length and all offsets will be 8 bytes in length
15035 instead of 4.
15036
f7ef9339
KB
15037 An older, non-standard 64-bit format is also handled by this
15038 function. The older format in question stores the initial length
15039 as an 8-byte quantity without an escape value. Lengths greater
15040 than 2^32 aren't very common which means that the initial 4 bytes
15041 is almost always zero. Since a length value of zero doesn't make
15042 sense for the 32-bit format, this initial zero can be considered to
15043 be an escape value which indicates the presence of the older 64-bit
15044 format. As written, the code can't detect (old format) lengths
917c78fc
MK
15045 greater than 4GB. If it becomes necessary to handle lengths
15046 somewhat larger than 4GB, we could allow other small values (such
15047 as the non-sensical values of 1, 2, and 3) to also be used as
15048 escape values indicating the presence of the old format.
f7ef9339 15049
917c78fc
MK
15050 The value returned via bytes_read should be used to increment the
15051 relevant pointer after calling read_initial_length().
c764a876 15052
613e1657
KB
15053 [ Note: read_initial_length() and read_offset() are based on the
15054 document entitled "DWARF Debugging Information Format", revision
f7ef9339 15055 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
15056 from:
15057
f7ef9339 15058 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 15059
613e1657
KB
15060 This document is only a draft and is subject to change. (So beware.)
15061
f7ef9339 15062 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
15063 determined empirically by examining 64-bit ELF files produced by
15064 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
15065
15066 - Kevin, July 16, 2002
613e1657
KB
15067 ] */
15068
15069static LONGEST
d521ce57 15070read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 15071{
fe1b8b76 15072 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 15073
dd373385 15074 if (length == 0xffffffff)
613e1657 15075 {
fe1b8b76 15076 length = bfd_get_64 (abfd, buf + 4);
613e1657 15077 *bytes_read = 12;
613e1657 15078 }
dd373385 15079 else if (length == 0)
f7ef9339 15080 {
dd373385 15081 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 15082 length = bfd_get_64 (abfd, buf);
f7ef9339 15083 *bytes_read = 8;
f7ef9339 15084 }
613e1657
KB
15085 else
15086 {
15087 *bytes_read = 4;
613e1657
KB
15088 }
15089
c764a876
DE
15090 return length;
15091}
dd373385 15092
c764a876
DE
15093/* Cover function for read_initial_length.
15094 Returns the length of the object at BUF, and stores the size of the
15095 initial length in *BYTES_READ and stores the size that offsets will be in
15096 *OFFSET_SIZE.
15097 If the initial length size is not equivalent to that specified in
15098 CU_HEADER then issue a complaint.
15099 This is useful when reading non-comp-unit headers. */
dd373385 15100
c764a876 15101static LONGEST
d521ce57 15102read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
15103 const struct comp_unit_head *cu_header,
15104 unsigned int *bytes_read,
15105 unsigned int *offset_size)
15106{
15107 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15108
15109 gdb_assert (cu_header->initial_length_size == 4
15110 || cu_header->initial_length_size == 8
15111 || cu_header->initial_length_size == 12);
15112
15113 if (cu_header->initial_length_size != *bytes_read)
15114 complaint (&symfile_complaints,
15115 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 15116
c764a876 15117 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 15118 return length;
613e1657
KB
15119}
15120
15121/* Read an offset from the data stream. The size of the offset is
917c78fc 15122 given by cu_header->offset_size. */
613e1657
KB
15123
15124static LONGEST
d521ce57
TT
15125read_offset (bfd *abfd, const gdb_byte *buf,
15126 const struct comp_unit_head *cu_header,
891d2f0b 15127 unsigned int *bytes_read)
c764a876
DE
15128{
15129 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 15130
c764a876
DE
15131 *bytes_read = cu_header->offset_size;
15132 return offset;
15133}
15134
15135/* Read an offset from the data stream. */
15136
15137static LONGEST
d521ce57 15138read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
15139{
15140 LONGEST retval = 0;
15141
c764a876 15142 switch (offset_size)
613e1657
KB
15143 {
15144 case 4:
fe1b8b76 15145 retval = bfd_get_32 (abfd, buf);
613e1657
KB
15146 break;
15147 case 8:
fe1b8b76 15148 retval = bfd_get_64 (abfd, buf);
613e1657
KB
15149 break;
15150 default:
8e65ff28 15151 internal_error (__FILE__, __LINE__,
c764a876 15152 _("read_offset_1: bad switch [in module %s]"),
659b0389 15153 bfd_get_filename (abfd));
613e1657
KB
15154 }
15155
917c78fc 15156 return retval;
613e1657
KB
15157}
15158
d521ce57
TT
15159static const gdb_byte *
15160read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
15161{
15162 /* If the size of a host char is 8 bits, we can return a pointer
15163 to the buffer, otherwise we have to copy the data to a buffer
15164 allocated on the temporary obstack. */
4bdf3d34 15165 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 15166 return buf;
c906108c
SS
15167}
15168
d521ce57
TT
15169static const char *
15170read_direct_string (bfd *abfd, const gdb_byte *buf,
15171 unsigned int *bytes_read_ptr)
c906108c
SS
15172{
15173 /* If the size of a host char is 8 bits, we can return a pointer
15174 to the string, otherwise we have to copy the string to a buffer
15175 allocated on the temporary obstack. */
4bdf3d34 15176 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
15177 if (*buf == '\0')
15178 {
15179 *bytes_read_ptr = 1;
15180 return NULL;
15181 }
d521ce57
TT
15182 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15183 return (const char *) buf;
4bdf3d34
JJ
15184}
15185
d521ce57 15186static const char *
cf2c3c16 15187read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 15188{
be391dca 15189 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 15190 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
15191 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15192 bfd_get_filename (abfd));
dce234bc 15193 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
15194 error (_("DW_FORM_strp pointing outside of "
15195 ".debug_str section [in module %s]"),
15196 bfd_get_filename (abfd));
4bdf3d34 15197 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 15198 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 15199 return NULL;
d521ce57 15200 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
15201}
15202
36586728
TT
15203/* Read a string at offset STR_OFFSET in the .debug_str section from
15204 the .dwz file DWZ. Throw an error if the offset is too large. If
15205 the string consists of a single NUL byte, return NULL; otherwise
15206 return a pointer to the string. */
15207
d521ce57 15208static const char *
36586728
TT
15209read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15210{
15211 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15212
15213 if (dwz->str.buffer == NULL)
15214 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15215 "section [in module %s]"),
15216 bfd_get_filename (dwz->dwz_bfd));
15217 if (str_offset >= dwz->str.size)
15218 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15219 ".debug_str section [in module %s]"),
15220 bfd_get_filename (dwz->dwz_bfd));
15221 gdb_assert (HOST_CHAR_BIT == 8);
15222 if (dwz->str.buffer[str_offset] == '\0')
15223 return NULL;
d521ce57 15224 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
15225}
15226
d521ce57
TT
15227static const char *
15228read_indirect_string (bfd *abfd, const gdb_byte *buf,
cf2c3c16
TT
15229 const struct comp_unit_head *cu_header,
15230 unsigned int *bytes_read_ptr)
15231{
15232 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15233
15234 return read_indirect_string_at_offset (abfd, str_offset);
15235}
15236
12df843f 15237static ULONGEST
d521ce57
TT
15238read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15239 unsigned int *bytes_read_ptr)
c906108c 15240{
12df843f 15241 ULONGEST result;
ce5d95e1 15242 unsigned int num_read;
c906108c
SS
15243 int i, shift;
15244 unsigned char byte;
15245
15246 result = 0;
15247 shift = 0;
15248 num_read = 0;
15249 i = 0;
15250 while (1)
15251 {
fe1b8b76 15252 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15253 buf++;
15254 num_read++;
12df843f 15255 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
15256 if ((byte & 128) == 0)
15257 {
15258 break;
15259 }
15260 shift += 7;
15261 }
15262 *bytes_read_ptr = num_read;
15263 return result;
15264}
15265
12df843f 15266static LONGEST
d521ce57
TT
15267read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15268 unsigned int *bytes_read_ptr)
c906108c 15269{
12df843f 15270 LONGEST result;
77e0b926 15271 int i, shift, num_read;
c906108c
SS
15272 unsigned char byte;
15273
15274 result = 0;
15275 shift = 0;
c906108c
SS
15276 num_read = 0;
15277 i = 0;
15278 while (1)
15279 {
fe1b8b76 15280 byte = bfd_get_8 (abfd, buf);
c906108c
SS
15281 buf++;
15282 num_read++;
12df843f 15283 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
15284 shift += 7;
15285 if ((byte & 128) == 0)
15286 {
15287 break;
15288 }
15289 }
77e0b926 15290 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 15291 result |= -(((LONGEST) 1) << shift);
c906108c
SS
15292 *bytes_read_ptr = num_read;
15293 return result;
15294}
15295
3019eac3
DE
15296/* Given index ADDR_INDEX in .debug_addr, fetch the value.
15297 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15298 ADDR_SIZE is the size of addresses from the CU header. */
15299
15300static CORE_ADDR
15301read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15302{
15303 struct objfile *objfile = dwarf2_per_objfile->objfile;
15304 bfd *abfd = objfile->obfd;
15305 const gdb_byte *info_ptr;
15306
15307 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15308 if (dwarf2_per_objfile->addr.buffer == NULL)
15309 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15310 objfile->name);
15311 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15312 error (_("DW_FORM_addr_index pointing outside of "
15313 ".debug_addr section [in module %s]"),
15314 objfile->name);
15315 info_ptr = (dwarf2_per_objfile->addr.buffer
15316 + addr_base + addr_index * addr_size);
15317 if (addr_size == 4)
15318 return bfd_get_32 (abfd, info_ptr);
15319 else
15320 return bfd_get_64 (abfd, info_ptr);
15321}
15322
15323/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15324
15325static CORE_ADDR
15326read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15327{
15328 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15329}
15330
15331/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15332
15333static CORE_ADDR
d521ce57 15334read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
15335 unsigned int *bytes_read)
15336{
15337 bfd *abfd = cu->objfile->obfd;
15338 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15339
15340 return read_addr_index (cu, addr_index);
15341}
15342
15343/* Data structure to pass results from dwarf2_read_addr_index_reader
15344 back to dwarf2_read_addr_index. */
15345
15346struct dwarf2_read_addr_index_data
15347{
15348 ULONGEST addr_base;
15349 int addr_size;
15350};
15351
15352/* die_reader_func for dwarf2_read_addr_index. */
15353
15354static void
15355dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 15356 const gdb_byte *info_ptr,
3019eac3
DE
15357 struct die_info *comp_unit_die,
15358 int has_children,
15359 void *data)
15360{
15361 struct dwarf2_cu *cu = reader->cu;
15362 struct dwarf2_read_addr_index_data *aidata =
15363 (struct dwarf2_read_addr_index_data *) data;
15364
15365 aidata->addr_base = cu->addr_base;
15366 aidata->addr_size = cu->header.addr_size;
15367}
15368
15369/* Given an index in .debug_addr, fetch the value.
15370 NOTE: This can be called during dwarf expression evaluation,
15371 long after the debug information has been read, and thus per_cu->cu
15372 may no longer exist. */
15373
15374CORE_ADDR
15375dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15376 unsigned int addr_index)
15377{
15378 struct objfile *objfile = per_cu->objfile;
15379 struct dwarf2_cu *cu = per_cu->cu;
15380 ULONGEST addr_base;
15381 int addr_size;
15382
15383 /* This is intended to be called from outside this file. */
15384 dw2_setup (objfile);
15385
15386 /* We need addr_base and addr_size.
15387 If we don't have PER_CU->cu, we have to get it.
15388 Nasty, but the alternative is storing the needed info in PER_CU,
15389 which at this point doesn't seem justified: it's not clear how frequently
15390 it would get used and it would increase the size of every PER_CU.
15391 Entry points like dwarf2_per_cu_addr_size do a similar thing
15392 so we're not in uncharted territory here.
15393 Alas we need to be a bit more complicated as addr_base is contained
15394 in the DIE.
15395
15396 We don't need to read the entire CU(/TU).
15397 We just need the header and top level die.
a1b64ce1 15398
3019eac3 15399 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 15400 For now we skip this optimization. */
3019eac3
DE
15401
15402 if (cu != NULL)
15403 {
15404 addr_base = cu->addr_base;
15405 addr_size = cu->header.addr_size;
15406 }
15407 else
15408 {
15409 struct dwarf2_read_addr_index_data aidata;
15410
a1b64ce1
DE
15411 /* Note: We can't use init_cutu_and_read_dies_simple here,
15412 we need addr_base. */
15413 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15414 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
15415 addr_base = aidata.addr_base;
15416 addr_size = aidata.addr_size;
15417 }
15418
15419 return read_addr_index_1 (addr_index, addr_base, addr_size);
15420}
15421
15422/* Given a DW_AT_str_index, fetch the string. */
15423
d521ce57 15424static const char *
3019eac3
DE
15425read_str_index (const struct die_reader_specs *reader,
15426 struct dwarf2_cu *cu, ULONGEST str_index)
15427{
15428 struct objfile *objfile = dwarf2_per_objfile->objfile;
15429 const char *dwo_name = objfile->name;
15430 bfd *abfd = objfile->obfd;
15431 struct dwo_sections *sections = &reader->dwo_file->sections;
d521ce57 15432 const gdb_byte *info_ptr;
3019eac3
DE
15433 ULONGEST str_offset;
15434
15435 dwarf2_read_section (objfile, &sections->str);
15436 dwarf2_read_section (objfile, &sections->str_offsets);
15437 if (sections->str.buffer == NULL)
15438 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15439 " in CU at offset 0x%lx [in module %s]"),
15440 (long) cu->header.offset.sect_off, dwo_name);
15441 if (sections->str_offsets.buffer == NULL)
15442 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15443 " in CU at offset 0x%lx [in module %s]"),
15444 (long) cu->header.offset.sect_off, dwo_name);
15445 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15446 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15447 " section in CU at offset 0x%lx [in module %s]"),
15448 (long) cu->header.offset.sect_off, dwo_name);
15449 info_ptr = (sections->str_offsets.buffer
15450 + str_index * cu->header.offset_size);
15451 if (cu->header.offset_size == 4)
15452 str_offset = bfd_get_32 (abfd, info_ptr);
15453 else
15454 str_offset = bfd_get_64 (abfd, info_ptr);
15455 if (str_offset >= sections->str.size)
15456 error (_("Offset from DW_FORM_str_index pointing outside of"
15457 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15458 (long) cu->header.offset.sect_off, dwo_name);
d521ce57 15459 return (const char *) (sections->str.buffer + str_offset);
3019eac3
DE
15460}
15461
3019eac3
DE
15462/* Return the length of an LEB128 number in BUF. */
15463
15464static int
15465leb128_size (const gdb_byte *buf)
15466{
15467 const gdb_byte *begin = buf;
15468 gdb_byte byte;
15469
15470 while (1)
15471 {
15472 byte = *buf++;
15473 if ((byte & 128) == 0)
15474 return buf - begin;
15475 }
15476}
15477
c906108c 15478static void
e142c38c 15479set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
15480{
15481 switch (lang)
15482 {
15483 case DW_LANG_C89:
76bee0cc 15484 case DW_LANG_C99:
c906108c 15485 case DW_LANG_C:
d1be3247 15486 case DW_LANG_UPC:
e142c38c 15487 cu->language = language_c;
c906108c
SS
15488 break;
15489 case DW_LANG_C_plus_plus:
e142c38c 15490 cu->language = language_cplus;
c906108c 15491 break;
6aecb9c2
JB
15492 case DW_LANG_D:
15493 cu->language = language_d;
15494 break;
c906108c
SS
15495 case DW_LANG_Fortran77:
15496 case DW_LANG_Fortran90:
b21b22e0 15497 case DW_LANG_Fortran95:
e142c38c 15498 cu->language = language_fortran;
c906108c 15499 break;
a766d390
DE
15500 case DW_LANG_Go:
15501 cu->language = language_go;
15502 break;
c906108c 15503 case DW_LANG_Mips_Assembler:
e142c38c 15504 cu->language = language_asm;
c906108c 15505 break;
bebd888e 15506 case DW_LANG_Java:
e142c38c 15507 cu->language = language_java;
bebd888e 15508 break;
c906108c 15509 case DW_LANG_Ada83:
8aaf0b47 15510 case DW_LANG_Ada95:
bc5f45f8
JB
15511 cu->language = language_ada;
15512 break;
72019c9c
GM
15513 case DW_LANG_Modula2:
15514 cu->language = language_m2;
15515 break;
fe8e67fd
PM
15516 case DW_LANG_Pascal83:
15517 cu->language = language_pascal;
15518 break;
22566fbd
DJ
15519 case DW_LANG_ObjC:
15520 cu->language = language_objc;
15521 break;
c906108c
SS
15522 case DW_LANG_Cobol74:
15523 case DW_LANG_Cobol85:
c906108c 15524 default:
e142c38c 15525 cu->language = language_minimal;
c906108c
SS
15526 break;
15527 }
e142c38c 15528 cu->language_defn = language_def (cu->language);
c906108c
SS
15529}
15530
15531/* Return the named attribute or NULL if not there. */
15532
15533static struct attribute *
e142c38c 15534dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 15535{
a48e046c 15536 for (;;)
c906108c 15537 {
a48e046c
TT
15538 unsigned int i;
15539 struct attribute *spec = NULL;
15540
15541 for (i = 0; i < die->num_attrs; ++i)
15542 {
15543 if (die->attrs[i].name == name)
15544 return &die->attrs[i];
15545 if (die->attrs[i].name == DW_AT_specification
15546 || die->attrs[i].name == DW_AT_abstract_origin)
15547 spec = &die->attrs[i];
15548 }
15549
15550 if (!spec)
15551 break;
c906108c 15552
f2f0e013 15553 die = follow_die_ref (die, spec, &cu);
f2f0e013 15554 }
c5aa993b 15555
c906108c
SS
15556 return NULL;
15557}
15558
348e048f
DE
15559/* Return the named attribute or NULL if not there,
15560 but do not follow DW_AT_specification, etc.
15561 This is for use in contexts where we're reading .debug_types dies.
15562 Following DW_AT_specification, DW_AT_abstract_origin will take us
15563 back up the chain, and we want to go down. */
15564
15565static struct attribute *
45e58e77 15566dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
15567{
15568 unsigned int i;
15569
15570 for (i = 0; i < die->num_attrs; ++i)
15571 if (die->attrs[i].name == name)
15572 return &die->attrs[i];
15573
15574 return NULL;
15575}
15576
05cf31d1
JB
15577/* Return non-zero iff the attribute NAME is defined for the given DIE,
15578 and holds a non-zero value. This function should only be used for
2dc7f7b3 15579 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
15580
15581static int
15582dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15583{
15584 struct attribute *attr = dwarf2_attr (die, name, cu);
15585
15586 return (attr && DW_UNSND (attr));
15587}
15588
3ca72b44 15589static int
e142c38c 15590die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 15591{
05cf31d1
JB
15592 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15593 which value is non-zero. However, we have to be careful with
15594 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15595 (via dwarf2_flag_true_p) follows this attribute. So we may
15596 end up accidently finding a declaration attribute that belongs
15597 to a different DIE referenced by the specification attribute,
15598 even though the given DIE does not have a declaration attribute. */
15599 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15600 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
15601}
15602
63d06c5c 15603/* Return the die giving the specification for DIE, if there is
f2f0e013 15604 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
15605 containing the return value on output. If there is no
15606 specification, but there is an abstract origin, that is
15607 returned. */
63d06c5c
DC
15608
15609static struct die_info *
f2f0e013 15610die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 15611{
f2f0e013
DJ
15612 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15613 *spec_cu);
63d06c5c 15614
edb3359d
DJ
15615 if (spec_attr == NULL)
15616 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15617
63d06c5c
DC
15618 if (spec_attr == NULL)
15619 return NULL;
15620 else
f2f0e013 15621 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 15622}
c906108c 15623
debd256d 15624/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
15625 refers to.
15626 NOTE: This is also used as a "cleanup" function. */
15627
debd256d
JB
15628static void
15629free_line_header (struct line_header *lh)
15630{
15631 if (lh->standard_opcode_lengths)
a8bc7b56 15632 xfree (lh->standard_opcode_lengths);
debd256d
JB
15633
15634 /* Remember that all the lh->file_names[i].name pointers are
15635 pointers into debug_line_buffer, and don't need to be freed. */
15636 if (lh->file_names)
a8bc7b56 15637 xfree (lh->file_names);
debd256d
JB
15638
15639 /* Similarly for the include directory names. */
15640 if (lh->include_dirs)
a8bc7b56 15641 xfree (lh->include_dirs);
debd256d 15642
a8bc7b56 15643 xfree (lh);
debd256d
JB
15644}
15645
debd256d 15646/* Add an entry to LH's include directory table. */
ae2de4f8 15647
debd256d 15648static void
d521ce57 15649add_include_dir (struct line_header *lh, const char *include_dir)
c906108c 15650{
debd256d
JB
15651 /* Grow the array if necessary. */
15652 if (lh->include_dirs_size == 0)
c5aa993b 15653 {
debd256d
JB
15654 lh->include_dirs_size = 1; /* for testing */
15655 lh->include_dirs = xmalloc (lh->include_dirs_size
15656 * sizeof (*lh->include_dirs));
15657 }
15658 else if (lh->num_include_dirs >= lh->include_dirs_size)
15659 {
15660 lh->include_dirs_size *= 2;
15661 lh->include_dirs = xrealloc (lh->include_dirs,
15662 (lh->include_dirs_size
15663 * sizeof (*lh->include_dirs)));
c5aa993b 15664 }
c906108c 15665
debd256d
JB
15666 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15667}
6e70227d 15668
debd256d 15669/* Add an entry to LH's file name table. */
ae2de4f8 15670
debd256d
JB
15671static void
15672add_file_name (struct line_header *lh,
d521ce57 15673 const char *name,
debd256d
JB
15674 unsigned int dir_index,
15675 unsigned int mod_time,
15676 unsigned int length)
15677{
15678 struct file_entry *fe;
15679
15680 /* Grow the array if necessary. */
15681 if (lh->file_names_size == 0)
15682 {
15683 lh->file_names_size = 1; /* for testing */
15684 lh->file_names = xmalloc (lh->file_names_size
15685 * sizeof (*lh->file_names));
15686 }
15687 else if (lh->num_file_names >= lh->file_names_size)
15688 {
15689 lh->file_names_size *= 2;
15690 lh->file_names = xrealloc (lh->file_names,
15691 (lh->file_names_size
15692 * sizeof (*lh->file_names)));
15693 }
15694
15695 fe = &lh->file_names[lh->num_file_names++];
15696 fe->name = name;
15697 fe->dir_index = dir_index;
15698 fe->mod_time = mod_time;
15699 fe->length = length;
aaa75496 15700 fe->included_p = 0;
cb1df416 15701 fe->symtab = NULL;
debd256d 15702}
6e70227d 15703
36586728
TT
15704/* A convenience function to find the proper .debug_line section for a
15705 CU. */
15706
15707static struct dwarf2_section_info *
15708get_debug_line_section (struct dwarf2_cu *cu)
15709{
15710 struct dwarf2_section_info *section;
15711
15712 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15713 DWO file. */
15714 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15715 section = &cu->dwo_unit->dwo_file->sections.line;
15716 else if (cu->per_cu->is_dwz)
15717 {
15718 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15719
15720 section = &dwz->line;
15721 }
15722 else
15723 section = &dwarf2_per_objfile->line;
15724
15725 return section;
15726}
15727
debd256d 15728/* Read the statement program header starting at OFFSET in
3019eac3 15729 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 15730 to a struct line_header, allocated using xmalloc.
debd256d
JB
15731
15732 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
15733 the returned object point into the dwarf line section buffer,
15734 and must not be freed. */
ae2de4f8 15735
debd256d 15736static struct line_header *
3019eac3 15737dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
15738{
15739 struct cleanup *back_to;
15740 struct line_header *lh;
d521ce57 15741 const gdb_byte *line_ptr;
c764a876 15742 unsigned int bytes_read, offset_size;
debd256d 15743 int i;
d521ce57 15744 const char *cur_dir, *cur_file;
3019eac3
DE
15745 struct dwarf2_section_info *section;
15746 bfd *abfd;
15747
36586728 15748 section = get_debug_line_section (cu);
3019eac3
DE
15749 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15750 if (section->buffer == NULL)
debd256d 15751 {
3019eac3
DE
15752 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15753 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15754 else
15755 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
15756 return 0;
15757 }
15758
fceca515
DE
15759 /* We can't do this until we know the section is non-empty.
15760 Only then do we know we have such a section. */
15761 abfd = section->asection->owner;
15762
a738430d
MK
15763 /* Make sure that at least there's room for the total_length field.
15764 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 15765 if (offset + 4 >= section->size)
debd256d 15766 {
4d3c2250 15767 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
15768 return 0;
15769 }
15770
15771 lh = xmalloc (sizeof (*lh));
15772 memset (lh, 0, sizeof (*lh));
15773 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15774 (void *) lh);
15775
3019eac3 15776 line_ptr = section->buffer + offset;
debd256d 15777
a738430d 15778 /* Read in the header. */
6e70227d 15779 lh->total_length =
c764a876
DE
15780 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15781 &bytes_read, &offset_size);
debd256d 15782 line_ptr += bytes_read;
3019eac3 15783 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 15784 {
4d3c2250 15785 dwarf2_statement_list_fits_in_line_number_section_complaint ();
2f324bf6 15786 do_cleanups (back_to);
debd256d
JB
15787 return 0;
15788 }
15789 lh->statement_program_end = line_ptr + lh->total_length;
15790 lh->version = read_2_bytes (abfd, line_ptr);
15791 line_ptr += 2;
c764a876
DE
15792 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15793 line_ptr += offset_size;
debd256d
JB
15794 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15795 line_ptr += 1;
2dc7f7b3
TT
15796 if (lh->version >= 4)
15797 {
15798 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15799 line_ptr += 1;
15800 }
15801 else
15802 lh->maximum_ops_per_instruction = 1;
15803
15804 if (lh->maximum_ops_per_instruction == 0)
15805 {
15806 lh->maximum_ops_per_instruction = 1;
15807 complaint (&symfile_complaints,
3e43a32a
MS
15808 _("invalid maximum_ops_per_instruction "
15809 "in `.debug_line' section"));
2dc7f7b3
TT
15810 }
15811
debd256d
JB
15812 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15813 line_ptr += 1;
15814 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15815 line_ptr += 1;
15816 lh->line_range = read_1_byte (abfd, line_ptr);
15817 line_ptr += 1;
15818 lh->opcode_base = read_1_byte (abfd, line_ptr);
15819 line_ptr += 1;
15820 lh->standard_opcode_lengths
fe1b8b76 15821 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
15822
15823 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15824 for (i = 1; i < lh->opcode_base; ++i)
15825 {
15826 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15827 line_ptr += 1;
15828 }
15829
a738430d 15830 /* Read directory table. */
9b1c24c8 15831 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15832 {
15833 line_ptr += bytes_read;
15834 add_include_dir (lh, cur_dir);
15835 }
15836 line_ptr += bytes_read;
15837
a738430d 15838 /* Read file name table. */
9b1c24c8 15839 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
15840 {
15841 unsigned int dir_index, mod_time, length;
15842
15843 line_ptr += bytes_read;
15844 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15845 line_ptr += bytes_read;
15846 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15847 line_ptr += bytes_read;
15848 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15849 line_ptr += bytes_read;
15850
15851 add_file_name (lh, cur_file, dir_index, mod_time, length);
15852 }
15853 line_ptr += bytes_read;
6e70227d 15854 lh->statement_program_start = line_ptr;
debd256d 15855
3019eac3 15856 if (line_ptr > (section->buffer + section->size))
4d3c2250 15857 complaint (&symfile_complaints,
3e43a32a
MS
15858 _("line number info header doesn't "
15859 "fit in `.debug_line' section"));
debd256d
JB
15860
15861 discard_cleanups (back_to);
15862 return lh;
15863}
c906108c 15864
c6da4cef
DE
15865/* Subroutine of dwarf_decode_lines to simplify it.
15866 Return the file name of the psymtab for included file FILE_INDEX
15867 in line header LH of PST.
15868 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15869 If space for the result is malloc'd, it will be freed by a cleanup.
1ed59174
JK
15870 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15871
15872 The function creates dangling cleanup registration. */
c6da4cef 15873
d521ce57 15874static const char *
c6da4cef
DE
15875psymtab_include_file_name (const struct line_header *lh, int file_index,
15876 const struct partial_symtab *pst,
15877 const char *comp_dir)
15878{
15879 const struct file_entry fe = lh->file_names [file_index];
d521ce57
TT
15880 const char *include_name = fe.name;
15881 const char *include_name_to_compare = include_name;
15882 const char *dir_name = NULL;
72b9f47f
TT
15883 const char *pst_filename;
15884 char *copied_name = NULL;
c6da4cef
DE
15885 int file_is_pst;
15886
15887 if (fe.dir_index)
15888 dir_name = lh->include_dirs[fe.dir_index - 1];
15889
15890 if (!IS_ABSOLUTE_PATH (include_name)
15891 && (dir_name != NULL || comp_dir != NULL))
15892 {
15893 /* Avoid creating a duplicate psymtab for PST.
15894 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15895 Before we do the comparison, however, we need to account
15896 for DIR_NAME and COMP_DIR.
15897 First prepend dir_name (if non-NULL). If we still don't
15898 have an absolute path prepend comp_dir (if non-NULL).
15899 However, the directory we record in the include-file's
15900 psymtab does not contain COMP_DIR (to match the
15901 corresponding symtab(s)).
15902
15903 Example:
15904
15905 bash$ cd /tmp
15906 bash$ gcc -g ./hello.c
15907 include_name = "hello.c"
15908 dir_name = "."
15909 DW_AT_comp_dir = comp_dir = "/tmp"
15910 DW_AT_name = "./hello.c" */
15911
15912 if (dir_name != NULL)
15913 {
d521ce57
TT
15914 char *tem = concat (dir_name, SLASH_STRING,
15915 include_name, (char *)NULL);
15916
15917 make_cleanup (xfree, tem);
15918 include_name = tem;
c6da4cef 15919 include_name_to_compare = include_name;
c6da4cef
DE
15920 }
15921 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15922 {
d521ce57
TT
15923 char *tem = concat (comp_dir, SLASH_STRING,
15924 include_name, (char *)NULL);
15925
15926 make_cleanup (xfree, tem);
15927 include_name_to_compare = tem;
c6da4cef
DE
15928 }
15929 }
15930
15931 pst_filename = pst->filename;
15932 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15933 {
72b9f47f
TT
15934 copied_name = concat (pst->dirname, SLASH_STRING,
15935 pst_filename, (char *)NULL);
15936 pst_filename = copied_name;
c6da4cef
DE
15937 }
15938
1e3fad37 15939 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 15940
72b9f47f
TT
15941 if (copied_name != NULL)
15942 xfree (copied_name);
c6da4cef
DE
15943
15944 if (file_is_pst)
15945 return NULL;
15946 return include_name;
15947}
15948
c91513d8
PP
15949/* Ignore this record_line request. */
15950
15951static void
15952noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15953{
15954 return;
15955}
15956
f3f5162e
DE
15957/* Subroutine of dwarf_decode_lines to simplify it.
15958 Process the line number information in LH. */
debd256d 15959
c906108c 15960static void
f3f5162e
DE
15961dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15962 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 15963{
d521ce57
TT
15964 const gdb_byte *line_ptr, *extended_end;
15965 const gdb_byte *line_end;
a8c50c1f 15966 unsigned int bytes_read, extended_len;
c906108c 15967 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
15968 CORE_ADDR baseaddr;
15969 struct objfile *objfile = cu->objfile;
f3f5162e 15970 bfd *abfd = objfile->obfd;
fbf65064 15971 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 15972 const int decode_for_pst_p = (pst != NULL);
f3f5162e 15973 struct subfile *last_subfile = NULL;
c91513d8
PP
15974 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15975 = record_line;
e142c38c
DJ
15976
15977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 15978
debd256d
JB
15979 line_ptr = lh->statement_program_start;
15980 line_end = lh->statement_program_end;
c906108c
SS
15981
15982 /* Read the statement sequences until there's nothing left. */
15983 while (line_ptr < line_end)
15984 {
15985 /* state machine registers */
15986 CORE_ADDR address = 0;
15987 unsigned int file = 1;
15988 unsigned int line = 1;
15989 unsigned int column = 0;
debd256d 15990 int is_stmt = lh->default_is_stmt;
c906108c
SS
15991 int basic_block = 0;
15992 int end_sequence = 0;
fbf65064 15993 CORE_ADDR addr;
2dc7f7b3 15994 unsigned char op_index = 0;
c906108c 15995
aaa75496 15996 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 15997 {
aaa75496 15998 /* Start a subfile for the current file of the state machine. */
debd256d
JB
15999 /* lh->include_dirs and lh->file_names are 0-based, but the
16000 directory and file name numbers in the statement program
16001 are 1-based. */
16002 struct file_entry *fe = &lh->file_names[file - 1];
d521ce57 16003 const char *dir = NULL;
a738430d 16004
debd256d
JB
16005 if (fe->dir_index)
16006 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
16007
16008 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
16009 }
16010
a738430d 16011 /* Decode the table. */
c5aa993b 16012 while (!end_sequence)
c906108c
SS
16013 {
16014 op_code = read_1_byte (abfd, line_ptr);
16015 line_ptr += 1;
59205f5a
JB
16016 if (line_ptr > line_end)
16017 {
16018 dwarf2_debug_line_missing_end_sequence_complaint ();
16019 break;
16020 }
9aa1fe7e 16021
debd256d 16022 if (op_code >= lh->opcode_base)
6e70227d 16023 {
a738430d 16024 /* Special operand. */
debd256d 16025 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
16026 address += (((op_index + (adj_opcode / lh->line_range))
16027 / lh->maximum_ops_per_instruction)
16028 * lh->minimum_instruction_length);
16029 op_index = ((op_index + (adj_opcode / lh->line_range))
16030 % lh->maximum_ops_per_instruction);
debd256d 16031 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 16032 if (lh->num_file_names < file || file == 0)
25e43795 16033 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
16034 /* For now we ignore lines not starting on an
16035 instruction boundary. */
16036 else if (op_index == 0)
25e43795
DJ
16037 {
16038 lh->file_names[file - 1].included_p = 1;
ca5f395d 16039 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16040 {
16041 if (last_subfile != current_subfile)
16042 {
16043 addr = gdbarch_addr_bits_remove (gdbarch, address);
16044 if (last_subfile)
c91513d8 16045 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16046 last_subfile = current_subfile;
16047 }
25e43795 16048 /* Append row to matrix using current values. */
7019d805 16049 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16050 (*p_record_line) (current_subfile, line, addr);
366da635 16051 }
25e43795 16052 }
ca5f395d 16053 basic_block = 0;
9aa1fe7e
GK
16054 }
16055 else switch (op_code)
c906108c
SS
16056 {
16057 case DW_LNS_extended_op:
3e43a32a
MS
16058 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16059 &bytes_read);
473b7be6 16060 line_ptr += bytes_read;
a8c50c1f 16061 extended_end = line_ptr + extended_len;
c906108c
SS
16062 extended_op = read_1_byte (abfd, line_ptr);
16063 line_ptr += 1;
16064 switch (extended_op)
16065 {
16066 case DW_LNE_end_sequence:
c91513d8 16067 p_record_line = record_line;
c906108c 16068 end_sequence = 1;
c906108c
SS
16069 break;
16070 case DW_LNE_set_address:
e7c27a73 16071 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
16072
16073 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16074 {
16075 /* This line table is for a function which has been
16076 GCd by the linker. Ignore it. PR gdb/12528 */
16077
16078 long line_offset
36586728 16079 = line_ptr - get_debug_line_section (cu)->buffer;
c91513d8
PP
16080
16081 complaint (&symfile_complaints,
16082 _(".debug_line address at offset 0x%lx is 0 "
16083 "[in module %s]"),
bb5ed363 16084 line_offset, objfile->name);
c91513d8
PP
16085 p_record_line = noop_record_line;
16086 }
16087
2dc7f7b3 16088 op_index = 0;
107d2387
AC
16089 line_ptr += bytes_read;
16090 address += baseaddr;
c906108c
SS
16091 break;
16092 case DW_LNE_define_file:
debd256d 16093 {
d521ce57 16094 const char *cur_file;
debd256d 16095 unsigned int dir_index, mod_time, length;
6e70227d 16096
3e43a32a
MS
16097 cur_file = read_direct_string (abfd, line_ptr,
16098 &bytes_read);
debd256d
JB
16099 line_ptr += bytes_read;
16100 dir_index =
16101 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16102 line_ptr += bytes_read;
16103 mod_time =
16104 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16105 line_ptr += bytes_read;
16106 length =
16107 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16108 line_ptr += bytes_read;
16109 add_file_name (lh, cur_file, dir_index, mod_time, length);
16110 }
c906108c 16111 break;
d0c6ba3d
CC
16112 case DW_LNE_set_discriminator:
16113 /* The discriminator is not interesting to the debugger;
16114 just ignore it. */
16115 line_ptr = extended_end;
16116 break;
c906108c 16117 default:
4d3c2250 16118 complaint (&symfile_complaints,
e2e0b3e5 16119 _("mangled .debug_line section"));
debd256d 16120 return;
c906108c 16121 }
a8c50c1f
DJ
16122 /* Make sure that we parsed the extended op correctly. If e.g.
16123 we expected a different address size than the producer used,
16124 we may have read the wrong number of bytes. */
16125 if (line_ptr != extended_end)
16126 {
16127 complaint (&symfile_complaints,
16128 _("mangled .debug_line section"));
16129 return;
16130 }
c906108c
SS
16131 break;
16132 case DW_LNS_copy:
59205f5a 16133 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16134 dwarf2_debug_line_missing_file_complaint ();
16135 else
366da635 16136 {
25e43795 16137 lh->file_names[file - 1].included_p = 1;
ca5f395d 16138 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
16139 {
16140 if (last_subfile != current_subfile)
16141 {
16142 addr = gdbarch_addr_bits_remove (gdbarch, address);
16143 if (last_subfile)
c91513d8 16144 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
16145 last_subfile = current_subfile;
16146 }
7019d805 16147 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16148 (*p_record_line) (current_subfile, line, addr);
fbf65064 16149 }
366da635 16150 }
c906108c
SS
16151 basic_block = 0;
16152 break;
16153 case DW_LNS_advance_pc:
2dc7f7b3
TT
16154 {
16155 CORE_ADDR adjust
16156 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16157
16158 address += (((op_index + adjust)
16159 / lh->maximum_ops_per_instruction)
16160 * lh->minimum_instruction_length);
16161 op_index = ((op_index + adjust)
16162 % lh->maximum_ops_per_instruction);
16163 line_ptr += bytes_read;
16164 }
c906108c
SS
16165 break;
16166 case DW_LNS_advance_line:
16167 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16168 line_ptr += bytes_read;
16169 break;
16170 case DW_LNS_set_file:
debd256d 16171 {
a738430d
MK
16172 /* The arrays lh->include_dirs and lh->file_names are
16173 0-based, but the directory and file name numbers in
16174 the statement program are 1-based. */
debd256d 16175 struct file_entry *fe;
d521ce57 16176 const char *dir = NULL;
a738430d 16177
debd256d
JB
16178 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16179 line_ptr += bytes_read;
59205f5a 16180 if (lh->num_file_names < file || file == 0)
25e43795
DJ
16181 dwarf2_debug_line_missing_file_complaint ();
16182 else
16183 {
16184 fe = &lh->file_names[file - 1];
16185 if (fe->dir_index)
16186 dir = lh->include_dirs[fe->dir_index - 1];
16187 if (!decode_for_pst_p)
16188 {
16189 last_subfile = current_subfile;
16190 dwarf2_start_subfile (fe->name, dir, comp_dir);
16191 }
16192 }
debd256d 16193 }
c906108c
SS
16194 break;
16195 case DW_LNS_set_column:
16196 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16197 line_ptr += bytes_read;
16198 break;
16199 case DW_LNS_negate_stmt:
16200 is_stmt = (!is_stmt);
16201 break;
16202 case DW_LNS_set_basic_block:
16203 basic_block = 1;
16204 break;
c2c6d25f
JM
16205 /* Add to the address register of the state machine the
16206 address increment value corresponding to special opcode
a738430d
MK
16207 255. I.e., this value is scaled by the minimum
16208 instruction length since special opcode 255 would have
b021a221 16209 scaled the increment. */
c906108c 16210 case DW_LNS_const_add_pc:
2dc7f7b3
TT
16211 {
16212 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16213
16214 address += (((op_index + adjust)
16215 / lh->maximum_ops_per_instruction)
16216 * lh->minimum_instruction_length);
16217 op_index = ((op_index + adjust)
16218 % lh->maximum_ops_per_instruction);
16219 }
c906108c
SS
16220 break;
16221 case DW_LNS_fixed_advance_pc:
16222 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 16223 op_index = 0;
c906108c
SS
16224 line_ptr += 2;
16225 break;
9aa1fe7e 16226 default:
a738430d
MK
16227 {
16228 /* Unknown standard opcode, ignore it. */
9aa1fe7e 16229 int i;
a738430d 16230
debd256d 16231 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
16232 {
16233 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16234 line_ptr += bytes_read;
16235 }
16236 }
c906108c
SS
16237 }
16238 }
59205f5a
JB
16239 if (lh->num_file_names < file || file == 0)
16240 dwarf2_debug_line_missing_file_complaint ();
16241 else
16242 {
16243 lh->file_names[file - 1].included_p = 1;
16244 if (!decode_for_pst_p)
fbf65064
UW
16245 {
16246 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 16247 (*p_record_line) (current_subfile, 0, addr);
fbf65064 16248 }
59205f5a 16249 }
c906108c 16250 }
f3f5162e
DE
16251}
16252
16253/* Decode the Line Number Program (LNP) for the given line_header
16254 structure and CU. The actual information extracted and the type
16255 of structures created from the LNP depends on the value of PST.
16256
16257 1. If PST is NULL, then this procedure uses the data from the program
16258 to create all necessary symbol tables, and their linetables.
16259
16260 2. If PST is not NULL, this procedure reads the program to determine
16261 the list of files included by the unit represented by PST, and
16262 builds all the associated partial symbol tables.
16263
16264 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16265 It is used for relative paths in the line table.
16266 NOTE: When processing partial symtabs (pst != NULL),
16267 comp_dir == pst->dirname.
16268
16269 NOTE: It is important that psymtabs have the same file name (via strcmp)
16270 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16271 symtab we don't use it in the name of the psymtabs we create.
16272 E.g. expand_line_sal requires this when finding psymtabs to expand.
16273 A good testcase for this is mb-inline.exp. */
16274
16275static void
16276dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16277 struct dwarf2_cu *cu, struct partial_symtab *pst,
16278 int want_line_info)
16279{
16280 struct objfile *objfile = cu->objfile;
16281 const int decode_for_pst_p = (pst != NULL);
16282 struct subfile *first_subfile = current_subfile;
16283
16284 if (want_line_info)
16285 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
16286
16287 if (decode_for_pst_p)
16288 {
16289 int file_index;
16290
16291 /* Now that we're done scanning the Line Header Program, we can
16292 create the psymtab of each included file. */
16293 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16294 if (lh->file_names[file_index].included_p == 1)
16295 {
d521ce57 16296 const char *include_name =
c6da4cef
DE
16297 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16298 if (include_name != NULL)
aaa75496
JB
16299 dwarf2_create_include_psymtab (include_name, pst, objfile);
16300 }
16301 }
cb1df416
DJ
16302 else
16303 {
16304 /* Make sure a symtab is created for every file, even files
16305 which contain only variables (i.e. no code with associated
16306 line numbers). */
cb1df416 16307 int i;
cb1df416
DJ
16308
16309 for (i = 0; i < lh->num_file_names; i++)
16310 {
d521ce57 16311 const char *dir = NULL;
f3f5162e 16312 struct file_entry *fe;
9a619af0 16313
cb1df416
DJ
16314 fe = &lh->file_names[i];
16315 if (fe->dir_index)
16316 dir = lh->include_dirs[fe->dir_index - 1];
16317 dwarf2_start_subfile (fe->name, dir, comp_dir);
16318
16319 /* Skip the main file; we don't need it, and it must be
16320 allocated last, so that it will show up before the
16321 non-primary symtabs in the objfile's symtab list. */
16322 if (current_subfile == first_subfile)
16323 continue;
16324
16325 if (current_subfile->symtab == NULL)
16326 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 16327 objfile);
cb1df416
DJ
16328 fe->symtab = current_subfile->symtab;
16329 }
16330 }
c906108c
SS
16331}
16332
16333/* Start a subfile for DWARF. FILENAME is the name of the file and
16334 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
16335 or NULL if not known. COMP_DIR is the compilation directory for the
16336 linetable's compilation unit or NULL if not known.
c906108c
SS
16337 This routine tries to keep line numbers from identical absolute and
16338 relative file names in a common subfile.
16339
16340 Using the `list' example from the GDB testsuite, which resides in
16341 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16342 of /srcdir/list0.c yields the following debugging information for list0.c:
16343
c5aa993b
JM
16344 DW_AT_name: /srcdir/list0.c
16345 DW_AT_comp_dir: /compdir
357e46e7 16346 files.files[0].name: list0.h
c5aa993b 16347 files.files[0].dir: /srcdir
357e46e7 16348 files.files[1].name: list0.c
c5aa993b 16349 files.files[1].dir: /srcdir
c906108c
SS
16350
16351 The line number information for list0.c has to end up in a single
4f1520fb
FR
16352 subfile, so that `break /srcdir/list0.c:1' works as expected.
16353 start_subfile will ensure that this happens provided that we pass the
16354 concatenation of files.files[1].dir and files.files[1].name as the
16355 subfile's name. */
c906108c
SS
16356
16357static void
d521ce57 16358dwarf2_start_subfile (const char *filename, const char *dirname,
3e43a32a 16359 const char *comp_dir)
c906108c 16360{
d521ce57 16361 char *copy = NULL;
4f1520fb
FR
16362
16363 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16364 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16365 second argument to start_subfile. To be consistent, we do the
16366 same here. In order not to lose the line information directory,
16367 we concatenate it to the filename when it makes sense.
16368 Note that the Dwarf3 standard says (speaking of filenames in line
16369 information): ``The directory index is ignored for file names
16370 that represent full path names''. Thus ignoring dirname in the
16371 `else' branch below isn't an issue. */
c906108c 16372
d5166ae1 16373 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
16374 {
16375 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16376 filename = copy;
16377 }
c906108c 16378
d521ce57 16379 start_subfile (filename, comp_dir);
4f1520fb 16380
d521ce57
TT
16381 if (copy != NULL)
16382 xfree (copy);
c906108c
SS
16383}
16384
f4dc4d17
DE
16385/* Start a symtab for DWARF.
16386 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16387
16388static void
16389dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 16390 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17
DE
16391{
16392 start_symtab (name, comp_dir, low_pc);
16393 record_debugformat ("DWARF 2");
16394 record_producer (cu->producer);
16395
16396 /* We assume that we're processing GCC output. */
16397 processing_gcc_compilation = 2;
16398
4d4ec4e5 16399 cu->processing_has_namespace_info = 0;
f4dc4d17
DE
16400}
16401
4c2df51b
DJ
16402static void
16403var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 16404 struct dwarf2_cu *cu)
4c2df51b 16405{
e7c27a73
DJ
16406 struct objfile *objfile = cu->objfile;
16407 struct comp_unit_head *cu_header = &cu->header;
16408
4c2df51b
DJ
16409 /* NOTE drow/2003-01-30: There used to be a comment and some special
16410 code here to turn a symbol with DW_AT_external and a
16411 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16412 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16413 with some versions of binutils) where shared libraries could have
16414 relocations against symbols in their debug information - the
16415 minimal symbol would have the right address, but the debug info
16416 would not. It's no longer necessary, because we will explicitly
16417 apply relocations when we read in the debug information now. */
16418
16419 /* A DW_AT_location attribute with no contents indicates that a
16420 variable has been optimized away. */
16421 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16422 {
f1e6e072 16423 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
16424 return;
16425 }
16426
16427 /* Handle one degenerate form of location expression specially, to
16428 preserve GDB's previous behavior when section offsets are
3019eac3
DE
16429 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16430 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
16431
16432 if (attr_form_is_block (attr)
3019eac3
DE
16433 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16434 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16435 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16436 && (DW_BLOCK (attr)->size
16437 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 16438 {
891d2f0b 16439 unsigned int dummy;
4c2df51b 16440
3019eac3
DE
16441 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16442 SYMBOL_VALUE_ADDRESS (sym) =
16443 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16444 else
16445 SYMBOL_VALUE_ADDRESS (sym) =
16446 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 16447 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
16448 fixup_symbol_section (sym, objfile);
16449 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16450 SYMBOL_SECTION (sym));
4c2df51b
DJ
16451 return;
16452 }
16453
16454 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16455 expression evaluator, and use LOC_COMPUTED only when necessary
16456 (i.e. when the value of a register or memory location is
16457 referenced, or a thread-local block, etc.). Then again, it might
16458 not be worthwhile. I'm assuming that it isn't unless performance
16459 or memory numbers show me otherwise. */
16460
f1e6e072 16461 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 16462
f1e6e072 16463 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 16464 cu->has_loclist = 1;
4c2df51b
DJ
16465}
16466
c906108c
SS
16467/* Given a pointer to a DWARF information entry, figure out if we need
16468 to make a symbol table entry for it, and if so, create a new entry
16469 and return a pointer to it.
16470 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
16471 used the passed type.
16472 If SPACE is not NULL, use it to hold the new symbol. If it is
16473 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
16474
16475static struct symbol *
34eaf542
TT
16476new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16477 struct symbol *space)
c906108c 16478{
e7c27a73 16479 struct objfile *objfile = cu->objfile;
c906108c 16480 struct symbol *sym = NULL;
15d034d0 16481 const char *name;
c906108c
SS
16482 struct attribute *attr = NULL;
16483 struct attribute *attr2 = NULL;
e142c38c 16484 CORE_ADDR baseaddr;
e37fd15a
SW
16485 struct pending **list_to_add = NULL;
16486
edb3359d 16487 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
16488
16489 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 16490
94af9270 16491 name = dwarf2_name (die, cu);
c906108c
SS
16492 if (name)
16493 {
94af9270 16494 const char *linkagename;
34eaf542 16495 int suppress_add = 0;
94af9270 16496
34eaf542
TT
16497 if (space)
16498 sym = space;
16499 else
e623cf5d 16500 sym = allocate_symbol (objfile);
c906108c 16501 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
16502
16503 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 16504 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
16505 linkagename = dwarf2_physname (name, die, cu);
16506 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 16507
f55ee35c
JK
16508 /* Fortran does not have mangling standard and the mangling does differ
16509 between gfortran, iFort etc. */
16510 if (cu->language == language_fortran
b250c185 16511 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 16512 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 16513 dwarf2_full_name (name, die, cu),
29df156d 16514 NULL);
f55ee35c 16515
c906108c 16516 /* Default assumptions.
c5aa993b 16517 Use the passed type or decode it from the die. */
176620f1 16518 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 16519 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
16520 if (type != NULL)
16521 SYMBOL_TYPE (sym) = type;
16522 else
e7c27a73 16523 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
16524 attr = dwarf2_attr (die,
16525 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16526 cu);
c906108c
SS
16527 if (attr)
16528 {
16529 SYMBOL_LINE (sym) = DW_UNSND (attr);
16530 }
cb1df416 16531
edb3359d
DJ
16532 attr = dwarf2_attr (die,
16533 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16534 cu);
cb1df416
DJ
16535 if (attr)
16536 {
16537 int file_index = DW_UNSND (attr);
9a619af0 16538
cb1df416
DJ
16539 if (cu->line_header == NULL
16540 || file_index > cu->line_header->num_file_names)
16541 complaint (&symfile_complaints,
16542 _("file index out of range"));
1c3d648d 16543 else if (file_index > 0)
cb1df416
DJ
16544 {
16545 struct file_entry *fe;
9a619af0 16546
cb1df416
DJ
16547 fe = &cu->line_header->file_names[file_index - 1];
16548 SYMBOL_SYMTAB (sym) = fe->symtab;
16549 }
16550 }
16551
c906108c
SS
16552 switch (die->tag)
16553 {
16554 case DW_TAG_label:
e142c38c 16555 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
16556 if (attr)
16557 {
16558 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16559 }
0f5238ed
TT
16560 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16561 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 16562 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 16563 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
16564 break;
16565 case DW_TAG_subprogram:
16566 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16567 finish_block. */
f1e6e072 16568 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 16569 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
16570 if ((attr2 && (DW_UNSND (attr2) != 0))
16571 || cu->language == language_ada)
c906108c 16572 {
2cfa0c8d
JB
16573 /* Subprograms marked external are stored as a global symbol.
16574 Ada subprograms, whether marked external or not, are always
16575 stored as a global symbol, because we want to be able to
16576 access them globally. For instance, we want to be able
16577 to break on a nested subprogram without having to
16578 specify the context. */
e37fd15a 16579 list_to_add = &global_symbols;
c906108c
SS
16580 }
16581 else
16582 {
e37fd15a 16583 list_to_add = cu->list_in_scope;
c906108c
SS
16584 }
16585 break;
edb3359d
DJ
16586 case DW_TAG_inlined_subroutine:
16587 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16588 finish_block. */
f1e6e072 16589 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 16590 SYMBOL_INLINED (sym) = 1;
481860b3 16591 list_to_add = cu->list_in_scope;
edb3359d 16592 break;
34eaf542
TT
16593 case DW_TAG_template_value_param:
16594 suppress_add = 1;
16595 /* Fall through. */
72929c62 16596 case DW_TAG_constant:
c906108c 16597 case DW_TAG_variable:
254e6b9e 16598 case DW_TAG_member:
0963b4bd
MS
16599 /* Compilation with minimal debug info may result in
16600 variables with missing type entries. Change the
16601 misleading `void' type to something sensible. */
c906108c 16602 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 16603 SYMBOL_TYPE (sym)
46bf5051 16604 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 16605
e142c38c 16606 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
16607 /* In the case of DW_TAG_member, we should only be called for
16608 static const members. */
16609 if (die->tag == DW_TAG_member)
16610 {
3863f96c
DE
16611 /* dwarf2_add_field uses die_is_declaration,
16612 so we do the same. */
254e6b9e
DE
16613 gdb_assert (die_is_declaration (die, cu));
16614 gdb_assert (attr);
16615 }
c906108c
SS
16616 if (attr)
16617 {
e7c27a73 16618 dwarf2_const_value (attr, sym, cu);
e142c38c 16619 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 16620 if (!suppress_add)
34eaf542
TT
16621 {
16622 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 16623 list_to_add = &global_symbols;
34eaf542 16624 else
e37fd15a 16625 list_to_add = cu->list_in_scope;
34eaf542 16626 }
c906108c
SS
16627 break;
16628 }
e142c38c 16629 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16630 if (attr)
16631 {
e7c27a73 16632 var_decode_location (attr, sym, cu);
e142c38c 16633 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
16634
16635 /* Fortran explicitly imports any global symbols to the local
16636 scope by DW_TAG_common_block. */
16637 if (cu->language == language_fortran && die->parent
16638 && die->parent->tag == DW_TAG_common_block)
16639 attr2 = NULL;
16640
caac4577
JG
16641 if (SYMBOL_CLASS (sym) == LOC_STATIC
16642 && SYMBOL_VALUE_ADDRESS (sym) == 0
16643 && !dwarf2_per_objfile->has_section_at_zero)
16644 {
16645 /* When a static variable is eliminated by the linker,
16646 the corresponding debug information is not stripped
16647 out, but the variable address is set to null;
16648 do not add such variables into symbol table. */
16649 }
16650 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 16651 {
f55ee35c
JK
16652 /* Workaround gfortran PR debug/40040 - it uses
16653 DW_AT_location for variables in -fPIC libraries which may
16654 get overriden by other libraries/executable and get
16655 a different address. Resolve it by the minimal symbol
16656 which may come from inferior's executable using copy
16657 relocation. Make this workaround only for gfortran as for
16658 other compilers GDB cannot guess the minimal symbol
16659 Fortran mangling kind. */
16660 if (cu->language == language_fortran && die->parent
16661 && die->parent->tag == DW_TAG_module
16662 && cu->producer
16663 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
f1e6e072 16664 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 16665
1c809c68
TT
16666 /* A variable with DW_AT_external is never static,
16667 but it may be block-scoped. */
16668 list_to_add = (cu->list_in_scope == &file_symbols
16669 ? &global_symbols : cu->list_in_scope);
1c809c68 16670 }
c906108c 16671 else
e37fd15a 16672 list_to_add = cu->list_in_scope;
c906108c
SS
16673 }
16674 else
16675 {
16676 /* We do not know the address of this symbol.
c5aa993b
JM
16677 If it is an external symbol and we have type information
16678 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16679 The address of the variable will then be determined from
16680 the minimal symbol table whenever the variable is
16681 referenced. */
e142c38c 16682 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
16683
16684 /* Fortran explicitly imports any global symbols to the local
16685 scope by DW_TAG_common_block. */
16686 if (cu->language == language_fortran && die->parent
16687 && die->parent->tag == DW_TAG_common_block)
16688 {
16689 /* SYMBOL_CLASS doesn't matter here because
16690 read_common_block is going to reset it. */
16691 if (!suppress_add)
16692 list_to_add = cu->list_in_scope;
16693 }
16694 else if (attr2 && (DW_UNSND (attr2) != 0)
16695 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 16696 {
0fe7935b
DJ
16697 /* A variable with DW_AT_external is never static, but it
16698 may be block-scoped. */
16699 list_to_add = (cu->list_in_scope == &file_symbols
16700 ? &global_symbols : cu->list_in_scope);
16701
f1e6e072 16702 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 16703 }
442ddf59
JK
16704 else if (!die_is_declaration (die, cu))
16705 {
16706 /* Use the default LOC_OPTIMIZED_OUT class. */
16707 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
16708 if (!suppress_add)
16709 list_to_add = cu->list_in_scope;
442ddf59 16710 }
c906108c
SS
16711 }
16712 break;
16713 case DW_TAG_formal_parameter:
edb3359d
DJ
16714 /* If we are inside a function, mark this as an argument. If
16715 not, we might be looking at an argument to an inlined function
16716 when we do not have enough information to show inlined frames;
16717 pretend it's a local variable in that case so that the user can
16718 still see it. */
16719 if (context_stack_depth > 0
16720 && context_stack[context_stack_depth - 1].name != NULL)
16721 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 16722 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
16723 if (attr)
16724 {
e7c27a73 16725 var_decode_location (attr, sym, cu);
c906108c 16726 }
e142c38c 16727 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16728 if (attr)
16729 {
e7c27a73 16730 dwarf2_const_value (attr, sym, cu);
c906108c 16731 }
f346a30d 16732
e37fd15a 16733 list_to_add = cu->list_in_scope;
c906108c
SS
16734 break;
16735 case DW_TAG_unspecified_parameters:
16736 /* From varargs functions; gdb doesn't seem to have any
16737 interest in this information, so just ignore it for now.
16738 (FIXME?) */
16739 break;
34eaf542
TT
16740 case DW_TAG_template_type_param:
16741 suppress_add = 1;
16742 /* Fall through. */
c906108c 16743 case DW_TAG_class_type:
680b30c7 16744 case DW_TAG_interface_type:
c906108c
SS
16745 case DW_TAG_structure_type:
16746 case DW_TAG_union_type:
72019c9c 16747 case DW_TAG_set_type:
c906108c 16748 case DW_TAG_enumeration_type:
f1e6e072 16749 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16750 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 16751
63d06c5c 16752 {
987504bb 16753 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
16754 really ever be static objects: otherwise, if you try
16755 to, say, break of a class's method and you're in a file
16756 which doesn't mention that class, it won't work unless
16757 the check for all static symbols in lookup_symbol_aux
16758 saves you. See the OtherFileClass tests in
16759 gdb.c++/namespace.exp. */
16760
e37fd15a 16761 if (!suppress_add)
34eaf542 16762 {
34eaf542
TT
16763 list_to_add = (cu->list_in_scope == &file_symbols
16764 && (cu->language == language_cplus
16765 || cu->language == language_java)
16766 ? &global_symbols : cu->list_in_scope);
63d06c5c 16767
64382290
TT
16768 /* The semantics of C++ state that "struct foo {
16769 ... }" also defines a typedef for "foo". A Java
16770 class declaration also defines a typedef for the
16771 class. */
16772 if (cu->language == language_cplus
16773 || cu->language == language_java
16774 || cu->language == language_ada)
16775 {
16776 /* The symbol's name is already allocated along
16777 with this objfile, so we don't need to
16778 duplicate it for the type. */
16779 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16780 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16781 }
63d06c5c
DC
16782 }
16783 }
c906108c
SS
16784 break;
16785 case DW_TAG_typedef:
f1e6e072 16786 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 16787 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16788 list_to_add = cu->list_in_scope;
63d06c5c 16789 break;
c906108c 16790 case DW_TAG_base_type:
a02abb62 16791 case DW_TAG_subrange_type:
f1e6e072 16792 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 16793 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 16794 list_to_add = cu->list_in_scope;
c906108c
SS
16795 break;
16796 case DW_TAG_enumerator:
e142c38c 16797 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
16798 if (attr)
16799 {
e7c27a73 16800 dwarf2_const_value (attr, sym, cu);
c906108c 16801 }
63d06c5c
DC
16802 {
16803 /* NOTE: carlton/2003-11-10: See comment above in the
16804 DW_TAG_class_type, etc. block. */
16805
e142c38c 16806 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
16807 && (cu->language == language_cplus
16808 || cu->language == language_java)
e142c38c 16809 ? &global_symbols : cu->list_in_scope);
63d06c5c 16810 }
c906108c 16811 break;
5c4e30ca 16812 case DW_TAG_namespace:
f1e6e072 16813 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 16814 list_to_add = &global_symbols;
5c4e30ca 16815 break;
4357ac6c 16816 case DW_TAG_common_block:
f1e6e072 16817 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
16818 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16819 add_symbol_to_list (sym, cu->list_in_scope);
16820 break;
c906108c
SS
16821 default:
16822 /* Not a tag we recognize. Hopefully we aren't processing
16823 trash data, but since we must specifically ignore things
16824 we don't recognize, there is nothing else we should do at
0963b4bd 16825 this point. */
e2e0b3e5 16826 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 16827 dwarf_tag_name (die->tag));
c906108c
SS
16828 break;
16829 }
df8a16a1 16830
e37fd15a
SW
16831 if (suppress_add)
16832 {
16833 sym->hash_next = objfile->template_symbols;
16834 objfile->template_symbols = sym;
16835 list_to_add = NULL;
16836 }
16837
16838 if (list_to_add != NULL)
16839 add_symbol_to_list (sym, list_to_add);
16840
df8a16a1
DJ
16841 /* For the benefit of old versions of GCC, check for anonymous
16842 namespaces based on the demangled name. */
4d4ec4e5 16843 if (!cu->processing_has_namespace_info
94af9270 16844 && cu->language == language_cplus)
a10964d1 16845 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
16846 }
16847 return (sym);
16848}
16849
34eaf542
TT
16850/* A wrapper for new_symbol_full that always allocates a new symbol. */
16851
16852static struct symbol *
16853new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16854{
16855 return new_symbol_full (die, type, cu, NULL);
16856}
16857
98bfdba5
PA
16858/* Given an attr with a DW_FORM_dataN value in host byte order,
16859 zero-extend it as appropriate for the symbol's type. The DWARF
16860 standard (v4) is not entirely clear about the meaning of using
16861 DW_FORM_dataN for a constant with a signed type, where the type is
16862 wider than the data. The conclusion of a discussion on the DWARF
16863 list was that this is unspecified. We choose to always zero-extend
16864 because that is the interpretation long in use by GCC. */
c906108c 16865
98bfdba5 16866static gdb_byte *
ff39bb5e 16867dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 16868 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 16869{
e7c27a73 16870 struct objfile *objfile = cu->objfile;
e17a4113
UW
16871 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16872 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
16873 LONGEST l = DW_UNSND (attr);
16874
16875 if (bits < sizeof (*value) * 8)
16876 {
16877 l &= ((LONGEST) 1 << bits) - 1;
16878 *value = l;
16879 }
16880 else if (bits == sizeof (*value) * 8)
16881 *value = l;
16882 else
16883 {
16884 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16885 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16886 return bytes;
16887 }
16888
16889 return NULL;
16890}
16891
16892/* Read a constant value from an attribute. Either set *VALUE, or if
16893 the value does not fit in *VALUE, set *BYTES - either already
16894 allocated on the objfile obstack, or newly allocated on OBSTACK,
16895 or, set *BATON, if we translated the constant to a location
16896 expression. */
16897
16898static void
ff39bb5e 16899dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
16900 const char *name, struct obstack *obstack,
16901 struct dwarf2_cu *cu,
d521ce57 16902 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
16903 struct dwarf2_locexpr_baton **baton)
16904{
16905 struct objfile *objfile = cu->objfile;
16906 struct comp_unit_head *cu_header = &cu->header;
c906108c 16907 struct dwarf_block *blk;
98bfdba5
PA
16908 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16909 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16910
16911 *value = 0;
16912 *bytes = NULL;
16913 *baton = NULL;
c906108c
SS
16914
16915 switch (attr->form)
16916 {
16917 case DW_FORM_addr:
3019eac3 16918 case DW_FORM_GNU_addr_index:
ac56253d 16919 {
ac56253d
TT
16920 gdb_byte *data;
16921
98bfdba5
PA
16922 if (TYPE_LENGTH (type) != cu_header->addr_size)
16923 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 16924 cu_header->addr_size,
98bfdba5 16925 TYPE_LENGTH (type));
ac56253d
TT
16926 /* Symbols of this form are reasonably rare, so we just
16927 piggyback on the existing location code rather than writing
16928 a new implementation of symbol_computed_ops. */
7919a973 16929 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
98bfdba5
PA
16930 (*baton)->per_cu = cu->per_cu;
16931 gdb_assert ((*baton)->per_cu);
ac56253d 16932
98bfdba5 16933 (*baton)->size = 2 + cu_header->addr_size;
7919a973 16934 data = obstack_alloc (obstack, (*baton)->size);
98bfdba5 16935 (*baton)->data = data;
ac56253d
TT
16936
16937 data[0] = DW_OP_addr;
16938 store_unsigned_integer (&data[1], cu_header->addr_size,
16939 byte_order, DW_ADDR (attr));
16940 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 16941 }
c906108c 16942 break;
4ac36638 16943 case DW_FORM_string:
93b5768b 16944 case DW_FORM_strp:
3019eac3 16945 case DW_FORM_GNU_str_index:
36586728 16946 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
16947 /* DW_STRING is already allocated on the objfile obstack, point
16948 directly to it. */
d521ce57 16949 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 16950 break;
c906108c
SS
16951 case DW_FORM_block1:
16952 case DW_FORM_block2:
16953 case DW_FORM_block4:
16954 case DW_FORM_block:
2dc7f7b3 16955 case DW_FORM_exprloc:
c906108c 16956 blk = DW_BLOCK (attr);
98bfdba5
PA
16957 if (TYPE_LENGTH (type) != blk->size)
16958 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16959 TYPE_LENGTH (type));
16960 *bytes = blk->data;
c906108c 16961 break;
2df3850c
JM
16962
16963 /* The DW_AT_const_value attributes are supposed to carry the
16964 symbol's value "represented as it would be on the target
16965 architecture." By the time we get here, it's already been
16966 converted to host endianness, so we just need to sign- or
16967 zero-extend it as appropriate. */
16968 case DW_FORM_data1:
3aef2284 16969 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 16970 break;
c906108c 16971 case DW_FORM_data2:
3aef2284 16972 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 16973 break;
c906108c 16974 case DW_FORM_data4:
3aef2284 16975 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 16976 break;
c906108c 16977 case DW_FORM_data8:
3aef2284 16978 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
16979 break;
16980
c906108c 16981 case DW_FORM_sdata:
98bfdba5 16982 *value = DW_SND (attr);
2df3850c
JM
16983 break;
16984
c906108c 16985 case DW_FORM_udata:
98bfdba5 16986 *value = DW_UNSND (attr);
c906108c 16987 break;
2df3850c 16988
c906108c 16989 default:
4d3c2250 16990 complaint (&symfile_complaints,
e2e0b3e5 16991 _("unsupported const value attribute form: '%s'"),
4d3c2250 16992 dwarf_form_name (attr->form));
98bfdba5 16993 *value = 0;
c906108c
SS
16994 break;
16995 }
16996}
16997
2df3850c 16998
98bfdba5
PA
16999/* Copy constant value from an attribute to a symbol. */
17000
2df3850c 17001static void
ff39bb5e 17002dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 17003 struct dwarf2_cu *cu)
2df3850c 17004{
98bfdba5
PA
17005 struct objfile *objfile = cu->objfile;
17006 struct comp_unit_head *cu_header = &cu->header;
12df843f 17007 LONGEST value;
d521ce57 17008 const gdb_byte *bytes;
98bfdba5 17009 struct dwarf2_locexpr_baton *baton;
2df3850c 17010
98bfdba5
PA
17011 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17012 SYMBOL_PRINT_NAME (sym),
17013 &objfile->objfile_obstack, cu,
17014 &value, &bytes, &baton);
2df3850c 17015
98bfdba5
PA
17016 if (baton != NULL)
17017 {
98bfdba5 17018 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17019 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
17020 }
17021 else if (bytes != NULL)
17022 {
17023 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 17024 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
17025 }
17026 else
17027 {
17028 SYMBOL_VALUE (sym) = value;
f1e6e072 17029 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 17030 }
2df3850c
JM
17031}
17032
c906108c
SS
17033/* Return the type of the die in question using its DW_AT_type attribute. */
17034
17035static struct type *
e7c27a73 17036die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17037{
c906108c 17038 struct attribute *type_attr;
c906108c 17039
e142c38c 17040 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
17041 if (!type_attr)
17042 {
17043 /* A missing DW_AT_type represents a void type. */
46bf5051 17044 return objfile_type (cu->objfile)->builtin_void;
c906108c 17045 }
348e048f 17046
673bfd45 17047 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17048}
17049
b4ba55a1
JB
17050/* True iff CU's producer generates GNAT Ada auxiliary information
17051 that allows to find parallel types through that information instead
17052 of having to do expensive parallel lookups by type name. */
17053
17054static int
17055need_gnat_info (struct dwarf2_cu *cu)
17056{
17057 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17058 of GNAT produces this auxiliary information, without any indication
17059 that it is produced. Part of enhancing the FSF version of GNAT
17060 to produce that information will be to put in place an indicator
17061 that we can use in order to determine whether the descriptive type
17062 info is available or not. One suggestion that has been made is
17063 to use a new attribute, attached to the CU die. For now, assume
17064 that the descriptive type info is not available. */
17065 return 0;
17066}
17067
b4ba55a1
JB
17068/* Return the auxiliary type of the die in question using its
17069 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17070 attribute is not present. */
17071
17072static struct type *
17073die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17074{
b4ba55a1 17075 struct attribute *type_attr;
b4ba55a1
JB
17076
17077 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17078 if (!type_attr)
17079 return NULL;
17080
673bfd45 17081 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
17082}
17083
17084/* If DIE has a descriptive_type attribute, then set the TYPE's
17085 descriptive type accordingly. */
17086
17087static void
17088set_descriptive_type (struct type *type, struct die_info *die,
17089 struct dwarf2_cu *cu)
17090{
17091 struct type *descriptive_type = die_descriptive_type (die, cu);
17092
17093 if (descriptive_type)
17094 {
17095 ALLOCATE_GNAT_AUX_TYPE (type);
17096 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17097 }
17098}
17099
c906108c
SS
17100/* Return the containing type of the die in question using its
17101 DW_AT_containing_type attribute. */
17102
17103static struct type *
e7c27a73 17104die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17105{
c906108c 17106 struct attribute *type_attr;
c906108c 17107
e142c38c 17108 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
17109 if (!type_attr)
17110 error (_("Dwarf Error: Problem turning containing type into gdb type "
17111 "[in module %s]"), cu->objfile->name);
17112
673bfd45 17113 return lookup_die_type (die, type_attr, cu);
c906108c
SS
17114}
17115
ac9ec31b
DE
17116/* Return an error marker type to use for the ill formed type in DIE/CU. */
17117
17118static struct type *
17119build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17120{
17121 struct objfile *objfile = dwarf2_per_objfile->objfile;
17122 char *message, *saved;
17123
17124 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17125 objfile->name,
17126 cu->header.offset.sect_off,
17127 die->offset.sect_off);
17128 saved = obstack_copy0 (&objfile->objfile_obstack,
17129 message, strlen (message));
17130 xfree (message);
17131
17132 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17133}
17134
673bfd45 17135/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
17136 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17137 DW_AT_containing_type.
673bfd45
DE
17138 If there is no type substitute an error marker. */
17139
c906108c 17140static struct type *
ff39bb5e 17141lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 17142 struct dwarf2_cu *cu)
c906108c 17143{
bb5ed363 17144 struct objfile *objfile = cu->objfile;
f792889a
DJ
17145 struct type *this_type;
17146
ac9ec31b
DE
17147 gdb_assert (attr->name == DW_AT_type
17148 || attr->name == DW_AT_GNAT_descriptive_type
17149 || attr->name == DW_AT_containing_type);
17150
673bfd45
DE
17151 /* First see if we have it cached. */
17152
36586728
TT
17153 if (attr->form == DW_FORM_GNU_ref_alt)
17154 {
17155 struct dwarf2_per_cu_data *per_cu;
17156 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17157
17158 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17159 this_type = get_die_type_at_offset (offset, per_cu);
17160 }
7771576e 17161 else if (attr_form_is_ref (attr))
673bfd45 17162 {
b64f50a1 17163 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
17164
17165 this_type = get_die_type_at_offset (offset, cu->per_cu);
17166 }
55f1336d 17167 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 17168 {
ac9ec31b 17169 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 17170
ac9ec31b 17171 return get_signatured_type (die, signature, cu);
673bfd45
DE
17172 }
17173 else
17174 {
ac9ec31b
DE
17175 complaint (&symfile_complaints,
17176 _("Dwarf Error: Bad type attribute %s in DIE"
17177 " at 0x%x [in module %s]"),
17178 dwarf_attr_name (attr->name), die->offset.sect_off,
17179 objfile->name);
17180 return build_error_marker_type (cu, die);
673bfd45
DE
17181 }
17182
17183 /* If not cached we need to read it in. */
17184
17185 if (this_type == NULL)
17186 {
ac9ec31b 17187 struct die_info *type_die = NULL;
673bfd45
DE
17188 struct dwarf2_cu *type_cu = cu;
17189
7771576e 17190 if (attr_form_is_ref (attr))
ac9ec31b
DE
17191 type_die = follow_die_ref (die, attr, &type_cu);
17192 if (type_die == NULL)
17193 return build_error_marker_type (cu, die);
17194 /* If we find the type now, it's probably because the type came
3019eac3
DE
17195 from an inter-CU reference and the type's CU got expanded before
17196 ours. */
ac9ec31b 17197 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
17198 }
17199
17200 /* If we still don't have a type use an error marker. */
17201
17202 if (this_type == NULL)
ac9ec31b 17203 return build_error_marker_type (cu, die);
673bfd45 17204
f792889a 17205 return this_type;
c906108c
SS
17206}
17207
673bfd45
DE
17208/* Return the type in DIE, CU.
17209 Returns NULL for invalid types.
17210
02142a6c 17211 This first does a lookup in die_type_hash,
673bfd45
DE
17212 and only reads the die in if necessary.
17213
17214 NOTE: This can be called when reading in partial or full symbols. */
17215
f792889a 17216static struct type *
e7c27a73 17217read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17218{
f792889a
DJ
17219 struct type *this_type;
17220
17221 this_type = get_die_type (die, cu);
17222 if (this_type)
17223 return this_type;
17224
673bfd45
DE
17225 return read_type_die_1 (die, cu);
17226}
17227
17228/* Read the type in DIE, CU.
17229 Returns NULL for invalid types. */
17230
17231static struct type *
17232read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17233{
17234 struct type *this_type = NULL;
17235
c906108c
SS
17236 switch (die->tag)
17237 {
17238 case DW_TAG_class_type:
680b30c7 17239 case DW_TAG_interface_type:
c906108c
SS
17240 case DW_TAG_structure_type:
17241 case DW_TAG_union_type:
f792889a 17242 this_type = read_structure_type (die, cu);
c906108c
SS
17243 break;
17244 case DW_TAG_enumeration_type:
f792889a 17245 this_type = read_enumeration_type (die, cu);
c906108c
SS
17246 break;
17247 case DW_TAG_subprogram:
17248 case DW_TAG_subroutine_type:
edb3359d 17249 case DW_TAG_inlined_subroutine:
f792889a 17250 this_type = read_subroutine_type (die, cu);
c906108c
SS
17251 break;
17252 case DW_TAG_array_type:
f792889a 17253 this_type = read_array_type (die, cu);
c906108c 17254 break;
72019c9c 17255 case DW_TAG_set_type:
f792889a 17256 this_type = read_set_type (die, cu);
72019c9c 17257 break;
c906108c 17258 case DW_TAG_pointer_type:
f792889a 17259 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
17260 break;
17261 case DW_TAG_ptr_to_member_type:
f792889a 17262 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
17263 break;
17264 case DW_TAG_reference_type:
f792889a 17265 this_type = read_tag_reference_type (die, cu);
c906108c
SS
17266 break;
17267 case DW_TAG_const_type:
f792889a 17268 this_type = read_tag_const_type (die, cu);
c906108c
SS
17269 break;
17270 case DW_TAG_volatile_type:
f792889a 17271 this_type = read_tag_volatile_type (die, cu);
c906108c 17272 break;
06d66ee9
TT
17273 case DW_TAG_restrict_type:
17274 this_type = read_tag_restrict_type (die, cu);
17275 break;
c906108c 17276 case DW_TAG_string_type:
f792889a 17277 this_type = read_tag_string_type (die, cu);
c906108c
SS
17278 break;
17279 case DW_TAG_typedef:
f792889a 17280 this_type = read_typedef (die, cu);
c906108c 17281 break;
a02abb62 17282 case DW_TAG_subrange_type:
f792889a 17283 this_type = read_subrange_type (die, cu);
a02abb62 17284 break;
c906108c 17285 case DW_TAG_base_type:
f792889a 17286 this_type = read_base_type (die, cu);
c906108c 17287 break;
81a17f79 17288 case DW_TAG_unspecified_type:
f792889a 17289 this_type = read_unspecified_type (die, cu);
81a17f79 17290 break;
0114d602
DJ
17291 case DW_TAG_namespace:
17292 this_type = read_namespace_type (die, cu);
17293 break;
f55ee35c
JK
17294 case DW_TAG_module:
17295 this_type = read_module_type (die, cu);
17296 break;
c906108c 17297 default:
3e43a32a
MS
17298 complaint (&symfile_complaints,
17299 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 17300 dwarf_tag_name (die->tag));
c906108c
SS
17301 break;
17302 }
63d06c5c 17303
f792889a 17304 return this_type;
63d06c5c
DC
17305}
17306
abc72ce4
DE
17307/* See if we can figure out if the class lives in a namespace. We do
17308 this by looking for a member function; its demangled name will
17309 contain namespace info, if there is any.
17310 Return the computed name or NULL.
17311 Space for the result is allocated on the objfile's obstack.
17312 This is the full-die version of guess_partial_die_structure_name.
17313 In this case we know DIE has no useful parent. */
17314
17315static char *
17316guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17317{
17318 struct die_info *spec_die;
17319 struct dwarf2_cu *spec_cu;
17320 struct die_info *child;
17321
17322 spec_cu = cu;
17323 spec_die = die_specification (die, &spec_cu);
17324 if (spec_die != NULL)
17325 {
17326 die = spec_die;
17327 cu = spec_cu;
17328 }
17329
17330 for (child = die->child;
17331 child != NULL;
17332 child = child->sibling)
17333 {
17334 if (child->tag == DW_TAG_subprogram)
17335 {
17336 struct attribute *attr;
17337
17338 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17339 if (attr == NULL)
17340 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17341 if (attr != NULL)
17342 {
17343 char *actual_name
17344 = language_class_name_from_physname (cu->language_defn,
17345 DW_STRING (attr));
17346 char *name = NULL;
17347
17348 if (actual_name != NULL)
17349 {
15d034d0 17350 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
17351
17352 if (die_name != NULL
17353 && strcmp (die_name, actual_name) != 0)
17354 {
17355 /* Strip off the class name from the full name.
17356 We want the prefix. */
17357 int die_name_len = strlen (die_name);
17358 int actual_name_len = strlen (actual_name);
17359
17360 /* Test for '::' as a sanity check. */
17361 if (actual_name_len > die_name_len + 2
3e43a32a
MS
17362 && actual_name[actual_name_len
17363 - die_name_len - 1] == ':')
abc72ce4 17364 name =
10f0c4bb
TT
17365 obstack_copy0 (&cu->objfile->objfile_obstack,
17366 actual_name,
17367 actual_name_len - die_name_len - 2);
abc72ce4
DE
17368 }
17369 }
17370 xfree (actual_name);
17371 return name;
17372 }
17373 }
17374 }
17375
17376 return NULL;
17377}
17378
96408a79
SA
17379/* GCC might emit a nameless typedef that has a linkage name. Determine the
17380 prefix part in such case. See
17381 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17382
17383static char *
17384anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17385{
17386 struct attribute *attr;
17387 char *base;
17388
17389 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17390 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17391 return NULL;
17392
17393 attr = dwarf2_attr (die, DW_AT_name, cu);
17394 if (attr != NULL && DW_STRING (attr) != NULL)
17395 return NULL;
17396
17397 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17398 if (attr == NULL)
17399 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17400 if (attr == NULL || DW_STRING (attr) == NULL)
17401 return NULL;
17402
17403 /* dwarf2_name had to be already called. */
17404 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17405
17406 /* Strip the base name, keep any leading namespaces/classes. */
17407 base = strrchr (DW_STRING (attr), ':');
17408 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17409 return "";
17410
10f0c4bb
TT
17411 return obstack_copy0 (&cu->objfile->objfile_obstack,
17412 DW_STRING (attr), &base[-1] - DW_STRING (attr));
96408a79
SA
17413}
17414
fdde2d81 17415/* Return the name of the namespace/class that DIE is defined within,
0114d602 17416 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 17417
0114d602
DJ
17418 For example, if we're within the method foo() in the following
17419 code:
17420
17421 namespace N {
17422 class C {
17423 void foo () {
17424 }
17425 };
17426 }
17427
17428 then determine_prefix on foo's die will return "N::C". */
fdde2d81 17429
0d5cff50 17430static const char *
e142c38c 17431determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 17432{
0114d602
DJ
17433 struct die_info *parent, *spec_die;
17434 struct dwarf2_cu *spec_cu;
17435 struct type *parent_type;
96408a79 17436 char *retval;
63d06c5c 17437
f55ee35c
JK
17438 if (cu->language != language_cplus && cu->language != language_java
17439 && cu->language != language_fortran)
0114d602
DJ
17440 return "";
17441
96408a79
SA
17442 retval = anonymous_struct_prefix (die, cu);
17443 if (retval)
17444 return retval;
17445
0114d602
DJ
17446 /* We have to be careful in the presence of DW_AT_specification.
17447 For example, with GCC 3.4, given the code
17448
17449 namespace N {
17450 void foo() {
17451 // Definition of N::foo.
17452 }
17453 }
17454
17455 then we'll have a tree of DIEs like this:
17456
17457 1: DW_TAG_compile_unit
17458 2: DW_TAG_namespace // N
17459 3: DW_TAG_subprogram // declaration of N::foo
17460 4: DW_TAG_subprogram // definition of N::foo
17461 DW_AT_specification // refers to die #3
17462
17463 Thus, when processing die #4, we have to pretend that we're in
17464 the context of its DW_AT_specification, namely the contex of die
17465 #3. */
17466 spec_cu = cu;
17467 spec_die = die_specification (die, &spec_cu);
17468 if (spec_die == NULL)
17469 parent = die->parent;
17470 else
63d06c5c 17471 {
0114d602
DJ
17472 parent = spec_die->parent;
17473 cu = spec_cu;
63d06c5c 17474 }
0114d602
DJ
17475
17476 if (parent == NULL)
17477 return "";
98bfdba5
PA
17478 else if (parent->building_fullname)
17479 {
17480 const char *name;
17481 const char *parent_name;
17482
17483 /* It has been seen on RealView 2.2 built binaries,
17484 DW_TAG_template_type_param types actually _defined_ as
17485 children of the parent class:
17486
17487 enum E {};
17488 template class <class Enum> Class{};
17489 Class<enum E> class_e;
17490
17491 1: DW_TAG_class_type (Class)
17492 2: DW_TAG_enumeration_type (E)
17493 3: DW_TAG_enumerator (enum1:0)
17494 3: DW_TAG_enumerator (enum2:1)
17495 ...
17496 2: DW_TAG_template_type_param
17497 DW_AT_type DW_FORM_ref_udata (E)
17498
17499 Besides being broken debug info, it can put GDB into an
17500 infinite loop. Consider:
17501
17502 When we're building the full name for Class<E>, we'll start
17503 at Class, and go look over its template type parameters,
17504 finding E. We'll then try to build the full name of E, and
17505 reach here. We're now trying to build the full name of E,
17506 and look over the parent DIE for containing scope. In the
17507 broken case, if we followed the parent DIE of E, we'd again
17508 find Class, and once again go look at its template type
17509 arguments, etc., etc. Simply don't consider such parent die
17510 as source-level parent of this die (it can't be, the language
17511 doesn't allow it), and break the loop here. */
17512 name = dwarf2_name (die, cu);
17513 parent_name = dwarf2_name (parent, cu);
17514 complaint (&symfile_complaints,
17515 _("template param type '%s' defined within parent '%s'"),
17516 name ? name : "<unknown>",
17517 parent_name ? parent_name : "<unknown>");
17518 return "";
17519 }
63d06c5c 17520 else
0114d602
DJ
17521 switch (parent->tag)
17522 {
63d06c5c 17523 case DW_TAG_namespace:
0114d602 17524 parent_type = read_type_die (parent, cu);
acebe513
UW
17525 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17526 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17527 Work around this problem here. */
17528 if (cu->language == language_cplus
17529 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17530 return "";
0114d602
DJ
17531 /* We give a name to even anonymous namespaces. */
17532 return TYPE_TAG_NAME (parent_type);
63d06c5c 17533 case DW_TAG_class_type:
680b30c7 17534 case DW_TAG_interface_type:
63d06c5c 17535 case DW_TAG_structure_type:
0114d602 17536 case DW_TAG_union_type:
f55ee35c 17537 case DW_TAG_module:
0114d602
DJ
17538 parent_type = read_type_die (parent, cu);
17539 if (TYPE_TAG_NAME (parent_type) != NULL)
17540 return TYPE_TAG_NAME (parent_type);
17541 else
17542 /* An anonymous structure is only allowed non-static data
17543 members; no typedefs, no member functions, et cetera.
17544 So it does not need a prefix. */
17545 return "";
abc72ce4 17546 case DW_TAG_compile_unit:
95554aad 17547 case DW_TAG_partial_unit:
abc72ce4
DE
17548 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17549 if (cu->language == language_cplus
8b70b953 17550 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
17551 && die->child != NULL
17552 && (die->tag == DW_TAG_class_type
17553 || die->tag == DW_TAG_structure_type
17554 || die->tag == DW_TAG_union_type))
17555 {
17556 char *name = guess_full_die_structure_name (die, cu);
17557 if (name != NULL)
17558 return name;
17559 }
17560 return "";
63d06c5c 17561 default:
8176b9b8 17562 return determine_prefix (parent, cu);
63d06c5c 17563 }
63d06c5c
DC
17564}
17565
3e43a32a
MS
17566/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17567 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17568 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17569 an obconcat, otherwise allocate storage for the result. The CU argument is
17570 used to determine the language and hence, the appropriate separator. */
987504bb 17571
f55ee35c 17572#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
17573
17574static char *
f55ee35c
JK
17575typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17576 int physname, struct dwarf2_cu *cu)
63d06c5c 17577{
f55ee35c 17578 const char *lead = "";
5c315b68 17579 const char *sep;
63d06c5c 17580
3e43a32a
MS
17581 if (suffix == NULL || suffix[0] == '\0'
17582 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
17583 sep = "";
17584 else if (cu->language == language_java)
17585 sep = ".";
f55ee35c
JK
17586 else if (cu->language == language_fortran && physname)
17587 {
17588 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17589 DW_AT_MIPS_linkage_name is preferred and used instead. */
17590
17591 lead = "__";
17592 sep = "_MOD_";
17593 }
987504bb
JJ
17594 else
17595 sep = "::";
63d06c5c 17596
6dd47d34
DE
17597 if (prefix == NULL)
17598 prefix = "";
17599 if (suffix == NULL)
17600 suffix = "";
17601
987504bb
JJ
17602 if (obs == NULL)
17603 {
3e43a32a
MS
17604 char *retval
17605 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 17606
f55ee35c
JK
17607 strcpy (retval, lead);
17608 strcat (retval, prefix);
6dd47d34
DE
17609 strcat (retval, sep);
17610 strcat (retval, suffix);
63d06c5c
DC
17611 return retval;
17612 }
987504bb
JJ
17613 else
17614 {
17615 /* We have an obstack. */
f55ee35c 17616 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 17617 }
63d06c5c
DC
17618}
17619
c906108c
SS
17620/* Return sibling of die, NULL if no sibling. */
17621
f9aca02d 17622static struct die_info *
fba45db2 17623sibling_die (struct die_info *die)
c906108c 17624{
639d11d3 17625 return die->sibling;
c906108c
SS
17626}
17627
71c25dea
TT
17628/* Get name of a die, return NULL if not found. */
17629
15d034d0
TT
17630static const char *
17631dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
17632 struct obstack *obstack)
17633{
17634 if (name && cu->language == language_cplus)
17635 {
17636 char *canon_name = cp_canonicalize_string (name);
17637
17638 if (canon_name != NULL)
17639 {
17640 if (strcmp (canon_name, name) != 0)
10f0c4bb 17641 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
71c25dea
TT
17642 xfree (canon_name);
17643 }
17644 }
17645
17646 return name;
c906108c
SS
17647}
17648
9219021c
DC
17649/* Get name of a die, return NULL if not found. */
17650
15d034d0 17651static const char *
e142c38c 17652dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
17653{
17654 struct attribute *attr;
17655
e142c38c 17656 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
17657 if ((!attr || !DW_STRING (attr))
17658 && die->tag != DW_TAG_class_type
17659 && die->tag != DW_TAG_interface_type
17660 && die->tag != DW_TAG_structure_type
17661 && die->tag != DW_TAG_union_type)
71c25dea
TT
17662 return NULL;
17663
17664 switch (die->tag)
17665 {
17666 case DW_TAG_compile_unit:
95554aad 17667 case DW_TAG_partial_unit:
71c25dea
TT
17668 /* Compilation units have a DW_AT_name that is a filename, not
17669 a source language identifier. */
17670 case DW_TAG_enumeration_type:
17671 case DW_TAG_enumerator:
17672 /* These tags always have simple identifiers already; no need
17673 to canonicalize them. */
17674 return DW_STRING (attr);
907af001 17675
418835cc
KS
17676 case DW_TAG_subprogram:
17677 /* Java constructors will all be named "<init>", so return
17678 the class name when we see this special case. */
17679 if (cu->language == language_java
17680 && DW_STRING (attr) != NULL
17681 && strcmp (DW_STRING (attr), "<init>") == 0)
17682 {
17683 struct dwarf2_cu *spec_cu = cu;
17684 struct die_info *spec_die;
17685
17686 /* GCJ will output '<init>' for Java constructor names.
17687 For this special case, return the name of the parent class. */
17688
17689 /* GCJ may output suprogram DIEs with AT_specification set.
17690 If so, use the name of the specified DIE. */
17691 spec_die = die_specification (die, &spec_cu);
17692 if (spec_die != NULL)
17693 return dwarf2_name (spec_die, spec_cu);
17694
17695 do
17696 {
17697 die = die->parent;
17698 if (die->tag == DW_TAG_class_type)
17699 return dwarf2_name (die, cu);
17700 }
95554aad
TT
17701 while (die->tag != DW_TAG_compile_unit
17702 && die->tag != DW_TAG_partial_unit);
418835cc 17703 }
907af001
UW
17704 break;
17705
17706 case DW_TAG_class_type:
17707 case DW_TAG_interface_type:
17708 case DW_TAG_structure_type:
17709 case DW_TAG_union_type:
17710 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17711 structures or unions. These were of the form "._%d" in GCC 4.1,
17712 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17713 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
17714 if (attr && DW_STRING (attr)
17715 && (strncmp (DW_STRING (attr), "._", 2) == 0
17716 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 17717 return NULL;
53832f31
TT
17718
17719 /* GCC might emit a nameless typedef that has a linkage name. See
17720 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17721 if (!attr || DW_STRING (attr) == NULL)
17722 {
df5c6c50 17723 char *demangled = NULL;
53832f31
TT
17724
17725 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17726 if (attr == NULL)
17727 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17728
17729 if (attr == NULL || DW_STRING (attr) == NULL)
17730 return NULL;
17731
df5c6c50
JK
17732 /* Avoid demangling DW_STRING (attr) the second time on a second
17733 call for the same DIE. */
17734 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 17735 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
17736
17737 if (demangled)
17738 {
96408a79
SA
17739 char *base;
17740
53832f31 17741 /* FIXME: we already did this for the partial symbol... */
10f0c4bb
TT
17742 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17743 demangled, strlen (demangled));
53832f31
TT
17744 DW_STRING_IS_CANONICAL (attr) = 1;
17745 xfree (demangled);
96408a79
SA
17746
17747 /* Strip any leading namespaces/classes, keep only the base name.
17748 DW_AT_name for named DIEs does not contain the prefixes. */
17749 base = strrchr (DW_STRING (attr), ':');
17750 if (base && base > DW_STRING (attr) && base[-1] == ':')
17751 return &base[1];
17752 else
17753 return DW_STRING (attr);
53832f31
TT
17754 }
17755 }
907af001
UW
17756 break;
17757
71c25dea 17758 default:
907af001
UW
17759 break;
17760 }
17761
17762 if (!DW_STRING_IS_CANONICAL (attr))
17763 {
17764 DW_STRING (attr)
17765 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17766 &cu->objfile->objfile_obstack);
17767 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 17768 }
907af001 17769 return DW_STRING (attr);
9219021c
DC
17770}
17771
17772/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
17773 is none. *EXT_CU is the CU containing DIE on input, and the CU
17774 containing the return value on output. */
9219021c
DC
17775
17776static struct die_info *
f2f0e013 17777dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
17778{
17779 struct attribute *attr;
9219021c 17780
f2f0e013 17781 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
17782 if (attr == NULL)
17783 return NULL;
17784
f2f0e013 17785 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
17786}
17787
c906108c
SS
17788/* Convert a DIE tag into its string name. */
17789
f39c6ffd 17790static const char *
aa1ee363 17791dwarf_tag_name (unsigned tag)
c906108c 17792{
f39c6ffd
TT
17793 const char *name = get_DW_TAG_name (tag);
17794
17795 if (name == NULL)
17796 return "DW_TAG_<unknown>";
17797
17798 return name;
c906108c
SS
17799}
17800
17801/* Convert a DWARF attribute code into its string name. */
17802
f39c6ffd 17803static const char *
aa1ee363 17804dwarf_attr_name (unsigned attr)
c906108c 17805{
f39c6ffd
TT
17806 const char *name;
17807
c764a876 17808#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
17809 if (attr == DW_AT_MIPS_fde)
17810 return "DW_AT_MIPS_fde";
17811#else
17812 if (attr == DW_AT_HP_block_index)
17813 return "DW_AT_HP_block_index";
c764a876 17814#endif
f39c6ffd
TT
17815
17816 name = get_DW_AT_name (attr);
17817
17818 if (name == NULL)
17819 return "DW_AT_<unknown>";
17820
17821 return name;
c906108c
SS
17822}
17823
17824/* Convert a DWARF value form code into its string name. */
17825
f39c6ffd 17826static const char *
aa1ee363 17827dwarf_form_name (unsigned form)
c906108c 17828{
f39c6ffd
TT
17829 const char *name = get_DW_FORM_name (form);
17830
17831 if (name == NULL)
17832 return "DW_FORM_<unknown>";
17833
17834 return name;
c906108c
SS
17835}
17836
17837static char *
fba45db2 17838dwarf_bool_name (unsigned mybool)
c906108c
SS
17839{
17840 if (mybool)
17841 return "TRUE";
17842 else
17843 return "FALSE";
17844}
17845
17846/* Convert a DWARF type code into its string name. */
17847
f39c6ffd 17848static const char *
aa1ee363 17849dwarf_type_encoding_name (unsigned enc)
c906108c 17850{
f39c6ffd 17851 const char *name = get_DW_ATE_name (enc);
c906108c 17852
f39c6ffd
TT
17853 if (name == NULL)
17854 return "DW_ATE_<unknown>";
c906108c 17855
f39c6ffd 17856 return name;
c906108c 17857}
c906108c 17858
f9aca02d 17859static void
d97bc12b 17860dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
17861{
17862 unsigned int i;
17863
d97bc12b
DE
17864 print_spaces (indent, f);
17865 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 17866 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
17867
17868 if (die->parent != NULL)
17869 {
17870 print_spaces (indent, f);
17871 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 17872 die->parent->offset.sect_off);
d97bc12b
DE
17873 }
17874
17875 print_spaces (indent, f);
17876 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 17877 dwarf_bool_name (die->child != NULL));
c906108c 17878
d97bc12b
DE
17879 print_spaces (indent, f);
17880 fprintf_unfiltered (f, " attributes:\n");
17881
c906108c
SS
17882 for (i = 0; i < die->num_attrs; ++i)
17883 {
d97bc12b
DE
17884 print_spaces (indent, f);
17885 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
17886 dwarf_attr_name (die->attrs[i].name),
17887 dwarf_form_name (die->attrs[i].form));
d97bc12b 17888
c906108c
SS
17889 switch (die->attrs[i].form)
17890 {
c906108c 17891 case DW_FORM_addr:
3019eac3 17892 case DW_FORM_GNU_addr_index:
d97bc12b 17893 fprintf_unfiltered (f, "address: ");
5af949e3 17894 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
17895 break;
17896 case DW_FORM_block2:
17897 case DW_FORM_block4:
17898 case DW_FORM_block:
17899 case DW_FORM_block1:
56eb65bd
SP
17900 fprintf_unfiltered (f, "block: size %s",
17901 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 17902 break;
2dc7f7b3 17903 case DW_FORM_exprloc:
56eb65bd
SP
17904 fprintf_unfiltered (f, "expression: size %s",
17905 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 17906 break;
4568ecf9
DE
17907 case DW_FORM_ref_addr:
17908 fprintf_unfiltered (f, "ref address: ");
17909 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17910 break;
36586728
TT
17911 case DW_FORM_GNU_ref_alt:
17912 fprintf_unfiltered (f, "alt ref address: ");
17913 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17914 break;
10b3939b
DJ
17915 case DW_FORM_ref1:
17916 case DW_FORM_ref2:
17917 case DW_FORM_ref4:
4568ecf9
DE
17918 case DW_FORM_ref8:
17919 case DW_FORM_ref_udata:
d97bc12b 17920 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 17921 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 17922 break;
c906108c
SS
17923 case DW_FORM_data1:
17924 case DW_FORM_data2:
17925 case DW_FORM_data4:
ce5d95e1 17926 case DW_FORM_data8:
c906108c
SS
17927 case DW_FORM_udata:
17928 case DW_FORM_sdata:
43bbcdc2
PH
17929 fprintf_unfiltered (f, "constant: %s",
17930 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 17931 break;
2dc7f7b3
TT
17932 case DW_FORM_sec_offset:
17933 fprintf_unfiltered (f, "section offset: %s",
17934 pulongest (DW_UNSND (&die->attrs[i])));
17935 break;
55f1336d 17936 case DW_FORM_ref_sig8:
ac9ec31b
DE
17937 fprintf_unfiltered (f, "signature: %s",
17938 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 17939 break;
c906108c 17940 case DW_FORM_string:
4bdf3d34 17941 case DW_FORM_strp:
3019eac3 17942 case DW_FORM_GNU_str_index:
36586728 17943 case DW_FORM_GNU_strp_alt:
8285870a 17944 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 17945 DW_STRING (&die->attrs[i])
8285870a
JK
17946 ? DW_STRING (&die->attrs[i]) : "",
17947 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
17948 break;
17949 case DW_FORM_flag:
17950 if (DW_UNSND (&die->attrs[i]))
d97bc12b 17951 fprintf_unfiltered (f, "flag: TRUE");
c906108c 17952 else
d97bc12b 17953 fprintf_unfiltered (f, "flag: FALSE");
c906108c 17954 break;
2dc7f7b3
TT
17955 case DW_FORM_flag_present:
17956 fprintf_unfiltered (f, "flag: TRUE");
17957 break;
a8329558 17958 case DW_FORM_indirect:
0963b4bd
MS
17959 /* The reader will have reduced the indirect form to
17960 the "base form" so this form should not occur. */
3e43a32a
MS
17961 fprintf_unfiltered (f,
17962 "unexpected attribute form: DW_FORM_indirect");
a8329558 17963 break;
c906108c 17964 default:
d97bc12b 17965 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 17966 die->attrs[i].form);
d97bc12b 17967 break;
c906108c 17968 }
d97bc12b 17969 fprintf_unfiltered (f, "\n");
c906108c
SS
17970 }
17971}
17972
f9aca02d 17973static void
d97bc12b 17974dump_die_for_error (struct die_info *die)
c906108c 17975{
d97bc12b
DE
17976 dump_die_shallow (gdb_stderr, 0, die);
17977}
17978
17979static void
17980dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17981{
17982 int indent = level * 4;
17983
17984 gdb_assert (die != NULL);
17985
17986 if (level >= max_level)
17987 return;
17988
17989 dump_die_shallow (f, indent, die);
17990
17991 if (die->child != NULL)
c906108c 17992 {
d97bc12b
DE
17993 print_spaces (indent, f);
17994 fprintf_unfiltered (f, " Children:");
17995 if (level + 1 < max_level)
17996 {
17997 fprintf_unfiltered (f, "\n");
17998 dump_die_1 (f, level + 1, max_level, die->child);
17999 }
18000 else
18001 {
3e43a32a
MS
18002 fprintf_unfiltered (f,
18003 " [not printed, max nesting level reached]\n");
d97bc12b
DE
18004 }
18005 }
18006
18007 if (die->sibling != NULL && level > 0)
18008 {
18009 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
18010 }
18011}
18012
d97bc12b
DE
18013/* This is called from the pdie macro in gdbinit.in.
18014 It's not static so gcc will keep a copy callable from gdb. */
18015
18016void
18017dump_die (struct die_info *die, int max_level)
18018{
18019 dump_die_1 (gdb_stdlog, 0, max_level, die);
18020}
18021
f9aca02d 18022static void
51545339 18023store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18024{
51545339 18025 void **slot;
c906108c 18026
b64f50a1
JK
18027 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18028 INSERT);
51545339
DJ
18029
18030 *slot = die;
c906108c
SS
18031}
18032
b64f50a1
JK
18033/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18034 required kind. */
18035
18036static sect_offset
ff39bb5e 18037dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 18038{
4568ecf9 18039 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 18040
7771576e 18041 if (attr_form_is_ref (attr))
b64f50a1 18042 return retval;
93311388 18043
b64f50a1 18044 retval.sect_off = 0;
93311388
DE
18045 complaint (&symfile_complaints,
18046 _("unsupported die ref attribute form: '%s'"),
18047 dwarf_form_name (attr->form));
b64f50a1 18048 return retval;
c906108c
SS
18049}
18050
43bbcdc2
PH
18051/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18052 * the value held by the attribute is not constant. */
a02abb62 18053
43bbcdc2 18054static LONGEST
ff39bb5e 18055dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62
JB
18056{
18057 if (attr->form == DW_FORM_sdata)
18058 return DW_SND (attr);
18059 else if (attr->form == DW_FORM_udata
18060 || attr->form == DW_FORM_data1
18061 || attr->form == DW_FORM_data2
18062 || attr->form == DW_FORM_data4
18063 || attr->form == DW_FORM_data8)
18064 return DW_UNSND (attr);
18065 else
18066 {
3e43a32a
MS
18067 complaint (&symfile_complaints,
18068 _("Attribute value is not a constant (%s)"),
a02abb62
JB
18069 dwarf_form_name (attr->form));
18070 return default_value;
18071 }
18072}
18073
348e048f
DE
18074/* Follow reference or signature attribute ATTR of SRC_DIE.
18075 On entry *REF_CU is the CU of SRC_DIE.
18076 On exit *REF_CU is the CU of the result. */
18077
18078static struct die_info *
ff39bb5e 18079follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
18080 struct dwarf2_cu **ref_cu)
18081{
18082 struct die_info *die;
18083
7771576e 18084 if (attr_form_is_ref (attr))
348e048f 18085 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 18086 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
18087 die = follow_die_sig (src_die, attr, ref_cu);
18088 else
18089 {
18090 dump_die_for_error (src_die);
18091 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18092 (*ref_cu)->objfile->name);
18093 }
18094
18095 return die;
03dd20cc
DJ
18096}
18097
5c631832 18098/* Follow reference OFFSET.
673bfd45
DE
18099 On entry *REF_CU is the CU of the source die referencing OFFSET.
18100 On exit *REF_CU is the CU of the result.
18101 Returns NULL if OFFSET is invalid. */
f504f079 18102
f9aca02d 18103static struct die_info *
36586728
TT
18104follow_die_offset (sect_offset offset, int offset_in_dwz,
18105 struct dwarf2_cu **ref_cu)
c906108c 18106{
10b3939b 18107 struct die_info temp_die;
f2f0e013 18108 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 18109
348e048f
DE
18110 gdb_assert (cu->per_cu != NULL);
18111
98bfdba5
PA
18112 target_cu = cu;
18113
3019eac3 18114 if (cu->per_cu->is_debug_types)
348e048f
DE
18115 {
18116 /* .debug_types CUs cannot reference anything outside their CU.
18117 If they need to, they have to reference a signatured type via
55f1336d 18118 DW_FORM_ref_sig8. */
348e048f 18119 if (! offset_in_cu_p (&cu->header, offset))
5c631832 18120 return NULL;
348e048f 18121 }
36586728
TT
18122 else if (offset_in_dwz != cu->per_cu->is_dwz
18123 || ! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
18124 {
18125 struct dwarf2_per_cu_data *per_cu;
9a619af0 18126
36586728
TT
18127 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18128 cu->objfile);
03dd20cc
DJ
18129
18130 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
18131 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18132 load_full_comp_unit (per_cu, cu->language);
03dd20cc 18133
10b3939b
DJ
18134 target_cu = per_cu->cu;
18135 }
98bfdba5
PA
18136 else if (cu->dies == NULL)
18137 {
18138 /* We're loading full DIEs during partial symbol reading. */
18139 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 18140 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 18141 }
c906108c 18142
f2f0e013 18143 *ref_cu = target_cu;
51545339 18144 temp_die.offset = offset;
b64f50a1 18145 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 18146}
10b3939b 18147
5c631832
JK
18148/* Follow reference attribute ATTR of SRC_DIE.
18149 On entry *REF_CU is the CU of SRC_DIE.
18150 On exit *REF_CU is the CU of the result. */
18151
18152static struct die_info *
ff39bb5e 18153follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
18154 struct dwarf2_cu **ref_cu)
18155{
b64f50a1 18156 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
18157 struct dwarf2_cu *cu = *ref_cu;
18158 struct die_info *die;
18159
36586728
TT
18160 die = follow_die_offset (offset,
18161 (attr->form == DW_FORM_GNU_ref_alt
18162 || cu->per_cu->is_dwz),
18163 ref_cu);
5c631832
JK
18164 if (!die)
18165 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18166 "at 0x%x [in module %s]"),
b64f50a1 18167 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 18168
5c631832
JK
18169 return die;
18170}
18171
d83e736b
JK
18172/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18173 Returned value is intended for DW_OP_call*. Returned
18174 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
18175
18176struct dwarf2_locexpr_baton
8b9737bf
TT
18177dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18178 struct dwarf2_per_cu_data *per_cu,
18179 CORE_ADDR (*get_frame_pc) (void *baton),
18180 void *baton)
5c631832 18181{
918dd910 18182 struct dwarf2_cu *cu;
5c631832
JK
18183 struct die_info *die;
18184 struct attribute *attr;
18185 struct dwarf2_locexpr_baton retval;
18186
8cf6f0b1
TT
18187 dw2_setup (per_cu->objfile);
18188
918dd910
JK
18189 if (per_cu->cu == NULL)
18190 load_cu (per_cu);
18191 cu = per_cu->cu;
18192
36586728 18193 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
5c631832
JK
18194 if (!die)
18195 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 18196 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18197
18198 attr = dwarf2_attr (die, DW_AT_location, cu);
18199 if (!attr)
18200 {
e103e986
JK
18201 /* DWARF: "If there is no such attribute, then there is no effect.".
18202 DATA is ignored if SIZE is 0. */
5c631832 18203
e103e986 18204 retval.data = NULL;
5c631832
JK
18205 retval.size = 0;
18206 }
8cf6f0b1
TT
18207 else if (attr_form_is_section_offset (attr))
18208 {
18209 struct dwarf2_loclist_baton loclist_baton;
18210 CORE_ADDR pc = (*get_frame_pc) (baton);
18211 size_t size;
18212
18213 fill_in_loclist_baton (cu, &loclist_baton, attr);
18214
18215 retval.data = dwarf2_find_location_expression (&loclist_baton,
18216 &size, pc);
18217 retval.size = size;
18218 }
5c631832
JK
18219 else
18220 {
18221 if (!attr_form_is_block (attr))
18222 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18223 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 18224 offset.sect_off, per_cu->objfile->name);
5c631832
JK
18225
18226 retval.data = DW_BLOCK (attr)->data;
18227 retval.size = DW_BLOCK (attr)->size;
18228 }
18229 retval.per_cu = cu->per_cu;
918dd910 18230
918dd910
JK
18231 age_cached_comp_units ();
18232
5c631832 18233 return retval;
348e048f
DE
18234}
18235
8b9737bf
TT
18236/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18237 offset. */
18238
18239struct dwarf2_locexpr_baton
18240dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18241 struct dwarf2_per_cu_data *per_cu,
18242 CORE_ADDR (*get_frame_pc) (void *baton),
18243 void *baton)
18244{
18245 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18246
18247 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18248}
18249
b6807d98
TT
18250/* Write a constant of a given type as target-ordered bytes into
18251 OBSTACK. */
18252
18253static const gdb_byte *
18254write_constant_as_bytes (struct obstack *obstack,
18255 enum bfd_endian byte_order,
18256 struct type *type,
18257 ULONGEST value,
18258 LONGEST *len)
18259{
18260 gdb_byte *result;
18261
18262 *len = TYPE_LENGTH (type);
18263 result = obstack_alloc (obstack, *len);
18264 store_unsigned_integer (result, *len, byte_order, value);
18265
18266 return result;
18267}
18268
18269/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18270 pointer to the constant bytes and set LEN to the length of the
18271 data. If memory is needed, allocate it on OBSTACK. If the DIE
18272 does not have a DW_AT_const_value, return NULL. */
18273
18274const gdb_byte *
18275dwarf2_fetch_constant_bytes (sect_offset offset,
18276 struct dwarf2_per_cu_data *per_cu,
18277 struct obstack *obstack,
18278 LONGEST *len)
18279{
18280 struct dwarf2_cu *cu;
18281 struct die_info *die;
18282 struct attribute *attr;
18283 const gdb_byte *result = NULL;
18284 struct type *type;
18285 LONGEST value;
18286 enum bfd_endian byte_order;
18287
18288 dw2_setup (per_cu->objfile);
18289
18290 if (per_cu->cu == NULL)
18291 load_cu (per_cu);
18292 cu = per_cu->cu;
18293
18294 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18295 if (!die)
18296 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18297 offset.sect_off, per_cu->objfile->name);
18298
18299
18300 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18301 if (attr == NULL)
18302 return NULL;
18303
18304 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18305 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18306
18307 switch (attr->form)
18308 {
18309 case DW_FORM_addr:
18310 case DW_FORM_GNU_addr_index:
18311 {
18312 gdb_byte *tem;
18313
18314 *len = cu->header.addr_size;
18315 tem = obstack_alloc (obstack, *len);
18316 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18317 result = tem;
18318 }
18319 break;
18320 case DW_FORM_string:
18321 case DW_FORM_strp:
18322 case DW_FORM_GNU_str_index:
18323 case DW_FORM_GNU_strp_alt:
18324 /* DW_STRING is already allocated on the objfile obstack, point
18325 directly to it. */
18326 result = (const gdb_byte *) DW_STRING (attr);
18327 *len = strlen (DW_STRING (attr));
18328 break;
18329 case DW_FORM_block1:
18330 case DW_FORM_block2:
18331 case DW_FORM_block4:
18332 case DW_FORM_block:
18333 case DW_FORM_exprloc:
18334 result = DW_BLOCK (attr)->data;
18335 *len = DW_BLOCK (attr)->size;
18336 break;
18337
18338 /* The DW_AT_const_value attributes are supposed to carry the
18339 symbol's value "represented as it would be on the target
18340 architecture." By the time we get here, it's already been
18341 converted to host endianness, so we just need to sign- or
18342 zero-extend it as appropriate. */
18343 case DW_FORM_data1:
18344 type = die_type (die, cu);
18345 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18346 if (result == NULL)
18347 result = write_constant_as_bytes (obstack, byte_order,
18348 type, value, len);
18349 break;
18350 case DW_FORM_data2:
18351 type = die_type (die, cu);
18352 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18353 if (result == NULL)
18354 result = write_constant_as_bytes (obstack, byte_order,
18355 type, value, len);
18356 break;
18357 case DW_FORM_data4:
18358 type = die_type (die, cu);
18359 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18360 if (result == NULL)
18361 result = write_constant_as_bytes (obstack, byte_order,
18362 type, value, len);
18363 break;
18364 case DW_FORM_data8:
18365 type = die_type (die, cu);
18366 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18367 if (result == NULL)
18368 result = write_constant_as_bytes (obstack, byte_order,
18369 type, value, len);
18370 break;
18371
18372 case DW_FORM_sdata:
18373 type = die_type (die, cu);
18374 result = write_constant_as_bytes (obstack, byte_order,
18375 type, DW_SND (attr), len);
18376 break;
18377
18378 case DW_FORM_udata:
18379 type = die_type (die, cu);
18380 result = write_constant_as_bytes (obstack, byte_order,
18381 type, DW_UNSND (attr), len);
18382 break;
18383
18384 default:
18385 complaint (&symfile_complaints,
18386 _("unsupported const value attribute form: '%s'"),
18387 dwarf_form_name (attr->form));
18388 break;
18389 }
18390
18391 return result;
18392}
18393
8a9b8146
TT
18394/* Return the type of the DIE at DIE_OFFSET in the CU named by
18395 PER_CU. */
18396
18397struct type *
b64f50a1 18398dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
18399 struct dwarf2_per_cu_data *per_cu)
18400{
b64f50a1
JK
18401 sect_offset die_offset_sect;
18402
8a9b8146 18403 dw2_setup (per_cu->objfile);
b64f50a1
JK
18404
18405 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18406 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
18407}
18408
ac9ec31b 18409/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 18410 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
18411 On exit *REF_CU is the CU of the result.
18412 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
18413
18414static struct die_info *
ac9ec31b
DE
18415follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18416 struct dwarf2_cu **ref_cu)
348e048f
DE
18417{
18418 struct objfile *objfile = (*ref_cu)->objfile;
18419 struct die_info temp_die;
348e048f
DE
18420 struct dwarf2_cu *sig_cu;
18421 struct die_info *die;
18422
ac9ec31b
DE
18423 /* While it might be nice to assert sig_type->type == NULL here,
18424 we can get here for DW_AT_imported_declaration where we need
18425 the DIE not the type. */
348e048f
DE
18426
18427 /* If necessary, add it to the queue and load its DIEs. */
18428
95554aad 18429 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 18430 read_signatured_type (sig_type);
348e048f 18431
348e048f 18432 sig_cu = sig_type->per_cu.cu;
69d751e3 18433 gdb_assert (sig_cu != NULL);
3019eac3
DE
18434 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18435 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
18436 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18437 temp_die.offset.sect_off);
348e048f
DE
18438 if (die)
18439 {
796a7ff8
DE
18440 /* For .gdb_index version 7 keep track of included TUs.
18441 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18442 if (dwarf2_per_objfile->index_table != NULL
18443 && dwarf2_per_objfile->index_table->version <= 7)
18444 {
18445 VEC_safe_push (dwarf2_per_cu_ptr,
18446 (*ref_cu)->per_cu->imported_symtabs,
18447 sig_cu->per_cu);
18448 }
18449
348e048f
DE
18450 *ref_cu = sig_cu;
18451 return die;
18452 }
18453
ac9ec31b
DE
18454 return NULL;
18455}
18456
18457/* Follow signatured type referenced by ATTR in SRC_DIE.
18458 On entry *REF_CU is the CU of SRC_DIE.
18459 On exit *REF_CU is the CU of the result.
18460 The result is the DIE of the type.
18461 If the referenced type cannot be found an error is thrown. */
18462
18463static struct die_info *
ff39bb5e 18464follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
18465 struct dwarf2_cu **ref_cu)
18466{
18467 ULONGEST signature = DW_SIGNATURE (attr);
18468 struct signatured_type *sig_type;
18469 struct die_info *die;
18470
18471 gdb_assert (attr->form == DW_FORM_ref_sig8);
18472
a2ce51a0 18473 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
18474 /* sig_type will be NULL if the signatured type is missing from
18475 the debug info. */
18476 if (sig_type == NULL)
18477 {
18478 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18479 " from DIE at 0x%x [in module %s]"),
18480 hex_string (signature), src_die->offset.sect_off,
18481 (*ref_cu)->objfile->name);
18482 }
18483
18484 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18485 if (die == NULL)
18486 {
18487 dump_die_for_error (src_die);
18488 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18489 " from DIE at 0x%x [in module %s]"),
18490 hex_string (signature), src_die->offset.sect_off,
18491 (*ref_cu)->objfile->name);
18492 }
18493
18494 return die;
18495}
18496
18497/* Get the type specified by SIGNATURE referenced in DIE/CU,
18498 reading in and processing the type unit if necessary. */
18499
18500static struct type *
18501get_signatured_type (struct die_info *die, ULONGEST signature,
18502 struct dwarf2_cu *cu)
18503{
18504 struct signatured_type *sig_type;
18505 struct dwarf2_cu *type_cu;
18506 struct die_info *type_die;
18507 struct type *type;
18508
a2ce51a0 18509 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
18510 /* sig_type will be NULL if the signatured type is missing from
18511 the debug info. */
18512 if (sig_type == NULL)
18513 {
18514 complaint (&symfile_complaints,
18515 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18516 " from DIE at 0x%x [in module %s]"),
18517 hex_string (signature), die->offset.sect_off,
18518 dwarf2_per_objfile->objfile->name);
18519 return build_error_marker_type (cu, die);
18520 }
18521
18522 /* If we already know the type we're done. */
18523 if (sig_type->type != NULL)
18524 return sig_type->type;
18525
18526 type_cu = cu;
18527 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18528 if (type_die != NULL)
18529 {
18530 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18531 is created. This is important, for example, because for c++ classes
18532 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18533 type = read_type_die (type_die, type_cu);
18534 if (type == NULL)
18535 {
18536 complaint (&symfile_complaints,
18537 _("Dwarf Error: Cannot build signatured type %s"
18538 " referenced from DIE at 0x%x [in module %s]"),
18539 hex_string (signature), die->offset.sect_off,
18540 dwarf2_per_objfile->objfile->name);
18541 type = build_error_marker_type (cu, die);
18542 }
18543 }
18544 else
18545 {
18546 complaint (&symfile_complaints,
18547 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18548 " from DIE at 0x%x [in module %s]"),
18549 hex_string (signature), die->offset.sect_off,
18550 dwarf2_per_objfile->objfile->name);
18551 type = build_error_marker_type (cu, die);
18552 }
18553 sig_type->type = type;
18554
18555 return type;
18556}
18557
18558/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18559 reading in and processing the type unit if necessary. */
18560
18561static struct type *
ff39bb5e 18562get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 18563 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
18564{
18565 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 18566 if (attr_form_is_ref (attr))
ac9ec31b
DE
18567 {
18568 struct dwarf2_cu *type_cu = cu;
18569 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18570
18571 return read_type_die (type_die, type_cu);
18572 }
18573 else if (attr->form == DW_FORM_ref_sig8)
18574 {
18575 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18576 }
18577 else
18578 {
18579 complaint (&symfile_complaints,
18580 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18581 " at 0x%x [in module %s]"),
18582 dwarf_form_name (attr->form), die->offset.sect_off,
18583 dwarf2_per_objfile->objfile->name);
18584 return build_error_marker_type (cu, die);
18585 }
348e048f
DE
18586}
18587
e5fe5e75 18588/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
18589
18590static void
e5fe5e75 18591load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 18592{
52dc124a 18593 struct signatured_type *sig_type;
348e048f 18594
f4dc4d17
DE
18595 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18596 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18597
6721b2ec
DE
18598 /* We have the per_cu, but we need the signatured_type.
18599 Fortunately this is an easy translation. */
18600 gdb_assert (per_cu->is_debug_types);
18601 sig_type = (struct signatured_type *) per_cu;
348e048f 18602
6721b2ec 18603 gdb_assert (per_cu->cu == NULL);
348e048f 18604
52dc124a 18605 read_signatured_type (sig_type);
348e048f 18606
6721b2ec 18607 gdb_assert (per_cu->cu != NULL);
348e048f
DE
18608}
18609
dee91e82
DE
18610/* die_reader_func for read_signatured_type.
18611 This is identical to load_full_comp_unit_reader,
18612 but is kept separate for now. */
348e048f
DE
18613
18614static void
dee91e82 18615read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 18616 const gdb_byte *info_ptr,
dee91e82
DE
18617 struct die_info *comp_unit_die,
18618 int has_children,
18619 void *data)
348e048f 18620{
dee91e82 18621 struct dwarf2_cu *cu = reader->cu;
348e048f 18622
dee91e82
DE
18623 gdb_assert (cu->die_hash == NULL);
18624 cu->die_hash =
18625 htab_create_alloc_ex (cu->header.length / 12,
18626 die_hash,
18627 die_eq,
18628 NULL,
18629 &cu->comp_unit_obstack,
18630 hashtab_obstack_allocate,
18631 dummy_obstack_deallocate);
348e048f 18632
dee91e82
DE
18633 if (has_children)
18634 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18635 &info_ptr, comp_unit_die);
18636 cu->dies = comp_unit_die;
18637 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
18638
18639 /* We try not to read any attributes in this function, because not
9cdd5dbd 18640 all CUs needed for references have been loaded yet, and symbol
348e048f 18641 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
18642 or we won't be able to build types correctly.
18643 Similarly, if we do not read the producer, we can not apply
18644 producer-specific interpretation. */
95554aad 18645 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 18646}
348e048f 18647
3019eac3
DE
18648/* Read in a signatured type and build its CU and DIEs.
18649 If the type is a stub for the real type in a DWO file,
18650 read in the real type from the DWO file as well. */
dee91e82
DE
18651
18652static void
18653read_signatured_type (struct signatured_type *sig_type)
18654{
18655 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 18656
3019eac3 18657 gdb_assert (per_cu->is_debug_types);
dee91e82 18658 gdb_assert (per_cu->cu == NULL);
348e048f 18659
f4dc4d17
DE
18660 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18661 read_signatured_type_reader, NULL);
7ee85ab1 18662 sig_type->per_cu.tu_read = 1;
c906108c
SS
18663}
18664
c906108c
SS
18665/* Decode simple location descriptions.
18666 Given a pointer to a dwarf block that defines a location, compute
18667 the location and return the value.
18668
4cecd739
DJ
18669 NOTE drow/2003-11-18: This function is called in two situations
18670 now: for the address of static or global variables (partial symbols
18671 only) and for offsets into structures which are expected to be
18672 (more or less) constant. The partial symbol case should go away,
18673 and only the constant case should remain. That will let this
18674 function complain more accurately. A few special modes are allowed
18675 without complaint for global variables (for instance, global
18676 register values and thread-local values).
c906108c
SS
18677
18678 A location description containing no operations indicates that the
4cecd739 18679 object is optimized out. The return value is 0 for that case.
6b992462
DJ
18680 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18681 callers will only want a very basic result and this can become a
21ae7a4d
JK
18682 complaint.
18683
18684 Note that stack[0] is unused except as a default error return. */
c906108c
SS
18685
18686static CORE_ADDR
e7c27a73 18687decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 18688{
e7c27a73 18689 struct objfile *objfile = cu->objfile;
56eb65bd
SP
18690 size_t i;
18691 size_t size = blk->size;
d521ce57 18692 const gdb_byte *data = blk->data;
21ae7a4d
JK
18693 CORE_ADDR stack[64];
18694 int stacki;
18695 unsigned int bytes_read, unsnd;
18696 gdb_byte op;
c906108c 18697
21ae7a4d
JK
18698 i = 0;
18699 stacki = 0;
18700 stack[stacki] = 0;
18701 stack[++stacki] = 0;
18702
18703 while (i < size)
18704 {
18705 op = data[i++];
18706 switch (op)
18707 {
18708 case DW_OP_lit0:
18709 case DW_OP_lit1:
18710 case DW_OP_lit2:
18711 case DW_OP_lit3:
18712 case DW_OP_lit4:
18713 case DW_OP_lit5:
18714 case DW_OP_lit6:
18715 case DW_OP_lit7:
18716 case DW_OP_lit8:
18717 case DW_OP_lit9:
18718 case DW_OP_lit10:
18719 case DW_OP_lit11:
18720 case DW_OP_lit12:
18721 case DW_OP_lit13:
18722 case DW_OP_lit14:
18723 case DW_OP_lit15:
18724 case DW_OP_lit16:
18725 case DW_OP_lit17:
18726 case DW_OP_lit18:
18727 case DW_OP_lit19:
18728 case DW_OP_lit20:
18729 case DW_OP_lit21:
18730 case DW_OP_lit22:
18731 case DW_OP_lit23:
18732 case DW_OP_lit24:
18733 case DW_OP_lit25:
18734 case DW_OP_lit26:
18735 case DW_OP_lit27:
18736 case DW_OP_lit28:
18737 case DW_OP_lit29:
18738 case DW_OP_lit30:
18739 case DW_OP_lit31:
18740 stack[++stacki] = op - DW_OP_lit0;
18741 break;
f1bea926 18742
21ae7a4d
JK
18743 case DW_OP_reg0:
18744 case DW_OP_reg1:
18745 case DW_OP_reg2:
18746 case DW_OP_reg3:
18747 case DW_OP_reg4:
18748 case DW_OP_reg5:
18749 case DW_OP_reg6:
18750 case DW_OP_reg7:
18751 case DW_OP_reg8:
18752 case DW_OP_reg9:
18753 case DW_OP_reg10:
18754 case DW_OP_reg11:
18755 case DW_OP_reg12:
18756 case DW_OP_reg13:
18757 case DW_OP_reg14:
18758 case DW_OP_reg15:
18759 case DW_OP_reg16:
18760 case DW_OP_reg17:
18761 case DW_OP_reg18:
18762 case DW_OP_reg19:
18763 case DW_OP_reg20:
18764 case DW_OP_reg21:
18765 case DW_OP_reg22:
18766 case DW_OP_reg23:
18767 case DW_OP_reg24:
18768 case DW_OP_reg25:
18769 case DW_OP_reg26:
18770 case DW_OP_reg27:
18771 case DW_OP_reg28:
18772 case DW_OP_reg29:
18773 case DW_OP_reg30:
18774 case DW_OP_reg31:
18775 stack[++stacki] = op - DW_OP_reg0;
18776 if (i < size)
18777 dwarf2_complex_location_expr_complaint ();
18778 break;
c906108c 18779
21ae7a4d
JK
18780 case DW_OP_regx:
18781 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18782 i += bytes_read;
18783 stack[++stacki] = unsnd;
18784 if (i < size)
18785 dwarf2_complex_location_expr_complaint ();
18786 break;
c906108c 18787
21ae7a4d
JK
18788 case DW_OP_addr:
18789 stack[++stacki] = read_address (objfile->obfd, &data[i],
18790 cu, &bytes_read);
18791 i += bytes_read;
18792 break;
d53d4ac5 18793
21ae7a4d
JK
18794 case DW_OP_const1u:
18795 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18796 i += 1;
18797 break;
18798
18799 case DW_OP_const1s:
18800 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18801 i += 1;
18802 break;
18803
18804 case DW_OP_const2u:
18805 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18806 i += 2;
18807 break;
18808
18809 case DW_OP_const2s:
18810 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18811 i += 2;
18812 break;
d53d4ac5 18813
21ae7a4d
JK
18814 case DW_OP_const4u:
18815 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18816 i += 4;
18817 break;
18818
18819 case DW_OP_const4s:
18820 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18821 i += 4;
18822 break;
18823
585861ea
JK
18824 case DW_OP_const8u:
18825 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18826 i += 8;
18827 break;
18828
21ae7a4d
JK
18829 case DW_OP_constu:
18830 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18831 &bytes_read);
18832 i += bytes_read;
18833 break;
18834
18835 case DW_OP_consts:
18836 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18837 i += bytes_read;
18838 break;
18839
18840 case DW_OP_dup:
18841 stack[stacki + 1] = stack[stacki];
18842 stacki++;
18843 break;
18844
18845 case DW_OP_plus:
18846 stack[stacki - 1] += stack[stacki];
18847 stacki--;
18848 break;
18849
18850 case DW_OP_plus_uconst:
18851 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18852 &bytes_read);
18853 i += bytes_read;
18854 break;
18855
18856 case DW_OP_minus:
18857 stack[stacki - 1] -= stack[stacki];
18858 stacki--;
18859 break;
18860
18861 case DW_OP_deref:
18862 /* If we're not the last op, then we definitely can't encode
18863 this using GDB's address_class enum. This is valid for partial
18864 global symbols, although the variable's address will be bogus
18865 in the psymtab. */
18866 if (i < size)
18867 dwarf2_complex_location_expr_complaint ();
18868 break;
18869
18870 case DW_OP_GNU_push_tls_address:
18871 /* The top of the stack has the offset from the beginning
18872 of the thread control block at which the variable is located. */
18873 /* Nothing should follow this operator, so the top of stack would
18874 be returned. */
18875 /* This is valid for partial global symbols, but the variable's
585861ea
JK
18876 address will be bogus in the psymtab. Make it always at least
18877 non-zero to not look as a variable garbage collected by linker
18878 which have DW_OP_addr 0. */
21ae7a4d
JK
18879 if (i < size)
18880 dwarf2_complex_location_expr_complaint ();
585861ea 18881 stack[stacki]++;
21ae7a4d
JK
18882 break;
18883
18884 case DW_OP_GNU_uninit:
18885 break;
18886
3019eac3 18887 case DW_OP_GNU_addr_index:
49f6c839 18888 case DW_OP_GNU_const_index:
3019eac3
DE
18889 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18890 &bytes_read);
18891 i += bytes_read;
18892 break;
18893
21ae7a4d
JK
18894 default:
18895 {
f39c6ffd 18896 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
18897
18898 if (name)
18899 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18900 name);
18901 else
18902 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18903 op);
18904 }
18905
18906 return (stack[stacki]);
d53d4ac5 18907 }
3c6e0cb3 18908
21ae7a4d
JK
18909 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18910 outside of the allocated space. Also enforce minimum>0. */
18911 if (stacki >= ARRAY_SIZE (stack) - 1)
18912 {
18913 complaint (&symfile_complaints,
18914 _("location description stack overflow"));
18915 return 0;
18916 }
18917
18918 if (stacki <= 0)
18919 {
18920 complaint (&symfile_complaints,
18921 _("location description stack underflow"));
18922 return 0;
18923 }
18924 }
18925 return (stack[stacki]);
c906108c
SS
18926}
18927
18928/* memory allocation interface */
18929
c906108c 18930static struct dwarf_block *
7b5a2f43 18931dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
18932{
18933 struct dwarf_block *blk;
18934
18935 blk = (struct dwarf_block *)
7b5a2f43 18936 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
18937 return (blk);
18938}
18939
c906108c 18940static struct die_info *
b60c80d6 18941dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
18942{
18943 struct die_info *die;
b60c80d6
DJ
18944 size_t size = sizeof (struct die_info);
18945
18946 if (num_attrs > 1)
18947 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 18948
b60c80d6 18949 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
18950 memset (die, 0, sizeof (struct die_info));
18951 return (die);
18952}
2e276125
JB
18953
18954\f
18955/* Macro support. */
18956
233d95b5
JK
18957/* Return file name relative to the compilation directory of file number I in
18958 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 18959 responsible for freeing it. */
233d95b5 18960
2e276125 18961static char *
233d95b5 18962file_file_name (int file, struct line_header *lh)
2e276125 18963{
6a83a1e6
EZ
18964 /* Is the file number a valid index into the line header's file name
18965 table? Remember that file numbers start with one, not zero. */
18966 if (1 <= file && file <= lh->num_file_names)
18967 {
18968 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 18969
233d95b5 18970 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
6a83a1e6 18971 return xstrdup (fe->name);
233d95b5
JK
18972 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18973 fe->name, NULL);
6a83a1e6 18974 }
2e276125
JB
18975 else
18976 {
6a83a1e6
EZ
18977 /* The compiler produced a bogus file number. We can at least
18978 record the macro definitions made in the file, even if we
18979 won't be able to find the file by name. */
18980 char fake_name[80];
9a619af0 18981
8c042590
PM
18982 xsnprintf (fake_name, sizeof (fake_name),
18983 "<bad macro file number %d>", file);
2e276125 18984
6e70227d 18985 complaint (&symfile_complaints,
6a83a1e6
EZ
18986 _("bad file number in macro information (%d)"),
18987 file);
2e276125 18988
6a83a1e6 18989 return xstrdup (fake_name);
2e276125
JB
18990 }
18991}
18992
233d95b5
JK
18993/* Return the full name of file number I in *LH's file name table.
18994 Use COMP_DIR as the name of the current directory of the
18995 compilation. The result is allocated using xmalloc; the caller is
18996 responsible for freeing it. */
18997static char *
18998file_full_name (int file, struct line_header *lh, const char *comp_dir)
18999{
19000 /* Is the file number a valid index into the line header's file name
19001 table? Remember that file numbers start with one, not zero. */
19002 if (1 <= file && file <= lh->num_file_names)
19003 {
19004 char *relative = file_file_name (file, lh);
19005
19006 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19007 return relative;
19008 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19009 }
19010 else
19011 return file_file_name (file, lh);
19012}
19013
2e276125
JB
19014
19015static struct macro_source_file *
19016macro_start_file (int file, int line,
19017 struct macro_source_file *current_file,
19018 const char *comp_dir,
19019 struct line_header *lh, struct objfile *objfile)
19020{
233d95b5
JK
19021 /* File name relative to the compilation directory of this source file. */
19022 char *file_name = file_file_name (file, lh);
2e276125 19023
2e276125 19024 if (! current_file)
abc9d0dc 19025 {
fc474241
DE
19026 /* Note: We don't create a macro table for this compilation unit
19027 at all until we actually get a filename. */
19028 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19029
abc9d0dc
TT
19030 /* If we have no current file, then this must be the start_file
19031 directive for the compilation unit's main source file. */
fc474241
DE
19032 current_file = macro_set_main (macro_table, file_name);
19033 macro_define_special (macro_table);
abc9d0dc 19034 }
2e276125 19035 else
233d95b5 19036 current_file = macro_include (current_file, line, file_name);
2e276125 19037
233d95b5 19038 xfree (file_name);
6e70227d 19039
2e276125
JB
19040 return current_file;
19041}
19042
19043
19044/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19045 followed by a null byte. */
19046static char *
19047copy_string (const char *buf, int len)
19048{
19049 char *s = xmalloc (len + 1);
9a619af0 19050
2e276125
JB
19051 memcpy (s, buf, len);
19052 s[len] = '\0';
2e276125
JB
19053 return s;
19054}
19055
19056
19057static const char *
19058consume_improper_spaces (const char *p, const char *body)
19059{
19060 if (*p == ' ')
19061 {
4d3c2250 19062 complaint (&symfile_complaints,
3e43a32a
MS
19063 _("macro definition contains spaces "
19064 "in formal argument list:\n`%s'"),
4d3c2250 19065 body);
2e276125
JB
19066
19067 while (*p == ' ')
19068 p++;
19069 }
19070
19071 return p;
19072}
19073
19074
19075static void
19076parse_macro_definition (struct macro_source_file *file, int line,
19077 const char *body)
19078{
19079 const char *p;
19080
19081 /* The body string takes one of two forms. For object-like macro
19082 definitions, it should be:
19083
19084 <macro name> " " <definition>
19085
19086 For function-like macro definitions, it should be:
19087
19088 <macro name> "() " <definition>
19089 or
19090 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19091
19092 Spaces may appear only where explicitly indicated, and in the
19093 <definition>.
19094
19095 The Dwarf 2 spec says that an object-like macro's name is always
19096 followed by a space, but versions of GCC around March 2002 omit
6e70227d 19097 the space when the macro's definition is the empty string.
2e276125
JB
19098
19099 The Dwarf 2 spec says that there should be no spaces between the
19100 formal arguments in a function-like macro's formal argument list,
19101 but versions of GCC around March 2002 include spaces after the
19102 commas. */
19103
19104
19105 /* Find the extent of the macro name. The macro name is terminated
19106 by either a space or null character (for an object-like macro) or
19107 an opening paren (for a function-like macro). */
19108 for (p = body; *p; p++)
19109 if (*p == ' ' || *p == '(')
19110 break;
19111
19112 if (*p == ' ' || *p == '\0')
19113 {
19114 /* It's an object-like macro. */
19115 int name_len = p - body;
19116 char *name = copy_string (body, name_len);
19117 const char *replacement;
19118
19119 if (*p == ' ')
19120 replacement = body + name_len + 1;
19121 else
19122 {
4d3c2250 19123 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19124 replacement = body + name_len;
19125 }
6e70227d 19126
2e276125
JB
19127 macro_define_object (file, line, name, replacement);
19128
19129 xfree (name);
19130 }
19131 else if (*p == '(')
19132 {
19133 /* It's a function-like macro. */
19134 char *name = copy_string (body, p - body);
19135 int argc = 0;
19136 int argv_size = 1;
19137 char **argv = xmalloc (argv_size * sizeof (*argv));
19138
19139 p++;
19140
19141 p = consume_improper_spaces (p, body);
19142
19143 /* Parse the formal argument list. */
19144 while (*p && *p != ')')
19145 {
19146 /* Find the extent of the current argument name. */
19147 const char *arg_start = p;
19148
19149 while (*p && *p != ',' && *p != ')' && *p != ' ')
19150 p++;
19151
19152 if (! *p || p == arg_start)
4d3c2250 19153 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19154 else
19155 {
19156 /* Make sure argv has room for the new argument. */
19157 if (argc >= argv_size)
19158 {
19159 argv_size *= 2;
19160 argv = xrealloc (argv, argv_size * sizeof (*argv));
19161 }
19162
19163 argv[argc++] = copy_string (arg_start, p - arg_start);
19164 }
19165
19166 p = consume_improper_spaces (p, body);
19167
19168 /* Consume the comma, if present. */
19169 if (*p == ',')
19170 {
19171 p++;
19172
19173 p = consume_improper_spaces (p, body);
19174 }
19175 }
19176
19177 if (*p == ')')
19178 {
19179 p++;
19180
19181 if (*p == ' ')
19182 /* Perfectly formed definition, no complaints. */
19183 macro_define_function (file, line, name,
6e70227d 19184 argc, (const char **) argv,
2e276125
JB
19185 p + 1);
19186 else if (*p == '\0')
19187 {
19188 /* Complain, but do define it. */
4d3c2250 19189 dwarf2_macro_malformed_definition_complaint (body);
2e276125 19190 macro_define_function (file, line, name,
6e70227d 19191 argc, (const char **) argv,
2e276125
JB
19192 p);
19193 }
19194 else
19195 /* Just complain. */
4d3c2250 19196 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19197 }
19198 else
19199 /* Just complain. */
4d3c2250 19200 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19201
19202 xfree (name);
19203 {
19204 int i;
19205
19206 for (i = 0; i < argc; i++)
19207 xfree (argv[i]);
19208 }
19209 xfree (argv);
19210 }
19211 else
4d3c2250 19212 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
19213}
19214
cf2c3c16
TT
19215/* Skip some bytes from BYTES according to the form given in FORM.
19216 Returns the new pointer. */
2e276125 19217
d521ce57
TT
19218static const gdb_byte *
19219skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
19220 enum dwarf_form form,
19221 unsigned int offset_size,
19222 struct dwarf2_section_info *section)
2e276125 19223{
cf2c3c16 19224 unsigned int bytes_read;
2e276125 19225
cf2c3c16 19226 switch (form)
2e276125 19227 {
cf2c3c16
TT
19228 case DW_FORM_data1:
19229 case DW_FORM_flag:
19230 ++bytes;
19231 break;
19232
19233 case DW_FORM_data2:
19234 bytes += 2;
19235 break;
19236
19237 case DW_FORM_data4:
19238 bytes += 4;
19239 break;
19240
19241 case DW_FORM_data8:
19242 bytes += 8;
19243 break;
19244
19245 case DW_FORM_string:
19246 read_direct_string (abfd, bytes, &bytes_read);
19247 bytes += bytes_read;
19248 break;
19249
19250 case DW_FORM_sec_offset:
19251 case DW_FORM_strp:
36586728 19252 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
19253 bytes += offset_size;
19254 break;
19255
19256 case DW_FORM_block:
19257 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19258 bytes += bytes_read;
19259 break;
19260
19261 case DW_FORM_block1:
19262 bytes += 1 + read_1_byte (abfd, bytes);
19263 break;
19264 case DW_FORM_block2:
19265 bytes += 2 + read_2_bytes (abfd, bytes);
19266 break;
19267 case DW_FORM_block4:
19268 bytes += 4 + read_4_bytes (abfd, bytes);
19269 break;
19270
19271 case DW_FORM_sdata:
19272 case DW_FORM_udata:
3019eac3
DE
19273 case DW_FORM_GNU_addr_index:
19274 case DW_FORM_GNU_str_index:
d521ce57 19275 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
19276 if (bytes == NULL)
19277 {
19278 dwarf2_section_buffer_overflow_complaint (section);
19279 return NULL;
19280 }
cf2c3c16
TT
19281 break;
19282
19283 default:
19284 {
19285 complain:
19286 complaint (&symfile_complaints,
19287 _("invalid form 0x%x in `%s'"),
19288 form,
19289 section->asection->name);
19290 return NULL;
19291 }
2e276125
JB
19292 }
19293
cf2c3c16
TT
19294 return bytes;
19295}
757a13d0 19296
cf2c3c16
TT
19297/* A helper for dwarf_decode_macros that handles skipping an unknown
19298 opcode. Returns an updated pointer to the macro data buffer; or,
19299 on error, issues a complaint and returns NULL. */
757a13d0 19300
d521ce57 19301static const gdb_byte *
cf2c3c16 19302skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
19303 const gdb_byte **opcode_definitions,
19304 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
19305 bfd *abfd,
19306 unsigned int offset_size,
19307 struct dwarf2_section_info *section)
19308{
19309 unsigned int bytes_read, i;
19310 unsigned long arg;
d521ce57 19311 const gdb_byte *defn;
2e276125 19312
cf2c3c16 19313 if (opcode_definitions[opcode] == NULL)
2e276125 19314 {
cf2c3c16
TT
19315 complaint (&symfile_complaints,
19316 _("unrecognized DW_MACFINO opcode 0x%x"),
19317 opcode);
19318 return NULL;
19319 }
2e276125 19320
cf2c3c16
TT
19321 defn = opcode_definitions[opcode];
19322 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19323 defn += bytes_read;
2e276125 19324
cf2c3c16
TT
19325 for (i = 0; i < arg; ++i)
19326 {
f664829e
DE
19327 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19328 section);
cf2c3c16
TT
19329 if (mac_ptr == NULL)
19330 {
19331 /* skip_form_bytes already issued the complaint. */
19332 return NULL;
19333 }
19334 }
757a13d0 19335
cf2c3c16
TT
19336 return mac_ptr;
19337}
757a13d0 19338
cf2c3c16
TT
19339/* A helper function which parses the header of a macro section.
19340 If the macro section is the extended (for now called "GNU") type,
19341 then this updates *OFFSET_SIZE. Returns a pointer to just after
19342 the header, or issues a complaint and returns NULL on error. */
757a13d0 19343
d521ce57
TT
19344static const gdb_byte *
19345dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 19346 bfd *abfd,
d521ce57 19347 const gdb_byte *mac_ptr,
cf2c3c16
TT
19348 unsigned int *offset_size,
19349 int section_is_gnu)
19350{
19351 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 19352
cf2c3c16
TT
19353 if (section_is_gnu)
19354 {
19355 unsigned int version, flags;
757a13d0 19356
cf2c3c16
TT
19357 version = read_2_bytes (abfd, mac_ptr);
19358 if (version != 4)
19359 {
19360 complaint (&symfile_complaints,
19361 _("unrecognized version `%d' in .debug_macro section"),
19362 version);
19363 return NULL;
19364 }
19365 mac_ptr += 2;
757a13d0 19366
cf2c3c16
TT
19367 flags = read_1_byte (abfd, mac_ptr);
19368 ++mac_ptr;
19369 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 19370
cf2c3c16
TT
19371 if ((flags & 2) != 0)
19372 /* We don't need the line table offset. */
19373 mac_ptr += *offset_size;
757a13d0 19374
cf2c3c16
TT
19375 /* Vendor opcode descriptions. */
19376 if ((flags & 4) != 0)
19377 {
19378 unsigned int i, count;
757a13d0 19379
cf2c3c16
TT
19380 count = read_1_byte (abfd, mac_ptr);
19381 ++mac_ptr;
19382 for (i = 0; i < count; ++i)
19383 {
19384 unsigned int opcode, bytes_read;
19385 unsigned long arg;
19386
19387 opcode = read_1_byte (abfd, mac_ptr);
19388 ++mac_ptr;
19389 opcode_definitions[opcode] = mac_ptr;
19390 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19391 mac_ptr += bytes_read;
19392 mac_ptr += arg;
19393 }
757a13d0 19394 }
cf2c3c16 19395 }
757a13d0 19396
cf2c3c16
TT
19397 return mac_ptr;
19398}
757a13d0 19399
cf2c3c16 19400/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 19401 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
19402
19403static void
d521ce57
TT
19404dwarf_decode_macro_bytes (bfd *abfd,
19405 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 19406 struct macro_source_file *current_file,
15d034d0 19407 struct line_header *lh, const char *comp_dir,
cf2c3c16 19408 struct dwarf2_section_info *section,
36586728 19409 int section_is_gnu, int section_is_dwz,
cf2c3c16 19410 unsigned int offset_size,
8fc3fc34
TT
19411 struct objfile *objfile,
19412 htab_t include_hash)
cf2c3c16
TT
19413{
19414 enum dwarf_macro_record_type macinfo_type;
19415 int at_commandline;
d521ce57 19416 const gdb_byte *opcode_definitions[256];
757a13d0 19417
cf2c3c16
TT
19418 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19419 &offset_size, section_is_gnu);
19420 if (mac_ptr == NULL)
19421 {
19422 /* We already issued a complaint. */
19423 return;
19424 }
757a13d0
JK
19425
19426 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19427 GDB is still reading the definitions from command line. First
19428 DW_MACINFO_start_file will need to be ignored as it was already executed
19429 to create CURRENT_FILE for the main source holding also the command line
19430 definitions. On first met DW_MACINFO_start_file this flag is reset to
19431 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19432
19433 at_commandline = 1;
19434
19435 do
19436 {
19437 /* Do we at least have room for a macinfo type byte? */
19438 if (mac_ptr >= mac_end)
19439 {
f664829e 19440 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
19441 break;
19442 }
19443
19444 macinfo_type = read_1_byte (abfd, mac_ptr);
19445 mac_ptr++;
19446
cf2c3c16
TT
19447 /* Note that we rely on the fact that the corresponding GNU and
19448 DWARF constants are the same. */
757a13d0
JK
19449 switch (macinfo_type)
19450 {
19451 /* A zero macinfo type indicates the end of the macro
19452 information. */
19453 case 0:
19454 break;
2e276125 19455
cf2c3c16
TT
19456 case DW_MACRO_GNU_define:
19457 case DW_MACRO_GNU_undef:
19458 case DW_MACRO_GNU_define_indirect:
19459 case DW_MACRO_GNU_undef_indirect:
36586728
TT
19460 case DW_MACRO_GNU_define_indirect_alt:
19461 case DW_MACRO_GNU_undef_indirect_alt:
2e276125 19462 {
891d2f0b 19463 unsigned int bytes_read;
2e276125 19464 int line;
d521ce57 19465 const char *body;
cf2c3c16 19466 int is_define;
2e276125 19467
cf2c3c16
TT
19468 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19469 mac_ptr += bytes_read;
19470
19471 if (macinfo_type == DW_MACRO_GNU_define
19472 || macinfo_type == DW_MACRO_GNU_undef)
19473 {
19474 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19475 mac_ptr += bytes_read;
19476 }
19477 else
19478 {
19479 LONGEST str_offset;
19480
19481 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19482 mac_ptr += offset_size;
2e276125 19483
36586728 19484 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
f7a35f02
TT
19485 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19486 || section_is_dwz)
36586728
TT
19487 {
19488 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19489
19490 body = read_indirect_string_from_dwz (dwz, str_offset);
19491 }
19492 else
19493 body = read_indirect_string_at_offset (abfd, str_offset);
cf2c3c16
TT
19494 }
19495
19496 is_define = (macinfo_type == DW_MACRO_GNU_define
36586728
TT
19497 || macinfo_type == DW_MACRO_GNU_define_indirect
19498 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
2e276125 19499 if (! current_file)
757a13d0
JK
19500 {
19501 /* DWARF violation as no main source is present. */
19502 complaint (&symfile_complaints,
19503 _("debug info with no main source gives macro %s "
19504 "on line %d: %s"),
cf2c3c16
TT
19505 is_define ? _("definition") : _("undefinition"),
19506 line, body);
757a13d0
JK
19507 break;
19508 }
3e43a32a
MS
19509 if ((line == 0 && !at_commandline)
19510 || (line != 0 && at_commandline))
4d3c2250 19511 complaint (&symfile_complaints,
757a13d0
JK
19512 _("debug info gives %s macro %s with %s line %d: %s"),
19513 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 19514 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
19515 line == 0 ? _("zero") : _("non-zero"), line, body);
19516
cf2c3c16 19517 if (is_define)
757a13d0 19518 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
19519 else
19520 {
19521 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
36586728
TT
19522 || macinfo_type == DW_MACRO_GNU_undef_indirect
19523 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
cf2c3c16
TT
19524 macro_undef (current_file, line, body);
19525 }
2e276125
JB
19526 }
19527 break;
19528
cf2c3c16 19529 case DW_MACRO_GNU_start_file:
2e276125 19530 {
891d2f0b 19531 unsigned int bytes_read;
2e276125
JB
19532 int line, file;
19533
19534 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19535 mac_ptr += bytes_read;
19536 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19537 mac_ptr += bytes_read;
19538
3e43a32a
MS
19539 if ((line == 0 && !at_commandline)
19540 || (line != 0 && at_commandline))
757a13d0
JK
19541 complaint (&symfile_complaints,
19542 _("debug info gives source %d included "
19543 "from %s at %s line %d"),
19544 file, at_commandline ? _("command-line") : _("file"),
19545 line == 0 ? _("zero") : _("non-zero"), line);
19546
19547 if (at_commandline)
19548 {
cf2c3c16
TT
19549 /* This DW_MACRO_GNU_start_file was executed in the
19550 pass one. */
757a13d0
JK
19551 at_commandline = 0;
19552 }
19553 else
19554 current_file = macro_start_file (file, line,
19555 current_file, comp_dir,
cf2c3c16 19556 lh, objfile);
2e276125
JB
19557 }
19558 break;
19559
cf2c3c16 19560 case DW_MACRO_GNU_end_file:
2e276125 19561 if (! current_file)
4d3c2250 19562 complaint (&symfile_complaints,
3e43a32a
MS
19563 _("macro debug info has an unmatched "
19564 "`close_file' directive"));
2e276125
JB
19565 else
19566 {
19567 current_file = current_file->included_by;
19568 if (! current_file)
19569 {
cf2c3c16 19570 enum dwarf_macro_record_type next_type;
2e276125
JB
19571
19572 /* GCC circa March 2002 doesn't produce the zero
19573 type byte marking the end of the compilation
19574 unit. Complain if it's not there, but exit no
19575 matter what. */
19576
19577 /* Do we at least have room for a macinfo type byte? */
19578 if (mac_ptr >= mac_end)
19579 {
f664829e 19580 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
19581 return;
19582 }
19583
19584 /* We don't increment mac_ptr here, so this is just
19585 a look-ahead. */
19586 next_type = read_1_byte (abfd, mac_ptr);
19587 if (next_type != 0)
4d3c2250 19588 complaint (&symfile_complaints,
3e43a32a
MS
19589 _("no terminating 0-type entry for "
19590 "macros in `.debug_macinfo' section"));
2e276125
JB
19591
19592 return;
19593 }
19594 }
19595 break;
19596
cf2c3c16 19597 case DW_MACRO_GNU_transparent_include:
36586728 19598 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19599 {
19600 LONGEST offset;
8fc3fc34 19601 void **slot;
a036ba48
TT
19602 bfd *include_bfd = abfd;
19603 struct dwarf2_section_info *include_section = section;
19604 struct dwarf2_section_info alt_section;
d521ce57 19605 const gdb_byte *include_mac_end = mac_end;
a036ba48 19606 int is_dwz = section_is_dwz;
d521ce57 19607 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
19608
19609 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19610 mac_ptr += offset_size;
19611
a036ba48
TT
19612 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19613 {
19614 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19615
19616 dwarf2_read_section (dwarf2_per_objfile->objfile,
19617 &dwz->macro);
19618
19619 include_bfd = dwz->macro.asection->owner;
19620 include_section = &dwz->macro;
19621 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19622 is_dwz = 1;
19623 }
19624
19625 new_mac_ptr = include_section->buffer + offset;
19626 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19627
8fc3fc34
TT
19628 if (*slot != NULL)
19629 {
19630 /* This has actually happened; see
19631 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19632 complaint (&symfile_complaints,
19633 _("recursive DW_MACRO_GNU_transparent_include in "
19634 ".debug_macro section"));
19635 }
19636 else
19637 {
d521ce57 19638 *slot = (void *) new_mac_ptr;
36586728 19639
a036ba48 19640 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
36586728 19641 include_mac_end, current_file,
8fc3fc34 19642 lh, comp_dir,
36586728 19643 section, section_is_gnu, is_dwz,
8fc3fc34
TT
19644 offset_size, objfile, include_hash);
19645
d521ce57 19646 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 19647 }
cf2c3c16
TT
19648 }
19649 break;
19650
2e276125 19651 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
19652 if (!section_is_gnu)
19653 {
19654 unsigned int bytes_read;
19655 int constant;
2e276125 19656
cf2c3c16
TT
19657 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19658 mac_ptr += bytes_read;
19659 read_direct_string (abfd, mac_ptr, &bytes_read);
19660 mac_ptr += bytes_read;
2e276125 19661
cf2c3c16
TT
19662 /* We don't recognize any vendor extensions. */
19663 break;
19664 }
19665 /* FALLTHROUGH */
19666
19667 default:
19668 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19669 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19670 section);
19671 if (mac_ptr == NULL)
19672 return;
19673 break;
2e276125 19674 }
757a13d0 19675 } while (macinfo_type != 0);
2e276125 19676}
8e19ed76 19677
cf2c3c16 19678static void
09262596 19679dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
15d034d0 19680 const char *comp_dir, int section_is_gnu)
cf2c3c16 19681{
bb5ed363 19682 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
19683 struct line_header *lh = cu->line_header;
19684 bfd *abfd;
d521ce57 19685 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
19686 struct macro_source_file *current_file = 0;
19687 enum dwarf_macro_record_type macinfo_type;
19688 unsigned int offset_size = cu->header.offset_size;
d521ce57 19689 const gdb_byte *opcode_definitions[256];
8fc3fc34
TT
19690 struct cleanup *cleanup;
19691 htab_t include_hash;
19692 void **slot;
09262596
DE
19693 struct dwarf2_section_info *section;
19694 const char *section_name;
19695
19696 if (cu->dwo_unit != NULL)
19697 {
19698 if (section_is_gnu)
19699 {
19700 section = &cu->dwo_unit->dwo_file->sections.macro;
19701 section_name = ".debug_macro.dwo";
19702 }
19703 else
19704 {
19705 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19706 section_name = ".debug_macinfo.dwo";
19707 }
19708 }
19709 else
19710 {
19711 if (section_is_gnu)
19712 {
19713 section = &dwarf2_per_objfile->macro;
19714 section_name = ".debug_macro";
19715 }
19716 else
19717 {
19718 section = &dwarf2_per_objfile->macinfo;
19719 section_name = ".debug_macinfo";
19720 }
19721 }
cf2c3c16 19722
bb5ed363 19723 dwarf2_read_section (objfile, section);
cf2c3c16
TT
19724 if (section->buffer == NULL)
19725 {
fceca515 19726 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
19727 return;
19728 }
09262596 19729 abfd = section->asection->owner;
cf2c3c16
TT
19730
19731 /* First pass: Find the name of the base filename.
19732 This filename is needed in order to process all macros whose definition
19733 (or undefinition) comes from the command line. These macros are defined
19734 before the first DW_MACINFO_start_file entry, and yet still need to be
19735 associated to the base file.
19736
19737 To determine the base file name, we scan the macro definitions until we
19738 reach the first DW_MACINFO_start_file entry. We then initialize
19739 CURRENT_FILE accordingly so that any macro definition found before the
19740 first DW_MACINFO_start_file can still be associated to the base file. */
19741
19742 mac_ptr = section->buffer + offset;
19743 mac_end = section->buffer + section->size;
19744
19745 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19746 &offset_size, section_is_gnu);
19747 if (mac_ptr == NULL)
19748 {
19749 /* We already issued a complaint. */
19750 return;
19751 }
19752
19753 do
19754 {
19755 /* Do we at least have room for a macinfo type byte? */
19756 if (mac_ptr >= mac_end)
19757 {
19758 /* Complaint is printed during the second pass as GDB will probably
19759 stop the first pass earlier upon finding
19760 DW_MACINFO_start_file. */
19761 break;
19762 }
19763
19764 macinfo_type = read_1_byte (abfd, mac_ptr);
19765 mac_ptr++;
19766
19767 /* Note that we rely on the fact that the corresponding GNU and
19768 DWARF constants are the same. */
19769 switch (macinfo_type)
19770 {
19771 /* A zero macinfo type indicates the end of the macro
19772 information. */
19773 case 0:
19774 break;
19775
19776 case DW_MACRO_GNU_define:
19777 case DW_MACRO_GNU_undef:
19778 /* Only skip the data by MAC_PTR. */
19779 {
19780 unsigned int bytes_read;
19781
19782 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19783 mac_ptr += bytes_read;
19784 read_direct_string (abfd, mac_ptr, &bytes_read);
19785 mac_ptr += bytes_read;
19786 }
19787 break;
19788
19789 case DW_MACRO_GNU_start_file:
19790 {
19791 unsigned int bytes_read;
19792 int line, file;
19793
19794 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19795 mac_ptr += bytes_read;
19796 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19797 mac_ptr += bytes_read;
19798
19799 current_file = macro_start_file (file, line, current_file,
bb5ed363 19800 comp_dir, lh, objfile);
cf2c3c16
TT
19801 }
19802 break;
19803
19804 case DW_MACRO_GNU_end_file:
19805 /* No data to skip by MAC_PTR. */
19806 break;
19807
19808 case DW_MACRO_GNU_define_indirect:
19809 case DW_MACRO_GNU_undef_indirect:
f7a35f02
TT
19810 case DW_MACRO_GNU_define_indirect_alt:
19811 case DW_MACRO_GNU_undef_indirect_alt:
cf2c3c16
TT
19812 {
19813 unsigned int bytes_read;
19814
19815 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19816 mac_ptr += bytes_read;
19817 mac_ptr += offset_size;
19818 }
19819 break;
19820
19821 case DW_MACRO_GNU_transparent_include:
f7a35f02 19822 case DW_MACRO_GNU_transparent_include_alt:
cf2c3c16
TT
19823 /* Note that, according to the spec, a transparent include
19824 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19825 skip this opcode. */
19826 mac_ptr += offset_size;
19827 break;
19828
19829 case DW_MACINFO_vendor_ext:
19830 /* Only skip the data by MAC_PTR. */
19831 if (!section_is_gnu)
19832 {
19833 unsigned int bytes_read;
19834
19835 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19836 mac_ptr += bytes_read;
19837 read_direct_string (abfd, mac_ptr, &bytes_read);
19838 mac_ptr += bytes_read;
19839 }
19840 /* FALLTHROUGH */
19841
19842 default:
19843 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 19844 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
19845 section);
19846 if (mac_ptr == NULL)
19847 return;
19848 break;
19849 }
19850 } while (macinfo_type != 0 && current_file == NULL);
19851
19852 /* Second pass: Process all entries.
19853
19854 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19855 command-line macro definitions/undefinitions. This flag is unset when we
19856 reach the first DW_MACINFO_start_file entry. */
19857
8fc3fc34
TT
19858 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19859 NULL, xcalloc, xfree);
19860 cleanup = make_cleanup_htab_delete (include_hash);
19861 mac_ptr = section->buffer + offset;
19862 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
d521ce57 19863 *slot = (void *) mac_ptr;
8fc3fc34 19864 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
36586728
TT
19865 current_file, lh, comp_dir, section,
19866 section_is_gnu, 0,
8fc3fc34
TT
19867 offset_size, objfile, include_hash);
19868 do_cleanups (cleanup);
cf2c3c16
TT
19869}
19870
8e19ed76 19871/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 19872 if so return true else false. */
380bca97 19873
8e19ed76 19874static int
6e5a29e1 19875attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
19876{
19877 return (attr == NULL ? 0 :
19878 attr->form == DW_FORM_block1
19879 || attr->form == DW_FORM_block2
19880 || attr->form == DW_FORM_block4
2dc7f7b3
TT
19881 || attr->form == DW_FORM_block
19882 || attr->form == DW_FORM_exprloc);
8e19ed76 19883}
4c2df51b 19884
c6a0999f
JB
19885/* Return non-zero if ATTR's value is a section offset --- classes
19886 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19887 You may use DW_UNSND (attr) to retrieve such offsets.
19888
19889 Section 7.5.4, "Attribute Encodings", explains that no attribute
19890 may have a value that belongs to more than one of these classes; it
19891 would be ambiguous if we did, because we use the same forms for all
19892 of them. */
380bca97 19893
3690dd37 19894static int
6e5a29e1 19895attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
19896{
19897 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
19898 || attr->form == DW_FORM_data8
19899 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
19900}
19901
3690dd37
JB
19902/* Return non-zero if ATTR's value falls in the 'constant' class, or
19903 zero otherwise. When this function returns true, you can apply
19904 dwarf2_get_attr_constant_value to it.
19905
19906 However, note that for some attributes you must check
19907 attr_form_is_section_offset before using this test. DW_FORM_data4
19908 and DW_FORM_data8 are members of both the constant class, and of
19909 the classes that contain offsets into other debug sections
19910 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19911 that, if an attribute's can be either a constant or one of the
19912 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19913 taken as section offsets, not constants. */
380bca97 19914
3690dd37 19915static int
6e5a29e1 19916attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
19917{
19918 switch (attr->form)
19919 {
19920 case DW_FORM_sdata:
19921 case DW_FORM_udata:
19922 case DW_FORM_data1:
19923 case DW_FORM_data2:
19924 case DW_FORM_data4:
19925 case DW_FORM_data8:
19926 return 1;
19927 default:
19928 return 0;
19929 }
19930}
19931
7771576e
SA
19932
19933/* DW_ADDR is always stored already as sect_offset; despite for the forms
19934 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19935
19936static int
6e5a29e1 19937attr_form_is_ref (const struct attribute *attr)
7771576e
SA
19938{
19939 switch (attr->form)
19940 {
19941 case DW_FORM_ref_addr:
19942 case DW_FORM_ref1:
19943 case DW_FORM_ref2:
19944 case DW_FORM_ref4:
19945 case DW_FORM_ref8:
19946 case DW_FORM_ref_udata:
19947 case DW_FORM_GNU_ref_alt:
19948 return 1;
19949 default:
19950 return 0;
19951 }
19952}
19953
3019eac3
DE
19954/* Return the .debug_loc section to use for CU.
19955 For DWO files use .debug_loc.dwo. */
19956
19957static struct dwarf2_section_info *
19958cu_debug_loc_section (struct dwarf2_cu *cu)
19959{
19960 if (cu->dwo_unit)
19961 return &cu->dwo_unit->dwo_file->sections.loc;
19962 return &dwarf2_per_objfile->loc;
19963}
19964
8cf6f0b1
TT
19965/* A helper function that fills in a dwarf2_loclist_baton. */
19966
19967static void
19968fill_in_loclist_baton (struct dwarf2_cu *cu,
19969 struct dwarf2_loclist_baton *baton,
ff39bb5e 19970 const struct attribute *attr)
8cf6f0b1 19971{
3019eac3
DE
19972 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19973
19974 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
19975
19976 baton->per_cu = cu->per_cu;
19977 gdb_assert (baton->per_cu);
19978 /* We don't know how long the location list is, but make sure we
19979 don't run off the edge of the section. */
3019eac3
DE
19980 baton->size = section->size - DW_UNSND (attr);
19981 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 19982 baton->base_address = cu->base_address;
f664829e 19983 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
19984}
19985
4c2df51b 19986static void
ff39bb5e 19987dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 19988 struct dwarf2_cu *cu, int is_block)
4c2df51b 19989{
bb5ed363 19990 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 19991 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 19992
3690dd37 19993 if (attr_form_is_section_offset (attr)
3019eac3 19994 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
19995 the section. If so, fall through to the complaint in the
19996 other branch. */
3019eac3 19997 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 19998 {
0d53c4c4 19999 struct dwarf2_loclist_baton *baton;
4c2df51b 20000
bb5ed363 20001 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20002 sizeof (struct dwarf2_loclist_baton));
4c2df51b 20003
8cf6f0b1 20004 fill_in_loclist_baton (cu, baton, attr);
be391dca 20005
d00adf39 20006 if (cu->base_known == 0)
0d53c4c4 20007 complaint (&symfile_complaints,
3e43a32a
MS
20008 _("Location list used without "
20009 "specifying the CU base address."));
4c2df51b 20010
f1e6e072
TT
20011 SYMBOL_ACLASS_INDEX (sym) = (is_block
20012 ? dwarf2_loclist_block_index
20013 : dwarf2_loclist_index);
0d53c4c4
DJ
20014 SYMBOL_LOCATION_BATON (sym) = baton;
20015 }
20016 else
20017 {
20018 struct dwarf2_locexpr_baton *baton;
20019
bb5ed363 20020 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 20021 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
20022 baton->per_cu = cu->per_cu;
20023 gdb_assert (baton->per_cu);
0d53c4c4
DJ
20024
20025 if (attr_form_is_block (attr))
20026 {
20027 /* Note that we're just copying the block's data pointer
20028 here, not the actual data. We're still pointing into the
6502dd73
DJ
20029 info_buffer for SYM's objfile; right now we never release
20030 that buffer, but when we do clean up properly this may
20031 need to change. */
0d53c4c4
DJ
20032 baton->size = DW_BLOCK (attr)->size;
20033 baton->data = DW_BLOCK (attr)->data;
20034 }
20035 else
20036 {
20037 dwarf2_invalid_attrib_class_complaint ("location description",
20038 SYMBOL_NATURAL_NAME (sym));
20039 baton->size = 0;
0d53c4c4 20040 }
6e70227d 20041
f1e6e072
TT
20042 SYMBOL_ACLASS_INDEX (sym) = (is_block
20043 ? dwarf2_locexpr_block_index
20044 : dwarf2_locexpr_index);
0d53c4c4
DJ
20045 SYMBOL_LOCATION_BATON (sym) = baton;
20046 }
4c2df51b 20047}
6502dd73 20048
9aa1f1e3
TT
20049/* Return the OBJFILE associated with the compilation unit CU. If CU
20050 came from a separate debuginfo file, then the master objfile is
20051 returned. */
ae0d2f24
UW
20052
20053struct objfile *
20054dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20055{
9291a0cd 20056 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
20057
20058 /* Return the master objfile, so that we can report and look up the
20059 correct file containing this variable. */
20060 if (objfile->separate_debug_objfile_backlink)
20061 objfile = objfile->separate_debug_objfile_backlink;
20062
20063 return objfile;
20064}
20065
96408a79
SA
20066/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20067 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20068 CU_HEADERP first. */
20069
20070static const struct comp_unit_head *
20071per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20072 struct dwarf2_per_cu_data *per_cu)
20073{
d521ce57 20074 const gdb_byte *info_ptr;
96408a79
SA
20075
20076 if (per_cu->cu)
20077 return &per_cu->cu->header;
20078
8a0459fd 20079 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
96408a79
SA
20080
20081 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 20082 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
20083
20084 return cu_headerp;
20085}
20086
ae0d2f24
UW
20087/* Return the address size given in the compilation unit header for CU. */
20088
98714339 20089int
ae0d2f24
UW
20090dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20091{
96408a79
SA
20092 struct comp_unit_head cu_header_local;
20093 const struct comp_unit_head *cu_headerp;
c471e790 20094
96408a79
SA
20095 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20096
20097 return cu_headerp->addr_size;
ae0d2f24
UW
20098}
20099
9eae7c52
TT
20100/* Return the offset size given in the compilation unit header for CU. */
20101
20102int
20103dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20104{
96408a79
SA
20105 struct comp_unit_head cu_header_local;
20106 const struct comp_unit_head *cu_headerp;
9c6c53f7 20107
96408a79
SA
20108 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20109
20110 return cu_headerp->offset_size;
20111}
20112
20113/* See its dwarf2loc.h declaration. */
20114
20115int
20116dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20117{
20118 struct comp_unit_head cu_header_local;
20119 const struct comp_unit_head *cu_headerp;
20120
20121 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20122
20123 if (cu_headerp->version == 2)
20124 return cu_headerp->addr_size;
20125 else
20126 return cu_headerp->offset_size;
181cebd4
JK
20127}
20128
9aa1f1e3
TT
20129/* Return the text offset of the CU. The returned offset comes from
20130 this CU's objfile. If this objfile came from a separate debuginfo
20131 file, then the offset may be different from the corresponding
20132 offset in the parent objfile. */
20133
20134CORE_ADDR
20135dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20136{
bb3fa9d0 20137 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
20138
20139 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20140}
20141
348e048f
DE
20142/* Locate the .debug_info compilation unit from CU's objfile which contains
20143 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
20144
20145static struct dwarf2_per_cu_data *
b64f50a1 20146dwarf2_find_containing_comp_unit (sect_offset offset,
36586728 20147 unsigned int offset_in_dwz,
ae038cb0
DJ
20148 struct objfile *objfile)
20149{
20150 struct dwarf2_per_cu_data *this_cu;
20151 int low, high;
36586728 20152 const sect_offset *cu_off;
ae038cb0 20153
ae038cb0
DJ
20154 low = 0;
20155 high = dwarf2_per_objfile->n_comp_units - 1;
20156 while (high > low)
20157 {
36586728 20158 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 20159 int mid = low + (high - low) / 2;
9a619af0 20160
36586728
TT
20161 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20162 cu_off = &mid_cu->offset;
20163 if (mid_cu->is_dwz > offset_in_dwz
20164 || (mid_cu->is_dwz == offset_in_dwz
20165 && cu_off->sect_off >= offset.sect_off))
ae038cb0
DJ
20166 high = mid;
20167 else
20168 low = mid + 1;
20169 }
20170 gdb_assert (low == high);
36586728
TT
20171 this_cu = dwarf2_per_objfile->all_comp_units[low];
20172 cu_off = &this_cu->offset;
20173 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
ae038cb0 20174 {
36586728 20175 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8
AC
20176 error (_("Dwarf Error: could not find partial DIE containing "
20177 "offset 0x%lx [in module %s]"),
b64f50a1 20178 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 20179
b64f50a1
JK
20180 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20181 <= offset.sect_off);
ae038cb0
DJ
20182 return dwarf2_per_objfile->all_comp_units[low-1];
20183 }
20184 else
20185 {
20186 this_cu = dwarf2_per_objfile->all_comp_units[low];
20187 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
20188 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20189 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20190 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
20191 return this_cu;
20192 }
20193}
20194
23745b47 20195/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 20196
9816fde3 20197static void
23745b47 20198init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 20199{
9816fde3 20200 memset (cu, 0, sizeof (*cu));
23745b47
DE
20201 per_cu->cu = cu;
20202 cu->per_cu = per_cu;
20203 cu->objfile = per_cu->objfile;
93311388 20204 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
20205}
20206
20207/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20208
20209static void
95554aad
TT
20210prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20211 enum language pretend_language)
9816fde3
JK
20212{
20213 struct attribute *attr;
20214
20215 /* Set the language we're debugging. */
20216 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20217 if (attr)
20218 set_cu_language (DW_UNSND (attr), cu);
20219 else
9cded63f 20220 {
95554aad 20221 cu->language = pretend_language;
9cded63f
TT
20222 cu->language_defn = language_def (cu->language);
20223 }
dee91e82
DE
20224
20225 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20226 if (attr)
20227 cu->producer = DW_STRING (attr);
93311388
DE
20228}
20229
ae038cb0
DJ
20230/* Release one cached compilation unit, CU. We unlink it from the tree
20231 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
20232 the caller is responsible for that.
20233 NOTE: DATA is a void * because this function is also used as a
20234 cleanup routine. */
ae038cb0
DJ
20235
20236static void
68dc6402 20237free_heap_comp_unit (void *data)
ae038cb0
DJ
20238{
20239 struct dwarf2_cu *cu = data;
20240
23745b47
DE
20241 gdb_assert (cu->per_cu != NULL);
20242 cu->per_cu->cu = NULL;
ae038cb0
DJ
20243 cu->per_cu = NULL;
20244
20245 obstack_free (&cu->comp_unit_obstack, NULL);
20246
20247 xfree (cu);
20248}
20249
72bf9492 20250/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 20251 when we're finished with it. We can't free the pointer itself, but be
dee91e82 20252 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
20253
20254static void
20255free_stack_comp_unit (void *data)
20256{
20257 struct dwarf2_cu *cu = data;
20258
23745b47
DE
20259 gdb_assert (cu->per_cu != NULL);
20260 cu->per_cu->cu = NULL;
20261 cu->per_cu = NULL;
20262
72bf9492
DJ
20263 obstack_free (&cu->comp_unit_obstack, NULL);
20264 cu->partial_dies = NULL;
ae038cb0
DJ
20265}
20266
20267/* Free all cached compilation units. */
20268
20269static void
20270free_cached_comp_units (void *data)
20271{
20272 struct dwarf2_per_cu_data *per_cu, **last_chain;
20273
20274 per_cu = dwarf2_per_objfile->read_in_chain;
20275 last_chain = &dwarf2_per_objfile->read_in_chain;
20276 while (per_cu != NULL)
20277 {
20278 struct dwarf2_per_cu_data *next_cu;
20279
20280 next_cu = per_cu->cu->read_in_chain;
20281
68dc6402 20282 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20283 *last_chain = next_cu;
20284
20285 per_cu = next_cu;
20286 }
20287}
20288
20289/* Increase the age counter on each cached compilation unit, and free
20290 any that are too old. */
20291
20292static void
20293age_cached_comp_units (void)
20294{
20295 struct dwarf2_per_cu_data *per_cu, **last_chain;
20296
20297 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20298 per_cu = dwarf2_per_objfile->read_in_chain;
20299 while (per_cu != NULL)
20300 {
20301 per_cu->cu->last_used ++;
20302 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20303 dwarf2_mark (per_cu->cu);
20304 per_cu = per_cu->cu->read_in_chain;
20305 }
20306
20307 per_cu = dwarf2_per_objfile->read_in_chain;
20308 last_chain = &dwarf2_per_objfile->read_in_chain;
20309 while (per_cu != NULL)
20310 {
20311 struct dwarf2_per_cu_data *next_cu;
20312
20313 next_cu = per_cu->cu->read_in_chain;
20314
20315 if (!per_cu->cu->mark)
20316 {
68dc6402 20317 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
20318 *last_chain = next_cu;
20319 }
20320 else
20321 last_chain = &per_cu->cu->read_in_chain;
20322
20323 per_cu = next_cu;
20324 }
20325}
20326
20327/* Remove a single compilation unit from the cache. */
20328
20329static void
dee91e82 20330free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
20331{
20332 struct dwarf2_per_cu_data *per_cu, **last_chain;
20333
20334 per_cu = dwarf2_per_objfile->read_in_chain;
20335 last_chain = &dwarf2_per_objfile->read_in_chain;
20336 while (per_cu != NULL)
20337 {
20338 struct dwarf2_per_cu_data *next_cu;
20339
20340 next_cu = per_cu->cu->read_in_chain;
20341
dee91e82 20342 if (per_cu == target_per_cu)
ae038cb0 20343 {
68dc6402 20344 free_heap_comp_unit (per_cu->cu);
dee91e82 20345 per_cu->cu = NULL;
ae038cb0
DJ
20346 *last_chain = next_cu;
20347 break;
20348 }
20349 else
20350 last_chain = &per_cu->cu->read_in_chain;
20351
20352 per_cu = next_cu;
20353 }
20354}
20355
fe3e1990
DJ
20356/* Release all extra memory associated with OBJFILE. */
20357
20358void
20359dwarf2_free_objfile (struct objfile *objfile)
20360{
20361 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20362
20363 if (dwarf2_per_objfile == NULL)
20364 return;
20365
20366 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20367 free_cached_comp_units (NULL);
20368
7b9f3c50
DE
20369 if (dwarf2_per_objfile->quick_file_names_table)
20370 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 20371
fe3e1990
DJ
20372 /* Everything else should be on the objfile obstack. */
20373}
20374
dee91e82
DE
20375/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20376 We store these in a hash table separate from the DIEs, and preserve them
20377 when the DIEs are flushed out of cache.
20378
20379 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 20380 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
20381 or the type may come from a DWO file. Furthermore, while it's more logical
20382 to use per_cu->section+offset, with Fission the section with the data is in
20383 the DWO file but we don't know that section at the point we need it.
20384 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20385 because we can enter the lookup routine, get_die_type_at_offset, from
20386 outside this file, and thus won't necessarily have PER_CU->cu.
20387 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 20388
dee91e82 20389struct dwarf2_per_cu_offset_and_type
1c379e20 20390{
dee91e82 20391 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 20392 sect_offset offset;
1c379e20
DJ
20393 struct type *type;
20394};
20395
dee91e82 20396/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20397
20398static hashval_t
dee91e82 20399per_cu_offset_and_type_hash (const void *item)
1c379e20 20400{
dee91e82 20401 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 20402
dee91e82 20403 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
20404}
20405
dee91e82 20406/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
20407
20408static int
dee91e82 20409per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 20410{
dee91e82
DE
20411 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20412 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 20413
dee91e82
DE
20414 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20415 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
20416}
20417
20418/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
20419 table if necessary. For convenience, return TYPE.
20420
20421 The DIEs reading must have careful ordering to:
20422 * Not cause infite loops trying to read in DIEs as a prerequisite for
20423 reading current DIE.
20424 * Not trying to dereference contents of still incompletely read in types
20425 while reading in other DIEs.
20426 * Enable referencing still incompletely read in types just by a pointer to
20427 the type without accessing its fields.
20428
20429 Therefore caller should follow these rules:
20430 * Try to fetch any prerequisite types we may need to build this DIE type
20431 before building the type and calling set_die_type.
e71ec853 20432 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
20433 possible before fetching more types to complete the current type.
20434 * Make the type as complete as possible before fetching more types. */
1c379e20 20435
f792889a 20436static struct type *
1c379e20
DJ
20437set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20438{
dee91e82 20439 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 20440 struct objfile *objfile = cu->objfile;
1c379e20 20441
b4ba55a1
JB
20442 /* For Ada types, make sure that the gnat-specific data is always
20443 initialized (if not already set). There are a few types where
20444 we should not be doing so, because the type-specific area is
20445 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20446 where the type-specific area is used to store the floatformat).
20447 But this is not a problem, because the gnat-specific information
20448 is actually not needed for these types. */
20449 if (need_gnat_info (cu)
20450 && TYPE_CODE (type) != TYPE_CODE_FUNC
20451 && TYPE_CODE (type) != TYPE_CODE_FLT
20452 && !HAVE_GNAT_AUX_INFO (type))
20453 INIT_GNAT_SPECIFIC (type);
20454
dee91e82 20455 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20456 {
dee91e82
DE
20457 dwarf2_per_objfile->die_type_hash =
20458 htab_create_alloc_ex (127,
20459 per_cu_offset_and_type_hash,
20460 per_cu_offset_and_type_eq,
20461 NULL,
20462 &objfile->objfile_obstack,
20463 hashtab_obstack_allocate,
20464 dummy_obstack_deallocate);
f792889a 20465 }
1c379e20 20466
dee91e82 20467 ofs.per_cu = cu->per_cu;
1c379e20
DJ
20468 ofs.offset = die->offset;
20469 ofs.type = type;
dee91e82
DE
20470 slot = (struct dwarf2_per_cu_offset_and_type **)
20471 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
20472 if (*slot)
20473 complaint (&symfile_complaints,
20474 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 20475 die->offset.sect_off);
673bfd45 20476 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 20477 **slot = ofs;
f792889a 20478 return type;
1c379e20
DJ
20479}
20480
02142a6c
DE
20481/* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20482 or return NULL if the die does not have a saved type. */
1c379e20
DJ
20483
20484static struct type *
b64f50a1 20485get_die_type_at_offset (sect_offset offset,
673bfd45 20486 struct dwarf2_per_cu_data *per_cu)
1c379e20 20487{
dee91e82 20488 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 20489
dee91e82 20490 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 20491 return NULL;
1c379e20 20492
dee91e82 20493 ofs.per_cu = per_cu;
673bfd45 20494 ofs.offset = offset;
dee91e82 20495 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
20496 if (slot)
20497 return slot->type;
20498 else
20499 return NULL;
20500}
20501
02142a6c 20502/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
20503 or return NULL if DIE does not have a saved type. */
20504
20505static struct type *
20506get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20507{
20508 return get_die_type_at_offset (die->offset, cu->per_cu);
20509}
20510
10b3939b
DJ
20511/* Add a dependence relationship from CU to REF_PER_CU. */
20512
20513static void
20514dwarf2_add_dependence (struct dwarf2_cu *cu,
20515 struct dwarf2_per_cu_data *ref_per_cu)
20516{
20517 void **slot;
20518
20519 if (cu->dependencies == NULL)
20520 cu->dependencies
20521 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20522 NULL, &cu->comp_unit_obstack,
20523 hashtab_obstack_allocate,
20524 dummy_obstack_deallocate);
20525
20526 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20527 if (*slot == NULL)
20528 *slot = ref_per_cu;
20529}
1c379e20 20530
f504f079
DE
20531/* Subroutine of dwarf2_mark to pass to htab_traverse.
20532 Set the mark field in every compilation unit in the
ae038cb0
DJ
20533 cache that we must keep because we are keeping CU. */
20534
10b3939b
DJ
20535static int
20536dwarf2_mark_helper (void **slot, void *data)
20537{
20538 struct dwarf2_per_cu_data *per_cu;
20539
20540 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
20541
20542 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20543 reading of the chain. As such dependencies remain valid it is not much
20544 useful to track and undo them during QUIT cleanups. */
20545 if (per_cu->cu == NULL)
20546 return 1;
20547
10b3939b
DJ
20548 if (per_cu->cu->mark)
20549 return 1;
20550 per_cu->cu->mark = 1;
20551
20552 if (per_cu->cu->dependencies != NULL)
20553 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20554
20555 return 1;
20556}
20557
f504f079
DE
20558/* Set the mark field in CU and in every other compilation unit in the
20559 cache that we must keep because we are keeping CU. */
20560
ae038cb0
DJ
20561static void
20562dwarf2_mark (struct dwarf2_cu *cu)
20563{
20564 if (cu->mark)
20565 return;
20566 cu->mark = 1;
10b3939b
DJ
20567 if (cu->dependencies != NULL)
20568 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
20569}
20570
20571static void
20572dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20573{
20574 while (per_cu)
20575 {
20576 per_cu->cu->mark = 0;
20577 per_cu = per_cu->cu->read_in_chain;
20578 }
72bf9492
DJ
20579}
20580
72bf9492
DJ
20581/* Trivial hash function for partial_die_info: the hash value of a DIE
20582 is its offset in .debug_info for this objfile. */
20583
20584static hashval_t
20585partial_die_hash (const void *item)
20586{
20587 const struct partial_die_info *part_die = item;
9a619af0 20588
b64f50a1 20589 return part_die->offset.sect_off;
72bf9492
DJ
20590}
20591
20592/* Trivial comparison function for partial_die_info structures: two DIEs
20593 are equal if they have the same offset. */
20594
20595static int
20596partial_die_eq (const void *item_lhs, const void *item_rhs)
20597{
20598 const struct partial_die_info *part_die_lhs = item_lhs;
20599 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 20600
b64f50a1 20601 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
20602}
20603
ae038cb0
DJ
20604static struct cmd_list_element *set_dwarf2_cmdlist;
20605static struct cmd_list_element *show_dwarf2_cmdlist;
20606
20607static void
20608set_dwarf2_cmd (char *args, int from_tty)
20609{
20610 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20611}
20612
20613static void
20614show_dwarf2_cmd (char *args, int from_tty)
6e70227d 20615{
ae038cb0
DJ
20616 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20617}
20618
4bf44c1c 20619/* Free data associated with OBJFILE, if necessary. */
dce234bc
PP
20620
20621static void
c1bd65d0 20622dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
20623{
20624 struct dwarf2_per_objfile *data = d;
8b70b953 20625 int ix;
8b70b953 20626
626f2d1c
TT
20627 /* Make sure we don't accidentally use dwarf2_per_objfile while
20628 cleaning up. */
20629 dwarf2_per_objfile = NULL;
20630
59b0c7c1
JB
20631 for (ix = 0; ix < data->n_comp_units; ++ix)
20632 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
796a7ff8 20633
59b0c7c1 20634 for (ix = 0; ix < data->n_type_units; ++ix)
796a7ff8 20635 VEC_free (dwarf2_per_cu_ptr,
59b0c7c1
JB
20636 data->all_type_units[ix]->per_cu.imported_symtabs);
20637 xfree (data->all_type_units);
95554aad 20638
8b70b953 20639 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
20640
20641 if (data->dwo_files)
20642 free_dwo_files (data->dwo_files, objfile);
5c6fa7ab
DE
20643 if (data->dwp_file)
20644 gdb_bfd_unref (data->dwp_file->dbfd);
36586728
TT
20645
20646 if (data->dwz_file && data->dwz_file->dwz_bfd)
20647 gdb_bfd_unref (data->dwz_file->dwz_bfd);
9291a0cd
TT
20648}
20649
20650\f
ae2de4f8 20651/* The "save gdb-index" command. */
9291a0cd
TT
20652
20653/* The contents of the hash table we create when building the string
20654 table. */
20655struct strtab_entry
20656{
20657 offset_type offset;
20658 const char *str;
20659};
20660
559a7a62
JK
20661/* Hash function for a strtab_entry.
20662
20663 Function is used only during write_hash_table so no index format backward
20664 compatibility is needed. */
b89be57b 20665
9291a0cd
TT
20666static hashval_t
20667hash_strtab_entry (const void *e)
20668{
20669 const struct strtab_entry *entry = e;
559a7a62 20670 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
20671}
20672
20673/* Equality function for a strtab_entry. */
b89be57b 20674
9291a0cd
TT
20675static int
20676eq_strtab_entry (const void *a, const void *b)
20677{
20678 const struct strtab_entry *ea = a;
20679 const struct strtab_entry *eb = b;
20680 return !strcmp (ea->str, eb->str);
20681}
20682
20683/* Create a strtab_entry hash table. */
b89be57b 20684
9291a0cd
TT
20685static htab_t
20686create_strtab (void)
20687{
20688 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20689 xfree, xcalloc, xfree);
20690}
20691
20692/* Add a string to the constant pool. Return the string's offset in
20693 host order. */
b89be57b 20694
9291a0cd
TT
20695static offset_type
20696add_string (htab_t table, struct obstack *cpool, const char *str)
20697{
20698 void **slot;
20699 struct strtab_entry entry;
20700 struct strtab_entry *result;
20701
20702 entry.str = str;
20703 slot = htab_find_slot (table, &entry, INSERT);
20704 if (*slot)
20705 result = *slot;
20706 else
20707 {
20708 result = XNEW (struct strtab_entry);
20709 result->offset = obstack_object_size (cpool);
20710 result->str = str;
20711 obstack_grow_str0 (cpool, str);
20712 *slot = result;
20713 }
20714 return result->offset;
20715}
20716
20717/* An entry in the symbol table. */
20718struct symtab_index_entry
20719{
20720 /* The name of the symbol. */
20721 const char *name;
20722 /* The offset of the name in the constant pool. */
20723 offset_type index_offset;
20724 /* A sorted vector of the indices of all the CUs that hold an object
20725 of this name. */
20726 VEC (offset_type) *cu_indices;
20727};
20728
20729/* The symbol table. This is a power-of-2-sized hash table. */
20730struct mapped_symtab
20731{
20732 offset_type n_elements;
20733 offset_type size;
20734 struct symtab_index_entry **data;
20735};
20736
20737/* Hash function for a symtab_index_entry. */
b89be57b 20738
9291a0cd
TT
20739static hashval_t
20740hash_symtab_entry (const void *e)
20741{
20742 const struct symtab_index_entry *entry = e;
20743 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20744 sizeof (offset_type) * VEC_length (offset_type,
20745 entry->cu_indices),
20746 0);
20747}
20748
20749/* Equality function for a symtab_index_entry. */
b89be57b 20750
9291a0cd
TT
20751static int
20752eq_symtab_entry (const void *a, const void *b)
20753{
20754 const struct symtab_index_entry *ea = a;
20755 const struct symtab_index_entry *eb = b;
20756 int len = VEC_length (offset_type, ea->cu_indices);
20757 if (len != VEC_length (offset_type, eb->cu_indices))
20758 return 0;
20759 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20760 VEC_address (offset_type, eb->cu_indices),
20761 sizeof (offset_type) * len);
20762}
20763
20764/* Destroy a symtab_index_entry. */
b89be57b 20765
9291a0cd
TT
20766static void
20767delete_symtab_entry (void *p)
20768{
20769 struct symtab_index_entry *entry = p;
20770 VEC_free (offset_type, entry->cu_indices);
20771 xfree (entry);
20772}
20773
20774/* Create a hash table holding symtab_index_entry objects. */
b89be57b 20775
9291a0cd 20776static htab_t
3876f04e 20777create_symbol_hash_table (void)
9291a0cd
TT
20778{
20779 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20780 delete_symtab_entry, xcalloc, xfree);
20781}
20782
20783/* Create a new mapped symtab object. */
b89be57b 20784
9291a0cd
TT
20785static struct mapped_symtab *
20786create_mapped_symtab (void)
20787{
20788 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20789 symtab->n_elements = 0;
20790 symtab->size = 1024;
20791 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20792 return symtab;
20793}
20794
20795/* Destroy a mapped_symtab. */
b89be57b 20796
9291a0cd
TT
20797static void
20798cleanup_mapped_symtab (void *p)
20799{
20800 struct mapped_symtab *symtab = p;
20801 /* The contents of the array are freed when the other hash table is
20802 destroyed. */
20803 xfree (symtab->data);
20804 xfree (symtab);
20805}
20806
20807/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
20808 the slot.
20809
20810 Function is used only during write_hash_table so no index format backward
20811 compatibility is needed. */
b89be57b 20812
9291a0cd
TT
20813static struct symtab_index_entry **
20814find_slot (struct mapped_symtab *symtab, const char *name)
20815{
559a7a62 20816 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
20817
20818 index = hash & (symtab->size - 1);
20819 step = ((hash * 17) & (symtab->size - 1)) | 1;
20820
20821 for (;;)
20822 {
20823 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20824 return &symtab->data[index];
20825 index = (index + step) & (symtab->size - 1);
20826 }
20827}
20828
20829/* Expand SYMTAB's hash table. */
b89be57b 20830
9291a0cd
TT
20831static void
20832hash_expand (struct mapped_symtab *symtab)
20833{
20834 offset_type old_size = symtab->size;
20835 offset_type i;
20836 struct symtab_index_entry **old_entries = symtab->data;
20837
20838 symtab->size *= 2;
20839 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20840
20841 for (i = 0; i < old_size; ++i)
20842 {
20843 if (old_entries[i])
20844 {
20845 struct symtab_index_entry **slot = find_slot (symtab,
20846 old_entries[i]->name);
20847 *slot = old_entries[i];
20848 }
20849 }
20850
20851 xfree (old_entries);
20852}
20853
156942c7
DE
20854/* Add an entry to SYMTAB. NAME is the name of the symbol.
20855 CU_INDEX is the index of the CU in which the symbol appears.
20856 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 20857
9291a0cd
TT
20858static void
20859add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 20860 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
20861 offset_type cu_index)
20862{
20863 struct symtab_index_entry **slot;
156942c7 20864 offset_type cu_index_and_attrs;
9291a0cd
TT
20865
20866 ++symtab->n_elements;
20867 if (4 * symtab->n_elements / 3 >= symtab->size)
20868 hash_expand (symtab);
20869
20870 slot = find_slot (symtab, name);
20871 if (!*slot)
20872 {
20873 *slot = XNEW (struct symtab_index_entry);
20874 (*slot)->name = name;
156942c7 20875 /* index_offset is set later. */
9291a0cd
TT
20876 (*slot)->cu_indices = NULL;
20877 }
156942c7
DE
20878
20879 cu_index_and_attrs = 0;
20880 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20881 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20882 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20883
20884 /* We don't want to record an index value twice as we want to avoid the
20885 duplication.
20886 We process all global symbols and then all static symbols
20887 (which would allow us to avoid the duplication by only having to check
20888 the last entry pushed), but a symbol could have multiple kinds in one CU.
20889 To keep things simple we don't worry about the duplication here and
20890 sort and uniqufy the list after we've processed all symbols. */
20891 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20892}
20893
20894/* qsort helper routine for uniquify_cu_indices. */
20895
20896static int
20897offset_type_compare (const void *ap, const void *bp)
20898{
20899 offset_type a = *(offset_type *) ap;
20900 offset_type b = *(offset_type *) bp;
20901
20902 return (a > b) - (b > a);
20903}
20904
20905/* Sort and remove duplicates of all symbols' cu_indices lists. */
20906
20907static void
20908uniquify_cu_indices (struct mapped_symtab *symtab)
20909{
20910 int i;
20911
20912 for (i = 0; i < symtab->size; ++i)
20913 {
20914 struct symtab_index_entry *entry = symtab->data[i];
20915
20916 if (entry
20917 && entry->cu_indices != NULL)
20918 {
20919 unsigned int next_to_insert, next_to_check;
20920 offset_type last_value;
20921
20922 qsort (VEC_address (offset_type, entry->cu_indices),
20923 VEC_length (offset_type, entry->cu_indices),
20924 sizeof (offset_type), offset_type_compare);
20925
20926 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20927 next_to_insert = 1;
20928 for (next_to_check = 1;
20929 next_to_check < VEC_length (offset_type, entry->cu_indices);
20930 ++next_to_check)
20931 {
20932 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20933 != last_value)
20934 {
20935 last_value = VEC_index (offset_type, entry->cu_indices,
20936 next_to_check);
20937 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20938 last_value);
20939 ++next_to_insert;
20940 }
20941 }
20942 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20943 }
20944 }
9291a0cd
TT
20945}
20946
20947/* Add a vector of indices to the constant pool. */
b89be57b 20948
9291a0cd 20949static offset_type
3876f04e 20950add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
20951 struct symtab_index_entry *entry)
20952{
20953 void **slot;
20954
3876f04e 20955 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
20956 if (!*slot)
20957 {
20958 offset_type len = VEC_length (offset_type, entry->cu_indices);
20959 offset_type val = MAYBE_SWAP (len);
20960 offset_type iter;
20961 int i;
20962
20963 *slot = entry;
20964 entry->index_offset = obstack_object_size (cpool);
20965
20966 obstack_grow (cpool, &val, sizeof (val));
20967 for (i = 0;
20968 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20969 ++i)
20970 {
20971 val = MAYBE_SWAP (iter);
20972 obstack_grow (cpool, &val, sizeof (val));
20973 }
20974 }
20975 else
20976 {
20977 struct symtab_index_entry *old_entry = *slot;
20978 entry->index_offset = old_entry->index_offset;
20979 entry = old_entry;
20980 }
20981 return entry->index_offset;
20982}
20983
20984/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20985 constant pool entries going into the obstack CPOOL. */
b89be57b 20986
9291a0cd
TT
20987static void
20988write_hash_table (struct mapped_symtab *symtab,
20989 struct obstack *output, struct obstack *cpool)
20990{
20991 offset_type i;
3876f04e 20992 htab_t symbol_hash_table;
9291a0cd
TT
20993 htab_t str_table;
20994
3876f04e 20995 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 20996 str_table = create_strtab ();
3876f04e 20997
9291a0cd
TT
20998 /* We add all the index vectors to the constant pool first, to
20999 ensure alignment is ok. */
21000 for (i = 0; i < symtab->size; ++i)
21001 {
21002 if (symtab->data[i])
3876f04e 21003 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
21004 }
21005
21006 /* Now write out the hash table. */
21007 for (i = 0; i < symtab->size; ++i)
21008 {
21009 offset_type str_off, vec_off;
21010
21011 if (symtab->data[i])
21012 {
21013 str_off = add_string (str_table, cpool, symtab->data[i]->name);
21014 vec_off = symtab->data[i]->index_offset;
21015 }
21016 else
21017 {
21018 /* While 0 is a valid constant pool index, it is not valid
21019 to have 0 for both offsets. */
21020 str_off = 0;
21021 vec_off = 0;
21022 }
21023
21024 str_off = MAYBE_SWAP (str_off);
21025 vec_off = MAYBE_SWAP (vec_off);
21026
21027 obstack_grow (output, &str_off, sizeof (str_off));
21028 obstack_grow (output, &vec_off, sizeof (vec_off));
21029 }
21030
21031 htab_delete (str_table);
3876f04e 21032 htab_delete (symbol_hash_table);
9291a0cd
TT
21033}
21034
0a5429f6
DE
21035/* Struct to map psymtab to CU index in the index file. */
21036struct psymtab_cu_index_map
21037{
21038 struct partial_symtab *psymtab;
21039 unsigned int cu_index;
21040};
21041
21042static hashval_t
21043hash_psymtab_cu_index (const void *item)
21044{
21045 const struct psymtab_cu_index_map *map = item;
21046
21047 return htab_hash_pointer (map->psymtab);
21048}
21049
21050static int
21051eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21052{
21053 const struct psymtab_cu_index_map *lhs = item_lhs;
21054 const struct psymtab_cu_index_map *rhs = item_rhs;
21055
21056 return lhs->psymtab == rhs->psymtab;
21057}
21058
21059/* Helper struct for building the address table. */
21060struct addrmap_index_data
21061{
21062 struct objfile *objfile;
21063 struct obstack *addr_obstack;
21064 htab_t cu_index_htab;
21065
21066 /* Non-zero if the previous_* fields are valid.
21067 We can't write an entry until we see the next entry (since it is only then
21068 that we know the end of the entry). */
21069 int previous_valid;
21070 /* Index of the CU in the table of all CUs in the index file. */
21071 unsigned int previous_cu_index;
0963b4bd 21072 /* Start address of the CU. */
0a5429f6
DE
21073 CORE_ADDR previous_cu_start;
21074};
21075
21076/* Write an address entry to OBSTACK. */
b89be57b 21077
9291a0cd 21078static void
0a5429f6
DE
21079add_address_entry (struct objfile *objfile, struct obstack *obstack,
21080 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 21081{
0a5429f6 21082 offset_type cu_index_to_write;
948f8e3d 21083 gdb_byte addr[8];
9291a0cd
TT
21084 CORE_ADDR baseaddr;
21085
21086 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21087
0a5429f6
DE
21088 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21089 obstack_grow (obstack, addr, 8);
21090 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21091 obstack_grow (obstack, addr, 8);
21092 cu_index_to_write = MAYBE_SWAP (cu_index);
21093 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21094}
21095
21096/* Worker function for traversing an addrmap to build the address table. */
21097
21098static int
21099add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21100{
21101 struct addrmap_index_data *data = datap;
21102 struct partial_symtab *pst = obj;
0a5429f6
DE
21103
21104 if (data->previous_valid)
21105 add_address_entry (data->objfile, data->addr_obstack,
21106 data->previous_cu_start, start_addr,
21107 data->previous_cu_index);
21108
21109 data->previous_cu_start = start_addr;
21110 if (pst != NULL)
21111 {
21112 struct psymtab_cu_index_map find_map, *map;
21113 find_map.psymtab = pst;
21114 map = htab_find (data->cu_index_htab, &find_map);
21115 gdb_assert (map != NULL);
21116 data->previous_cu_index = map->cu_index;
21117 data->previous_valid = 1;
21118 }
21119 else
21120 data->previous_valid = 0;
21121
21122 return 0;
21123}
21124
21125/* Write OBJFILE's address map to OBSTACK.
21126 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21127 in the index file. */
21128
21129static void
21130write_address_map (struct objfile *objfile, struct obstack *obstack,
21131 htab_t cu_index_htab)
21132{
21133 struct addrmap_index_data addrmap_index_data;
21134
21135 /* When writing the address table, we have to cope with the fact that
21136 the addrmap iterator only provides the start of a region; we have to
21137 wait until the next invocation to get the start of the next region. */
21138
21139 addrmap_index_data.objfile = objfile;
21140 addrmap_index_data.addr_obstack = obstack;
21141 addrmap_index_data.cu_index_htab = cu_index_htab;
21142 addrmap_index_data.previous_valid = 0;
21143
21144 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21145 &addrmap_index_data);
21146
21147 /* It's highly unlikely the last entry (end address = 0xff...ff)
21148 is valid, but we should still handle it.
21149 The end address is recorded as the start of the next region, but that
21150 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21151 anyway. */
21152 if (addrmap_index_data.previous_valid)
21153 add_address_entry (objfile, obstack,
21154 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21155 addrmap_index_data.previous_cu_index);
9291a0cd
TT
21156}
21157
156942c7
DE
21158/* Return the symbol kind of PSYM. */
21159
21160static gdb_index_symbol_kind
21161symbol_kind (struct partial_symbol *psym)
21162{
21163 domain_enum domain = PSYMBOL_DOMAIN (psym);
21164 enum address_class aclass = PSYMBOL_CLASS (psym);
21165
21166 switch (domain)
21167 {
21168 case VAR_DOMAIN:
21169 switch (aclass)
21170 {
21171 case LOC_BLOCK:
21172 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21173 case LOC_TYPEDEF:
21174 return GDB_INDEX_SYMBOL_KIND_TYPE;
21175 case LOC_COMPUTED:
21176 case LOC_CONST_BYTES:
21177 case LOC_OPTIMIZED_OUT:
21178 case LOC_STATIC:
21179 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21180 case LOC_CONST:
21181 /* Note: It's currently impossible to recognize psyms as enum values
21182 short of reading the type info. For now punt. */
21183 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21184 default:
21185 /* There are other LOC_FOO values that one might want to classify
21186 as variables, but dwarf2read.c doesn't currently use them. */
21187 return GDB_INDEX_SYMBOL_KIND_OTHER;
21188 }
21189 case STRUCT_DOMAIN:
21190 return GDB_INDEX_SYMBOL_KIND_TYPE;
21191 default:
21192 return GDB_INDEX_SYMBOL_KIND_OTHER;
21193 }
21194}
21195
9291a0cd 21196/* Add a list of partial symbols to SYMTAB. */
b89be57b 21197
9291a0cd
TT
21198static void
21199write_psymbols (struct mapped_symtab *symtab,
987d643c 21200 htab_t psyms_seen,
9291a0cd
TT
21201 struct partial_symbol **psymp,
21202 int count,
987d643c
TT
21203 offset_type cu_index,
21204 int is_static)
9291a0cd
TT
21205{
21206 for (; count-- > 0; ++psymp)
21207 {
156942c7
DE
21208 struct partial_symbol *psym = *psymp;
21209 void **slot;
987d643c 21210
156942c7 21211 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 21212 error (_("Ada is not currently supported by the index"));
987d643c 21213
987d643c 21214 /* Only add a given psymbol once. */
156942c7 21215 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
21216 if (!*slot)
21217 {
156942c7
DE
21218 gdb_index_symbol_kind kind = symbol_kind (psym);
21219
21220 *slot = psym;
21221 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21222 is_static, kind, cu_index);
987d643c 21223 }
9291a0cd
TT
21224 }
21225}
21226
21227/* Write the contents of an ("unfinished") obstack to FILE. Throw an
21228 exception if there is an error. */
b89be57b 21229
9291a0cd
TT
21230static void
21231write_obstack (FILE *file, struct obstack *obstack)
21232{
21233 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21234 file)
21235 != obstack_object_size (obstack))
21236 error (_("couldn't data write to file"));
21237}
21238
21239/* Unlink a file if the argument is not NULL. */
b89be57b 21240
9291a0cd
TT
21241static void
21242unlink_if_set (void *p)
21243{
21244 char **filename = p;
21245 if (*filename)
21246 unlink (*filename);
21247}
21248
1fd400ff
TT
21249/* A helper struct used when iterating over debug_types. */
21250struct signatured_type_index_data
21251{
21252 struct objfile *objfile;
21253 struct mapped_symtab *symtab;
21254 struct obstack *types_list;
987d643c 21255 htab_t psyms_seen;
1fd400ff
TT
21256 int cu_index;
21257};
21258
21259/* A helper function that writes a single signatured_type to an
21260 obstack. */
b89be57b 21261
1fd400ff
TT
21262static int
21263write_one_signatured_type (void **slot, void *d)
21264{
21265 struct signatured_type_index_data *info = d;
21266 struct signatured_type *entry = (struct signatured_type *) *slot;
0186c6a7 21267 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
1fd400ff
TT
21268 gdb_byte val[8];
21269
21270 write_psymbols (info->symtab,
987d643c 21271 info->psyms_seen,
3e43a32a
MS
21272 info->objfile->global_psymbols.list
21273 + psymtab->globals_offset,
987d643c
TT
21274 psymtab->n_global_syms, info->cu_index,
21275 0);
1fd400ff 21276 write_psymbols (info->symtab,
987d643c 21277 info->psyms_seen,
3e43a32a
MS
21278 info->objfile->static_psymbols.list
21279 + psymtab->statics_offset,
987d643c
TT
21280 psymtab->n_static_syms, info->cu_index,
21281 1);
1fd400ff 21282
b64f50a1
JK
21283 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21284 entry->per_cu.offset.sect_off);
1fd400ff 21285 obstack_grow (info->types_list, val, 8);
3019eac3
DE
21286 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21287 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
21288 obstack_grow (info->types_list, val, 8);
21289 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21290 obstack_grow (info->types_list, val, 8);
21291
21292 ++info->cu_index;
21293
21294 return 1;
21295}
21296
95554aad
TT
21297/* Recurse into all "included" dependencies and write their symbols as
21298 if they appeared in this psymtab. */
21299
21300static void
21301recursively_write_psymbols (struct objfile *objfile,
21302 struct partial_symtab *psymtab,
21303 struct mapped_symtab *symtab,
21304 htab_t psyms_seen,
21305 offset_type cu_index)
21306{
21307 int i;
21308
21309 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21310 if (psymtab->dependencies[i]->user != NULL)
21311 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21312 symtab, psyms_seen, cu_index);
21313
21314 write_psymbols (symtab,
21315 psyms_seen,
21316 objfile->global_psymbols.list + psymtab->globals_offset,
21317 psymtab->n_global_syms, cu_index,
21318 0);
21319 write_psymbols (symtab,
21320 psyms_seen,
21321 objfile->static_psymbols.list + psymtab->statics_offset,
21322 psymtab->n_static_syms, cu_index,
21323 1);
21324}
21325
9291a0cd 21326/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 21327
9291a0cd
TT
21328static void
21329write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21330{
21331 struct cleanup *cleanup;
21332 char *filename, *cleanup_filename;
1fd400ff
TT
21333 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21334 struct obstack cu_list, types_cu_list;
9291a0cd
TT
21335 int i;
21336 FILE *out_file;
21337 struct mapped_symtab *symtab;
21338 offset_type val, size_of_contents, total_len;
21339 struct stat st;
987d643c 21340 htab_t psyms_seen;
0a5429f6
DE
21341 htab_t cu_index_htab;
21342 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 21343
9291a0cd
TT
21344 if (dwarf2_per_objfile->using_index)
21345 error (_("Cannot use an index to create the index"));
21346
8b70b953
TT
21347 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21348 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21349
260b681b
DE
21350 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21351 return;
21352
9291a0cd 21353 if (stat (objfile->name, &st) < 0)
7e17e088 21354 perror_with_name (objfile->name);
9291a0cd
TT
21355
21356 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21357 INDEX_SUFFIX, (char *) NULL);
21358 cleanup = make_cleanup (xfree, filename);
21359
614c279d 21360 out_file = gdb_fopen_cloexec (filename, "wb");
9291a0cd
TT
21361 if (!out_file)
21362 error (_("Can't open `%s' for writing"), filename);
21363
21364 cleanup_filename = filename;
21365 make_cleanup (unlink_if_set, &cleanup_filename);
21366
21367 symtab = create_mapped_symtab ();
21368 make_cleanup (cleanup_mapped_symtab, symtab);
21369
21370 obstack_init (&addr_obstack);
21371 make_cleanup_obstack_free (&addr_obstack);
21372
21373 obstack_init (&cu_list);
21374 make_cleanup_obstack_free (&cu_list);
21375
1fd400ff
TT
21376 obstack_init (&types_cu_list);
21377 make_cleanup_obstack_free (&types_cu_list);
21378
987d643c
TT
21379 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21380 NULL, xcalloc, xfree);
96408a79 21381 make_cleanup_htab_delete (psyms_seen);
987d643c 21382
0a5429f6
DE
21383 /* While we're scanning CU's create a table that maps a psymtab pointer
21384 (which is what addrmap records) to its index (which is what is recorded
21385 in the index file). This will later be needed to write the address
21386 table. */
21387 cu_index_htab = htab_create_alloc (100,
21388 hash_psymtab_cu_index,
21389 eq_psymtab_cu_index,
21390 NULL, xcalloc, xfree);
96408a79 21391 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
21392 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21393 xmalloc (sizeof (struct psymtab_cu_index_map)
21394 * dwarf2_per_objfile->n_comp_units);
21395 make_cleanup (xfree, psymtab_cu_index_map);
21396
21397 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
21398 work here. Also, the debug_types entries do not appear in
21399 all_comp_units, but only in their own hash table. */
9291a0cd
TT
21400 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21401 {
3e43a32a
MS
21402 struct dwarf2_per_cu_data *per_cu
21403 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 21404 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 21405 gdb_byte val[8];
0a5429f6
DE
21406 struct psymtab_cu_index_map *map;
21407 void **slot;
9291a0cd 21408
92fac807
JK
21409 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21410 It may be referenced from a local scope but in such case it does not
21411 need to be present in .gdb_index. */
21412 if (psymtab == NULL)
21413 continue;
21414
95554aad
TT
21415 if (psymtab->user == NULL)
21416 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 21417
0a5429f6
DE
21418 map = &psymtab_cu_index_map[i];
21419 map->psymtab = psymtab;
21420 map->cu_index = i;
21421 slot = htab_find_slot (cu_index_htab, map, INSERT);
21422 gdb_assert (slot != NULL);
21423 gdb_assert (*slot == NULL);
21424 *slot = map;
9291a0cd 21425
b64f50a1
JK
21426 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21427 per_cu->offset.sect_off);
9291a0cd 21428 obstack_grow (&cu_list, val, 8);
e254ef6a 21429 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
21430 obstack_grow (&cu_list, val, 8);
21431 }
21432
0a5429f6
DE
21433 /* Dump the address map. */
21434 write_address_map (objfile, &addr_obstack, cu_index_htab);
21435
1fd400ff
TT
21436 /* Write out the .debug_type entries, if any. */
21437 if (dwarf2_per_objfile->signatured_types)
21438 {
21439 struct signatured_type_index_data sig_data;
21440
21441 sig_data.objfile = objfile;
21442 sig_data.symtab = symtab;
21443 sig_data.types_list = &types_cu_list;
987d643c 21444 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
21445 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21446 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21447 write_one_signatured_type, &sig_data);
21448 }
21449
156942c7
DE
21450 /* Now that we've processed all symbols we can shrink their cu_indices
21451 lists. */
21452 uniquify_cu_indices (symtab);
21453
9291a0cd
TT
21454 obstack_init (&constant_pool);
21455 make_cleanup_obstack_free (&constant_pool);
21456 obstack_init (&symtab_obstack);
21457 make_cleanup_obstack_free (&symtab_obstack);
21458 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21459
21460 obstack_init (&contents);
21461 make_cleanup_obstack_free (&contents);
1fd400ff 21462 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
21463 total_len = size_of_contents;
21464
21465 /* The version number. */
796a7ff8 21466 val = MAYBE_SWAP (8);
9291a0cd
TT
21467 obstack_grow (&contents, &val, sizeof (val));
21468
21469 /* The offset of the CU list from the start of the file. */
21470 val = MAYBE_SWAP (total_len);
21471 obstack_grow (&contents, &val, sizeof (val));
21472 total_len += obstack_object_size (&cu_list);
21473
1fd400ff
TT
21474 /* The offset of the types CU list from the start of the file. */
21475 val = MAYBE_SWAP (total_len);
21476 obstack_grow (&contents, &val, sizeof (val));
21477 total_len += obstack_object_size (&types_cu_list);
21478
9291a0cd
TT
21479 /* The offset of the address table from the start of the file. */
21480 val = MAYBE_SWAP (total_len);
21481 obstack_grow (&contents, &val, sizeof (val));
21482 total_len += obstack_object_size (&addr_obstack);
21483
21484 /* The offset of the symbol table from the start of the file. */
21485 val = MAYBE_SWAP (total_len);
21486 obstack_grow (&contents, &val, sizeof (val));
21487 total_len += obstack_object_size (&symtab_obstack);
21488
21489 /* The offset of the constant pool from the start of the file. */
21490 val = MAYBE_SWAP (total_len);
21491 obstack_grow (&contents, &val, sizeof (val));
21492 total_len += obstack_object_size (&constant_pool);
21493
21494 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21495
21496 write_obstack (out_file, &contents);
21497 write_obstack (out_file, &cu_list);
1fd400ff 21498 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
21499 write_obstack (out_file, &addr_obstack);
21500 write_obstack (out_file, &symtab_obstack);
21501 write_obstack (out_file, &constant_pool);
21502
21503 fclose (out_file);
21504
21505 /* We want to keep the file, so we set cleanup_filename to NULL
21506 here. See unlink_if_set. */
21507 cleanup_filename = NULL;
21508
21509 do_cleanups (cleanup);
21510}
21511
90476074
TT
21512/* Implementation of the `save gdb-index' command.
21513
21514 Note that the file format used by this command is documented in the
21515 GDB manual. Any changes here must be documented there. */
11570e71 21516
9291a0cd
TT
21517static void
21518save_gdb_index_command (char *arg, int from_tty)
21519{
21520 struct objfile *objfile;
21521
21522 if (!arg || !*arg)
96d19272 21523 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
21524
21525 ALL_OBJFILES (objfile)
21526 {
21527 struct stat st;
21528
21529 /* If the objfile does not correspond to an actual file, skip it. */
21530 if (stat (objfile->name, &st) < 0)
21531 continue;
21532
21533 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21534 if (dwarf2_per_objfile)
21535 {
21536 volatile struct gdb_exception except;
21537
21538 TRY_CATCH (except, RETURN_MASK_ERROR)
21539 {
21540 write_psymtabs_to_index (objfile, arg);
21541 }
21542 if (except.reason < 0)
21543 exception_fprintf (gdb_stderr, except,
21544 _("Error while writing index for `%s': "),
21545 objfile->name);
21546 }
21547 }
dce234bc
PP
21548}
21549
9291a0cd
TT
21550\f
21551
9eae7c52
TT
21552int dwarf2_always_disassemble;
21553
21554static void
21555show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21556 struct cmd_list_element *c, const char *value)
21557{
3e43a32a
MS
21558 fprintf_filtered (file,
21559 _("Whether to always disassemble "
21560 "DWARF expressions is %s.\n"),
9eae7c52
TT
21561 value);
21562}
21563
900e11f9
JK
21564static void
21565show_check_physname (struct ui_file *file, int from_tty,
21566 struct cmd_list_element *c, const char *value)
21567{
21568 fprintf_filtered (file,
21569 _("Whether to check \"physname\" is %s.\n"),
21570 value);
21571}
21572
6502dd73
DJ
21573void _initialize_dwarf2_read (void);
21574
21575void
21576_initialize_dwarf2_read (void)
21577{
96d19272
JK
21578 struct cmd_list_element *c;
21579
dce234bc 21580 dwarf2_objfile_data_key
c1bd65d0 21581 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 21582
1bedd215
AC
21583 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21584Set DWARF 2 specific variables.\n\
21585Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21586 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21587 0/*allow-unknown*/, &maintenance_set_cmdlist);
21588
1bedd215
AC
21589 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21590Show DWARF 2 specific variables\n\
21591Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
21592 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21593 0/*allow-unknown*/, &maintenance_show_cmdlist);
21594
21595 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
21596 &dwarf2_max_cache_age, _("\
21597Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21598Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21599A higher limit means that cached compilation units will be stored\n\
21600in memory longer, and more total memory will be used. Zero disables\n\
21601caching, which can slow down startup."),
2c5b56ce 21602 NULL,
920d2a44 21603 show_dwarf2_max_cache_age,
2c5b56ce 21604 &set_dwarf2_cmdlist,
ae038cb0 21605 &show_dwarf2_cmdlist);
d97bc12b 21606
9eae7c52
TT
21607 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21608 &dwarf2_always_disassemble, _("\
21609Set whether `info address' always disassembles DWARF expressions."), _("\
21610Show whether `info address' always disassembles DWARF expressions."), _("\
21611When enabled, DWARF expressions are always printed in an assembly-like\n\
21612syntax. When disabled, expressions will be printed in a more\n\
21613conversational style, when possible."),
21614 NULL,
21615 show_dwarf2_always_disassemble,
21616 &set_dwarf2_cmdlist,
21617 &show_dwarf2_cmdlist);
21618
45cfd468
DE
21619 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21620Set debugging of the dwarf2 reader."), _("\
21621Show debugging of the dwarf2 reader."), _("\
21622When enabled, debugging messages are printed during dwarf2 reading\n\
21623and symtab expansion."),
21624 NULL,
21625 NULL,
21626 &setdebuglist, &showdebuglist);
21627
ccce17b0 21628 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
d97bc12b
DE
21629Set debugging of the dwarf2 DIE reader."), _("\
21630Show debugging of the dwarf2 DIE reader."), _("\
21631When enabled (non-zero), DIEs are dumped after they are read in.\n\
21632The value is the maximum depth to print."),
ccce17b0
YQ
21633 NULL,
21634 NULL,
21635 &setdebuglist, &showdebuglist);
9291a0cd 21636
900e11f9
JK
21637 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21638Set cross-checking of \"physname\" code against demangler."), _("\
21639Show cross-checking of \"physname\" code against demangler."), _("\
21640When enabled, GDB's internal \"physname\" code is checked against\n\
21641the demangler."),
21642 NULL, show_check_physname,
21643 &setdebuglist, &showdebuglist);
21644
e615022a
DE
21645 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21646 no_class, &use_deprecated_index_sections, _("\
21647Set whether to use deprecated gdb_index sections."), _("\
21648Show whether to use deprecated gdb_index sections."), _("\
21649When enabled, deprecated .gdb_index sections are used anyway.\n\
21650Normally they are ignored either because of a missing feature or\n\
21651performance issue.\n\
21652Warning: This option must be enabled before gdb reads the file."),
21653 NULL,
21654 NULL,
21655 &setlist, &showlist);
21656
96d19272 21657 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 21658 _("\
fc1a9d6e 21659Save a gdb-index file.\n\
11570e71 21660Usage: save gdb-index DIRECTORY"),
96d19272
JK
21661 &save_cmdlist);
21662 set_cmd_completer (c, filename_completer);
f1e6e072
TT
21663
21664 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21665 &dwarf2_locexpr_funcs);
21666 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21667 &dwarf2_loclist_funcs);
21668
21669 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21670 &dwarf2_block_frame_base_locexpr_funcs);
21671 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21672 &dwarf2_block_frame_base_loclist_funcs);
6502dd73 21673}
This page took 3.315453 seconds and 4 git commands to generate.